2 #include <sys/socket.h>
5 #include <netinet/in.h>
14 usage(const char *cmd
)
17 "%s -m addr -p port [-d dst] [-i addr] [-D delay]\n", cmd
);
22 main(int argc
, char *argv
[])
25 struct sockaddr_in in
, dst
;
29 memset(&in
, 0, sizeof(in
));
30 in
.sin_family
= AF_INET
;
32 memset(&dst
, 0, sizeof(dst
));
33 dst
.sin_family
= AF_INET
;
35 memset(&iface
, 0, sizeof(iface
));
38 while ((opt
= getopt(argc
, argv
, "m:p:d:i:D:")) != -1) {
41 if (inet_pton(AF_INET
, optarg
, &in
.sin_addr
) <= 0) {
42 fprintf(stderr
, "invalid -m addr %s\n", optarg
);
48 in
.sin_port
= htons(atoi(optarg
));
49 dst
.sin_port
= in
.sin_port
;
53 if (inet_pton(AF_INET
, optarg
, &dst
.sin_addr
) <= 0) {
54 fprintf(stderr
, "invalid -d addr %s\n", optarg
);
60 if (inet_pton(AF_INET
, optarg
, &iface
) <= 0) {
61 fprintf(stderr
, "invalid -i addr %s\n", optarg
);
75 if (in
.sin_addr
.s_addr
== INADDR_ANY
|| in
.sin_port
== 0)
77 if (dst
.sin_addr
.s_addr
== 0)
78 dst
.sin_addr
= in
.sin_addr
;
80 s
= socket(AF_INET
, SOCK_DGRAM
, 0);
82 fprintf(stderr
, "socket failed: %d\n", errno
);
86 if (bind(s
, (const struct sockaddr
*)&in
, sizeof(in
)) < 0) {
87 fprintf(stderr
, "bind failed: %d\n", errno
);
91 memset(&mreq
, 0, sizeof(mreq
));
92 mreq
.imr_multiaddr
= in
.sin_addr
;
93 mreq
.imr_interface
= iface
;
94 if (setsockopt(s
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
95 &mreq
, sizeof(mreq
)) < 0) {
96 fprintf(stderr
, "setsockopt IP MEMSHIP failed: %d\n", errno
);
100 if (iface
.s_addr
!= INADDR_ANY
) {
101 if (setsockopt(s
, IPPROTO_IP
, IP_MULTICAST_IF
,
102 &iface
, sizeof(iface
)) < 0) {
103 fprintf(stderr
, "setsockopt IP IF failed: %d\n", errno
);
111 n
= sendto(s
, &mreq
, sizeof(mreq
), 0,
112 (const struct sockaddr
*)&dst
, sizeof(dst
));
114 fprintf(stderr
, "sendto failed: %d\n", errno
);