diff options
author | rmax <rmax> | 2010-09-28 15:13:54 (GMT) |
---|---|---|
committer | rmax <rmax> | 2010-09-28 15:13:54 (GMT) |
commit | 8de390107eb243b132d238c82d5dad142732ea6f (patch) | |
tree | 3c49ea8f056c9653ddd2cfed43ac4615cba322bb /generic/tclIOSock.c | |
parent | 76ae3756ac54d0957e5d6c430aec55b52ccc0bf3 (diff) | |
download | tcl-8de390107eb243b132d238c82d5dad142732ea6f.zip tcl-8de390107eb243b132d238c82d5dad142732ea6f.tar.gz tcl-8de390107eb243b132d238c82d5dad142732ea6f.tar.bz2 |
* doc/socket.n: Document the changes to the [socket] and
[fconfiguyre] commands.
* generic/tclInt.h: Introduce TclCreateSocketAddress() as a
* generic/tclIOSock.c: replacement for the platform-dependent
* unix/tclUnixSock.c: TclpCreateSocketAddress() functions.
* unix/tclUnixChan.c: Extend the [socket] and [fconfigure]
* unix/tclUnixPort.h: commands to behave as proposed in
* win/tclWinSock.c: TIP #162.
* win/tclWinPort.h:
* compat/fake-rfc2553.c: A compat implementation of the APIs
* compat/fake-rfc2553.h: defined in RFC-2553 (getaddrinfo() and
friends) on top of the existing
gethostbyname() etc.
* unix/configure.in: Test whether the fake-implementation is
* unix/tcl.m4: needed.
* unix/Makefile.in: Add a compile target for fake-rfc2553.
* win/configure.in: Allow cross-compilation by default
* tests/socket.test: Improve the test suite to make more use of
* tests/remote.tcl: randomized ports to reduce interference with
tests running in parallel or other services
on the machine.
Diffstat (limited to 'generic/tclIOSock.c')
-rw-r--r-- | generic/tclIOSock.c | 147 |
1 files changed, 146 insertions, 1 deletions
diff --git a/generic/tclIOSock.c b/generic/tclIOSock.c index 6887b0c..15de01e 100644 --- a/generic/tclIOSock.c +++ b/generic/tclIOSock.c @@ -8,7 +8,7 @@ * See the file "license.terms" for information on usage and redistribution of * this file, and for a DISCLAIMER OF ALL WARRANTIES. * - * RCS: @(#) $Id: tclIOSock.c,v 1.11 2007/02/20 23:24:04 nijtmans Exp $ + * RCS: @(#) $Id: tclIOSock.c,v 1.12 2010/09/28 15:13:54 rmax Exp $ */ #include "tclInt.h" @@ -107,6 +107,151 @@ TclSockMinimumBuffers( } /* + *---------------------------------------------------------------------- + * + * TclCreateSocketAddress -- + * + * This function initializes a sockaddr structure for a host and port. + * + * Results: + * 1 if the host was valid, 0 if the host could not be converted to an IP + * address. + * + * Side effects: + * Fills in the *sockaddrPtr structure. + * + *---------------------------------------------------------------------- + */ + +int +TclCreateSocketAddress( + Tcl_Interp *interp, /* Interpreter for querying + * the desired socket family */ + struct addrinfo **addrlist, /* Socket address list */ + const char *host, /* Host. NULL implies INADDR_ANY */ + int port, /* Port number */ + int willBind, /* Is this an address to bind() to or + * to connect() to? */ + const char **errorMsgPtr) /* Place to store the error message + * detail, if available. */ +{ + struct addrinfo hints; + struct addrinfo *p; + struct addrinfo *v4head = NULL, *v4ptr = NULL; + struct addrinfo *v6head = NULL, *v6ptr = NULL; + char *native = NULL, portstring[TCL_INTEGER_SPACE]; + const char *family; + Tcl_DString ds; + int result, i; + + TclFormatInt(portstring, port); + + if (host != NULL) { + native = Tcl_UtfToExternalDString(NULL, host, -1, &ds); + } + + (void) memset(&hints, 0, sizeof(hints)); + + hints.ai_family = AF_UNSPEC; + /* Magic variable to enforce a certain address family */ + family = Tcl_GetVar(interp, "::tcl::unsupported::socketAF", 0); + if (family != NULL) { + if (strcmp(family, "inet") == 0) { + hints.ai_family = AF_INET; + } else if (strcmp(family, "inet6") == 0) { + hints.ai_family = AF_INET6; + } + } + + hints.ai_socktype = SOCK_STREAM; +#if defined(AI_ADDRCONFIG) && !defined(_AIX) + /* Missing on: OpenBSD, NetBSD. Causes failure when used on AIX 5.1 */ + hints.ai_flags |= AI_ADDRCONFIG; +#endif + if (willBind) { + hints.ai_flags |= AI_PASSIVE; + } + + result = getaddrinfo(native, portstring, &hints, addrlist); + + if (host != NULL) { + Tcl_DStringFree(&ds); + } + + if (result != 0) { + goto error; + } + + /* + * Put IPv4 addresses before IPv6 addresses to maximize backwards + * compatibility of [fconfigure -sockname] output. + * + * There might be more elegant/efficient ways to do this. + */ + if (willBind) { + for (p = *addrlist; p != NULL; p = p->ai_next) { + if (p->ai_family == AF_INET) { + if (v4head == NULL) { + v4head = p; + } else { + v4ptr->ai_next = p; + } + v4ptr = p; + } else { + if (v6head == NULL) { + v6head = p; + } else { + v6ptr->ai_next = p; + } + v6ptr = p; + } + } + *addrlist = NULL; + if (v6head != NULL) { + *addrlist = v6head; + v6ptr->ai_next = NULL; + } + if (v4head != NULL) { + v4ptr->ai_next = *addrlist; + *addrlist = v4head; + } + } + i = 0; + for (p = *addrlist; p != NULL; p = p->ai_next) { + i++; + } + + return 1; + + /* + * Ought to use gai_strerror() here... + */ + +error: + switch (result) { + case EAI_NONAME: + case EAI_SERVICE: +#if defined(EAI_ADDRFAMILY) && EAI_ADDRFAMILY != EAI_NONAME + case EAI_ADDRFAMILY: +#endif +#if defined(EAI_NODATA) && EAI_NODATA != EAI_NONAME + case EAI_NODATA: +#endif + *errorMsgPtr = gai_strerror(result); + errno = EHOSTUNREACH; + return 0; +#ifdef EAI_SYSTEM + case EAI_SYSTEM: + return 0; +#endif + default: + *errorMsgPtr = gai_strerror(result); + errno = ENXIO; + return 0; + } +} + +/* * Local Variables: * mode: c * c-basic-offset: 4 |