From b25bb33625f3c84b5d310952b00fab51403bc4bd Mon Sep 17 00:00:00 2001 From: Thomas Flueeli Date: Sun, 31 Oct 2010 13:23:45 +0100 Subject: [PATCH] fix fd.o#24775 --- src/bytestream-socks5.c | 72 ++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 68 insertions(+), 4 deletions(-) diff --git a/src/bytestream-socks5.c b/src/bytestream-socks5.c index e7ab748..2d0a171 100644 --- a/src/bytestream-socks5.c +++ b/src/bytestream-socks5.c @@ -1637,10 +1637,74 @@ socks5_init_error: static GSList * get_local_interfaces_ips (void) { - /* FIXME: fd.o#24775: please implement this using ioctlsocket() and - * SIO_GET_INTERFACE_LIST, if you care about doing SOCKS5 bytestreams on - * Windows */ - return NULL; + gint sockfd; + INTERFACE_INFO *iflist = NULL; + gsize size = 0; + int ret; + int error; + gsize bytes; + gsize num; + gsize i; + struct sockaddr_in *sa; + GSList *ips = NULL; + + /* FIXME: add IPv6 addresses */ + if ((sockfd = socket (AF_INET, SOCK_DGRAM, IPPROTO_IP)) == INVALID_SOCKET) + { + DEBUG ("Cannot open socket to retrieve interface list"); + return NULL; + } + + /* Loop and get each interface the system has, one by one... */ + do + { + size += sizeof (INTERFACE_INFO); + /* realloc buffer size until no overflow occurs */ + if (NULL == (iflist = realloc (iflist, size))) + { + DEBUG ("Out of memory while allocation interface configuration" + " structure"); + closesocket (sockfd); + return NULL; + } + + ret = WSAIoctl (sockfd, SIO_GET_INTERFACE_LIST, NULL, 0, iflist, + size, &bytes, NULL, NULL); + error = WSAGetLastError (); + + if (ret == SOCKET_ERROR && error != WSAEFAULT) + { + DEBUG ("Cannot retrieve interface list"); + closesocket (sockfd); + free (iflist); + return NULL; + } + } while (ret == SOCKET_ERROR); + + num = bytes / sizeof (INTERFACE_INFO); + + /* Loop throught the interface list and get the IP address of each IF */ + for (i = 0; i < num; i++) + { + /* no ip address from interface that is down */ + if ((iflist[i].iiFlags & IFF_UP) == 0) + continue; + + if ((iflist[i].iiFlags & IFF_LOOPBACK) == IFF_LOOPBACK) + { + DEBUG ("Ignoring loopback interface"); + continue; + } + + sa = (struct sockaddr_in *) &(iflist[i].iiAddress); + ips = g_slist_prepend (ips, g_strdup (inet_ntoa (sa->sin_addr))); + DEBUG ("IP Address: %s", inet_ntoa (sa->sin_addr)); + } + + closesocket (sockfd); + free (iflist); + + return ips; } #else -- 1.7.0.2.msysgit.0