2 #include <sys/socket.h>
5 #include <netinet/in.h>
15 usage(const char *cmd
)
17 fprintf(stderr
, "%s -p port -d dst -i addr [-P bind_port]\n", cmd
);
22 main(int argc
, char *argv
[])
24 struct sockaddr_in dst
, local_in
;
26 int s
, opt
, n
, loop
= 0;
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) {
40 local_in
.sin_port
= strtol(optarg
, NULL
, 10);
41 local_in
.sin_port
= htons(local_in
.sin_port
);
45 if (inet_pton(AF_INET
, optarg
, &dst
.sin_addr
) <= 0)
50 if (inet_pton(AF_INET
, optarg
, &iface
) <= 0)
55 dst
.sin_port
= strtol(optarg
, NULL
, 10);
56 dst
.sin_port
= htons(dst
.sin_port
);
64 if (iface
.s_addr
== INADDR_ANY
||
65 dst
.sin_addr
.s_addr
== INADDR_ANY
|| dst
.sin_port
== 0)
68 s
= socket(AF_INET
, SOCK_DGRAM
, 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
));
90 err(2, "sendto failed");
91 else if (n
< (int)sizeof(buf
))
92 errx(2, "sent truncated data %d", n
);