Merge branch 'vendor/DHCPCD'
[dragonfly.git] / test / udp / sendwildcard / udp_sendwildcard.c
CommitLineData
f503b4c4
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 <err.h>
8#include <stdio.h>
9#include <stdint.h>
10#include <stdlib.h>
11#include <string.h>
12#include <unistd.h>
13
14static void
15usage(const char *cmd)
16{
c4122907 17 fprintf(stderr, "%s -4 ip4 -p port [-r]\n", cmd);
f503b4c4
SZ
18 exit(1);
19}
20
21int
22main(int argc, char *argv[])
23{
24 struct sockaddr_in in;
c4122907 25 int s, opt, n, reuseport;
f503b4c4
SZ
26 uint8_t buf[18];
27
28 memset(&in, 0, sizeof(in));
29 in.sin_family = AF_INET;
30
c4122907
SZ
31 reuseport = 0;
32
33 while ((opt = getopt(argc, argv, "4:p:r")) != -1) {
f503b4c4
SZ
34 switch (opt) {
35 case '4':
36 if (inet_pton(AF_INET, optarg, &in.sin_addr) <= 0)
37 usage(argv[0]);
38 break;
39
40 case 'p':
41 in.sin_port = strtol(optarg, NULL, 10);
42 in.sin_port = htons(in.sin_port);
43 break;
44
c4122907
SZ
45 case 'r':
46 reuseport = 1;
47 break;
48
f503b4c4
SZ
49 default:
50 usage(argv[0]);
51 }
52 }
53
54 if (in.sin_addr.s_addr == INADDR_ANY || in.sin_port == 0)
55 usage(argv[0]);
56
57 s = socket(AF_INET, SOCK_DGRAM, 0);
58 if (s < 0)
59 err(2, "socket failed");
60
c4122907
SZ
61 if (reuseport) {
62 if (setsockopt(s, SOL_SOCKET, SO_REUSEPORT,
63 &reuseport, sizeof(reuseport)) < 0)
64 err(2, "setsockopt SO_REUSEPORT failed");
65 }
66
f503b4c4
SZ
67 n = sendto(s, buf, sizeof(buf), 0,
68 (const struct sockaddr *)&in, sizeof(in));
69 if (n < 0)
70 err(2, "sendto failed");
71 else if (n != (int)sizeof(buf))
72 errx(2, "sent truncated data %d", n);
73
74 n = read(s, buf, sizeof(buf));
75 if (n < 0)
76 err(2, "read failed");
77 printf("read %d, done\n", n);
78
79 exit(0);
80}