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