Native Windows support.
authorGuus Sliepen <guus@tinc-vpn.org>
Tue, 29 Jul 2003 22:59:01 +0000 (22:59 +0000)
committerGuus Sliepen <guus@tinc-vpn.org>
Tue, 29 Jul 2003 22:59:01 +0000 (22:59 +0000)
13 files changed:
lib/dropin.c
lib/dropin.h
src/logger.c
src/meta.c
src/mingw/device.c
src/net.c
src/net.h
src/net_setup.c
src/net_socket.c
src/netutl.c
src/protocol.h
src/tincd.c
system.h

index c92c0b81ebb65d9c39fe8dc3c51021e157c56b48..d47560100bc7fc6729656b1e78e45d17c0b96807 100644 (file)
@@ -17,7 +17,7 @@
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id: dropin.c,v 1.1.2.17 2003/07/28 22:06:09 guus Exp $
+    $Id: dropin.c,v 1.1.2.18 2003/07/29 22:59:00 guus Exp $
 */
 
 #include "system.h"
 */
 
 #include "system.h"
@@ -139,7 +139,7 @@ int asprintf(char **buf, const char *fmt, ...)
        va_end(ap);
 
        if(status >= 0)
        va_end(ap);
 
        if(status >= 0)
-               *buf = xrealloc(*buf, status);
+               *buf = xrealloc(*buf, status + 1);
 
        if(status > len - 1) {
                len = status;
 
        if(status > len - 1) {
                len = status;
@@ -159,3 +159,15 @@ int gettimeofday(struct timeval *tv, void *tz) {
        return 0;
 }
 #endif
        return 0;
 }
 #endif
+
+#ifndef HAVE_RANDOM
+#include <openssl/rand.h>
+
+long int random(void) {
+       long int x;
+       
+       RAND_pseudo_bytes((unsigned char *)&x, sizeof(x));
+
+       return x;
+}
+#endif
index 3afa69ccc60fb71f9eed5832c4d8dac7dd7dd6ea..a3afa719a35ba13ff5d1b573a9a7a55a67134154 100644 (file)
@@ -17,7 +17,7 @@
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id: dropin.h,v 1.1.2.13 2003/07/18 13:42:35 guus Exp $
+    $Id: dropin.h,v 1.1.2.14 2003/07/29 22:59:00 guus Exp $
 */
 
 #ifndef __DROPIN_H__
 */
 
 #ifndef __DROPIN_H__
@@ -43,4 +43,12 @@ extern int getnameinfo(const struct sockaddr *sa, size_t salen, char *host,
                                           size_t hostlen, char *serv, size_t servlen, int flags);
 #endif
 
                                           size_t hostlen, char *serv, size_t servlen, int flags);
 #endif
 
+#ifndef HAVE_GETTIMEOFDAY
+extern int gettimeofday(struct timeval *, void *);
+#endif
+
+#ifndef HAVE_RANDOM
+extern long int random(void);
+#endif
+
 #endif                                                 /* __DROPIN_H__ */
 #endif                                                 /* __DROPIN_H__ */
index 871f2c14d25aaf7efb8df78acf9827e75505af13..f85bd152d23301a0ad7440a541824c1d67b76d9b 100644 (file)
@@ -17,7 +17,7 @@
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id: logger.c,v 1.1.2.7 2003/07/29 10:50:15 guus Exp $
+    $Id: logger.c,v 1.1.2.8 2003/07/29 22:59:00 guus Exp $
 */
 
 #include "system.h"
 */
 
 #include "system.h"
@@ -65,6 +65,7 @@ void logger(int priority, const char *format, ...) {
                case LOGMODE_STDERR:
                        vfprintf(stderr, format, ap);
                        fprintf(stderr, "\n");
                case LOGMODE_STDERR:
                        vfprintf(stderr, format, ap);
                        fprintf(stderr, "\n");
+                       fflush(stderr);
                        break;
                case LOGMODE_FILE:
                        fprintf(logfile, "%ld %s[%d]: ", time(NULL), logident, logpid);
                        break;
                case LOGMODE_FILE:
                        fprintf(logfile, "%ld %s[%d]: ", time(NULL), logident, logpid);
index e818b0b4b6b3a48e247e8899c040bff3e2ad504f..0d103c03581ede781d7ff77dd02e432e79a9e506 100644 (file)
@@ -17,7 +17,7 @@
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id: meta.c,v 1.1.2.37 2003/07/22 20:55:19 guus Exp $
+    $Id: meta.c,v 1.1.2.38 2003/07/29 22:59:00 guus Exp $
 */
 
 #include "system.h"
 */
 
 #include "system.h"
@@ -52,7 +52,7 @@ bool send_meta(connection_t *c, char *buffer, int length)
                bufp = buffer;
 
        while(length) {
                bufp = buffer;
 
        while(length) {
-               result = write(c->socket, bufp, length);
+               result = send(c->socket, bufp, length, 0);
                if(result <= 0) {
                        if(errno == EINTR)
                                continue;
                if(result <= 0) {
                        if(errno == EINTR)
                                continue;
@@ -114,7 +114,7 @@ bool receive_meta(connection_t *c)
           - If not, keep stuff in buffer and exit.
         */
 
           - If not, keep stuff in buffer and exit.
         */
 
-       lenin = read(c->socket, c->buffer + c->buflen, MAXBUFSIZE - c->buflen);
+       lenin = recv(c->socket, c->buffer + c->buflen, MAXBUFSIZE - c->buflen, 0);
 
        if(lenin <= 0) {
                if(lenin == 0) {
 
        if(lenin <= 0) {
                if(lenin == 0) {
index d005a63b1faae1faeeb841600c899a1832a62ed7..dfda08951f01de73ab8b47828004042a47f19771 100644 (file)
@@ -17,7 +17,7 @@
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id: device.c,v 1.1.2.5 2003/07/29 12:38:49 guus Exp $
+    $Id: device.c,v 1.1.2.6 2003/07/29 22:59:01 guus Exp $
 */
 
 #include "system.h"
 */
 
 #include "system.h"
@@ -51,7 +51,8 @@
 /* FIXME: This only works for Windows 2000 */
 #define OSTYPE 5
 
 /* FIXME: This only works for Windows 2000 */
 #define OSTYPE 5
 
-HANDLE device_fd = INVALID_HANDLE_VALUE;
+int device_fd = 0;
+HANDLE device_handle = INVALID_HANDLE_VALUE;
 char *device = NULL;
 char *iface = NULL;
 char *device_info = NULL;
 char *device = NULL;
 char *iface = NULL;
 char *device_info = NULL;
@@ -59,6 +60,72 @@ char *device_info = NULL;
 int device_total_in = 0;
 int device_total_out = 0;
 
 int device_total_in = 0;
 int device_total_out = 0;
 
+DWORD WINAPI tapreader(void *bla) {
+       int sock, err, status;
+       struct addrinfo *ai;
+       struct addrinfo hint = {
+               .ai_family = AF_UNSPEC,
+               .ai_socktype = SOCK_DGRAM,
+               .ai_protocol = IPPROTO_UDP,
+               .ai_flags = 0,
+       };
+       char buf[MTU];
+       long len;
+       OVERLAPPED overlapped;
+
+       /* Open a socket to the parent process */
+
+       err = getaddrinfo(NULL, "12345", &hint, &ai);
+
+       if(err || !ai) {
+               logger(LOG_ERR, _("System call `%s' failed: %s"), "getaddrinfo", gai_strerror(errno));
+               return -1;
+       }
+
+       sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
+
+       freeaddrinfo(ai);
+
+       if(sock < 0) {
+               logger(LOG_ERR, _("System call `%s' failed: %s"), "socket", strerror(errno));
+               return -1;
+       }
+
+       if(connect(sock, ai->ai_addr, ai->ai_addrlen)) {
+               logger(LOG_ERR, _("System call `%s' failed: %s"), "connect", strerror(errno));
+               return -1;
+       }
+
+       logger(LOG_DEBUG, _("Tap reader running"));
+
+       /* Read from tap device and send to parent */
+
+       overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
+       
+       for(;;) {
+               overlapped.Offset = 0;
+               overlapped.OffsetHigh = 0;
+               ResetEvent(overlapped.hEvent);
+
+               status = ReadFile(device_handle, buf, sizeof(buf), &len, &overlapped);
+
+               if(!status) {
+                       if(GetLastError() == ERROR_IO_PENDING) {
+                               WaitForSingleObject(overlapped.hEvent, INFINITE);
+                               if(!GetOverlappedResult(device_handle, &overlapped, &len, FALSE))
+                                       continue;
+                       } else {
+                               logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
+                                          device, strerror(errno));
+                               return -1;
+                       }
+               }
+
+               if(send(sock, buf, len, 0) <= 0)
+                       return -1;
+       }
+}
+
 bool setup_device(void)
 {
        HKEY key, key2;
 bool setup_device(void)
 {
        HKEY key, key2;
@@ -68,11 +135,21 @@ bool setup_device(void)
        char adapterid[1024];
        char adaptername[1024];
        char tapname[1024];
        char adapterid[1024];
        char adaptername[1024];
        char tapname[1024];
-       char gelukt = 0;
        long len;
 
        bool found = false;
 
        long len;
 
        bool found = false;
 
+       int sock, err;
+       HANDLE thread;
+
+       struct addrinfo *ai;
+       struct addrinfo hint = {
+               .ai_family = AF_UNSPEC,
+               .ai_socktype = SOCK_DGRAM,
+               .ai_protocol = IPPROTO_UDP,
+               .ai_flags = AI_PASSIVE,
+       };
+
        cp();
 
        get_config_string(lookup_config(config_tree, "Device"), &device);
        cp();
 
        get_config_string(lookup_config(config_tree, "Device"), &device);
@@ -117,8 +194,8 @@ bool setup_device(void)
                }
 
                snprintf(tapname, sizeof(tapname), USERMODEDEVICEDIR "%s" TAPSUFFIX, adapterid);
                }
 
                snprintf(tapname, sizeof(tapname), USERMODEDEVICEDIR "%s" TAPSUFFIX, adapterid);
-               device_fd = CreateFile(tapname, GENERIC_WRITE | GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM, 0);
-               if(device_fd != INVALID_HANDLE_VALUE) {
+               device_handle = CreateFile(tapname, GENERIC_WRITE | GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, 0);
+               if(device_handle != INVALID_HANDLE_VALUE) {
                        found = true;
                        break;
                }
                        found = true;
                        break;
                }
@@ -129,24 +206,27 @@ bool setup_device(void)
                return false;
        }
 
                return false;
        }
 
-       device = adapterid;
-       iface = adaptername;
+       if(!device)
+               device = xstrdup(adapterid);
+
+       if(!iface)
+               iface = xstrdup(adaptername);
 
        /* Try to open the corresponding tap device */
 
 
        /* Try to open the corresponding tap device */
 
-       if(device_fd == INVALID_HANDLE_VALUE) {
+       if(device_handle == INVALID_HANDLE_VALUE) {
                snprintf(tapname, sizeof(tapname), USERMODEDEVICEDIR "%s" TAPSUFFIX, device);
                snprintf(tapname, sizeof(tapname), USERMODEDEVICEDIR "%s" TAPSUFFIX, device);
-               device_fd = CreateFile(tapname, GENERIC_WRITE | GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM, 0);
+               device_handle = CreateFile(tapname, GENERIC_WRITE | GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, 0);
        }
        
        }
        
-       if(device_fd == INVALID_HANDLE_VALUE) {
+       if(device_handle == INVALID_HANDLE_VALUE) {
                logger(LOG_ERR, _("%s (%s) is no a usable Windows tap device!"), device, iface);
                return false;
        }
 
        /* Get MAC address from tap device */
 
                logger(LOG_ERR, _("%s (%s) is no a usable Windows tap device!"), device, iface);
                return false;
        }
 
        /* Get MAC address from tap device */
 
-       if(!DeviceIoControl(device_fd, TAP_IOCTL_GET_MAC, mymac.x, sizeof(mymac.x), mymac.x, sizeof(mymac.x), &len, 0)) {
+       if(!DeviceIoControl(device_handle, TAP_IOCTL_GET_MAC, mymac.x, sizeof(mymac.x), mymac.x, sizeof(mymac.x), &len, 0)) {
                logger(LOG_ERR, _("Could not get MAC address from Windows tap device!"));
                return false;
        }
                logger(LOG_ERR, _("Could not get MAC address from Windows tap device!"));
                return false;
        }
@@ -155,6 +235,52 @@ bool setup_device(void)
                overwrite_mac = 1;
        }
 
                overwrite_mac = 1;
        }
 
+       /* Create a listening socket */
+
+       err = getaddrinfo(NULL, "12345", &hint, &ai);
+
+       if(err || !ai) {
+               logger(LOG_ERR, _("System call `%s' failed: %s"), "getaddrinfo", gai_strerror(errno));
+               return false;
+       }
+
+       sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
+
+       if(sock < 0) {
+               logger(LOG_ERR, _("System call `%s' failed: %s"), "socket", strerror(errno));
+               return false;
+       }
+
+       if(bind(sock, ai->ai_addr, ai->ai_addrlen)) {
+               logger(LOG_ERR, _("System call `%s' failed: %s"), "bind", strerror(errno));
+               return false;
+       }
+
+       freeaddrinfo(ai);
+
+       if(listen(sock, 1)) {
+               logger(LOG_ERR, _("System call `%s' failed: %s"), "listen", strerror(errno));
+               return false;
+       }
+
+       /* Start the tap reader */
+
+       thread = CreateThread(NULL, 0, tapreader, NULL, 0, NULL);
+
+       if(!thread) {
+               logger(LOG_ERR, _("System call `%s' failed: %s"), "CreateThread", strerror(errno));
+               return false;
+       }
+
+       /* Wait for the tap reader to connect back to us */
+
+       if((device_fd = accept(sock, NULL, 0)) == -1) {
+               logger(LOG_ERR, _("System call `%s' failed: %s"), "accept", strerror(errno));
+               return false;
+       }
+
+       closesocket(sock);
+
        device_info = _("Windows tap device");
 
        logger(LOG_INFO, _("%s (%s) is a %s"), device, iface, device_info);
        device_info = _("Windows tap device");
 
        logger(LOG_INFO, _("%s (%s) is a %s"), device, iface, device_info);
@@ -166,16 +292,16 @@ void close_device(void)
 {
        cp();
 
 {
        cp();
 
-       CloseHandle(device_fd);
+       CloseHandle(device_handle);
 }
 
 bool read_packet(vpn_packet_t *packet)
 {
 }
 
 bool read_packet(vpn_packet_t *packet)
 {
-       long lenin;
+       int lenin;
 
        cp();
 
 
        cp();
 
-       if(!ReadFile(device_fd, packet->data, MTU, &lenin, NULL)) {
+       if((lenin = recv(device_fd, packet->data, MTU, 0)) <= 0) {
                logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
                           device, strerror(errno));
                return false;
                logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
                           device, strerror(errno));
                return false;
@@ -194,13 +320,14 @@ bool read_packet(vpn_packet_t *packet)
 bool write_packet(vpn_packet_t *packet)
 {
        long lenout;
 bool write_packet(vpn_packet_t *packet)
 {
        long lenout;
+       OVERLAPPED overlapped = {0};
 
        cp();
 
        ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Writing packet of %d bytes to %s"),
                           packet->len, device_info);
 
 
        cp();
 
        ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Writing packet of %d bytes to %s"),
                           packet->len, device_info);
 
-       if(!WriteFile(device_fd, packet->data, packet->len, &lenout, NULL)) {
+       if(!WriteFile(device_handle, packet->data, packet->len, &lenout, &overlapped)) {
                logger(LOG_ERR, _("Error while writing to %s %s"), device_info, device);
                return false;
        }
                logger(LOG_ERR, _("Error while writing to %s %s"), device_info, device);
                return false;
        }
index c4637dabf1633585e06fde49dcaaeb87a069391b..e1580e651ad4d202d1769112ee8a0b05a1c22302 100644 (file)
--- a/src/net.c
+++ b/src/net.c
@@ -17,7 +17,7 @@
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id: net.c,v 1.35.4.194 2003/07/29 10:50:15 guus Exp $
+    $Id: net.c,v 1.35.4.195 2003/07/29 22:59:00 guus Exp $
 */
 
 #include "system.h"
 */
 
 #include "system.h"
@@ -39,6 +39,7 @@
 #include "protocol.h"
 #include "route.h"
 #include "subnet.h"
 #include "protocol.h"
 #include "route.h"
 #include "subnet.h"
+#include "xalloc.h"
 
 bool do_purge = false;
 
 
 bool do_purge = false;
 
@@ -153,7 +154,7 @@ void terminate_connection(connection_t *c, bool report)
                c->node->connection = NULL;
 
        if(c->socket)
                c->node->connection = NULL;
 
        if(c->socket)
-               close(c->socket);
+               closesocket(c->socket);
 
        if(c->edge) {
                if(report)
 
        if(c->edge) {
                if(report)
@@ -254,7 +255,7 @@ static void check_network_activity(fd_set * f)
                                        ifdebug(CONNECTIONS) logger(LOG_DEBUG,
                                                           _("Error while connecting to %s (%s): %s"),
                                                           c->name, c->hostname, strerror(result));
                                        ifdebug(CONNECTIONS) logger(LOG_DEBUG,
                                                           _("Error while connecting to %s (%s): %s"),
                                                           c->name, c->hostname, strerror(result));
-                                       close(c->socket);
+                                       closesocket(c->socket);
                                        do_outgoing_connection(c);
                                        continue;
                                }
                                        do_outgoing_connection(c);
                                        continue;
                                }
index 8ab33615c7e1a9bbc9334072be947b2e07a37fb4..f1494666d3ac8f4e2f946beab0e1cd8152d1d0f0 100644 (file)
--- a/src/net.h
+++ b/src/net.h
@@ -17,7 +17,7 @@
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id: net.h,v 1.9.4.67 2003/07/24 12:08:15 guus Exp $
+    $Id: net.h,v 1.9.4.68 2003/07/29 22:59:00 guus Exp $
 */
 
 #ifndef __TINC_NET_H__
 */
 
 #ifndef __TINC_NET_H__
@@ -140,4 +140,8 @@ extern void terminate_connection(struct connection_t *, bool);
 extern void flush_queue(struct node_t *);
 extern bool read_rsa_public_key(struct connection_t *);
 
 extern void flush_queue(struct node_t *);
 extern bool read_rsa_public_key(struct connection_t *);
 
+#ifndef HAVE_MINGW
+#define closesocket(s) close(s)
+#endif
+
 #endif                                                 /* __TINC_NET_H__ */
 #endif                                                 /* __TINC_NET_H__ */
index 6f7f70adbe24a58e8fdebc96fef2121f5d557f0c..3695e18e6aedcba572a4c0b337726304ad886f24 100644 (file)
@@ -17,7 +17,7 @@
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id: net_setup.c,v 1.1.2.39 2003/07/23 22:17:31 guus Exp $
+    $Id: net_setup.c,v 1.1.2.40 2003/07/29 22:59:00 guus Exp $
 */
 
 #include "system.h"
 */
 
 #include "system.h"
@@ -444,14 +444,14 @@ bool setup_myself(void)
 
        /* Open sockets */
 
 
        /* Open sockets */
 
-       memset(&hint, 0, sizeof(hint));
-
        get_config_string(lookup_config(config_tree, "BindToAddress"), &address);
 
        get_config_string(lookup_config(config_tree, "BindToAddress"), &address);
 
-       hint.ai_family = addressfamily;
-       hint.ai_socktype = SOCK_STREAM;
-       hint.ai_protocol = IPPROTO_TCP;
-       hint.ai_flags = AI_PASSIVE;
+       hint = (struct addrinfo) {
+               .ai_family = addressfamily,
+               .ai_socktype = SOCK_STREAM,
+               .ai_protocol = IPPROTO_TCP,
+               .ai_flags = AI_PASSIVE,
+       };
 
        err = getaddrinfo(address, myport, &hint, &ai);
 
 
        err = getaddrinfo(address, myport, &hint, &ai);
 
index 5f9e2173e749355b88ed7ec0d523cca1adcda08d..78e1ad2c2703f945260f517af236551fa70f3a95 100644 (file)
@@ -17,7 +17,7 @@
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id: net_socket.c,v 1.1.2.32 2003/07/28 22:06:09 guus Exp $
+    $Id: net_socket.c,v 1.1.2.33 2003/07/29 22:59:00 guus Exp $
 */
 
 #include "system.h"
 */
 
 #include "system.h"
@@ -70,7 +70,7 @@ int setup_listen_socket(const sockaddr_t *sa)
        flags = fcntl(nfd, F_GETFL);
 
        if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0) {
        flags = fcntl(nfd, F_GETFL);
 
        if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0) {
-               close(nfd);
+               closesocket(nfd);
                logger(LOG_ERR, _("System call `%s' failed: %s"), "fcntl",
                           strerror(errno));
                return -1;
                logger(LOG_ERR, _("System call `%s' failed: %s"), "fcntl",
                           strerror(errno));
                return -1;
@@ -98,7 +98,7 @@ int setup_listen_socket(const sockaddr_t *sa)
                strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
 
                if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr))) {
                strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
 
                if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr))) {
-                       close(nfd);
+                       closesocket(nfd);
                        logger(LOG_ERR, _("Can't bind to interface %s: %s"), iface,
                                   strerror(errno));
                        return -1;
                        logger(LOG_ERR, _("Can't bind to interface %s: %s"), iface,
                                   strerror(errno));
                        return -1;
@@ -109,7 +109,7 @@ int setup_listen_socket(const sockaddr_t *sa)
        }
 
        if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
        }
 
        if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
-               close(nfd);
+               closesocket(nfd);
                addrstr = sockaddr2hostname(sa);
                logger(LOG_ERR, _("Can't bind to %s/tcp: %s"), addrstr,
                           strerror(errno));
                addrstr = sockaddr2hostname(sa);
                logger(LOG_ERR, _("Can't bind to %s/tcp: %s"), addrstr,
                           strerror(errno));
@@ -118,7 +118,7 @@ int setup_listen_socket(const sockaddr_t *sa)
        }
 
        if(listen(nfd, 3)) {
        }
 
        if(listen(nfd, 3)) {
-               close(nfd);
+               closesocket(nfd);
                logger(LOG_ERR, _("System call `%s' failed: %s"), "listen",
                           strerror(errno));
                return -1;
                logger(LOG_ERR, _("System call `%s' failed: %s"), "listen",
                           strerror(errno));
                return -1;
@@ -149,7 +149,7 @@ int setup_vpn_in_socket(const sockaddr_t *sa)
 #ifdef O_NONBLOCK
        flags = fcntl(nfd, F_GETFL);
        if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0) {
 #ifdef O_NONBLOCK
        flags = fcntl(nfd, F_GETFL);
        if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0) {
-               close(nfd);
+               closesocket(nfd);
                logger(LOG_ERR, _("System call `%s' failed: %s"), "fcntl",
                           strerror(errno));
                return -1;
                logger(LOG_ERR, _("System call `%s' failed: %s"), "fcntl",
                           strerror(errno));
                return -1;
@@ -166,7 +166,7 @@ int setup_vpn_in_socket(const sockaddr_t *sa)
                strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
 
                if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr))) {
                strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
 
                if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr))) {
-                       close(nfd);
+                       closesocket(nfd);
                        logger(LOG_ERR, _("Can't bind to interface %s: %s"), iface,
                                   strerror(errno));
                        return -1;
                        logger(LOG_ERR, _("Can't bind to interface %s: %s"), iface,
                                   strerror(errno));
                        return -1;
@@ -175,7 +175,7 @@ int setup_vpn_in_socket(const sockaddr_t *sa)
 #endif
 
        if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
 #endif
 
        if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
-               close(nfd);
+               closesocket(nfd);
                addrstr = sockaddr2hostname(sa);
                logger(LOG_ERR, _("Can't bind to %s/udp: %s"), addrstr,
                           strerror(errno));
                addrstr = sockaddr2hostname(sa);
                logger(LOG_ERR, _("Can't bind to %s/udp: %s"), addrstr,
                           strerror(errno));
@@ -308,7 +308,7 @@ begin:
                        return;
                }
 
                        return;
                }
 
-               close(c->socket);
+               closesocket(c->socket);
 
                ifdebug(CONNECTIONS) logger(LOG_ERR, _("%s: %s"), c->hostname, strerror(errno));
 
 
                ifdebug(CONNECTIONS) logger(LOG_ERR, _("%s: %s"), c->hostname, strerror(errno));
 
index cd50792feafed8502b4c36ca9ce127419c80e452..1ec6e5a82199548254b3f3c3813082ef7b47e544 100644 (file)
@@ -17,7 +17,7 @@
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id: netutl.c,v 1.12.4.49 2003/07/24 12:08:15 guus Exp $
+    $Id: netutl.c,v 1.12.4.50 2003/07/29 22:59:00 guus Exp $
 */
 
 #include "system.h"
 */
 
 #include "system.h"
@@ -36,16 +36,15 @@ bool hostnames = false;
 */
 struct addrinfo *str2addrinfo(const char *address, const char *service, int socktype)
 {
 */
 struct addrinfo *str2addrinfo(const char *address, const char *service, int socktype)
 {
-       struct addrinfo hint, *ai;
+       struct addrinfo *ai;
+       struct addrinfo hint = {
+               .ai_family = addressfamily,
+               .ai_socktype = socktype,
+       };
        int err;
 
        cp();
 
        int err;
 
        cp();
 
-       memset(&hint, 0, sizeof(hint));
-
-       hint.ai_family = addressfamily;
-       hint.ai_socktype = socktype;
-
        err = getaddrinfo(address, service, &hint, &ai);
 
        if(err) {
        err = getaddrinfo(address, service, &hint, &ai);
 
        if(err) {
@@ -59,18 +58,17 @@ struct addrinfo *str2addrinfo(const char *address, const char *service, int sock
 
 sockaddr_t str2sockaddr(const char *address, const char *port)
 {
 
 sockaddr_t str2sockaddr(const char *address, const char *port)
 {
-       struct addrinfo hint, *ai;
+       struct addrinfo *ai;
+       struct addrinfo hint = {
+               .ai_family = AF_UNSPEC,
+               .ai_flags = AI_NUMERICHOST,
+               .ai_socktype = SOCK_STREAM,
+       };
        sockaddr_t result;
        int err;
 
        cp();
 
        sockaddr_t result;
        int err;
 
        cp();
 
-       memset(&hint, 0, sizeof(hint));
-
-       hint.ai_family = AF_UNSPEC;
-       hint.ai_flags = AI_NUMERICHOST;
-       hint.ai_socktype = SOCK_STREAM;
-
        err = getaddrinfo(address, port, &hint, &ai);
 
        if(err || !ai) {
        err = getaddrinfo(address, port, &hint, &ai);
 
        if(err || !ai) {
index 0eae830d187b2a30b059b03cdad4bd06416efcbb..7a32a17abd75aab218727051d166ce1ff84ecbbe 100644 (file)
@@ -17,7 +17,7 @@
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id: protocol.h,v 1.5.4.42 2003/07/29 10:50:15 guus Exp $
+    $Id: protocol.h,v 1.5.4.43 2003/07/29 22:59:00 guus Exp $
 */
 
 #ifndef __TINC_PROTOCOL_H__
 */
 
 #ifndef __TINC_PROTOCOL_H__
 
 #define PROT_CURRENT 17
 
 
 #define PROT_CURRENT 17
 
+/* Silly Windows */
+
+#ifdef ERROR
+#undef ERROR
+#endif
+
 /* Request numbers */
 
 typedef enum request_t {
 /* Request numbers */
 
 typedef enum request_t {
index d2179ab1cdaa440bdbfeb32792471fea1c6e1f9c..6b90de761c413c35e3800aee4f7e2045bd34a973 100644 (file)
@@ -17,7 +17,7 @@
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id: tincd.c,v 1.10.4.77 2003/07/28 22:06:09 guus Exp $
+    $Id: tincd.c,v 1.10.4.78 2003/07/29 22:59:00 guus Exp $
 */
 
 #include "system.h"
 */
 
 #include "system.h"
@@ -93,6 +93,10 @@ static struct option const long_options[] = {
        {NULL, 0, NULL, 0}
 };
 
        {NULL, 0, NULL, 0}
 };
 
+#ifdef HAVE_MINGW
+static struct WSAData wsa_state;
+#endif
+
 static void usage(bool status)
 {
        if(status)
 static void usage(bool status)
 {
        if(status)
@@ -417,6 +421,13 @@ int main(int argc, char **argv, char **envp)
                exit(1);
        }
 
                exit(1);
        }
 
+#ifdef HAVE_MINGW
+       if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) {
+               logger(LOG_ERR, _("System call `%s' failed: %s"), "WSAStartup", strerror(errno));
+               exit(1);
+       }
+#endif
+       
        if(!detach())
                exit(1);
                
        if(!detach())
                exit(1);
                
index d4381dc5104ec82a845d6e463dc8b669380a00dd..73f7aeb1cd558ce3c35f94fb8c41f7a3e0e18657 100644 (file)
--- a/system.h
+++ b/system.h
 
 #ifdef HAVE_MINGW
 #include <windows.h>
 
 #ifdef HAVE_MINGW
 #include <windows.h>
-#include <winsock.h>
+#include <winsock2.h>
 #endif
 
 /* Include localisation support */
 #endif
 
 /* Include localisation support */