From abcfcd557336e87768808071bdb19db8596978cc Mon Sep 17 00:00:00 2001 From: Bert Belder Date: Sun, 10 Apr 2011 18:53:27 +0200 Subject: [PATCH] Implicitly bind client socket in oio_connect --- oio-win.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/oio-win.c b/oio-win.c index 1607a61b..a8c4b011 100644 --- a/oio-win.c +++ b/oio-win.c @@ -85,6 +85,7 @@ LPFN_TRANSMITFILE pTransmitFile; */ #define OIO_HANDLE_CLOSING 0x01 #define OIO_HANDLE_CLOSED 0x03 +#define OIO_HANDLE_BOUND 0x04 /* * Private oio_req flags. @@ -120,6 +121,10 @@ int oio_errno_; int oio_refs_ = 0; +/* Ip address used to bind to any port at any interface */ +struct sockaddr_in addr_ip4_any_; + + /* * Display an error message and abort the event loop. */ @@ -194,6 +199,8 @@ void oio_init() { oio_fatal_error(errorno, "WSAStartup"); } + /* Set implicit binding address used by connectEx */ + addr_ip4_any_ = oio_ip4_addr("0.0.0.0", 0); /* Retrieve the needed winsock extension function pointers. */ dummy = socket(AF_INET, SOCK_STREAM, IPPROTO_IP); @@ -383,6 +390,8 @@ int oio_bind(oio_handle* handle, struct sockaddr* addr) { return -1; } + handle->flags |= OIO_HANDLE_BOUND; + return 0; } @@ -479,8 +488,13 @@ int oio_connect(oio_req* req, struct sockaddr* addr) { if (addr->sa_family == AF_INET) { addrsize = sizeof(struct sockaddr_in); + if (!(handle->flags & OIO_HANDLE_BOUND) && + oio_bind(handle, &addr_ip4_any_) < 0) + return -1; } else if (addr->sa_family == AF_INET6) { addrsize = sizeof(struct sockaddr_in6); + assert(0); + return -1; } else { assert(0); return -1;