1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
/*
* tclIOSock.c --
*
* Common routines used by all socket based channel types.
*
* Copyright (c) 1995-1997 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include "tclInt.h"
#if defined(_WIN32) && defined(UNICODE)
/* On Windows, we always need the ASCII version. */
# undef gai_strerror
# define gai_strerror gai_strerrorA
#endif
/*
*---------------------------------------------------------------------------
*
* TclSockGetPort --
*
* Maps from a string, which could be a service name, to a port. Used by
* socket creation code to get port numbers and resolve registered
* service names to port numbers.
*
* Results:
* A standard Tcl result. On success, the port number is returned in
* portPtr. On failure, an error message is left in the interp's result.
*
* Side effects:
* None.
*
*---------------------------------------------------------------------------
*/
int
TclSockGetPort(
Tcl_Interp *interp,
const char *string, /* Integer or service name */
const char *proto, /* "tcp" or "udp", typically */
int *portPtr) /* Return port number */
{
struct servent *sp; /* Protocol info for named services */
Tcl_DString ds;
const char *native;
if (Tcl_GetInt(NULL, string, portPtr) != TCL_OK) {
/*
* Don't bother translating 'proto' to native.
*/
native = Tcl_UtfToExternalDString(NULL, string, -1, &ds);
sp = getservbyname(native, proto); /* INTL: Native. */
Tcl_DStringFree(&ds);
if (sp != NULL) {
*portPtr = ntohs((unsigned short) sp->s_port);
return TCL_OK;
}
}
if (Tcl_GetInt(interp, string, portPtr) != TCL_OK) {
return TCL_ERROR;
}
if (*portPtr > 0xFFFF) {
Tcl_AppendResult(interp, "couldn't open socket: port number too high",
NULL);
return TCL_ERROR;
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* TclSockMinimumBuffers --
*
* Ensure minimum buffer sizes (non zero).
*
* Results:
* A standard Tcl result.
*
* Side effects:
* Sets SO_SNDBUF and SO_RCVBUF sizes.
*
*----------------------------------------------------------------------
*/
#ifndef _WIN32
# define SOCKET size_t
#endif
int
TclSockMinimumBuffers(
void *sock, /* Socket file descriptor */
int size) /* Minimum buffer size */
{
int current;
socklen_t len;
len = sizeof(int);
getsockopt((SOCKET)sock, SOL_SOCKET, SO_SNDBUF, (char *)¤t, &len);
if (current < size) {
len = sizeof(int);
setsockopt((SOCKET)sock, SOL_SOCKET, SO_SNDBUF, (char *)&size, len);
}
len = sizeof(int);
getsockopt((SOCKET)sock, SOL_SOCKET, SO_RCVBUF, (char *)¤t, &len);
if (current < size) {
len = sizeof(int);
setsockopt((SOCKET)sock, SOL_SOCKET, SO_RCVBUF, (char *)&size, len);
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* TclCreateSocketAddress --
*
* This function initializes a sockaddr structure for a host and port.
*
* Results:
* 1 if the host was valid, 0 if the host could not be converted to an IP
* address.
*
* Side effects:
* Fills in the *sockaddrPtr structure.
*
*----------------------------------------------------------------------
*/
int
TclCreateSocketAddress(
Tcl_Interp *interp, /* Interpreter for querying
* the desired socket family */
struct addrinfo **addrlist, /* Socket address list */
const char *host, /* Host. NULL implies INADDR_ANY */
int port, /* Port number */
int willBind, /* Is this an address to bind() to or
* to connect() to? */
const char **errorMsgPtr) /* Place to store the error message
* detail, if available. */
{
struct addrinfo hints;
struct addrinfo *p;
struct addrinfo *v4head = NULL, *v4ptr = NULL;
struct addrinfo *v6head = NULL, *v6ptr = NULL;
char *native = NULL, portstring[TCL_INTEGER_SPACE];
const char *family = NULL;
Tcl_DString ds;
int result, i;
TclFormatInt(portstring, port);
if (host != NULL) {
native = Tcl_UtfToExternalDString(NULL, host, -1, &ds);
}
(void) memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
/*
* Magic variable to enforce a certain address family - to be superseded
* by a TIP that adds explicit switches to [socket]
*/
if (interp != NULL) {
family = Tcl_GetVar(interp, "::tcl::unsupported::socketAF", 0);
if (family != NULL) {
if (strcmp(family, "inet") == 0) {
hints.ai_family = AF_INET;
} else if (strcmp(family, "inet6") == 0) {
hints.ai_family = AF_INET6;
}
}
}
hints.ai_socktype = SOCK_STREAM;
#if 0
/*
* We found some problems when using AI_ADDRCONFIG, e.g. on systems that
* have no networking besides the loopback interface and want to resolve
* localhost. See bugs 3385024, 3382419, 3382431. As the advantage of
* using AI_ADDRCONFIG in situations where it works, is probably low,
* we'll leave it out for now. After all, it is just an optimisation.
*/
#if defined(AI_ADDRCONFIG) && !defined(_AIX) && !defined(__hpux)
/*
* Missing on: OpenBSD, NetBSD.
* Causes failure when used on AIX 5.1 and HP-UX
*/
hints.ai_flags |= AI_ADDRCONFIG;
#endif
#endif
if (willBind) {
hints.ai_flags |= AI_PASSIVE;
}
result = getaddrinfo(native, portstring, &hints, addrlist);
if (host != NULL) {
Tcl_DStringFree(&ds);
}
if (result != 0) {
goto error;
}
/*
* Put IPv4 addresses before IPv6 addresses to maximize backwards
* compatibility of [fconfigure -sockname] output.
*
* There might be more elegant/efficient ways to do this.
*/
if (willBind) {
for (p = *addrlist; p != NULL; p = p->ai_next) {
if (p->ai_family == AF_INET) {
if (v4head == NULL) {
v4head = p;
} else {
v4ptr->ai_next = p;
}
v4ptr = p;
} else {
if (v6head == NULL) {
v6head = p;
} else {
v6ptr->ai_next = p;
}
v6ptr = p;
}
}
*addrlist = NULL;
if (v6head != NULL) {
*addrlist = v6head;
v6ptr->ai_next = NULL;
}
if (v4head != NULL) {
v4ptr->ai_next = *addrlist;
*addrlist = v4head;
}
}
i = 0;
for (p = *addrlist; p != NULL; p = p->ai_next) {
i++;
}
return 1;
/*
* Ought to use gai_strerror() here...
*/
error:
switch (result) {
case EAI_NONAME:
case EAI_SERVICE:
#if defined(EAI_ADDRFAMILY) && EAI_ADDRFAMILY != EAI_NONAME
case EAI_ADDRFAMILY:
#endif
#if defined(EAI_NODATA) && EAI_NODATA != EAI_NONAME
case EAI_NODATA:
#endif
*errorMsgPtr = gai_strerror(result);
errno = EHOSTUNREACH;
return 0;
#ifdef EAI_SYSTEM
case EAI_SYSTEM:
return 0;
#endif
default:
*errorMsgPtr = gai_strerror(result);
errno = ENXIO;
return 0;
}
}
/*
* Local Variables:
* mode: c
* c-basic-offset: 4
* fill-column: 78
* End:
*/
|