xref: /dragonfly/test/udp/mcastbind/mcastbind.c (revision 31524921)
1 #include <sys/types.h>
2 #include <sys/socket.h>
3 
4 #include <arpa/inet.h>
5 #include <netinet/in.h>
6 
7 #include <err.h>
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13 
14 static void
15 usage(const char *cmd)
16 {
17 	fprintf(stderr, "%s -m addr -p port -i addr [-a]\n", cmd);
18 	exit(1);
19 }
20 
21 static int
22 create_sock(const struct sockaddr_in *in0, const struct in_addr *iface,
23     int bind_any)
24 {
25 	struct ip_mreq mreq;
26 	struct sockaddr_in in;
27 	int s, on;
28 
29 	s = socket(AF_INET, SOCK_DGRAM, 0);
30 	if (s < 0)
31 		err(2, "socket failed");
32 
33 	on = 1;
34 	if (setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &on, sizeof(on)) < 0)
35 		err(2, "setsockopt SO_REUSEPORT failed");
36 
37 	in = *in0;
38 	if (bind_any)
39 		in.sin_addr.s_addr = htonl(INADDR_ANY);
40 
41 	if (bind(s, (const struct sockaddr *)&in, sizeof(in)) < 0)
42 		err(2, "bind failed");
43 
44 	memset(&mreq, 0, sizeof(mreq));
45 	mreq.imr_multiaddr = in0->sin_addr;
46 	mreq.imr_interface = *iface;
47 	if (setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP,
48 	    &mreq, sizeof(mreq)) < 0)
49 		err(2, "setsockopt IP_ADD_MEMBERSHIP failed");
50 
51 	return s;
52 }
53 
54 int
55 main(int argc, char *argv[])
56 {
57 	struct sockaddr_in in;
58 	struct in_addr iface;
59 	int s1, s2, opt, n, bind_any;
60 	uint8_t buf[18];
61 
62 	memset(&in, 0, sizeof(in));
63 	in.sin_family = AF_INET;
64 
65 	memset(&iface, 0, sizeof(iface));
66 	bind_any = 0;
67 
68 	while ((opt = getopt(argc, argv, "ai:m:p:")) != -1) {
69 		switch (opt) {
70 		case 'a':
71 			bind_any = 1;
72 			break;
73 
74 		case 'i':
75 			if (inet_pton(AF_INET, optarg, &iface) <= 0)
76 				usage(argv[0]);
77 			break;
78 
79 		case 'm':
80 			if (inet_pton(AF_INET, optarg, &in.sin_addr) <= 0)
81 				usage(argv[0]);
82 			break;
83 
84 		case 'p':
85 			in.sin_port = strtol(optarg, NULL, 10);
86 			in.sin_port = htons(in.sin_port);
87 			break;
88 
89 		default:
90 			usage(argv[0]);
91 		}
92 	}
93 
94 	if (in.sin_addr.s_addr == INADDR_ANY || in.sin_port == 0 ||
95 	    iface.s_addr == INADDR_ANY)
96 		usage(argv[0]);
97 
98 	s1 = create_sock(&in, &iface, bind_any);
99 	s2 = create_sock(&in, &iface, bind_any);
100 
101 	n = read(s1, buf, sizeof(buf));
102 	if (n < 0)
103 		err(2, "read 1 failed");
104 	fprintf(stderr, "read 1 got %d\n", n);
105 
106 	n = read(s2, buf, sizeof(buf));
107 	if (n < 0)
108 		err(2, "read 2 failed");
109 	fprintf(stderr, "read 2 got %d\n", n);
110 
111 	exit(0);
112 }
113