From b9a7444b9fa216d16d91f34d2e12f51fc4d60e46 Mon Sep 17 00:00:00 2001 From: Kirill Isakov Date: Sat, 2 Apr 2022 17:41:50 +0600 Subject: [PATCH] Correct close() function when building with MSVC The original close() was throwing assertion errors from inside the Windows libraries because we were trying to close OS handles as file descriptors. --- src/dropin.h | 1 + src/net_packet.c | 6 +++--- src/net_setup.c | 6 +++--- src/route.c | 4 ++-- src/tincctl.c | 16 ++++++++-------- 5 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/dropin.h b/src/dropin.h index da10cd9a..384b4dbf 100644 --- a/src/dropin.h +++ b/src/dropin.h @@ -76,6 +76,7 @@ extern int gettimeofday(struct timeval *, void *); #define PATH_MAX MAX_PATH #define strcasecmp _stricmp #define strncasecmp _strnicmp +#define close CloseHandle #define __const const typedef int mode_t; diff --git a/src/net_packet.c b/src/net_packet.c index 5bdcb1aa..d171fec6 100644 --- a/src/net_packet.c +++ b/src/net_packet.c @@ -1257,7 +1257,7 @@ static length_t choose_initial_maxmtu(node_t *n) { if(connect(sock, &sa->sa, SALEN(sa->sa))) { logger(DEBUG_TRAFFIC, LOG_ERR, "Connecting MTU assessment socket for %s (%s) failed: %s", n->name, n->hostname, sockstrerror(sockerrno)); - close(sock); + closesocket(sock); return MTU; } @@ -1266,11 +1266,11 @@ static length_t choose_initial_maxmtu(node_t *n) { if(getsockopt(sock, IPPROTO_IP, IP_MTU, (void *)&ip_mtu, &ip_mtu_len)) { logger(DEBUG_TRAFFIC, LOG_ERR, "getsockopt(IP_MTU) on %s (%s) failed: %s", n->name, n->hostname, sockstrerror(sockerrno)); - close(sock); + closesocket(sock); return MTU; } - close(sock); + closesocket(sock); if(ip_mtu < MINMTU) { logger(DEBUG_TRAFFIC, LOG_ERR, "getsockopt(IP_MTU) on %s (%s) returned absurdly small value: %d", n->name, n->hostname, ip_mtu); diff --git a/src/net_setup.c b/src/net_setup.c index 43895dda..60fc692d 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -606,7 +606,7 @@ static bool add_listen_address(char *address, bool bindto) { int udp_fd = setup_vpn_in_socket((sockaddr_t *) aip->ai_addr); if(udp_fd < 0) { - close(tcp_fd); + closesocket(tcp_fd); continue; } @@ -1180,8 +1180,8 @@ void close_network_connections(void) { for(int i = 0; i < listen_sockets; i++) { io_del(&listen_socket[i].tcp); io_del(&listen_socket[i].udp); - close(listen_socket[i].tcp.fd); - close(listen_socket[i].udp.fd); + closesocket(listen_socket[i].tcp.fd); + closesocket(listen_socket[i].udp.fd); } exit_requests(); diff --git a/src/route.c b/src/route.c index 25f2be5e..6f1ffd4c 100644 --- a/src/route.c +++ b/src/route.c @@ -164,7 +164,7 @@ static void route_ipv4_unreachable(node_t *source, vpn_packet_t *packet, length_ } } - close(sockfd); + closesocket(sockfd); } } @@ -269,7 +269,7 @@ static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, length_ } } - close(sockfd); + closesocket(sockfd); } } diff --git a/src/tincctl.c b/src/tincctl.c index c700f947..5ae76d31 100644 --- a/src/tincctl.c +++ b/src/tincctl.c @@ -644,7 +644,7 @@ static bool stop_tincd(void) { // wait for tincd to close the connection... } - close(fd); + closesocket(fd); pid = 0; fd = -1; @@ -717,7 +717,7 @@ bool connect_tincd(bool verbose) { if(select(fd + 1, &r, NULL, NULL, &tv)) { fprintf(stderr, "Previous connection to tincd lost, reconnecting.\n"); - close(fd); + closesocket(fd); fd = -1; } else { return true; @@ -784,7 +784,7 @@ bool connect_tincd(bool verbose) { fprintf(stderr, "Cannot connect to UNIX socket %s: %s\n", unixsocketname, sockstrerror(sockerrno)); } - close(fd); + closesocket(fd); fd = -1; return false; } @@ -830,7 +830,7 @@ bool connect_tincd(bool verbose) { fprintf(stderr, "Cannot connect to %s port %s: %s\n", host, port, sockstrerror(sockerrno)); } - close(fd); + closesocket(fd); fd = -1; return false; } @@ -853,7 +853,7 @@ bool connect_tincd(bool verbose) { fprintf(stderr, "Cannot read greeting from control socket: %s\n", sockstrerror(sockerrno)); } - close(fd); + closesocket(fd); fd = -1; return false; } @@ -863,7 +863,7 @@ bool connect_tincd(bool verbose) { fprintf(stderr, "Could not fully establish control socket connection\n"); } - close(fd); + closesocket(fd); fd = -1; return false; } @@ -1528,7 +1528,7 @@ static int cmd_log(int argc, char *argv[]) { signal(SIGINT, SIG_DFL); #endif - close(fd); + closesocket(fd); fd = -1; return 0; } @@ -2623,7 +2623,7 @@ static int switch_network(char *name) { } if(fd >= 0) { - close(fd); + closesocket(fd); fd = -1; } -- 2.20.1