forked from shanereetz/Pong
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pong.cpp
989 lines (863 loc) · 34 KB
/
pong.cpp
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
/*
Pong
Using the Internet Control Message Protocol (ICMP) ECHO_REQUEST
and ECHO_REPLY messages to measure round-trip-delays and packet
loss across network paths. Eventually, we'd like to expand
this to UDP pinging as well.
Created as a 2013 NDSU Capstone Project with specifications
and requirements provided by Ericsson.
An extension/reimplementation of the original ping.c for the 21st
century.
Emphasis is on readability and ease of understanding the code.
Inspiration and design inspired by:
Mike Muuss
U. S. Army Ballistic Research Laboratory
December, 1983
Modified at UC Berkeley
*/
/*
Imports and definitions
*/
#define IP_MINLENGTH 34
#define ICMP_MINLENGTH 16
#define DEFAULT_LISTEN_TIMEOUT 2
#include <omp.h>
#include <math.h>
#include <iostream>
#include <random>
#include <fstream>
#include <sys/param.h>
#include <sys/file.h>
#include <sys/time.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include <time.h>
#include <sys/time.h>
#include <sys/types.h>
#ifdef __MACH__
#include <mach/clock.h>
#include <mach/mach.h>
#endif
#if __unix__||__APPLE__
#include <arpa/inet.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <netdb.h>
#include <signal.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#elif WIN32
#include <windows.h>
#include <winsock2.h>
struct sockaddr_in *socketAddress;
/* ICMP Header for Windows branch */
struct icmp {
BYTE icmp_type; // ICMP packet type
BYTE icmp_code; // Type sub code
USHORT icmp_cksum;
USHORT icmp_id;
USHORT icmp_seq;
ULONG icmp_data; // not part of ICMP, but we need it
};
#endif
/*
Initialize Structs
*/
struct sockaddr_in *whereto;
struct in_addr destIP;
struct in_addr srcIP;
struct icmp * icmpHeader;
struct icmp * receivedICMPHeader;
struct icmp * receivedIPHeader;
struct ip * ipHeader;
u_char * packet[100];
u_char * packetIP[100];
/*
Global Variables for stats and keeping track
of how many pings were sent, how many need
to be excluded, and more.
*/
int sent;
int packetsTimedOut = 0;
int processID;
int icmpPayloadLength = 30;
int pingsToSend = 5;
int pingsSent = 0;
int pingsToExclude = 0;
double totalResponseTime = 0.0;
double sumOfResponseTimesSquared = 0.0;
double roundTripTime = 0.0;
int pingsReceived = 0;
bool excludingPing;
int timeoutInput = DEFAULT_LISTEN_TIMEOUT;
volatile bool killSwitch = true;
/*
Variables for CSV file
*/
using namespace std;
ofstream csvOutput;
string csvFileName = "output.csv";
/*
Time settings
*/
#if __MACH__
clock_serv_t cclock;
mach_timespec_t sentTime, receivedTime;
#elif __WINDOWS__
#elif __GNUC__
struct timespec sentTime2, receivedTime2;
#endif
/*
Checksum()
Simple checksum function for ICMP Header. This implentation was
adapted from Mike Musss' version of ping.c
*/
static u_short checksum(u_short *ICMPHeader, int len)
{
register int nleft = len;
register u_short *ICMPPointer = ICMPHeader;
register int sum = 0;
u_short answer = 0;
/*
Our algorithm is simple, using a 32 bit accumulator (sum), we add
sequential 16 bit words to it, and at the end, fold back all the
carry bits from the top 16 bits into the lower 16 bits.
*/
while (nleft > 1)
{
sum += *ICMPPointer;
ICMPPointer++;
nleft -= 2;
}
/* Mop up an odd byte, if necessary */
if (nleft == 1)
{
*(u_char *)(&answer) = *(u_char *) ICMPPointer;
sum += answer;
}
/* Add back carry outs from top 16 bits to low 16 bits */
sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
sum += (sum >> 16); /* add carry */
answer = (u_short)~sum; /* truncate to 16 bits */
return(answer);
}
/*
pingICMP()
This method actually sends our ECHO_REQUEST across the internet.
It computes the ICMP checksum and sends the packet to the address
specified in main().
*/
void pingICMP(int socketDescriptor, int icmpPayloadLength)
{
/* Get time, put it in the packet */
/* Set time sent */
#ifdef __MACH__
host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
clock_get_time(cclock, (mach_timespec_t *)icmpHeader->icmp_data);
mach_port_deallocate(mach_task_self(), cclock);
#elif __WINDOWS__
// GetTick64Count()
#elif __GNUC__
clock_gettime(CLOCK_MONOTONIC, (struct timespec *)icmpHeader->icmp_data);
#else
//clock_gettime(CLOCK_REALTIME, &ts);
#endif
/* Compute checksum */
icmpHeader->icmp_cksum = 0;
icmpHeader->icmp_cksum = checksum((u_short *)icmpHeader, sizeof(*icmpHeader));
/* Try to send the packet */
sent = sendto(socketDescriptor, packet, icmpPayloadLength + 8, 0, (struct sockaddr *)whereto, sizeof(struct sockaddr));
/* Check if the packet sent or not */
if(sent > 0)
{
pingsSent++;
/* Increment the packet sequence number */
icmpHeader->icmp_seq++;
}
else
{
printf("Ping not sent.\n\n");
}
}
/*
report()
This function reports the final statistics, either to the command line
or to a CSV (comma separated value) file. Current statistics and output
information includes pings sent, pings received, pings dropped, pings excluded,
average response time, and standard deviation for response time.
*/
void report()
{
if(pingsSent != 0)
{
printf("----------------------------------------------------------------\n");
printf("%d packets sent, %d dropped", pingsSent, (pingsSent - pingsReceived));
if(excludingPing)
{
printf(", %d excluded from summary\n", pingsToExclude);
}
else
{
printf("\n");
}
double average = totalResponseTime / (pingsSent - pingsToExclude);
printf("Stats avg/stddev : %f / %f\n", average, sqrt((sumOfResponseTimesSquared / (pingsReceived - pingsToExclude)) - (average * average)));
printf("----------------------------------------------------------------\n");
}
}
/*
listenICMP()
This function waits from an incoming packet, checks to see if it is our ECHO_REPLY,
and then gets data out of the packet and computes statistics or doesn't (depending
on if we're excluding that response or not).
*/
void listenICMP(int socketDescriptor, sockaddr_in * fromWhom, bool quiet, bool excludingPings, int timeoutLength)
{
/* Setting some variables needed for select() and our file descriptor */
char receivedPacketBuffer[512];
fd_set readfds;
FD_SET(socketDescriptor, &readfds);
struct timeval timeout;
timeout.tv_sec = timeoutLength; // timeout period, seconds (added second, if that matters)
//printf("Timeout length")
timeout.tv_usec = 0; // timeuot period, microseconds 1,000,000 micro = second
// TODO Make this timeout dependent on how many pings have been sent...
int selectStatus = select(socketDescriptor+1, &readfds, NULL, NULL, &timeout);
socklen_t fromWhomLength = sizeof(fromWhom);
if(selectStatus == -1)
{
printf("LISTEN: Error in select()\n");
}
else if(selectStatus == 0)
{
printf("I'm tired of waiting. Timeout.\n");
packetsTimedOut++;
csvOutput << "Dropped" << endl;
return;
}
else
{
if(FD_ISSET(socketDescriptor, &readfds))
{
/* Receive the data */
ssize_t bytesReceived = recvfrom(socketDescriptor, receivedPacketBuffer, sizeof(receivedPacketBuffer), 0, (struct sockaddr *)&fromWhom, &fromWhomLength);
/* Format the received data into the IP struct, then shift bits */
struct ip * receivedIPHeader = (struct ip *) receivedPacketBuffer;
int headerLength = receivedIPHeader->ip_hl << 2;
/* Format the received data into the ICMP struct */
receivedICMPHeader = (struct icmp *)(receivedPacketBuffer + headerLength);
/* Get the time */
#if __MACH__
clock_get_time(cclock, &receivedTime);
mach_timespec_t * sentTime = (mach_timespec_t *)receivedICMPHeader->icmp_data;
/* Thanks Richard Stevens' book UNIX Network Programming for helping with
this next chunk of time processing code */
if ( (receivedTime.tv_nsec -= sentTime->tv_nsec) < 0)
{
--receivedTime.tv_sec;
receivedTime.tv_nsec += 1000000000;
}
#elif __WINDOWS__
// GetTick64Count()
#elif __GNUC__
clock_gettime(CLOCK_MONOTONIC, &receivedTime2);
struct timespec * sentTime2 = (struct timespec *)receivedICMPHeader->icmp_data;
/* Thanks Richard Stevens' book UNIX Network Programming for helping with
this next chunk of time processing code */
if ( (receivedTime2.tv_nsec -= sentTime2->tv_nsec) < 0)
{
--receivedTime2.tv_sec;
receivedTime2.tv_nsec += 1000000000;
}
#endif
/* Check if the packet was an ECHO_REPLY, and if it was meant for our computer using the ICMP id,
which we set to the process ID */
if (receivedICMPHeader->icmp_type == 0)
{
if(receivedICMPHeader->icmp_id != processID)
{
printf("Not our packet\n");
printf("processID = %d \t ID = %d\n", processID, receivedICMPHeader->icmp_id);
}
else
{
/* We got a valid reply. Count it! */
pingsReceived++;
/* Calculate round trip time */
if(!excludingPings)
{
#if __MACH__
receivedTime.tv_sec -= sentTime->tv_sec;
roundTripTime = receivedTime.tv_sec * 1000.0 + receivedTime.tv_nsec / 1000000.0;
#elif __GNUC__
receivedTime2.tv_sec -= sentTime2->tv_sec;
roundTripTime = receivedTime2.tv_sec * 1000.0 + receivedTime2.tv_nsec / 1000000.0;
#endif
sumOfResponseTimesSquared += roundTripTime * roundTripTime;
totalResponseTime += roundTripTime;
}
/* Get presentation format of source IP */
char str[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &(receivedIPHeader->ip_src), str, INET_ADDRSTRLEN);
/* If we're counting stats for this reply, add data to running stats */
if(!excludingPings)
{
/* If we're in CSV mode, print info to the file instead of to the terminal */
if(quiet)
{
printf(".\n");
csvOutput << (bytesReceived + 14) << "," << str << "," << receivedICMPHeader->icmp_seq << "," << (int)receivedIPHeader->ip_ttl << "," << roundTripTime << endl;
}
/* Otherwise, print received reply information to the terminal */
else
{
printf("%d bytes from %s packet number:%d ttl:%d time:%f ms\n", (bytesReceived + 14), str, receivedICMPHeader->icmp_seq, (int)receivedIPHeader->ip_ttl, roundTripTime);
}
}
}
}
}
}
}
/*
buildPing()
buildPing() initializes the ICMP Header and IP Header structs, which
contain essential packet information. The IP address information is
set in main().
*/
void buildPing()
{
icmpHeader = (struct icmp *) packet;
ipHeader= (struct ip *) packetIP;
icmpHeader->icmp_type = 8; // This shouldn't change
icmpHeader->icmp_code = 0;
icmpHeader->icmp_cksum = 0;
icmpHeader->icmp_seq = 1;
icmpHeader->icmp_id = processID;
ipHeader->ip_p = 1;
ipHeader->ip_ttl = 64; //Recommended value, according to the internet.
ipHeader->ip_hl = 5;
}
void interrupt(int signal);
/*
main()
Where the magic happens. Command line flags are set, program
control is set.
*/
char *argv[2];
int main(int argc, const char** argv)
{
signal(SIGINT, interrupt);
#ifdef WIN32
WSADATA wsaData;
int result=WSAStartup(MAKEWORD(2,0), &wsaData);
if(result != 0) {
printf("WSAStartup failed with error: %d\n", wsaData);
return 1;
#endif
printf("----------------------------------------------------------------\n");
int socketDescriptor;
/*
Variables for command line flag processing
*/
const char* destination = argv[1];
bool timeBetweenReq = 0; // -q // --request-time
float msecsBetweenReq = 1000;
bool timeBetweenRepReq = 0; // -b // --reply-request-time
int msecsBetweenRepReq = 0;
bool datagramSizeSet = 0; // -d // --datagram-size
bool payloadSize = 0; // -p // --payload-size
bool randSizeMinMax = 0; // -l // --random-size-minmax
int bytesSizeMin = 0;
int bytesSizeMax = 0;
bool randSizeAvgStd = 0; // -r // --random-size-avgstd
int bytesSizeAvg = 0;
int bytesSizeStd = 0;
bool randTimeMinMax = 0; // -s // --random-time-minmax
int msecsTimeMin = 0;
int msecsTimeMax = 0;
bool randTimeAvgStd = 0; // -t // --random-time-avgstd
int msecsTimeAvg = 0;
int msecsTimeStd = 0;
bool increasingSize = 0; // -i // --increasing-size
int sizeInitial = 0; // Must be greater than or equal to (IP_MINLENGTH + ICMP_MINLENGTH)
int sizeGrowth = 0;
excludingPing = 0; // -e // --exclud
bool multiplePings = 0; // -n // --ping-count
bool csvMode = 0; // -c // --csv
if(argc - 1 == 0)
{
printf("USAGE:\nePing [host IP or domain name]\n[-n/--pings-to-send num of pings to send]\n[-e/--exclude num of pings to exclude from stats]\n\nTIME\n[-q/--request-time time between sent pings]\n[-t/--random-time-avgstd set random, normally distributed time between pings - enter average and standard deviation]\n[-z/--timeout manually set timeout for listening method]\n\nSIZE\n[-p/--payload-size ICMP payload size] OR [-d/--datagram-size size of whole packet]\n[-r/--random-size-avgstd set random, normally distributed packet sizes - enter average and standard deviation]\n\nOUTPUT\n[-c/--csv output reply information to a csv file]\n");
printf("----------------------------------------------------------------\n");
return(1);
}
else
{
/* Check if we're root. If not, we can't create the raw socket necessary for ICMP */
if(getuid()!=0 && geteuid()!=0)
{
printf("UID: %d EUID: %d", getuid(), geteuid());
printf("\nCan't run. I need root permissions to create raw socket. Sorry.\n");
printf("----------------------------------------------------------------\n");
return(1);
}
/* Create socket descriptor for sending and receiving traffic */
socketDescriptor = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
/* Drop root permissions */
seteuid(501);
setuid(getuid());
}
for(int i = 2; i < argc; i++) {
// argv[0] is the ./a which is input
// argv[1] is the address (IPv4, DNS, IPv6)
if(strcmp(argv[i],"-q") == 0 || strcmp(argv[i],"--request-time") == 0)
{
if(timeBetweenRepReq || randTimeMinMax || randTimeAvgStd)
{
printf("-q flag not set, conflicting with previously set flag.\n");
}
else
{
if(i + 1 < argc && atoi(argv[i + 1]) >= 0)
{
timeBetweenReq = true;
msecsBetweenReq = atoi(argv[i + 1]);
printf("Flag -q set! Waiting %f milliseconds between ping requests.\n", msecsBetweenReq);
i++;
}
else
{
printf("-q flag not set, requires parameter.\n");
}
}
}
else if(strcmp(argv[i],"-b") == 0 || strcmp(argv[i],"--reply-request-time") == 0)
{
if(timeBetweenReq || randTimeMinMax || randTimeAvgStd)
{
printf("-b flag not set, conflicting with previously set flag.\n");
}
else
{
if(i + 1 < argc && atoi(argv[i + 1]) >= 0)
{
timeBetweenRepReq = true;
msecsBetweenRepReq = atoi(argv[i + 1]);
printf("Flag -b set! Waiting %d milliseconds between receiving a reply and sending a request\n", msecsBetweenRepReq);
i++;
}
else
{
printf("-b flag not set, requires parameter.\n");
}
}
}
else if(strcmp(argv[i],"-d") == 0 || strcmp(argv[i],"--datagram-size") == 0)
{
if(payloadSize || randSizeMinMax || randSizeAvgStd)
{
printf("-d flag not set, conflicting with previously set flag.\n");
}
else
{
if(i + 1 < argc && atoi(argv[i + 1]) > 0)
{
datagramSizeSet = true;
icmpPayloadLength = atoi(argv[i + 1]) - ICMP_MINLEN - IP_MINLENGTH;
if(icmpPayloadLength < 8)
{
printf("Error: datagram size must be greater than 50 bytes.\n");
printf("----------------------------------------------------------------\n");
return(1);
}
printf("Flag -d set! Datagram will be %d bytes large.\n", icmpPayloadLength+IP_MINLENGTH+ICMP_MINLEN);
i++;
}
else
{
printf("-d flag not set, requires parameter.\n");
}
}
}
else if(strcmp(argv[i],"-p") == 0 || strcmp(argv[i],"--payload-size") == 0)
{
if(datagramSizeSet || randSizeMinMax || randSizeAvgStd)
{
printf("-p flag not set, conflicting with previously set flag.\n");
}
else
{
if(i + 1 < argc && atoi(argv[i + 1]) >= 0)
{
payloadSize = true;
icmpPayloadLength = atoi(argv[i + 1]);
if(icmpPayloadLength < 8)
{
printf("Error: Payload size must be 8 bytes or greater.\n");
printf("----------------------------------------------------------------\n");
return(1);
}
printf("Flag -p set! Payload size will be %d bytes large.\n", icmpPayloadLength);
i++;
}
else
{
printf("-p flag not set, requires parameter.\n");
}
}
}
else if(strcmp(argv[i],"-l") == 0 || strcmp(argv[i],"--random-size-minmax") == 0)
{
if(datagramSizeSet || payloadSize || increasingSize || randSizeAvgStd)
{
printf("-l flag not set, conflicting with previously set flag.\n");
}
else
{
if(i + 2 < argc && atoi(argv[i + 2]) > 0)
{
randSizeMinMax = true;
if(atoi(argv[i + 1]) > atoi(argv[i + 2]))
{
bytesSizeMin = atoi(argv[i + 2]);
bytesSizeMax = atoi(argv[i + 1]);
}
else
{
bytesSizeMin = atoi(argv[i + 1]);
bytesSizeMax = atoi(argv[i + 2]);
}
printf("Flag -l set! Random size will be between %d ", bytesSizeMin);
printf("and %d.\n", bytesSizeMax);
i += 2;
}
else
{
printf("-l flag not set, requires parameters.\n");
}
}
}
else if(strcmp(argv[i],"-r") == 0 || strcmp(argv[i],"--random-size-avgstd") == 0)
{
if(datagramSizeSet || payloadSize || increasingSize || randSizeMinMax)
{
printf("-r flag not set, conflicting with previously set flag.\n");
}
else
{
if(i + 2 < argc && atoi(argv[i + 2]) > 0)
{
randSizeAvgStd = true;
bytesSizeAvg = atoi(argv[i + 1]);
bytesSizeStd = atoi(argv[i + 2]);
printf("Flag -r set! Random size will average %d",bytesSizeAvg);
printf(" with a std. dev. of %d.\n", bytesSizeStd);
i += 2;
}
else
{
printf("-r flag not set, requires parameters.\n");
}
}
}
else if(strcmp(argv[i],"-s") == 0 || strcmp(argv[i],"--random-time-minmax") == 0)
{
if(timeBetweenReq || timeBetweenRepReq || randTimeAvgStd)
{
printf("-s flag not set, conflicting with previously set flag.\n");
}
else
{
if(i + 2 < argc && atoi(argv[i + 2]) > 0)
{
randTimeMinMax = true;
if(atoi(argv[i + 1]) > atoi(argv[i + 2]))
{
msecsTimeMin = atoi(argv[i + 2]);
msecsTimeMax = atoi(argv[i + 1]);
}
else
{
msecsTimeMin = atoi(argv[i + 1]);
msecsTimeMax = atoi(argv[i + 2]);
}
printf("Flag -s set! Random time between requests will be between %d ", msecsTimeMin);
printf("and %d.\n", msecsTimeMax);
i += 2;
}
else
{
printf("-s flag not set, requires parameters.\n");
}
}
}
else if(strcmp(argv[i],"-t") == 0 || strcmp(argv[i],"--random-time-avgstd") == 0)
{
if(timeBetweenReq || timeBetweenRepReq || randTimeMinMax)
{
printf("-t flag not set, conflicting with previously set flag.\n");
}
else
{
if(i + 2 < argc && atoi(argv[i + 2]) > 0)
{
randTimeAvgStd = true;
msecsTimeAvg = atoi(argv[i + 1]);
msecsTimeStd = atoi(argv[i + 2]);
printf("Flag -t set! Random time between requests will average %d ",msecsTimeAvg);
printf("with a std. dev. of %d.\n", msecsTimeStd);
i += 2;
}
else
{
printf("-t flag not set, requires parameters.\n");
}
}
}
else if(strcmp(argv[i],"-i") == 0 || strcmp(argv[i],"--increasing-size") == 0)
{
if(datagramSizeSet || payloadSize || randSizeMinMax || randSizeAvgStd)
{
printf("-i flag not set, conflicting with previously set flag.\n");
}
else
{
if(i + 2 < argc && atoi(argv[i + 2]) > 0)
{
increasingSize = true;
sizeInitial = atoi(argv[i + 1]);
sizeGrowth = atoi(argv[i + 2]);
if(sizeInitial >= (IP_MINLENGTH + ICMP_MINLENGTH))
{
printf("Flag -i set! Pings will have an initial size of %d ", sizeInitial);
printf("and grow at a rate of %d per request.\n", sizeGrowth);
//Subtract growth from initial once so when we ping, we can add sizeGrowth to it every time,
//and initialGrowth is still proper
icmpPayloadLength = sizeInitial - sizeGrowth - IP_MINLENGTH - ICMP_MINLENGTH + 8;
}
else
{
printf("Problem: Initial size must be greater than IP header size plus the ICMP header size (%d).\n", IP_MINLENGTH + ICMP_MINLENGTH);
return(1);
}
i += 2;
}
else
{
printf("-i flag not set, requires parameters.\n");
}
}
}
else if(strcmp(argv[i],"-e") == 0 || strcmp(argv[i],"--exclude") == 0)
{
if(i + 1 < argc && atoi(argv[i + 1]) > 0)
{
excludingPing = true;
pingsToExclude = atoi(argv[i + 1]);
if(pingsToExclude >= pingsToSend)
{
printf("Trying to exclude more pings than you send huh? Not funny.\n");
exit(0);
}
printf("Flag -e set! %d earliest pings to be excluded from final statistics.\n", pingsToExclude);
i++;
}
else
{
printf("-e flag not set, requires parameter.\n");
}
}
else if(strcmp(argv[i],"-n") == 0 || strcmp(argv[i],"--pings-to-send") == 0)
{
if(i + 1 < argc && atoi(argv[i + 1]) > 0)
{
multiplePings = true;
pingsToSend = atoi(argv[i + 1]);
printf("Flag -n set! %d pings to be sent.\n", pingsToSend);
i++;
}
else
{
printf("-n flag not set, requires parameter.\n");
}
}
else if(strcmp(argv[i], "-c") == 0 || strcmp(argv[i], "--csv") == 0)
{
csvMode = 1;
if(i + 1 < argc)
{
csvFileName = argv[i+1];
i++;
}
else
{
csvFileName = "output.csv";
}
/* Open the CSV file */
csvOutput.open(csvFileName);
printf("Flag -c set! Replies will be output to %s.\n", csvFileName.c_str());
}
else if(strcmp(argv[i], "-z") == 0 || strcmp(argv[i], "--timeout") == 0)
{
if(i + 1 < argc && atoi(argv[i + 1]) > 0)
{
timeoutInput = atoi(argv[i + 1]);
i++;
}
printf("Flag -z set! Listening timeout will be set to %d seconds.\n", timeoutInput);
}
else
{
printf("Flag not recognized, \"%s\"\n",argv[i]);
}
}
printf("Destination set to: %s\n", argv[1]);
struct addrinfo *result, hints;
hints.ai_family = AF_INET; /* Allow IPv4 or IPv6 */
hints.ai_socktype = SOCK_RAW; /* RAW socket */
hints.ai_flags = AI_PASSIVE; /* Fill in my IP address */
int status;
/* Convert address */
if((status = getaddrinfo(destination, NULL, &hints, &result)) != 0)
{
printf("getaddrinfo error: %s\n", gai_strerror(status));
printf("Double check the address that you want to ping.\n");
exit(1);
}
whereto = (struct sockaddr_in *)result->ai_addr;
/*
WINDOWS block for setting the source and destination IP address
*/
#if WIN32
sockAddress = &whereto;
int sizeOfAddress=sizeof(IPHeader.destinationIPAddress);
if(WSAStringToAddress((char *)destination,AF_INET,NULL,(LPSOCKADDR)&IPHeader.destinationIPAddress.S_un.S_un_W,&sizeOfAddress)!=0)
{
int error=WSAGetLastError();
std::cout<<error<<std::endl;
std::cout<<sizeOfAddress<<std::endl;
}
if(WSAStringToAddress((char*)destination,AF_INET,NULL,(LPSOCKADDR)&(socketAddress->sin_addr),(int*)sizeof(socketAddress->sin_addr))!=0)
{
int error=WSAGetLastError();
std::cout<<error<<std::endl;
}
#endif
/*
Set up the listening sockaddr_in struct
*/
sockaddr_in sourceSocket;
sourceSocket.sin_addr = srcIP;
sourceSocket.sin_family = AF_INET;
/* We use the process ID from this program as our ICMP packet id */
processID = getpid() & 0xFFFF;
printf("----------------------------------------------------------------\n");
/* Initialize some ICMP and IP header values */
buildPing();
/* Just a counting variable */
int i = 0;
/* Variables and generators for random size/time */
double randomDatagramSize;
double randomTime;
std::random_device rd;
std::default_random_engine generator( rd() );
std::normal_distribution<double> distribution(bytesSizeAvg, bytesSizeStd);
std::normal_distribution<double> distribution2(msecsTimeAvg, msecsTimeStd);
/* Specify that we want two threads (one for listening, one for sending) */
omp_set_num_threads(2);
/*
Execute the ping/listen functions in sequential mode (wait for reply before sending request)
*/
if(timeBetweenRepReq)
{
while(pingsSent < pingsToSend)
{
pingICMP(socketDescriptor, icmpPayloadLength);
listenICMP(socketDescriptor, &sourceSocket, 0, 0, timeoutInput);
usleep(msecsBetweenRepReq * 1000);
}
}
/*
Execute the ping/listen functions in parallel with OpenMP threading
*/
else
{
#pragma omp parallel sections
{
/* Sending block */
#pragma omp section
for (i = 0; i < pingsToSend; i++)
{
/* Ping */
if(randSizeAvgStd)
{
randomDatagramSize = distribution(generator);
pingICMP(socketDescriptor, randomDatagramSize - IP_MINLENGTH - ICMP_MINLEN);
}
else if(increasingSize)
{
icmpPayloadLength += sizeGrowth;
pingICMP(socketDescriptor, icmpPayloadLength);
}
else
{
pingICMP(socketDescriptor, icmpPayloadLength);
}
/* Wait */
if(randTimeAvgStd)
{
randomTime = distribution2(generator);
usleep(randomTime * 1000);
}
else
{
usleep(msecsBetweenReq * 1000);
}
}
/* Listening block */
#pragma omp section
while(i != pingsToSend - 1 && pingsToSend != pingsReceived + packetsTimedOut && killSwitch)
{
/* If we're excluding some pings, listen but don't print any info */
if(excludingPing && pingsReceived < pingsToExclude)
{
listenICMP(socketDescriptor, &sourceSocket, 1, 1, timeoutInput);
}
/* If we're outputting to a CSV file, don't print out so much on the terminal */
else if(csvMode)
{
listenICMP(socketDescriptor, &sourceSocket, 1, 0, timeoutInput);
}
/* Otherwise, print all statistics and information to the terminal */
else
{
listenICMP(socketDescriptor, &sourceSocket, 0, 0, timeoutInput);
}
}
}
}
/* Print final statistics and quit */
report();
if(csvMode)
{
csvOutput.close();
}
#ifdef WIN32
int result = WSACleanup();
#endif
return(0);
}
void interrupt(int signal)
{
if(killSwitch)
{
pingsToSend = 0;
killSwitch = false;
}
}