This repository has been archived by the owner. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
522 lines (457 loc) · 16.4 KB
/
main.c
File metadata and controls
522 lines (457 loc) · 16.4 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
// Made by AlexVanin
#include "n_packet.h"
#include "n_plan.h"
#include "rand_exp.h"
#include <stdio.h>
#include <getopt.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <malloc.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include <netinet/tcp.h>
#include <netinet/ether.h>
#define eexit(text) printf((text)); exit(1);
const unsigned long long nano = 1000000000;
char* init_packet(int sockfd, char* if_name, char* ip_s, uint16_t port_s, char* ip_d, uint16_t port_d, protocol p_proto,
uint8_t eth_s[6], uint8_t eth_d[6], struct sockaddr_ll * sockaddr, uint16_t* h_size)
{
struct ifreq if_idx;
struct ifreq if_mac;
struct ifreq if_ip;
struct ether_header *eh;
struct iphdr *iph;
struct udphdr *udph;
struct tcphdr *tcph;
uint16_t tx_len = 0;
char* p_buf = malloc(BUF_SIZ);
eh = (struct ether_header *) p_buf;
iph = (struct iphdr *) (p_buf + sizeof(struct ether_header));
udph = (struct udphdr *) (p_buf + sizeof(struct iphdr) + sizeof(struct ether_header));
tcph = (struct tcphdr *) (p_buf + sizeof(struct iphdr) + sizeof(struct ether_header));
/* Get the index of the interface to send on */
memset(&if_idx, 0, sizeof(struct ifreq));
strncpy(if_idx.ifr_name, if_name, IFNAMSIZ-1);
if (ioctl(sockfd, SIOCGIFINDEX, &if_idx) < 0) {
perror("SIOCGIFINDEX");
exit(1);
}
/* Get the MAC address of the interface to send on */
memset(&if_mac, 0, sizeof(struct ifreq));
strncpy(if_mac.ifr_name, if_name, IFNAMSIZ-1);
if (ioctl(sockfd, SIOCGIFHWADDR, &if_mac) < 0) {
perror("SIOCGIFHWADDR");
exit(1);
}
/* Get the IP address of the interface to send on */
memset(&if_ip, 0, sizeof(struct ifreq));
strncpy(if_ip.ifr_name, if_name, IFNAMSIZ-1);
if (ioctl(sockfd, SIOCGIFADDR, &if_ip) < 0) {
perror("SIOCGIFADDR");
exit(1);
}
/* Construct the Ethernet header */
//memset(sendbuf, 0, BUF_SIZ);
if (eth_s == NULL) {
eh->ether_shost[0] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[0];
eh->ether_shost[1] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[1];
eh->ether_shost[2] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[2];
eh->ether_shost[3] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[3];
eh->ether_shost[4] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[4];
eh->ether_shost[5] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[5];
}
else {
eh->ether_shost[0] = eth_s[0];
eh->ether_shost[1] = eth_s[1];
eh->ether_shost[2] = eth_s[2];
eh->ether_shost[3] = eth_s[3];
eh->ether_shost[4] = eth_s[4];
eh->ether_shost[5] = eth_s[5];
}
if (eth_d == NULL) {
eh->ether_dhost[0] = MY_DEST_MAC0;
eh->ether_dhost[1] = MY_DEST_MAC1;
eh->ether_dhost[2] = MY_DEST_MAC2;
eh->ether_dhost[3] = MY_DEST_MAC3;
eh->ether_dhost[4] = MY_DEST_MAC4;
eh->ether_dhost[5] = MY_DEST_MAC5;
}
else {
eh->ether_dhost[0] = eth_d[0];
eh->ether_dhost[1] = eth_d[1];
eh->ether_dhost[2] = eth_d[2];
eh->ether_dhost[3] = eth_d[3];
eh->ether_dhost[4] = eth_d[4];
eh->ether_dhost[5] = eth_d[5];
}
eh->ether_type = htons(ETH_P_IP);
tx_len += sizeof(struct ether_header);
iph->ihl = 5;
iph->version = 4;
iph->tos = 16; // Low delay
iph->id = htons(54321);
iph->ttl = IPDEFTTL; // hops
iph->protocol = p_proto;
if (ip_s == NULL) iph->saddr = inet_addr(inet_ntoa(((struct sockaddr_in *)&if_ip.ifr_addr)->sin_addr));
else {
iph->saddr = inet_addr(ip_s);
if (iph->saddr == -1) {
eexit("Cannot parse ipS\n");
}
}
if (ip_d == NULL) iph->daddr = inet_addr(MY_DEST_IP);
else {
iph->daddr = inet_addr(ip_d);
if (iph->daddr == -1) {
eexit("Cannot parse ipD\n");
}
}
tx_len += sizeof(struct iphdr);
if (p_proto == UDP) {
if (port_s == 0) udph->source = htons(MY_SRC_PORT);
else udph->source = htons(port_s);
if (port_d == 0) udph->dest = htons(MY_DEST_PORT);
else udph->dest = htons(port_d);
udph->check = 0;
tx_len += sizeof(struct udphdr);
}
else if (p_proto == TCP) {
if (port_s == 0)
tcph->source = htons(MY_SRC_PORT);
else
tcph->source = htons(port_s);
if (port_d == 0)
tcph->dest = htons(MY_DEST_PORT);
else
tcph->dest = htons(port_d);
tcph->seq = htons(MY_SEQ_NUMBER); // inital sequence number
tcph->ack_seq = htons(0); // acknowledgement number
tcph->ack = 0; // acknowledgement flag
tcph->syn = 1; // synchronize flag
tcph->rst = 0; // reset flag
tcph->psh = 0; // push flag
tcph->fin = 0; // finish flag
tcph->urg = 0; // urgent flag
tcph->check = 0; // tcp checksum
tcph->doff = 5; // data offset
tcph->res1 = 0;
tcph->res2 = 0;
tx_len += sizeof(struct tcphdr);
}
/* Index of the network device */
sockaddr->sll_ifindex = if_idx.ifr_ifindex;
/* Address length*/
sockaddr->sll_halen = ETH_ALEN;
/* Destination MAC */
sockaddr->sll_addr[0] = MY_DEST_MAC0;
sockaddr->sll_addr[1] = MY_DEST_MAC1;
sockaddr->sll_addr[2] = MY_DEST_MAC2;
sockaddr->sll_addr[3] = MY_DEST_MAC3;
sockaddr->sll_addr[4] = MY_DEST_MAC4;
sockaddr->sll_addr[5] = MY_DEST_MAC5;
*h_size = tx_len;
return p_buf;
}
int destroy_packet(char* buf)
{
free( buf );
return 0;
}
int fill_dataunit(t_dataunit* d_unit, char* p_buf, uint16_t h_size, uint16_t p_size, protocol p_proto)
{
struct iphdr *iph = (struct iphdr *) (p_buf + sizeof(struct ether_header));
struct udphdr *udph = (struct udphdr *) (p_buf + sizeof(struct ether_header)+sizeof(struct iphdr));
if (h_size > p_size) { eexit("Header size is more than packet size, plaese specify right params\n") }
d_unit->p_size = p_size;
d_unit->p_buf = p_buf;
d_unit->p_proto = p_proto;
d_unit->p_ip_size = calc_ip_size(p_size);
iph->tot_len = htons(d_unit->p_ip_size);
d_unit->p_ip_csum = calc_ip_csum(p_buf);
if (p_proto == UDP)
{
d_unit->p_tr_size = calc_udp_size(p_size);
udph->len = htons(d_unit->p_tr_size);
d_unit->p_tr_csum = calc_tr_csum(p_buf, p_size);
}
else if (p_proto == TCP)
{
d_unit->p_tr_csum = calc_tr_csum(p_buf, p_size);
}
return 0;
}
int extract_dataunit(t_dataunit* d_unit)
{
struct iphdr *iph = (struct iphdr*)(d_unit->p_buf + sizeof(struct ether_header));
struct udphdr *udph = (struct udphdr*)(d_unit->p_buf + sizeof(struct ether_header)+sizeof(struct iphdr));
struct tcphdr *tcph = (struct tcphdr*)(d_unit->p_buf + sizeof(struct ether_header)+sizeof(struct iphdr));
iph->tot_len = htons(d_unit->p_ip_size);
iph->check = d_unit->p_ip_csum;
if (d_unit->p_proto == UDP) {
udph->len = htons(d_unit->p_tr_size);
udph->check = d_unit->p_tr_csum;
}
else {
tcph->check = d_unit->p_tr_csum;
}
return 0;
}
t_planunit* creat_plan(char* p_buf, uint16_t h_size, t_metaunit* m_plan, uint32_t p_cnt, protocol p_proto)
{
t_planunit* plan = malloc(sizeof(t_planunit) * p_cnt);
for (int i = 0; i < p_cnt; i++) {
plan[i].time = m_plan[i].p_delay;
fill_dataunit(&(plan[i].d_unit), p_buf, h_size, m_plan[i].p_size, p_proto);
}
return plan;
}
int exec_plan(t_planunit* plan, uint32_t p_cnt, int sockfd, struct sockaddr_ll* sockaddr)
{
struct timespec tm;
uint64_t t1, t2;
t1 = 0; t2 = 0;
int64_t t3;
for (int i = 0; i < p_cnt; i++) {
extract_dataunit(&(plan[i].d_unit));
if (i != 0) {
clock_gettime( CLOCK_REALTIME, &tm );
t1 = tm.tv_nsec + tm.tv_sec * nano;
t3 = (int64_t)(plan[i-1].time) - (int64_t)((t1 - t2)/1000);
if (t3 - 700 > 0) //Magic Number 600
usleep(t3-700);
}
if (sendto(sockfd, plan[i].d_unit.p_buf, plan[i].d_unit.p_size, 0, (struct sockaddr*)sockaddr, sizeof(struct sockaddr_ll)) < 0)
printf("Send failed\n");
clock_gettime( CLOCK_REALTIME, &tm );
t2 = tm.tv_nsec + tm.tv_sec * nano;
}
return 0;
}
int destroy_plan(t_planunit* plan)
{
free( plan );
return 0;
}
int main (int argc, char* argv[])
{
int sockfd;
uint16_t h_size;
struct sockaddr_ll sockaddr;
t_metaunit* m_plan;
t_planunit* plan;
char* p_buff;
static protocol p_proto = 0;
static int distribution = 0;
static char my_eth = 1;
char* if_name = NULL;
uint32_t p_cnt = 0;
uint8_t eth_s[6];
uint8_t eth_d[6] = { MY_DEST_MAC0, MY_DEST_MAC1, MY_DEST_MAC2,
MY_DEST_MAC3, MY_DEST_MAC4, MY_DEST_MAC5};
char *ip_s = NULL;
char *ip_d = NULL;
uint16_t port_s = 0;
uint32_t port_d = 0;
char* t_file_n = NULL;
char* s_file_n = NULL;
FILE* t_file = NULL;
FILE* s_file = NULL;
uint16_t s_a = 0, s_b = 0;
uint32_t t_a = 0, t_b = 0;
double s_mu = 0.0, t_mu = 0.0;
int c;
while ( 1 )
{
static struct option long_options[] =
{
{"UDP", no_argument, (void*)&p_proto, UDP},
{"TCP", no_argument, (void*)&p_proto, TCP},
{"exponential", no_argument, (void*)&distribution, EXPONEN_D},
{"flat", no_argument, (void*)&distribution, FLAT_D},
{"custom", no_argument, (void*)&distribution, CUSTOM_D},
{"iface", required_argument, 0, 'a'},
{"count", required_argument, 0, 'b'},
{"ethS", required_argument, 0, 'c'},
{"ethD", required_argument, 0, 'd'},
{"ipS", required_argument, 0, 'e'},
{"ipD", required_argument, 0, 'f'},
{"portS", required_argument, 0, 'g'},
{"portD", required_argument, 0, 'h'},
{"minT", required_argument, 0, 'i'},
{"maxT", required_argument, 0, 'j'},
{"avgT", required_argument, 0, 'k'},
{"minS", required_argument, 0, 'l'},
{"maxS", required_argument, 0, 'm'},
{"avgS", required_argument, 0, 'n'},
{"fileT", required_argument, 0, 'o'},
{"fileS", required_argument, 0, 'p'},
{0, 0, 0, 0}
};
int option_index = 0;
c = getopt_long_only(argc, argv, "", long_options, &option_index);
if (c == -1)
break;
switch (c)
{
case 0:
break;
case 'a':
if_name = optarg;
break;
case 'b':
p_cnt = atoi(optarg);
if (!p_cnt) { eexit("Bad Package Count\n") }
break;
case 'c':
my_eth = 0;
if( 6 != sscanf( optarg, "%x:%x:%x:%x:%x:%x",
(unsigned int*)ð_s[0], (unsigned int*)ð_s[1], (unsigned int*)ð_s[2],
(unsigned int*)ð_s[3], (unsigned int*)ð_s[4], (unsigned int*)ð_s[5] ) )
{ eexit("Bad Source Mac Address\n") }
break;
case 'd':
if( 6 != sscanf( optarg, "%x:%x:%x:%x:%x:%x",
(unsigned int*)ð_d[0], (unsigned int*)ð_d[1], (unsigned int*)ð_d[2],
(unsigned int*)ð_d[3], (unsigned int*)ð_d[4], (unsigned int*)ð_d[5] ) )
{ eexit("Bad Destination Mac Address\n") }
break;
case 'e':
ip_s = optarg;
break;
case 'f':
ip_d = optarg;
break;
case 'g':
if (atoi(optarg) > 65535 || atoi(optarg) <= 0)
{ eexit("Bad Source Port\n") }
else port_s = atoi(optarg);
break;
case 'h':
if (atoi(optarg) > 65535 || atoi(optarg) <= 0)
{ eexit("Bad Destination Port\n") }
else port_d = atoi(optarg);
break;
case 'i':
t_a = atoi(optarg) * 1000;
break;
case 'j':
t_b = atoi(optarg) * 1000;
break;
case 'k':
t_mu = atof(optarg) * 1000;
break;
case 'l':
s_a = atoi(optarg);
break;
case 'm':
s_b = atoi(optarg);
break;
case 'n':
s_mu = atof(optarg);
break;
case 'o':
t_file_n = optarg;
break;
case 'p':
s_file_n = optarg;
break;
default:
break;
}
}
if (p_proto == 0) { eexit("Protocol not specified\n") }
if (distribution == 0) { eexit("Distribution not specified\n") }
if (if_name == NULL) { eexit("No interface specified\n") }
if (p_cnt == 0) { eexit("No package to send\n") }
if ((ip_s == NULL) || (ip_d == NULL)) { eexit("IP-adresses not specified\n") }
if (distribution == FLAT_D) {
if ( (t_mu != 0.0) && (t_a == 0) && (t_b == 0) && (t_file_n == NULL) ) {
t_a = (uint32_t)t_mu;
t_mu = 0.0;
}
else { eexit("Wrong time paramaters combination for flat distribution\n") }
if ( (s_mu != 0.0) && (s_a == 0) && (s_b == 0) && (s_file_n == NULL) ) {
s_a = (uint32_t)s_mu;
s_mu = 0.0;
}
else { eexit("Wrong size paramaters combination for flat distribution\n") }
}
else if (distribution == EXPONEN_D) {
if ( (t_mu != 0.0) && (t_a == 0) && (t_b == 0) && (t_file_n == NULL) ) {
t_a = (uint32_t)t_mu;
t_mu = 0.0;
}
else if ( (t_mu != 0.0) && (t_a != 0) && (t_b != 0) && (t_file_n == NULL) ) {
if (t_a >= t_b) { eexit("Wrong time params\n") }
if (((uint32_t)t_mu <= t_a) || ((uint32_t)t_mu >= t_b))
{ eexit("Wrong avg time\n") }
}
else { eexit("Wrong time paramaters combination for exponential distribution\n") }
if ( (s_mu != 0.0) && (s_a == 0) && (s_b == 0) && (s_file_n == NULL) ) {
s_a = (uint32_t)s_mu;
s_mu = 0.0;
}
else if ( (s_mu != 0.0) && (s_a != 0) && (s_b != 0) && (s_file_n == NULL) ) {
if (s_a >= s_b) { eexit("Wrong time params\n") }
if (((uint32_t)s_mu <= s_a) || ((uint32_t)s_mu >= s_b))
{ eexit("Wrong avg time\n") }
}
else { eexit("Wrong size paramaters combination for exponential distribution\n") }
}
else if (distribution == CUSTOM_D) {
if ( (t_mu != 0.0) && (t_a == 0) && (t_b == 0) && (t_file_n == NULL) ) {
t_a = (uint32_t)t_mu;
t_mu = 0.0;
}
else if ( (t_mu != 0.0) && (t_a != 0) && (t_b != 0) && (t_file_n == NULL) ) {
if (t_a >= t_b) { eexit("Wrong time params\n") }
if (((uint32_t)t_mu <= t_a) || ((uint32_t)t_mu >= t_b))
{ eexit("Wrong avg time\n") }
}
else if ( (t_mu == 0.0) && (t_a != 0) && (t_b != 0) && (t_file_n != NULL) ) {
if (t_a >= t_b) { eexit("Wrong time params\n") }
}
else { eexit("Wrong time paramaters combination for custom distribution\n") }
if ( (s_mu != 0.0) && (s_a == 0) && (s_b == 0) && (s_file_n == NULL) ) {
s_a = (uint32_t)s_mu;
s_mu = 0.0;
}
else if ( (s_mu != 0.0) && (s_a != 0) && (s_b != 0) && (s_file_n == NULL) ) {
if (s_a >= s_b) { eexit("Wrong time params\n") }
if (((uint32_t)s_mu <= s_a) || ((uint32_t)s_mu >= s_b))
{ eexit("Wrong avg time\n") }
}
else if ( (s_mu == 0.0) && (s_a != 0) && (s_b != 0) && (s_file_n != NULL) ) {
if (s_a >= s_b) { eexit("Wrong time params\n") }
}
else { eexit("Wrong size paramaters combination for custom distribution\n") }
}
if ((sockfd = socket(AF_PACKET, SOCK_RAW, IPPROTO_RAW)) == -1) {
perror("Cannot create Socket\n");
exit(1);
}
if (t_file_n != NULL) t_file = fopen(t_file_n, "r");
if (s_file_n != NULL) s_file = fopen(s_file_n, "r");
if (my_eth) p_buff = init_packet(sockfd, if_name, ip_s, port_s, ip_d, port_d, p_proto, NULL, eth_d, &sockaddr, &h_size);
else p_buff = init_packet(sockfd, if_name, ip_s, port_s, ip_d, port_d, p_proto, eth_s, eth_d, &sockaddr, &h_size);
m_plan = cr_meta(p_cnt, s_a, s_b, s_mu, t_a, t_b, t_mu, s_file, t_file);
plan = creat_plan(p_buff, h_size, m_plan, p_cnt, p_proto);
/* for (int i = 0; i < p_cnt; i++)
{
printf("%d\n",plan[i].d_unit.p_size);
}
return 0; */
exec_plan(plan, p_cnt, sockfd, &sockaddr);
destroy_meta(m_plan);
destroy_plan(plan);
if (t_file != NULL) fclose(t_file);
if (s_file != NULL) fclose(s_file);
return 0;
}