-
Notifications
You must be signed in to change notification settings - Fork 0
/
udptest.c
1387 lines (1155 loc) · 40 KB
/
udptest.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
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
990
991
992
993
994
995
996
997
998
999
1000
#include <assert.h>
#include <fcntl.h>
#include <math.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <errno.h>
#include <limits.h>
#include <sched.h>
#include <pthread.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/if.h>
#include <linux/if_ether.h>
#include "rxbuff.h"
#include "tsutil.h"
#include "uthash.h"
#define PACKET_BUFFER_LEN 2048
#define DEFAULT_DCLIENT_INTERVAL 1000000
#define DEFAULT_TIME_SUFFIX 's'
#define DEFAULT_TIME_LIMIT 30
/*
* Measurement data sent in each UDP packet.
*/
struct packet_info {
uint32_t session; // session ID from the sender's perspective
uint32_t seq; // sender sequence number
uint32_t sent_sec; // sender timestamp (seconds part)
uint32_t sent_nsec; // sender timestamp (nanoseconds part)
uint32_t key; // private shared key
uint32_t size; // size (bytes) of payload
uint32_t delay_sec; // time since previous send (seconds part)
uint32_t delay_nsec;// time since previous send (nanoseconds part)
} __attribute__((__packed__));
const size_t MIN_PACKET_LENGTH = sizeof(struct packet_info);
struct client_info {
struct sockaddr_storage addr;
socklen_t addr_len;
char addrstr[INET6_ADDRSTRLEN];
char portstr[6];
struct timespec timeout;
uint32_t session;
uint32_t next_seq;
int sockfd;
tsval_t packet_interval;
UT_hash_handle hh;
};
enum {
MODE_UP_SERVER,
MODE_UP_CLIENT,
MODE_DOWN_SERVER,
MODE_DOWN_CLIENT,
MODE_TCP_DOWN_SERVER,
MODE_TCP_DOWN_CLIENT,
};
const char *OPTSTRING = "SCDRd:p:l:r:t:k:i:b:ch";
const struct option LONGOPTS[] = {
{.name = "userver", .has_arg = 0, .val = 'S'},
{.name = "uclient", .has_arg = 0, .val = 'C'},
{.name = "dserver", .has_arg = 0, .val = 'D'},
{.name = "dclient", .has_arg = 0, .val = 'R'},
{.name = "dest", .has_arg = 1, .val = 'd'},
{.name = "port", .has_arg = 1, .val = 'p'},
{.name = "length", .has_arg = 1, .val = 'l'},
{.name = "rate", .has_arg = 1, .val = 'r'},
{.name = "time", .has_arg = 1, .val = 't'},
{.name = "key", .has_arg = 1, .val = 'k'},
{.name = "interval",.has_arg = 1, .val = 'i'},
{.name = "bind", .has_arg = 1, .val = 'b'},
{.name = "csv", .has_arg = 0, .val = 'c'},
{.name = "connect-timeout", .has_arg = 1, .val = 'n'},
{.name = "timeout", .has_arg = 1, .val = 'o'},
{.name = "help", .has_arg = 0, .val = 'h'},
{.name = "tcp-dserver", .has_arg = 0, .val = MODE_TCP_DOWN_SERVER},
{.name = "tcp-dclient", .has_arg = 0, .val = MODE_TCP_DOWN_CLIENT},
{.name = 0, .has_arg = 0, .val = 0},
};
/*
* Command-line arguments that control program behavior.
*/
static int mode = -1;
static const char *server_addr = "127.0.0.1";
static int server_port = 5050;
static long packet_length = 1400;
static long sending_rate = 1000000;
static tsval_t time_limit = -1;
static unsigned access_key = 0;
static tsval_t packet_interval = -1;
static const char *bind_device = NULL;
static tsval_t connect_timeout = -1;
static tsval_t data_timeout = -1;
static int csv_output = 0;
/*
* Parse a null-terminated string specifying a bit rate.
*
* The expected format is a number followed by an optional multiplier suffix,
* G, M, or k.
*
* Returns a negative value on error.
*/
static long parse_rate(const char *rate_str)
{
char *end;
long rate = strtol(rate_str, &end, 10);
if((errno == ERANGE && (rate == LONG_MAX || rate == LONG_MIN)) ||
(errno != 0 && rate == 0)) {
perror("strtol");
return -1;
}
while(end && *end) {
if(isalpha(*end)) {
switch(*end) {
case 'G':
rate *= 1e9;
break;
case 'M':
rate *= 1e6;
break;
case 'k':
rate *= 1e3;
break;
default:
return -1;
}
}
end++;
}
return rate;
}
/*
* Parses a time string with an optional suffix.
* Returns a value in nanoseconds, negative indicates an error.
*/
tsval_t parse_time(const char *time_str)
{
char *end;
long time = strtol(time_str, &end, 10);
if((errno == ERANGE && (time == LONG_MAX || time == LONG_MIN)) ||
(errno != 0 && time == 0)) {
perror("strtol");
return -1;
}
while(end && *end) {
if(isalpha(*end)) {
return apply_time_suffix(time, *end);
}
end++;
}
return apply_time_suffix(time, DEFAULT_TIME_SUFFIX);
}
static void print_usage(const char *cmd)
{
printf("udptest is a featureful UDP+TCP bandwidth testing tool.\n");
printf("Copyright (C) 2013 Lance Hartung\n");
printf("\n");
printf("Usage: %s <mode> [options]\n", cmd);
printf("\n");
printf("Modes:\n");
printf(" --userver Receiving server for upload test\n");
printf(" --uclient Sending client for upload test\n");
printf(" --dserver Sending server for download test\n");
printf(" --dclient Receiving client for download test\n");
printf(" --tcp-dserver Sending server for download test\n");
printf(" --tcp-dclient Receiving client for download test\n");
printf("\n");
printf("Options:\n");
printf(" --dest\n");
printf(" --port\n");
printf(" --length Length of payload (bytes) in data packets\n");
printf(" --rate Target bit rate (optionally append a suffix k, M, or G without a space)\n");
printf(" --time Time limit (in seconds, zero means no limit)\n");
printf(" --key Authentication key (positive integer) to be used between dserver and dclient\n");
printf(" --interval Packet interval (seconds, overrides --rate, optionally append a suffix m, u, or n without a space)\n");
printf(" --bind Bind to device (super-user only)\n");
printf(" --connect-timeout Timeout (seconds) for establishing TCP connection\n");
printf(" --timeout Timeout (seconds) for receiving data\n");
printf(" --csv Produce output in CSV format\n");
}
/*
* Parse the command-line arguments.
*
* Returns 0 on success or a negative value on failure.
*/
static int parse_args(int argc, char *argv[])
{
const char *command = argv[0];
int opt;
opt = getopt_long(argc, argv, OPTSTRING, LONGOPTS, NULL);
while(opt > 0) {
switch(opt) {
case 'S':
mode = MODE_UP_SERVER;
break;
case 'C':
mode = MODE_UP_CLIENT;
break;
case 'D':
mode = MODE_DOWN_SERVER;
break;
case 'R':
mode = MODE_DOWN_CLIENT;
break;
case 'd':
server_addr = optarg;
break;
case 'p':
server_port = atoi(optarg);
break;
case 'l':
packet_length = atol(optarg);
if(packet_length < (long)MIN_PACKET_LENGTH) {
fprintf(stderr, "Packet length (%ld) must be at least %u\n",
packet_length, MIN_PACKET_LENGTH);
return -1;
}
break;
case 'r':
sending_rate = parse_rate(optarg);
if(sending_rate < 0) {
fprintf(stderr, "Invalid rate: %s\n", optarg);
return -1;
}
break;
case 't':
time_limit = parse_time(optarg);
if(time_limit < 0) {
fprintf(stderr, "Invalid time limit: %s\n", optarg);
return -1;
}
break;
case 'k':
access_key = atoi(optarg);
break;
case 'i':
packet_interval = parse_time(optarg);
if(packet_interval < 0) {
fprintf(stderr, "Invalid packet interval: %s\n", optarg);
return -1;
}
break;
case 'b':
bind_device = optarg;
break;
case MODE_TCP_DOWN_SERVER:
mode = MODE_TCP_DOWN_SERVER;
break;
case MODE_TCP_DOWN_CLIENT:
mode = MODE_TCP_DOWN_CLIENT;
break;
case 'n':
connect_timeout = parse_time(optarg);
break;
case 'o':
data_timeout = parse_time(optarg);
break;
case 'c':
csv_output = 1;
break;
case 'h':
print_usage(command);
return -1;
default:
fprintf(stderr, "Invalid option: %c\n", opt);
print_usage(command);
return -1;
}
opt = getopt_long(argc, argv, OPTSTRING, LONGOPTS, NULL);
}
return 0;
}
/*
* Compute packet spacing to achieve desired rate.
* Rate in bits/second, length in bits, spacing in microseconds.
*/
tsval_t compute_spacing(long rate, long length)
{
double spacing = (double)length / (double)rate;
return (tsval_t)round(TSVAL_BASE * spacing);
}
/*
* SET SCHED PRIORITY
*
* Sets the process' priority. If priority is 0, setSchedPriority will set the
* scheduling priority to the default used by most processes. If priority is
* non-zero, the result will be that this process will preempt most other
* running processes. priority must not be negative.
*
* Use ps to see the effect:
* ps -eo command,pid,policy,rtprio,pcpu
*/
int setSchedPriority(int priority)
{
int rtn;
struct sched_param param;
if(priority < 0) {
fprintf(stderr, "Priority cannot be negative!");
return -1;
}
rtn = sched_getparam(0, ¶m);
if(rtn < 0) {
fprintf(stderr, "sched_getparam failed\n");
return -1;
}
param.sched_priority = priority;
const int policy = (priority == 0) ? SCHED_OTHER : SCHED_RR;
rtn = sched_setscheduler(0, policy, ¶m);
if(rtn < 0) {
fprintf(stderr, "sched_setscheduler failed\n");
return -1;
}
return 0;
}
int main(int argc, char *argv[])
{
int result;
srand(time(0));
/* If running as root, we can set real-time priority for better accuracy
* with inter-packet spacing. */
if(getuid() == 0)
setSchedPriority(1);
result = parse_args(argc, argv);
if(result < 0)
exit(EXIT_FAILURE);
if(mode < 0) {
print_usage(argv[0]);
return 1;
}
switch(mode) {
case MODE_UP_SERVER:
return upload_server_main();
case MODE_UP_CLIENT:
return upload_client_main();
case MODE_DOWN_SERVER:
return download_server_main();
case MODE_DOWN_CLIENT:
return download_client_main();
case MODE_TCP_DOWN_SERVER:
return tcp_download_server_main();
case MODE_TCP_DOWN_CLIENT:
return tcp_download_client_main();
}
return 0;
}
/*
* Bind a socket to a specified network device.
*/
static int socket_bind_device(int sockfd, const char *device)
{
assert(device);
socklen_t option_len = strnlen(device, IFNAMSIZ);
if(setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE, device, option_len) < 0) {
if(errno == EPERM || errno == EACCES)
fprintf(stderr, "Error: binding to %s failed with permission denied, must be run with super user\n", device);
else
perror("SO_BINDTODEVICE");
return -1;
}
return 0;
}
/*
* Set the nonblocking flag on a socket.
* Pass nonzero value to nonblocking to enable, or zero to disable.
*/
int socket_set_nonblocking(int sockfd, int nonblocking)
{
int flags = fcntl(sockfd, F_GETFL);
if(nonblocking) {
flags |= O_NONBLOCK;
} else {
flags &= ~O_NONBLOCK;
}
int result = fcntl(sockfd, F_SETFL, flags);
if(result < 0)
perror("fcntl F_SETFL");
return result;
}
/*
* Get a timestamp for the last received packet. Tries to use the most
* accurate method available but falls back to another method if that fails.
*/
int recv_timestamp(int sockfd, struct timespec *ts)
{
// Method gets incremented in the case of a failure, so that we fall back
// to a working method without continuously retrying a failing method.
static int method = 0;
if(method == 0) {
int result = ioctl(sockfd, SIOCGSTAMPNS, ts);
if(result < 0) {
perror("ioctl SIOCGSTAMPNS");
method++;
} else {
return 0;
}
}
return clock_gettime(CLOCK_REALTIME, ts);
}
/*
* Server main function.
*/
int upload_server_main()
{
char port_str[16];
int result;
int sockfd;
struct addrinfo *addrinfo;
char *buffer;
int buffer_len = packet_length > PACKET_BUFFER_LEN ?
packet_length : PACKET_BUFFER_LEN;
snprintf(port_str, sizeof(port_str), "%d", server_port);
const struct addrinfo hints = {
.ai_flags = AI_PASSIVE | AI_NUMERICSERV,
.ai_family = AF_UNSPEC,
.ai_socktype = SOCK_DGRAM,
.ai_protocol = 0,
};
result = getaddrinfo(NULL, port_str, &hints, &addrinfo);
if(result < 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(result));
return EXIT_FAILURE;
}
sockfd = socket(addrinfo->ai_family, addrinfo->ai_socktype, addrinfo->ai_protocol);
if(sockfd < 0) {
perror("socket");
return EXIT_FAILURE;
}
result = bind(sockfd, addrinfo->ai_addr, addrinfo->ai_addrlen);
if(result < 0) {
perror("bind");
return EXIT_FAILURE;
}
if(bind_device)
socket_bind_device(sockfd, bind_device);
buffer = malloc(buffer_len);
if(!buffer) {
fprintf(stderr, "Out of memory.\n");
return EXIT_FAILURE;
}
const char *format;
if(csv_output) {
printf("receiver_time,source_address,sport,session,sequence,bytes,sender_time,delay\n");
format = "%d.%09d,%s,%s,%u,%u,%u,%d.%09d,%d.%09d\n";
} else {
printf("receiver_time source_address sport session sequence bytes sender_time delay \n");
format = "%10d.%09d %-15s %-5s %-10u %-10u %-10u %10d.%09d %1d.%09d\n";
}
while(1) {
struct sockaddr_storage from_addr;
socklen_t from_addr_len = sizeof(from_addr);
result = recvfrom(sockfd, buffer, buffer_len, 0,
(struct sockaddr *)&from_addr, &from_addr_len);
if(result < 0) {
perror("recvfrom");
return EXIT_FAILURE;
} else {
struct packet_info *info = (struct packet_info *)buffer;
struct timespec received;
recv_timestamp(sockfd, &received);
char addrstr[INET6_ADDRSTRLEN];
char portstr[6];
getnameinfo((struct sockaddr *)&from_addr, from_addr_len,
addrstr, sizeof(addrstr),
portstr, sizeof(portstr),
NI_NUMERICHOST | NI_NUMERICSERV);
printf(format,
received.tv_sec, received.tv_nsec,
addrstr, portstr,
ntohl(info->session), ntohl(info->seq), result,
ntohl(info->sent_sec), ntohl(info->sent_nsec),
ntohl(info->delay_sec), ntohl(info->delay_nsec));
fflush(stdout);
}
}
free(buffer);
freeaddrinfo(addrinfo);
close(sockfd);
return 0;
}
/*
* Client main function.
*/
int upload_client_main()
{
char port_str[16];
int result;
int sockfd;
struct addrinfo *addrinfo;
char *buffer;
uint32_t next_seq = 0;
uint32_t session = htonl(rand());
snprintf(port_str, sizeof(port_str), "%d", server_port);
const struct addrinfo hints = {
.ai_flags = AI_NUMERICSERV,
.ai_family = AF_UNSPEC,
.ai_socktype = SOCK_DGRAM,
.ai_protocol = 0,
};
result = getaddrinfo(server_addr, port_str, &hints, &addrinfo);
if(result < 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(result));
return EXIT_FAILURE;
}
sockfd = socket(addrinfo->ai_family, addrinfo->ai_socktype, addrinfo->ai_protocol);
if(sockfd < 0) {
perror("socket");
return EXIT_FAILURE;
}
if(bind_device)
socket_bind_device(sockfd, bind_device);
buffer = malloc(packet_length);
if(!buffer) {
fprintf(stderr, "Out of memory.\n");
return EXIT_FAILURE;
}
if(packet_interval < 0)
packet_interval = compute_spacing(sending_rate, 8 * packet_length);
if(time_limit < 0) {
fprintf(stderr, "Warning: time_limit was not set, defaulting to %d seconds.\n",
DEFAULT_TIME_LIMIT);
time_limit = apply_time_suffix(DEFAULT_TIME_LIMIT, 's');
}
struct timespec stop_time;
clock_gettime(CLOCK_REALTIME, &stop_time);
timespec_add(&stop_time, time_limit);
struct timespec start;
struct timespec end;
clock_gettime(CLOCK_REALTIME, &end);
struct timespec delay;
timespec_fill(packet_interval, &delay);
while(1) {
clock_gettime(CLOCK_REALTIME, &start);
if(time_limit > 0 && timespec_after(&start, &stop_time))
break;
struct timespec actual_delay;
timespec_sub(&start, &end, &actual_delay);
struct packet_info *info = (struct packet_info *)buffer;
info->session = session;
info->seq = htonl(next_seq++);
info->sent_sec = htonl(start.tv_sec);
info->sent_nsec = htonl(start.tv_nsec);
info->key = htonl(access_key);
info->size = htonl(packet_length);
info->delay_sec = htonl(actual_delay.tv_sec);
info->delay_nsec = htonl(actual_delay.tv_nsec);
result = sendto(sockfd, buffer, packet_length, 0,
addrinfo->ai_addr, addrinfo->ai_addrlen);
if(result < 0) {
perror("sendto");
return EXIT_FAILURE;
}
clock_gettime(CLOCK_REALTIME, &end);
struct timespec sleep_time;
timespec_sub(&end, &start, &sleep_time);
timespec_sub(&delay, &sleep_time, &sleep_time);
if(sleep_time.tv_sec >= 0)
nanosleep(&sleep_time, NULL);
}
free(buffer);
freeaddrinfo(addrinfo);
close(sockfd);
return 0;
}
struct client_info *clients = NULL;
static void *dserver_send(void *arg)
{
struct client_info *client = (struct client_info *)arg;
char *buffer = malloc(packet_length);
if(!buffer) {
fprintf(stderr, "Out of memory\n");
return NULL;
}
struct timespec start;
struct timespec end;
clock_gettime(CLOCK_REALTIME, &end);
struct timespec delay;
timespec_fill(client->packet_interval, &delay);
while(1) {
clock_gettime(CLOCK_REALTIME, &start);
struct timespec actual_delay;
timespec_sub(&start, &end, &actual_delay);
if(time_limit > 0 && timespec_after(&start, &client->timeout))
goto out;
struct packet_info *info = (struct packet_info *)buffer;
info->session = htonl(client->session);
info->seq = htonl(client->next_seq++);
info->sent_sec = htonl(start.tv_sec);
info->sent_nsec = htonl(start.tv_nsec);
info->key = htonl(access_key);
info->size = htonl(packet_length);
info->delay_sec = htonl(actual_delay.tv_sec);
info->delay_nsec = htonl(actual_delay.tv_nsec);
int result = sendto(client->sockfd, buffer, packet_length, 0,
(struct sockaddr *)&client->addr, client->addr_len);
if(result < 0) {
perror("sendto");
goto out;
}
clock_gettime(CLOCK_REALTIME, &end);
struct timespec sleep_time;
timespec_sub(&end, &start, &sleep_time);
timespec_sub(&delay, &sleep_time, &sleep_time);
if(sleep_time.tv_sec >= 0)
nanosleep(&sleep_time, NULL);
}
out:
free(buffer);
HASH_DEL(clients, client);
free(client);
return NULL;
}
/*
* Download Server main function.
*
* Listens for packets from clients and responds by sending a UDP packet stream
* to the client.
*/
int download_server_main()
{
char port_str[16];
int result;
int sockfd;
struct addrinfo *addrinfo;
char *buffer;
int buffer_len = packet_length > PACKET_BUFFER_LEN ?
packet_length : PACKET_BUFFER_LEN;
unsigned next_session = 0;
if(packet_interval < 0)
packet_interval = compute_spacing(sending_rate, 8 * packet_length);
if(time_limit == 0) {
fprintf(stderr, "Warning: time_limit is set to zero, will send packets indefinitely.\n");
} else if(time_limit < 0) {
fprintf(stderr, "Warning: time_limit was not set, defaulting to %d seconds.\n",
DEFAULT_TIME_LIMIT);
time_limit = apply_time_suffix(DEFAULT_TIME_LIMIT, 's');
}
snprintf(port_str, sizeof(port_str), "%d", server_port);
const struct addrinfo hints = {
.ai_flags = AI_PASSIVE | AI_NUMERICSERV,
.ai_family = AF_UNSPEC,
.ai_socktype = SOCK_DGRAM,
.ai_protocol = 0,
};
result = getaddrinfo(NULL, port_str, &hints, &addrinfo);
if(result < 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(result));
return EXIT_FAILURE;
}
sockfd = socket(addrinfo->ai_family, addrinfo->ai_socktype, addrinfo->ai_protocol);
if(sockfd < 0) {
perror("socket");
return EXIT_FAILURE;
}
result = bind(sockfd, addrinfo->ai_addr, addrinfo->ai_addrlen);
if(result < 0) {
perror("bind");
return EXIT_FAILURE;
}
if(bind_device)
socket_bind_device(sockfd, bind_device);
buffer = malloc(buffer_len);
if(!buffer) {
fprintf(stderr, "Out of memory.\n");
return EXIT_FAILURE;
}
const char *format;
if(csv_output) {
printf("local_time,source_address,sport,session,next_seq\n");
format = "%d.%09d,%s,%s,%u,%u";
} else {
printf("local_time source_address sport session next_seq \n");
format = "%10d.%09d %-15s %-5s %-10u %-10u\n";
}
while(1) {
struct sockaddr_storage from_addr;
socklen_t from_addr_len = sizeof(from_addr);
result = recvfrom(sockfd, buffer, buffer_len, 0,
(struct sockaddr *)&from_addr, &from_addr_len);
if(result < 0) {
perror("recvfrom");
return EXIT_FAILURE;
} else {
struct packet_info *info = (struct packet_info *)buffer;
struct timespec received;
struct sockaddr_storage key;
if(ntohl(info->key) != access_key) {
fprintf(stderr, "Client access key (%u) is incorrect.\n",
ntohl(info->key));
} else {
recv_timestamp(sockfd, &received);
/* Copying the address this way ensures that the unused bytes are zero. */
memset(&key, 0, sizeof(key));
memcpy(&key, &from_addr, from_addr_len);
struct client_info *client = NULL;
HASH_FIND(hh, clients, &key, sizeof(key), client);
if(!client) {
client = malloc(sizeof(struct client_info));
if(!client) {
fprintf(stderr, "Out of memory\n");
} else {
memset(client, 0, sizeof(struct client_info));
memcpy(&client->addr, &from_addr, from_addr_len);
client->addr_len = from_addr_len;
getnameinfo((struct sockaddr *)&from_addr, from_addr_len,
client->addrstr, sizeof(client->addrstr),
client->portstr, sizeof(client->portstr),
NI_NUMERICHOST | NI_NUMERICSERV);
client->session = next_session++;
client->next_seq = 0;
client->sockfd = sockfd;
client->packet_interval = packet_interval;
HASH_ADD(hh, clients, addr, sizeof(client->addr), client);
pthread_t thread;
pthread_create(&thread, NULL, dserver_send, client);
}
}
if(client) {
clock_gettime(CLOCK_REALTIME, &client->timeout);
timespec_add(&client->timeout, time_limit);
printf(format,
received.tv_sec, received.tv_nsec,
client->addrstr, client->portstr,
client->session, client->next_seq);
fflush(stdout);
}
}
}
}
free(buffer);
freeaddrinfo(addrinfo);
close(sockfd);
return 0;
}
/*
* Receive Client main function.
*/
int download_client_main()
{
char port_str[16];
int result;
int sockfd;
struct addrinfo *addrinfo;
char *buffer;
int buffer_len = packet_length > PACKET_BUFFER_LEN ?
packet_length : PACKET_BUFFER_LEN;
uint32_t next_seq = 0;
uint32_t session = htonl(rand());
snprintf(port_str, sizeof(port_str), "%d", server_port);
const struct addrinfo hints = {
.ai_flags = AI_NUMERICSERV,
.ai_family = AF_UNSPEC,
.ai_socktype = SOCK_DGRAM,
.ai_protocol = 0,
};
result = getaddrinfo(server_addr, port_str, &hints, &addrinfo);
if(result < 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(result));
return EXIT_FAILURE;
}
sockfd = socket(addrinfo->ai_family, addrinfo->ai_socktype, addrinfo->ai_protocol);
if(sockfd < 0) {
perror("socket");
return EXIT_FAILURE;
}
if(socket_set_nonblocking(sockfd, 1) < 0)
return EXIT_FAILURE;
if(bind_device)
socket_bind_device(sockfd, bind_device);
buffer = malloc(buffer_len);
if(!buffer) {
fprintf(stderr, "Out of memory.\n");
return EXIT_FAILURE;
}
if(packet_interval < 0)
packet_interval = DEFAULT_DCLIENT_INTERVAL;
struct timespec stop_time;
clock_gettime(CLOCK_REALTIME, &stop_time);
if (time_limit > 0)
timespec_add(&stop_time, time_limit);
const char *format;
if(csv_output) {
printf("receiver_time,session,sequence,bytes,sender_time,delay\n");
format = "%d.%09d,%u,%u,%u,%d.%09d,%d.%09d\n";
} else {
printf("receiver_time session sequence bytes sender_time delay \n");
format = "%10d.%09d %-10u %-10u %-10u %10d.%09d %1d.%09d\n";
}
struct timespec timeout;
timeout.tv_sec = 0;
timeout.tv_nsec = 0;
struct timespec next_send;
clock_gettime(CLOCK_REALTIME, &next_send);
struct timespec delay;
timespec_fill(packet_interval, &delay);
while(1) {
struct timespec now;
clock_gettime(CLOCK_REALTIME, &now);
if(time_limit > 0 && timespec_after(&now, &stop_time))
break;
struct sockaddr_storage from_addr;
socklen_t from_addr_len = sizeof(from_addr);
result = recvfrom(sockfd, buffer, buffer_len, 0,
(struct sockaddr *)&from_addr, &from_addr_len);
if(result > 0) {
struct packet_info *info = (struct packet_info *)buffer;
struct timespec received;
struct sockaddr_storage key;
if(ntohl(info->key) != access_key) {
fprintf(stderr, "Sender access key (%u) is incorrect.\n",
ntohl(info->key));
} else {
recv_timestamp(sockfd, &received);
printf(format,
received.tv_sec, received.tv_nsec,
ntohl(info->session), ntohl(info->seq), result,
ntohl(info->sent_sec), ntohl(info->sent_nsec),
ntohl(info->delay_sec), ntohl(info->delay_nsec));
fflush(stdout);
}
} else {
if(errno != EWOULDBLOCK) {
perror("recvfrom");
return EXIT_FAILURE;
}
}
struct timespec start;
clock_gettime(CLOCK_REALTIME, &start);
if(timespec_after(&start, &next_send)) {
struct packet_info *info = (struct packet_info *)buffer;
info->session = htonl(session);
info->seq = htonl(next_seq++);
info->sent_sec = htonl(start.tv_sec);
info->sent_nsec = htonl(start.tv_nsec);
info->key = htonl(access_key);
info->size = htonl(packet_length);
info->delay_sec = htonl(delay.tv_sec); // TODO: Measure the actual time since last send.
info->delay_nsec = htonl(delay.tv_nsec);
result = sendto(sockfd, buffer, sizeof(*info), 0,
addrinfo->ai_addr, addrinfo->ai_addrlen);
if(result < 0) {
perror("sendto");
}