Improve error message when binding http port.
[darkstat] / dns.c
diff --git a/dns.c b/dns.c
index e1960d8..e8bdbb4 100644 (file)
--- a/dns.c
+++ b/dns.c
@@ -9,7 +9,6 @@
 
 #include "cdefs.h"
 #include "conv.h"
-#include "config.h"
 #include "decode.h"
 #include "dns.h"
 #include "err.h"
@@ -17,6 +16,7 @@
 #include "queue.h"
 #include "str.h"
 #include "tree.h"
+#include "bsd.h" /* for setproctitle, strlcpy */
 
 #include <sys/param.h>
 #include <sys/socket.h>
@@ -198,8 +198,9 @@ dns_get_result(struct addr *ipaddr, char **name)
             type = "site-local";
          else if (IN6_IS_ADDR_MULTICAST(&reply.addr.ip.v6))
             type = "multicast";
-      } else { /* AF_INET */
-         if (IN_MULTICAST(reply.addr.ip.v4))
+      } else {
+         assert(reply.addr.family == IPv4);
+         if (IN_MULTICAST(htonl(reply.addr.ip.v4)))
             type = "multicast";
       }
       xasprintf(name, "(%s)", type);
@@ -295,9 +296,7 @@ dns_main(void)
 {
    struct addr ip;
 
-#ifdef HAVE_SETPROCTITLE
    setproctitle("DNS child");
-#endif
    fd_set_nonblock(sock[CHILD]);
    verbosef("DNS child entering main DNS loop");
    for (;;) {
@@ -341,6 +340,7 @@ dns_main(void)
          struct dns_reply reply;
          struct sockaddr_in sin;
          struct sockaddr_in6 sin6;
+         struct hostent *he;
          char host[NI_MAXHOST];
          int ret, flags;
 
@@ -355,6 +355,19 @@ dns_main(void)
                sin.sin_addr.s_addr = ip.ip.v4;
                ret = getnameinfo((struct sockaddr *) &sin, sizeof(sin),
                                  host, sizeof(host), NULL, 0, flags);
+               if (ret == EAI_FAMILY) {
+                  verbosef("getnameinfo error %s, trying gethostbyname",
+                     gai_strerror(ret));
+                  he = gethostbyaddr(&sin.sin_addr.s_addr,
+                     sizeof(sin.sin_addr.s_addr), sin.sin_family);
+                  if (he == NULL) {
+                     ret = EAI_FAIL;
+                     verbosef("gethostbyname error %s", hstrerror(h_errno));
+                  } else {
+                     ret = 0;
+                     strlcpy(host, he->h_name, sizeof(host));
+                  }
+               }
                break;
             case IPv6:
                sin6.sin6_family = AF_INET6;