1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
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
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
/*
* 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.62 2008/02/22 11:50:54 patthoyts Exp $
*
* -----------------------------------------------------------------------
*
* General information on how this module works.
*
* - Each Tcl-thread with its sockets maintains an internal window to receive
* socket messages from the OS.
*
* - To ensure that message reception is always running this window is
* actually owned and handled by an internal thread. This we call the
* co-thread of Tcl's thread.
*
* - The whole structure is set up by InitSockets() which is called for each
* Tcl thread. The implementation of the co-thread is in SocketThread(),
* and the messages are handled by SocketProc(). The connection between
* both is not directly visible, it is done through a Win32 window class.
* This class is initialized by InitSockets() as well, and used in the
* creation of the message receiver windows.
*
* - An important thing to note is that *both* thread and co-thread have
* access to the list of sockets maintained in the private TSD data of the
* thread. The co-thread was given access to it upon creation through the
* new thread's client-data.
*
* Because of this dual access the TSD data contains an OS mutex, the
* "socketListLock", to mediate exclusion between thread and co-thread.
*
* The co-thread's access is all in SocketProc(). The thread's access is
* through SocketEventProc() (1) and the functions called by it.
*
* (Ad 1) This is the handler function for all queued socket events, which
* all the OS messages are translated to through the EventSource (2)
* driven by the OS messages.
*
* (Ad 2) The main functions for this are SocketSetupProc() and
* SocketCheckProc().
*/
#include "tclWinInt.h"
#ifdef _MSC_VER
# pragma comment (lib, "ws2_32")
#endif
|
| ︙ | | |
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
+
|
/*
* 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;
static const TCHAR classname[] = TEXT("TclSocket");
TCL_DECLARE_MUTEX(socketMutex)
/*
* The following variable holds the network name of this host.
*/
static TclInitProcessGlobalValueProc InitializeHostName;
|
| ︙ | | |
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
|
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
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
+
-
+
|
#define SOCKET_MESSAGE WM_USER+1
#define SOCKET_SELECT WM_USER+2
#define SOCKET_TERMINATE WM_USER+3
#define SELECT TRUE
#define UNSELECT FALSE
/*
* This is needed to comply with the strict aliasing rules of GCC, but it also
* simplifies casting between the different sockaddr types.
*/
typedef union {
struct sockaddr sa;
struct sockaddr_in sa4;
struct sockaddr_in6 sa6;
struct sockaddr_storage sas;
} address;
#ifndef IN6_ARE_ADDR_EQUAL
#define IN6_ARE_ADDR_EQUAL IN6_ADDR_EQUAL
#endif
typedef struct SocketInfo SocketInfo;
typedef struct TcpFdList {
SocketInfo *infoPtr;
SOCKET fd;
struct TcpFdList *next;
} TcpFdList;
/*
* The following structure is used to store the data associated with each
* socket.
*/
typedef struct SocketInfo {
struct SocketInfo {
Tcl_Channel channel; /* Channel associated with this socket. */
SOCKET socket; /* Windows SOCKET handle. */
struct TcpFdList *sockets; /* Windows SOCKET handle. */
int flags; /* Bit field comprised of the flags described
* below. */
int watchEvents; /* OR'ed combination of FD_READ, FD_WRITE,
* FD_CLOSE, FD_ACCEPT and FD_CONNECT that
* indicate which events are interesting. */
int readyEvents; /* OR'ed combination of FD_READ, FD_WRITE,
* FD_CLOSE, FD_ACCEPT and FD_CONNECT that
* indicate which events have occurred. */
int selectEvents; /* OR'ed combination of FD_READ, FD_WRITE,
* FD_CLOSE, FD_ACCEPT and FD_CONNECT that
* indicate which events are currently being
* selected. */
int acceptEventCount; /* Count of the current number of FD_ACCEPTs
* that have arrived and not yet processed. */
Tcl_TcpAcceptProc *acceptProc;
/* Proc to call on accept. */
ClientData acceptProcData; /* The data for the accept proc. */
int lastError; /* Error code from last message. */
struct SocketInfo *nextPtr; /* The next socket on the per-thread socket
* list. */
} SocketInfo;
};
/*
* The following structure is what is added to the Tcl event queue when a
* socket event occurs.
*/
typedef struct SocketEvent {
|
| ︙ | | |
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
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
|
+
+
-
+
|
/*
* Static functions defined in this file.
*/
static SocketInfo * CreateSocket(Tcl_Interp *interp, int port,
const char *host, int server, const char *myaddr,
int myport, int async);
#if 0
static int CreateSocketAddress(LPSOCKADDR_IN sockaddrPtr,
const char *host, int port);
#endif
static void InitSockets(void);
static SocketInfo * NewSocketInfo(SOCKET socket);
static void SocketExitHandler(ClientData clientData);
static LRESULT CALLBACK SocketProc(HWND hwnd, UINT message, WPARAM wParam,
LPARAM lParam);
static int SocketsEnabled(void);
static void TcpAccept(SocketInfo *infoPtr);
static void TcpAccept(TcpFdList *fds);
static int WaitForSocketEvent(SocketInfo *infoPtr, int events,
int *errorCodePtr);
static DWORD WINAPI SocketThread(LPVOID arg);
static void TcpThreadActionProc(ClientData instanceData,
int action);
static Tcl_EventCheckProc SocketCheckProc;
|
| ︙ | | |
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
|
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
|
-
+
-
+
|
static Tcl_DriverGetHandleProc TcpGetHandleProc;
/*
* This structure describes the channel type structure for TCP socket
* based IO.
*/
static Tcl_ChannelType tcpChannelType = {
static const Tcl_ChannelType tcpChannelType = {
"tcp", /* Type name. */
TCL_CHANNEL_VERSION_5, /* v5 channel */
TcpCloseProc, /* Close proc. */
TcpInputProc, /* Input proc. */
TcpOutputProc, /* Output proc. */
NULL, /* Seek proc. */
TcpSetOptionProc, /* Set option proc. */
TcpGetOptionProc, /* Get option proc. */
TcpWatchProc, /* Set up notifier to watch this channel. */
TcpGetHandleProc, /* Get an OS handle from channel. */
TcpClose2Proc, /* Close2proc. */
TcpBlockProc, /* Set socket into (non-)blocking mode. */
NULL, /* flush proc. */
NULL, /* handler proc. */
NULL, /* wide seek proc */
TcpThreadActionProc, /* thread action proc */
NULL, /* truncate */
NULL /* truncate */
};
/*
*----------------------------------------------------------------------
*
* InitSockets --
*
|
| ︙ | | |
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
|
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
|
-
+
-
+
-
+
|
WSADATA wsaData;
DWORD err;
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
TclThreadDataKeyGet(&dataKey);
if (!initialized) {
initialized = 1;
Tcl_CreateExitHandler(SocketExitHandler, (ClientData) NULL);
TclCreateLateExitHandler(SocketExitHandler, 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.lpszClassName = classname;
windowClass.lpfnWndProc = SocketProc;
windowClass.hIcon = NULL;
windowClass.hCursor = NULL;
if (!RegisterClassA(&windowClass)) {
if (!RegisterClass(&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
|
| ︙ | | |
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
|
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
|
-
+
|
Tcl_MutexLock(&socketMutex);
/*
* Make sure the socket event handling window is cleaned-up for, at
* most, this thread.
*/
TclpFinalizeSockets();
UnregisterClass("TclSocket", TclWinGetTclInstance());
UnregisterClass(classname, TclWinGetTclInstance());
WSACleanup();
initialized = 0;
Tcl_MutexUnlock(&socketMutex);
}
/*
*----------------------------------------------------------------------
|
| ︙ | | |
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
|
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
|
-
+
|
for (infoPtr = tsdPtr->socketList; infoPtr != NULL;
infoPtr = infoPtr->nextPtr) {
if ((infoPtr->readyEvents & infoPtr->watchEvents)
&& !(infoPtr->flags & SOCKET_PENDING)) {
infoPtr->flags |= SOCKET_PENDING;
evPtr = (SocketEvent *) ckalloc(sizeof(SocketEvent));
evPtr->header.proc = SocketEventProc;
evPtr->socket = infoPtr->socket;
evPtr->socket = infoPtr->sockets->fd;
Tcl_QueueEvent((Tcl_Event *) evPtr, TCL_QUEUE_TAIL);
}
}
SetEvent(tsdPtr->socketListLock);
}
/*
|
| ︙ | | |
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
|
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
|
+
-
+
+
-
+
+
|
* such as TCL_FILE_EVENTS. */
{
SocketInfo *infoPtr;
SocketEvent *eventPtr = (SocketEvent *) evPtr;
int mask = 0;
int events;
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
TcpFdList *fds;
if (!(flags & TCL_FILE_EVENTS)) {
return 0;
}
/*
* Find the specified socket on the socket list.
*/
WaitForSingleObject(tsdPtr->socketListLock, INFINITE);
for (infoPtr = tsdPtr->socketList; infoPtr != NULL;
infoPtr = infoPtr->nextPtr) {
if (infoPtr->socket == eventPtr->socket) {
if (infoPtr->sockets->fd == eventPtr->socket) {
break;
}
}
SetEvent(tsdPtr->socketListLock);
/*
* Discard events that have gone stale.
*/
if (!infoPtr) {
return 1;
}
infoPtr->flags &= ~SOCKET_PENDING;
/*
* Handle connection requests directly.
*/
if (infoPtr->readyEvents & FD_ACCEPT) {
for (fds = infoPtr->sockets; fds != NULL; fds = fds->next) {
TcpAccept(infoPtr);
TcpAccept(fds);
}
return 1;
}
/*
* Mask off unwanted events and compute the read/write mask so we can
* notify the channel.
*/
|
| ︙ | | |
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
|
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
|
-
+
|
* select handler and keep waiting.
*/
SendMessage(tsdPtr->hwnd, SOCKET_SELECT,
(WPARAM) UNSELECT, (LPARAM) infoPtr);
FD_ZERO(&readFds);
FD_SET(infoPtr->socket, &readFds);
FD_SET(infoPtr->sockets->fd, &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);
|
| ︙ | | |
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
|
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
|
-
+
|
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) {
if (closesocket(infoPtr->sockets->fd) == SOCKET_ERROR) {
TclWinConvertWSAError((DWORD) WSAGetLastError());
errorCode = Tcl_GetErrno();
}
}
/*
* TIP #218. Removed the code removing the structure from the global
|
| ︙ | | |
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
|
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
|
-
+
|
break;
default:
if (interp) {
Tcl_AppendResult(interp, "Socket close2proc called bidirectionally", NULL);
}
return TCL_ERROR;
}
if (shutdown(infoPtr->socket,sd) == SOCKET_ERROR) {
if (shutdown(infoPtr->sockets->fd,sd) == SOCKET_ERROR) {
TclWinConvertWSAError((DWORD) WSAGetLastError());
errorCode = Tcl_GetErrno();
}
return errorCode;
}
|
| ︙ | | |
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
|
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
|
+
-
+
+
+
+
-
+
+
-
+
|
*/
static SocketInfo *
NewSocketInfo(
SOCKET socket)
{
SocketInfo *infoPtr;
TcpFdList *fds;
/* ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); */
infoPtr = (SocketInfo *) ckalloc((unsigned) sizeof(SocketInfo));
fds = (TcpFdList*) ckalloc(sizeof(TcpFdList));
fds->fd = socket;
fds->next = NULL;
infoPtr = (SocketInfo *) ckalloc((unsigned) sizeof(SocketInfo));
fds->infoPtr = infoPtr;
/* ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); */
infoPtr->channel = 0;
infoPtr->socket = socket;
infoPtr->sockets = fds;
infoPtr->flags = 0;
infoPtr->watchEvents = 0;
infoPtr->readyEvents = 0;
infoPtr->selectEvents = 0;
infoPtr->acceptEventCount = 0;
infoPtr->acceptProc = NULL;
infoPtr->acceptProcData = NULL;
|
| ︙ | | |
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
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
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
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
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
|
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
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
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
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
1156
1157
1158
1159
1160
1161
1162
1163
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
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
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
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
|
+
-
-
+
+
+
-
+
-
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
-
-
-
-
-
+
+
+
+
+
+
-
-
-
+
+
-
-
-
-
-
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
+
+
+
+
+
+
+
+
+
-
-
-
-
+
+
+
+
+
+
+
-
+
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
-
+
+
+
+
+
+
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
-
+
|
int myport, /* Optional client-side port */
int async) /* If nonzero, connect client socket
* asynchronously. */
{
u_long flag = 1; /* Indicates nonblocking mode. */
int asyncConnect = 0; /* Will be 1 if async connect is in
* progress. */
unsigned short chosenport = 0;
SOCKADDR_IN sockaddr; /* Socket address */
SOCKADDR_IN mysockaddr; /* Socket address for client */
struct addrinfo *addrlist = NULL, *addrPtr; /* socket address */
struct addrinfo *myaddrlist = NULL, *myaddrPtr; /* Socket address for client */
const char *errorMsg = NULL;
SOCKET sock = INVALID_SOCKET;
SocketInfo *infoPtr; /* The returned value. */
SocketInfo *infoPtr = NULL; /* The returned value. */
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
TclThreadDataKeyGet(&dataKey);
/*
* 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;
}
if (!CreateSocketAddress(&sockaddr, host, port)) {
if (!TclCreateSocketAddress(interp, &addrlist, host, port, server, &errorMsg)) {
goto error;
}
if ((myaddr != NULL || myport != 0) &&
!CreateSocketAddress(&mysockaddr, myaddr, myport)) {
if (!TclCreateSocketAddress(interp, &myaddrlist, myaddr, myport, 1, &errorMsg)) {
goto error;
}
if (server) {
TcpFdList *fds = NULL, *newfds;
for (addrPtr = addrlist; addrPtr != NULL; addrPtr = addrPtr->ai_next) {
sock = socket(addrPtr->ai_family, SOCK_STREAM, 0);
if (sock == INVALID_SOCKET) {
TclWinConvertWSAError((DWORD) WSAGetLastError());
continue;
}
/*
* Win-NT has a misfeature that sockets are inherited in child
* processes by default. Turn off the inherit bit.
*/
SetHandleInformation((HANDLE) sock, HANDLE_FLAG_INHERIT, 0);
/*
* Set kernel space buffering
*/
TclSockMinimumBuffers((int) sock, TCP_BUFFER_SIZE);
/*
* Make sure we use the same port when opening two server sockets
* for IPv4 and IPv6.
*
* As sockaddr_in6 uses the same offset and size for the port
* member as sockaddr_in, we can handle both through the IPv4 API.
*/
if (port == 0 && chosenport != 0) {
((struct sockaddr_in *) addrPtr->ai_addr)->sin_port =
htons(chosenport);
}
/*
* Bind to the specified port. Note that we must not call
* setsockopt with SO_REUSEADDR because Microsoft allows addresses
* to be reused 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, addrPtr->ai_addr, addrPtr->ai_addrlen)
== SOCKET_ERROR) {
TclWinConvertWSAError((DWORD) WSAGetLastError());
closesocket(sock);
continue;
}
if (port == 0 && chosenport == 0) {
address sockname;
socklen_t namelen = sizeof(sockname);
/*
* Synchronize port numbers when binding to port 0 of multiple
* addresses.
*/
if (getsockname(sock, &sockname.sa, &namelen) >= 0) {
chosenport = ntohs(sockname.sa4.sin_port);
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.
*/
}
}
/*
* 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) {
TclWinConvertWSAError((DWORD) WSAGetLastError());
closesocket(sock);
continue;
}
if (infoPtr == NULL) {
/*
* Add this socket to the global list of sockets.
*/
infoPtr = NewSocketInfo(sock);
fds = infoPtr->sockets;
/*
* Set up the select mask for connection request events.
*/
infoPtr->selectEvents = FD_ACCEPT;
infoPtr->watchEvents |= FD_ACCEPT;
} else {
newfds = (TcpFdList *) ckalloc((unsigned) sizeof(TcpFdList));
memset(newfds, (int) 0, sizeof(TcpFdList));
newfds->fd = sock;
newfds->infoPtr = infoPtr;
newfds->next = NULL;
fds->next = newfds;
fds = newfds;
}
}
} else {
for (myaddrPtr = myaddrlist; myaddrPtr != NULL;
myaddrPtr = myaddrPtr->ai_next) {
for (addrPtr = addrlist; addrPtr != NULL;
addrPtr = addrPtr->ai_next) {
/*
* No need to try combinations of local and remote addresses
* of different families.
*/
if (myaddrPtr->ai_family != addrPtr->ai_family) {
continue;
}
sock = socket(myaddrPtr->ai_family, SOCK_STREAM, 0);
if (sock == INVALID_SOCKET) {
TclWinConvertWSAError((DWORD) WSAGetLastError());
continue;
}
/*
* Win-NT has a misfeature that sockets are inherited in child
* processes by default. Turn off the inherit bit.
*/
SetHandleInformation((HANDLE) sock, HANDLE_FLAG_INHERIT, 0);
SetHandleInformation((HANDLE) sock, HANDLE_FLAG_INHERIT, 0);
/*
* Set kernel space buffering
*/
TclSockMinimumBuffers((int) sock, TCP_BUFFER_SIZE);
/*
* Set kernel space buffering
*/
TclSockMinimumBuffers((int) sock, TCP_BUFFER_SIZE);
if (server) {
/*
* Bind to the specified port. Note that we must not call setsockopt
/*
* Try to bind to a local port.
* with SO_REUSEADDR because Microsoft allows addresses to be reused
* 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
*/
if (bind(sock, myaddrPtr->ai_addr, myaddrPtr->ai_addrlen)
== SOCKET_ERROR) {
TclWinConvertWSAError((DWORD) WSAGetLastError());
goto looperror;
}
/*
* Set the socket into nonblocking mode if the connect should
* 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
* be done in the background.
*/
if (async) {
if (ioctlsocket(sock, (long) FIONBIO, &flag)
== SOCKET_ERROR) {
TclWinConvertWSAError((DWORD) WSAGetLastError());
goto looperror;
}
}
/*
* Attempt to connect to the remote socket.
*/
if (connect(sock, addrPtr->ai_addr, addrPtr->ai_addrlen)
== SOCKET_ERROR) {
TclWinConvertWSAError((DWORD) WSAGetLastError());
if (Tcl_GetErrno() != EWOULDBLOCK) {
goto looperror;
}
/*
* The connection is progressing in the background.
* allowed on each platform (Win32 and Win32s may be different, and
* there may be differences between TCP/IP stacks).
*/
*/
asyncConnect = 1;
goto connected;
} else {
goto connected;
}
looperror:
if (sock != INVALID_SOCKET) {
closesocket(sock);
if (listen(sock, SOMAXCONN) == SOCKET_ERROR) {
goto error;
}
sock = INVALID_SOCKET;
}
}
}
goto error;
connected:
/*
* Add this socket to the global list of sockets.
*/
infoPtr = NewSocketInfo(sock);
/*
* Set up the select mask for connection request events.
*/
infoPtr->selectEvents = FD_ACCEPT;
infoPtr->watchEvents |= FD_ACCEPT;
} 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.
*/
asyncConnect = 1;
}
/*
* Add this socket to the global list of sockets.
*/
infoPtr = NewSocketInfo(sock);
/*
* Set up the select mask for read/write events. If the connect
* attempt has not completed, include connect events.
* Set up the select mask for read/write events. If the
* connect attempt has not completed, include connect events.
*/
infoPtr->selectEvents = FD_READ | FD_WRITE | FD_CLOSE;
if (asyncConnect) {
infoPtr->flags |= SOCKET_ASYNC_CONNECT;
infoPtr->selectEvents |= FD_CONNECT;
}
}
error:
if (addrlist == NULL)
freeaddrinfo(addrlist);
if (myaddrlist == NULL)
freeaddrinfo(myaddrlist);
/*
* 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:
if (infoPtr != NULL) {
ioctlsocket(sock, (long) FIONBIO, &flag);
SendMessage(tsdPtr->hwnd, SOCKET_SELECT, (WPARAM) SELECT, (LPARAM) infoPtr);
return infoPtr;
}
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;
}
#if 0
/*
*----------------------------------------------------------------------
*
* CreateSocketAddress --
*
* This function initializes a sockaddr structure for a host and port.
*
|
| ︙ | | |
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
|
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
|
+
|
* incorrect behavior on 64 bit machines such as DEC Alphas. Should we
* modify this code to do an explicit memcpy?
*/
sockaddrPtr->sin_addr.s_addr = addr.s_addr;
return 1; /* Success. */
}
#endif
/*
*----------------------------------------------------------------------
*
* WaitForSocketEvent --
*
* Waits until one of the specified events occurs on a socket.
|
| ︙ | | |
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
|
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
|
-
+
-
+
|
*/
infoPtr = CreateSocket(interp, port, host, 0, myaddr, myport, async);
if (infoPtr == NULL) {
return NULL;
}
wsprintfA(channelName, "sock%d", infoPtr->socket);
wsprintfA(channelName, "sock%d", infoPtr->sockets->fd);
infoPtr->channel = Tcl_CreateChannel(&tcpChannelType, channelName,
(ClientData) infoPtr, (TCL_READABLE | TCL_WRITABLE));
infoPtr, (TCL_READABLE | TCL_WRITABLE));
if (Tcl_SetChannelOption(interp, infoPtr->channel, "-translation",
"auto crlf") == TCL_ERROR) {
Tcl_Close((Tcl_Interp *) NULL, infoPtr->channel);
return (Tcl_Channel) NULL;
}
if (Tcl_SetChannelOption(NULL, infoPtr->channel, "-eofchar", "")
== TCL_ERROR) {
|
| ︙ | | |
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
|
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
|
-
+
-
+
|
* Start watching for read/write events on the socket.
*/
infoPtr->selectEvents = FD_READ | FD_CLOSE | FD_WRITE;
SendMessage(tsdPtr->hwnd, SOCKET_SELECT,
(WPARAM) SELECT, (LPARAM) infoPtr);
wsprintfA(channelName, "sock%d", infoPtr->socket);
wsprintfA(channelName, "sock%d", infoPtr->sockets->fd);
infoPtr->channel = Tcl_CreateChannel(&tcpChannelType, channelName,
(ClientData) infoPtr, (TCL_READABLE | TCL_WRITABLE));
infoPtr, (TCL_READABLE | TCL_WRITABLE));
Tcl_SetChannelOption(NULL, infoPtr->channel, "-translation", "auto crlf");
return infoPtr->channel;
}
/*
*----------------------------------------------------------------------
*
|
| ︙ | | |
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
|
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
|
-
+
-
+
|
if (infoPtr == NULL) {
return NULL;
}
infoPtr->acceptProc = acceptProc;
infoPtr->acceptProcData = acceptProcData;
wsprintfA(channelName, "sock%d", infoPtr->socket);
wsprintfA(channelName, "sock%d", infoPtr->sockets->fd);
infoPtr->channel = Tcl_CreateChannel(&tcpChannelType, channelName,
(ClientData) infoPtr, 0);
infoPtr, 0);
if (Tcl_SetChannelOption(interp, infoPtr->channel, "-eofchar", "")
== TCL_ERROR) {
Tcl_Close((Tcl_Interp *) NULL, infoPtr->channel);
return (Tcl_Channel) NULL;
}
return infoPtr->channel;
|
| ︙ | | |
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
|
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
|
-
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
|
* Invokes the accept proc which may invoke arbitrary Tcl code.
*
*----------------------------------------------------------------------
*/
static void
TcpAccept(
SocketInfo *infoPtr) /* Socket to accept. */
TcpFdList *fds) /* Socket to accept. */
{
SOCKET newSocket;
SocketInfo *newInfoPtr;
SocketInfo *infoPtr = fds->infoPtr;
SOCKADDR_IN addr;
int len;
char channelName[16 + TCL_INTEGER_SPACE];
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
TclThreadDataKeyGet(&dataKey);
/*
* Accept the incoming connection request.
*/
len = sizeof(SOCKADDR_IN);
newSocket = accept(infoPtr->socket, (SOCKADDR *)&addr,
&len);
newSocket = accept(fds->fd, (SOCKADDR *)&addr, &len);
/*
* Protect access to sockets (acceptEventCount, readyEvents) in socketList
* by the lock. Fix for SF Tcl Bug 3056775.
*/
WaitForSingleObject(tsdPtr->socketListLock, INFINITE);
/*
* 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.
*/
if (newSocket == INVALID_SOCKET) {
infoPtr->acceptEventCount = 0;
infoPtr->readyEvents &= ~(FD_ACCEPT);
SetEvent(tsdPtr->socketListLock);
return;
}
/*
* It is possible that more than one FD_ACCEPT has been sent, so an extra
* count must be kept. Decrement the count, and reset the readyEvent bit
* if the count is no longer > 0.
*/
infoPtr->acceptEventCount--;
if (infoPtr->acceptEventCount <= 0) {
infoPtr->readyEvents &= ~(FD_ACCEPT);
}
SetEvent(tsdPtr->socketListLock);
/*
* Win-NT has a misfeature that sockets are inherited in child processes
* by default. Turn off the inherit bit.
*/
SetHandleInformation((HANDLE) newSocket, HANDLE_FLAG_INHERIT, 0);
|
| ︙ | | |
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
|
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
|
-
+
-
+
|
* Select on read/write events and create the channel.
*/
newInfoPtr->selectEvents = (FD_READ | FD_WRITE | FD_CLOSE);
SendMessage(tsdPtr->hwnd, SOCKET_SELECT,
(WPARAM) SELECT, (LPARAM) newInfoPtr);
wsprintfA(channelName, "sock%d", newInfoPtr->socket);
wsprintfA(channelName, "sock%d", newInfoPtr->sockets->fd);
newInfoPtr->channel = Tcl_CreateChannel(&tcpChannelType, channelName,
(ClientData) newInfoPtr, (TCL_READABLE | TCL_WRITABLE));
newInfoPtr, (TCL_READABLE | TCL_WRITABLE));
if (Tcl_SetChannelOption(NULL, newInfoPtr->channel, "-translation",
"auto crlf") == TCL_ERROR) {
Tcl_Close((Tcl_Interp *) NULL, newInfoPtr->channel);
return;
}
if (Tcl_SetChannelOption(NULL, newInfoPtr->channel, "-eofchar", "")
== TCL_ERROR) {
|
| ︙ | | |
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
|
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
|
-
+
|
* 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);
bytesRead = recv(infoPtr->sockets->fd, buf, toRead, 0);
infoPtr->readyEvents &= ~(FD_READ);
/*
* Check for end-of-file condition or successful read.
*/
if (bytesRead == 0) {
|
| ︙ | | |
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
|
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
|
+
+
+
+
+
+
+
+
+
+
+
+
+
-
|
*/
if (infoPtr->readyEvents & FD_CLOSE) {
infoPtr->flags |= SOCKET_EOF;
bytesRead = 0;
break;
}
error = WSAGetLastError();
/*
* If an RST comes, then ignore the error and report an EOF just like
* on unix.
*/
if (error == WSAECONNRESET) {
infoPtr->flags |= SOCKET_EOF;
bytesRead = 0;
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;
}
|
| ︙ | | |
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
|
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
|
-
+
|
return -1;
}
while (1) {
SendMessage(tsdPtr->hwnd, SOCKET_SELECT,
(WPARAM) UNSELECT, (LPARAM) infoPtr);
bytesWritten = send(infoPtr->socket, buf, toWrite, 0);
bytesWritten = send(infoPtr->sockets->fd, 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.
*/
|
| ︙ | | |
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
|
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
|
-
+
-
+
-
+
|
if (interp) {
Tcl_AppendResult(interp, "winsock is not initialized", NULL);
}
return TCL_ERROR;
}
infoPtr = (SocketInfo *) instanceData;
sock = infoPtr->socket;
sock = infoPtr->sockets->fd;
#ifdef TCL_FEATURE_KEEPALIVE_NAGLE
if (!stricmp(optionName, "-keepalive")) {
if (!strcasecmp(optionName, "-keepalive")) {
BOOL val = FALSE;
int boolVar, rtn;
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")) {
} else if (!strcasecmp(optionName, "-nagle")) {
BOOL val = FALSE;
int boolVar, rtn;
if (Tcl_GetBoolean(interp, value, &boolVar) != TCL_OK) {
return TCL_ERROR;
}
if (!boolVar) {
|
| ︙ | | |
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
|
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
|
-
-
-
+
-
-
+
+
-
+
|
const char *optionName, /* Name of the option to retrieve the value
* for, or NULL to get all options and their
* values. */
Tcl_DString *dsPtr) /* Where to store the computed value;
* initialized by caller. */
{
SocketInfo *infoPtr;
SOCKADDR_IN sockname;
SOCKADDR_IN peername;
struct hostent *hostEntPtr;
char host[NI_MAXHOST], port[NI_MAXSERV];
SOCKET sock;
int size = sizeof(SOCKADDR_IN);
size_t len = 0;
char buf[TCL_INTEGER_SPACE];
int reverseDNS = 0;
#define SUPPRESS_RDNS_VAR "::tcl::unsupported::noReverseDNS"
/*
* 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()) {
if (interp) {
Tcl_AppendResult(interp, "winsock is not initialized", NULL);
}
return TCL_ERROR;
}
infoPtr = (SocketInfo *) instanceData;
sock = (int) infoPtr->socket;
sock = (int) infoPtr->sockets->fd;
if (optionName != NULL) {
len = strlen(optionName);
}
if ((len > 1) && (optionName[1] == 'e') &&
(strncmp(optionName, "-error", len) == 0)) {
int optlen;
|
| ︙ | | |
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
|
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
|
+
+
+
+
+
+
-
+
-
-
-
+
+
-
-
-
-
-
-
+
-
-
+
+
+
-
-
-
+
|
}
if (err) {
TclWinConvertWSAError(err);
Tcl_DStringAppend(dsPtr, Tcl_ErrnoMsg(Tcl_GetErrno()), -1);
}
return TCL_OK;
}
if (interp != NULL && Tcl_GetVar(interp, SUPPRESS_RDNS_VAR, 0) != NULL) {
reverseDNS = NI_NUMERICHOST;
}
if ((len == 0) || ((len > 1) && (optionName[1] == 'p') &&
(strncmp(optionName, "-peername", len) == 0))) {
address peername;
socklen_t size = sizeof(peername);
if (getpeername(sock, (LPSOCKADDR) &peername, &size) == 0) {
if (getpeername(sock, (LPSOCKADDR) &(peername.sa), &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;
getnameinfo(&(peername.sa), size, host, sizeof(host),
NULL, 0, NI_NUMERICHOST);
} else {
hostEntPtr = gethostbyaddr((char *) &(peername.sin_addr),
sizeof(peername.sin_addr), AF_INET);
}
if (hostEntPtr != NULL) {
Tcl_DStringAppendElement(dsPtr, hostEntPtr->h_name);
Tcl_DStringAppendElement(dsPtr, host);
} else {
Tcl_DStringAppendElement(dsPtr, inet_ntoa(peername.sin_addr));
getnameinfo(&(peername.sa), size, host, sizeof(host),
port, sizeof(port), reverseDNS | NI_NUMERICSERV);
Tcl_DStringAppendElement(dsPtr, host);
}
TclFormatInt(buf, ntohs(peername.sin_port));
Tcl_DStringAppendElement(dsPtr, buf);
Tcl_DStringAppendElement(dsPtr, port);
if (len == 0) {
Tcl_DStringEndSublist(dsPtr);
} else {
return TCL_OK;
}
} else {
/*
|
| ︙ | | |
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
|
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
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
-
-
+
+
-
+
-
+
|
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);
TcpFdList *fds;
address sockname;
socklen_t size;
int found = 0;
if (len == 0) {
Tcl_DStringAppendElement(dsPtr, "-sockname");
Tcl_DStringStartSublist(dsPtr);
}
for (fds = infoPtr->sockets; fds != NULL; fds = fds->next) {
sock = fds->fd;
size = sizeof(sockname);
if (getsockname(sock, &(sockname.sa), &size) >= 0) {
int flags = reverseDNS;
found = 1;
getnameinfo(&sockname.sa, size, host, sizeof(host),
NULL, 0, NI_NUMERICHOST);
Tcl_DStringAppendElement(dsPtr, host);
/*
* We don't want to resolve INADDR_ANY and sin6addr_any; they
* can sometimes cause problems (and never have a name).
*/
flags |= NI_NUMERICSERV;
if (sockname.sa.sa_family == AF_INET) {
if (sockname.sa4.sin_addr.s_addr == INADDR_ANY) {
flags |= NI_NUMERICHOST;
}
} else if (sockname.sa.sa_family == AF_INET6) {
if ((IN6_ARE_ADDR_EQUAL(&sockname.sa6.sin6_addr,
&in6addr_any))
|| (IN6_IS_ADDR_V4MAPPED(&sockname.sa6.sin6_addr) &&
sockname.sa6.sin6_addr.s6_addr[12] == 0 &&
sockname.sa6.sin6_addr.s6_addr[13] == 0 &&
sockname.sa6.sin6_addr.s6_addr[14] == 0 &&
sockname.sa6.sin6_addr.s6_addr[15] == 0)) {
flags |= NI_NUMERICHOST;
}
}
getnameinfo(&sockname.sa, size, host, sizeof(host),
port, sizeof(port), flags);
Tcl_DStringAppendElement(dsPtr, host);
} else {
Tcl_DStringAppendElement(dsPtr, inet_ntoa(sockname.sin_addr));
Tcl_DStringAppendElement(dsPtr, port);
}
TclFormatInt(buf, ntohs(sockname.sin_port));
Tcl_DStringAppendElement(dsPtr, buf);
}
if (found) {
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);
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");
|
| ︙ | | |
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
|
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
|
-
+
|
TcpGetHandleProc(
ClientData instanceData, /* The socket state. */
int direction, /* Not used. */
ClientData *handlePtr) /* Where to store the handle. */
{
SocketInfo *statePtr = (SocketInfo *) instanceData;
*handlePtr = (ClientData) statePtr->socket;
*handlePtr = INT2PTR(statePtr->sockets->fd);
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* SocketThread --
|
| ︙ | | |
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
|
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
|
-
+
|
MSG msg;
ThreadSpecificData *tsdPtr = (ThreadSpecificData *) arg;
/*
* Create a dummy window receiving socket events.
*/
tsdPtr->hwnd = CreateWindow("TclSocket", "TclSocket",
tsdPtr->hwnd = CreateWindow(classname, classname,
WS_TILED, 0, 0, 0, 0, NULL, NULL, windowClass.hInstance, arg);
/*
* Signalize thread creator that we are done creating the window.
*/
SetEvent(tsdPtr->readyEvent);
|
| ︙ | | |
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
|
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
|
-
+
|
* Find the specified socket on the socket list and update its
* eventState flag.
*/
WaitForSingleObject(tsdPtr->socketListLock, INFINITE);
for (infoPtr = tsdPtr->socketList; infoPtr != NULL;
infoPtr = infoPtr->nextPtr) {
if (infoPtr->socket == socket) {
if (infoPtr->sockets->fd == socket) {
/*
* Update the socket state.
*
* A count of FD_ACCEPTS is stored, so if an FD_CLOSE event
* happens, then clear the FD_ACCEPT count. Otherwise,
* increment the count if the current event is an FD_ACCEPT.
*/
|
| ︙ | | |
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
|
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
|
-
+
-
+
|
}
SetEvent(tsdPtr->socketListLock);
break;
case SOCKET_SELECT:
infoPtr = (SocketInfo *) lParam;
if (wParam == SELECT) {
WSAAsyncSelect(infoPtr->socket, hwnd,
WSAAsyncSelect(infoPtr->sockets->fd, hwnd,
SOCKET_MESSAGE, infoPtr->selectEvents);
} else {
/*
* Clear the selection mask
*/
WSAAsyncSelect(infoPtr->socket, hwnd, 0, 0);
WSAAsyncSelect(infoPtr->sockets->fd, hwnd, 0, 0);
}
break;
case SOCKET_TERMINATE:
DestroyWindow(hwnd);
break;
}
|
| ︙ | | |
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
|
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
|
-
-
+
+
-
+
-
+
|
void
InitializeHostName(
char **valuePtr,
int *lengthPtr,
Tcl_Encoding *encodingPtr)
{
WCHAR wbuf[MAX_COMPUTERNAME_LENGTH + 1];
DWORD length = sizeof(wbuf) / sizeof(WCHAR);
TCHAR tbuf[MAX_COMPUTERNAME_LENGTH + 1];
DWORD length = MAX_COMPUTERNAME_LENGTH + 1;
Tcl_DString ds;
if (tclWinProcs->getComputerNameProc(wbuf, &length) != 0) {
if (GetComputerName(tbuf, &length) != 0) {
/*
* Convert string from native to UTF then change to lowercase.
*/
Tcl_UtfToLower(Tcl_WinTCharToUtf((TCHAR *) wbuf, -1, &ds));
Tcl_UtfToLower(Tcl_WinTCharToUtf(tbuf, -1, &ds));
} else {
Tcl_DStringInit(&ds);
if (TclpHasSockets(NULL) == TCL_OK) {
/*
* Buffer length of 255 copied slavishly from previous version of
* this routine. Presumably there's a more "correct" macro value
|
| ︙ | | |