From 82e4c00bd77b1c08b6355b3b2c250b3490ca1430 Mon Sep 17 00:00:00 2001 From: Patrick Gansterer Date: Fri, 28 Feb 2014 12:37:52 +0100 Subject: [PATCH 1/3] Use WSAGetLastError() instead of errno on Windows Error codes set by Windows Sockets are not made available through the errno variable. Checking them via WSAGetLastError() is the corret solution. --- lib/client-handshake.c | 8 ++++---- lib/client.c | 2 +- lib/libwebsockets.c | 20 ++++++++++---------- lib/output.c | 4 ++-- lib/private-libwebsockets.h | 26 ++++++++++++++------------ lib/server.c | 6 +++--- win32port/win32helpers/websock-w32.c | 3 +-- 7 files changed, 35 insertions(+), 34 deletions(-) diff --git a/lib/client-handshake.c b/lib/client-handshake.c index 6bce8fe52d..d87d21027c 100644 --- a/lib/client-handshake.c +++ b/lib/client-handshake.c @@ -93,9 +93,9 @@ struct libwebsocket *libwebsocket_client_connect_2( bzero(&server_addr.sin_zero, 8); if (connect(wsi->sock, (struct sockaddr *)&server_addr, - sizeof(struct sockaddr)) == -1 || errno == EISCONN) { + sizeof(struct sockaddr)) == -1 || LWS_ERRNO == LWS_EISCONN) { - if (errno == EALREADY || errno == EINPROGRESS) { + if (LWS_ERRNO == LWS_EALREADY || LWS_ERRNO == LWS_EINPROGRESS) { lwsl_client("nonblocking connect retry\n"); /* @@ -107,9 +107,9 @@ struct libwebsocket *libwebsocket_client_connect_2( return wsi; } - if (errno != EISCONN) { + if (LWS_ERRNO != LWS_EISCONN) { - lwsl_debug("Connect failed errno=%d\n", errno); + lwsl_debug("Connect failed errno=%d\n", LWS_ERRNO); goto failed; } } diff --git a/lib/client.c b/lib/client.c index 4775555ada..729e9b3821 100644 --- a/lib/client.c +++ b/lib/client.c @@ -78,7 +78,7 @@ int lws_client_socket_service(struct libwebsocket_context *context, sizeof(context->service_buffer), 0); if (n < 0) { - if (errno == EAGAIN) { + if (LWS_ERRNO == LWS_EAGAIN) { lwsl_debug( "Proxy read returned EAGAIN... retrying\n"); return 0; diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c index 0b5c309fc7..a280f8aaef 100644 --- a/lib/libwebsockets.c +++ b/lib/libwebsockets.c @@ -523,12 +523,12 @@ libwebsocket_close_and_free_session(struct libwebsocket_context *context, n = shutdown(wsi->sock, SHUT_RDWR); if (n) lwsl_debug("closing: shutdown returned %d\n", - errno); + LWS_ERRNO); n = compatible_close(wsi->sock); if (n) lwsl_debug("closing: close returned %d\n", - errno); + LWS_ERRNO); } #ifdef LWS_OPENSSL_SUPPORT } @@ -585,14 +585,14 @@ libwebsockets_get_peer_addresses(struct libwebsocket_context *context, len = sizeof(sin); if (getpeername(fd, (struct sockaddr *) &sin, &len) < 0) { - lwsl_warn("getpeername: %s\n", strerror(errno)); + lwsl_warn("getpeername: %s\n", strerror(LWS_ERRNO)); goto bail; } host = gethostbyaddr((char *) &sin.sin_addr, sizeof(sin.sin_addr), AF_INET); if (host == NULL) { - lwsl_warn("gethostbyaddr: %s\n", strerror(errno)); + lwsl_warn("gethostbyaddr: %s\n", strerror(LWS_ERRNO)); goto bail; } @@ -1149,8 +1149,8 @@ libwebsocket_service_fd(struct libwebsocket_context *context, if (eff_buf.token_len < 0) { lwsl_debug("service_fd read ret = %d, errno = %d\n", - eff_buf.token_len, errno); - if (errno != EINTR && errno != EAGAIN) + eff_buf.token_len, LWS_ERRNO); + if (LWS_ERRNO != LWS_EINTR && LWS_ERRNO != LWS_EAGAIN) goto close_and_handled; n = 0; goto handled; @@ -1425,7 +1425,7 @@ libwebsocket_service(struct libwebsocket_context *context, int timeout_ms) } if (n < 0) { - if (errno != EINTR) + if (LWS_ERRNO != LWS_EINTR) return -1; else return 0; @@ -2428,7 +2428,7 @@ libwebsocket_create_context(struct lws_context_creation_info *info) sizeof(serv_addr)); if (n < 0) { lwsl_err("ERROR on binding to port %d (%d %d)\n", - info->port, n, errno); + info->port, n, LWS_ERRNO); compatible_close(sockfd); goto bail; } @@ -2467,10 +2467,10 @@ libwebsocket_create_context(struct lws_context_creation_info *info) #else if (info->gid != -1) if (setgid(info->gid)) - lwsl_warn("setgid: %s\n", strerror(errno)); + lwsl_warn("setgid: %s\n", strerror(LWS_ERRNO)); if (info->uid != -1) if (setuid(info->uid)) - lwsl_warn("setuid: %s\n", strerror(errno)); + lwsl_warn("setuid: %s\n", strerror(LWS_ERRNO)); #endif /* initialize supported protocols */ diff --git a/lib/output.c b/lib/output.c index 4b5d948c38..a835ec3914 100644 --- a/lib/output.c +++ b/lib/output.c @@ -156,7 +156,7 @@ int lws_issue_raw(struct libwebsocket *wsi, unsigned char *buf, size_t len) n = SSL_write(wsi->ssl, buf, len); lws_latency(context, wsi, "SSL_write lws_issue_raw", n, n >= 0); if (n < 0) { - if (errno == EAGAIN || errno == EINTR) { + if (LWS_ERRNO == LWS_EAGAIN || LWS_ERRNO == LWS_EINTR) { n = 0; goto handle_truncated_send; } @@ -168,7 +168,7 @@ int lws_issue_raw(struct libwebsocket *wsi, unsigned char *buf, size_t len) n = send(wsi->sock, buf, len, MSG_NOSIGNAL); lws_latency(context, wsi, "send lws_issue_raw", n, n == len); if (n < 0) { - if (errno == EAGAIN || errno == EINTR) { + if (LWS_ERRNO == LWS_EAGAIN || LWS_ERRNO == LWS_EINTR) { n = 0; goto handle_truncated_send; } diff --git a/lib/private-libwebsockets.h b/lib/private-libwebsockets.h index c325c9be47..ed2ff3b993 100644 --- a/lib/private-libwebsockets.h +++ b/lib/private-libwebsockets.h @@ -61,18 +61,13 @@ #if defined(WIN32) || defined(_WIN32) #define LWS_NO_DAEMONIZE -#ifndef EWOULDBLOCK -#define EWOULDBLOCK EAGAIN -#endif -#ifndef EALREADY -#define EALREADY WSAEALREADY -#endif -#ifndef EINPROGRESS -#define EINPROGRESS WSAEINPROGRESS -#endif -#ifndef EISCONN -#define EISCONN WSAEISCONN -#endif +#define LWS_ERRNO WSAGetLastError() +#define LWS_EAGAIN WSAEWOULDBLOCK +#define LWS_EALREADY WSAEALREADY +#define LWS_EINPROGRESS WSAEINPROGRESS +#define LWS_EINTR WSAEINTR +#define LWS_EISCONN WSAEISCONN +#define LWS_EWOULDBLOCK WSAEWOULDBLOCK #define compatible_close(fd) closesocket(fd); #ifdef __MINGW64__ @@ -103,6 +98,13 @@ #include #include +#define LWS_ERRNO errno +#define LWS_EAGAIN EAGAIN +#define LWS_EALREADY EALREADY +#define LWS_EINPROGRESS EINPROGRESS +#define LWS_EINTR EINTR +#define LWS_EISCONN EISCONN +#define LWS_EWOULDBLOCK EWOULDBLOCK #define LWS_INVALID_FILE -1 #define compatible_close(fd) close(fd); #endif diff --git a/lib/server.c b/lib/server.c index 749cec06c3..2f2c5b3822 100644 --- a/lib/server.c +++ b/lib/server.c @@ -153,7 +153,7 @@ int lws_server_socket_service(struct libwebsocket_context *context, if (len < 0) { lwsl_debug("Socket read returned %d\n", len); - if (errno != EINTR && errno != EAGAIN) + if (LWS_ERRNO != LWS_EINTR && LWS_ERRNO != LWS_EAGAIN) libwebsocket_close_and_free_session( context, wsi, LWS_CLOSE_STATUS_NOSTATUS); @@ -226,11 +226,11 @@ int lws_server_socket_service(struct libwebsocket_context *context, "unencrypted accept LWS_CONNMODE_SERVER_LISTENER", accept_fd, accept_fd >= 0); if (accept_fd < 0) { - if (errno == EAGAIN || errno == EWOULDBLOCK) { + if (LWS_ERRNO == LWS_EAGAIN || LWS_ERRNO == LWS_EWOULDBLOCK) { lwsl_debug("accept asks to try again\n"); break; } - lwsl_warn("ERROR on accept: %s\n", strerror(errno)); + lwsl_warn("ERROR on accept: %s\n", strerror(LWS_ERRNO)); break; } diff --git a/win32port/win32helpers/websock-w32.c b/win32port/win32helpers/websock-w32.c index ec3022e93d..a919c4b072 100644 --- a/win32port/win32helpers/websock-w32.c +++ b/win32port/win32helpers/websock-w32.c @@ -11,7 +11,6 @@ #endif #include -#include #include "websock-w32.h" PFNWSAPOLL poll = NULL; @@ -29,7 +28,7 @@ INT WSAAPI emulated_poll(LPWSAPOLLFD fdarray, ULONG nfds, INT timeout) WSAPOLLFD * poll_fd = fdarray; if (NULL == fdarray) { - errno = EFAULT; + WSASetLastError(WSAEFAULT); return -1; } From af93a71a901564f3cef0cd79c18e646600fdbd51 Mon Sep 17 00:00:00 2001 From: Patrick Gansterer Date: Fri, 28 Feb 2014 12:57:46 +0100 Subject: [PATCH 2/3] Remove duplicated implementations for bzero() Define bzero() in a central place if HAVE_BZERO is not set --- lib/private-libwebsockets.h | 4 ++++ lib/sha-1.c | 2 -- win32port/win32helpers/websock-w32.h | 2 -- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/private-libwebsockets.h b/lib/private-libwebsockets.h index ed2ff3b993..4c340a1c68 100644 --- a/lib/private-libwebsockets.h +++ b/lib/private-libwebsockets.h @@ -109,6 +109,10 @@ #define compatible_close(fd) close(fd); #endif +#ifndef HAVE_BZERO +#define bzero(b, len) (memset((b), '\0', (len)), (void) 0) +#endif + #ifndef HAVE_STRERROR #define strerror(x) "" #endif diff --git a/lib/sha-1.c b/lib/sha-1.c index e280345ef6..8b4ccb434b 100644 --- a/lib/sha-1.c +++ b/lib/sha-1.c @@ -61,8 +61,6 @@ typedef unsigned __int64 u_int64_t; #endif #endif -#define bzero(b, len) (memset((b), '\0', (len)), (void) 0) - #else #include diff --git a/win32port/win32helpers/websock-w32.h b/win32port/win32helpers/websock-w32.h index 0413b856fd..9ce1cae693 100644 --- a/win32port/win32helpers/websock-w32.h +++ b/win32port/win32helpers/websock-w32.h @@ -9,8 +9,6 @@ #pragma warning(disable : 4996) -#define bzero(b,len) (memset((b), '\0', (len)), (void) 0) - #define MSG_NOSIGNAL 0 #define SHUT_RDWR SD_BOTH From 536d44bba1ecae2643ff5abf77cac78b24d244e9 Mon Sep 17 00:00:00 2001 From: Patrick Gansterer Date: Fri, 28 Feb 2014 15:15:21 +0100 Subject: [PATCH 3/3] Replace inline with LWS_INLINE Use a new macro instead of defining inline to avoid namespace pollution in the embedding application. --- CMakeLists.txt | 21 --------------------- config.h.cmake | 3 --- lib/libwebsockets.c | 2 +- lib/libwebsockets.h | 10 +++++++++- lib/private-libwebsockets.h | 4 ++-- lib/server.c | 4 ++-- 6 files changed, 14 insertions(+), 30 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1f7642f4bb..bbdeb27a56 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -115,27 +115,6 @@ endif() include_directories("${PROJECT_BINARY_DIR}") -include(CheckCSourceCompiles) - -# Check for different inline keyword versions. -foreach(KEYWORD "inline" "__inline__" "__inline") - set(CMAKE_REQUIRED_DEFINITIONS "-DKEYWORD=${KEYWORD}") - CHECK_C_SOURCE_COMPILES( - " - #include - KEYWORD void a() {} - int main(int argc, char **argv) { a(); return 0; } - " HAVE_${KEYWORD}) -endforeach() - -if (NOT HAVE_inline) - if (HAVE___inline__) - set(inline __inline__) - elseif(HAVE___inline) - set(inline __inline) - endif() -endif() - # Put the libaries and binaries that get built into directories at the # top of the build tree rather than in hard-to-find leaf directories. SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin") diff --git a/config.h.cmake b/config.h.cmake index 0c2dbfba39..4a7bb4d9f2 100644 --- a/config.h.cmake +++ b/config.h.cmake @@ -154,6 +154,3 @@ /* Define as `fork' if `vfork' does not work. */ //#cmakedefine vfork - -/* Define if the inline keyword doesn't exist. */ -#cmakedefine inline diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c index a280f8aaef..b47517bd31 100644 --- a/lib/libwebsockets.c +++ b/lib/libwebsockets.c @@ -144,7 +144,7 @@ static unsigned long long time_in_microseconds() } #ifdef _WIN32_WCE -static inline time_t time(time_t *t) +static LWS_INLINE time_t time(time_t *t) { time_t ret = time_in_microseconds() / 1000000; *t = ret; diff --git a/lib/libwebsockets.h b/lib/libwebsockets.h index a7c07aaa0c..aff0e2aab2 100644 --- a/lib/libwebsockets.h +++ b/lib/libwebsockets.h @@ -86,10 +86,18 @@ typedef SSIZE_T ssize_t; #include +#if !defined(__cplusplus) && defined(_MSC_VER) +#define LWS_INLINE __inline +#endif + #ifndef LWS_EXTERN #define LWS_EXTERN extern #endif +#ifndef LWS_INLINE +#define LWS_INLINE inline +#endif + #define CONTEXT_PORT_NO_LISTEN 0 #define MAX_MUX_RECURSION 2 @@ -1034,7 +1042,7 @@ libwebsocket_write(struct libwebsocket *wsi, unsigned char *buf, size_t len, enum libwebsocket_write_protocol protocol); /* helper for case where buffer may be const */ -static inline int +static LWS_INLINE int libwebsocket_write_http(struct libwebsocket *wsi, const unsigned char *buf, size_t len) { diff --git a/lib/private-libwebsockets.h b/lib/private-libwebsockets.h index 4c340a1c68..6e68d5fd56 100644 --- a/lib/private-libwebsockets.h +++ b/lib/private-libwebsockets.h @@ -461,10 +461,10 @@ libwebsocket_close_and_free_session(struct libwebsocket_context *context, struct libwebsocket *wsi, enum lws_close_status); #ifndef LWS_LATENCY -static inline void lws_latency(struct libwebsocket_context *context, +static LWS_INLINE void lws_latency(struct libwebsocket_context *context, struct libwebsocket *wsi, const char *action, int ret, int completion) { while (0); } -static inline void lws_latency_pre(struct libwebsocket_context *context, +static LWS_INLINE void lws_latency_pre(struct libwebsocket_context *context, struct libwebsocket *wsi) { while (0); } #else #define lws_latency_pre(_context, _wsi) lws_latency(_context, _wsi, NULL, 0, 0) diff --git a/lib/server.c b/lib/server.c index 2f2c5b3822..2ec280b57e 100644 --- a/lib/server.c +++ b/lib/server.c @@ -485,7 +485,7 @@ LWS_VISIBLE int libwebsockets_return_http_status( } #if defined(WIN32) || defined(_WIN32) -static inline HANDLE lws_open_file(const char* filename, unsigned long* filelen) +static LWS_INLINE HANDLE lws_open_file(const char* filename, unsigned long* filelen) { HANDLE ret; WCHAR buffer[MAX_PATH]; @@ -501,7 +501,7 @@ static inline HANDLE lws_open_file(const char* filename, unsigned long* filelen) return ret; } #else -static inline int lws_open_file(const char* filename, unsigned long* filelen) +static LWS_INLINE int lws_open_file(const char* filename, unsigned long* filelen) { struct stat stat_buf; int ret = open(filename, O_RDONLY);