mdns: remove dependencies on asm/byteorder.h, fix constant endian swap initialization
[project/mdnsd.git] / util.c
diff --git a/util.c b/util.c
index a4a117b..eeba305 100644 (file)
--- a/util.c
+++ b/util.c
 #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,91 +62,16 @@ rand_time_delta(uint32_t t)
        return val;
 }
 
-const char*
-get_iface_ipv4(const char *ifname)
+void get_hostname(void)
 {
-       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));
+       struct utsname utsname;
 
-       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;
-}
-
-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)
-{
-       struct ip_mreqn mreq;
-       uint8_t ttl = 255;
-       int yes = 1;
-       int no = 0;
-       struct sockaddr_in sa;
-
-       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 = htonl(INADDR_ANY);
-       mreq.imr_multiaddr = sa.sin_addr;
-
-       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");
-
-       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;
-}
-
-void*
-memdup(void *d, int l)
-{
-       void *r = malloc(l);
-       if (!r)
-               return NULL;
-       memcpy(r, d, l);
-       return r;
-}
-