-
Notifications
You must be signed in to change notification settings - Fork 1
/
udpbench.c
1254 lines (1134 loc) · 31 KB
/
udpbench.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
/*
* Copyright (c) 2019-2024 Alexander Bluhm <bluhm@genua.de>
* Copyright (c) 2022 Moritz Buhl <mbuhl@genua.de>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <netinet/udp.h>
#include <assert.h>
#include <err.h>
#include <errno.h>
#include <limits.h>
#include <netdb.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
sig_atomic_t alarm_signaled;
const char *progname, *hostname, *service = "12345", *remotessh;
int divert, hopbyhop, sendmode;
int delay, idle = 1, timeout = 1;
long long bitrate;
int buffersize, mmsglen, repeat;
size_t udplength;
long packetrate;
char status_line[1024];
const char *pseudo = "none";
void udp_connect_send(struct timeval *, struct timeval *);
void udp_bind_receive(struct timeval *, struct timeval *, struct timeval *);
void udp_socket_fork(int *,
int(*)(int, struct sockaddr *, socklen_t *),
int(*)(int, const struct sockaddr *, socklen_t));
int udp_socket_wait(int, pid_t, FILE *);
void alarm_handler(int);
int udp_bind(int *, const char *, const char *);
int udp_connect(int *, const char *, const char *);
void udp_getsockname(int, char *, char *);
void udp_setbuffersize(int, int, int);
void udp_setrouteralert(int);
void udp_send(int, int, unsigned long);
void udp_receive(int, int, struct timeval *);
struct mmsghdr *mmsg_alloc(int, size_t, int);
void mmsg_free(struct mmsghdr *);
void status_init(const char *, unsigned long, unsigned long, unsigned long,
int, const struct timeval *, const struct timeval *);
void status_final(const struct timeval *, const struct timeval *);
unsigned long udp2iplength(unsigned long, int, unsigned long *);
unsigned long udp2etherlength(unsigned long , int);
pid_t ssh_bind(FILE **, const char *, const char *);
pid_t ssh_connect(FILE **, const char *, const char *);
pid_t ssh_pipe(FILE **, char **);
void ssh_getpeername(FILE *, char *, char *);
void ssh_wait(pid_t, FILE *);
static void
usage(void)
{
fprintf(stderr, "usage: udpbench [-DH] [-B bitrate] [-b bufsize] "
"[-C pseudo] [-d delay] [-i idle] [-l length] [-m mmsglen] "
"[-N repeat] [-P packetrate] [-p port] [-R remoteprog] "
"[-r remotessh] [-t timeout] send|recv [hostname]\n"
" -B bitrate bits per seconds send rate\n"
" -b bufsize set size of send or receive buffer\n"
" -C pseudo pseudo network device changes packet length\n"
" -D use pf divert packet for receive\n"
" -d delay wait for setup before sending\n"
" -H send hop-by-hop router alert option\n"
" -i idle idle timeout before receiving stops, "
"default 1\n"
" -l length set length of udp payload\n"
" -m mmsglen number of mmsghdr for sendmmsg or recvmmsg\n"
" -N repeat run parallel process with incremented address\n"
" -P packetrate packets per second send rate\n"
" -p port udp port, default 12345, random 0\n"
" -R remoteprog path of udpbench tool on remote side\n"
" -r remotessh ssh host to start udpbench on remote side\n"
" -t timeout send duration or receive timeout, default 1\n"
" send|recv send or receive mode for local side\n"
" hostname address of receiving side\n"
);
exit(2);
}
int
main(int argc, char *argv[])
{
struct sigaction act;
struct timeval start, stop, final;
const char *errstr;
int ch;
progname = argv[0];
if (setvbuf(stdout, NULL, _IOLBF, 0) != 0)
err(1, "setvbuf");
while ((ch = getopt(argc, argv, "B:b:C:Dd:Hi:l:m:N:P:p:R:r:t:"))
!= -1) {
switch (ch) {
case 'B':
bitrate = strtonum(optarg, 0, LLONG_MAX, &errstr);
if (errstr != NULL)
errx(1, "bits per second rate is %s: %s",
errstr, optarg);
break;
case 'b':
buffersize = strtonum(optarg, 0, INT_MAX, &errstr);
if (errstr != NULL)
errx(1, "buffer size is %s: %s",
errstr, optarg);
break;
case 'C':
pseudo = optarg;
break;
case 'D':
divert = 1;
break;
case 'd':
delay = strtonum(optarg, 0, INT_MAX, &errstr);
if (errstr != NULL)
errx(1, "delay is %s: %s",
errstr, optarg);
break;
case 'H':
hopbyhop = 1;
break;
case 'i':
idle = strtonum(optarg, 0, INT_MAX, &errstr);
if (errstr != NULL)
errx(1, "idle is %s: %s",
errstr, optarg);
break;
case 'l':
udplength = strtonum(optarg, 0, IP_MAXPACKET, &errstr);
if (errstr != NULL)
errx(1, "payload length is %s: %s",
errstr, optarg);
break;
case 'm':
mmsglen = strtonum(optarg, 0, 1024, &errstr);
if (errstr != NULL)
errx(1, "msghdr size is %s: %s",
errstr, optarg);
break;
case 'N':
repeat = strtonum(optarg, 0, 256, &errstr);
if (errstr != NULL)
errx(1, "repeat number is %s: %s",
errstr, optarg);
break;
case 'P':
packetrate = strtonum(optarg, 0, LONG_MAX, &errstr);
if (errstr != NULL)
errx(1, "packets per second rate is %s: %s",
errstr, optarg);
break;
case 'p':
service = optarg;
break;
case 'R':
progname = optarg;
break;
case 'r':
remotessh = optarg;
break;
case 't':
timeout = strtonum(optarg, 0, INT_MAX, &errstr);
if (errstr != NULL)
errx(1, "timeout is %s: %s",
errstr, optarg);
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
if (argc > 2)
usage();
if (argc < 1)
errx(1, "send or recv required");
if (strcmp(argv[0], "send") == 0) {
sendmode = 1;
setprogname("udpbench send");
} else if (strcmp(argv[0], "recv") == 0) {
sendmode = 0;
setprogname("udpbench recv");
} else
errx(1, "bad send or recv: %s", argv[0]);
if (sendmode && argc < 2)
errx(1, "hostname required for send");
if (argc >= 2)
hostname = argv[1];
if (bitrate && packetrate)
errx(1, "either bitrate or packetrate may be given");
#ifdef __OpenBSD__
if (sendmode && hopbyhop) {
;
} else if (remotessh != NULL) {
if (pledge("stdio dns inet proc exec", NULL) == -1)
err(1, "pledge");
} else if (repeat) {
if (pledge("stdio dns inet proc", NULL) == -1)
err(1, "pledge");
} else {
if (pledge("stdio dns inet", NULL) == -1)
err(1, "pledge");
}
#endif
memset(&act, 0, sizeof(act));
act.sa_handler = alarm_handler;
act.sa_flags = SA_RESETHAND;
if (sigaction(SIGALRM, &act, NULL) == -1)
err(1, "sigaction");
timerclear(&start);
timerclear(&stop);
timerclear(&final);
if (sendmode)
udp_connect_send(&start, &stop);
else
udp_bind_receive(&start, &stop, &final);
if (timerisset(&stop)) {
status_final(&start, &stop);
if (!sendmode && idle && final.tv_sec < idle) {
errx(1, "not enough idle time: %lld.%06ld",
(long long)final.tv_sec, final.tv_usec);
}
}
return 0;
}
void
udp_connect_send(struct timeval *start, struct timeval *stop)
{
const char *remotehost, *remoteserv;
char localaddr[NI_MAXHOST], localport[NI_MAXSERV];
long sendrate;
int udp_socket, udp_family = AF_UNSPEC;
FILE *ssh_stream;
pid_t ssh_pid;
remotehost = hostname;
remoteserv = service;
if (remotessh != NULL) {
char remoteaddr[NI_MAXHOST], remoteport[NI_MAXSERV];
ssh_pid = ssh_bind(&ssh_stream, remotehost, remoteserv);
#ifdef __OpenBSD__
if (hopbyhop) {
;
} else if (repeat) {
if (pledge("stdio dns inet proc", NULL) == -1)
err(1, "pledge");
} else {
if (pledge("stdio dns inet", NULL) == -1)
err(1, "pledge");
}
#endif
ssh_getpeername(ssh_stream, remoteaddr, remoteport);
if (!divert) {
remotehost = remoteaddr;
remoteserv = remoteport;
}
}
udp_socket = udp_connect(&udp_family, remotehost, remoteserv);
if (gettimeofday(start, NULL) == -1)
err(1, "gettimeofday start");
udp_getsockname(udp_socket, localaddr, localport);
if (repeat > 0) {
udp_socket_fork(&udp_socket, getpeername, connect);
if (gettimeofday(start, NULL) == -1)
err(1, "gettimeofday start");
if (udp_socket_wait(udp_socket, ssh_pid, ssh_stream))
return;
}
if (buffersize)
udp_setbuffersize(udp_socket, SO_SNDBUF, buffersize);
if (hopbyhop) {
if (udp_family != AF_INET6)
errx(1, "hopbyhop only allowed with IPv6");
udp_setrouteralert(udp_socket);
#ifdef __OpenBSD__
if (pledge("stdio dns inet", NULL) == -1)
err(1, "pledge");
#endif
}
if (bitrate) {
unsigned long etherlen;
etherlen = udp2etherlength(udplength, udp_family);
sendrate = bitrate / 8 / etherlen;
if (sendrate == 0)
errx(1, "bitrate %llu too small for ether %lu",
bitrate, etherlen);
} else
sendrate = packetrate;
if (delay) {
if (send(udp_socket, "", 0, 0) == -1)
err(1, "send initial");
sleep(delay);
}
if (timeout > 0)
alarm(timeout);
udp_send(udp_socket, udp_family, sendrate);
if (gettimeofday(stop, NULL) == -1)
err(1, "gettimeofday stop");
if (close(udp_socket) == -1)
err(1, "close");
if (repeat == 0 && remotessh != NULL)
ssh_wait(ssh_pid, ssh_stream);
}
void
udp_bind_receive(struct timeval *start, struct timeval *stop,
struct timeval *final)
{
const char *localhost, *localserv;
char localaddr[NI_MAXHOST], localport[NI_MAXSERV];
int udp_socket, udp_family = AF_UNSPEC;
FILE *ssh_stream;
pid_t ssh_pid;
localhost = hostname;
localserv = service;
udp_socket = udp_bind(&udp_family, localhost, localserv);
if (gettimeofday(start, NULL) == -1)
err(1, "gettimeofday start");
udp_getsockname(udp_socket, localaddr, localport);
if (!divert) {
localhost = localaddr;
localserv = localport;
}
if (repeat > 0) {
udp_socket_fork(&udp_socket, getsockname, bind);
if (gettimeofday(start, NULL) == -1)
err(1, "gettimeofday start");
}
if ((repeat == 0 || udp_socket == -1) && remotessh != NULL) {
char remoteaddr[NI_MAXHOST], remoteport[NI_MAXSERV];
ssh_pid = ssh_connect(&ssh_stream, localhost, localserv);
#ifdef __OpenBSD__
if (repeat) {
if (pledge("stdio dns inet proc", NULL) == -1)
err(1, "pledge");
} else {
if (pledge("stdio dns inet", NULL) == -1)
err(1, "pledge");
}
#endif
ssh_getpeername(ssh_stream, remoteaddr, remoteport);
}
if (repeat > 0) {
if (udp_socket_wait(udp_socket, ssh_pid, ssh_stream))
return;
}
if (buffersize)
udp_setbuffersize(udp_socket, SO_RCVBUF, buffersize);
if (timeout > 0)
alarm(timeout + delay + idle + 1);
udp_receive(udp_socket, udp_family, final);
if (gettimeofday(stop, NULL) == -1)
err(1, "gettimeofday stop");
if (close(udp_socket) == -1)
err(1, "close");
if (repeat == 0 && remotessh != NULL)
ssh_wait(ssh_pid, ssh_stream);
}
void
udp_socket_fork(int *udp_socket,
int(*getname)(int, struct sockaddr *, socklen_t *),
int(*setname)(int, const struct sockaddr *, socklen_t))
{
char localaddr[NI_MAXHOST], localport[NI_MAXSERV];
struct sockaddr_storage ss;
socklen_t sslen;
int n;
sslen = sizeof(ss);
if (getname(*udp_socket, (struct sockaddr *)&ss, &sslen) == -1)
err(1, "getname");
for (n = repeat; n > 0; n--) {
switch (fork()) {
case -1:
err(1, "fork");
default:
/* parent */
if (close(*udp_socket) == -1)
err(1, "close %d", n);
*udp_socket = -1;
if (n == 1)
break;
switch (ss.ss_family) {
struct sockaddr_in *sin;
struct sockaddr_in6 *sin6;
case AF_INET:
sin = (struct sockaddr_in *)&ss;
((uint8_t *)&sin->sin_addr.s_addr)[3]++;
break;
case AF_INET6:
sin6 = (struct sockaddr_in6 *)&ss;
((uint8_t *)&sin6->sin6_addr.s6_addr)[15]++;
break;
}
*udp_socket = socket(ss.ss_family, SOCK_DGRAM,
IPPROTO_UDP);
if (*udp_socket == -1)
err(1, "socket %d", n);
if (setname(*udp_socket, (struct sockaddr *)&ss, sslen)
== -1)
err(1, "setname %d", n);
udp_getsockname(*udp_socket, localaddr, localport);
break;
case 0:
/* child */
#ifdef __OpenBSD__
if (pledge("stdio dns inet", NULL) == -1)
err(1, "pledge");
#endif
{
static char name[32];
snprintf(name, sizeof(name), "udpbench %s %d",
sendmode ? "send" : "recv", repeat - n);
setprogname(name);
}
n = 0;
break;
}
}
}
int
udp_socket_wait(int udp_socket, pid_t ssh_pid, FILE *ssh_stream)
{
int n;
if (udp_socket != -1)
return 0;
if (remotessh != NULL) {
for (n = repeat; n > 1; n--) {
char remoteaddr[NI_MAXHOST], remoteport[NI_MAXSERV];
ssh_getpeername(ssh_stream, remoteaddr, remoteport);
}
ssh_wait(ssh_pid, ssh_stream);
}
for (n = repeat; n > 0; n--) {
int status;
if (wait(&status) == -1)
err(1, "wait");
if (status != 0)
errx(1, "status: %d", status);
}
return 1;
}
void
alarm_handler(int sig)
{
alarm_signaled = 1;
}
int
udp_bind(int *udp_family, const char *host, const char *serv)
{
struct addrinfo hints, *res, *res0;
int error, udp_socket;
int save_errno;
const char *cause = NULL;
memset(&hints, 0, sizeof(hints));
hints.ai_family = *udp_family;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
hints.ai_flags = AI_PASSIVE;
error = getaddrinfo(host, serv, &hints, &res0);
if (error)
errx(1, "getaddrinfo: %s", gai_strerror(error));
udp_socket = -1;
for (res = res0; res; res = res->ai_next) {
if (divert) {
/* pf divert packet uses raw socket */
res->ai_socktype = SOCK_RAW;
#ifdef IPPROTO_DIVERT
res->ai_protocol = IPPROTO_DIVERT;
#else
errx(1, "IPPROTO_DIVERT not defined");
#endif
}
udp_socket = socket(res->ai_family, res->ai_socktype,
res->ai_protocol);
if (udp_socket == -1) {
cause = "socket";
continue;
}
if (divert) {
/* divert packet socket is bound to port only */
if (res->ai_family == AF_INET) {
struct sockaddr_in *sin;
sin = (struct sockaddr_in *)res->ai_addr;
memset(&sin->sin_addr, 0,
sizeof(struct in_addr));
if (sin->sin_port == 0)
errx(1, "divert needs bind port");
}
if (res->ai_family == AF_INET) {
struct sockaddr_in6 *sin6;
sin6 = (struct sockaddr_in6 *)res->ai_addr;
memset(&sin6->sin6_addr, 0,
sizeof(struct in6_addr));
if (sin6->sin6_port == 0)
errx(1, "divert needs bind port");
}
}
if (bind(udp_socket, res->ai_addr, res->ai_addrlen) == -1) {
cause = "bind";
save_errno = errno;
close(udp_socket);
errno = save_errno;
udp_socket = -1;
continue;
}
break; /* okay we got one */
}
if (udp_socket == -1)
err(1, "%s", cause);
*udp_family = res->ai_family;
freeaddrinfo(res0);
return udp_socket;
}
int
udp_connect(int *udp_family, const char *host, const char *serv)
{
struct addrinfo hints, *res, *res0;
int error, udp_socket;
int save_errno;
const char *cause = NULL;
memset(&hints, 0, sizeof(hints));
hints.ai_family = *udp_family;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
error = getaddrinfo(host, serv, &hints, &res0);
if (error)
errx(1, "getaddrinfo: %s", gai_strerror(error));
udp_socket = -1;
for (res = res0; res; res = res->ai_next) {
udp_socket = socket(res->ai_family, res->ai_socktype,
res->ai_protocol);
if (udp_socket == -1) {
cause = "socket";
continue;
}
if (connect(udp_socket, res->ai_addr, res->ai_addrlen) == -1) {
cause = "connect";
save_errno = errno;
close(udp_socket);
errno = save_errno;
udp_socket = -1;
continue;
}
break; /* okay we got one */
}
if (udp_socket == -1)
err(1, "%s", cause);
*udp_family = res->ai_family;
freeaddrinfo(res0);
return udp_socket;
}
void
udp_getsockname(int udp_socket, char *addr, char *port)
{
struct sockaddr_storage ss;
socklen_t sslen;
int error;
sslen = sizeof(ss);
if (getsockname(udp_socket, (struct sockaddr *)&ss, &sslen) == -1)
err(1, "getsockname");
error = getnameinfo((struct sockaddr *)&ss, sslen, addr, NI_MAXHOST,
port, NI_MAXSERV, NI_NUMERICHOST | NI_NUMERICSERV | NI_DGRAM);
if (error)
errx(1, "getnameinfo: %s", gai_strerror(error));
printf("sockname: %s %s\n", addr, port);
}
void
udp_setbuffersize(int udp_socket, int name, int size)
{
socklen_t len;
len = sizeof(size);
if (setsockopt(udp_socket, SOL_SOCKET, name, &size, len) == -1)
err(1, "setsockopt buffer size %d", size);
}
void
udp_setrouteralert(int udp_socket)
{
struct {
struct ip6_hbh hbh;
struct ip6_opt_router ra;
u_char pad[2];
} opts;
socklen_t len;
opts.hbh.ip6h_nxt = IPPROTO_UDP;
opts.hbh.ip6h_len = (sizeof(opts) - 1) / 8;
opts.ra.ip6or_type = IP6OPT_ROUTER_ALERT;
opts.ra.ip6or_len = sizeof(opts.ra) - 2;
*(uint16_t *)opts.ra.ip6or_value = IP6_ALERT_AN;
opts.pad[0] = IP6OPT_PAD1;
opts.pad[1] = IP6OPT_PAD1;
len = sizeof(opts);
if (setsockopt(udp_socket, IPPROTO_IPV6, IPV6_HOPOPTS, &opts, len)
== -1)
err(1, "setsockopt router alert");
}
struct mmsghdr *
mmsg_alloc(int packets, size_t paylen, int fill)
{
struct mmsghdr *mmsg, *mhdr;
struct iovec *iov;
char *payload;
if ((mmsg = calloc(packets, sizeof(struct mmsghdr))) == NULL)
err(1, "calloc mmsghdr");
if ((iov = calloc(packets, sizeof(struct iovec))) == NULL)
err(1, "calloc iovec");
if ((payload = calloc(packets, paylen)) == NULL)
err(1, "calloc payload");
if (fill)
arc4random_buf(payload, packets * paylen);
mhdr = mmsg;
while (packets > 0) {
mhdr->msg_hdr.msg_iov = iov;
mhdr->msg_hdr.msg_iovlen = 1;
iov->iov_base = payload;
iov->iov_len = paylen;
mhdr++;
iov++;
payload += paylen;
packets--;
}
return mmsg;
}
void
mmsg_free(struct mmsghdr *mmsg)
{
free(mmsg->msg_hdr.msg_iov->iov_base);
free(mmsg->msg_hdr.msg_iov);
free(mmsg);
}
void
udp_send(int udp_socket, int udp_family, unsigned long sendrate)
{
struct timeval begin, end, duration;
struct timespec wait;
unsigned long syscall, packet;
struct mmsghdr *mmsg;
char *payload;
size_t udplen;
ssize_t sndlen;
int pkts;
udplen = udplength;
if (mmsglen) {
mmsg = mmsg_alloc(mmsglen, udplen, 1);
} else {
pkts = 1;
if ((payload = malloc(udplen)) == NULL)
err(1, "malloc payload");
arc4random_buf(payload, udplen);
}
if (gettimeofday(&begin, NULL) == -1)
err(1, "gettimeofday begin");
timerclear(&end);
syscall = 0;
packet = 0;
sndlen = 0;
while (!alarm_signaled) {
syscall++;
if (mmsglen)
pkts = sendmmsg(udp_socket, mmsg, mmsglen, 0);
else
sndlen = send(udp_socket, payload, udplen, 0);
if (pkts == -1 || sndlen == -1) {
if (errno == ENOBUFS || errno == EINTR)
continue;
err(1, "send");
}
packet += pkts;
if (sendrate) {
double expectduration, waittime;
if (!timerisset(&end)) {
if (gettimeofday(&end, NULL) == -1)
err(1, "gettimeofday rate before");
}
timersub(&end, &begin, &duration);
if (!timerisset(&duration))
duration.tv_usec = 1;
expectduration = (double)packet / (double)sendrate;
waittime = expectduration - (double)duration.tv_sec -
(double)duration.tv_usec / 1000000.;
wait.tv_sec = waittime;
wait.tv_nsec = (waittime - (double)wait.tv_sec) *
1000000000.;
if (wait.tv_sec > 0 || wait.tv_nsec > 0) {
nanosleep(&wait, NULL);
if (gettimeofday(&end, NULL) == -1)
err(1, "gettimeofday rate after");
}
}
}
if (gettimeofday(&end, NULL) == -1)
err(1, "gettimeofday end");
status_init("send", syscall, packet, udplen, udp_family, &begin, &end);
if (mmsglen)
mmsg_free(mmsg);
else
free(payload);
}
void
udp_receive(int udp_socket, int udp_family, struct timeval *final)
{
struct timeval begin, end, timeo;
unsigned long syscall, packet;
unsigned long headerlen, paylen;
long bored;
struct mmsghdr *mmsg;
char *payload;
size_t udplen;
ssize_t rcvlen;
socklen_t len;
int pkts;
udplen = udplength;
if (divert) {
headerlen = sizeof(struct udphdr);
if (udp_family == AF_INET)
headerlen += sizeof(struct ip);
if (udp_family == AF_INET6)
headerlen += sizeof(struct ip6_hdr);
udplen += headerlen;
}
if (mmsglen)
mmsg = mmsg_alloc(mmsglen, udplen + 1, 0);
else
pkts = 1;
if ((payload = malloc(udplen + 1)) == NULL)
err(1, "malloc payload");
if (delay) {
/* before the delay expect an empty initial packet */
rcvlen = recv(udp_socket, payload, udplen + 1, 0);
if (rcvlen == -1)
err(1, "recv initial");
if (rcvlen > 0)
warnx("receive initial packet not empty %zd", rcvlen);
}
/* wait for the first packet to start timing */
rcvlen = recv(udp_socket, payload, udplen + 1, 0);
if (rcvlen == -1)
err(1, "recv first");
paylen = rcvlen;
if (paylen < udplen)
warnx("receive packet truncated %zd", rcvlen);
if (paylen > udplen)
warnx("receive packet oversized %zd", rcvlen);
if (divert) {
if (paylen < headerlen)
errx(1, "receive length %zd too short for header",
rcvlen);
paylen -= headerlen;
}
if (gettimeofday(&begin, NULL) == -1)
err(1, "gettimeofday begin");
timerclear(final);
timerclear(&timeo);
timeo.tv_usec = 100000;
len = sizeof(timeo);
if (setsockopt(udp_socket, SOL_SOCKET, SO_RCVTIMEO, &timeo, len) == -1)
err(1, "setsockopt recv timeout");
syscall = 1;
packet = 1;
bored = 0;
while (!alarm_signaled) {
syscall++;
if (mmsglen)
pkts = recvmmsg(udp_socket, mmsg, mmsglen, 0, NULL);
else
rcvlen = recv(udp_socket, payload, udplen + 1, 0);
if (pkts == -1 || rcvlen == -1) {
if (errno == EWOULDBLOCK) {
bored++;
if (bored == 1) {
if (gettimeofday(final, NULL) == -1)
err(1, "gettimeofday final");
/* packet was seen before timeout */
timersub(final, &timeo, final);
}
if (idle && bored * timeo.tv_usec >
1000000L * idle ) {
/* more than a second idle time */
break;
}
continue;
}
if (errno == EINTR)
continue;
err(1, "recv");
}
timerclear(final);
bored = 0;
packet += pkts;
}
if (gettimeofday(&end, NULL) == -1)
err(1, "gettimeofday end");
if (timerisset(final)) {
struct timeval tmp;
tmp = end;
/* last packet was seen at final time */
end = *final;
/* new final is duration without packets */
timersub(&tmp, final, final);
}
status_init("recv", syscall, packet, paylen, udp_family, &begin, &end);
if (mmsglen)
mmsg_free(mmsg);
free(payload);
}
void
status_init(const char *action, unsigned long syscall, unsigned long packet,
unsigned long paylen, int af, const struct timeval *begin,
const struct timeval *end)
{
struct timeval duration;
unsigned long frame, iplen, etherlen;
double bits;
iplen = udp2iplength(paylen, af, &frame);
etherlen = udp2etherlength(paylen, af);
bits = (double)packet * etherlen * 8;
timersub(end, begin, &duration);
bits /= (double)duration.tv_sec + (double)duration.tv_usec / 1000000;
snprintf(status_line, sizeof(status_line),
"%s: syscalls %lu, packets %lu, "
"frame %lu, payload %lu, ip %lu, ether %lu, "
"begin %lld.%06ld, end %lld.%06ld, "
"duration %lld.%06ld, bit/s %e",
action, syscall, packet, frame, paylen, iplen, etherlen,
(long long)begin->tv_sec, begin->tv_usec,
(long long)end->tv_sec, end->tv_usec,
(long long)duration.tv_sec, duration.tv_usec, bits);
}
void
status_final(const struct timeval *start, const struct timeval *stop)
{
size_t len;
len = strlen(status_line);
if (len >= sizeof(status_line))
return;
snprintf(status_line + len, sizeof(status_line) - len,
", start %lld.%06ld, stop %lld.%06ld\n",
(long long)start->tv_sec, start->tv_usec,
(long long)stop->tv_sec, stop->tv_usec);
len = strlen(status_line);
fflush(stdout);
fwrite(status_line, len, 1, stdout);
fflush(stdout);
}
unsigned long
udp2iplength(unsigned long payload, int af, unsigned long *packets)
{
unsigned long iplength = 0;
/* encapsulate IP in IP */
if (strcmp(pseudo, "gif") == 0)
iplength += 20;
if (strcmp(pseudo, "gif6") == 0)
iplength += 40;
if (strcmp(pseudo, "gre") == 0)
iplength += 20 + 8;
/* IPv4 header */
if (af == AF_INET)
iplength += 20;
/* IPv6 header */
if (af == AF_INET6)
iplength += 40;
/* UDP header, payload */
iplength += 8 + payload;
/* maximum ethernet payload */
if (iplength > 1500) {
if (af == AF_INET) {
/* length without IPv4 header, align to fragment */
*packets = (iplength - 20) / ((1500 - 20) & ~7);
/* final fragment */
iplength = (iplength - 20) % ((1500 - 20) & ~7);
if (iplength == 0) {
/* full sized fragments */
iplength = *packets * (((1500 - 20) & ~7) + 20);
} else {
/* final fragment header, full fragments */
iplength += 20 +
(*packets * (((1500 - 20) & ~7) + 20));
++*packets;
}
}
if (af == AF_INET6) {
/* length without IPv6, fragement header, alignment */
*packets = (iplength - 40) / ((1500 - 40 - 8) & ~7);
/* final fragment */
iplength = (iplength - 40) % ((1500 - 40 - 8) & ~7);
if (iplength == 0) {
/* full sized fragments */
iplength = *packets * (1500 & ~7);
} else {
/* final fragment header, full fragments */
iplength += 40 + 8 + (*packets * (1500 & ~7));
++*packets;
}
}
} else
*packets = 1;