Example #1
0
static void
InitializeHostName(
    char **valuePtr,
    int *lengthPtr,
    Tcl_Encoding *encodingPtr)
{
    const char *native = NULL;

#ifndef NO_UNAME
    struct utsname u;
    struct hostent *hp;

    memset(&u, (int) 0, sizeof(struct utsname));
    if (uname(&u) > -1) {				/* INTL: Native. */
        hp = TclpGetHostByName(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(dot - u.nodename + 1);

		memcpy(node, u.nodename, (size_t) (dot - u.nodename));
		node[dot - u.nodename] = '\0';
		hp = TclpGetHostByName(node);
		ckfree(node);
	    }
	}
        if (hp != NULL) {
	    native = hp->h_name;
        } else {
	    native = u.nodename;
        }
    }
    if (native == NULL) {
	native = tclEmptyStringRep;
    }
#else /* !NO_UNAME */
    /*
     * Uname doesn't exist; try gethostname instead.
     *
     * 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 ([email protected])
     */

#    if defined(SYS_NMLN) && (SYS_NMLEN >= 256)
    char buffer[SYS_NMLEN];
#    else
    char buffer[256];
#    endif

    if (gethostname(buffer, sizeof(buffer)) > -1) {	/* INTL: Native. */
	native = buffer;
    }
#endif /* NO_UNAME */

    *encodingPtr = Tcl_GetEncoding(NULL, NULL);
    *lengthPtr = strlen(native);
    *valuePtr = ckalloc((*lengthPtr) + 1);
    memcpy(*valuePtr, native, (size_t)(*lengthPtr)+1);
}
Example #2
0
EXPORT_C 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 = TclpGetHostByName(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 = TclpGetHostByName(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;
}