2 * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2.1
6 * as published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <sys/socket.h>
15 #include <sys/ioctl.h>
16 #include <sys/types.h>
18 #include <sys/utsname.h>
20 #include <linux/sockios.h>
21 #include <arpa/inet.h>
31 #include <libubox/uloop.h>
37 struct uloop_fd listener;
40 signal_shutdown(int signal)
48 signal(SIGPIPE, SIG_IGN);
49 signal(SIGTERM, signal_shutdown);
50 signal(SIGKILL, signal_shutdown);
54 rand_time_delta(uint32_t t)
57 int fd = open("/dev/urandom", O_RDONLY);
62 if (read(fd, &val, sizeof(val)) == sizeof(val)) {
66 val = t + (rand() % range) - (range / 2);
77 get_iface_ipv4(const char *ifname)
79 static char buffer[INET_ADDRSTRLEN];
84 sock = socket(AF_INET, SOCK_DGRAM, 0);
88 memset(&ir, 0, sizeof(struct ifreq));
90 strncpy(ir.ifr_name, ifname, sizeof(ir.ifr_name));
92 if (ioctl(sock, SIOCGIFADDR, &ir) < 0)
95 ret = inet_ntop(AF_INET, &((struct sockaddr_in *) &ir.ifr_addr)->sin_addr, buffer, sizeof(buffer));
102 get_iface_index(const char *ifname)
107 sock = socket(AF_INET, SOCK_DGRAM, 0);
111 memset(&ir, 0, sizeof(struct ifreq));
113 strncpy(ir.ifr_name, ifname, sizeof(ir.ifr_name));
115 if (ioctl(sock, SIOCGIFINDEX, &ir) < 0)
120 return ir.ifr_ifindex;
126 static struct utsname utsname;
128 if (uname(&utsname) < 0)
131 return utsname.nodename;
135 socket_setup(int fd, const char *ip)
137 struct ip_mreqn mreq;
141 struct sockaddr_in sa = { 0 };
144 inet_aton(iface_ip, &in);
146 sa.sin_family = AF_INET;
147 sa.sin_port = htons(MCAST_PORT);
148 inet_pton(AF_INET, MCAST_ADDR, &sa.sin_addr);
150 memset(&mreq, 0, sizeof(mreq));
151 mreq.imr_address.s_addr = in.s_addr;
152 mreq.imr_multiaddr = sa.sin_addr;
153 mreq.imr_ifindex = iface_index;
155 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0)
156 fprintf(stderr, "ioctl failed: IP_MULTICAST_TTL\n");
158 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0)
159 fprintf(stderr, "ioctl failed: SO_REUSEADDR\n");
161 /* Some network drivers have issues with dropping membership of
162 * mcast groups when the iface is down, but don't allow rejoining
163 * when it comes back up. This is an ugly workaround
164 * -- this was copied from avahi --
166 setsockopt(fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq));
168 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
169 fprintf(stderr, "failed to join multicast group: %s\n", strerror(errno));
175 if (setsockopt(fd, IPPROTO_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0)
176 fprintf(stderr, "ioctl failed: IP_RECVTTL\n");
178 if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0)
179 fprintf(stderr, "ioctl failed: IP_PKTINFO\n");
181 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &no, sizeof(no)) < 0)
182 fprintf(stderr, "ioctl failed: IP_MULTICAST_LOOP\n");
188 memdup(const void *d, int l)