1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
/*
* tclWinSock.c --
*
* This file contains Windows-specific socket related code.
*
* 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.
*
* RCS: @(#) $Id: tclWinSock.c,v 1.59 2007/11/29 18:00:20 dgp Exp $
*/
#include "tclWinInt.h"
/*
* Support for control over sockets' KEEPALIVE and NODELAY behavior is
* currently disabled.
|
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
/*
* tclWinSock.c --
*
* This file contains Windows-specific socket related code.
*
* 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.
*
* RCS: @(#) $Id: tclWinSock.c,v 1.60 2007/11/30 01:09:11 hobbs Exp $
*/
#include "tclWinInt.h"
/*
* Support for control over sockets' KEEPALIVE and NODELAY behavior is
* currently disabled.
|
| ︙ | | | ︙ | |
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
|
#undef getservbyname
#undef getsockopt
#undef ntohs
#undef setsockopt
/*
* The following variable is used to tell whether this module has been
* initialized.
*/
static int initialized = 0;
TCL_DECLARE_MUTEX(socketMutex)
/*
* The following variable holds the network name of this host.
*/
static TclInitProcessGlobalValueProc InitializeHostName;
static ProcessGlobalValue hostName = {
0, 0, NULL, NULL, InitializeHostName, NULL, NULL
};
/*
* Mingw, Cygwin and OpenWatcom may not have LPFN_* typedefs.
*/
#ifdef HAVE_NO_LPFN_DECLS
typedef SOCKET (PASCAL FAR *LPFN_ACCEPT)(SOCKET s, struct sockaddr FAR *addr,
int FAR *addrlen);
typedef int (PASCAL FAR *LPFN_BIND)(SOCKET s, const struct sockaddr FAR *addr,
int namelen);
typedef int (PASCAL FAR *LPFN_CLOSESOCKET)(SOCKET s);
typedef int (PASCAL FAR *LPFN_CONNECT)(SOCKET s,
const struct sockaddr FAR *name, int namelen);
typedef struct hostent FAR *(PASCAL FAR *LPFN_GETHOSTBYADDR)
(const char FAR *addr, int addrlen, int addrtype);
typedef struct hostent FAR *(PASCAL FAR *LPFN_GETHOSTBYNAME)
(const char FAR *name);
typedef int (PASCAL FAR *LPFN_GETHOSTNAME)(char FAR *name, int namelen);
typedef int (PASCAL FAR *LPFN_GETPEERNAME)(SOCKET sock,
struct sockaddr FAR *name, int FAR *namelen);
typedef struct servent FAR *(PASCAL FAR *LPFN_GETSERVBYNAME)
(const char FAR *name, const char FAR *proto);
typedef int (PASCAL FAR *LPFN_GETSOCKNAME)(SOCKET sock,
struct sockaddr FAR *name, int FAR *namelen);
typedef int (PASCAL FAR *LPFN_GETSOCKOPT)(SOCKET s, int level, int optname,
char FAR *optval, int FAR *optlen);
typedef u_short (PASCAL FAR *LPFN_HTONS)(u_short hostshort);
typedef unsigned long (PASCAL FAR *LPFN_INET_ADDR)(const char FAR *cp);
typedef char FAR *(PASCAL FAR *LPFN_INET_NTOA)(struct in_addr in);
typedef int (PASCAL FAR *LPFN_IOCTLSOCKET)(SOCKET s, long cmd,
u_long FAR *argp);
typedef int (PASCAL FAR *LPFN_LISTEN)(SOCKET s, int backlog);
typedef u_short (PASCAL FAR *LPFN_NTOHS)(u_short netshort);
typedef int (PASCAL FAR *LPFN_RECV)(SOCKET s, char FAR *buf, int len,
int flags);
typedef int (PASCAL FAR *LPFN_SELECT)(int nfds, fd_set FAR *readfds,
fd_set FAR *writefds, fd_set FAR *exceptfds,
const struct timeval FAR *timeout);
typedef int (PASCAL FAR *LPFN_SEND)(SOCKET s,
const char FAR *buf, int len, int flags);
typedef int (PASCAL FAR *LPFN_SETSOCKOPT)(SOCKET s, int level, int optname,
const char FAR *optval, int optlen);
typedef SOCKET (PASCAL FAR *LPFN_SOCKET)(int af, int type, int protocol);
typedef int (PASCAL FAR *LPFN_WSAASYNCSELECT)(SOCKET s, HWND hWnd, u_int wMsg,
long lEvent);
typedef int (PASCAL FAR *LPFN_WSACLEANUP)(void);
typedef int (PASCAL FAR *LPFN_WSAGETLASTERROR)(void);
typedef int (PASCAL FAR *LPFN_WSASTARTUP)(WORD wVersionRequired,
LPWSADATA lpWSAData);
#endif
/*
* The following structure contains pointers to all of the WinSock API entry
* points used by Tcl. It is initialized by InitSockets. Since we dynamically
* load the Winsock DLL on demand, we must use this function table to refer to
* functions in the winsock API.
*/
static struct {
HMODULE hModule; /* Handle to WinSock library. */
/* Winsock 1.1 functions */
LPFN_ACCEPT accept;
LPFN_BIND bind;
LPFN_CLOSESOCKET closesocket;
LPFN_CONNECT connect;
LPFN_GETHOSTBYADDR gethostbyaddr;
LPFN_GETHOSTBYNAME gethostbyname;
LPFN_GETHOSTNAME gethostname;
LPFN_GETPEERNAME getpeername;
LPFN_GETSERVBYNAME getservbyname;
LPFN_GETSOCKNAME getsockname;
LPFN_GETSOCKOPT getsockopt;
LPFN_HTONS htons;
LPFN_INET_ADDR inet_addr;
LPFN_INET_NTOA inet_ntoa;
LPFN_IOCTLSOCKET ioctlsocket;
LPFN_LISTEN listen;
LPFN_NTOHS ntohs;
LPFN_RECV recv;
LPFN_SELECT select;
LPFN_SEND send;
LPFN_SETSOCKOPT setsockopt;
LPFN_SOCKET socket;
LPFN_WSAASYNCSELECT WSAAsyncSelect;
LPFN_WSACLEANUP WSACleanup;
LPFN_WSAGETLASTERROR WSAGetLastError;
LPFN_WSASTARTUP WSAStartup;
} winSock;
/*
* The following defines declare the messages used on socket windows.
*/
#define SOCKET_MESSAGE WM_USER+1
#define SOCKET_SELECT WM_USER+2
#define SOCKET_TERMINATE WM_USER+3
|
|
>
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
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
|
#undef getservbyname
#undef getsockopt
#undef ntohs
#undef setsockopt
/*
* The following variable is used to tell whether this module has been
* initialized. If 1, initialization of sockets was successful, if -1 then
* socket initialization failed (WSAStartup failed).
*/
static int initialized = 0;
TCL_DECLARE_MUTEX(socketMutex)
/*
* The following variable holds the network name of this host.
*/
static TclInitProcessGlobalValueProc InitializeHostName;
static ProcessGlobalValue hostName = {
0, 0, NULL, NULL, InitializeHostName, NULL, NULL
};
/*
* The following defines declare the messages used on socket windows.
*/
#define SOCKET_MESSAGE WM_USER+1
#define SOCKET_SELECT WM_USER+2
#define SOCKET_TERMINATE WM_USER+3
|
| ︙ | | | ︙ | |
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
|
};
/*
*----------------------------------------------------------------------
*
* InitSockets --
*
* Initialize the socket module. Attempts to load the wsock32.dll library
* and set up the winSock function table. If successful, registers the
* event window for the socket notifier code.
*
* Assumes socketMutex is held.
*
* Results:
* None.
*
* Side effects:
* Dynamically loads wsock32.dll, and registers a new window class and
* creates a window for use in asynchronous socket notification.
*
*----------------------------------------------------------------------
*/
static void
InitSockets(void)
{
DWORD id;
WSADATA wsaData;
DWORD err;
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
TclThreadDataKeyGet(&dataKey);
if (!initialized) {
initialized = 1;
Tcl_CreateExitHandler(SocketExitHandler, (ClientData) NULL);
winSock.hModule = LoadLibraryA("wsock32.dll");
if (winSock.hModule == NULL) {
return;
}
/*
* Initialize the function table.
*/
winSock.accept = (LPFN_ACCEPT)
GetProcAddress(winSock.hModule, "accept");
winSock.bind = (LPFN_BIND)
GetProcAddress(winSock.hModule, "bind");
winSock.closesocket = (LPFN_CLOSESOCKET)
GetProcAddress(winSock.hModule, "closesocket");
winSock.connect = (LPFN_CONNECT)
GetProcAddress(winSock.hModule, "connect");
winSock.gethostbyaddr = (LPFN_GETHOSTBYADDR)
GetProcAddress(winSock.hModule, "gethostbyaddr");
winSock.gethostbyname = (LPFN_GETHOSTBYNAME)
GetProcAddress(winSock.hModule, "gethostbyname");
winSock.gethostname = (LPFN_GETHOSTNAME)
GetProcAddress(winSock.hModule, "gethostname");
winSock.getpeername = (LPFN_GETPEERNAME)
GetProcAddress(winSock.hModule, "getpeername");
winSock.getservbyname = (LPFN_GETSERVBYNAME)
GetProcAddress(winSock.hModule, "getservbyname");
winSock.getsockname = (LPFN_GETSOCKNAME)
GetProcAddress(winSock.hModule, "getsockname");
winSock.getsockopt = (LPFN_GETSOCKOPT)
GetProcAddress(winSock.hModule, "getsockopt");
winSock.htons = (LPFN_HTONS)
GetProcAddress(winSock.hModule, "htons");
winSock.inet_addr = (LPFN_INET_ADDR)
GetProcAddress(winSock.hModule, "inet_addr");
winSock.inet_ntoa = (LPFN_INET_NTOA)
GetProcAddress(winSock.hModule, "inet_ntoa");
winSock.ioctlsocket = (LPFN_IOCTLSOCKET)
GetProcAddress(winSock.hModule, "ioctlsocket");
winSock.listen = (LPFN_LISTEN)
GetProcAddress(winSock.hModule, "listen");
winSock.ntohs = (LPFN_NTOHS)
GetProcAddress(winSock.hModule, "ntohs");
winSock.recv = (LPFN_RECV)
GetProcAddress(winSock.hModule, "recv");
winSock.select = (LPFN_SELECT)
GetProcAddress(winSock.hModule, "select");
winSock.send = (LPFN_SEND)
GetProcAddress(winSock.hModule, "send");
winSock.setsockopt = (LPFN_SETSOCKOPT)
GetProcAddress(winSock.hModule, "setsockopt");
winSock.socket = (LPFN_SOCKET)
GetProcAddress(winSock.hModule, "socket");
winSock.WSAAsyncSelect = (LPFN_WSAASYNCSELECT)
GetProcAddress(winSock.hModule, "WSAAsyncSelect");
winSock.WSACleanup = (LPFN_WSACLEANUP)
GetProcAddress(winSock.hModule, "WSACleanup");
winSock.WSAGetLastError = (LPFN_WSAGETLASTERROR)
GetProcAddress(winSock.hModule, "WSAGetLastError");
winSock.WSAStartup = (LPFN_WSASTARTUP)
GetProcAddress(winSock.hModule, "WSAStartup");
/*
* Now check that all fields are properly initialized. If not, return
* zero to indicate that we failed to initialize properly.
*/
if ((winSock.accept == NULL) ||
(winSock.bind == NULL) ||
(winSock.closesocket == NULL) ||
(winSock.connect == NULL) ||
(winSock.gethostbyname == NULL) ||
(winSock.gethostbyaddr == NULL) ||
(winSock.gethostname == NULL) ||
(winSock.getpeername == NULL) ||
(winSock.getservbyname == NULL) ||
(winSock.getsockname == NULL) ||
(winSock.getsockopt == NULL) ||
(winSock.htons == NULL) ||
(winSock.inet_addr == NULL) ||
(winSock.inet_ntoa == NULL) ||
(winSock.ioctlsocket == NULL) ||
(winSock.listen == NULL) ||
(winSock.ntohs == NULL) ||
(winSock.recv == NULL) ||
(winSock.select == NULL) ||
(winSock.send == NULL) ||
(winSock.setsockopt == NULL) ||
(winSock.socket == NULL) ||
(winSock.WSAAsyncSelect == NULL) ||
(winSock.WSACleanup == NULL) ||
(winSock.WSAGetLastError == NULL) ||
(winSock.WSAStartup == NULL)) {
goto unloadLibrary;
}
/*
* Create the async notification window with a new class. We must
* create a new class to avoid a Windows 95 bug that causes us to get
* the wrong message number for socket events if the message window is
* a subclass of a static control.
*/
windowClass.style = 0;
windowClass.cbClsExtra = 0;
windowClass.cbWndExtra = 0;
windowClass.hInstance = TclWinGetTclInstance();
windowClass.hbrBackground = NULL;
windowClass.lpszMenuName = NULL;
windowClass.lpszClassName = "TclSocket";
windowClass.lpfnWndProc = SocketProc;
windowClass.hIcon = NULL;
windowClass.hCursor = NULL;
if (!RegisterClassA(&windowClass)) {
TclWinConvertError(GetLastError());
goto unloadLibrary;
}
/*
* Initialize the winsock library and check the interface version
* actually loaded. We only ask for the 1.1 interface and do require
* that it not be less than 1.1.
*/
#define WSA_VERSION_MAJOR 1
#define WSA_VERSION_MINOR 1
#define WSA_VERSION_REQD MAKEWORD(WSA_VERSION_MAJOR, WSA_VERSION_MINOR)
err = winSock.WSAStartup((WORD)WSA_VERSION_REQD, &wsaData);
if (err != 0) {
TclWinConvertWSAError(err);
goto unloadLibrary;
}
/*
* Note the byte positions are swapped for the comparison, so that
* 0x0002 (2.0, MAKEWORD(2,0)) doesn't look less than 0x0101 (1.1).
* We want the comparison to be 0x0200 < 0x0101.
*/
if (MAKEWORD(HIBYTE(wsaData.wVersion), LOBYTE(wsaData.wVersion))
< MAKEWORD(WSA_VERSION_MINOR, WSA_VERSION_MAJOR)) {
TclWinConvertWSAError(WSAVERNOTSUPPORTED);
winSock.WSACleanup();
goto unloadLibrary;
}
#undef WSA_VERSION_REQD
#undef WSA_VERSION_MAJOR
#undef WSA_VERSION_MINOR
}
/*
* Check for per-thread initialization.
*/
if (tsdPtr == NULL) {
tsdPtr = TCL_TSD_INIT(&dataKey);
tsdPtr->socketList = NULL;
tsdPtr->hwnd = NULL;
tsdPtr->threadId = Tcl_GetCurrentThread();
tsdPtr->readyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if (tsdPtr->readyEvent == NULL) {
goto unloadLibrary;
}
tsdPtr->socketListLock = CreateEvent(NULL, FALSE, TRUE, NULL);
if (tsdPtr->socketListLock == NULL) {
goto unloadLibrary;
}
tsdPtr->socketThread = CreateThread(NULL, 256, SocketThread, tsdPtr,
0, &id);
if (tsdPtr->socketThread == NULL) {
goto unloadLibrary;
}
SetThreadPriority(tsdPtr->socketThread, THREAD_PRIORITY_HIGHEST);
/*
* Wait for the thread to signal when the window has been created and
* if it is ready to go.
*/
WaitForSingleObject(tsdPtr->readyEvent, INFINITE);
if (tsdPtr->hwnd == NULL) {
goto unloadLibrary; /* Trouble creating the window */
}
Tcl_CreateEventSource(SocketSetupProc, SocketCheckProc, NULL);
}
return;
unloadLibrary:
TclpFinalizeSockets();
FreeLibrary(winSock.hModule);
winSock.hModule = NULL;
return;
}
/*
*----------------------------------------------------------------------
*
* SocketsEnabled --
*
* Check that the WinSock DLL is loaded and ready.
*
* Results:
* 1 if it is.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
/* ARGSUSED */
static int
SocketsEnabled(void)
{
int enabled;
Tcl_MutexLock(&socketMutex);
enabled = (winSock.hModule != NULL);
Tcl_MutexUnlock(&socketMutex);
return enabled;
}
/*
*----------------------------------------------------------------------
|
|
<
|
|
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
|
|
|
|
|
|
|
|
|
<
|
|
|
|
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
|
};
/*
*----------------------------------------------------------------------
*
* InitSockets --
*
* Initialize the socket module. If winsock startup is successful,
* registers the event window for the socket notifier code.
*
* Assumes socketMutex is held.
*
* Results:
* None.
*
* Side effects:
* Initializes winsock, registers a new window class and creates a
* window for use in asynchronous socket notification.
*
*----------------------------------------------------------------------
*/
static void
InitSockets(void)
{
DWORD id;
WSADATA wsaData;
DWORD err;
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
TclThreadDataKeyGet(&dataKey);
if (!initialized) {
initialized = 1;
Tcl_CreateExitHandler(SocketExitHandler, (ClientData) NULL);
/*
* Create the async notification window with a new class. We must
* create a new class to avoid a Windows 95 bug that causes us to get
* the wrong message number for socket events if the message window is
* a subclass of a static control.
*/
windowClass.style = 0;
windowClass.cbClsExtra = 0;
windowClass.cbWndExtra = 0;
windowClass.hInstance = TclWinGetTclInstance();
windowClass.hbrBackground = NULL;
windowClass.lpszMenuName = NULL;
windowClass.lpszClassName = "TclSocket";
windowClass.lpfnWndProc = SocketProc;
windowClass.hIcon = NULL;
windowClass.hCursor = NULL;
if (!RegisterClassA(&windowClass)) {
TclWinConvertError(GetLastError());
goto initFailure;
}
/*
* Initialize the winsock library and check the interface version
* actually loaded. We only ask for the 1.1 interface and do require
* that it not be less than 1.1.
*/
#define WSA_VERSION_MAJOR 1
#define WSA_VERSION_MINOR 1
#define WSA_VERSION_REQD MAKEWORD(WSA_VERSION_MAJOR, WSA_VERSION_MINOR)
err = WSAStartup((WORD)WSA_VERSION_REQD, &wsaData);
if (err != 0) {
TclWinConvertWSAError(err);
goto initFailure;
}
/*
* Note the byte positions are swapped for the comparison, so that
* 0x0002 (2.0, MAKEWORD(2,0)) doesn't look less than 0x0101 (1.1).
* We want the comparison to be 0x0200 < 0x0101.
*/
if (MAKEWORD(HIBYTE(wsaData.wVersion), LOBYTE(wsaData.wVersion))
< MAKEWORD(WSA_VERSION_MINOR, WSA_VERSION_MAJOR)) {
TclWinConvertWSAError(WSAVERNOTSUPPORTED);
WSACleanup();
goto initFailure;
}
#undef WSA_VERSION_REQD
#undef WSA_VERSION_MAJOR
#undef WSA_VERSION_MINOR
}
/*
* Check for per-thread initialization.
*/
if (tsdPtr == NULL) {
tsdPtr = TCL_TSD_INIT(&dataKey);
tsdPtr->socketList = NULL;
tsdPtr->hwnd = NULL;
tsdPtr->threadId = Tcl_GetCurrentThread();
tsdPtr->readyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if (tsdPtr->readyEvent == NULL) {
goto initFailure;
}
tsdPtr->socketListLock = CreateEvent(NULL, FALSE, TRUE, NULL);
if (tsdPtr->socketListLock == NULL) {
goto initFailure;
}
tsdPtr->socketThread = CreateThread(NULL, 256, SocketThread, tsdPtr,
0, &id);
if (tsdPtr->socketThread == NULL) {
goto initFailure;
}
SetThreadPriority(tsdPtr->socketThread, THREAD_PRIORITY_HIGHEST);
/*
* Wait for the thread to signal when the window has been created and
* if it is ready to go.
*/
WaitForSingleObject(tsdPtr->readyEvent, INFINITE);
if (tsdPtr->hwnd == NULL) {
goto initFailure; /* Trouble creating the window */
}
Tcl_CreateEventSource(SocketSetupProc, SocketCheckProc, NULL);
}
return;
initFailure:
TclpFinalizeSockets();
initialized = -1;
return;
}
/*
*----------------------------------------------------------------------
*
* SocketsEnabled --
*
* Check that the WinSock was successfully initialized.
*
* Results:
* 1 if it is.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
/* ARGSUSED */
static int
SocketsEnabled(void)
{
int enabled;
Tcl_MutexLock(&socketMutex);
enabled = (initialized == 1);
Tcl_MutexUnlock(&socketMutex);
return enabled;
}
/*
*----------------------------------------------------------------------
|
| ︙ | | | ︙ | |
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
|
/* ARGSUSED */
static void
SocketExitHandler(
ClientData clientData) /* Not used. */
{
Tcl_MutexLock(&socketMutex);
if (winSock.hModule) {
/*
* Make sure the socket event handling window is cleaned-up for, at
* most, this thread.
*/
TclpFinalizeSockets();
UnregisterClass("TclSocket", TclWinGetTclInstance());
winSock.WSACleanup();
FreeLibrary(winSock.hModule);
winSock.hModule = NULL;
}
initialized = 0;
Tcl_MutexUnlock(&socketMutex);
}
/*
*----------------------------------------------------------------------
*
|
<
<
|
|
|
|
|
|
|
<
<
<
|
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
|
/* ARGSUSED */
static void
SocketExitHandler(
ClientData clientData) /* Not used. */
{
Tcl_MutexLock(&socketMutex);
/*
* Make sure the socket event handling window is cleaned-up for, at
* most, this thread.
*/
TclpFinalizeSockets();
UnregisterClass("TclSocket", TclWinGetTclInstance());
WSACleanup();
initialized = 0;
Tcl_MutexUnlock(&socketMutex);
}
/*
*----------------------------------------------------------------------
*
|
| ︙ | | | ︙ | |
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
|
(WPARAM) UNSELECT, (LPARAM) infoPtr);
FD_ZERO(&readFds);
FD_SET(infoPtr->socket, &readFds);
timeout.tv_usec = 0;
timeout.tv_sec = 0;
if (winSock.select(0, &readFds, NULL, NULL, &timeout) != 0) {
mask |= TCL_READABLE;
} else {
infoPtr->readyEvents &= ~(FD_READ);
SendMessage(tsdPtr->hwnd, SOCKET_SELECT,
(WPARAM) SELECT, (LPARAM) infoPtr);
}
}
|
|
|
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
|
(WPARAM) UNSELECT, (LPARAM) infoPtr);
FD_ZERO(&readFds);
FD_SET(infoPtr->socket, &readFds);
timeout.tv_usec = 0;
timeout.tv_sec = 0;
if (select(0, &readFds, NULL, NULL, &timeout) != 0) {
mask |= TCL_READABLE;
} else {
infoPtr->readyEvents &= ~(FD_READ);
SendMessage(tsdPtr->hwnd, SOCKET_SELECT,
(WPARAM) SELECT, (LPARAM) infoPtr);
}
}
|
| ︙ | | | ︙ | |
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
|
if (SocketsEnabled()) {
/*
* Clean up the OS socket handle. The default Windows setting for a
* socket is SO_DONTLINGER, which does a graceful shutdown in the
* background.
*/
if (winSock.closesocket(infoPtr->socket) == SOCKET_ERROR) {
TclWinConvertWSAError((DWORD) winSock.WSAGetLastError());
errorCode = Tcl_GetErrno();
}
}
/*
* TIP #218. Removed the code removing the structure from the global
* socket list. This is now done by the thread action callbacks, and only
|
|
|
|
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
|
if (SocketsEnabled()) {
/*
* Clean up the OS socket handle. The default Windows setting for a
* socket is SO_DONTLINGER, which does a graceful shutdown in the
* background.
*/
if (closesocket(infoPtr->socket) == SOCKET_ERROR) {
TclWinConvertWSAError((DWORD) WSAGetLastError());
errorCode = Tcl_GetErrno();
}
}
/*
* TIP #218. Removed the code removing the structure from the global
* socket list. This is now done by the thread action callbacks, and only
|
| ︙ | | | ︙ | |
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
|
goto error;
}
if ((myaddr != NULL || myport != 0) &&
!CreateSocketAddress(&mysockaddr, myaddr, myport)) {
goto error;
}
sock = winSock.socket(AF_INET, SOCK_STREAM, 0);
if (sock == INVALID_SOCKET) {
goto error;
}
/*
* Win-NT has a misfeature that sockets are inherited in child processes
* by default. Turn off the inherit bit.
|
|
|
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
|
goto error;
}
if ((myaddr != NULL || myport != 0) &&
!CreateSocketAddress(&mysockaddr, myaddr, myport)) {
goto error;
}
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == INVALID_SOCKET) {
goto error;
}
/*
* Win-NT has a misfeature that sockets are inherited in child processes
* by default. Turn off the inherit bit.
|
| ︙ | | | ︙ | |
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
|
* even if they are still in use.
*
* Bind should not be affected by the socket having already been set
* into nonblocking mode. If there is trouble, this is one place to
* look for bugs.
*/
if (winSock.bind(sock, (SOCKADDR *) &sockaddr,
sizeof(SOCKADDR_IN)) == SOCKET_ERROR) {
goto error;
}
/*
* Set the maximum number of pending connect requests to the max value
* allowed on each platform (Win32 and Win32s may be different, and
* there may be differences between TCP/IP stacks).
*/
if (winSock.listen(sock, SOMAXCONN) == SOCKET_ERROR) {
goto error;
}
/*
* Add this socket to the global list of sockets.
*/
|
|
|
|
|
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
|
* even if they are still in use.
*
* Bind should not be affected by the socket having already been set
* into nonblocking mode. If there is trouble, this is one place to
* look for bugs.
*/
if (bind(sock, (SOCKADDR *) &sockaddr, sizeof(SOCKADDR_IN))
== SOCKET_ERROR) {
goto error;
}
/*
* Set the maximum number of pending connect requests to the max value
* allowed on each platform (Win32 and Win32s may be different, and
* there may be differences between TCP/IP stacks).
*/
if (listen(sock, SOMAXCONN) == SOCKET_ERROR) {
goto error;
}
/*
* Add this socket to the global list of sockets.
*/
|
| ︙ | | | ︙ | |
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
|
} else {
/*
* Try to bind to a local port, if specified.
*/
if (myaddr != NULL || myport != 0) {
if (winSock.bind(sock, (SOCKADDR *) &mysockaddr,
sizeof(SOCKADDR_IN)) == SOCKET_ERROR) {
goto error;
}
}
/*
* Set the socket into nonblocking mode if the connect should be done
* in the background.
*/
if (async) {
if (winSock.ioctlsocket(sock, (long) FIONBIO,
&flag) == SOCKET_ERROR) {
goto error;
}
}
/*
* Attempt to connect to the remote socket.
*/
if (winSock.connect(sock, (SOCKADDR *) &sockaddr,
sizeof(SOCKADDR_IN)) == SOCKET_ERROR) {
TclWinConvertWSAError((DWORD) winSock.WSAGetLastError());
if (Tcl_GetErrno() != EWOULDBLOCK) {
goto error;
}
/*
* The connection is progressing in the background.
*/
|
|
|
|
<
|
|
|
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
|
} else {
/*
* Try to bind to a local port, if specified.
*/
if (myaddr != NULL || myport != 0) {
if (bind(sock, (SOCKADDR *) &mysockaddr, sizeof(SOCKADDR_IN))
== SOCKET_ERROR) {
goto error;
}
}
/*
* Set the socket into nonblocking mode if the connect should be done
* in the background.
*/
if (async) {
if (ioctlsocket(sock, (long) FIONBIO, &flag) == SOCKET_ERROR) {
goto error;
}
}
/*
* Attempt to connect to the remote socket.
*/
if (connect(sock, (SOCKADDR *) &sockaddr,
sizeof(SOCKADDR_IN)) == SOCKET_ERROR) {
TclWinConvertWSAError((DWORD) WSAGetLastError());
if (Tcl_GetErrno() != EWOULDBLOCK) {
goto error;
}
/*
* The connection is progressing in the background.
*/
|
| ︙ | | | ︙ | |
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
|
}
/*
* Register for interest in events in the select mask. Note that this
* automatically places the socket into non-blocking mode.
*/
winSock.ioctlsocket(sock, (long) FIONBIO, &flag);
SendMessage(tsdPtr->hwnd, SOCKET_SELECT,
(WPARAM) SELECT, (LPARAM) infoPtr);
return infoPtr;
error:
TclWinConvertWSAError((DWORD) winSock.WSAGetLastError());
if (interp != NULL) {
Tcl_AppendResult(interp, "couldn't open socket: ",
Tcl_PosixError(interp), NULL);
}
if (sock != INVALID_SOCKET) {
winSock.closesocket(sock);
}
return NULL;
}
/*
*----------------------------------------------------------------------
*
|
|
|
<
|
|
|
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
|
}
/*
* Register for interest in events in the select mask. Note that this
* automatically places the socket into non-blocking mode.
*/
ioctlsocket(sock, (long) FIONBIO, &flag);
SendMessage(tsdPtr->hwnd, SOCKET_SELECT, (WPARAM) SELECT, (LPARAM) infoPtr);
return infoPtr;
error:
TclWinConvertWSAError((DWORD) WSAGetLastError());
if (interp != NULL) {
Tcl_AppendResult(interp, "couldn't open socket: ",
Tcl_PosixError(interp), NULL);
}
if (sock != INVALID_SOCKET) {
closesocket(sock);
}
return NULL;
}
/*
*----------------------------------------------------------------------
*
|
| ︙ | | | ︙ | |
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
|
if (!SocketsEnabled()) {
Tcl_SetErrno(EFAULT);
return 0;
}
ZeroMemory(sockaddrPtr, sizeof(SOCKADDR_IN));
sockaddrPtr->sin_family = AF_INET;
sockaddrPtr->sin_port = winSock.htons((u_short) (port & 0xFFFF));
if (host == NULL) {
addr.s_addr = INADDR_ANY;
} else {
addr.s_addr = winSock.inet_addr(host);
if (addr.s_addr == INADDR_NONE) {
hostent = winSock.gethostbyname(host);
if (hostent != NULL) {
memcpy(&addr, hostent->h_addr, (size_t) hostent->h_length);
} else {
#ifdef EHOSTUNREACH
Tcl_SetErrno(EHOSTUNREACH);
#else
#ifdef ENXIO
|
|
|
|
|
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
|
if (!SocketsEnabled()) {
Tcl_SetErrno(EFAULT);
return 0;
}
ZeroMemory(sockaddrPtr, sizeof(SOCKADDR_IN));
sockaddrPtr->sin_family = AF_INET;
sockaddrPtr->sin_port = htons((u_short) (port & 0xFFFF));
if (host == NULL) {
addr.s_addr = INADDR_ANY;
} else {
addr.s_addr = inet_addr(host);
if (addr.s_addr == INADDR_NONE) {
hostent = gethostbyname(host);
if (hostent != NULL) {
memcpy(&addr, hostent->h_addr, (size_t) hostent->h_length);
} else {
#ifdef EHOSTUNREACH
Tcl_SetErrno(EHOSTUNREACH);
#else
#ifdef ENXIO
|
| ︙ | | | ︙ | |
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
|
/*
* Accept the incoming connection request.
*/
len = sizeof(SOCKADDR_IN);
newSocket = winSock.accept(infoPtr->socket, (SOCKADDR *)&addr,
&len);
/*
* Clear the ready mask so we can detect the next connection request. Note
* that connection requests are level triggered, so if there is a request
* already pending, a new event will be generated.
*/
|
|
|
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
|
/*
* Accept the incoming connection request.
*/
len = sizeof(SOCKADDR_IN);
newSocket = accept(infoPtr->socket, (SOCKADDR *)&addr,
&len);
/*
* Clear the ready mask so we can detect the next connection request. Note
* that connection requests are level triggered, so if there is a request
* already pending, a new event will be generated.
*/
|
| ︙ | | | ︙ | |
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
|
/*
* Invoke the accept callback function.
*/
if (infoPtr->acceptProc != NULL) {
(infoPtr->acceptProc) (infoPtr->acceptProcData, newInfoPtr->channel,
winSock.inet_ntoa(addr.sin_addr),
winSock.ntohs(addr.sin_port));
}
}
/*
*----------------------------------------------------------------------
*
* TcpInputProc --
|
|
<
|
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
|
/*
* Invoke the accept callback function.
*/
if (infoPtr->acceptProc != NULL) {
(infoPtr->acceptProc) (infoPtr->acceptProcData, newInfoPtr->channel,
inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
}
}
/*
*----------------------------------------------------------------------
*
* TcpInputProc --
|
| ︙ | | | ︙ | |
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
|
* read. We have to simulate blocking behavior here since we are always
* using non-blocking sockets.
*/
while (1) {
SendMessage(tsdPtr->hwnd, SOCKET_SELECT,
(WPARAM) UNSELECT, (LPARAM) infoPtr);
bytesRead = winSock.recv(infoPtr->socket, buf, toRead, 0);
infoPtr->readyEvents &= ~(FD_READ);
/*
* Check for end-of-file condition or successful read.
*/
if (bytesRead == 0) {
|
|
|
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
|
* read. We have to simulate blocking behavior here since we are always
* using non-blocking sockets.
*/
while (1) {
SendMessage(tsdPtr->hwnd, SOCKET_SELECT,
(WPARAM) UNSELECT, (LPARAM) infoPtr);
bytesRead = recv(infoPtr->socket, buf, toRead, 0);
infoPtr->readyEvents &= ~(FD_READ);
/*
* Check for end-of-file condition or successful read.
*/
if (bytesRead == 0) {
|
| ︙ | | | ︙ | |
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
|
break;
}
/*
* Check for error condition or underflow in non-blocking case.
*/
error = winSock.WSAGetLastError();
if ((infoPtr->flags & SOCKET_ASYNC) || (error != WSAEWOULDBLOCK)) {
TclWinConvertWSAError(error);
*errorCodePtr = Tcl_GetErrno();
bytesRead = -1;
break;
}
|
|
|
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
|
break;
}
/*
* Check for error condition or underflow in non-blocking case.
*/
error = WSAGetLastError();
if ((infoPtr->flags & SOCKET_ASYNC) || (error != WSAEWOULDBLOCK)) {
TclWinConvertWSAError(error);
*errorCodePtr = Tcl_GetErrno();
bytesRead = -1;
break;
}
|
| ︙ | | | ︙ | |
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
|
return -1;
}
while (1) {
SendMessage(tsdPtr->hwnd, SOCKET_SELECT,
(WPARAM) UNSELECT, (LPARAM) infoPtr);
bytesWritten = winSock.send(infoPtr->socket, buf, toWrite, 0);
if (bytesWritten != SOCKET_ERROR) {
/*
* Since Windows won't generate a new write event until we hit an
* overflow condition, we need to force the event loop to poll
* until the condition changes.
*/
|
|
|
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
|
return -1;
}
while (1) {
SendMessage(tsdPtr->hwnd, SOCKET_SELECT,
(WPARAM) UNSELECT, (LPARAM) infoPtr);
bytesWritten = send(infoPtr->socket, buf, toWrite, 0);
if (bytesWritten != SOCKET_ERROR) {
/*
* Since Windows won't generate a new write event until we hit an
* overflow condition, we need to force the event loop to poll
* until the condition changes.
*/
|
| ︙ | | | ︙ | |
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
|
/*
* Check for error condition or overflow. In the event of overflow, we
* need to clear the FD_WRITE flag so we can detect the next writable
* event. Note that Windows only sends a new writable event after a
* send fails with WSAEWOULDBLOCK.
*/
error = winSock.WSAGetLastError();
if (error == WSAEWOULDBLOCK) {
infoPtr->readyEvents &= ~(FD_WRITE);
if (infoPtr->flags & SOCKET_ASYNC) {
*errorCodePtr = EWOULDBLOCK;
bytesWritten = -1;
break;
}
|
|
|
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
|
/*
* Check for error condition or overflow. In the event of overflow, we
* need to clear the FD_WRITE flag so we can detect the next writable
* event. Note that Windows only sends a new writable event after a
* send fails with WSAEWOULDBLOCK.
*/
error = WSAGetLastError();
if (error == WSAEWOULDBLOCK) {
infoPtr->readyEvents &= ~(FD_WRITE);
if (infoPtr->flags & SOCKET_ASYNC) {
*errorCodePtr = EWOULDBLOCK;
bytesWritten = -1;
break;
}
|
| ︙ | | | ︙ | |
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
|
if (Tcl_GetBoolean(interp, value, &boolVar) != TCL_OK) {
return TCL_ERROR;
}
if (boolVar) {
val = TRUE;
}
rtn = winSock.setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
(const char *) &val, sizeof(BOOL));
if (rtn != 0) {
TclWinConvertWSAError(winSock.WSAGetLastError());
if (interp) {
Tcl_AppendResult(interp, "couldn't set socket option: ",
Tcl_PosixError(interp), NULL);
}
return TCL_ERROR;
}
return TCL_OK;
} else if (!stricmp(optionName, "-nagle")) {
BOOL val = FALSE;
int boolVar, rtn;
if (Tcl_GetBoolean(interp, value, &boolVar) != TCL_OK) {
return TCL_ERROR;
}
if (!boolVar) {
val = TRUE;
}
rtn = winSock.setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
(const char *) &val, sizeof(BOOL));
if (rtn != 0) {
TclWinConvertWSAError(winSock.WSAGetLastError());
if (interp) {
Tcl_AppendResult(interp, "couldn't set socket option: ",
Tcl_PosixError(interp), NULL);
}
return TCL_ERROR;
}
return TCL_OK;
|
|
|
|
|
|
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
|
if (Tcl_GetBoolean(interp, value, &boolVar) != TCL_OK) {
return TCL_ERROR;
}
if (boolVar) {
val = TRUE;
}
rtn = setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
(const char *) &val, sizeof(BOOL));
if (rtn != 0) {
TclWinConvertWSAError(WSAGetLastError());
if (interp) {
Tcl_AppendResult(interp, "couldn't set socket option: ",
Tcl_PosixError(interp), NULL);
}
return TCL_ERROR;
}
return TCL_OK;
} else if (!stricmp(optionName, "-nagle")) {
BOOL val = FALSE;
int boolVar, rtn;
if (Tcl_GetBoolean(interp, value, &boolVar) != TCL_OK) {
return TCL_ERROR;
}
if (!boolVar) {
val = TRUE;
}
rtn = setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
(const char *) &val, sizeof(BOOL));
if (rtn != 0) {
TclWinConvertWSAError(WSAGetLastError());
if (interp) {
Tcl_AppendResult(interp, "couldn't set socket option: ",
Tcl_PosixError(interp), NULL);
}
return TCL_ERROR;
}
return TCL_OK;
|
| ︙ | | | ︙ | |
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
|
DWORD err;
int ret;
optlen = sizeof(int);
ret = TclWinGetSockOpt((int)sock, SOL_SOCKET, SO_ERROR,
(char *)&err, &optlen);
if (ret == SOCKET_ERROR) {
err = winSock.WSAGetLastError();
}
if (err) {
TclWinConvertWSAError(err);
Tcl_DStringAppend(dsPtr, Tcl_ErrnoMsg(Tcl_GetErrno()), -1);
}
return TCL_OK;
}
if ((len == 0) || ((len > 1) && (optionName[1] == 'p') &&
(strncmp(optionName, "-peername", len) == 0))) {
if (winSock.getpeername(sock, (LPSOCKADDR) &peername, &size) == 0) {
if (len == 0) {
Tcl_DStringAppendElement(dsPtr, "-peername");
Tcl_DStringStartSublist(dsPtr);
}
Tcl_DStringAppendElement(dsPtr,
winSock.inet_ntoa(peername.sin_addr));
if (peername.sin_addr.s_addr == 0) {
hostEntPtr = NULL;
} else {
hostEntPtr = winSock.gethostbyaddr(
(char *) &(peername.sin_addr),
sizeof(peername.sin_addr), AF_INET);
}
if (hostEntPtr != NULL) {
Tcl_DStringAppendElement(dsPtr, hostEntPtr->h_name);
} else {
Tcl_DStringAppendElement(dsPtr,
winSock.inet_ntoa(peername.sin_addr));
}
TclFormatInt(buf, winSock.ntohs(peername.sin_port));
Tcl_DStringAppendElement(dsPtr, buf);
if (len == 0) {
Tcl_DStringEndSublist(dsPtr);
} else {
return TCL_OK;
}
} else {
/*
* getpeername failed - but if we were asked for all the options
* (len==0), don't flag an error at that point because it could be
* an fconfigure request on a server socket (such sockets have no
* peer). {Copied from unix/tclUnixChan.c}
*/
if (len) {
TclWinConvertWSAError((DWORD) winSock.WSAGetLastError());
if (interp) {
Tcl_AppendResult(interp, "can't get peername: ",
Tcl_PosixError(interp), NULL);
}
return TCL_ERROR;
}
}
}
if ((len == 0) || ((len > 1) && (optionName[1] == 's') &&
(strncmp(optionName, "-sockname", len) == 0))) {
if (winSock.getsockname(sock, (LPSOCKADDR) &sockname, &size) == 0) {
if (len == 0) {
Tcl_DStringAppendElement(dsPtr, "-sockname");
Tcl_DStringStartSublist(dsPtr);
}
Tcl_DStringAppendElement(dsPtr,
winSock.inet_ntoa(sockname.sin_addr));
if (sockname.sin_addr.s_addr == 0) {
hostEntPtr = NULL;
} else {
hostEntPtr = winSock.gethostbyaddr(
(char *) &(sockname.sin_addr),
sizeof(peername.sin_addr), AF_INET);
}
if (hostEntPtr != NULL) {
Tcl_DStringAppendElement(dsPtr, hostEntPtr->h_name);
} else {
Tcl_DStringAppendElement(dsPtr,
winSock.inet_ntoa(sockname.sin_addr));
}
TclFormatInt(buf, winSock.ntohs(sockname.sin_port));
Tcl_DStringAppendElement(dsPtr, buf);
if (len == 0) {
Tcl_DStringEndSublist(dsPtr);
} else {
return TCL_OK;
}
} else {
if (interp) {
TclWinConvertWSAError((DWORD) winSock.WSAGetLastError());
Tcl_AppendResult(interp, "can't get sockname: ",
Tcl_PosixError(interp), NULL);
}
return TCL_ERROR;
}
}
#ifdef TCL_FEATURE_KEEPALIVE_NAGLE
if (len == 0 || !strncmp(optionName, "-keepalive", len)) {
int optlen;
BOOL opt = FALSE;
if (len == 0) {
Tcl_DStringAppendElement(dsPtr, "-keepalive");
}
optlen = sizeof(BOOL);
winSock.getsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char *)&opt,
&optlen);
if (opt) {
Tcl_DStringAppendElement(dsPtr, "1");
} else {
Tcl_DStringAppendElement(dsPtr, "0");
}
if (len > 0) {
return TCL_OK;
}
}
if (len == 0 || !strncmp(optionName, "-nagle", len)) {
int optlen;
BOOL opt = FALSE;
if (len == 0) {
Tcl_DStringAppendElement(dsPtr, "-nagle");
}
optlen = sizeof(BOOL);
winSock.getsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *)&opt,
&optlen);
if (opt) {
Tcl_DStringAppendElement(dsPtr, "0");
} else {
Tcl_DStringAppendElement(dsPtr, "1");
}
if (len > 0) {
|
|
|
|
<
<
|
|
<
|
|
|
|
<
<
|
|
<
|
|
|
<
|
|
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
|
DWORD err;
int ret;
optlen = sizeof(int);
ret = TclWinGetSockOpt((int)sock, SOL_SOCKET, SO_ERROR,
(char *)&err, &optlen);
if (ret == SOCKET_ERROR) {
err = WSAGetLastError();
}
if (err) {
TclWinConvertWSAError(err);
Tcl_DStringAppend(dsPtr, Tcl_ErrnoMsg(Tcl_GetErrno()), -1);
}
return TCL_OK;
}
if ((len == 0) || ((len > 1) && (optionName[1] == 'p') &&
(strncmp(optionName, "-peername", len) == 0))) {
if (getpeername(sock, (LPSOCKADDR) &peername, &size) == 0) {
if (len == 0) {
Tcl_DStringAppendElement(dsPtr, "-peername");
Tcl_DStringStartSublist(dsPtr);
}
Tcl_DStringAppendElement(dsPtr, inet_ntoa(peername.sin_addr));
if (peername.sin_addr.s_addr == 0) {
hostEntPtr = NULL;
} else {
hostEntPtr = gethostbyaddr((char *) &(peername.sin_addr),
sizeof(peername.sin_addr), AF_INET);
}
if (hostEntPtr != NULL) {
Tcl_DStringAppendElement(dsPtr, hostEntPtr->h_name);
} else {
Tcl_DStringAppendElement(dsPtr, inet_ntoa(peername.sin_addr));
}
TclFormatInt(buf, ntohs(peername.sin_port));
Tcl_DStringAppendElement(dsPtr, buf);
if (len == 0) {
Tcl_DStringEndSublist(dsPtr);
} else {
return TCL_OK;
}
} else {
/*
* getpeername failed - but if we were asked for all the options
* (len==0), don't flag an error at that point because it could be
* an fconfigure request on a server socket (such sockets have no
* peer). {Copied from unix/tclUnixChan.c}
*/
if (len) {
TclWinConvertWSAError((DWORD) WSAGetLastError());
if (interp) {
Tcl_AppendResult(interp, "can't get peername: ",
Tcl_PosixError(interp), NULL);
}
return TCL_ERROR;
}
}
}
if ((len == 0) || ((len > 1) && (optionName[1] == 's') &&
(strncmp(optionName, "-sockname", len) == 0))) {
if (getsockname(sock, (LPSOCKADDR) &sockname, &size) == 0) {
if (len == 0) {
Tcl_DStringAppendElement(dsPtr, "-sockname");
Tcl_DStringStartSublist(dsPtr);
}
Tcl_DStringAppendElement(dsPtr, inet_ntoa(sockname.sin_addr));
if (sockname.sin_addr.s_addr == 0) {
hostEntPtr = NULL;
} else {
hostEntPtr = gethostbyaddr((char *) &(sockname.sin_addr),
sizeof(peername.sin_addr), AF_INET);
}
if (hostEntPtr != NULL) {
Tcl_DStringAppendElement(dsPtr, hostEntPtr->h_name);
} else {
Tcl_DStringAppendElement(dsPtr, inet_ntoa(sockname.sin_addr));
}
TclFormatInt(buf, ntohs(sockname.sin_port));
Tcl_DStringAppendElement(dsPtr, buf);
if (len == 0) {
Tcl_DStringEndSublist(dsPtr);
} else {
return TCL_OK;
}
} else {
if (interp) {
TclWinConvertWSAError((DWORD) WSAGetLastError());
Tcl_AppendResult(interp, "can't get sockname: ",
Tcl_PosixError(interp), NULL);
}
return TCL_ERROR;
}
}
#ifdef TCL_FEATURE_KEEPALIVE_NAGLE
if (len == 0 || !strncmp(optionName, "-keepalive", len)) {
int optlen;
BOOL opt = FALSE;
if (len == 0) {
Tcl_DStringAppendElement(dsPtr, "-keepalive");
}
optlen = sizeof(BOOL);
getsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char *)&opt, &optlen);
if (opt) {
Tcl_DStringAppendElement(dsPtr, "1");
} else {
Tcl_DStringAppendElement(dsPtr, "0");
}
if (len > 0) {
return TCL_OK;
}
}
if (len == 0 || !strncmp(optionName, "-nagle", len)) {
int optlen;
BOOL opt = FALSE;
if (len == 0) {
Tcl_DStringAppendElement(dsPtr, "-nagle");
}
optlen = sizeof(BOOL);
getsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *)&opt,
&optlen);
if (opt) {
Tcl_DStringAppendElement(dsPtr, "0");
} else {
Tcl_DStringAppendElement(dsPtr, "1");
}
if (len > 0) {
|
| ︙ | | | ︙ | |
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
|
}
SetEvent(tsdPtr->socketListLock);
break;
case SOCKET_SELECT:
infoPtr = (SocketInfo *) lParam;
if (wParam == SELECT) {
winSock.WSAAsyncSelect(infoPtr->socket, hwnd,
SOCKET_MESSAGE, infoPtr->selectEvents);
} else {
/*
* Clear the selection mask
*/
winSock.WSAAsyncSelect(infoPtr->socket, hwnd, 0, 0);
}
break;
case SOCKET_TERMINATE:
DestroyWindow(hwnd);
break;
}
|
|
|
|
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
|
}
SetEvent(tsdPtr->socketListLock);
break;
case SOCKET_SELECT:
infoPtr = (SocketInfo *) lParam;
if (wParam == SELECT) {
WSAAsyncSelect(infoPtr->socket, hwnd,
SOCKET_MESSAGE, infoPtr->selectEvents);
} else {
/*
* Clear the selection mask
*/
WSAAsyncSelect(infoPtr->socket, hwnd, 0, 0);
}
break;
case SOCKET_TERMINATE:
DestroyWindow(hwnd);
break;
}
|
| ︙ | | | ︙ | |
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
|
* Buffer length of 255 copied slavishly from previous version of
* this routine. Presumably there's a more "correct" macro value
* for a properly sized buffer for a gethostname() call.
* Maintainers are welcome to supply it.
*/
Tcl_DString inDs;
Tcl_DStringInit(&inDs);
Tcl_DStringSetLength(&inDs, 255);
if (winSock.gethostname(Tcl_DStringValue(&inDs),
Tcl_DStringLength(&inDs)) == 0) {
Tcl_DStringSetLength(&ds, 0);
} else {
Tcl_ExternalToUtfDString(NULL,
Tcl_DStringValue(&inDs), -1, &ds);
}
Tcl_DStringFree(&inDs);
}
|
|
|
|
|
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
|
* Buffer length of 255 copied slavishly from previous version of
* this routine. Presumably there's a more "correct" macro value
* for a properly sized buffer for a gethostname() call.
* Maintainers are welcome to supply it.
*/
Tcl_DString inDs;
Tcl_DStringInit(&inDs);
Tcl_DStringSetLength(&inDs, 255);
if (gethostname(Tcl_DStringValue(&inDs),
Tcl_DStringLength(&inDs)) == 0) {
Tcl_DStringSetLength(&ds, 0);
} else {
Tcl_ExternalToUtfDString(NULL,
Tcl_DStringValue(&inDs), -1, &ds);
}
Tcl_DStringFree(&inDs);
}
|
| ︙ | | | ︙ | |
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
|
* WinSock ran before other exit handlers that want to use sockets.
*/
if (!SocketsEnabled()) {
return SOCKET_ERROR;
}
return winSock.getsockopt((SOCKET)s, level, optname, optval, optlen);
}
int
TclWinSetSockOpt(
int s,
int level,
int optname,
const char * optval,
int optlen)
{
/*
* Check that WinSock is initialized; do not call it if not, to prevent
* system crashes. This can happen at exit time if the exit handler for
* WinSock ran before other exit handlers that want to use sockets.
*/
if (!SocketsEnabled()) {
return SOCKET_ERROR;
}
return winSock.setsockopt((SOCKET)s, level, optname, optval, optlen);
}
u_short
TclWinNToHS(
u_short netshort)
{
/*
* Check that WinSock is initialized; do not call it if not, to prevent
* system crashes. This can happen at exit time if the exit handler for
* WinSock ran before other exit handlers that want to use sockets.
*/
if (!SocketsEnabled()) {
return (u_short) -1;
}
return winSock.ntohs(netshort);
}
struct servent *
TclWinGetServByName(
const char *name,
const char *proto)
{
/*
* Check that WinSock is initialized; do not call it if not, to prevent
* system crashes. This can happen at exit time if the exit handler for
* WinSock ran before other exit handlers that want to use sockets.
*/
if (!SocketsEnabled()) {
return NULL;
}
return winSock.getservbyname(name, proto);
}
/*
*----------------------------------------------------------------------
*
* TcpThreadActionProc --
*
|
|
|
|
|
|
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
|
* WinSock ran before other exit handlers that want to use sockets.
*/
if (!SocketsEnabled()) {
return SOCKET_ERROR;
}
return getsockopt((SOCKET)s, level, optname, optval, optlen);
}
int
TclWinSetSockOpt(
int s,
int level,
int optname,
const char * optval,
int optlen)
{
/*
* Check that WinSock is initialized; do not call it if not, to prevent
* system crashes. This can happen at exit time if the exit handler for
* WinSock ran before other exit handlers that want to use sockets.
*/
if (!SocketsEnabled()) {
return SOCKET_ERROR;
}
return setsockopt((SOCKET)s, level, optname, optval, optlen);
}
u_short
TclWinNToHS(
u_short netshort)
{
/*
* Check that WinSock is initialized; do not call it if not, to prevent
* system crashes. This can happen at exit time if the exit handler for
* WinSock ran before other exit handlers that want to use sockets.
*/
if (!SocketsEnabled()) {
return (u_short) -1;
}
return ntohs(netshort);
}
struct servent *
TclWinGetServByName(
const char *name,
const char *proto)
{
/*
* Check that WinSock is initialized; do not call it if not, to prevent
* system crashes. This can happen at exit time if the exit handler for
* WinSock ran before other exit handlers that want to use sockets.
*/
if (!SocketsEnabled()) {
return NULL;
}
return getservbyname(name, proto);
}
/*
*----------------------------------------------------------------------
*
* TcpThreadActionProc --
*
|
| ︙ | | | ︙ | |