Correct close() function when building with MSVC
authorKirill Isakov <bootctl@gmail.com>
Sat, 2 Apr 2022 11:41:50 +0000 (17:41 +0600)
committerKirill Isakov <bootctl@gmail.com>
Sun, 10 Apr 2022 07:14:26 +0000 (13:14 +0600)
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
src/net_packet.c
src/net_setup.c
src/route.c
src/tincctl.c

index da10cd9..384b4db 100644 (file)
@@ -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;
index 5bdcb1a..d171fec 100644 (file)
@@ -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);
index 43895dd..60fc692 100644 (file)
@@ -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();
index 25f2be5..6f1ffd4 100644 (file)
@@ -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);
                }
        }
 
index c700f94..5ae76d3 100644 (file)
@@ -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;
        }