kernel/usb4bsd: Add usbdevs.h to a few Makefile which need it.
[dragonfly.git] / test / mcast / mcast.c
1 #include <sys/types.h>
2 #include <sys/socket.h>
3
4 #include <arpa/inet.h>
5 #include <netinet/in.h>
6
7 #include <errno.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12
13 static void
14 usage(const char *cmd)
15 {
16         fprintf(stderr,
17             "%s -m addr -p port [-d dst] [-i addr] [-D delay]\n", cmd);
18         exit(1);
19 }
20
21 int
22 main(int argc, char *argv[])
23 {
24         struct ip_mreq mreq;
25         struct sockaddr_in in, dst;
26         struct in_addr iface;
27         int s, opt, n, delay;
28
29         memset(&in, 0, sizeof(in));
30         in.sin_family = AF_INET;
31
32         memset(&dst, 0, sizeof(dst));
33         dst.sin_family = AF_INET;
34
35         memset(&iface, 0, sizeof(iface));
36         delay = 0;
37
38         while ((opt = getopt(argc, argv, "m:p:d:i:D:")) != -1) {
39                 switch (opt) {
40                 case 'm':
41                         if (inet_pton(AF_INET, optarg, &in.sin_addr) <= 0) {
42                                 fprintf(stderr, "invalid -m addr %s\n", optarg);
43                                 exit(1);
44                         }
45                         break;
46
47                 case 'p':
48                         in.sin_port = htons(atoi(optarg));
49                         dst.sin_port = in.sin_port;
50                         break;
51
52                 case 'd':
53                         if (inet_pton(AF_INET, optarg, &dst.sin_addr) <= 0) {
54                                 fprintf(stderr, "invalid -d addr %s\n", optarg);
55                                 exit(1);
56                         }
57                         break;
58
59                 case 'i':
60                         if (inet_pton(AF_INET, optarg, &iface) <= 0) {
61                                 fprintf(stderr, "invalid -i addr %s\n", optarg);
62                                 exit(1);
63                         }
64                         break;
65
66                 case 'D':
67                         delay = atoi(optarg);
68                         break;
69
70                 default:
71                         usage(argv[0]);
72                 }
73         }
74
75         if (in.sin_addr.s_addr == INADDR_ANY || in.sin_port == 0)
76                 usage(argv[0]);
77         if (dst.sin_addr.s_addr == 0)
78                 dst.sin_addr = in.sin_addr;
79
80         s = socket(AF_INET, SOCK_DGRAM, 0);
81         if (s < 0) {
82                 fprintf(stderr, "socket failed: %d\n", errno);
83                 exit(1);
84         }
85
86         if (bind(s, (const struct sockaddr *)&in, sizeof(in)) < 0) {
87                 fprintf(stderr, "bind failed: %d\n", errno);
88                 exit(1);
89         }
90
91         memset(&mreq, 0, sizeof(mreq));
92         mreq.imr_multiaddr = in.sin_addr;
93         if (setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP,
94             &mreq, sizeof(mreq)) < 0) {
95                 fprintf(stderr, "setsockopt IP MEMSHIP failed: %d\n", errno);
96                 exit(1);
97         }
98
99         if (iface.s_addr != INADDR_ANY) {
100                 if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF,
101                     &iface, sizeof(iface)) < 0) {
102                         fprintf(stderr, "setsockopt IP IF failed: %d\n", errno);
103                         exit(1);
104                 }
105         }
106
107         if (delay > 0)
108                 sleep(delay);
109
110         n = sendto(s, &mreq, sizeof(mreq), 0,
111             (const struct sockaddr *)&dst, sizeof(dst));
112         if (n < 0) {
113                 fprintf(stderr, "sendto failed: %d\n", errno);
114                 exit(1);
115         }
116
117         exit(0);
118 }