1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
/*
* tclUnixSock.c --
*
* This file contains Unix-specific socket related code.
*
* Copyright (c) 1995 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* RCS: @(#) $Id: tclUnixSock.c,v 1.6 2002/02/27 01:16:43 hobbs Exp $
*/
#include "tcl.h"
#include "tclPort.h"
/*
* There is no portable macro for the maximum length
* of host names returned by gethostbyname(). We should only
* trust SYS_NMLN if it is at least 255 + 1 bytes to comply with DNS
* host name limits.
*
* Note: SYS_NMLN is a restriction on "uname" not on gethostbyname!
*
* For example HP-UX 10.20 has SYS_NMLN == 9, while gethostbyname()
* can return a fully qualified name from DNS of up to 255 bytes.
*
* Fix suggested by Viktor Dukhovni (viktor@esm.com)
*/
#if defined(SYS_NMLN) && SYS_NMLEN >= 256
#define TCL_HOSTNAME_LEN SYS_NMLEN
#else
#define TCL_HOSTNAME_LEN 256
#endif
/*
* The following variable holds the network name of this host.
*/
static char hostname[TCL_HOSTNAME_LEN + 1];
static int hostnameInited = 0;
TCL_DECLARE_MUTEX(hostMutex)
/*
*----------------------------------------------------------------------
*
* Tcl_GetHostName --
*
* Returns the name of the local host.
*
* Results:
* A string containing the network name for this machine, or
* an empty string if we can't figure out the name. The caller
* must not modify or free this string.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
CONST char *
Tcl_GetHostName()
{
#ifndef NO_UNAME
struct utsname u;
struct hostent *hp;
#else
char buffer[sizeof(hostname)];
#endif
CONST char *native;
Tcl_MutexLock(&hostMutex);
if (hostnameInited) {
Tcl_MutexUnlock(&hostMutex);
return hostname;
}
native = NULL;
#ifndef NO_UNAME
(VOID *) memset((VOID *) &u, (int) 0, sizeof(struct utsname));
if (uname(&u) > -1) { /* INTL: Native. */
hp = gethostbyname(u.nodename); /* INTL: Native. */
if (hp == NULL) {
/*
* Sometimes the nodename is fully qualified, but gets truncated
* as it exceeds SYS_NMLN. See if we can just get the immediate
* nodename and get a proper answer that way.
*/
char *dot = strchr(u.nodename, '.');
if (dot != NULL) {
char *node = ckalloc((unsigned) (dot - u.nodename + 1));
memcpy(node, u.nodename, (size_t) (dot - u.nodename));
node[dot - u.nodename] = '\0';
hp = gethostbyname(node);
ckfree(node);
}
}
if (hp != NULL) {
native = hp->h_name;
} else {
native = u.nodename;
}
}
#else
/*
* Uname doesn't exist; try gethostname instead.
*/
if (gethostname(buffer, sizeof(buffer)) > -1) { /* INTL: Native. */
native = buffer;
}
#endif
if (native == NULL) {
hostname[0] = 0;
} else {
Tcl_ExternalToUtf(NULL, NULL, native, -1, 0, NULL, hostname,
sizeof(hostname), NULL, NULL, NULL);
}
hostnameInited = 1;
Tcl_MutexUnlock(&hostMutex);
return hostname;
}
/*
*----------------------------------------------------------------------
*
* TclpHasSockets --
*
* Detect if sockets are available on this platform.
*
* Results:
* Returns TCL_OK.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int
TclpHasSockets(interp)
Tcl_Interp *interp; /* Not used. */
{
return TCL_OK;
}
|