sl@0: /* sl@0: * tclUnixSock.c -- sl@0: * sl@0: * This file contains Unix-specific socket related code. sl@0: * sl@0: * Copyright (c) 1995 Sun Microsystems, Inc. sl@0: * Portions Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiaries. All rights reserved. sl@0: * sl@0: * See the file "license.terms" for information on usage and redistribution sl@0: * of this file, and for a DISCLAIMER OF ALL WARRANTIES. sl@0: * sl@0: * RCS: @(#) $Id: tclUnixSock.c,v 1.6.2.4 2006/09/07 09:01:07 vasiljevic Exp $ sl@0: */ sl@0: sl@0: #include "tcl.h" sl@0: #include "tclPort.h" sl@0: sl@0: /* sl@0: * There is no portable macro for the maximum length sl@0: * of host names returned by gethostbyname(). We should only sl@0: * trust SYS_NMLN if it is at least 255 + 1 bytes to comply with DNS sl@0: * host name limits. sl@0: * sl@0: * Note: SYS_NMLN is a restriction on "uname" not on gethostbyname! sl@0: * sl@0: * For example HP-UX 10.20 has SYS_NMLN == 9, while gethostbyname() sl@0: * can return a fully qualified name from DNS of up to 255 bytes. sl@0: * sl@0: * Fix suggested by Viktor Dukhovni (viktor@esm.com) sl@0: */ sl@0: sl@0: #if defined(SYS_NMLN) && SYS_NMLEN >= 256 sl@0: #define TCL_HOSTNAME_LEN SYS_NMLEN sl@0: #else sl@0: #define TCL_HOSTNAME_LEN 256 sl@0: #endif sl@0: sl@0: sl@0: /* sl@0: * The following variable holds the network name of this host. sl@0: */ sl@0: sl@0: static char hostname[TCL_HOSTNAME_LEN + 1]; sl@0: static int hostnameInited = 0; sl@0: TCL_DECLARE_MUTEX(hostMutex) sl@0: sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * Tcl_GetHostName -- sl@0: * sl@0: * Returns the name of the local host. sl@0: * sl@0: * Results: sl@0: * A string containing the network name for this machine, or sl@0: * an empty string if we can't figure out the name. The caller sl@0: * must not modify or free this string. sl@0: * sl@0: * Side effects: sl@0: * None. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: EXPORT_C CONST char * sl@0: Tcl_GetHostName() sl@0: { sl@0: #ifndef NO_UNAME sl@0: struct utsname u; sl@0: struct hostent *hp; sl@0: #else sl@0: char buffer[sizeof(hostname)]; sl@0: #endif sl@0: CONST char *native; sl@0: sl@0: Tcl_MutexLock(&hostMutex); sl@0: if (hostnameInited) { sl@0: Tcl_MutexUnlock(&hostMutex); sl@0: return hostname; sl@0: } sl@0: sl@0: native = NULL; sl@0: #ifndef NO_UNAME sl@0: (VOID *) memset((VOID *) &u, (int) 0, sizeof(struct utsname)); sl@0: if (uname(&u) > -1) { /* INTL: Native. */ sl@0: hp = TclpGetHostByName(u.nodename); /* INTL: Native. */ sl@0: if (hp == NULL) { sl@0: /* sl@0: * Sometimes the nodename is fully qualified, but gets truncated sl@0: * as it exceeds SYS_NMLN. See if we can just get the immediate sl@0: * nodename and get a proper answer that way. sl@0: */ sl@0: char *dot = strchr(u.nodename, '.'); sl@0: if (dot != NULL) { sl@0: char *node = ckalloc((unsigned) (dot - u.nodename + 1)); sl@0: memcpy(node, u.nodename, (size_t) (dot - u.nodename)); sl@0: node[dot - u.nodename] = '\0'; sl@0: hp = TclpGetHostByName(node); sl@0: ckfree(node); sl@0: } sl@0: } sl@0: if (hp != NULL) { sl@0: native = hp->h_name; sl@0: } else { sl@0: native = u.nodename; sl@0: } sl@0: } sl@0: #else sl@0: /* sl@0: * Uname doesn't exist; try gethostname instead. sl@0: */ sl@0: sl@0: if (gethostname(buffer, sizeof(buffer)) > -1) { /* INTL: Native. */ sl@0: native = buffer; sl@0: } sl@0: #endif sl@0: sl@0: if (native == NULL) { sl@0: hostname[0] = 0; sl@0: } else { sl@0: Tcl_ExternalToUtf(NULL, NULL, native, -1, 0, NULL, hostname, sl@0: sizeof(hostname), NULL, NULL, NULL); sl@0: } sl@0: hostnameInited = 1; sl@0: Tcl_MutexUnlock(&hostMutex); sl@0: return hostname; sl@0: } sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * TclpHasSockets -- sl@0: * sl@0: * Detect if sockets are available on this platform. sl@0: * sl@0: * Results: sl@0: * Returns TCL_OK. sl@0: * sl@0: * Side effects: sl@0: * None. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: int sl@0: TclpHasSockets(interp) sl@0: Tcl_Interp *interp; /* Not used. */ sl@0: { sl@0: return TCL_OK; sl@0: } sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * TclpFinalizeSockets -- sl@0: * sl@0: * Performs per-thread socket subsystem finalization. sl@0: * sl@0: * Results: sl@0: * None. sl@0: * sl@0: * Side effects: sl@0: * None. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: void sl@0: TclpFinalizeSockets() sl@0: { sl@0: return; sl@0: }