-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
192 lines (152 loc) · 5.84 KB
/
client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include "parse_json.h"
#include "mem.h"
#include "construct_packet.h"
#include "logger.h"
#define MAXDATA 1024
#define IP_MTU_DISCOVER 10
#define IP_PMTUDISC_DO 2
int main (int argc, char **argv) {
struct json config;
int socketFd, val, addressLen, udpFd, df;
unsigned int packetId = 0;
struct sockaddr_in client, cli, address, clientSrc;
uint8_t *randomData, *randomData2;
char data[MAXDATA] = {0};
/* Pre Probing phase */
if (argc != 2) {
fprintf(stderr, "Usage: ./compdetectclient <config file>\n");
return EXIT_FAILURE;
}
/* Read Config */
read_json(&config, argv[1], data);
/* Set up socket connection */
socketFd = socket(AF_INET, SOCK_STREAM, 0);
if (socketFd == -1) {
fprintf(stderr, "Failed to set up socket connection");
exit(EXIT_FAILURE);
}
bzero(&client, sizeof(client));
client.sin_family = AF_INET;
client.sin_addr.s_addr = inet_addr(config.serverIp);
client.sin_port = htons(atoi(config.tcpPort));
/* Try Connecting to server */
if (connect(socketFd, (struct sockaddr *) &client, sizeof(client)) != 0) {
fprintf(stderr, "Failure to Connect to Server");
return EXIT_FAILURE;
} else {
LOGP("Connection Successful!! :)\n");
}
/* Attempt to send data to server */
if (send(socketFd, data, MAXDATA - 1, 0) == -1) {
fprintf(stderr, "Unable to send data! Exiting...\n");
return EXIT_FAILURE;
}
/* Check if we can receive data from server after sending */
char success[8];
memset(success, 0, 8);
if (recv(socketFd, success, 8, 0) == -1) {
fprintf(stderr, "Error reciving confirmation from server. Exiting...\n");
return EXIT_FAILURE;
}
if (strncmp(success, "Success", 7) != 0) {
fprintf(stderr, "PreProbing Failed!\n");
return EXIT_FAILURE;
}
/* If we are up to this point pre-probing phase is complete; close the TCP connection */
close(socketFd);
/* Probing Phase - Send UDP Train */
udpFd = socket(AF_INET, SOCK_DGRAM, 0);
if (udpFd < 0) {
fprintf(stderr, "Failure setting up UDP Socket :(\n");
return EXIT_FAILURE;
}
/* Set up struct for sending packets to server */
bzero(&address, sizeof(address));
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr(config.serverIp);
address.sin_port = htons(atoi(config.dstPortUdp));
/* Set up struct for recieving packets from any source once we bind to the socket */
memset(&clientSrc, 0, sizeof(clientSrc));
clientSrc.sin_family = AF_INET;
clientSrc.sin_addr.s_addr = htonl(INADDR_ANY); //why INADDR_ANY? in the bind call, the socket will be bound to all local interfaces
clientSrc.sin_port = htons(atoi(config.srcPortUdp));
df = IP_PMTUDISC_DO;
/* Set up Don't Fragment Bit see https://www.programmersought.com/article/48555483026/ for approach */
if (setsockopt(udpFd, IPPROTO_IP, IP_MTU_DISCOVER, &df, sizeof(df)) == -1) {
fprintf(stderr, "Don't Fragment bit not set :(\n");
return EXIT_FAILURE;
}
/* Bind Socket */
if (bind(udpFd, (struct sockaddr *) &clientSrc, sizeof(clientSrc)) == -1) {
fprintf(stderr, "Failed to Bind to socket :(\n");
return EXIT_FAILURE;
}
/* Connect Socket */
if (connect(udpFd, (struct sockaddr *) &address, sizeof(address)) == -1) {
fprintf(stderr, "Failed to connect to socket :(\n");
return EXIT_FAILURE;
}
/* IF WE HAVE MADE IT THIS FAR WE CAN START SENDING PACKETS OVER!!! WOOT WOOT */
/* Send low entropy packets */
randomData = unsgnintmem(config.payloadSize);
randomData2 = unsgnintmem(config.payloadSize);
entropy(&randomData2[16], config.payloadSize - 16);
addressLen = sizeof(address);
sendto(udpFd, randomData, config.payloadSize, 0, (struct sockaddr *) &address, addressLen);
/*Send Low Entropy Data*/
for (int i = 0; i < config.numPackets; i++) {
set_up_packet(randomData, packetId++);
if (sendto(udpFd, randomData, config.payloadSize, 0, (struct sockaddr *) &address, addressLen) <= 0) {
continue;
}
}
sleep(config.msrTime);
/* Send High Entropy Data */
for (int i = 0; i < config.numPackets; i++) {
set_up_packet(randomData2, packetId++);
if (sendto(udpFd, randomData2, config.payloadSize, 0, (struct sockaddr *) &address, addressLen) <= 0) {
continue;
}
}
printf("Packets Sent, closing connection in 3, 2, 1 ...\n");
close(udpFd);
sleep(10);
/* IF WE HAVE MADE IT THIS FAR WE ARE IN PHASE 3 */
struct sockaddr_in serverAddress;
//create another socket to report the findings of the server
socketFd = socket(AF_INET, SOCK_STREAM, 0);
if (socketFd == -1) {
fprintf(stderr, "Could not set up socket :(\n");
return EXIT_FAILURE;
}
bzero(&serverAddress, sizeof(serverAddress));
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = inet_addr(config.serverIp);
serverAddress.sin_port = htons(8082);
//time to connect client socket to server socket to see if there is any compression
if (connect(socketFd, (struct sockaddr *) &serverAddress, sizeof(serverAddress)) == -1) {
fprintf(stderr, "Could not connect to Server :(\n");
return EXIT_FAILURE;
}
memset(&data, 0, MAXDATA);
send(socketFd, data, MAXDATA, 0);
memset(&data, 0, MAXDATA);
recv(socketFd, data, MAXDATA, 0);
close(socketFd);
//Did we have compression???
printf("%s\n", data);
free(randomData);
free(randomData2);
return 0;
}