socket-util: Move sock_errno() to socket-util.
[sliver-openvswitch.git] / lib / socket-util.h
index b54d1a1..6e55a4c 100644 (file)
@@ -17,6 +17,7 @@
 #ifndef SOCKET_UTIL_H
 #define SOCKET_UTIL_H 1
 
+#include <errno.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <sys/time.h>
@@ -86,5 +87,28 @@ uint16_t ss_get_port(const struct sockaddr_storage *);
 char *ss_format_address(const struct sockaddr_storage *,
                         char *buf, size_t bufsize);
 size_t ss_length(const struct sockaddr_storage *);
+const char *sock_strerror(int error);
+
+#ifdef _WIN32
+/* Windows defines the 'optval' argument as char * instead of void *. */
+#define setsockopt(sock, level, optname, optval, optlen) \
+    rpl_setsockopt(sock, level, optname, optval, optlen)
+static inline int rpl_setsockopt(int sock, int level, int optname,
+                                 const void *optval, socklen_t optlen)
+{
+    return (setsockopt)(sock, level, optname, optval, optlen);
+}
+#endif
+
+/* In Windows platform, errno is not set for socket calls.
+ * The last error has to be gotten from WSAGetLastError(). */
+static inline int sock_errno(void)
+{
+#ifdef _WIN32
+    return WSAGetLastError();
+#else
+    return errno;
+#endif
+}
 
 #endif /* socket-util.h */