tclIOSock.c

Go to the documentation of this file.
00001 /*
00002  * tclIOSock.c --
00003  *
00004  *      Common routines used by all socket based channel types.
00005  *
00006  * Copyright (c) 1995-1997 Sun Microsystems, Inc.
00007  *
00008  * See the file "license.terms" for information on usage and redistribution of
00009  * this file, and for a DISCLAIMER OF ALL WARRANTIES.
00010  *
00011  * RCS: @(#) $Id: tclIOSock.c,v 1.11 2007/02/20 23:24:04 nijtmans Exp $
00012  */
00013 
00014 #include "tclInt.h"
00015 
00016 /*
00017  *---------------------------------------------------------------------------
00018  *
00019  * TclSockGetPort --
00020  *
00021  *      Maps from a string, which could be a service name, to a port. Used by
00022  *      socket creation code to get port numbers and resolve registered
00023  *      service names to port numbers.
00024  *
00025  * Results:
00026  *      A standard Tcl result. On success, the port number is returned in
00027  *      portPtr. On failure, an error message is left in the interp's result.
00028  *
00029  * Side effects:
00030  *      None.
00031  *
00032  *---------------------------------------------------------------------------
00033  */
00034 
00035 int
00036 TclSockGetPort(
00037     Tcl_Interp *interp,
00038     const char *string, /* Integer or service name */
00039     const char *proto, /* "tcp" or "udp", typically */
00040     int *portPtr)               /* Return port number */
00041 {
00042     struct servent *sp;         /* Protocol info for named services */
00043     Tcl_DString ds;
00044     const char *native;
00045 
00046     if (Tcl_GetInt(NULL, string, portPtr) != TCL_OK) {
00047         /*
00048          * Don't bother translating 'proto' to native.
00049          */
00050 
00051         native = Tcl_UtfToExternalDString(NULL, string, -1, &ds);
00052         sp = getservbyname(native, proto);              /* INTL: Native. */
00053         Tcl_DStringFree(&ds);
00054         if (sp != NULL) {
00055             *portPtr = ntohs((unsigned short) sp->s_port);
00056             return TCL_OK;
00057         }
00058     }
00059     if (Tcl_GetInt(interp, string, portPtr) != TCL_OK) {
00060         return TCL_ERROR;
00061     }
00062     if (*portPtr > 0xFFFF) {
00063         Tcl_AppendResult(interp, "couldn't open socket: port number too high",
00064                 NULL);
00065         return TCL_ERROR;
00066     }
00067     return TCL_OK;
00068 }
00069 
00070 /*
00071  *----------------------------------------------------------------------
00072  *
00073  * TclSockMinimumBuffers --
00074  *
00075  *      Ensure minimum buffer sizes (non zero).
00076  *
00077  * Results:
00078  *      A standard Tcl result.
00079  *
00080  * Side effects:
00081  *      Sets SO_SNDBUF and SO_RCVBUF sizes.
00082  *
00083  *----------------------------------------------------------------------
00084  */
00085 
00086 int
00087 TclSockMinimumBuffers(
00088     int sock,                   /* Socket file descriptor */
00089     int size)                   /* Minimum buffer size */
00090 {
00091     int current;
00092     socklen_t len;
00093 
00094     len = sizeof(int);
00095     getsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char *)&current, &len);
00096     if (current < size) {
00097         len = sizeof(int);
00098         setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char *)&size, len);
00099     }
00100     len = sizeof(int);
00101     getsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)&current, &len);
00102     if (current < size) {
00103         len = sizeof(int);
00104         setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)&size, len);
00105     }
00106     return TCL_OK;
00107 }
00108 
00109 /*
00110  * Local Variables:
00111  * mode: c
00112  * c-basic-offset: 4
00113  * fill-column: 78
00114  * End:
00115  */



Generated on Wed Mar 12 12:18:18 2008 by  doxygen 1.5.1