Merge branch 'vendor/OPENRESOLV' with the following changes:
[dragonfly.git] / test / mcast / mcast.c
CommitLineData
cecb9aae
SZ
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
13static void
14usage(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
21int
22main(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;
cab8bf9b 93 mreq.imr_interface = iface;
cecb9aae
SZ
94 if (setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP,
95 &mreq, sizeof(mreq)) < 0) {
96 fprintf(stderr, "setsockopt IP MEMSHIP failed: %d\n", errno);
97 exit(1);
98 }
99
100 if (iface.s_addr != INADDR_ANY) {
101 if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF,
102 &iface, sizeof(iface)) < 0) {
103 fprintf(stderr, "setsockopt IP IF failed: %d\n", errno);
104 exit(1);
105 }
106 }
107
108 if (delay > 0)
109 sleep(delay);
110
111 n = sendto(s, &mreq, sizeof(mreq), 0,
112 (const struct sockaddr *)&dst, sizeof(dst));
113 if (n < 0) {
114 fprintf(stderr, "sendto failed: %d\n", errno);
115 exit(1);
116 }
117
118 exit(0);
119}