test - Fix fsx build
[dragonfly.git] / test / udp / mcastsend / mcastsend.c
blobb15d08c9941dacd733d84b1e669a853cedbdf901
1 #include <sys/types.h>
2 #include <sys/socket.h>
4 #include <arpa/inet.h>
5 #include <netinet/in.h>
7 #include <err.h>
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
14 static void
15 usage(const char *cmd)
17 fprintf(stderr, "%s -p port -d dst -i addr [-P bind_port]\n", cmd);
18 exit(1);
21 int
22 main(int argc, char *argv[])
24 struct sockaddr_in dst, local_in;
25 struct in_addr iface;
26 int s, opt, n, loop = 0;
27 uint8_t buf[18];
29 memset(&dst, 0, sizeof(dst));
30 dst.sin_family = AF_INET;
32 memset(&local_in, 0, sizeof(local_in));
33 local_in.sin_family = AF_INET;
35 memset(&iface, 0, sizeof(iface));
37 while ((opt = getopt(argc, argv, "P:d:i:p:")) != -1) {
38 switch (opt) {
39 case 'P':
40 local_in.sin_port = strtol(optarg, NULL, 10);
41 local_in.sin_port = htons(local_in.sin_port);
42 break;
44 case 'd':
45 if (inet_pton(AF_INET, optarg, &dst.sin_addr) <= 0)
46 usage(argv[0]);
47 break;
49 case 'i':
50 if (inet_pton(AF_INET, optarg, &iface) <= 0)
51 usage(argv[0]);
52 break;
54 case 'p':
55 dst.sin_port = strtol(optarg, NULL, 10);
56 dst.sin_port = htons(dst.sin_port);
57 break;
59 default:
60 usage(argv[0]);
64 if (iface.s_addr == INADDR_ANY ||
65 dst.sin_addr.s_addr == INADDR_ANY || dst.sin_port == 0)
66 usage(argv[0]);
68 s = socket(AF_INET, SOCK_DGRAM, 0);
69 if (s < 0)
70 err(2, "socket failed");
72 if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF,
73 &iface, sizeof(iface)) < 0)
74 err(2, "setsockopt IP_MULTICAST_IF failed");
76 if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP,
77 &loop, sizeof(loop)) < 0)
78 err(2, "setsockopt IP_MULTICAST_LOOP failed");
80 if (local_in.sin_port != 0) {
81 local_in.sin_addr = iface;
82 if (bind(s, (const struct sockaddr *)&local_in,
83 sizeof(local_in)) < 0)
84 err(2, "bind failed");
87 n = sendto(s, buf, sizeof(buf), 0,
88 (const struct sockaddr *)&dst, sizeof(dst));
89 if (n < 0)
90 err(2, "sendto failed");
91 else if (n < (int)sizeof(buf))
92 errx(2, "sent truncated data %d", n);
94 exit(0);