[or-cvs] Working strerror for windows socket errors, plus some snide...
Nick Mathewson
nickm at seul.org
Sun May 2 20:18:23 UTC 2004
Update of /home/or/cvsroot/src/common
In directory moria.mit.edu:/tmp/cvs-serv18306/src/common
Modified Files:
tortls.c util.c util.h
Log Message:
Working strerror for windows socket errors, plus some snide comments.
Index: tortls.c
===================================================================
RCS file: /home/or/cvsroot/src/common/tortls.c,v
retrieving revision 1.53
retrieving revision 1.54
diff -u -d -r1.53 -r1.54
--- tortls.c 1 May 2004 23:29:20 -0000 1.53
+++ tortls.c 2 May 2004 20:18:21 -0000 1.54
@@ -121,7 +121,7 @@
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;
Index: util.c
===================================================================
RCS file: /home/or/cvsroot/src/common/util.c,v
retrieving revision 1.97
retrieving revision 1.98
diff -u -d -r1.97 -r1.98
--- util.c 1 May 2004 23:29:20 -0000 1.97
+++ util.c 2 May 2004 20:18:21 -0000 1.98
@@ -1111,6 +1111,79 @@
}
#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 @@
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());
Index: util.h
===================================================================
RCS file: /home/or/cvsroot/src/common/util.h,v
retrieving revision 1.64
retrieving revision 1.65
diff -u -d -r1.64 -r1.65
--- util.h 1 May 2004 20:46:27 -0000 1.64
+++ util.h 2 May 2004 20:18:21 -0000 1.65
@@ -250,11 +250,13 @@
#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
More information about the tor-commits
mailing list