chiark / gitweb /
lib/addr.c: Handle `AF_UNIX' sockets in `netaddress_resolve'.
[disorder] / lib / addr.c
index d1c9d011db7382befb4dbac82e32d4ba5adbabbb..802204a83372d2e69eef831603867ee180cf0642 100644 (file)
@@ -20,6 +20,8 @@
 
 #include "common.h"
 
+#include <stddef.h>
+
 #include <sys/types.h>
 #if HAVE_SYS_SOCKET_H
 # include <sys/socket.h>
@@ -33,6 +35,9 @@
 #if HAVE_SYS_UN_H
 # include <sys/un.h>
 #endif
+#if HAVE_WS2TCPIP_H
+# include <Ws2tcpip.h>
+#endif
 
 #include "log.h"
 #include "printf.h"
@@ -170,12 +175,12 @@ static inline char *format_sockaddr4(const struct sockaddr_in *sin4) {
 
   if(sin4->sin_port)
     byte_xasprintf(&r, "%s port %u",
-                  inet_ntop(sin4->sin_family, &sin4->sin_addr,
+                  inet_ntop(sin4->sin_family, (void *)&sin4->sin_addr,
                             buffer, sizeof buffer),
                   ntohs(sin4->sin_port));
   else
     byte_xasprintf(&r, "%s",
-                  inet_ntop(sin4->sin_family, &sin4->sin_addr,
+                  inet_ntop(sin4->sin_family, (void *)&sin4->sin_addr,
                             buffer, sizeof buffer));
   return r;
 }
@@ -186,12 +191,12 @@ static inline char *format_sockaddr6(const struct sockaddr_in6 *sin6) {
 
   if(sin6->sin6_port)
     byte_xasprintf(&r, "%s port %u",
-                  inet_ntop(sin6->sin6_family, &sin6->sin6_addr,
+                  inet_ntop(sin6->sin6_family, (void *)&sin6->sin6_addr,
                             buffer, sizeof buffer),
                   ntohs(sin6->sin6_port));
   else
     byte_xasprintf(&r, "%s",
-                  inet_ntop(sin6->sin6_family, &sin6->sin6_addr,
+                  inet_ntop(sin6->sin6_family, (void *)&sin6->sin6_addr,
                             buffer, sizeof buffer));
   return r;
 }
@@ -232,6 +237,8 @@ int netaddress_parse(struct netaddress *na,
                     int nvec,
                     char **vec) {
   const char *port;
+  long p;
+  int e;
 
   na->af = AF_UNSPEC;
   if(nvec > 0 && vec[0][0] == '-') {
@@ -282,8 +289,7 @@ int netaddress_parse(struct netaddress *na,
     }
     if(port[strspn(port, "0123456789")])
       return -1;
-    long p;
-    int e = xstrtol(&p, port, NULL, 10);
+    e = xstrtol(&p, port, NULL, 10);
 
     if(e)
       return -1;
@@ -320,7 +326,7 @@ void netaddress_format(const struct netaddress *na,
   if(na->port != -1) {
     char buffer[64];
 
-    snprintf(buffer, sizeof buffer, "%d", na->port);
+    byte_snprintf(buffer, sizeof buffer, "%d", na->port);
     vector_append(v, xstrdup(buffer));
   }
   vector_terminate(v);
@@ -335,31 +341,70 @@ void netaddress_format(const struct netaddress *na,
  * @param passive True if passive (bindable) address is desired
  * @param protocol Protocol number desired (e.g. @c IPPROTO_TCP)
  * @return List of suitable addresses or NULL
+ *
+ * Free the address using netaddress_freeaddrinfo() because it might not
+ * have come from getaddrinfo() directly.
  */
 struct addrinfo *netaddress_resolve(const struct netaddress *na,
                                    int passive,
                                    int protocol) {
   struct addrinfo *res, hints[1];
+  struct sockaddr_un *sun;
   char service[64];
   int rc;
   char errbuf[1024];
 
-  memset(hints, 0, sizeof hints);
-  hints->ai_family = na->af;
-  hints->ai_protocol = protocol;
-  hints->ai_flags = passive ? AI_PASSIVE : 0;
-  snprintf(service, sizeof service, "%d", na->port);
-  rc = getaddrinfo(na->address, service, hints, &res);
-  if(rc) {
-    disorder_error(0, "getaddrinfo %s %d: %s",
-                  na->address ? na->address : "*",
-                   na->port,
-                   format_error(ec_getaddrinfo, rc, errbuf, sizeof errbuf));
-    return NULL;
+#if HAVE_SYS_UN_H
+  if (na->af == AF_UNIX) {
+    /* `getaddrinfo' won't work, so we make our own one */
+    res = xmalloc(sizeof(*res));
+    res->ai_flags = 0;
+    res->ai_family = AF_UNIX;
+    res->ai_socktype = (protocol == IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM);
+    res->ai_protocol = 0;
+    res->ai_addrlen = offsetof(struct sockaddr_un, sun_path) +
+      strlen(na->address) + 1;
+    sun = xmalloc(res->ai_addrlen);
+    sun->sun_family = AF_UNIX;
+    strcpy(sun->sun_path, na->address);
+    res->ai_addr = (struct sockaddr *)sun;
+    res->ai_canonname = sun->sun_path;
+    res->ai_next = 0;
+  } else
+#endif
+  {
+    /* get the system to do the heavy lifting */
+    memset(hints, 0, sizeof hints);
+    hints->ai_family = na->af;
+    hints->ai_protocol = protocol;
+    hints->ai_flags = passive ? AI_PASSIVE : 0;
+    byte_snprintf(service, sizeof service, "%d", na->port);
+    rc = getaddrinfo(na->address, service, hints, &res);
+    if(rc) {
+      disorder_error(0, "getaddrinfo %s %d: %s",
+                    na->address ? na->address : "*",
+                    na->port,
+                    format_error(ec_getaddrinfo, rc, errbuf, sizeof errbuf));
+      return NULL;
+    }
+    assert(res->ai_family != AF_UNIX);
   }
   return res;
 }
 
+/** @brief Free an address-info list from netaddress_resovle()
+ * @param res Address-info list
+ */
+void netaddress_freeaddrinfo(struct addrinfo *res) {
+#if HAVE_SYS_UN_H
+  if(res->ai_family == AF_UNIX) {
+    xfree(res->ai_addr);
+    xfree(res);
+  } else
+#endif
+    freeaddrinfo(res);
+}
+
 /*
 Local Variables:
 c-basic-offset:2