[ovs-dev] [PATCH v2 2/3] socket-util: error number to string for sockets.

Gurucharan Shetty shettyg at nicira.com
Mon Feb 10 19:16:21 UTC 2014


For winsock2 functions, error number has to be converted to string
using FormatMessage().

Signed-off-by: Gurucharan Shetty <gshetty at nicira.com>
---
 lib/socket-util.c |   25 +++++++++++++++++++++++++
 lib/socket-util.h |    2 ++
 2 files changed, 27 insertions(+)

diff --git a/lib/socket-util.c b/lib/socket-util.c
index 24fc6fe..df28424 100644
--- a/lib/socket-util.c
+++ b/lib/socket-util.c
@@ -22,6 +22,7 @@
 #include <net/if.h>
 #include <netdb.h>
 #include <poll.h>
+#include <pthread.h>
 #include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -35,6 +36,7 @@
 #include <unistd.h>
 #include "dynamic-string.h"
 #include "fatal-signal.h"
+#include "ovs-thread.h"
 #include "packets.h"
 #include "poll-loop.h"
 #include "util.h"
@@ -64,6 +66,13 @@ VLOG_DEFINE_THIS_MODULE(socket_util);
  * space for a null terminator. */
 #define MAX_UN_LEN (sizeof(((struct sockaddr_un *) 0)->sun_path) - 1)
 
+#ifdef _WIN32
+/* Buffer used by sock_strerror(). */
+DEFINE_STATIC_PER_THREAD_DATA(struct { char s[128]; },
+                              sockerror_buffer,
+                              { "" });
+#endif
+
 static int getsockopt_int(int fd, int level, int option, const char *optname,
                           int *valuep);
 
@@ -1226,3 +1235,19 @@ af_inet_ifreq_ioctl(const char *name, struct ifreq *ifr, unsigned long int cmd,
     return error;
 }
 
+/* Convert socket error number to a string. */
+char *
+sock_strerror(int error)
+{
+#ifdef _WIN32
+    enum { BUFSIZE = sizeof sockerror_buffer_get()->s };
+    char *buffer = sockerror_buffer_get()->s;
+
+    FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
+                  | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, error, 0,
+                  buffer, BUFSIZE, NULL);
+    return buffer;
+#else
+    return (char *)ovs_strerror(error);
+#endif
+}
diff --git a/lib/socket-util.h b/lib/socket-util.h
index d5b44b0..9cb5e6a 100644
--- a/lib/socket-util.h
+++ b/lib/socket-util.h
@@ -79,4 +79,6 @@ int af_inet_ioctl(unsigned long int command, const void *arg);
 int af_inet_ifreq_ioctl(const char *name, struct ifreq *,
                         unsigned long int cmd, const char *cmd_name);
 
+char *sock_strerror(int error);
+
 #endif /* socket-util.h */
-- 
1.7.9.5




More information about the dev mailing list