Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtdocbook / tcl / tclIOSock.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: tclIOSock.c /main/2 1996/08/08 14:44:39 cde-hp $ */
24 /* 
25  * tclIOSock.c --
26  *
27  *      Common routines used by all socket based channel types.
28  *
29  * Copyright (c) 1995 Sun Microsystems, Inc.
30  *
31  * See the file "license.terms" for information on usage and redistribution
32  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
33  *
34  * SCCS: @(#) tclIOSock.c 1.16 96/03/12 07:04:33
35  */
36
37 #include "tclInt.h"
38 #include "tclPort.h"
39 \f
40 /*
41  *----------------------------------------------------------------------
42  *
43  * TclSockGetPort --
44  *
45  *      Maps from a string, which could be a service name, to a port.
46  *      Used by socket creation code to get port numbers and resolve
47  *      registered service names to port numbers.
48  *
49  * Results:
50  *      A standard Tcl result.  On success, the port number is
51  *      returned in portPtr. On failure, an error message is left in
52  *      interp->result.
53  *
54  * Side effects:
55  *      None.
56  *
57  *----------------------------------------------------------------------
58  */
59
60 int
61 TclSockGetPort(interp, string, proto, portPtr)
62     Tcl_Interp *interp;
63     char *string;               /* Integer or service name */
64     char *proto;                /* "tcp" or "udp", typically */
65     int *portPtr;               /* Return port number */
66 {
67     struct servent *sp = getservbyname(string, proto);    
68     if (sp != NULL) {
69         *portPtr = ntohs((unsigned short) sp->s_port);
70         return TCL_OK;
71     }
72     if (Tcl_GetInt(interp, string, portPtr) != TCL_OK) {
73         return TCL_ERROR;
74     }
75     if (*portPtr > 0xFFFF) {
76         Tcl_AppendResult(interp, "couldn't open socket: port number too high",
77                 (char *) NULL);
78         return TCL_ERROR;
79     }
80     return TCL_OK;
81 }
82 \f
83 /*
84  *----------------------------------------------------------------------
85  *
86  * TclSockMinimumBuffers --
87  *
88  *      Ensure minimum buffer sizes (non zero).
89  *
90  * Results:
91  *      A standard Tcl result.
92  *
93  * Side effects:
94  *      Sets SO_SNDBUF and SO_RCVBUF sizes.
95  *
96  *----------------------------------------------------------------------
97  */
98
99 int
100 TclSockMinimumBuffers(sock, size)
101     int sock;                   /* Socket file descriptor */
102     int size;                   /* Minimum buffer size */
103 {
104     int current;
105     int len = sizeof(int);
106
107     getsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char *) &current, &len);
108     if (current < size) {
109         len = sizeof(int);
110         setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char *) &size, len);
111     }
112     len = sizeof(int);
113     getsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *) &current, &len);
114     if (current < size) {
115         len = sizeof(int);
116         setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *) &size, len);
117     }
118     return TCL_OK;
119 }