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\n", cmd
);
22 main(int argc
, char *argv
[])
24 struct sockaddr_in in
, local_in
;
25 int s1
, s2
, opt
, n
, on
= 1;
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
;
34 while ((opt
= getopt(argc
, argv
, "4:P:b:p:")) != -1) {
37 if (inet_pton(AF_INET
, optarg
, &in
.sin_addr
) <= 0)
42 local_in
.sin_port
= strtol(optarg
, NULL
, 10);
43 local_in
.sin_port
= htons(local_in
.sin_port
);
47 if (inet_pton(AF_INET
, optarg
, &local_in
.sin_addr
) <= 0)
52 in
.sin_port
= strtol(optarg
, NULL
, 10);
53 in
.sin_port
= htons(in
.sin_port
);
61 if (in
.sin_addr
.s_addr
== INADDR_ANY
|| in
.sin_port
== 0 ||
62 local_in
.sin_port
== 0)
65 s1
= socket(AF_INET
, SOCK_DGRAM
, 0);
67 err(2, "socket 1 failed");
69 if (setsockopt(s1
, SOL_SOCKET
, SO_REUSEPORT
, &on
, sizeof(on
)) < 0)
70 err(2, "setsockopt 1 SO_REUSEPORT failed");
72 if (bind(s1
, (const struct sockaddr
*)&local_in
, sizeof(local_in
)) < 0)
73 err(2, "bind 1 failed");
75 if (connect(s1
, (const struct sockaddr
*)&in
, sizeof(in
)) < 0)
76 err(2, "connect 1 failed");
78 s2
= socket(AF_INET
, SOCK_DGRAM
, 0);
80 err(2, "socket 2 failed");
82 if (setsockopt(s2
, SOL_SOCKET
, SO_REUSEPORT
, &on
, sizeof(on
)) < 0)
83 err(2, "setsockopt 2 SO_REUSEPORT failed");
85 if (bind(s2
, (const struct sockaddr
*)&local_in
, sizeof(local_in
)) < 0)
86 err(2, "bind 2 failed");
88 if (connect(s2
, (const struct sockaddr
*)&in
, sizeof(in
)) == 0)
89 errx(2, "connect 2 succeeded");
93 n
= sendto(s2
, buf
, sizeof(buf
), 0,
94 (const struct sockaddr
*)&in
, sizeof(in
));
96 err(2, "sendto failed");
97 else if (n
!= (int)sizeof(buf
))
98 errx(2, "sent truncated data %d", n
);
100 n
= read(s2
, buf
, sizeof(buf
));
102 err(2, "read failed");
103 printf("read %d, done\n", n
);