X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fmdnsd.git;a=blobdiff_plain;f=util.c;h=63f1612fdb2c935887de2d0bdd2953ae5d630a40;hp=1708a404ca8ce20fa526409ba4f4a8544e24fef1;hb=c0c78c92d57c53276bb7f08468bcc716ba272497;hpb=8381f595a6066f893cc24e8a161e27a10a140cf0 diff --git a/util.c b/util.c index 1708a40..63f1612 100644 --- a/util.c +++ b/util.c @@ -16,8 +16,6 @@ #include #include #include -#include -#include #include #include @@ -29,26 +27,16 @@ #include #include +#include #include "dns.h" #include "util.h" +uint8_t mdns_buf[MDNS_BUF_LEN]; int debug = 0; -struct uloop_fd listener; -static void -signal_shutdown(int signal) -{ - uloop_end(); -} - -void -signal_setup(void) -{ - signal(SIGPIPE, SIG_IGN); - signal(SIGTERM, signal_shutdown); - signal(SIGKILL, signal_shutdown); -} +char mdns_hostname[HOSTNAME_LEN]; +char mdns_hostname_local[HOSTNAME_LEN + 6]; uint32_t rand_time_delta(uint32_t t) @@ -73,124 +61,23 @@ rand_time_delta(uint32_t t) return val; } -const char* -get_iface_ipv4(const char *ifname) -{ - static char buffer[INET_ADDRSTRLEN]; - struct ifreq ir; - const char *ret; - int sock; - - sock = socket(AF_INET, SOCK_DGRAM, 0); - if (sock < 0) - return NULL; - - memset(&ir, 0, sizeof(struct ifreq)); - - strncpy(ir.ifr_name, ifname, sizeof(ir.ifr_name)); - - if (ioctl(sock, SIOCGIFADDR, &ir) < 0) - return NULL; - - ret = inet_ntop(AF_INET, &((struct sockaddr_in *) &ir.ifr_addr)->sin_addr, buffer, sizeof(buffer)); - close(sock); - - return ret; -} - -int -get_iface_index(const char *ifname) +void get_hostname(void) { - struct ifreq ir; - int sock; - - sock = socket(AF_INET, SOCK_DGRAM, 0); - if (sock < 0) - return 0; - - memset(&ir, 0, sizeof(struct ifreq)); + struct utsname utsname; - strncpy(ir.ifr_name, ifname, sizeof(ir.ifr_name)); - - if (ioctl(sock, SIOCGIFINDEX, &ir) < 0) - return 0; - - close(sock); - - return ir.ifr_ifindex; -} - -char* -get_hostname(void) -{ - static struct utsname utsname; + mdns_hostname[0] = 0; + mdns_hostname_local[0] = 0; if (uname(&utsname) < 0) - return NULL; + return; - return utsname.nodename; + snprintf(mdns_hostname, sizeof(mdns_hostname), "%s", utsname.nodename); + snprintf(mdns_hostname_local, sizeof(mdns_hostname_local), "%s.local", utsname.nodename); } -int -socket_setup(int fd, const char *ip) +time_t monotonic_time(void) { - struct ip_mreqn mreq; - uint8_t ttl = 255; - int yes = 1; - int no = 0; - struct sockaddr_in sa = { 0 }; - struct in_addr in; - - inet_aton(iface_ip, &in); - - sa.sin_family = AF_INET; - sa.sin_port = htons(MCAST_PORT); - inet_pton(AF_INET, MCAST_ADDR, &sa.sin_addr); - - memset(&mreq, 0, sizeof(mreq)); - mreq.imr_address.s_addr = in.s_addr; - mreq.imr_multiaddr = sa.sin_addr; - mreq.imr_ifindex = iface_index; - - if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0) - fprintf(stderr, "ioctl failed: IP_MULTICAST_TTL\n"); - - if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) - fprintf(stderr, "ioctl failed: SO_REUSEADDR\n"); - - /* Some network drivers have issues with dropping membership of - * mcast groups when the iface is down, but don't allow rejoining - * when it comes back up. This is an ugly workaround - * -- this was copied from avahi -- - */ - setsockopt(fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)); - - if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) { - fprintf(stderr, "failed to join multicast group: %s\n", strerror(errno)); - close(fd); - fd = -1; - return -1; - } - - if (setsockopt(fd, IPPROTO_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0) - fprintf(stderr, "ioctl failed: IP_RECVTTL\n"); - - if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0) - fprintf(stderr, "ioctl failed: IP_PKTINFO\n"); - - if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &no, sizeof(no)) < 0) - fprintf(stderr, "ioctl failed: IP_MULTICAST_LOOP\n"); - - return 0; + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return ts.tv_sec; } - -void* -memdup(void *d, int l) -{ - void *r = malloc(l); - if (!r) - return NULL; - memcpy(r, d, l); - return r; -} -