Przeglądaj źródła

Working strerror for windows socket errors, plus some snide comments.

svn:r1775
Nick Mathewson 20 lat temu
rodzic
commit
af08c4f878

+ 1 - 1
src/common/tortls.c

@@ -121,7 +121,7 @@ tor_tls_get_error(tor_tls *tls, int r, int extra,
       else {
         int e = tor_socket_errno(tls->socket);
         log(severity, "TLS error: <syscall error while %s> (errno=%d: %s)",
-            doing, e, strerror(e));
+            doing, e, tor_socket_strerror(e));
       }
       tls_log_errors(severity, doing);
       return TOR_TLS_ERROR;

+ 74 - 1
src/common/util.c

@@ -1111,6 +1111,79 @@ int tor_socket_errno(int sock)
 }
 #endif
 
+/* There does not seem to be a strerror equivalent for winsock errors.
+ * Naturally, we have to roll our own.
+ */
+#ifdef MS_WINDOWS
+#define E(code, s) { code, (s " [" #code " ]") }
+struct { int code; char *msg; } windows_socket_errors = {
+  E(WSAEINTR, "Interrupted function call"),
+  E(WSAEACCES, "Permission denied"),
+  E(WSAEFAULT, "Bad address"),
+  E(WSAEINVAL, "Invalid argument"),
+  E(WSAEMFILE, "Too many open files"),
+  E(WSAEWOULDBLOCK,  "Resource temporarily unavailable"),
+  E(WSAEINPROGRESS, "Operation now in progress"),
+  E(WSAEALREADY, "Operation already in progress"),
+  E(WSAENOTSOCK, "Socket operation on nonsocket"),
+  E(WSAEDESTADDRREQ, "Destination address required"),
+  E(WSAEMSGSIZE, "Message too long"),
+  E(WASEPROTOTYPE, "Protocol wrong for socket"),
+  E(WSAENOPROTOOPT, "Bad protocol option"),
+  E(WSAEPROTONOSUPPORT, "Protocol not supported"),
+  E(WSAESOCKTNOSUPPORT, "Socket type not supported"),
+  /* What's the difference between NOTSUPP and NOSUPPORT? :) */
+  E(WASEOPNOTSUPP, "Operation not supported"),
+  E(WSAEPFNOSUPPORT,  "Protocol family not supported"),
+  E(WSAEAFNOSUPPORT, "Address family not supported by protocol family"),
+  E(WSAEADDRINUSE, "Address already in use"),
+  E(WSAEADDRNOTAVAIL, "Cannot assign requested address"),
+  E(WSAENETDOWN, "Network is down"),
+  E(WSAENETUNREACH, "Network is unreachable"),
+  E(WSAENETRESET, "Network dropped connection on reset")
+  E(WSAECONNABORTED, "Software caused connection abort"),
+  E(WSAECONNRESET, "Connection reset by peer"),
+  E(WSAENOBUFS, "No buffer space avaialable"),
+  E(WSAEISCONN, "Socket is already connected"),
+  E(WSAENOTCONN, "Socket is not connected"),
+  E(WSAESHUTDOWN, "Cannot send after socket shutdown"),
+  E(WSAETIMEDOUT, "Connection timed out"),
+  E(WSAECONNREFUSED, "Connection refused"),
+  E(WSAEHOSTDOWN, "Host is down"),
+  E(WSAEHOSTUNREACH, "No route to host"),
+  E(WSAEPROCLIM, "Too many processes"),
+  /* Yes, some of these start with WSA, not WSAE. No, I don't know why. */
+  E(WSASYSNOTREADY, "Network subsystem is unavailable"),
+  E(WSAVERNOTSUPPORTED, "Winsock.dll out of range"),
+  E(WSANOTINITIALISED, "Successful WSAStartup not yet performed"),
+  E(WSAEDISCONN, "Graceful shutdown no in progress"),
+  E(WSATYPE_NOT_FOUND, "Class type not found"),
+  E(WSAHOST_NOT_FOUND, "Host not found"),
+  E(WSATRY_AGAIN, "Nonauthoritative host not found"),
+  E(WSANO_RECOVERY, "This is a nonrecoverable error"),
+  E(WSANO_DATA, "Valid name, no data record of requested type)"),
+
+  /* There are some more error codes whose numeric values are marked
+   * 'OS dependent'. They start with WSA_, apparently for the same
+   * reason that practitioners of some craft traditions deliberately
+   * introduce imperfections into their baskets and rugs "to allow the
+   * evil spirits to escape."  If we catch them, then our binaries
+   * might not report consistent results across versions of Windows.
+   * Thus, I'm going to let them all fall through.
+   */
+  { -1, NULL },
+};
+const char *tor_socket_strerror(int e)
+{
+  int i;
+  for (i=0; windows_socket_errors[i].code >= 0; ++i) {
+    if (e == windows_socket_errors[i].code)
+      return windows_socket_errors[i].msg;
+  }
+  return strerror(e);
+}
+#endif
+
 /*
  *    Filesystem operations.
  */
@@ -1476,7 +1549,7 @@ void write_pidfile(char *filename) {
   FILE *pidfile;
 
   if ((pidfile = fopen(filename, "w")) == NULL) {
-    log_fn(LOG_WARN, "unable to open %s for writing: %s", filename,
+    log_fn(LOG_WARN, "Unable to open %s for writing: %s", filename,
            strerror(errno));
   } else {
     fprintf(pidfile, "%d\n", (int)getpid());

+ 2 - 0
src/common/util.h

@@ -250,11 +250,13 @@ int tor_lookup_hostname(const char *name, uint32_t *addr);
 #define ERRNO_IS_EINPROGRESS(e)      ((e) == WSAEINPROGRESS)
 #define ERRNO_IS_CONN_EINPROGRESS(e) ((e) == WSAEINPROGRESS || (e)== WSAEINVAL)
 int tor_socket_errno(int sock);
+const char *tor_socket_strerror(int e);
 #else
 #define ERRNO_IS_EAGAIN(e)           ((e) == EAGAIN)
 #define ERRNO_IS_EINPROGRESS(e)      ((e) == EINPROGRESS)
 #define ERRNO_IS_CONN_EINPROGRESS(e) ((e) == EINPROGRESS)
 #define tor_socket_errno(sock)       (errno)
+#define tor_socket_strerror(e)              strerror(e)
 #endif
 
 #endif

+ 3 - 3
src/or/connection.c

@@ -279,13 +279,13 @@ int connection_create_listener(char *bindaddress, uint16_t bindport, int type) {
 
   if(bind(s,(struct sockaddr *)&bindaddr,sizeof(bindaddr)) < 0) {
     log_fn(LOG_WARN,"Could not bind to port %u: %s",bindport,
-           strerror(tor_socket_errno(s)));
+           tor_socket_strerror(tor_socket_errno(s)));
     return -1;
   }
 
   if(listen(s,SOMAXCONN) < 0) {
     log_fn(LOG_WARN,"Could not listen on port %u: %s",bindport,
-           strerror(tor_socket_errno(s)));
+           tor_socket_strerror(tor_socket_errno(s)));
     return -1;
   }
 
@@ -392,7 +392,7 @@ int connection_connect(connection_t *conn, char *address, uint32_t addr, uint16_
     if(!ERRNO_IS_CONN_EINPROGRESS(tor_socket_errno(s))) {
       /* yuck. kill it. */
       log_fn(LOG_INFO,"Connect() to %s:%u failed: %s",address,port,
-             strerror(tor_socket_errno(s)));
+             tor_socket_strerror(tor_socket_errno(s)));
       tor_close_socket(s);
       return -1;
     } else {

+ 2 - 1
src/or/connection_edge.c

@@ -1067,7 +1067,8 @@ int connection_ap_make_bridge(char *address, uint16_t port) {
   log_fn(LOG_INFO,"Making AP bridge to %s:%d ...",address,port);
 
   if(tor_socketpair(AF_UNIX, SOCK_STREAM, 0, fd) < 0) {
-    log(LOG_WARN, "Couldn't construct socketpair (%s). Network down? Delaying.", strerror(errno));
+    log(LOG_WARN,"Couldn't construct socketpair (%s). Network down? Delaying.",
+        tor_socket_strerror(tor_socket_errno(-1)));
     return -1;
   }
 

+ 2 - 1
src/or/cpuworker.c

@@ -214,7 +214,8 @@ static int spawn_cpuworker(void) {
   connection_t *conn;
 
   if(tor_socketpair(AF_UNIX, SOCK_STREAM, 0, fd) < 0) {
-    log(LOG_ERR, "Couldn't construct socketpair: %s", strerror(errno));
+    log(LOG_ERR, "Couldn't construct socketpair: %s",
+        tor_socket_strerror(tor_socket_errno(-1)));
     exit(1);
   }
 

+ 2 - 1
src/or/dns.c

@@ -503,7 +503,8 @@ static int spawn_dnsworker(void) {
   connection_t *conn;
 
   if(tor_socketpair(AF_UNIX, SOCK_STREAM, 0, fd) < 0) {
-    log(LOG_ERR, "Couldn't construct socketpair: %s", strerror(errno));
+    log(LOG_ERR, "Couldn't construct socketpair: %s",
+        tor_socket_strerror(tor_socket_errno(-1)));
     exit(1);
   }
 

+ 2 - 1
src/or/main.c

@@ -656,7 +656,8 @@ static int do_main_loop(void) {
       /* let the program survive things like ^z */
       if(tor_socket_errno(-1) != EINTR) {
         log_fn(LOG_ERR,"poll failed: %s [%d]",
-               strerror(tor_socket_errno(-1)), tor_socket_errno(-1));
+               tor_socket_strerror(tor_socket_errno(-1)),
+               tor_socket_errno(-1));
         return -1;
       } else {
         log_fn(LOG_DEBUG,"poll interrupted.");