Skip to content
Snippets Groups Projects
Commit 2dbd837c authored by Patrick Gansterer's avatar Patrick Gansterer Committed by Andy Green
Browse files

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.
parent 61a6ae4f
Branches
Tags
No related merge requests found
...@@ -93,9 +93,9 @@ struct libwebsocket *libwebsocket_client_connect_2( ...@@ -93,9 +93,9 @@ struct libwebsocket *libwebsocket_client_connect_2(
bzero(&server_addr.sin_zero, 8); bzero(&server_addr.sin_zero, 8);
if (connect(wsi->sock, (struct sockaddr *)&server_addr, 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"); lwsl_client("nonblocking connect retry\n");
/* /*
...@@ -107,9 +107,9 @@ struct libwebsocket *libwebsocket_client_connect_2( ...@@ -107,9 +107,9 @@ struct libwebsocket *libwebsocket_client_connect_2(
return wsi; 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; goto failed;
} }
} }
... ...
......
...@@ -78,7 +78,7 @@ int lws_client_socket_service(struct libwebsocket_context *context, ...@@ -78,7 +78,7 @@ int lws_client_socket_service(struct libwebsocket_context *context,
sizeof(context->service_buffer), 0); sizeof(context->service_buffer), 0);
if (n < 0) { if (n < 0) {
if (errno == EAGAIN) { if (LWS_ERRNO == LWS_EAGAIN) {
lwsl_debug( lwsl_debug(
"Proxy read returned EAGAIN... retrying\n"); "Proxy read returned EAGAIN... retrying\n");
return 0; return 0;
... ...
......
...@@ -523,12 +523,12 @@ just_kill_connection: ...@@ -523,12 +523,12 @@ just_kill_connection:
n = shutdown(wsi->sock, SHUT_RDWR); n = shutdown(wsi->sock, SHUT_RDWR);
if (n) if (n)
lwsl_debug("closing: shutdown returned %d\n", lwsl_debug("closing: shutdown returned %d\n",
errno); LWS_ERRNO);
n = compatible_close(wsi->sock); n = compatible_close(wsi->sock);
if (n) if (n)
lwsl_debug("closing: close returned %d\n", lwsl_debug("closing: close returned %d\n",
errno); LWS_ERRNO);
} }
#ifdef LWS_OPENSSL_SUPPORT #ifdef LWS_OPENSSL_SUPPORT
} }
...@@ -585,14 +585,14 @@ libwebsockets_get_peer_addresses(struct libwebsocket_context *context, ...@@ -585,14 +585,14 @@ libwebsockets_get_peer_addresses(struct libwebsocket_context *context,
len = sizeof(sin); len = sizeof(sin);
if (getpeername(fd, (struct sockaddr *) &sin, &len) < 0) { 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; goto bail;
} }
host = gethostbyaddr((char *) &sin.sin_addr, sizeof(sin.sin_addr), host = gethostbyaddr((char *) &sin.sin_addr, sizeof(sin.sin_addr),
AF_INET); AF_INET);
if (host == NULL) { if (host == NULL) {
lwsl_warn("gethostbyaddr: %s\n", strerror(errno)); lwsl_warn("gethostbyaddr: %s\n", strerror(LWS_ERRNO));
goto bail; goto bail;
} }
...@@ -1149,8 +1149,8 @@ read_pending: ...@@ -1149,8 +1149,8 @@ read_pending:
if (eff_buf.token_len < 0) { if (eff_buf.token_len < 0) {
lwsl_debug("service_fd read ret = %d, errno = %d\n", lwsl_debug("service_fd read ret = %d, errno = %d\n",
eff_buf.token_len, errno); eff_buf.token_len, LWS_ERRNO);
if (errno != EINTR && errno != EAGAIN) if (LWS_ERRNO != LWS_EINTR && LWS_ERRNO != LWS_EAGAIN)
goto close_and_handled; goto close_and_handled;
n = 0; n = 0;
goto handled; goto handled;
...@@ -1425,7 +1425,7 @@ libwebsocket_service(struct libwebsocket_context *context, int timeout_ms) ...@@ -1425,7 +1425,7 @@ libwebsocket_service(struct libwebsocket_context *context, int timeout_ms)
} }
if (n < 0) { if (n < 0) {
if (errno != EINTR) if (LWS_ERRNO != LWS_EINTR)
return -1; return -1;
else else
return 0; return 0;
...@@ -2428,7 +2428,7 @@ libwebsocket_create_context(struct lws_context_creation_info *info) ...@@ -2428,7 +2428,7 @@ libwebsocket_create_context(struct lws_context_creation_info *info)
sizeof(serv_addr)); sizeof(serv_addr));
if (n < 0) { if (n < 0) {
lwsl_err("ERROR on binding to port %d (%d %d)\n", lwsl_err("ERROR on binding to port %d (%d %d)\n",
info->port, n, errno); info->port, n, LWS_ERRNO);
compatible_close(sockfd); compatible_close(sockfd);
goto bail; goto bail;
} }
...@@ -2467,10 +2467,10 @@ libwebsocket_create_context(struct lws_context_creation_info *info) ...@@ -2467,10 +2467,10 @@ libwebsocket_create_context(struct lws_context_creation_info *info)
#else #else
if (info->gid != -1) if (info->gid != -1)
if (setgid(info->gid)) if (setgid(info->gid))
lwsl_warn("setgid: %s\n", strerror(errno)); lwsl_warn("setgid: %s\n", strerror(LWS_ERRNO));
if (info->uid != -1) if (info->uid != -1)
if (setuid(info->uid)) if (setuid(info->uid))
lwsl_warn("setuid: %s\n", strerror(errno)); lwsl_warn("setuid: %s\n", strerror(LWS_ERRNO));
#endif #endif
/* initialize supported protocols */ /* initialize supported protocols */
... ...
......
...@@ -156,7 +156,7 @@ int lws_issue_raw(struct libwebsocket *wsi, unsigned char *buf, size_t len) ...@@ -156,7 +156,7 @@ int lws_issue_raw(struct libwebsocket *wsi, unsigned char *buf, size_t len)
n = SSL_write(wsi->ssl, buf, len); n = SSL_write(wsi->ssl, buf, len);
lws_latency(context, wsi, "SSL_write lws_issue_raw", n, n >= 0); lws_latency(context, wsi, "SSL_write lws_issue_raw", n, n >= 0);
if (n < 0) { if (n < 0) {
if (errno == EAGAIN || errno == EINTR) { if (LWS_ERRNO == LWS_EAGAIN || LWS_ERRNO == LWS_EINTR) {
n = 0; n = 0;
goto handle_truncated_send; goto handle_truncated_send;
} }
...@@ -168,7 +168,7 @@ int lws_issue_raw(struct libwebsocket *wsi, unsigned char *buf, size_t len) ...@@ -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); n = send(wsi->sock, buf, len, MSG_NOSIGNAL);
lws_latency(context, wsi, "send lws_issue_raw", n, n == len); lws_latency(context, wsi, "send lws_issue_raw", n, n == len);
if (n < 0) { if (n < 0) {
if (errno == EAGAIN || errno == EINTR) { if (LWS_ERRNO == LWS_EAGAIN || LWS_ERRNO == LWS_EINTR) {
n = 0; n = 0;
goto handle_truncated_send; goto handle_truncated_send;
} }
... ...
......
...@@ -61,18 +61,13 @@ ...@@ -61,18 +61,13 @@
#if defined(WIN32) || defined(_WIN32) #if defined(WIN32) || defined(_WIN32)
#define LWS_NO_DAEMONIZE #define LWS_NO_DAEMONIZE
#ifndef EWOULDBLOCK #define LWS_ERRNO WSAGetLastError()
#define EWOULDBLOCK EAGAIN #define LWS_EAGAIN WSAEWOULDBLOCK
#endif #define LWS_EALREADY WSAEALREADY
#ifndef EALREADY #define LWS_EINPROGRESS WSAEINPROGRESS
#define EALREADY WSAEALREADY #define LWS_EINTR WSAEINTR
#endif #define LWS_EISCONN WSAEISCONN
#ifndef EINPROGRESS #define LWS_EWOULDBLOCK WSAEWOULDBLOCK
#define EINPROGRESS WSAEINPROGRESS
#endif
#ifndef EISCONN
#define EISCONN WSAEISCONN
#endif
#define compatible_close(fd) closesocket(fd); #define compatible_close(fd) closesocket(fd);
#ifdef __MINGW64__ #ifdef __MINGW64__
...@@ -103,6 +98,13 @@ ...@@ -103,6 +98,13 @@
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/time.h> #include <sys/time.h>
#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 LWS_INVALID_FILE -1
#define compatible_close(fd) close(fd); #define compatible_close(fd) close(fd);
#endif #endif
... ...
......
...@@ -153,7 +153,7 @@ int lws_server_socket_service(struct libwebsocket_context *context, ...@@ -153,7 +153,7 @@ int lws_server_socket_service(struct libwebsocket_context *context,
if (len < 0) { if (len < 0) {
lwsl_debug("Socket read returned %d\n", len); 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( libwebsocket_close_and_free_session(
context, wsi, context, wsi,
LWS_CLOSE_STATUS_NOSTATUS); LWS_CLOSE_STATUS_NOSTATUS);
...@@ -226,11 +226,11 @@ int lws_server_socket_service(struct libwebsocket_context *context, ...@@ -226,11 +226,11 @@ int lws_server_socket_service(struct libwebsocket_context *context,
"unencrypted accept LWS_CONNMODE_SERVER_LISTENER", "unencrypted accept LWS_CONNMODE_SERVER_LISTENER",
accept_fd, accept_fd >= 0); accept_fd, accept_fd >= 0);
if (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"); lwsl_debug("accept asks to try again\n");
break; break;
} }
lwsl_warn("ERROR on accept: %s\n", strerror(errno)); lwsl_warn("ERROR on accept: %s\n", strerror(LWS_ERRNO));
break; break;
} }
... ...
......
...@@ -11,7 +11,6 @@ ...@@ -11,7 +11,6 @@
#endif #endif
#include <stdlib.h> #include <stdlib.h>
#include <errno.h>
#include "websock-w32.h" #include "websock-w32.h"
PFNWSAPOLL poll = NULL; PFNWSAPOLL poll = NULL;
...@@ -29,7 +28,7 @@ INT WSAAPI emulated_poll(LPWSAPOLLFD fdarray, ULONG nfds, INT timeout) ...@@ -29,7 +28,7 @@ INT WSAAPI emulated_poll(LPWSAPOLLFD fdarray, ULONG nfds, INT timeout)
WSAPOLLFD * poll_fd = fdarray; WSAPOLLFD * poll_fd = fdarray;
if (NULL == fdarray) { if (NULL == fdarray) {
errno = EFAULT; WSASetLastError(WSAEFAULT);
return -1; return -1;
} }
... ...
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment