/*
* Windows networking abstraction.
*
- * Due to this clean abstraction it was possible
- * to easily implement IPv6 support :)
- *
- * IPv6 patch 1 (27 October 2000) Jeroen Massar <jeroen@unfix.org>
- * - Preliminary hacked IPv6 support.
- * - Connecting to IPv6 address (eg fec0:4242:4242:100:2d0:b7ff:fe8f:5d42) works.
- * - Connecting to IPv6 hostname (eg heaven.ipv6.unfix.org) works.
- * - Compiles as either IPv4 or IPv6.
- *
- * IPv6 patch 2 (29 October 2000) Jeroen Massar <jeroen@unfix.org>
- * - When compiled as IPv6 it also allows connecting to IPv4 hosts.
- * - Added some more documentation.
- *
- * IPv6 patch 3 (18 November 2000) Jeroen Massar <jeroen@unfix.org>
- * - It now supports dynamically loading the IPv6 resolver dll's.
- * This way we should be able to distribute one (1) binary
- * which supports both IPv4 and IPv6.
- * - getaddrinfo() and getnameinfo() are loaded dynamicaly if possible.
- * - in6addr_any is defined in this file so we don't need to link to wship6.lib
- * - The patch is now more unified so that we can still
- * remove all IPv6 support by undef'ing IPV6.
- * But where it fallsback to IPv4 it uses the IPv4 code which is already in place...
- * - Canonical name resolving works.
- *
- * IPv6 patch 4 (07 January 2001) Jeroen Massar <jeroen@unfix.org>
- * - patch against CVS of today, will be submitted to the bugs list
- * as a 'cvs diff -u' on Simon's request...
- *
+ * For the IPv6 code in here I am indebted to Jeroen Massar and
+ * unfix.org.
*/
-/*
- * Define IPV6 to have IPv6 on-the-fly-loading support.
- * This means that one doesn't have to have an IPv6 stack to use it.
- * But if an IPv6 stack is found it is used with a fallback to IPv4.
- */
-/* #define IPV6 1 */
-
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "tree234.h"
#include <ws2tcpip.h>
-#ifdef IPV6
-#include <tpipv6.h>
+
+#ifndef NO_IPV6
+const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
+const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
#endif
#define ipv4_is_loopback(addr) \
((p_ntohl(addr.s_addr) & 0xFF000000L) == 0x7F000000L)
+/*
+ * We used to typedef struct Socket_tag *Socket.
+ *
+ * Since we have made the networking abstraction slightly more
+ * abstract, Socket no longer means a tcp socket (it could mean
+ * an ssl socket). So now we must use Actual_Socket when we know
+ * we are talking about a tcp socket.
+ */
+typedef struct Socket_tag *Actual_Socket;
+
struct Socket_tag {
const struct socket_function_table *fn;
/* the above variable absolutely *must* be the first in this structure */
int sending_oob;
int oobinline;
int pending_error; /* in case send() returns error */
+ /*
+ * We sometimes need pairs of Socket structures to be linked:
+ * if we are listening on the same IPv6 and v4 port, for
+ * example. So here we define `parent' and `child' pointers to
+ * track this link.
+ */
+ Actual_Socket parent, child;
};
-/*
- * We used to typedef struct Socket_tag *Socket.
- *
- * Since we have made the networking abstraction slightly more
- * abstract, Socket no longer means a tcp socket (it could mean
- * an ssl socket). So now we must use Actual_Socket when we know
- * we are talking about a tcp socket.
- */
-typedef struct Socket_tag *Actual_Socket;
-
struct SockAddr_tag {
char *error;
/*
* IPv4; AF_INET6 for IPv6; AF_UNSPEC indicates that name
* resolution has not been done and a simple host name is held
* in this SockAddr structure.
+ * The hostname field is also used when the hostname has both
+ * an IPv6 and IPv4 address and the IPv6 connection attempt
+ * fails. We then try the IPv4 address.
+ * This 'family' should become an option in the GUI and
+ * on the commandline for selecting a default protocol.
*/
int family;
unsigned long address; /* Address IPv4 style. */
-#ifdef IPV6
- struct addrinfo *ai; /* Address IPv6 style. */
+#ifndef NO_IPV6
+ struct addrinfo *ai; /* Address AF-independent (IPv4+IPv6) style. */
#endif
char hostname[512]; /* Store an unresolved host name. */
};
}
}
-SockAddr sk_namelookup(const char *host, char **canonicalname)
+SockAddr sk_namelookup(const char *host, char **canonicalname,
+ int address_family)
{
SockAddr ret = snew(struct SockAddr_tag);
unsigned long a;
struct hostent *h = NULL;
char realhost[8192];
+ int ret_family;
+ int err;
/* Clear the structure and default to IPv4. */
memset(ret, 0, sizeof(struct SockAddr_tag));
- ret->family = 0; /* We set this one when we have resolved the host. */
+ ret->family = (address_family == ADDRTYPE_IPV4 ? AF_INET :
+#ifndef NO_IPV6
+ address_family == ADDRTYPE_IPV6 ? AF_INET6 :
+#endif
+ AF_UNSPEC);
+ ret_family = AF_UNSPEC;
*realhost = '\0';
if ((a = p_inet_addr(host)) == (unsigned long) INADDR_NONE) {
-#ifdef IPV6
+#ifndef NO_IPV6
/* Try to get the getaddrinfo() function from wship6.dll */
/* This way one doesn't need to have IPv6 dll's to use PuTTY and
* it will fallback to IPv4. */
typedef int (CALLBACK * FGETADDRINFO) (const char *nodename,
const char *servname,
- const struct addrinfo *
- hints,
- struct addrinfo ** res);
+ const struct addrinfo *hints,
+ struct addrinfo **res);
FGETADDRINFO fGetAddrInfo = NULL;
HINSTANCE dllWSHIP6 = LoadLibrary("wship6.dll");
"getaddrinfo");
/*
- * Use fGetAddrInfo when it's available (which usually also
- * means IPv6 is installed...)
+ * Use fGetAddrInfo when it's available
*/
if (fGetAddrInfo) {
- /*debug(("Resolving \"%s\" with getaddrinfo() (IPv4+IPv6 capable)...\n", host)); */
- if (fGetAddrInfo(host, NULL, NULL, &ret->ai) == 0)
- ret->family = ret->ai->ai_family;
+ struct addrinfo hints;
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family = ret->family;
+ if ((err = fGetAddrInfo(host, NULL, &hints, &ret->ai)) == 0)
+ ret_family = ret->ai->ai_family;
} else
#endif
{
/*
* Otherwise use the IPv4-only gethostbyname...
- * (NOTE: we don't use gethostbyname as a
- * fallback!)
+ * (NOTE: we don't use gethostbyname as a fallback!)
*/
- if (ret->family == 0) {
- /*debug(("Resolving \"%s\" with gethostbyname() (IPv4 only)...\n", host)); */
- if ( (h = p_gethostbyname(host)) )
- ret->family = AF_INET;
- }
+ if ( (h = p_gethostbyname(host)) )
+ ret_family = AF_INET;
+ else
+ err = p_WSAGetLastError();
}
- /*debug(("Done resolving...(family is %d) AF_INET = %d, AF_INET6 = %d\n", ret->family, AF_INET, AF_INET6)); */
- if (ret->family == 0) {
- DWORD err = p_WSAGetLastError();
+ if (ret_family == AF_UNSPEC) {
ret->error = (err == WSAENETDOWN ? "Network is down" :
- err ==
- WSAHOST_NOT_FOUND ? "Host does not exist" : err
- == WSATRY_AGAIN ? "Host not found" :
-#ifdef IPV6
+ err == WSAHOST_NOT_FOUND ? "Host does not exist" :
+ err == WSATRY_AGAIN ? "Host not found" :
+#ifndef NO_IPV6
fGetAddrInfo ? "getaddrinfo: unknown error" :
#endif
"gethostbyname: unknown error");
-#ifdef DEBUG
- {
- LPVOID lpMsgBuf;
- FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
- FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
- (LPTSTR) & lpMsgBuf, 0, NULL);
- /*debug(("Error %ld: %s (h=%lx)\n", err, lpMsgBuf, h)); */
- /* Free the buffer. */
- LocalFree(lpMsgBuf);
- }
-#endif
} else {
ret->error = NULL;
+ ret->family = ret_family;
-#ifdef IPV6
+#ifndef NO_IPV6
/* If we got an address info use that... */
if (ret->ai) {
typedef int (CALLBACK * FGETNAMEINFO)
strncpy(realhost, h->h_name, sizeof(realhost));
}
}
-#ifdef IPV6
+#ifndef NO_IPV6
FreeLibrary(dllWSHIP6);
#endif
} else {
void sk_getaddr(SockAddr addr, char *buf, int buflen)
{
-#ifdef IPV6
+#ifndef NO_IPV6
if (addr->family == AF_INET6) {
- FIXME; /* I don't know how to get a text form of an IPv6 address. */
+ /* Try to get the WSAAddressToStringA() function from wship6.dll */
+ /* This way one doesn't need to have IPv6 dll's to use PuTTY and
+ * it will fallback to IPv4. */
+ typedef int (CALLBACK * FADDRTOSTR) (LPSOCKADDR lpsaAddress,
+ DWORD dwAddressLength,
+ LPWSAPROTOCOL_INFO lpProtocolInfo,
+ OUT LPTSTR lpszAddressString,
+ IN OUT LPDWORD lpdwAddressStringLength
+ );
+ FADDRTOSTR fAddrToStr = NULL;
+
+ HINSTANCE dllWS2 = LoadLibrary("ws2_32.dll");
+ if (dllWS2) {
+ fAddrToStr = (FADDRTOSTR)GetProcAddress(dllWS2,
+ "WSAAddressToStringA");
+ if (fAddrToStr) {
+ fAddrToStr(addr->ai->ai_addr, addr->ai->ai_addrlen,
+ NULL, buf, &buflen);
+ }
+ else strncpy(buf, "IPv6", buflen);
+ FreeLibrary(dllWS2);
+ }
} else
#endif
if (addr->family == AF_INET) {
strncpy(buf, p_inet_ntoa(a), buflen);
buf[buflen-1] = '\0';
} else {
- assert(addr->family == AF_UNSPEC);
strncpy(buf, addr->hostname, buflen);
buf[buflen-1] = '\0';
}
int sk_address_is_local(SockAddr addr)
{
-#ifdef IPV6
+#ifndef NO_IPV6
if (addr->family == AF_INET6) {
- FIXME; /* someone who can compile for IPV6 had better do this bit */
+ return IN6_IS_ADDR_LOOPBACK((const struct in6_addr *)addr->ai->ai_addr);
} else
#endif
if (addr->family == AF_INET) {
int sk_addrtype(SockAddr addr)
{
return (addr->family == AF_INET ? ADDRTYPE_IPV4 :
-#ifdef IPV6
+#ifndef NO_IPV6
addr->family == AF_INET6 ? ADDRTYPE_IPV6 :
#endif
ADDRTYPE_NAME);
void sk_addrcopy(SockAddr addr, char *buf)
{
assert(addr->family != AF_UNSPEC);
-#ifdef IPV6
+#ifndef NO_IPV6
if (addr->family == AF_INET6) {
memcpy(buf, (char*) addr->ai, 16);
} else
ret->frozen_readable = 0;
ret->localhost_only = 0; /* unused, but best init anyway */
ret->pending_error = 0;
+ ret->parent = ret->child = NULL;
ret->s = (SOCKET)sock;
};
SOCKET s;
-#ifdef IPV6
+#ifndef NO_IPV6
SOCKADDR_IN6 a6;
#endif
SOCKADDR_IN a;
ret->frozen_readable = 0;
ret->localhost_only = 0; /* unused, but best init anyway */
ret->pending_error = 0;
+ ret->parent = ret->child = NULL;
/*
* Open socket.
*/
- assert(addr->family != AF_UNSPEC);
+#ifndef NO_IPV6
+ /* Let's default to IPv6, this shouldn't hurt anybody
+ * If the stack supports IPv6 it will also allow IPv4 connections. */
+ if (addr->family == AF_UNSPEC) addr->family = AF_INET6;
+#else
+ /* No other choice, default to IPv4 */
+ if (addr->family == AF_UNSPEC) addr->family = AF_INET;
+#endif
s = p_socket(addr->family, SOCK_STREAM, 0);
ret->s = s;
while (1) {
int retcode;
-#ifdef IPV6
+#ifndef NO_IPV6
if (addr->family == AF_INET6) {
memset(&a6, 0, sizeof(a6));
a6.sin6_family = AF_INET6;
-/*a6.sin6_addr = in6addr_any; *//* == 0 */
+ /*a6.sin6_addr = in6addr_any; */ /* == 0 done by memset() */
a6.sin6_port = p_htons(localport);
} else
#endif
a.sin_addr.s_addr = p_htonl(INADDR_ANY);
a.sin_port = p_htons(localport);
}
-#ifdef IPV6
+#ifndef NO_IPV6
retcode = p_bind(s, (addr->family == AF_INET6 ?
(struct sockaddr *) &a6 :
(struct sockaddr *) &a),
/*
* Connect to remote address.
*/
-#ifdef IPV6
+#ifndef NO_IPV6
if (addr->family == AF_INET6) {
memset(&a, 0, sizeof(a));
a6.sin6_family = AF_INET6;
}
if ((
-#ifdef IPV6
+#ifndef NO_IPV6
p_connect(s, ((addr->family == AF_INET6) ?
(struct sockaddr *) &a6 : (struct sockaddr *) &a),
(addr->family == AF_INET6) ? sizeof(a6) : sizeof(a))
return (Socket) ret;
}
-Socket sk_newlistener(char *srcaddr, int port, Plug plug, int local_host_only)
+Socket sk_newlistener(char *srcaddr, int port, Plug plug, int local_host_only,
+ int orig_address_family)
{
static const struct socket_function_table fn_table = {
sk_tcp_plug,
};
SOCKET s;
-#ifdef IPV6
+#ifndef NO_IPV6
SOCKADDR_IN6 a6;
#endif
SOCKADDR_IN a;
+
DWORD err;
char *errstr;
Actual_Socket ret;
int retcode;
int on = 1;
+ int address_family;
+
/*
* Create Socket structure.
*/
ret->frozen_readable = 0;
ret->localhost_only = local_host_only;
ret->pending_error = 0;
+ ret->parent = ret->child = NULL;
+
+ /*
+ * Translate address_family from platform-independent constants
+ * into local reality.
+ */
+ address_family = (orig_address_family == ADDRTYPE_IPV4 ? AF_INET :
+#ifndef NO_IPV6
+ orig_address_family == ADDRTYPE_IPV6 ? AF_INET6 :
+#endif
+ AF_UNSPEC);
+
+ /*
+ * Our default, if passed the `don't care' value
+ * ADDRTYPE_UNSPEC, is to listen on IPv4. If IPv6 is supported,
+ * we will also set up a second socket listening on IPv6, but
+ * the v4 one is primary since that ought to work even on
+ * non-v6-supporting systems.
+ */
+ if (address_family == AF_UNSPEC) address_family = AF_INET;
/*
* Open socket.
*/
- s = p_socket(AF_INET, SOCK_STREAM, 0);
+ s = p_socket(address_family, SOCK_STREAM, 0);
ret->s = s;
if (s == INVALID_SOCKET) {
p_setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on));
-#ifdef IPV6
- if (addr->family == AF_INET6) {
+#ifndef NO_IPV6
+ if (address_family == AF_INET6) {
memset(&a6, 0, sizeof(a6));
a6.sin6_family = AF_INET6;
/* FIXME: srcaddr is ignored for IPv6, because I (SGT) don't
- * know how to do it. :-) */
+ * know how to do it. :-)
+ * (jeroen:) saddr is specified as an address.. eg 2001:db8::1
+ * Thus we need either a parser that understands [2001:db8::1]:80
+ * style addresses and/or enhance this to understand hostnames too. */
if (local_host_only)
a6.sin6_addr = in6addr_loopback;
else
a.sin_port = p_htons((short)port);
}
-#ifdef IPV6
- retcode = p_bind(s, (addr->family == AF_INET6 ?
+#ifndef NO_IPV6
+ retcode = p_bind(s, (address_family == AF_INET6 ?
(struct sockaddr *) &a6 :
(struct sockaddr *) &a),
- (addr->family ==
+ (address_family ==
AF_INET6 ? sizeof(a6) : sizeof(a)));
#else
retcode = p_bind(s, (struct sockaddr *) &a, sizeof(a));
}
if (err) {
+ p_closesocket(s);
ret->error = winsock_error_string(err);
return (Socket) ret;
}
* window, or an EventSelect on an event object. */
errstr = do_select(s, 1);
if (errstr) {
+ p_closesocket(s);
ret->error = errstr;
return (Socket) ret;
}
add234(sktree, ret);
+#ifndef NO_IPV6
+ /*
+ * If we were given ADDRTYPE_UNSPEC, we must also create an
+ * IPv6 listening socket and link it to this one.
+ */
+ if (address_family == AF_INET && orig_address_family == ADDRTYPE_UNSPEC) {
+ Actual_Socket other;
+
+ other = (Actual_Socket) sk_newlistener(srcaddr, port, plug,
+ local_host_only, ADDRTYPE_IPV6);
+
+ if (other) {
+ if (!other->error) {
+ other->parent = ret;
+ ret->child = other;
+ } else {
+ sfree(other);
+ }
+ }
+ }
+#endif
+
return (Socket) ret;
}
extern char *do_select(SOCKET skt, int startup);
Actual_Socket s = (Actual_Socket) sock;
+ if (s->child)
+ sk_tcp_close((Socket)s->child);
+
del234(sktree, s);
do_select(s->s, 0);
p_closesocket(s->s);
return open;
case FD_ACCEPT:
{
+#ifdef NO_IPV6
struct sockaddr_in isa;
- int addrlen = sizeof(struct sockaddr_in);
+#else
+ struct sockaddr_storage isa;
+#endif
+ int addrlen = sizeof(isa);
SOCKET t; /* socket of connection */
- memset(&isa, 0, sizeof(struct sockaddr_in));
+ memset(&isa, 0, sizeof(isa));
err = 0;
t = p_accept(s->s,(struct sockaddr *)&isa,&addrlen);
if (t == INVALID_SOCKET)
if (err == WSATRY_AGAIN)
break;
}
-
+#ifndef NO_IPV6
+ if (isa.ss_family == AF_INET &&
+ s->localhost_only &&
+ !ipv4_is_local_addr(((struct sockaddr_in *)&isa)->sin_addr)) {
+#else
if (s->localhost_only && !ipv4_is_local_addr(isa.sin_addr)) {
+#endif
p_closesocket(t); /* dodgy WinSock let nonlocal through */
} else if (plug_accepting(s->plug, (void*)t)) {
p_closesocket(t); /* denied or error */
return s ? s->s : INVALID_SOCKET;
}
+extern int socket_writable(SOCKET skt)
+{
+ Actual_Socket s = find234(sktree, (void *)skt, cmpforsearch);
+
+ if (s)
+ return bufchain_size(&s->output_data) > 0;
+ else
+ return 0;
+}
+
int net_service_lookup(char *service)
{
struct servent *se;