1 |
/* $Header$ */ |
2 |
/* |
3 |
* tclIOSock.c -- |
4 |
* |
5 |
* Common routines used by all socket based channel types. |
6 |
* |
7 |
* Copyright (c) 1995-1997 Sun Microsystems, Inc. |
8 |
* |
9 |
* See the file "license.terms" for information on usage and redistribution |
10 |
* of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
11 |
* |
12 |
* RCS: @(#) $Id: tcliosock.c,v 1.1.1.1 2001/06/13 04:42:17 dtashley Exp $ |
13 |
*/ |
14 |
|
15 |
#include "tclInt.h" |
16 |
#include "tclPort.h" |
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 returned |
29 |
* in portPtr. On failure, an error message is left in the interp's |
30 |
* 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; /* Protocol info for named services */ |
46 |
Tcl_DString ds; |
47 |
char *native; |
48 |
|
49 |
if (Tcl_GetInt(NULL, string, portPtr) != TCL_OK) { |
50 |
/* |
51 |
* Don't bother translating 'proto' to native. |
52 |
*/ |
53 |
|
54 |
native = Tcl_UtfToExternalDString(NULL, string, -1, &ds); |
55 |
sp = getservbyname(native, proto); /* INTL: Native. */ |
56 |
Tcl_DStringFree(&ds); |
57 |
if (sp != NULL) { |
58 |
*portPtr = ntohs((unsigned short) sp->s_port); |
59 |
return TCL_OK; |
60 |
} |
61 |
} |
62 |
if (Tcl_GetInt(interp, string, portPtr) != TCL_OK) { |
63 |
return TCL_ERROR; |
64 |
} |
65 |
if (*portPtr > 0xFFFF) { |
66 |
Tcl_AppendResult(interp, "couldn't open socket: port number too high", |
67 |
(char *) NULL); |
68 |
return TCL_ERROR; |
69 |
} |
70 |
return TCL_OK; |
71 |
} |
72 |
|
73 |
/* |
74 |
*---------------------------------------------------------------------- |
75 |
* |
76 |
* TclSockMinimumBuffers -- |
77 |
* |
78 |
* Ensure minimum buffer sizes (non zero). |
79 |
* |
80 |
* Results: |
81 |
* A standard Tcl result. |
82 |
* |
83 |
* Side effects: |
84 |
* Sets SO_SNDBUF and SO_RCVBUF sizes. |
85 |
* |
86 |
*---------------------------------------------------------------------- |
87 |
*/ |
88 |
|
89 |
int |
90 |
TclSockMinimumBuffers(sock, size) |
91 |
int sock; /* Socket file descriptor */ |
92 |
int size; /* Minimum buffer size */ |
93 |
{ |
94 |
int current; |
95 |
/* |
96 |
* Should be socklen_t, but HP10.20 (g)cc chokes |
97 |
*/ |
98 |
size_t len; |
99 |
|
100 |
len = sizeof(int); |
101 |
getsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char *)¤t, &len); |
102 |
if (current < size) { |
103 |
len = sizeof(int); |
104 |
setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char *)&size, len); |
105 |
} |
106 |
len = sizeof(int); |
107 |
getsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)¤t, &len); |
108 |
if (current < size) { |
109 |
len = sizeof(int); |
110 |
setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)&size, len); |
111 |
} |
112 |
return TCL_OK; |
113 |
} |
114 |
|
115 |
/* End of tcliosock.c */ |