colldef/mklocale: Remove some workarounds from "locale megapatch" times.
[dragonfly.git] / test / mcast / mcast.c
blob59b631cb50e065c272eb916f7f8af15cb584037e
1 #include <sys/types.h>
2 #include <sys/socket.h>
4 #include <arpa/inet.h>
5 #include <netinet/in.h>
7 #include <errno.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
13 static void
14 usage(const char *cmd)
16 fprintf(stderr,
17 "%s -m addr -p port [-d dst] [-i addr] [-D delay]\n", cmd);
18 exit(1);
21 int
22 main(int argc, char *argv[])
24 struct ip_mreq mreq;
25 struct sockaddr_in in, dst;
26 struct in_addr iface;
27 int s, opt, n, delay;
29 memset(&in, 0, sizeof(in));
30 in.sin_family = AF_INET;
32 memset(&dst, 0, sizeof(dst));
33 dst.sin_family = AF_INET;
35 memset(&iface, 0, sizeof(iface));
36 delay = 0;
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);
45 break;
47 case 'p':
48 in.sin_port = htons(atoi(optarg));
49 dst.sin_port = in.sin_port;
50 break;
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);
57 break;
59 case 'i':
60 if (inet_pton(AF_INET, optarg, &iface) <= 0) {
61 fprintf(stderr, "invalid -i addr %s\n", optarg);
62 exit(1);
64 break;
66 case 'D':
67 delay = atoi(optarg);
68 break;
70 default:
71 usage(argv[0]);
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;
80 s = socket(AF_INET, SOCK_DGRAM, 0);
81 if (s < 0) {
82 fprintf(stderr, "socket failed: %d\n", errno);
83 exit(1);
86 if (bind(s, (const struct sockaddr *)&in, sizeof(in)) < 0) {
87 fprintf(stderr, "bind failed: %d\n", errno);
88 exit(1);
91 memset(&mreq, 0, sizeof(mreq));
92 mreq.imr_multiaddr = in.sin_addr;
93 mreq.imr_interface = iface;
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);
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);
108 if (delay > 0)
109 sleep(delay);
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);
118 exit(0);