diff --git a/lib/client-handshake.c b/lib/client-handshake.c index 004ed939..09e83bda 100644 --- a/lib/client-handshake.c +++ b/lib/client-handshake.c @@ -159,8 +159,10 @@ struct libwebsocket *libwebsocket_client_connect_2( wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_CONNECT; lws_libev_accept(context, wsi, wsi->sock); - if (insert_wsi_socket_into_fds(context, wsi)) + if (insert_wsi_socket_into_fds(context, wsi)) { + compatible_close(wsi->sock); goto oom4; + } /* * past here, we can't simply free the structs as error @@ -190,14 +192,12 @@ struct libwebsocket *libwebsocket_client_connect_2( (struct sockaddr_in *)v, n) < 0) { lwsl_err("Unable to find interface %s\n", context->iface); - compatible_close(wsi->sock); goto failed; } if (bind(wsi->sock, v, n) < 0) { lwsl_err("Error binding to interface %s", context->iface); - compatible_close(wsi->sock); goto failed; } } @@ -350,7 +350,7 @@ libwebsocket_client_connect(struct libwebsocket_context *context, if (wsi == NULL) goto bail; - wsi->sock = -1; + wsi->sock = LWS_SOCK_INVALID; /* -1 means just use latest supported */ diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c index d1ab6194..792d1485 100644 --- a/lib/libwebsockets.c +++ b/lib/libwebsockets.c @@ -300,7 +300,7 @@ just_kill_connection: /* lwsl_info("closing fd=%d\n", wsi->sock); */ - if (!lws_ssl_close(wsi) && wsi->sock >= 0) { + if (!lws_ssl_close(wsi) && lws_socket_is_valid(wsi->sock)) { n = shutdown(wsi->sock, SHUT_RDWR); if (n) lwsl_debug("closing: shutdown ret %d\n", LWS_ERRNO); @@ -308,6 +308,7 @@ just_kill_connection: n = compatible_close(wsi->sock); if (n) lwsl_debug("closing: close ret %d\n", LWS_ERRNO); + wsi->sock = LWS_SOCK_INVALID; } /* outermost destroy notification for wsi (user_space still intact) */ diff --git a/lib/output.c b/lib/output.c index 44b2ab7e..81d1d5c1 100644 --- a/lib/output.c +++ b/lib/output.c @@ -120,7 +120,7 @@ int lws_issue_raw(struct libwebsocket *wsi, unsigned char *buf, size_t len) n = m; goto handle_truncated_send; } - if (wsi->sock < 0) + if (!lws_socket_is_valid(wsi->sock)) lwsl_warn("** error invalid sock but expected to send\n"); /* diff --git a/lib/pollfd.c b/lib/pollfd.c index a70f8e64..04e2e4d3 100644 --- a/lib/pollfd.c +++ b/lib/pollfd.c @@ -41,7 +41,7 @@ insert_wsi_socket_into_fds(struct libwebsocket_context *context, #endif assert(wsi); - assert(wsi->sock >= 0); + assert(lws_socket_is_valid(wsi->sock)); lwsl_info("insert_wsi_socket_into_fds: wsi=%p, sock=%d, fds pos=%d\n", wsi, wsi->sock, context->fds_count); @@ -115,7 +115,7 @@ remove_wsi_socket_from_fds(struct libwebsocket_context *context, wsi->position_in_fds_table = -1; /* remove also from external POLL support via protocol 0 */ - if (wsi->sock) { + if (lws_socket_is_valid(wsi->sock)) { if (context->protocols[0].callback(context, wsi, LWS_CALLBACK_DEL_POLL_FD, wsi->user_space, (void *) &pa, 0)) diff --git a/lib/server.c b/lib/server.c index 0b46402f..632a5293 100644 --- a/lib/server.c +++ b/lib/server.c @@ -49,7 +49,7 @@ int lws_context_init_server(struct lws_context_creation_info *info, #endif sockfd = socket(AF_INET, SOCK_STREAM, 0); - if (sockfd < 0) { + if (sockfd == -1) { lwsl_err("ERROR opening socket\n"); return 1; } @@ -671,7 +671,7 @@ int lws_server_socket_service(struct libwebsocket_context *context, struct libwebsocket *wsi, struct libwebsocket_pollfd *pollfd) { struct libwebsocket *new_wsi = NULL; - int accept_fd = 0; + int accept_fd = LWS_SOCK_INVALID; socklen_t clilen; struct sockaddr_in cli_addr; int n;