os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/generic/tclIOSock.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/generic/tclIOSock.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,109 @@
     1.4 +/* 
     1.5 + * tclIOSock.c --
     1.6 + *
     1.7 + *	Common routines used by all socket based channel types.
     1.8 + *
     1.9 + * Copyright (c) 1995-1997 Sun Microsystems, Inc.
    1.10 + *
    1.11 + * See the file "license.terms" for information on usage and redistribution
    1.12 + * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
    1.13 + *
    1.14 + * RCS: @(#) $Id: tclIOSock.c,v 1.7 2002/07/29 16:54:41 rmax Exp $
    1.15 + */
    1.16 +
    1.17 +#include "tclInt.h"
    1.18 +#include "tclPort.h"
    1.19 +
    1.20 +/*
    1.21 + *---------------------------------------------------------------------------
    1.22 + *
    1.23 + * TclSockGetPort --
    1.24 + *
    1.25 + *	Maps from a string, which could be a service name, to a port.
    1.26 + *	Used by socket creation code to get port numbers and resolve
    1.27 + *	registered service names to port numbers.
    1.28 + *
    1.29 + * Results:
    1.30 + *	A standard Tcl result.  On success, the port number is returned
    1.31 + *	in portPtr. On failure, an error message is left in the interp's
    1.32 + *	result.
    1.33 + *
    1.34 + * Side effects:
    1.35 + *	None.
    1.36 + *
    1.37 + *---------------------------------------------------------------------------
    1.38 + */
    1.39 +
    1.40 +int
    1.41 +TclSockGetPort(interp, string, proto, portPtr)
    1.42 +    Tcl_Interp *interp;
    1.43 +    char *string;		/* Integer or service name */
    1.44 +    char *proto;		/* "tcp" or "udp", typically */
    1.45 +    int *portPtr;		/* Return port number */
    1.46 +{
    1.47 +    struct servent *sp;		/* Protocol info for named services */
    1.48 +    Tcl_DString ds;
    1.49 +    CONST char *native;
    1.50 +
    1.51 +    if (Tcl_GetInt(NULL, string, portPtr) != TCL_OK) {
    1.52 +	/*
    1.53 +	 * Don't bother translating 'proto' to native.
    1.54 +	 */
    1.55 +	 
    1.56 +	native = Tcl_UtfToExternalDString(NULL, string, -1, &ds);
    1.57 +	sp = getservbyname(native, proto);		/* INTL: Native. */
    1.58 +	Tcl_DStringFree(&ds);
    1.59 +	if (sp != NULL) {
    1.60 +	    *portPtr = ntohs((unsigned short) sp->s_port);
    1.61 +	    return TCL_OK;
    1.62 +	}
    1.63 +    }
    1.64 +    if (Tcl_GetInt(interp, string, portPtr) != TCL_OK) {
    1.65 +	return TCL_ERROR;
    1.66 +    }
    1.67 +    if (*portPtr > 0xFFFF) {
    1.68 +        Tcl_AppendResult(interp, "couldn't open socket: port number too high",
    1.69 +                (char *) NULL);
    1.70 +	return TCL_ERROR;
    1.71 +    }
    1.72 +    return TCL_OK;
    1.73 +}
    1.74 +
    1.75 +/*
    1.76 + *----------------------------------------------------------------------
    1.77 + *
    1.78 + * TclSockMinimumBuffers --
    1.79 + *
    1.80 + *	Ensure minimum buffer sizes (non zero).
    1.81 + *
    1.82 + * Results:
    1.83 + *	A standard Tcl result.
    1.84 + *
    1.85 + * Side effects:
    1.86 + *	Sets SO_SNDBUF and SO_RCVBUF sizes.
    1.87 + *
    1.88 + *----------------------------------------------------------------------
    1.89 + */
    1.90 +
    1.91 +int
    1.92 +TclSockMinimumBuffers(sock, size)
    1.93 +    int sock;			/* Socket file descriptor */
    1.94 +    int size;			/* Minimum buffer size */
    1.95 +{
    1.96 +    int current;
    1.97 +    socklen_t len;
    1.98 +
    1.99 +    len = sizeof(int);
   1.100 +    getsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char *)&current, &len);
   1.101 +    if (current < size) {
   1.102 +	len = sizeof(int);
   1.103 +	setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char *)&size, len);
   1.104 +    }
   1.105 +    len = sizeof(int);
   1.106 +    getsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)&current, &len);
   1.107 +    if (current < size) {
   1.108 +	len = sizeof(int);
   1.109 +	setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)&size, len);
   1.110 +    }
   1.111 +    return TCL_OK;
   1.112 +}