2 #include <sys/socket.h>
5 #include <netinet/in.h>
15 usage(const char *cmd
)
17 fprintf(stderr
, "%s -4 ip4 -p port [-b ip4] -P bind_port [-r]\n", cmd
);
22 main(int argc
, char *argv
[])
24 struct sockaddr_in in
, local_in
;
25 int s
, opt
, n
, reuseport
;
28 memset(&in
, 0, sizeof(in
));
29 in
.sin_family
= AF_INET
;
31 memset(&local_in
, 0, sizeof(local_in
));
32 local_in
.sin_family
= AF_INET
;
36 while ((opt
= getopt(argc
, argv
, "4:P:b:p:r")) != -1) {
39 if (inet_pton(AF_INET
, optarg
, &in
.sin_addr
) <= 0)
44 local_in
.sin_port
= strtol(optarg
, NULL
, 10);
45 local_in
.sin_port
= htons(local_in
.sin_port
);
49 if (inet_pton(AF_INET
, optarg
, &local_in
.sin_addr
) <= 0)
54 in
.sin_port
= strtol(optarg
, NULL
, 10);
55 in
.sin_port
= htons(in
.sin_port
);
67 if (in
.sin_addr
.s_addr
== INADDR_ANY
|| in
.sin_port
== 0 ||
68 local_in
.sin_port
== 0)
71 s
= socket(AF_INET
, SOCK_DGRAM
, 0);
73 err(2, "socket failed");
76 if (setsockopt(s
, SOL_SOCKET
, SO_REUSEPORT
,
77 &reuseport
, sizeof(reuseport
)) < 0)
78 err(2, "setsockopt SO_REUSEPORT failed");
81 if (bind(s
, (const struct sockaddr
*)&local_in
, sizeof(local_in
)) < 0)
82 err(2, "bind failed");
84 if (connect(s
, (const struct sockaddr
*)&in
, sizeof(in
)) < 0)
85 err(2, "connect failed");
87 n
= write(s
, buf
, sizeof(buf
));
89 err(2, "write failed");
90 else if (n
!= (int)sizeof(buf
))
91 errx(2, "written truncated data %d", n
);
93 n
= read(s
, buf
, sizeof(buf
));
95 err(2, "read failed");
96 printf("read %d, done\n", n
);