home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tkisrc04.zip / tcl / os2 / tclIOSock.c < prev    next >
C/C++ Source or Header  |  1998-08-07  |  3KB  |  98 lines

  1. /* 
  2.  * tclIOSock.c --
  3.  *
  4.  *    Common routines used by all socket based channel types.
  5.  *
  6.  * Copyright (c) 1995 Sun Microsystems, Inc.
  7.  *
  8.  * See the file "license.terms" for information on usage and redistribution
  9.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  10.  *
  11.  * SCCS: @(#) tclIOSock.c 1.16 96/03/12 07:04:33
  12.  */
  13.  
  14. #include "tclInt.h"
  15. #include "tclPort.h"
  16. #include <utils.h>   /*MM*/
  17.  
  18. /*
  19.  *----------------------------------------------------------------------
  20.  *
  21.  * TclSockGetPort --
  22.  *
  23.  *    Maps from a string, which could be a service name, to a port.
  24.  *    Used by socket creation code to get port numbers and resolve
  25.  *    registered service names to port numbers.
  26.  *
  27.  * Results:
  28.  *    A standard Tcl result.  On success, the port number is
  29.  *    returned in portPtr. On failure, an error message is left in
  30.  *    interp->result.
  31.  *
  32.  * Side effects:
  33.  *    None.
  34.  *
  35.  *----------------------------------------------------------------------
  36.  */
  37.  
  38. int
  39. TclSockGetPort(interp, string, proto, portPtr)
  40.     Tcl_Interp *interp;
  41.     char *string;        /* Integer or service name */
  42.     char *proto;        /* "tcp" or "udp", typically */
  43.     int *portPtr;        /* Return port number */
  44. {
  45.     struct servent *sp = getservbyname(string, proto);    
  46.     if (sp != NULL) {
  47.     *portPtr = ntohs((unsigned short) sp->s_port);
  48.     return TCL_OK;
  49.     }
  50.     if (Tcl_GetInt(interp, string, portPtr) != TCL_OK) {
  51.     return TCL_ERROR;
  52.     }
  53.     if (*portPtr > 0xFFFF) {
  54.         Tcl_AppendResult(interp, "couldn't open socket: port number too high",
  55.                 (char *) NULL);
  56.     return TCL_ERROR;
  57.     }
  58.     return TCL_OK;
  59. }
  60.  
  61. /*
  62.  *----------------------------------------------------------------------
  63.  *
  64.  * TclSockMinimumBuffers --
  65.  *
  66.  *    Ensure minimum buffer sizes (non zero).
  67.  *
  68.  * Results:
  69.  *    A standard Tcl result.
  70.  *
  71.  * Side effects:
  72.  *    Sets SO_SNDBUF and SO_RCVBUF sizes.
  73.  *
  74.  *----------------------------------------------------------------------
  75.  */
  76.  
  77. int
  78. TclSockMinimumBuffers(sock, size)
  79.     int sock;            /* Socket file descriptor */
  80.     int size;            /* Minimum buffer size */
  81. {
  82.     int current;
  83.     int len = sizeof(int);
  84.  
  85.     getsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char *) ¤t, &len);
  86.     if (current < size) {
  87.     len = sizeof(int);
  88.     setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char *) &size, len);
  89.     }
  90.     len = sizeof(int);
  91.     getsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *) ¤t, &len);
  92.     if (current < size) {
  93.     len = sizeof(int);
  94.     setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *) &size, len);
  95.     }
  96.     return TCL_OK;
  97. }
  98.