chiark / gitweb /
log: more general error message formatting
[disorder] / lib / addr.c
index ecbab22e7c48ea9d4f66e8658c334d1aef3d8c97..d1c9d011db7382befb4dbac82e32d4ba5adbabbb 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * This file is part of DisOrder.
- * Copyright (C) 2004, 2007, 2008 Richard Kettlewell
+ * Copyright (C) 2004, 2007, 2008, 2013 Richard Kettlewell
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 #include "common.h"
 
 #include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <sys/un.h>
+#if HAVE_SYS_SOCKET_H
+# include <sys/socket.h>
+#endif
+#if HAVE_NETINET_IN_H
+# include <netinet/in.h>
+#endif
+#if HAVE_ARPA_INET_H
+# include <arpa/inet.h>
+#endif
+#if HAVE_SYS_UN_H
+# include <sys/un.h>
+#endif
 
 #include "log.h"
 #include "printf.h"
@@ -58,28 +66,32 @@ struct addrinfo *get_address(const struct stringlist *a,
   struct addrinfo *res;
   char *name;
   int rc;
+  char errbuf[1024];
 
   switch(a->n) {  
   case 1:
     byte_xasprintf(&name, "host * service %s", a->s[0]);
     if((rc = getaddrinfo(0, a->s[0], pref, &res))) {
-      error(0, "getaddrinfo %s: %s", a->s[0], gai_strerror(rc));
+      disorder_error(0, "getaddrinfo %s: %s", a->s[0],
+                     format_error(ec_getaddrinfo, rc, errbuf, sizeof errbuf));
       return 0;
     }
     break;
   case 2:
     byte_xasprintf(&name, "host %s service %s", a->s[0], a->s[1]);
     if((rc = getaddrinfo(a->s[0], a->s[1], pref, &res))) {
-      error(0, "getaddrinfo %s %s: %s", a->s[0], a->s[1], gai_strerror(rc));
+      disorder_error(0, "getaddrinfo %s %s: %s",
+                     a->s[0], a->s[1],
+                     format_error(ec_getaddrinfo, rc, errbuf, sizeof errbuf));
       return 0;
     }
     break;
   default:
-    error(0, "invalid network address specification (n=%d)", a->n);
+    disorder_error(0, "invalid network address specification (n=%d)", a->n);
     return 0;
   }
   if(!res || (pref && res->ai_socktype != pref->ai_socktype)) {
-    error(0, "getaddrinfo didn't give us a suitable socket address");
+    disorder_error(0, "getaddrinfo didn't give us a suitable socket address");
     if(res)
       freeaddrinfo(res);
     return 0;
@@ -126,7 +138,7 @@ int sockaddrcmp(const struct sockaddr *a,
     return memcmp(&in6a->sin6_addr, &in6b->sin6_addr,
                  sizeof (struct in6_addr));
   default:
-    fatal(0, "unsupported protocol family %d", a->sa_family);
+    disorder_fatal(0, "unsupported protocol family %d", a->sa_family);
   }
 }
 
@@ -184,10 +196,12 @@ static inline char *format_sockaddr6(const struct sockaddr_in6 *sin6) {
   return r;
 }
 
+#if HAVE_SYS_UN_H
 /** @brief Format a UNIX socket address */
 static inline char *format_sockaddrun(const struct sockaddr_un *sun) {
   return xstrdup(sun->sun_path);
 }
+#endif
     
 /** @brief Construct a text description a sockaddr
  * @param sa Socket address
@@ -199,8 +213,10 @@ char *format_sockaddr(const struct sockaddr *sa) {
     return format_sockaddr4((const struct sockaddr_in *)sa);
   case AF_INET6:
     return format_sockaddr6((const struct sockaddr_in6 *)sa);
+#if HAVE_SYS_UN_H
   case AF_UNIX:
     return format_sockaddrun((const struct sockaddr_un *)sa);
+#endif
   default:
     return 0;
   }
@@ -326,6 +342,7 @@ struct addrinfo *netaddress_resolve(const struct netaddress *na,
   struct addrinfo *res, hints[1];
   char service[64];
   int rc;
+  char errbuf[1024];
 
   memset(hints, 0, sizeof hints);
   hints->ai_family = na->af;
@@ -334,9 +351,10 @@ struct addrinfo *netaddress_resolve(const struct netaddress *na,
   snprintf(service, sizeof service, "%d", na->port);
   rc = getaddrinfo(na->address, service, hints, &res);
   if(rc) {
-    error(0, "getaddrinfo %s %d: %s",
-         na->address ? na->address : "*",
-         na->port, gai_strerror(rc));
+    disorder_error(0, "getaddrinfo %s %d: %s",
+                  na->address ? na->address : "*",
+                   na->port,
+                   format_error(ec_getaddrinfo, rc, errbuf, sizeof errbuf));
     return NULL;
   }
   return res;