Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/generic/tclIOSock.c @ 35

Last change on this file since 35 was 25, checked in by landauf, 17 years ago

added tcl to libs

File size: 2.8 KB
Line 
1/*
2 * tclIOSock.c --
3 *
4 *      Common routines used by all socket based channel types.
5 *
6 * Copyright (c) 1995-1997 Sun Microsystems, Inc.
7 *
8 * See the file "license.terms" for information on usage and redistribution of
9 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
10 *
11 * RCS: @(#) $Id: tclIOSock.c,v 1.11 2007/02/20 23:24:04 nijtmans Exp $
12 */
13
14#include "tclInt.h"
15
16/*
17 *---------------------------------------------------------------------------
18 *
19 * TclSockGetPort --
20 *
21 *      Maps from a string, which could be a service name, to a port. Used by
22 *      socket creation code to get port numbers and resolve registered
23 *      service names to port numbers.
24 *
25 * Results:
26 *      A standard Tcl result. On success, the port number is returned in
27 *      portPtr. On failure, an error message is left in the interp's result.
28 *
29 * Side effects:
30 *      None.
31 *
32 *---------------------------------------------------------------------------
33 */
34
35int
36TclSockGetPort(
37    Tcl_Interp *interp,
38    const char *string, /* Integer or service name */
39    const char *proto, /* "tcp" or "udp", typically */
40    int *portPtr)               /* Return port number */
41{
42    struct servent *sp;         /* Protocol info for named services */
43    Tcl_DString ds;
44    const char *native;
45
46    if (Tcl_GetInt(NULL, string, portPtr) != TCL_OK) {
47        /*
48         * Don't bother translating 'proto' to native.
49         */
50
51        native = Tcl_UtfToExternalDString(NULL, string, -1, &ds);
52        sp = getservbyname(native, proto);              /* INTL: Native. */
53        Tcl_DStringFree(&ds);
54        if (sp != NULL) {
55            *portPtr = ntohs((unsigned short) sp->s_port);
56            return TCL_OK;
57        }
58    }
59    if (Tcl_GetInt(interp, string, portPtr) != TCL_OK) {
60        return TCL_ERROR;
61    }
62    if (*portPtr > 0xFFFF) {
63        Tcl_AppendResult(interp, "couldn't open socket: port number too high",
64                NULL);
65        return TCL_ERROR;
66    }
67    return TCL_OK;
68}
69
70/*
71 *----------------------------------------------------------------------
72 *
73 * TclSockMinimumBuffers --
74 *
75 *      Ensure minimum buffer sizes (non zero).
76 *
77 * Results:
78 *      A standard Tcl result.
79 *
80 * Side effects:
81 *      Sets SO_SNDBUF and SO_RCVBUF sizes.
82 *
83 *----------------------------------------------------------------------
84 */
85
86int
87TclSockMinimumBuffers(
88    int sock,                   /* Socket file descriptor */
89    int size)                   /* Minimum buffer size */
90{
91    int current;
92    socklen_t len;
93
94    len = sizeof(int);
95    getsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char *)&current, &len);
96    if (current < size) {
97        len = sizeof(int);
98        setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char *)&size, len);
99    }
100    len = sizeof(int);
101    getsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)&current, &len);
102    if (current < size) {
103        len = sizeof(int);
104        setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)&size, len);
105    }
106    return TCL_OK;
107}
108
109/*
110 * Local Variables:
111 * mode: c
112 * c-basic-offset: 4
113 * fill-column: 78
114 * End:
115 */
Note: See TracBrowser for help on using the repository browser.