forked from petershh/minix
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsocklib.c
More file actions
3600 lines (2801 loc) · 96.7 KB
/
Copy pathsocklib.c
File metadata and controls
3600 lines (2801 loc) · 96.7 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
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
/*
* Socket test code library. This file contains code that is worth sharing
* between TCP/IP and UDS tests, as well as code that is worth sharing between
* various TCP/IP tests.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <sys/param.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet6/in6_var.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <unistd.h>
#include <fcntl.h>
#include "common.h"
#include "socklib.h"
#define TEST_PORT_A 12345 /* this port should be free and usable */
#define TEST_PORT_B 12346 /* this port should be free and usable */
#define LOOPBACK_IFNAME "lo0" /* loopback interface name */
#define LOOPBACK_IPV4 "127.0.0.1" /* IPv4 address */
#define LOOPBACK_IPV6_LL "fe80::1" /* link-local IPv6 address */
/* These address should simply eat all packets. */
#define TEST_BLACKHOLE_IPV4 "127.255.0.254"
#define TEST_BLACKHOLE_IPV6 "::2"
#define TEST_BLACKHOLE_IPV6_LL "fe80::ffff"
/* Addresses for multicast-related testing. */
#define TEST_MULTICAST_IPV4 "233.252.0.1" /* RFC 5771 Sec. 9.2 */
#define TEST_MULTICAST_IPV6 "ff0e::db8:0:1" /* RFC 6676 Sec. 3 */
#define TEST_MULTICAST_IPV6_LL "ff02::db8:0:1"
#define TEST_MULTICAST_IPV6_BAD "ff00::db8:0:1"
#define BAD_IFINDEX 255 /* guaranteed not to belong to an interface */
/* 0 = check, 1 = generate source, 2 = generate CSV */
#define SOCKLIB_SWEEP_GENERATE 0
#if SOCKLIB_SWEEP_GENERATE
/* Link against minix/usr.bin/trace/error.o to make this work! */
const char *get_error_name(int err);
#if SOCKLIB_SWEEP_GENERATE == 2
static const char *statename[S_MAX] = {
"S_NEW",
"S_N_SHUT_R",
"S_N_SHUT_W",
"S_N_SHUT_RW",
"S_BOUND",
"S_LISTENING",
"S_L_SHUT_R",
"S_L_SHUT_W",
"S_L_SHUT_RW",
"S_CONNECTING",
"S_C_SHUT_R",
"S_C_SHUT_W",
"S_C_SHUT_RW",
"S_CONNECTED",
"S_ACCEPTED",
"S_SHUT_R",
"S_SHUT_W",
"S_SHUT_RW",
"S_RSHUT_R",
"S_RSHUT_W",
"S_RSHUT_RW",
"S_SHUT2_R",
"S_SHUT2_W",
"S_SHUT2_RW",
"S_PRE_EOF",
"S_AT_EOF",
"S_POST_EOF",
"S_PRE_SHUT_R",
"S_EOF_SHUT_R",
"S_POST_SHUT_R",
"S_PRE_SHUT_W",
"S_EOF_SHUT_W",
"S_POST_SHUT_W",
"S_PRE_SHUT_RW",
"S_EOF_SHUT_RW",
"S_POST_SHUT_RW",
"S_PRE_RESET",
"S_AT_RESET",
"S_POST_RESET",
"S_FAILED",
"S_POST_FAILED",
};
#endif
static const char *callname[C_MAX] = {
"C_ACCEPT",
"C_BIND",
"C_CONNECT",
"C_GETPEERNAME",
"C_GETSOCKNAME",
"C_GETSOCKOPT_ERR",
"C_GETSOCKOPT_KA",
"C_GETSOCKOPT_RB",
"C_IOCTL_NREAD",
"C_LISTEN",
"C_RECV",
"C_RECVFROM",
"C_SEND",
"C_SENDTO",
"C_SELECT_R",
"C_SELECT_W",
"C_SELECT_X",
"C_SETSOCKOPT_BC",
"C_SETSOCKOPT_KA",
"C_SETSOCKOPT_L",
"C_SETSOCKOPT_RA",
"C_SHUTDOWN_R",
"C_SHUTDOWN_RW",
"C_SHUTDOWN_W",
};
#endif
static int socklib_sigpipe;
/*
* Signal handler for SIGPIPE signals.
*/
static void
socklib_signal(int sig)
{
if (sig != SIGPIPE) e(0);
socklib_sigpipe++;
}
/*
* The given socket file descriptor 'fd' has been set up in the desired state.
* Perform the given call 'call' on it, possibly using local socket address
* 'local_addr' (for binding) or remote socket address 'remote_addr' (for
* connecting or to store resulting addresses), both of size 'addr_len'.
* Return the result of the call, using a positive value if the call succeeded,
* or a negated errno code if the call failed.
*/
int
socklib_sweep_call(enum call call, int fd, struct sockaddr * local_addr,
struct sockaddr * remote_addr, socklen_t addr_len)
{
char data[1];
struct linger l;
fd_set fd_set;
struct timeval tv;
socklen_t len;
int i, r, fd2;
fd2 = -1;
switch (call) {
case C_ACCEPT:
r = accept(fd, remote_addr, &addr_len);
if (r >= 0)
fd2 = r;
break;
case C_BIND:
r = bind(fd, local_addr, addr_len);
break;
case C_CONNECT:
r = connect(fd, remote_addr, addr_len);
break;
case C_GETPEERNAME:
r = getpeername(fd, remote_addr, &addr_len);
break;
case C_GETSOCKNAME:
r = getsockname(fd, remote_addr, &addr_len);
break;
case C_GETSOCKOPT_ERR:
len = sizeof(i);
r = getsockopt(fd, SOL_SOCKET, SO_ERROR, &i, &len);
/*
* We assume this call always succeeds, and test against the
* pending error.
*/
if (r != 0) e(0);
if (i != 0) {
r = -1;
errno = i;
}
break;
case C_GETSOCKOPT_KA:
len = sizeof(i);
r = getsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &i, &len);
break;
case C_GETSOCKOPT_RB:
len = sizeof(i);
r = getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &i, &len);
break;
case C_IOCTL_NREAD:
r = ioctl(fd, FIONREAD, &i);
/* On success, we test against the returned value here. */
if (r == 0)
r = i;
break;
case C_LISTEN:
r = listen(fd, 1);
break;
case C_RECV:
r = recv(fd, data, sizeof(data), 0);
break;
case C_RECVFROM:
r = recvfrom(fd, data, sizeof(data), 0, remote_addr,
&addr_len);
break;
case C_SEND:
data[0] = 0;
r = send(fd, data, sizeof(data), 0);
break;
case C_SENDTO:
data[0] = 0;
r = sendto(fd, data, sizeof(data), 0, remote_addr, addr_len);
break;
case C_SETSOCKOPT_BC:
i = 0;
r = setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &i, sizeof(i));
break;
case C_SETSOCKOPT_KA:
i = 1;
r = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &i, sizeof(i));
break;
case C_SETSOCKOPT_L:
l.l_onoff = 1;
l.l_linger = 0;
r = setsockopt(fd, SOL_SOCKET, SO_LINGER, &l, sizeof(l));
break;
case C_SETSOCKOPT_RA:
i = 1;
r = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
break;
case C_SELECT_R:
case C_SELECT_W:
case C_SELECT_X:
FD_ZERO(&fd_set);
FD_SET(fd, &fd_set);
tv.tv_sec = 0;
tv.tv_usec = 0;
r = select(fd + 1, (call == C_SELECT_R) ? &fd_set : NULL,
(call == C_SELECT_W) ? &fd_set : NULL,
(call == C_SELECT_X) ? &fd_set : NULL, &tv);
break;
case C_SHUTDOWN_R:
r = shutdown(fd, SHUT_RD);
break;
case C_SHUTDOWN_W:
r = shutdown(fd, SHUT_WR);
break;
case C_SHUTDOWN_RW:
r = shutdown(fd, SHUT_RDWR);
break;
default:
r = -1;
errno = EINVAL;
e(0);
}
if (r < -1) e(0);
if (r == -1)
r = -errno;
if (fd2 >= 0 && close(fd2) != 0) e(0);
return r;
}
/*
* Perform a sweep of socket calls vs socket states, testing the outcomes
* against provided tables or (if SOCKLIB_SWEEP_GENERATE is set) reporting on
* the outcomes instead. The caller must provide the following:
*
* - the socket domain, type, and protocol to test; these are simply forwarded
* to the callback function (see below);
* - the set of S_ states to test, as array 'states' with 'nstates' elements;
* - unless generating output, a matrix of expected results as 'results', which
* is actually a two-dimensional array with dimensions [C_MAX][nstates], with
* either positive call output or a negated call errno code in each cell;
* - a callback function 'proc' that must set up a socket in the given state
* and pass it to socklib_sweep_call().
*
* The 'states' array allows each socket sweep test to support a different set
* of states, because not every type of socket can be put in every possible
* state. All calls are always tried in each state, though.
*
* The sweep also tests for SIGPIPE generation, which assumes that all calls on
* SOCK_STREAM sockets that return EPIPE, also raise a SIGPIPE signal, and that
* no other SIGPIPE signal is ever raised otherwise.
*
* Standard e() error throwing is used for set-up and result mismatches.
*/
void
socklib_sweep(int domain, int type, int protocol, const enum state * states,
unsigned int nstates, const int * results, int (* proc)(int domain,
int type, int protocol, enum state, enum call))
{
struct sigaction act, oact;
enum state state;
enum call call;
#if SOCKLIB_SWEEP_GENERATE
const char *name;
int res, *nresults;
#else
int res, exp;
#endif
memset(&act, 0, sizeof(act));
act.sa_handler = socklib_signal;
if (sigaction(SIGPIPE, &act, &oact) != 0) e(0);
#if SOCKLIB_SWEEP_GENERATE
if ((nresults = malloc(nstates * C_MAX)) == NULL) e(0);
#endif
for (state = 0; state < nstates; state++) {
for (call = 0; call < C_MAX; call++) {
socklib_sigpipe = 0;
res = proc(domain, type, protocol, states[state],
call);
/*
* If the result was EPIPE and this is a stream-type
* socket, we must have received exactly one SIGPIPE
* signal. Otherwise, we must not have received one.
* Note that technically, the SIGPIPE could arrive
* sometime after this check, but with regular system
* service scheduling that will never happen.
*/
if (socklib_sigpipe !=
(res == -EPIPE && type == SOCK_STREAM)) e(0);
#if SOCKLIB_SWEEP_GENERATE
nresults[call * nstates + state] = res;
#else
exp = results[call * nstates + state];
if (res != exp) {
printf("FAIL state %d call %d res %d exp %d\n",
state, call, res, exp);
e(0);
}
#endif
}
}
if (sigaction(SIGPIPE, &oact, NULL) != 0) e(0);
#if SOCKLIB_SWEEP_GENERATE
#if SOCKLIB_SWEEP_GENERATE == 1
/*
* Generate a table in C form, ready to be pasted into test source.
* Obviously, generated results should be hand-checked carefully before
* being pasted into a test. Arguably these tables should be hand-made
* for maximum scrutiny, but I already checked the results from the
* CSV form (#define SOCKLIB_SWEEP_GENERATE 2) and have no desire for
* RSI -dcvmoole
*/
printf("\nstatic const int X_results[][__arraycount(X_states)] = {\n");
for (call = 0; call < C_MAX; call++) {
if ((name = callname[call]) == NULL) e(0);
printf("\t[%s]%s%s%s= {", name,
(strlen(name) <= 21) ? "\t" : "",
(strlen(name) <= 13) ? "\t" : "",
(strlen(name) <= 5) ? "\t" : "");
for (state = 0; state < nstates; state++) {
if (state % 4 == 0)
printf("\n\t\t");
res = nresults[call * nstates + state];
name = (res < 0) ? get_error_name(-res) : NULL;
if (name != NULL) {
printf("-%s,", name);
if ((state + 1) % 4 != 0 &&
state < nstates - 1)
printf("%s%s",
(strlen(name) <= 13) ? "\t" : "",
(strlen(name) <= 5) ? "\t" : "");
} else {
printf("%d,", res);
if ((state + 1) % 4 != 0 &&
state < nstates - 1)
printf("\t\t");
}
}
printf("\n\t},\n");
}
printf("};\n");
#elif SOCKLIB_SWEEP_GENERATE == 2
/* Generate table in CSV form. */
printf("\n");
for (state = 0; state < nstates; state++)
printf(",%s", statename[states[state]] + 2);
for (call = 0; call < C_MAX; call++) {
printf("\n%s", callname[call] + 2);
for (state = 0; state < nstates; state++) {
res = nresults[call * nstates + state];
name = (res < 0) ? get_error_name(-res) : NULL;
if (name != NULL)
printf(",%s", name);
else
printf(",%d", res);
}
}
printf("\n");
#endif
free(nresults);
#endif
}
/*
* Test for setting and retrieving UDP/RAW multicast transmission options.
* This is an interface-level test only: we do not (yet) test whether the
* options have any effect. The given 'type' must be SOCK_DGRAM or SOCK_RAW.
*/
void
socklib_multicast_tx_options(int type)
{
struct in_addr in_addr;
socklen_t len;
unsigned int ifindex;
uint8_t byte;
int fd, val;
subtest = 10;
if ((fd = socket(AF_INET, type, 0)) < 0) e(0);
/*
* Initially, the multicast TTL is expected be 1, looping should be
* enabled, and the multicast source address should be <any>.
*/
byte = 0;
len = sizeof(byte);
if (getsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &byte, &len) != 0)
e(0);
if (len != sizeof(byte)) e(0);
if (type != SOCK_STREAM && byte != 1) e(0);
byte = 0;
len = sizeof(byte);
if (getsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &byte, &len) != 0)
e(0);
if (len != sizeof(byte)) e(0);
if (byte != 1) e(0);
len = sizeof(in_addr);
if (getsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &in_addr, &len) != 0)
e(0);
if (len != sizeof(in_addr)) e(0);
if (in_addr.s_addr != htonl(INADDR_ANY)) e(0);
/* It must not be possible to get/set IPv6 options on IPv4 sockets. */
val = 0;
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val,
sizeof(val)) != -1) e(0);
if (errno != ENOPROTOOPT) e(0);
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &val,
sizeof(val)) != -1) e(0);
if (errno != ENOPROTOOPT) e(0);
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &val,
sizeof(val) /*wrong but it doesn't matter*/) != -1) e(0);
if (errno != ENOPROTOOPT) e(0);
len = sizeof(val);
if (getsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val,
&len) != -1) e(0);
if (errno != ENOPROTOOPT) e(0);
if (getsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &val,
&len) != -1) e(0);
if (errno != ENOPROTOOPT) e(0);
if (getsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &val, &len) != -1)
e(0);
if (errno != ENOPROTOOPT) e(0);
if (close(fd) != 0) e(0);
if ((fd = socket(AF_INET6, type, 0)) < 0) e(0);
/*
* Expect the same defaults as for IPv4. IPV6_MULTICAST_IF uses an
* interface index rather than an IP address, though.
*/
val = 0;
len = sizeof(val);
if (getsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, &len) != 0)
e(0);
if (len != sizeof(val)) e(0);
if (type != SOCK_STREAM && val != 1) e(0);
val = 0;
len = sizeof(val);
if (getsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &val, &len) != 0)
e(0);
if (len != sizeof(val)) e(0);
if (val != 1) e(0);
len = sizeof(val);
if (getsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &val, &len) != 0)
e(0);
if (len != sizeof(val)) e(0);
if (val != 0) e(0);
/* It must not be possible to get/set IPv4 options on IPv6 sockets. */
byte = 0;
if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &byte,
sizeof(byte)) != -1) e(0);
if (errno != ENOPROTOOPT) e(0);
if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &byte,
sizeof(byte)) != -1) e(0);
if (errno != ENOPROTOOPT) e(0);
if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &byte,
sizeof(byte) /* wrong but it doesn't matter */) != -1) e(0);
if (errno != ENOPROTOOPT) e(0);
len = sizeof(byte);
if (getsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &val, &len) != -1)
e(0);
if (errno != ENOPROTOOPT) e(0);
if (getsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &val, &len) != -1)
e(0);
if (errno != ENOPROTOOPT) e(0);
if (getsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &val, &len) != -1)
e(0);
if (errno != ENOPROTOOPT) e(0);
if (close(fd) != 0) e(0);
/* Test changing options. */
if ((fd = socket(AF_INET, type, 0)) < 0) e(0);
byte = 129;
if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &byte,
sizeof(byte)) != 0) e(0);
byte = 0;
len = sizeof(byte);
if (getsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &byte, &len) != 0)
e(0);
if (len != sizeof(byte)) e(0);
if (byte != 129) e(0);
byte = 0;
if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &byte,
sizeof(byte)) != 0)
e(0);
byte = 1;
len = sizeof(byte);
if (getsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &byte, &len) != 0)
e(0);
if (len != sizeof(byte)) e(0);
if (byte != 0) e(0);
in_addr.s_addr = htonl(INADDR_LOOPBACK);
if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &in_addr,
sizeof(in_addr)) != 0)
e(0);
in_addr.s_addr = htonl(INADDR_ANY);
len = sizeof(in_addr);
if (getsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &in_addr, &len) != 0)
e(0);
if (len != sizeof(in_addr)) e(0);
if (in_addr.s_addr != htonl(INADDR_LOOPBACK)) e(0);
if (close(fd) != 0) e(0);
if ((fd = socket(AF_INET6, type, 0)) < 0) e(0);
val = 137;
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val,
sizeof(val)) != 0) e(0);
val = 0;
len = sizeof(val);
if (getsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, &len) != 0)
e(0);
if (len != sizeof(val)) e(0);
if (val != 137) e(0);
val = -2;
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val,
sizeof(val)) != -1) e(0);
if (errno != EINVAL) e(0);
val = 256;
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val,
sizeof(val)) != -1) e(0);
if (errno != EINVAL) e(0);
val = 0;
len = sizeof(val);
if (getsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, &len) != 0)
e(0);
if (len != sizeof(val)) e(0);
if (val != 137) e(0);
val = -1; /* use default */
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val,
sizeof(val)) != 0) e(0);
val = 0;
len = sizeof(val);
if (getsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, &len) != 0)
e(0);
if (len != sizeof(val)) e(0);
if (val != 1) e(0);
val = 0;
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &val,
sizeof(val)) != 0) e(0);
val = 1;
len = sizeof(val);
if (getsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &val, &len) != 0)
e(0);
if (len != sizeof(val)) e(0);
if (val != 0) e(0);
val = 1;
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &val,
sizeof(val)) != 0) e(0);
val = -1;
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &val,
sizeof(val)) != -1) e(0);
if (errno != EINVAL) e(0);
val = 2;
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &val,
sizeof(val)) != -1) e(0);
if (errno != EINVAL) e(0);
val = 0;
len = sizeof(val);
if (getsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &val, &len) != 0)
e(0);
if (len != sizeof(val)) e(0);
if (val != 1) e(0);
val = -1;
ifindex = if_nametoindex(LOOPBACK_IFNAME);
val = ifindex;
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &val,
sizeof(val)) != 0) e(0);
val = 0;
len = sizeof(val);
if (getsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &val, &len) != 0)
e(0);
if (len != sizeof(val)) e(0);
if (val != ifindex) e(0);
val = BAD_IFINDEX;
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &val,
sizeof(val)) != -1) e(0);
val = -1;
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &val,
sizeof(val)) != -1) e(0);
val = 0;
len = sizeof(val);
if (getsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &val, &len) != 0)
e(0);
if (len != sizeof(val)) e(0);
if (val != ifindex) e(0);
val = 0;
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &val,
sizeof(val)) != 0) e(0);
val = ifindex;
len = sizeof(val);
if (getsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &val, &len) != 0)
e(0);
if (len != sizeof(val)) e(0);
if (val != 0) e(0);
if (close(fd) != 0) e(0);
}
/*
* Test for large sends and receives on stream sockets with MSG_WAITALL.
*/
void
socklib_large_transfers(int fd[2])
{
char *buf;
pid_t pid;
int i, status;
#define LARGE_BUF (4096*1024)
if ((buf = malloc(LARGE_BUF)) == NULL) e(0);
memset(buf, 0, LARGE_BUF);
pid = fork();
switch (pid) {
case 0:
errct = 0;
if (close(fd[0]) != 0) e(0);
/* Part 1. */
if (recv(fd[1], buf, LARGE_BUF, MSG_WAITALL) != LARGE_BUF)
e(0);
for (i = 0; i < LARGE_BUF; i++)
if (buf[i] != (char)(i + (i >> 16))) e(0);
if (recv(fd[1], buf, LARGE_BUF,
MSG_DONTWAIT | MSG_WAITALL) != -1) e(0);
if (errno != EWOULDBLOCK) e(0);
/* Part 2. */
if (send(fd[1], buf, LARGE_BUF / 2, 0) != LARGE_BUF / 2) e(0);
if (shutdown(fd[1], SHUT_WR) != 0) e(0);
/* Part 3. */
memset(buf, 'y', LARGE_BUF);
if (recv(fd[1], buf, LARGE_BUF, MSG_WAITALL) != LARGE_BUF - 1)
e(0);
for (i = 0; i < LARGE_BUF - 1; i++)
if (buf[i] != (char)(i + (i >> 16))) e(0);
if (buf[LARGE_BUF - 1] != 'y') e(0);
if (recv(fd[1], buf, LARGE_BUF, MSG_WAITALL) != 0) e(0);
exit(errct);
case -1:
e(0);
}
if (close(fd[1]) != 0) e(0);
/* Part 1: check that a large send fully arrives. */
for (i = 0; i < LARGE_BUF; i++)
buf[i] = (char)(i + (i >> 16));
if (send(fd[0], buf, LARGE_BUF, 0) != LARGE_BUF) e(0);
/* Part 2: check that remote shutdown terminates a partial receive. */
memset(buf, 'x', LARGE_BUF);
if (recv(fd[0], buf, LARGE_BUF, MSG_WAITALL) != LARGE_BUF / 2) e(0);
for (i = 0; i < LARGE_BUF / 2; i++)
if (buf[i] != (char)(i + (i >> 16))) e(0);
for (; i < LARGE_BUF; i++)
if (buf[i] != 'x') e(0);
if (recv(fd[0], buf, LARGE_BUF, MSG_WAITALL) != 0) e(0);
/* Part 3: check that remote close terminates a partial receive. */
for (i = 0; i < LARGE_BUF; i++)
buf[i] = (char)(i + (i >> 16));
if (send(fd[0], buf, LARGE_BUF - 1, 0) != LARGE_BUF - 1) e(0);
if (close(fd[0]) != 0) e(0);
if (waitpid(pid, &status, 0) != pid) e(0);
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) e(0);
free(buf);
}
#define PRINT_STATS 0
/*
* A randomized producer-consumer test for stream sockets. As part of this,
* we also perform very basic bulk functionality tests of FIONREAD, MSG_PEEK,
* MSG_DONTWAIT, and MSG_WAITALL.
*/
void
socklib_producer_consumer(int fd[2])
{
char *buf;
time_t t;
socklen_t len, size, off;
ssize_t r;
pid_t pid;
int i, rcvlen, status, exp, flags, num, stat[3] = { 0, 0, 0 };
len = sizeof(rcvlen);
if (getsockopt(fd[0], SOL_SOCKET, SO_RCVBUF, &rcvlen, &len) != 0) e(0);
if (len != sizeof(rcvlen)) e(0);
size = rcvlen * 3;
if ((buf = malloc(size)) == NULL) e(0);
t = time(NULL);
/*
* We vary small versus large (random) send and receive sizes,
* splitting the entire transfer in four phases along those lines.
*
* In theory, the use of an extra system call, the use of MSG_PEEK, and
* the fact that without MSG_WAITALL a receive call may return any
* partial result, all contribute to the expectation that the consumer
* side will fall behind the producer. In order to test both filling
* and draining the receive queue, we use a somewhat larger small
* receive size for the consumer size (up to 256 bytes rather than 64)
* during each half of the four phases. The effectiveness of these
* numbers can be verified with statistics (disabled by default).
*/
#define TRANSFER_SIZE (16 * 1024 * 1024)
pid = fork();
switch (pid) {
case 0:
errct = 0;
if (close(fd[0]) != 0) e(0);
srand48(t + 1);
for (off = 0; off < TRANSFER_SIZE; ) {
if (off < TRANSFER_SIZE / 2)
len = lrand48() %
((off / (TRANSFER_SIZE / 8) % 2) ? 64 :
256);
else
len = lrand48() % size;
num = lrand48() % 16;
flags = 0;
if (num & 1) flags |= MSG_PEEK;
if (num & 2) flags |= MSG_WAITALL;
if (num & 4) flags |= MSG_DONTWAIT;
if (num & 8) {
/*
* Obviously there are race conditions here but
* the returned number should be a lower bound.
*/
if (ioctl(fd[1], FIONREAD, &exp) != 0) e(0);
if (exp < 0 || exp > rcvlen) e(0);
} else
exp = -1;
stat[0]++;
if ((r = recv(fd[1], buf, len, flags)) == -1) {
if (errno != EWOULDBLOCK) e(0);
if (exp > 0) e(0);
stat[2]++;
continue;
}
if (r < len) {
stat[1]++;
if (exp > r) e(0);
}
for (i = 0; i < r; i++)
if (buf[i] != (char)((off + i) +
((off + i) >> 16))) e(0);
if (!(flags & MSG_PEEK)) {
off += r;
if ((flags & (MSG_DONTWAIT | MSG_WAITALL)) ==
MSG_WAITALL && r != len &&
off < TRANSFER_SIZE) e(0);
}
}
#if PRINT_STATS
/*
* The second and third numbers should ideally be a large but
* non-dominating fraction of the first one.
*/
printf("RECV: total %d short %d again %d\n",
stat[0], stat[1], stat[2]);
#endif
if (close(fd[1]) != 0) e(0);
exit(errct);
case -1:
e(0);
}
if (close(fd[1]) != 0) e(0);
srand48(t);
for (off = 0; off < TRANSFER_SIZE; ) {
if (off < TRANSFER_SIZE / 4 ||
(off >= TRANSFER_SIZE / 2 && off < TRANSFER_SIZE * 3 / 4))
len = lrand48() % 64;
else
len = lrand48() % size;
if (len > TRANSFER_SIZE - off)
len = TRANSFER_SIZE - off;
for (i = 0; i < len; i++)
buf[i] = (off + i) + ((off + i) >> 16);
flags = (lrand48() % 2) ? MSG_DONTWAIT : 0;
stat[0]++;
r = send(fd[0], buf, len, flags);
if (r != len) {
if (r > (ssize_t)len) e(0);
if (!(flags & MSG_DONTWAIT)) e(0);
if (r == -1) {
if (errno != EWOULDBLOCK) e(0);
r = 0;
stat[2]++;
} else
stat[1]++;
}
if (off / (TRANSFER_SIZE / 4) !=
(off + r) / (TRANSFER_SIZE / 4))
sleep(1);
off += r;