[ovs-dev] [PATCH v3 2/3] socket-util: error number to string for sockets.
Gurucharan Shetty
shettyg at nicira.com
Mon Feb 10 23:25:37 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 | 29 +++++++++++++++++++++++++++++
lib/socket-util.h | 2 ++
lib/util.c | 4 ++++
3 files changed, 35 insertions(+)
diff --git a/lib/socket-util.c b/lib/socket-util.c
index 24fc6fe..fca18ae 100644
--- a/lib/socket-util.c
+++ b/lib/socket-util.c
@@ -35,6 +35,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 +65,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 +1234,24 @@ af_inet_ifreq_ioctl(const char *name, struct ifreq *ifr, unsigned long int cmd,
return error;
}
+/* For Windows socket calls, 'errno' is not set. One has to call
+ * WSAGetLastError() to get the error number and then pass it to
+ * FormatMessage() (through this function) to get the correct error string.
+
+ * ovs_strerror() calls strerror_r() and would not get the correct error
+ * string for windows sockets, but is good for POSIX. */
+const 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 ovs_strerror(error);
+#endif
+}
diff --git a/lib/socket-util.h b/lib/socket-util.h
index d5b44b0..d2b9f38 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);
+const char *sock_strerror(int error);
+
#endif /* socket-util.h */
diff --git a/lib/util.c b/lib/util.c
index 845f86c..1c868e6 100644
--- a/lib/util.c
+++ b/lib/util.c
@@ -323,6 +323,10 @@ ovs_retval_to_string(int retval)
: ovs_strerror(retval));
}
+/* This function returns the string describing the error number in 'error'
+ * for POSIX platforms. For windows, this function can be used for C library
+ * calls. For socket calls that are also used in windows, use sock_strerror()
+ * instead. For WINAPI calls, look at ovs_lasterror_to_string(). */
const char *
ovs_strerror(int error)
{
--
1.7.9.5
More information about the dev
mailing list