xref: /xnu-8796.141.3/tests/mcast_group_race_82820812.c (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions #include <arpa/inet.h>
2*1b191cb5SApple OSS Distributions #include <pthread.h>
3*1b191cb5SApple OSS Distributions #include <unistd.h>
4*1b191cb5SApple OSS Distributions #include <darwintest.h>
5*1b191cb5SApple OSS Distributions #include <TargetConditionals.h>
6*1b191cb5SApple OSS Distributions 
7*1b191cb5SApple OSS Distributions volatile static int lock_a;
8*1b191cb5SApple OSS Distributions volatile static int lock_b;
9*1b191cb5SApple OSS Distributions 
10*1b191cb5SApple OSS Distributions static int fd;
11*1b191cb5SApple OSS Distributions static struct sockaddr_in saddr;
12*1b191cb5SApple OSS Distributions 
13*1b191cb5SApple OSS Distributions static struct ip_mreq filler_group;
14*1b191cb5SApple OSS Distributions static struct ip_mreq group_a;
15*1b191cb5SApple OSS Distributions static struct ip_mreq group_b;
16*1b191cb5SApple OSS Distributions 
17*1b191cb5SApple OSS Distributions #define ITERATIONS_LIMIT 1000
18*1b191cb5SApple OSS Distributions 
19*1b191cb5SApple OSS Distributions static void *
thread_func(__unused void * arg)20*1b191cb5SApple OSS Distributions thread_func(__unused void* arg)
21*1b191cb5SApple OSS Distributions {
22*1b191cb5SApple OSS Distributions 	lock_a = 1;
23*1b191cb5SApple OSS Distributions 	while (lock_b == 0) {
24*1b191cb5SApple OSS Distributions 	}
25*1b191cb5SApple OSS Distributions 
26*1b191cb5SApple OSS Distributions 	setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &group_a, sizeof(group_a));
27*1b191cb5SApple OSS Distributions 
28*1b191cb5SApple OSS Distributions 	return NULL;
29*1b191cb5SApple OSS Distributions }
30*1b191cb5SApple OSS Distributions 
31*1b191cb5SApple OSS Distributions T_DECL(mcast_group_race_82820812, "Race between multicast group join operations.",
32*1b191cb5SApple OSS Distributions     T_META_ASROOT(true),
33*1b191cb5SApple OSS Distributions     T_META_ENABLED(!TARGET_OS_BRIDGE && !TARGET_OS_SIMULATOR))
34*1b191cb5SApple OSS Distributions {
35*1b191cb5SApple OSS Distributions 	pthread_t th;
36*1b191cb5SApple OSS Distributions 	uint32_t i = 0;
37*1b191cb5SApple OSS Distributions 	uint32_t j = 0;
38*1b191cb5SApple OSS Distributions 
39*1b191cb5SApple OSS Distributions 	saddr.sin_family = AF_INET;
40*1b191cb5SApple OSS Distributions 
41*1b191cb5SApple OSS Distributions 	group_a.imr_multiaddr.s_addr = inet_addr("224.0.0.1");
42*1b191cb5SApple OSS Distributions 	group_b.imr_multiaddr.s_addr = inet_addr("224.0.0.2");
43*1b191cb5SApple OSS Distributions 
44*1b191cb5SApple OSS Distributions 	for (i = 0; i < ITERATIONS_LIMIT; ++i) {
45*1b191cb5SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(fd = socket(AF_INET, SOCK_DGRAM, 0), "socket");
46*1b191cb5SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(bind(fd, (struct sockaddr *) &saddr, sizeof(saddr)), "bind");
47*1b191cb5SApple OSS Distributions 
48*1b191cb5SApple OSS Distributions 		for (j = 0; j < IP_MIN_MEMBERSHIPS - 1; ++j) {
49*1b191cb5SApple OSS Distributions 			filler_group.imr_multiaddr.s_addr = htonl(ntohl(inet_addr("224.0.0.3")) + j);
50*1b191cb5SApple OSS Distributions 			setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &filler_group, sizeof(filler_group));
51*1b191cb5SApple OSS Distributions 		}
52*1b191cb5SApple OSS Distributions 
53*1b191cb5SApple OSS Distributions 		T_ASSERT_POSIX_ZERO(pthread_create(&th, NULL, thread_func, NULL), "pthread_create");
54*1b191cb5SApple OSS Distributions 
55*1b191cb5SApple OSS Distributions 		while (lock_a == 0) {
56*1b191cb5SApple OSS Distributions 		}
57*1b191cb5SApple OSS Distributions 		lock_b = 1;
58*1b191cb5SApple OSS Distributions 
59*1b191cb5SApple OSS Distributions 		setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &group_b, sizeof(group_b));
60*1b191cb5SApple OSS Distributions 
61*1b191cb5SApple OSS Distributions 		T_ASSERT_POSIX_ZERO(pthread_join(th, NULL), "pthread_join");
62*1b191cb5SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(close(fd), "close");
63*1b191cb5SApple OSS Distributions 	}
64*1b191cb5SApple OSS Distributions }
65