From 54e3687178623ac0a155d075fd8e82107b537e54 Mon Sep 17 00:00:00 2001 From: Hamish Coleman Date: Mon, 9 Dec 2024 18:08:39 +1100 Subject: [PATCH] Handle non-blocking tcp connections on windows --- src/edge_utils.c | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/edge_utils.c b/src/edge_utils.c index 14bcf5a..d7865a4 100644 --- a/src/edge_utils.c +++ b/src/edge_utils.c @@ -381,12 +381,35 @@ void supernode_connect (struct n3n_runtime_data *eee) { fill_sockaddr((struct sockaddr*)&sn_sock, sizeof(sn_sock), &eee->curr_sn->sock); - if((connect(eee->sock, (struct sockaddr*)&(sn_sock), sizeof(struct sockaddr)) < 0) - && (errno != EINPROGRESS)) { + int result = connect( + eee->sock, + (struct sockaddr*)&(sn_sock), + sizeof(struct sockaddr) + ); + +#ifndef _WIN32 + if((result == -1) && (errno != EINPROGRESS)) { traceEvent(TRACE_INFO, "Error connecting TCP: %i", errno); + closesocket(eee->sock); + mainloop_unregister_fd(eee->sock); eee->sock = -1; return; } +#else + // Oh Windows, this just seems needlessly incompatible + int wsaerr = WSAGetLastError(); + if((result == -1) && (wsaerr != WSAEWOULDBLOCK)) { + traceEvent( + TRACE_INFO, + "Error connecting TCP: WSAGetLastError %i", + wsaerr + ); + closesocket(eee->sock); + mainloop_unregister_fd(eee->sock); + eee->sock = -1; + return; + } +#endif } if(eee->conf.tos) {