| 1 |
/* |
| 2 |
* tclWinSock.c -- |
| 3 |
* |
| 4 |
* This file contains Windows-specific socket related code. |
| 5 |
* |
| 6 |
* Copyright (c) 1995-1997 Sun Microsystems, Inc. |
| 7 |
* |
| 8 |
* See the file "license.terms" for information on usage and redistribution |
| 9 |
* of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 10 |
* |
| 11 |
* RCS: @(#) $Id: tclWinSock.c,v 1.36.2.1 2003/10/23 16:24:42 andreas_kupries Exp $ |
| 12 |
*/ |
| 13 |
|
| 14 |
#include "tclWinInt.h" |
| 15 |
|
| 16 |
/* |
| 17 |
* Make sure to remove the redirection defines set in tclWinPort.h |
| 18 |
* that is in use in other sections of the core, except for us. |
| 19 |
*/ |
| 20 |
#undef getservbyname |
| 21 |
#undef getsockopt |
| 22 |
#undef ntohs |
| 23 |
#undef setsockopt |
| 24 |
|
| 25 |
/* |
| 26 |
* The following variable is used to tell whether this module has been |
| 27 |
* initialized. |
| 28 |
*/ |
| 29 |
|
| 30 |
static int initialized = 0; |
| 31 |
|
| 32 |
static int hostnameInitialized = 0; |
| 33 |
static char hostname[255]; /* This buffer should be big enough for |
| 34 |
* hostname plus domain name. */ |
| 35 |
|
| 36 |
TCL_DECLARE_MUTEX(socketMutex) |
| 37 |
|
| 38 |
|
| 39 |
/* |
| 40 |
* Mingw and Cygwin may not have LPFN_* typedefs. |
| 41 |
*/ |
| 42 |
|
| 43 |
#ifdef HAVE_NO_LPFN_DECLS |
| 44 |
typedef SOCKET (PASCAL FAR *LPFN_ACCEPT)(SOCKET s, |
| 45 |
struct sockaddr FAR * addr, int FAR * addrlen); |
| 46 |
typedef int (PASCAL FAR *LPFN_BIND)(SOCKET s, |
| 47 |
const struct sockaddr FAR *addr, int namelen); |
| 48 |
typedef int (PASCAL FAR *LPFN_CLOSESOCKET)(SOCKET s); |
| 49 |
typedef int (PASCAL FAR *LPFN_CONNECT)(SOCKET s, |
| 50 |
const struct sockaddr FAR *name, int namelen); |
| 51 |
typedef struct hostent FAR * (PASCAL FAR *LPFN_GETHOSTBYADDR) |
| 52 |
(const char FAR *addr, int addrlen, int addrtype); |
| 53 |
typedef struct hostent FAR * (PASCAL FAR *LPFN_GETHOSTBYNAME) |
| 54 |
(const char FAR * name); |
| 55 |
typedef int (PASCAL FAR *LPFN_GETHOSTNAME)(char FAR * name, |
| 56 |
int namelen); |
| 57 |
typedef int (PASCAL FAR *LPFN_GETPEERNAME)(SOCKET sock, |
| 58 |
struct sockaddr FAR *name, int FAR *namelen); |
| 59 |
typedef struct servent FAR * (PASCAL FAR *LPFN_GETSERVBYNAME) |
| 60 |
(const char FAR * name, const char FAR * proto); |
| 61 |
typedef int (PASCAL FAR *LPFN_GETSOCKNAME)(SOCKET sock, |
| 62 |
struct sockaddr FAR *name, int FAR *namelen); |
| 63 |
typedef int (PASCAL FAR *LPFN_GETSOCKOPT)(SOCKET s, int level, |
| 64 |
int optname, char FAR * optval, int FAR *optlen); |
| 65 |
typedef u_short (PASCAL FAR *LPFN_HTONS)(u_short hostshort); |
| 66 |
typedef unsigned long (PASCAL FAR *LPFN_INET_ADDR) |
| 67 |
(const char FAR * cp); |
| 68 |
typedef char FAR * (PASCAL FAR *LPFN_INET_NTOA) |
| 69 |
(struct in_addr in); |
| 70 |
typedef int (PASCAL FAR *LPFN_IOCTLSOCKET)(SOCKET s, |
| 71 |
long cmd, u_long FAR *argp); |
| 72 |
typedef int (PASCAL FAR *LPFN_LISTEN)(SOCKET s, int backlog); |
| 73 |
typedef u_short (PASCAL FAR *LPFN_NTOHS)(u_short netshort); |
| 74 |
typedef int (PASCAL FAR *LPFN_RECV)(SOCKET s, char FAR * buf, |
| 75 |
int len, int flags); |
| 76 |
typedef int (PASCAL FAR *LPFN_SELECT)(int nfds, |
| 77 |
fd_set FAR * readfds, fd_set FAR * writefds, |
| 78 |
fd_set FAR * exceptfds, |
| 79 |
const struct timeval FAR * timeout); |
| 80 |
typedef int (PASCAL FAR *LPFN_SEND)(SOCKET s, |
| 81 |
const char FAR * buf, int len, int flags); |
| 82 |
typedef int (PASCAL FAR *LPFN_SETSOCKOPT)(SOCKET s, |
| 83 |
int level, int optname, const char FAR * optval, |
| 84 |
int optlen); |
| 85 |
typedef SOCKET (PASCAL FAR *LPFN_SOCKET)(int af, |
| 86 |
int type, int protocol); |
| 87 |
typedef int (PASCAL FAR *LPFN_WSAASYNCSELECT)(SOCKET s, |
| 88 |
HWND hWnd, u_int wMsg, long lEvent); |
| 89 |
typedef int (PASCAL FAR *LPFN_WSACLEANUP)(void); |
| 90 |
typedef int (PASCAL FAR *LPFN_WSAGETLASTERROR)(void); |
| 91 |
typedef int (PASCAL FAR *LPFN_WSASTARTUP)(WORD wVersionRequired, |
| 92 |
LPWSADATA lpWSAData); |
| 93 |
#endif |
| 94 |
|
| 95 |
|
| 96 |
/* |
| 97 |
* The following structure contains pointers to all of the WinSock API |
| 98 |
* entry points used by Tcl. It is initialized by InitSockets. Since |
| 99 |
* we dynamically load the Winsock DLL on demand, we must use this |
| 100 |
* function table to refer to functions in the winsock API. |
| 101 |
*/ |
| 102 |
|
| 103 |
static struct { |
| 104 |
HMODULE hModule; /* Handle to WinSock library. */ |
| 105 |
|
| 106 |
/* Winsock 1.1 functions */ |
| 107 |
LPFN_ACCEPT accept; |
| 108 |
LPFN_BIND bind; |
| 109 |
LPFN_CLOSESOCKET closesocket; |
| 110 |
LPFN_CONNECT connect; |
| 111 |
LPFN_GETHOSTBYADDR gethostbyaddr; |
| 112 |
LPFN_GETHOSTBYNAME gethostbyname; |
| 113 |
LPFN_GETHOSTNAME gethostname; |
| 114 |
LPFN_GETPEERNAME getpeername; |
| 115 |
LPFN_GETSERVBYNAME getservbyname; |
| 116 |
LPFN_GETSOCKNAME getsockname; |
| 117 |
LPFN_GETSOCKOPT getsockopt; |
| 118 |
LPFN_HTONS htons; |
| 119 |
LPFN_INET_ADDR inet_addr; |
| 120 |
LPFN_INET_NTOA inet_ntoa; |
| 121 |
LPFN_IOCTLSOCKET ioctlsocket; |
| 122 |
LPFN_LISTEN listen; |
| 123 |
LPFN_NTOHS ntohs; |
| 124 |
LPFN_RECV recv; |
| 125 |
LPFN_SELECT select; |
| 126 |
LPFN_SEND send; |
| 127 |
LPFN_SETSOCKOPT setsockopt; |
| 128 |
LPFN_SOCKET socket; |
| 129 |
LPFN_WSAASYNCSELECT WSAAsyncSelect; |
| 130 |
LPFN_WSACLEANUP WSACleanup; |
| 131 |
LPFN_WSAGETLASTERROR WSAGetLastError; |
| 132 |
LPFN_WSASTARTUP WSAStartup; |
| 133 |
|
| 134 |
} winSock; |
| 135 |
|
| 136 |
/* |
| 137 |
* The following defines declare the messages used on socket windows. |
| 138 |
*/ |
| 139 |
|
| 140 |
#define SOCKET_MESSAGE WM_USER+1 |
| 141 |
#define SOCKET_SELECT WM_USER+2 |
| 142 |
#define SOCKET_TERMINATE WM_USER+3 |
| 143 |
#define SELECT TRUE |
| 144 |
#define UNSELECT FALSE |
| 145 |
|
| 146 |
/* |
| 147 |
* The following structure is used to store the data associated with |
| 148 |
* each socket. |
| 149 |
*/ |
| 150 |
|
| 151 |
typedef struct SocketInfo { |
| 152 |
Tcl_Channel channel; /* Channel associated with this |
| 153 |
* socket. */ |
| 154 |
SOCKET socket; /* Windows SOCKET handle. */ |
| 155 |
int flags; /* Bit field comprised of the flags |
| 156 |
* described below. */ |
| 157 |
int watchEvents; /* OR'ed combination of FD_READ, |
| 158 |
* FD_WRITE, FD_CLOSE, FD_ACCEPT and |
| 159 |
* FD_CONNECT that indicate which |
| 160 |
* events are interesting. */ |
| 161 |
int readyEvents; /* OR'ed combination of FD_READ, |
| 162 |
* FD_WRITE, FD_CLOSE, FD_ACCEPT and |
| 163 |
* FD_CONNECT that indicate which |
| 164 |
* events have occurred. */ |
| 165 |
int selectEvents; /* OR'ed combination of FD_READ, |
| 166 |
* FD_WRITE, FD_CLOSE, FD_ACCEPT and |
| 167 |
* FD_CONNECT that indicate which |
| 168 |
* events are currently being |
| 169 |
* selected. */ |
| 170 |
int acceptEventCount; /* Count of the current number of |
| 171 |
* FD_ACCEPTs that have arrived and |
| 172 |
* not yet processed. */ |
| 173 |
Tcl_TcpAcceptProc *acceptProc; /* Proc to call on accept. */ |
| 174 |
ClientData acceptProcData; /* The data for the accept proc. */ |
| 175 |
int lastError; /* Error code from last message. */ |
| 176 |
struct SocketInfo *nextPtr; /* The next socket on the per-thread |
| 177 |
* socket list. */ |
| 178 |
} SocketInfo; |
| 179 |
|
| 180 |
/* |
| 181 |
* The following structure is what is added to the Tcl event queue when |
| 182 |
* a socket event occurs. |
| 183 |
*/ |
| 184 |
|
| 185 |
typedef struct SocketEvent { |
| 186 |
Tcl_Event header; /* Information that is standard for |
| 187 |
* all events. */ |
| 188 |
SOCKET socket; /* Socket descriptor that is ready. Used |
| 189 |
* to find the SocketInfo structure for |
| 190 |
* the file (can't point directly to the |
| 191 |
* SocketInfo structure because it could |
| 192 |
* go away while the event is queued). */ |
| 193 |
} SocketEvent; |
| 194 |
|
| 195 |
/* |
| 196 |
* This defines the minimum buffersize maintained by the kernel. |
| 197 |
*/ |
| 198 |
|
| 199 |
#define TCP_BUFFER_SIZE 4096 |
| 200 |
|
| 201 |
/* |
| 202 |
* The following macros may be used to set the flags field of |
| 203 |
* a SocketInfo structure. |
| 204 |
*/ |
| 205 |
|
| 206 |
#define SOCKET_ASYNC (1<<0) /* The socket is in blocking |
| 207 |
* mode. */ |
| 208 |
#define SOCKET_EOF (1<<1) /* A zero read happened on |
| 209 |
* the socket. */ |
| 210 |
#define SOCKET_ASYNC_CONNECT (1<<2) /* This socket uses async |
| 211 |
* connect. */ |
| 212 |
#define SOCKET_PENDING (1<<3) /* A message has been sent |
| 213 |
* for this socket */ |
| 214 |
|
| 215 |
typedef struct ThreadSpecificData { |
| 216 |
HWND hwnd; /* Handle to window for socket messages. */ |
| 217 |
HANDLE socketThread; /* Thread handling the window */ |
| 218 |
Tcl_ThreadId threadId; /* Parent thread. */ |
| 219 |
HANDLE readyEvent; /* Event indicating that a socket event is |
| 220 |
* ready. Also used to indicate that the |
| 221 |
* socketThread has been initialized and has |
| 222 |
* started. */ |
| 223 |
HANDLE socketListLock; /* Win32 Event to lock the socketList */ |
| 224 |
SocketInfo *socketList; /* Every open socket in this thread has an |
| 225 |
* entry on this list. */ |
| 226 |
} ThreadSpecificData; |
| 227 |
|
| 228 |
static Tcl_ThreadDataKey dataKey; |
| 229 |
static WNDCLASS windowClass; |
| 230 |
|
| 231 |
/* |
| 232 |
* Static functions defined in this file. |
| 233 |
*/ |
| 234 |
|
| 235 |
static SocketInfo * CreateSocket _ANSI_ARGS_((Tcl_Interp *interp, |
| 236 |
int port, CONST char *host, |
| 237 |
int server, CONST char *myaddr, |
| 238 |
int myport, int async)); |
| 239 |
static int CreateSocketAddress _ANSI_ARGS_( |
| 240 |
(LPSOCKADDR_IN sockaddrPtr, |
| 241 |
CONST char *host, int port)); |
| 242 |
static void InitSockets _ANSI_ARGS_((void)); |
| 243 |
static SocketInfo * NewSocketInfo _ANSI_ARGS_((SOCKET socket)); |
| 244 |
static Tcl_EventCheckProc SocketCheckProc; |
| 245 |
static Tcl_EventProc SocketEventProc; |
| 246 |
static void SocketExitHandler _ANSI_ARGS_(( |
| 247 |
ClientData clientData)); |
| 248 |
static LRESULT CALLBACK SocketProc _ANSI_ARGS_((HWND hwnd, |
| 249 |
UINT message, WPARAM wParam, |
| 250 |
LPARAM lParam)); |
| 251 |
static Tcl_EventSetupProc SocketSetupProc; |
| 252 |
static Tcl_ExitProc SocketThreadExitHandler; |
| 253 |
static int SocketsEnabled _ANSI_ARGS_((void)); |
| 254 |
static void TcpAccept _ANSI_ARGS_((SocketInfo *infoPtr)); |
| 255 |
static Tcl_DriverBlockModeProc TcpBlockProc; |
| 256 |
static Tcl_DriverCloseProc TcpCloseProc; |
| 257 |
static Tcl_DriverSetOptionProc TcpSetOptionProc; |
| 258 |
static Tcl_DriverGetOptionProc TcpGetOptionProc; |
| 259 |
static Tcl_DriverInputProc TcpInputProc; |
| 260 |
static Tcl_DriverOutputProc TcpOutputProc; |
| 261 |
static Tcl_DriverWatchProc TcpWatchProc; |
| 262 |
static Tcl_DriverGetHandleProc TcpGetHandleProc; |
| 263 |
static int WaitForSocketEvent _ANSI_ARGS_(( |
| 264 |
SocketInfo *infoPtr, int events, |
| 265 |
int *errorCodePtr)); |
| 266 |
static DWORD WINAPI SocketThread _ANSI_ARGS_((LPVOID arg)); |
| 267 |
|
| 268 |
/* |
| 269 |
* This structure describes the channel type structure for TCP socket |
| 270 |
* based IO. |
| 271 |
*/ |
| 272 |
|
| 273 |
static Tcl_ChannelType tcpChannelType = { |
| 274 |
"tcp", /* Type name. */ |
| 275 |
TCL_CHANNEL_VERSION_2, /* v2 channel */ |
| 276 |
TcpCloseProc, /* Close proc. */ |
| 277 |
TcpInputProc, /* Input proc. */ |
| 278 |
TcpOutputProc, /* Output proc. */ |
| 279 |
NULL, /* Seek proc. */ |
| 280 |
TcpSetOptionProc, /* Set option proc. */ |
| 281 |
TcpGetOptionProc, /* Get option proc. */ |
| 282 |
TcpWatchProc, /* Set up notifier to watch this channel. */ |
| 283 |
TcpGetHandleProc, /* Get an OS handle from channel. */ |
| 284 |
NULL, /* close2proc. */ |
| 285 |
TcpBlockProc, /* Set socket into (non-)blocking mode. */ |
| 286 |
NULL, /* flush proc. */ |
| 287 |
NULL, /* handler proc. */ |
| 288 |
}; |
| 289 |
|
| 290 |
|
| 291 |
/* |
| 292 |
*---------------------------------------------------------------------- |
| 293 |
* |
| 294 |
* InitSockets -- |
| 295 |
* |
| 296 |
* Initialize the socket module. Attempts to load the wsock32.dll |
| 297 |
* library and set up the winSock function table. If successful, |
| 298 |
* registers the event window for the socket notifier code. |
| 299 |
* |
| 300 |
* Assumes Mutex is held. |
| 301 |
* |
| 302 |
* Results: |
| 303 |
* None. |
| 304 |
* |
| 305 |
* Side effects: |
| 306 |
* Dynamically loads wsock32.dll, and registers a new window |
| 307 |
* class and creates a window for use in asynchronous socket |
| 308 |
* notification. |
| 309 |
* |
| 310 |
*---------------------------------------------------------------------- |
| 311 |
*/ |
| 312 |
|
| 313 |
static void |
| 314 |
InitSockets() |
| 315 |
{ |
| 316 |
DWORD id; |
| 317 |
WSADATA wsaData; |
| 318 |
DWORD err; |
| 319 |
ThreadSpecificData *tsdPtr = |
| 320 |
(ThreadSpecificData *)TclThreadDataKeyGet(&dataKey); |
| 321 |
|
| 322 |
if (!initialized) { |
| 323 |
initialized = 1; |
| 324 |
Tcl_CreateExitHandler(SocketExitHandler, (ClientData) NULL); |
| 325 |
|
| 326 |
winSock.hModule = LoadLibraryA("wsock32.dll"); |
| 327 |
|
| 328 |
if (winSock.hModule == NULL) { |
| 329 |
return; |
| 330 |
} |
| 331 |
|
| 332 |
/* |
| 333 |
* Initialize the function table. |
| 334 |
*/ |
| 335 |
|
| 336 |
winSock.accept = (LPFN_ACCEPT) |
| 337 |
GetProcAddress(winSock.hModule, "accept"); |
| 338 |
winSock.bind = (LPFN_BIND) |
| 339 |
GetProcAddress(winSock.hModule, "bind"); |
| 340 |
winSock.closesocket = (LPFN_CLOSESOCKET) |
| 341 |
GetProcAddress(winSock.hModule, "closesocket"); |
| 342 |
winSock.connect = (LPFN_CONNECT) |
| 343 |
GetProcAddress(winSock.hModule, "connect"); |
| 344 |
winSock.gethostbyaddr = (LPFN_GETHOSTBYADDR) |
| 345 |
GetProcAddress(winSock.hModule, "gethostbyaddr"); |
| 346 |
winSock.gethostbyname = (LPFN_GETHOSTBYNAME) |
| 347 |
GetProcAddress(winSock.hModule, "gethostbyname"); |
| 348 |
winSock.gethostname = (LPFN_GETHOSTNAME) |
| 349 |
GetProcAddress(winSock.hModule, "gethostname"); |
| 350 |
winSock.getpeername = (LPFN_GETPEERNAME) |
| 351 |
GetProcAddress(winSock.hModule, "getpeername"); |
| 352 |
winSock.getservbyname = (LPFN_GETSERVBYNAME) |
| 353 |
GetProcAddress(winSock.hModule, "getservbyname"); |
| 354 |
winSock.getsockname = (LPFN_GETSOCKNAME) |
| 355 |
GetProcAddress(winSock.hModule, "getsockname"); |
| 356 |
winSock.getsockopt = (LPFN_GETSOCKOPT) |
| 357 |
GetProcAddress(winSock.hModule, "getsockopt"); |
| 358 |
winSock.htons = (LPFN_HTONS) |
| 359 |
GetProcAddress(winSock.hModule, "htons"); |
| 360 |
winSock.inet_addr = (LPFN_INET_ADDR) |
| 361 |
GetProcAddress(winSock.hModule, "inet_addr"); |
| 362 |
winSock.inet_ntoa = (LPFN_INET_NTOA) |
| 363 |
GetProcAddress(winSock.hModule, "inet_ntoa"); |
| 364 |
winSock.ioctlsocket = (LPFN_IOCTLSOCKET) |
| 365 |
GetProcAddress(winSock.hModule, "ioctlsocket"); |
| 366 |
winSock.listen = (LPFN_LISTEN) |
| 367 |
GetProcAddress(winSock.hModule, "listen"); |
| 368 |
winSock.ntohs = (LPFN_NTOHS) |
| 369 |
GetProcAddress(winSock.hModule, "ntohs"); |
| 370 |
winSock.recv = (LPFN_RECV) |
| 371 |
GetProcAddress(winSock.hModule, "recv"); |
| 372 |
winSock.select = (LPFN_SELECT) |
| 373 |
GetProcAddress(winSock.hModule, "select"); |
| 374 |
winSock.send = (LPFN_SEND) |
| 375 |
GetProcAddress(winSock.hModule, "send"); |
| 376 |
winSock.setsockopt = (LPFN_SETSOCKOPT) |
| 377 |
GetProcAddress(winSock.hModule, "setsockopt"); |
| 378 |
winSock.socket = (LPFN_SOCKET) |
| 379 |
GetProcAddress(winSock.hModule, "socket"); |
| 380 |
winSock.WSAAsyncSelect = (LPFN_WSAASYNCSELECT) |
| 381 |
GetProcAddress(winSock.hModule, "WSAAsyncSelect"); |
| 382 |
winSock.WSACleanup = (LPFN_WSACLEANUP) |
| 383 |
GetProcAddress(winSock.hModule, "WSACleanup"); |
| 384 |
winSock.WSAGetLastError = (LPFN_WSAGETLASTERROR) |
| 385 |
GetProcAddress(winSock.hModule, "WSAGetLastError"); |
| 386 |
winSock.WSAStartup = (LPFN_WSASTARTUP) |
| 387 |
GetProcAddress(winSock.hModule, "WSAStartup"); |
| 388 |
|
| 389 |
/* |
| 390 |
* Now check that all fields are properly initialized. If not, |
| 391 |
* return zero to indicate that we failed to initialize |
| 392 |
* properly. |
| 393 |
*/ |
| 394 |
|
| 395 |
if ((winSock.accept == NULL) || |
| 396 |
(winSock.bind == NULL) || |
| 397 |
(winSock.closesocket == NULL) || |
| 398 |
(winSock.connect == NULL) || |
| 399 |
(winSock.gethostbyname == NULL) || |
| 400 |
(winSock.gethostbyaddr == NULL) || |
| 401 |
(winSock.gethostname == NULL) || |
| 402 |
(winSock.getpeername == NULL) || |
| 403 |
(winSock.getservbyname == NULL) || |
| 404 |
(winSock.getsockname == NULL) || |
| 405 |
(winSock.getsockopt == NULL) || |
| 406 |
(winSock.htons == NULL) || |
| 407 |
(winSock.inet_addr == NULL) || |
| 408 |
(winSock.inet_ntoa == NULL) || |
| 409 |
(winSock.ioctlsocket == NULL) || |
| 410 |
(winSock.listen == NULL) || |
| 411 |
(winSock.ntohs == NULL) || |
| 412 |
(winSock.recv == NULL) || |
| 413 |
(winSock.select == NULL) || |
| 414 |
(winSock.send == NULL) || |
| 415 |
(winSock.setsockopt == NULL) || |
| 416 |
(winSock.socket == NULL) || |
| 417 |
(winSock.WSAAsyncSelect == NULL) || |
| 418 |
(winSock.WSACleanup == NULL) || |
| 419 |
(winSock.WSAGetLastError == NULL) || |
| 420 |
(winSock.WSAStartup == NULL)) |
| 421 |
{ |
| 422 |
goto unloadLibrary; |
| 423 |
} |
| 424 |
|
| 425 |
/* |
| 426 |
* Create the async notification window with a new class. We |
| 427 |
* must create a new class to avoid a Windows 95 bug that causes |
| 428 |
* us to get the wrong message number for socket events if the |
| 429 |
* message window is a subclass of a static control. |
| 430 |
*/ |
| 431 |
|
| 432 |
windowClass.style = 0; |
| 433 |
windowClass.cbClsExtra = 0; |
| 434 |
windowClass.cbWndExtra = 0; |
| 435 |
windowClass.hInstance = TclWinGetTclInstance(); |
| 436 |
windowClass.hbrBackground = NULL; |
| 437 |
windowClass.lpszMenuName = NULL; |
| 438 |
windowClass.lpszClassName = "TclSocket"; |
| 439 |
windowClass.lpfnWndProc = SocketProc; |
| 440 |
windowClass.hIcon = NULL; |
| 441 |
windowClass.hCursor = NULL; |
| 442 |
|
| 443 |
if (!RegisterClassA(&windowClass)) { |
| 444 |
TclWinConvertError(GetLastError()); |
| 445 |
goto unloadLibrary; |
| 446 |
} |
| 447 |
|
| 448 |
/* |
| 449 |
* Initialize the winsock library and check the interface |
| 450 |
* version actually loaded. We only ask for the 1.1 interface |
| 451 |
* and do require that it not be less than 1.1. |
| 452 |
*/ |
| 453 |
|
| 454 |
#define WSA_VERSION_MAJOR 1 |
| 455 |
#define WSA_VERSION_MINOR 1 |
| 456 |
#define WSA_VERSION_REQD MAKEWORD(WSA_VERSION_MAJOR, WSA_VERSION_MINOR) |
| 457 |
|
| 458 |
if ((err = winSock.WSAStartup(WSA_VERSION_REQD, &wsaData)) != 0) { |
| 459 |
TclWinConvertWSAError(err); |
| 460 |
goto unloadLibrary; |
| 461 |
} |
| 462 |
|
| 463 |
/* |
| 464 |
* Note the byte positions are swapped for the comparison, so |
| 465 |
* that 0x0002 (2.0, MAKEWORD(2,0)) doesn't look less than 0x0101 |
| 466 |
* (1.1). We want the comparison to be 0x0200 < 0x0101. |
| 467 |
*/ |
| 468 |
|
| 469 |
if (MAKEWORD(HIBYTE(wsaData.wVersion), LOBYTE(wsaData.wVersion)) |
| 470 |
< MAKEWORD(WSA_VERSION_MINOR, WSA_VERSION_MAJOR)) { |
| 471 |
TclWinConvertWSAError(WSAVERNOTSUPPORTED); |
| 472 |
winSock.WSACleanup(); |
| 473 |
goto unloadLibrary; |
| 474 |
} |
| 475 |
|
| 476 |
#undef WSA_VERSION_REQD |
| 477 |
#undef WSA_VERSION_MAJOR |
| 478 |
#undef WSA_VERSION_MINOR |
| 479 |
} |
| 480 |
|
| 481 |
/* |
| 482 |
* Check for per-thread initialization. |
| 483 |
*/ |
| 484 |
|
| 485 |
if (tsdPtr == NULL) { |
| 486 |
tsdPtr = TCL_TSD_INIT(&dataKey); |
| 487 |
tsdPtr->socketList = NULL; |
| 488 |
tsdPtr->hwnd = NULL; |
| 489 |
|
| 490 |
tsdPtr->threadId = Tcl_GetCurrentThread(); |
| 491 |
|
| 492 |
tsdPtr->readyEvent = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 493 |
tsdPtr->socketListLock = CreateEvent(NULL, FALSE, TRUE, NULL); |
| 494 |
tsdPtr->socketThread = CreateThread(NULL, 256, SocketThread, |
| 495 |
tsdPtr, 0, &id); |
| 496 |
SetThreadPriority(tsdPtr->socketThread, THREAD_PRIORITY_HIGHEST); |
| 497 |
|
| 498 |
if (tsdPtr->socketThread == NULL) { |
| 499 |
goto unloadLibrary; |
| 500 |
} |
| 501 |
|
| 502 |
|
| 503 |
/* |
| 504 |
* Wait for the thread to signal that the window has |
| 505 |
* been created and is ready to go. Timeout after twenty |
| 506 |
* seconds. |
| 507 |
*/ |
| 508 |
|
| 509 |
if (WaitForSingleObject(tsdPtr->readyEvent, 20000) |
| 510 |
== WAIT_TIMEOUT) { |
| 511 |
goto unloadLibrary; |
| 512 |
} |
| 513 |
|
| 514 |
if (tsdPtr->hwnd == NULL) { |
| 515 |
goto unloadLibrary; |
| 516 |
} |
| 517 |
|
| 518 |
Tcl_CreateEventSource(SocketSetupProc, SocketCheckProc, NULL); |
| 519 |
Tcl_CreateThreadExitHandler(SocketThreadExitHandler, NULL); |
| 520 |
} |
| 521 |
return; |
| 522 |
|
| 523 |
unloadLibrary: |
| 524 |
if (tsdPtr != NULL && tsdPtr->hwnd != NULL) { |
| 525 |
SocketThreadExitHandler(0); |
| 526 |
} |
| 527 |
FreeLibrary(winSock.hModule); |
| 528 |
winSock.hModule = NULL; |
| 529 |
return; |
| 530 |
} |
| 531 |
|
| 532 |
/* |
| 533 |
*---------------------------------------------------------------------- |
| 534 |
* |
| 535 |
* SocketsEnabled -- |
| 536 |
* |
| 537 |
* Check that the WinSock DLL is loaded and ready. |
| 538 |
* |
| 539 |
* Results: |
| 540 |
* 1 if it is. |
| 541 |
* |
| 542 |
* Side effects: |
| 543 |
* None. |
| 544 |
* |
| 545 |
*---------------------------------------------------------------------- |
| 546 |
*/ |
| 547 |
|
| 548 |
/* ARGSUSED */ |
| 549 |
static int |
| 550 |
SocketsEnabled() |
| 551 |
{ |
| 552 |
int enabled; |
| 553 |
Tcl_MutexLock(&socketMutex); |
| 554 |
enabled = (winSock.hModule != NULL); |
| 555 |
Tcl_MutexUnlock(&socketMutex); |
| 556 |
return enabled; |
| 557 |
} |
| 558 |
|
| 559 |
|
| 560 |
/* |
| 561 |
*---------------------------------------------------------------------- |
| 562 |
* |
| 563 |
* SocketExitHandler -- |
| 564 |
* |
| 565 |
* Callback invoked during exit clean up to delete the socket |
| 566 |
* communication window and to release the WinSock DLL. |
| 567 |
* |
| 568 |
* Results: |
| 569 |
* None. |
| 570 |
* |
| 571 |
* Side effects: |
| 572 |
* None. |
| 573 |
* |
| 574 |
*---------------------------------------------------------------------- |
| 575 |
*/ |
| 576 |
|
| 577 |
/* ARGSUSED */ |
| 578 |
static void |
| 579 |
SocketExitHandler(clientData) |
| 580 |
ClientData clientData; /* Not used. */ |
| 581 |
{ |
| 582 |
Tcl_MutexLock(&socketMutex); |
| 583 |
if (winSock.hModule) { |
| 584 |
/* |
| 585 |
* Make sure the socket event handling window is cleaned-up |
| 586 |
* for, at most, this thread. |
| 587 |
*/ |
| 588 |
SocketThreadExitHandler(clientData); |
| 589 |
UnregisterClass("TclSocket", TclWinGetTclInstance()); |
| 590 |
winSock.WSACleanup(); |
| 591 |
FreeLibrary(winSock.hModule); |
| 592 |
winSock.hModule = NULL; |
| 593 |
} |
| 594 |
initialized = 0; |
| 595 |
hostnameInitialized = 0; |
| 596 |
Tcl_MutexUnlock(&socketMutex); |
| 597 |
} |
| 598 |
|
| 599 |
/* |
| 600 |
*---------------------------------------------------------------------- |
| 601 |
* |
| 602 |
* SocketThreadExitHandler -- |
| 603 |
* |
| 604 |
* Callback invoked during thread clean up to delete the socket |
| 605 |
* event source. |
| 606 |
* |
| 607 |
* Results: |
| 608 |
* None. |
| 609 |
* |
| 610 |
* Side effects: |
| 611 |
* Delete the event source. |
| 612 |
* |
| 613 |
*---------------------------------------------------------------------- |
| 614 |
*/ |
| 615 |
|
| 616 |
/* ARGSUSED */ |
| 617 |
static void |
| 618 |
SocketThreadExitHandler(clientData) |
| 619 |
ClientData clientData; /* Not used. */ |
| 620 |
{ |
| 621 |
ThreadSpecificData *tsdPtr = |
| 622 |
(ThreadSpecificData *)TclThreadDataKeyGet(&dataKey); |
| 623 |
|
| 624 |
if (tsdPtr != NULL && tsdPtr->socketThread != NULL) { |
| 625 |
DWORD exitCode; |
| 626 |
|
| 627 |
GetExitCodeThread(tsdPtr->socketThread, &exitCode); |
| 628 |
if (exitCode == STILL_ACTIVE) { |
| 629 |
PostMessage(tsdPtr->hwnd, SOCKET_TERMINATE, 0, 0); |
| 630 |
|
| 631 |
/* |
| 632 |
* Wait for the thread to close. This ensures that we are |
| 633 |
* completely cleaned up before we leave this function. |
| 634 |
*/ |
| 635 |
|
| 636 |
WaitForSingleObject(tsdPtr->socketThread, INFINITE); |
| 637 |
} |
| 638 |
CloseHandle(tsdPtr->socketThread); |
| 639 |
tsdPtr->socketThread = NULL; |
| 640 |
CloseHandle(tsdPtr->readyEvent); |
| 641 |
CloseHandle(tsdPtr->socketListLock); |
| 642 |
|
| 643 |
Tcl_DeleteThreadExitHandler(SocketThreadExitHandler, NULL); |
| 644 |
Tcl_DeleteEventSource(SocketSetupProc, SocketCheckProc, NULL); |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
/* |
| 649 |
*---------------------------------------------------------------------- |
| 650 |
* |
| 651 |
* TclpHasSockets -- |
| 652 |
* |
| 653 |
* This function determines whether sockets are available on the |
| 654 |
* current system and returns an error in interp if they are not. |
| 655 |
* Note that interp may be NULL. |
| 656 |
* |
| 657 |
* Results: |
| 658 |
* Returns TCL_OK if the system supports sockets, or TCL_ERROR with |
| 659 |
* an error in interp. |
| 660 |
* |
| 661 |
* Side effects: |
| 662 |
* If not already prepared, initializes the TSD structure and |
| 663 |
* socket message handling thread associated to the calling thread |
| 664 |
* for the subsystem of the driver. |
| 665 |
* |
| 666 |
*---------------------------------------------------------------------- |
| 667 |
*/ |
| 668 |
|
| 669 |
int |
| 670 |
TclpHasSockets(interp) |
| 671 |
Tcl_Interp *interp; |
| 672 |
{ |
| 673 |
Tcl_MutexLock(&socketMutex); |
| 674 |
InitSockets(); |
| 675 |
Tcl_MutexUnlock(&socketMutex); |
| 676 |
|
| 677 |
if (SocketsEnabled()) { |
| 678 |
return TCL_OK; |
| 679 |
} |
| 680 |
if (interp != NULL) { |
| 681 |
Tcl_AppendResult(interp, "sockets are not available on this system", |
| 682 |
NULL); |
| 683 |
} |
| 684 |
return TCL_ERROR; |
| 685 |
} |
| 686 |
|
| 687 |
/* |
| 688 |
*---------------------------------------------------------------------- |
| 689 |
* |
| 690 |
* SocketSetupProc -- |
| 691 |
* |
| 692 |
* This procedure is invoked before Tcl_DoOneEvent blocks waiting |
| 693 |
* for an event. |
| 694 |
* |
| 695 |
* Results: |
| 696 |
* None. |
| 697 |
* |
| 698 |
* Side effects: |
| 699 |
* Adjusts the block time if needed. |
| 700 |
* |
| 701 |
*---------------------------------------------------------------------- |
| 702 |
*/ |
| 703 |
|
| 704 |
void |
| 705 |
SocketSetupProc(data, flags) |
| 706 |
ClientData data; /* Not used. */ |
| 707 |
int flags; /* Event flags as passed to Tcl_DoOneEvent. */ |
| 708 |
{ |
| 709 |
SocketInfo *infoPtr; |
| 710 |
Tcl_Time blockTime = { 0, 0 }; |
| 711 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 712 |
|
| 713 |
if (!(flags & TCL_FILE_EVENTS)) { |
| 714 |
return; |
| 715 |
} |
| 716 |
|
| 717 |
/* |
| 718 |
* Check to see if there is a ready socket. If so, poll. |
| 719 |
*/ |
| 720 |
|
| 721 |
WaitForSingleObject(tsdPtr->socketListLock, INFINITE); |
| 722 |
for (infoPtr = tsdPtr->socketList; infoPtr != NULL; |
| 723 |
infoPtr = infoPtr->nextPtr) { |
| 724 |
if (infoPtr->readyEvents & infoPtr->watchEvents) { |
| 725 |
Tcl_SetMaxBlockTime(&blockTime); |
| 726 |
break; |
| 727 |
} |
| 728 |
} |
| 729 |
SetEvent(tsdPtr->socketListLock); |
| 730 |
} |
| 731 |
|
| 732 |
/* |
| 733 |
*---------------------------------------------------------------------- |
| 734 |
* |
| 735 |
* SocketCheckProc -- |
| 736 |
* |
| 737 |
* This procedure is called by Tcl_DoOneEvent to check the socket |
| 738 |
* event source for events. |
| 739 |
* |
| 740 |
* Results: |
| 741 |
* None. |
| 742 |
* |
| 743 |
* Side effects: |
| 744 |
* May queue an event. |
| 745 |
* |
| 746 |
*---------------------------------------------------------------------- |
| 747 |
*/ |
| 748 |
|
| 749 |
static void |
| 750 |
SocketCheckProc(data, flags) |
| 751 |
ClientData data; /* Not used. */ |
| 752 |
int flags; /* Event flags as passed to Tcl_DoOneEvent. */ |
| 753 |
{ |
| 754 |
SocketInfo *infoPtr; |
| 755 |
SocketEvent *evPtr; |
| 756 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 757 |
|
| 758 |
if (!(flags & TCL_FILE_EVENTS)) { |
| 759 |
return; |
| 760 |
} |
| 761 |
|
| 762 |
/* |
| 763 |
* Queue events for any ready sockets that don't already have events |
| 764 |
* queued (caused by persistent states that won't generate WinSock |
| 765 |
* events). |
| 766 |
*/ |
| 767 |
|
| 768 |
WaitForSingleObject(tsdPtr->socketListLock, INFINITE); |
| 769 |
for (infoPtr = tsdPtr->socketList; infoPtr != NULL; |
| 770 |
infoPtr = infoPtr->nextPtr) { |
| 771 |
if ((infoPtr->readyEvents & infoPtr->watchEvents) |
| 772 |
&& !(infoPtr->flags & SOCKET_PENDING)) { |
| 773 |
infoPtr->flags |= SOCKET_PENDING; |
| 774 |
evPtr = (SocketEvent *) ckalloc(sizeof(SocketEvent)); |
| 775 |
evPtr->header.proc = SocketEventProc; |
| 776 |
evPtr->socket = infoPtr->socket; |
| 777 |
Tcl_QueueEvent((Tcl_Event *) evPtr, TCL_QUEUE_TAIL); |
| 778 |
} |
| 779 |
} |
| 780 |
SetEvent(tsdPtr->socketListLock); |
| 781 |
} |
| 782 |
|
| 783 |
/* |
| 784 |
*---------------------------------------------------------------------- |
| 785 |
* |
| 786 |
* SocketEventProc -- |
| 787 |
* |
| 788 |
* This procedure is called by Tcl_ServiceEvent when a socket event |
| 789 |
* reaches the front of the event queue. This procedure is |
| 790 |
* responsible for notifying the generic channel code. |
| 791 |
* |
| 792 |
* Results: |
| 793 |
* Returns 1 if the event was handled, meaning it should be removed |
| 794 |
* from the queue. Returns 0 if the event was not handled, meaning |
| 795 |
* it should stay on the queue. The only time the event isn't |
| 796 |
* handled is if the TCL_FILE_EVENTS flag bit isn't set. |
| 797 |
* |
| 798 |
* Side effects: |
| 799 |
* Whatever the channel callback procedures do. |
| 800 |
* |
| 801 |
*---------------------------------------------------------------------- |
| 802 |
*/ |
| 803 |
|
| 804 |
static int |
| 805 |
SocketEventProc(evPtr, flags) |
| 806 |
Tcl_Event *evPtr; /* Event to service. */ |
| 807 |
int flags; /* Flags that indicate what events to |
| 808 |
* handle, such as TCL_FILE_EVENTS. */ |
| 809 |
{ |
| 810 |
SocketInfo *infoPtr; |
| 811 |
SocketEvent *eventPtr = (SocketEvent *) evPtr; |
| 812 |
int mask = 0; |
| 813 |
int events; |
| 814 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 815 |
|
| 816 |
if (!(flags & TCL_FILE_EVENTS)) { |
| 817 |
return 0; |
| 818 |
} |
| 819 |
|
| 820 |
/* |
| 821 |
* Find the specified socket on the socket list. |
| 822 |
*/ |
| 823 |
|
| 824 |
WaitForSingleObject(tsdPtr->socketListLock, INFINITE); |
| 825 |
for (infoPtr = tsdPtr->socketList; infoPtr != NULL; |
| 826 |
infoPtr = infoPtr->nextPtr) { |
| 827 |
if (infoPtr->socket == eventPtr->socket) { |
| 828 |
break; |
| 829 |
} |
| 830 |
} |
| 831 |
SetEvent(tsdPtr->socketListLock); |
| 832 |
|
| 833 |
/* |
| 834 |
* Discard events that have gone stale. |
| 835 |
*/ |
| 836 |
|
| 837 |
if (!infoPtr) { |
| 838 |
return 1; |
| 839 |
} |
| 840 |
|
| 841 |
infoPtr->flags &= ~SOCKET_PENDING; |
| 842 |
|
| 843 |
/* |
| 844 |
* Handle connection requests directly. |
| 845 |
*/ |
| 846 |
|
| 847 |
if (infoPtr->readyEvents & FD_ACCEPT) { |
| 848 |
TcpAccept(infoPtr); |
| 849 |
return 1; |
| 850 |
} |
| 851 |
|
| 852 |
/* |
| 853 |
* Mask off unwanted events and compute the read/write mask so |
| 854 |
* we can notify the channel. |
| 855 |
*/ |
| 856 |
|
| 857 |
events = infoPtr->readyEvents & infoPtr->watchEvents; |
| 858 |
|
| 859 |
if (events & FD_CLOSE) { |
| 860 |
/* |
| 861 |
* If the socket was closed and the channel is still interested |
| 862 |
* in read events, then we need to ensure that we keep polling |
| 863 |
* for this event until someone does something with the channel. |
| 864 |
* Note that we do this before calling Tcl_NotifyChannel so we don't |
| 865 |
* have to watch out for the channel being deleted out from under |
| 866 |
* us. This may cause a redundant trip through the event loop, but |
| 867 |
* it's simpler than trying to do unwind protection. |
| 868 |
*/ |
| 869 |
|
| 870 |
Tcl_Time blockTime = { 0, 0 }; |
| 871 |
Tcl_SetMaxBlockTime(&blockTime); |
| 872 |
mask |= TCL_READABLE|TCL_WRITABLE; |
| 873 |
} else if (events & FD_READ) { |
| 874 |
fd_set readFds; |
| 875 |
struct timeval timeout; |
| 876 |
|
| 877 |
/* |
| 878 |
* We must check to see if data is really available, since someone |
| 879 |
* could have consumed the data in the meantime. Turn off async |
| 880 |
* notification so select will work correctly. If the socket is |
| 881 |
* still readable, notify the channel driver, otherwise reset the |
| 882 |
* async select handler and keep waiting. |
| 883 |
*/ |
| 884 |
|
| 885 |
SendMessage(tsdPtr->hwnd, SOCKET_SELECT, |
| 886 |
(WPARAM) UNSELECT, (LPARAM) infoPtr); |
| 887 |
|
| 888 |
FD_ZERO(&readFds); |
| 889 |
FD_SET(infoPtr->socket, &readFds); |
| 890 |
timeout.tv_usec = 0; |
| 891 |
timeout.tv_sec = 0; |
| 892 |
|
| 893 |
if (winSock.select(0, &readFds, NULL, NULL, &timeout) != 0) { |
| 894 |
mask |= TCL_READABLE; |
| 895 |
} else { |
| 896 |
infoPtr->readyEvents &= ~(FD_READ); |
| 897 |
SendMessage(tsdPtr->hwnd, SOCKET_SELECT, |
| 898 |
(WPARAM) SELECT, (LPARAM) infoPtr); |
| 899 |
} |
| 900 |
} |
| 901 |
if (events & (FD_WRITE | FD_CONNECT)) { |
| 902 |
mask |= TCL_WRITABLE; |
| 903 |
} |
| 904 |
|
| 905 |
if (mask) { |
| 906 |
Tcl_NotifyChannel(infoPtr->channel, mask); |
| 907 |
} |
| 908 |
return 1; |
| 909 |
} |
| 910 |
|
| 911 |
/* |
| 912 |
*---------------------------------------------------------------------- |
| 913 |
* |
| 914 |
* TcpBlockProc -- |
| 915 |
* |
| 916 |
* Sets a socket into blocking or non-blocking mode. |
| 917 |
* |
| 918 |
* Results: |
| 919 |
* 0 if successful, errno if there was an error. |
| 920 |
* |
| 921 |
* Side effects: |
| 922 |
* None. |
| 923 |
* |
| 924 |
*---------------------------------------------------------------------- |
| 925 |
*/ |
| 926 |
|
| 927 |
static int |
| 928 |
TcpBlockProc(instanceData, mode) |
| 929 |
ClientData instanceData; /* The socket to block/un-block. */ |
| 930 |
int mode; /* TCL_MODE_BLOCKING or |
| 931 |
* TCL_MODE_NONBLOCKING. */ |
| 932 |
{ |
| 933 |
SocketInfo *infoPtr = (SocketInfo *) instanceData; |
| 934 |
|
| 935 |
if (mode == TCL_MODE_NONBLOCKING) { |
| 936 |
infoPtr->flags |= SOCKET_ASYNC; |
| 937 |
} else { |
| 938 |
infoPtr->flags &= ~(SOCKET_ASYNC); |
| 939 |
} |
| 940 |
return 0; |
| 941 |
} |
| 942 |
|
| 943 |
/* |
| 944 |
*---------------------------------------------------------------------- |
| 945 |
* |
| 946 |
* TcpCloseProc -- |
| 947 |
* |
| 948 |
* This procedure is called by the generic IO level to perform |
| 949 |
* channel type specific cleanup on a socket based channel |
| 950 |
* when the channel is closed. |
| 951 |
* |
| 952 |
* Results: |
| 953 |
* 0 if successful, the value of errno if failed. |
| 954 |
* |
| 955 |
* Side effects: |
| 956 |
* Closes the socket. |
| 957 |
* |
| 958 |
*---------------------------------------------------------------------- |
| 959 |
*/ |
| 960 |
|
| 961 |
/* ARGSUSED */ |
| 962 |
static int |
| 963 |
TcpCloseProc(instanceData, interp) |
| 964 |
ClientData instanceData; /* The socket to close. */ |
| 965 |
Tcl_Interp *interp; /* Unused. */ |
| 966 |
{ |
| 967 |
SocketInfo *infoPtr = (SocketInfo *) instanceData; |
| 968 |
SocketInfo **nextPtrPtr; |
| 969 |
int errorCode = 0; |
| 970 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 971 |
|
| 972 |
/* |
| 973 |
* Check that WinSock is initialized; do not call it if not, to |
| 974 |
* prevent system crashes. This can happen at exit time if the exit |
| 975 |
* handler for WinSock ran before other exit handlers that want to |
| 976 |
* use sockets. |
| 977 |
*/ |
| 978 |
|
| 979 |
if (SocketsEnabled()) { |
| 980 |
|
| 981 |
/* |
| 982 |
* Clean up the OS socket handle. The default Windows setting |
| 983 |
* for a socket is SO_DONTLINGER, which does a graceful shutdown |
| 984 |
* in the background. |
| 985 |
*/ |
| 986 |
|
| 987 |
if (winSock.closesocket(infoPtr->socket) == SOCKET_ERROR) { |
| 988 |
TclWinConvertWSAError((DWORD) winSock.WSAGetLastError()); |
| 989 |
errorCode = Tcl_GetErrno(); |
| 990 |
} |
| 991 |
} |
| 992 |
|
| 993 |
/* |
| 994 |
* Remove the socket from socketList. |
| 995 |
*/ |
| 996 |
|
| 997 |
WaitForSingleObject(tsdPtr->socketListLock, INFINITE); |
| 998 |
for (nextPtrPtr = &(tsdPtr->socketList); (*nextPtrPtr) != NULL; |
| 999 |
nextPtrPtr = &((*nextPtrPtr)->nextPtr)) { |
| 1000 |
if ((*nextPtrPtr) == infoPtr) { |
| 1001 |
(*nextPtrPtr) = infoPtr->nextPtr; |
| 1002 |
break; |
| 1003 |
} |
| 1004 |
} |
| 1005 |
|
| 1006 |
SetEvent(tsdPtr->socketListLock); |
| 1007 |
ckfree((char *) infoPtr); |
| 1008 |
return errorCode; |
| 1009 |
} |
| 1010 |
|
| 1011 |
/* |
| 1012 |
*---------------------------------------------------------------------- |
| 1013 |
* |
| 1014 |
* NewSocketInfo -- |
| 1015 |
* |
| 1016 |
* This function allocates and initializes a new SocketInfo |
| 1017 |
* structure. |
| 1018 |
* |
| 1019 |
* Results: |
| 1020 |
* Returns a newly allocated SocketInfo. |
| 1021 |
* |
| 1022 |
* Side effects: |
| 1023 |
* Adds the socket to the global socket list. |
| 1024 |
* |
| 1025 |
*---------------------------------------------------------------------- |
| 1026 |
*/ |
| 1027 |
|
| 1028 |
static SocketInfo * |
| 1029 |
NewSocketInfo(socket) |
| 1030 |
SOCKET socket; |
| 1031 |
{ |
| 1032 |
SocketInfo *infoPtr; |
| 1033 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 1034 |
|
| 1035 |
infoPtr = (SocketInfo *) ckalloc((unsigned) sizeof(SocketInfo)); |
| 1036 |
infoPtr->socket = socket; |
| 1037 |
infoPtr->flags = 0; |
| 1038 |
infoPtr->watchEvents = 0; |
| 1039 |
infoPtr->readyEvents = 0; |
| 1040 |
infoPtr->selectEvents = 0; |
| 1041 |
infoPtr->acceptEventCount = 0; |
| 1042 |
infoPtr->acceptProc = NULL; |
| 1043 |
infoPtr->lastError = 0; |
| 1044 |
|
| 1045 |
WaitForSingleObject(tsdPtr->socketListLock, INFINITE); |
| 1046 |
infoPtr->nextPtr = tsdPtr->socketList; |
| 1047 |
tsdPtr->socketList = infoPtr; |
| 1048 |
SetEvent(tsdPtr->socketListLock); |
| 1049 |
|
| 1050 |
return infoPtr; |
| 1051 |
} |
| 1052 |
|
| 1053 |
/* |
| 1054 |
*---------------------------------------------------------------------- |
| 1055 |
* |
| 1056 |
* CreateSocket -- |
| 1057 |
* |
| 1058 |
* This function opens a new socket and initializes the |
| 1059 |
* SocketInfo structure. |
| 1060 |
* |
| 1061 |
* Results: |
| 1062 |
* Returns a new SocketInfo, or NULL with an error in interp. |
| 1063 |
* |
| 1064 |
* Side effects: |
| 1065 |
* Adds a new socket to the socketList. |
| 1066 |
* |
| 1067 |
*---------------------------------------------------------------------- |
| 1068 |
*/ |
| 1069 |
|
| 1070 |
static SocketInfo * |
| 1071 |
CreateSocket(interp, port, host, server, myaddr, myport, async) |
| 1072 |
Tcl_Interp *interp; /* For error reporting; can be NULL. */ |
| 1073 |
int port; /* Port number to open. */ |
| 1074 |
CONST char *host; /* Name of host on which to open port. */ |
| 1075 |
int server; /* 1 if socket should be a server socket, |
| 1076 |
* else 0 for a client socket. */ |
| 1077 |
CONST char *myaddr; /* Optional client-side address */ |
| 1078 |
int myport; /* Optional client-side port */ |
| 1079 |
int async; /* If nonzero, connect client socket |
| 1080 |
* asynchronously. */ |
| 1081 |
{ |
| 1082 |
u_long flag = 1; /* Indicates nonblocking mode. */ |
| 1083 |
int asyncConnect = 0; /* Will be 1 if async connect is |
| 1084 |
* in progress. */ |
| 1085 |
SOCKADDR_IN sockaddr; /* Socket address */ |
| 1086 |
SOCKADDR_IN mysockaddr; /* Socket address for client */ |
| 1087 |
SOCKET sock; |
| 1088 |
SocketInfo *infoPtr; /* The returned value. */ |
| 1089 |
ThreadSpecificData *tsdPtr = |
| 1090 |
(ThreadSpecificData *)TclThreadDataKeyGet(&dataKey); |
| 1091 |
|
| 1092 |
/* |
| 1093 |
* Check that WinSock is initialized; do not call it if not, to |
| 1094 |
* prevent system crashes. This can happen at exit time if the exit |
| 1095 |
* handler for WinSock ran before other exit handlers that want to |
| 1096 |
* use sockets. |
| 1097 |
*/ |
| 1098 |
|
| 1099 |
if (!SocketsEnabled()) { |
| 1100 |
return NULL; |
| 1101 |
} |
| 1102 |
|
| 1103 |
if (! CreateSocketAddress(&sockaddr, host, port)) { |
| 1104 |
goto error; |
| 1105 |
} |
| 1106 |
if ((myaddr != NULL || myport != 0) && |
| 1107 |
! CreateSocketAddress(&mysockaddr, myaddr, myport)) { |
| 1108 |
goto error; |
| 1109 |
} |
| 1110 |
|
| 1111 |
sock = winSock.socket(AF_INET, SOCK_STREAM, 0); |
| 1112 |
if (sock == INVALID_SOCKET) { |
| 1113 |
goto error; |
| 1114 |
} |
| 1115 |
|
| 1116 |
/* |
| 1117 |
* Win-NT has a misfeature that sockets are inherited in child |
| 1118 |
* processes by default. Turn off the inherit bit. |
| 1119 |
*/ |
| 1120 |
|
| 1121 |
SetHandleInformation( (HANDLE) sock, HANDLE_FLAG_INHERIT, 0 ); |
| 1122 |
|
| 1123 |
/* |
| 1124 |
* Set kernel space buffering |
| 1125 |
*/ |
| 1126 |
|
| 1127 |
TclSockMinimumBuffers((int) sock, TCP_BUFFER_SIZE); |
| 1128 |
|
| 1129 |
if (server) { |
| 1130 |
/* |
| 1131 |
* Bind to the specified port. Note that we must not call setsockopt |
| 1132 |
* with SO_REUSEADDR because Microsoft allows addresses to be reused |
| 1133 |
* even if they are still in use. |
| 1134 |
* |
| 1135 |
* Bind should not be affected by the socket having already been |
| 1136 |
* set into nonblocking mode. If there is trouble, this is one place |
| 1137 |
* to look for bugs. |
| 1138 |
*/ |
| 1139 |
|
| 1140 |
if (winSock.bind(sock, (SOCKADDR *) &sockaddr, |
| 1141 |
sizeof(SOCKADDR_IN)) == SOCKET_ERROR) { |
| 1142 |
goto error; |
| 1143 |
} |
| 1144 |
|
| 1145 |
/* |
| 1146 |
* Set the maximum number of pending connect requests to the |
| 1147 |
* max value allowed on each platform (Win32 and Win32s may be |
| 1148 |
* different, and there may be differences between TCP/IP stacks). |
| 1149 |
*/ |
| 1150 |
|
| 1151 |
if (winSock.listen(sock, SOMAXCONN) == SOCKET_ERROR) { |
| 1152 |
goto error; |
| 1153 |
} |
| 1154 |
|
| 1155 |
/* |
| 1156 |
* Add this socket to the global list of sockets. |
| 1157 |
*/ |
| 1158 |
|
| 1159 |
infoPtr = NewSocketInfo(sock); |
| 1160 |
|
| 1161 |
/* |
| 1162 |
* Set up the select mask for connection request events. |
| 1163 |
*/ |
| 1164 |
|
| 1165 |
infoPtr->selectEvents = FD_ACCEPT; |
| 1166 |
infoPtr->watchEvents |= FD_ACCEPT; |
| 1167 |
|
| 1168 |
} else { |
| 1169 |
|
| 1170 |
/* |
| 1171 |
* Try to bind to a local port, if specified. |
| 1172 |
*/ |
| 1173 |
|
| 1174 |
if (myaddr != NULL || myport != 0) { |
| 1175 |
if (winSock.bind(sock, (SOCKADDR *) &mysockaddr, |
| 1176 |
sizeof(SOCKADDR_IN)) == SOCKET_ERROR) { |
| 1177 |
goto error; |
| 1178 |
} |
| 1179 |
} |
| 1180 |
|
| 1181 |
/* |
| 1182 |
* Set the socket into nonblocking mode if the connect should be |
| 1183 |
* done in the background. |
| 1184 |
*/ |
| 1185 |
|
| 1186 |
if (async) { |
| 1187 |
if (winSock.ioctlsocket(sock, (long) FIONBIO, &flag) == SOCKET_ERROR) { |
| 1188 |
goto error; |
| 1189 |
} |
| 1190 |
} |
| 1191 |
|
| 1192 |
/* |
| 1193 |
* Attempt to connect to the remote socket. |
| 1194 |
*/ |
| 1195 |
|
| 1196 |
if (winSock.connect(sock, (SOCKADDR *) &sockaddr, |
| 1197 |
sizeof(SOCKADDR_IN)) == SOCKET_ERROR) { |
| 1198 |
TclWinConvertWSAError((DWORD) winSock.WSAGetLastError()); |
| 1199 |
if (Tcl_GetErrno() != EWOULDBLOCK) { |
| 1200 |
goto error; |
| 1201 |
} |
| 1202 |
|
| 1203 |
/* |
| 1204 |
* The connection is progressing in the background. |
| 1205 |
*/ |
| 1206 |
|
| 1207 |
asyncConnect = 1; |
| 1208 |
} |
| 1209 |
|
| 1210 |
/* |
| 1211 |
* Add this socket to the global list of sockets. |
| 1212 |
*/ |
| 1213 |
|
| 1214 |
infoPtr = NewSocketInfo(sock); |
| 1215 |
|
| 1216 |
/* |
| 1217 |
* Set up the select mask for read/write events. If the connect |
| 1218 |
* attempt has not completed, include connect events. |
| 1219 |
*/ |
| 1220 |
|
| 1221 |
infoPtr->selectEvents = FD_READ | FD_WRITE | FD_CLOSE; |
| 1222 |
if (asyncConnect) { |
| 1223 |
infoPtr->flags |= SOCKET_ASYNC_CONNECT; |
| 1224 |
infoPtr->selectEvents |= FD_CONNECT; |
| 1225 |
} |
| 1226 |
} |
| 1227 |
|
| 1228 |
/* |
| 1229 |
* Register for interest in events in the select mask. Note that this |
| 1230 |
* automatically places the socket into non-blocking mode. |
| 1231 |
*/ |
| 1232 |
|
| 1233 |
winSock.ioctlsocket(sock, (long) FIONBIO, &flag); |
| 1234 |
SendMessage(tsdPtr->hwnd, SOCKET_SELECT, |
| 1235 |
(WPARAM) SELECT, (LPARAM) infoPtr); |
| 1236 |
|
| 1237 |
return infoPtr; |
| 1238 |
|
| 1239 |
error: |
| 1240 |
TclWinConvertWSAError((DWORD) winSock.WSAGetLastError()); |
| 1241 |
if (interp != NULL) { |
| 1242 |
Tcl_AppendResult(interp, "couldn't open socket: ", |
| 1243 |
Tcl_PosixError(interp), (char *) NULL); |
| 1244 |
} |
| 1245 |
if (sock != INVALID_SOCKET) { |
| 1246 |
winSock.closesocket(sock); |
| 1247 |
} |
| 1248 |
return NULL; |
| 1249 |
} |
| 1250 |
|
| 1251 |
/* |
| 1252 |
*---------------------------------------------------------------------- |
| 1253 |
* |
| 1254 |
* CreateSocketAddress -- |
| 1255 |
* |
| 1256 |
* This function initializes a sockaddr structure for a host and port. |
| 1257 |
* |
| 1258 |
* Results: |
| 1259 |
* 1 if the host was valid, 0 if the host could not be converted to |
| 1260 |
* an IP address. |
| 1261 |
* |
| 1262 |
* Side effects: |
| 1263 |
* Fills in the *sockaddrPtr structure. |
| 1264 |
* |
| 1265 |
*---------------------------------------------------------------------- |
| 1266 |
*/ |
| 1267 |
|
| 1268 |
static int |
| 1269 |
CreateSocketAddress(sockaddrPtr, host, port) |
| 1270 |
LPSOCKADDR_IN sockaddrPtr; /* Socket address */ |
| 1271 |
CONST char *host; /* Host. NULL implies INADDR_ANY */ |
| 1272 |
int port; /* Port number */ |
| 1273 |
{ |
| 1274 |
struct hostent *hostent; /* Host database entry */ |
| 1275 |
struct in_addr addr; /* For 64/32 bit madness */ |
| 1276 |
|
| 1277 |
/* |
| 1278 |
* Check that WinSock is initialized; do not call it if not, to |
| 1279 |
* prevent system crashes. This can happen at exit time if the exit |
| 1280 |
* handler for WinSock ran before other exit handlers that want to |
| 1281 |
* use sockets. |
| 1282 |
*/ |
| 1283 |
|
| 1284 |
if (!SocketsEnabled()) { |
| 1285 |
Tcl_SetErrno(EFAULT); |
| 1286 |
return 0; |
| 1287 |
} |
| 1288 |
|
| 1289 |
ZeroMemory(sockaddrPtr, sizeof(SOCKADDR_IN)); |
| 1290 |
sockaddrPtr->sin_family = AF_INET; |
| 1291 |
sockaddrPtr->sin_port = winSock.htons((u_short) (port & 0xFFFF)); |
| 1292 |
if (host == NULL) { |
| 1293 |
addr.s_addr = INADDR_ANY; |
| 1294 |
} else { |
| 1295 |
addr.s_addr = winSock.inet_addr(host); |
| 1296 |
if (addr.s_addr == INADDR_NONE) { |
| 1297 |
hostent = winSock.gethostbyname(host); |
| 1298 |
if (hostent != NULL) { |
| 1299 |
memcpy(&addr, hostent->h_addr, (size_t) hostent->h_length); |
| 1300 |
} else { |
| 1301 |
#ifdef EHOSTUNREACH |
| 1302 |
Tcl_SetErrno(EHOSTUNREACH); |
| 1303 |
#else |
| 1304 |
#ifdef ENXIO |
| 1305 |
Tcl_SetErrno(ENXIO); |
| 1306 |
#endif |
| 1307 |
#endif |
| 1308 |
return 0; /* Error. */ |
| 1309 |
} |
| 1310 |
} |
| 1311 |
} |
| 1312 |
|
| 1313 |
/* |
| 1314 |
* NOTE: On 64 bit machines the assignment below is rumored to not |
| 1315 |
* do the right thing. Please report errors related to this if you |
| 1316 |
* observe incorrect behavior on 64 bit machines such as DEC Alphas. |
| 1317 |
* Should we modify this code to do an explicit memcpy? |
| 1318 |
*/ |
| 1319 |
|
| 1320 |
sockaddrPtr->sin_addr.s_addr = addr.s_addr; |
| 1321 |
return 1; /* Success. */ |
| 1322 |
} |
| 1323 |
|
| 1324 |
/* |
| 1325 |
*---------------------------------------------------------------------- |
| 1326 |
* |
| 1327 |
* WaitForSocketEvent -- |
| 1328 |
* |
| 1329 |
* Waits until one of the specified events occurs on a socket. |
| 1330 |
* |
| 1331 |
* Results: |
| 1332 |
* Returns 1 on success or 0 on failure, with an error code in |
| 1333 |
* errorCodePtr. |
| 1334 |
* |
| 1335 |
* Side effects: |
| 1336 |
* Processes socket events off the system queue. |
| 1337 |
* |
| 1338 |
*---------------------------------------------------------------------- |
| 1339 |
*/ |
| 1340 |
|
| 1341 |
static int |
| 1342 |
WaitForSocketEvent(infoPtr, events, errorCodePtr) |
| 1343 |
SocketInfo *infoPtr; /* Information about this socket. */ |
| 1344 |
int events; /* Events to look for. */ |
| 1345 |
int *errorCodePtr; /* Where to store errors? */ |
| 1346 |
{ |
| 1347 |
int result = 1; |
| 1348 |
int oldMode; |
| 1349 |
ThreadSpecificData *tsdPtr = |
| 1350 |
(ThreadSpecificData *)TclThreadDataKeyGet(&dataKey); |
| 1351 |
|
| 1352 |
/* |
| 1353 |
* Be sure to disable event servicing so we are truly modal. |
| 1354 |
*/ |
| 1355 |
|
| 1356 |
oldMode = Tcl_SetServiceMode(TCL_SERVICE_NONE); |
| 1357 |
|
| 1358 |
/* |
| 1359 |
* Reset WSAAsyncSelect so we have a fresh set of events pending. |
| 1360 |
*/ |
| 1361 |
|
| 1362 |
SendMessage(tsdPtr->hwnd, SOCKET_SELECT, |
| 1363 |
(WPARAM) UNSELECT, (LPARAM) infoPtr); |
| 1364 |
|
| 1365 |
SendMessage(tsdPtr->hwnd, SOCKET_SELECT, |
| 1366 |
(WPARAM) SELECT, (LPARAM) infoPtr); |
| 1367 |
|
| 1368 |
while (1) { |
| 1369 |
|
| 1370 |
if (infoPtr->lastError) { |
| 1371 |
*errorCodePtr = infoPtr->lastError; |
| 1372 |
result = 0; |
| 1373 |
break; |
| 1374 |
} else if (infoPtr->readyEvents & events) { |
| 1375 |
break; |
| 1376 |
} else if (infoPtr->flags & SOCKET_ASYNC) { |
| 1377 |
*errorCodePtr = EWOULDBLOCK; |
| 1378 |
result = 0; |
| 1379 |
break; |
| 1380 |
} |
| 1381 |
|
| 1382 |
/* |
| 1383 |
* Wait until something happens. |
| 1384 |
*/ |
| 1385 |
WaitForSingleObject(tsdPtr->readyEvent, INFINITE); |
| 1386 |
} |
| 1387 |
|
| 1388 |
(void) Tcl_SetServiceMode(oldMode); |
| 1389 |
return result; |
| 1390 |
} |
| 1391 |
|
| 1392 |
/* |
| 1393 |
*---------------------------------------------------------------------- |
| 1394 |
* |
| 1395 |
* Tcl_OpenTcpClient -- |
| 1396 |
* |
| 1397 |
* Opens a TCP client socket and creates a channel around it. |
| 1398 |
* |
| 1399 |
* Results: |
| 1400 |
* The channel or NULL if failed. An error message is returned |
| 1401 |
* in the interpreter on failure. |
| 1402 |
* |
| 1403 |
* Side effects: |
| 1404 |
* Opens a client socket and creates a new channel. |
| 1405 |
* |
| 1406 |
*---------------------------------------------------------------------- |
| 1407 |
*/ |
| 1408 |
|
| 1409 |
Tcl_Channel |
| 1410 |
Tcl_OpenTcpClient(interp, port, host, myaddr, myport, async) |
| 1411 |
Tcl_Interp *interp; /* For error reporting; can be NULL. */ |
| 1412 |
int port; /* Port number to open. */ |
| 1413 |
CONST char *host; /* Host on which to open port. */ |
| 1414 |
CONST char *myaddr; /* Client-side address */ |
| 1415 |
int myport; /* Client-side port */ |
| 1416 |
int async; /* If nonzero, should connect |
| 1417 |
* client socket asynchronously. */ |
| 1418 |
{ |
| 1419 |
SocketInfo *infoPtr; |
| 1420 |
char channelName[16 + TCL_INTEGER_SPACE]; |
| 1421 |
|
| 1422 |
if (TclpHasSockets(interp) != TCL_OK) { |
| 1423 |
return NULL; |
| 1424 |
} |
| 1425 |
|
| 1426 |
/* |
| 1427 |
* Create a new client socket and wrap it in a channel. |
| 1428 |
*/ |
| 1429 |
|
| 1430 |
infoPtr = CreateSocket(interp, port, host, 0, myaddr, myport, async); |
| 1431 |
if (infoPtr == NULL) { |
| 1432 |
return NULL; |
| 1433 |
} |
| 1434 |
|
| 1435 |
wsprintfA(channelName, "sock%d", infoPtr->socket); |
| 1436 |
|
| 1437 |
infoPtr->channel = Tcl_CreateChannel(&tcpChannelType, channelName, |
| 1438 |
(ClientData) infoPtr, (TCL_READABLE | TCL_WRITABLE)); |
| 1439 |
if (Tcl_SetChannelOption(interp, infoPtr->channel, "-translation", |
| 1440 |
"auto crlf") == TCL_ERROR) { |
| 1441 |
Tcl_Close((Tcl_Interp *) NULL, infoPtr->channel); |
| 1442 |
return (Tcl_Channel) NULL; |
| 1443 |
} |
| 1444 |
if (Tcl_SetChannelOption(NULL, infoPtr->channel, "-eofchar", "") |
| 1445 |
== TCL_ERROR) { |
| 1446 |
Tcl_Close((Tcl_Interp *) NULL, infoPtr->channel); |
| 1447 |
return (Tcl_Channel) NULL; |
| 1448 |
} |
| 1449 |
return infoPtr->channel; |
| 1450 |
} |
| 1451 |
|
| 1452 |
/* |
| 1453 |
*---------------------------------------------------------------------- |
| 1454 |
* |
| 1455 |
* Tcl_MakeTcpClientChannel -- |
| 1456 |
* |
| 1457 |
* Creates a Tcl_Channel from an existing client TCP socket. |
| 1458 |
* |
| 1459 |
* Results: |
| 1460 |
* The Tcl_Channel wrapped around the preexisting TCP socket. |
| 1461 |
* |
| 1462 |
* Side effects: |
| 1463 |
* None. |
| 1464 |
* |
| 1465 |
* NOTE: Code contributed by Mark Diekhans ([email protected]) |
| 1466 |
* |
| 1467 |
*---------------------------------------------------------------------- |
| 1468 |
*/ |
| 1469 |
|
| 1470 |
Tcl_Channel |
| 1471 |
Tcl_MakeTcpClientChannel(sock) |
| 1472 |
ClientData sock; /* The socket to wrap up into a channel. */ |
| 1473 |
{ |
| 1474 |
SocketInfo *infoPtr; |
| 1475 |
char channelName[16 + TCL_INTEGER_SPACE]; |
| 1476 |
ThreadSpecificData *tsdPtr; |
| 1477 |
|
| 1478 |
if (TclpHasSockets(NULL) != TCL_OK) { |
| 1479 |
return NULL; |
| 1480 |
} |
| 1481 |
|
| 1482 |
tsdPtr = (ThreadSpecificData *)TclThreadDataKeyGet(&dataKey); |
| 1483 |
|
| 1484 |
/* |
| 1485 |
* Set kernel space buffering and non-blocking. |
| 1486 |
*/ |
| 1487 |
|
| 1488 |
TclSockMinimumBuffers((int) sock, TCP_BUFFER_SIZE); |
| 1489 |
|
| 1490 |
infoPtr = NewSocketInfo((SOCKET) sock); |
| 1491 |
|
| 1492 |
/* |
| 1493 |
* Start watching for read/write events on the socket. |
| 1494 |
*/ |
| 1495 |
|
| 1496 |
infoPtr->selectEvents = FD_READ | FD_CLOSE | FD_WRITE; |
| 1497 |
SendMessage(tsdPtr->hwnd, SOCKET_SELECT, |
| 1498 |
(WPARAM) SELECT, (LPARAM) infoPtr); |
| 1499 |
|
| 1500 |
wsprintfA(channelName, "sock%d", infoPtr->socket); |
| 1501 |
infoPtr->channel = Tcl_CreateChannel(&tcpChannelType, channelName, |
| 1502 |
(ClientData) infoPtr, (TCL_READABLE | TCL_WRITABLE)); |
| 1503 |
Tcl_SetChannelOption(NULL, infoPtr->channel, "-translation", "auto crlf"); |
| 1504 |
return infoPtr->channel; |
| 1505 |
} |
| 1506 |
|
| 1507 |
/* |
| 1508 |
*---------------------------------------------------------------------- |
| 1509 |
* |
| 1510 |
* Tcl_OpenTcpServer -- |
| 1511 |
* |
| 1512 |
* Opens a TCP server socket and creates a channel around it. |
| 1513 |
* |
| 1514 |
* Results: |
| 1515 |
* The channel or NULL if failed. An error message is returned |
| 1516 |
* in the interpreter on failure. |
| 1517 |
* |
| 1518 |
* Side effects: |
| 1519 |
* Opens a server socket and creates a new channel. |
| 1520 |
* |
| 1521 |
*---------------------------------------------------------------------- |
| 1522 |
*/ |
| 1523 |
|
| 1524 |
Tcl_Channel |
| 1525 |
Tcl_OpenTcpServer(interp, port, host, acceptProc, acceptProcData) |
| 1526 |
Tcl_Interp *interp; /* For error reporting - may be |
| 1527 |
* NULL. */ |
| 1528 |
int port; /* Port number to open. */ |
| 1529 |
CONST char *host; /* Name of local host. */ |
| 1530 |
Tcl_TcpAcceptProc *acceptProc; /* Callback for accepting connections |
| 1531 |
* from new clients. */ |
| 1532 |
ClientData acceptProcData; /* Data for the callback. */ |
| 1533 |
{ |
| 1534 |
SocketInfo *infoPtr; |
| 1535 |
char channelName[16 + TCL_INTEGER_SPACE]; |
| 1536 |
|
| 1537 |
if (TclpHasSockets(interp) != TCL_OK) { |
| 1538 |
return NULL; |
| 1539 |
} |
| 1540 |
|
| 1541 |
/* |
| 1542 |
* Create a new client socket and wrap it in a channel. |
| 1543 |
*/ |
| 1544 |
|
| 1545 |
infoPtr = CreateSocket(interp, port, host, 1, NULL, 0, 0); |
| 1546 |
if (infoPtr == NULL) { |
| 1547 |
return NULL; |
| 1548 |
} |
| 1549 |
|
| 1550 |
infoPtr->acceptProc = acceptProc; |
| 1551 |
infoPtr->acceptProcData = acceptProcData; |
| 1552 |
|
| 1553 |
wsprintfA(channelName, "sock%d", infoPtr->socket); |
| 1554 |
|
| 1555 |
infoPtr->channel = Tcl_CreateChannel(&tcpChannelType, channelName, |
| 1556 |
(ClientData) infoPtr, 0); |
| 1557 |
if (Tcl_SetChannelOption(interp, infoPtr->channel, "-eofchar", "") |
| 1558 |
== TCL_ERROR) { |
| 1559 |
Tcl_Close((Tcl_Interp *) NULL, infoPtr->channel); |
| 1560 |
return (Tcl_Channel) NULL; |
| 1561 |
} |
| 1562 |
|
| 1563 |
return infoPtr->channel; |
| 1564 |
} |
| 1565 |
|
| 1566 |
/* |
| 1567 |
*---------------------------------------------------------------------- |
| 1568 |
* |
| 1569 |
* TcpAccept -- |
| 1570 |
* Accept a TCP socket connection. This is called by |
| 1571 |
* SocketEventProc and it in turns calls the registered accept |
| 1572 |
* procedure. |
| 1573 |
* |
| 1574 |
* Results: |
| 1575 |
* None. |
| 1576 |
* |
| 1577 |
* Side effects: |
| 1578 |
* Invokes the accept proc which may invoke arbitrary Tcl code. |
| 1579 |
* |
| 1580 |
*---------------------------------------------------------------------- |
| 1581 |
*/ |
| 1582 |
|
| 1583 |
static void |
| 1584 |
TcpAccept(infoPtr) |
| 1585 |
SocketInfo *infoPtr; /* Socket to accept. */ |
| 1586 |
{ |
| 1587 |
SOCKET newSocket; |
| 1588 |
SocketInfo *newInfoPtr; |
| 1589 |
SOCKADDR_IN addr; |
| 1590 |
int len; |
| 1591 |
char channelName[16 + TCL_INTEGER_SPACE]; |
| 1592 |
ThreadSpecificData *tsdPtr = |
| 1593 |
(ThreadSpecificData *)TclThreadDataKeyGet(&dataKey); |
| 1594 |
|
| 1595 |
/* |
| 1596 |
* Accept the incoming connection request. |
| 1597 |
*/ |
| 1598 |
|
| 1599 |
len = sizeof(SOCKADDR_IN); |
| 1600 |
|
| 1601 |
newSocket = winSock.accept(infoPtr->socket, (SOCKADDR *)&addr, |
| 1602 |
&len); |
| 1603 |
|
| 1604 |
/* |
| 1605 |
* Clear the ready mask so we can detect the next connection request. |
| 1606 |
* Note that connection requests are level triggered, so if there is |
| 1607 |
* a request already pending, a new event will be generated. |
| 1608 |
*/ |
| 1609 |
|
| 1610 |
if (newSocket == INVALID_SOCKET) { |
| 1611 |
infoPtr->acceptEventCount = 0; |
| 1612 |
infoPtr->readyEvents &= ~(FD_ACCEPT); |
| 1613 |
return; |
| 1614 |
} |
| 1615 |
|
| 1616 |
/* |
| 1617 |
* It is possible that more than one FD_ACCEPT has been sent, so an extra |
| 1618 |
* count must be kept. Decrement the count, and reset the readyEvent bit |
| 1619 |
* if the count is no longer > 0. |
| 1620 |
*/ |
| 1621 |
|
| 1622 |
infoPtr->acceptEventCount--; |
| 1623 |
|
| 1624 |
if (infoPtr->acceptEventCount <= 0) { |
| 1625 |
infoPtr->readyEvents &= ~(FD_ACCEPT); |
| 1626 |
} |
| 1627 |
|
| 1628 |
/* |
| 1629 |
* Win-NT has a misfeature that sockets are inherited in child |
| 1630 |
* processes by default. Turn off the inherit bit. |
| 1631 |
*/ |
| 1632 |
|
| 1633 |
SetHandleInformation( (HANDLE) newSocket, HANDLE_FLAG_INHERIT, 0 ); |
| 1634 |
|
| 1635 |
/* |
| 1636 |
* Add this socket to the global list of sockets. |
| 1637 |
*/ |
| 1638 |
|
| 1639 |
newInfoPtr = NewSocketInfo(newSocket); |
| 1640 |
|
| 1641 |
/* |
| 1642 |
* Select on read/write events and create the channel. |
| 1643 |
*/ |
| 1644 |
|
| 1645 |
newInfoPtr->selectEvents = (FD_READ | FD_WRITE | FD_CLOSE); |
| 1646 |
SendMessage(tsdPtr->hwnd, SOCKET_SELECT, |
| 1647 |
(WPARAM) SELECT, (LPARAM) newInfoPtr); |
| 1648 |
|
| 1649 |
wsprintfA(channelName, "sock%d", newInfoPtr->socket); |
| 1650 |
newInfoPtr->channel = Tcl_CreateChannel(&tcpChannelType, channelName, |
| 1651 |
(ClientData) newInfoPtr, (TCL_READABLE | TCL_WRITABLE)); |
| 1652 |
if (Tcl_SetChannelOption(NULL, newInfoPtr->channel, "-translation", |
| 1653 |
"auto crlf") == TCL_ERROR) { |
| 1654 |
Tcl_Close((Tcl_Interp *) NULL, newInfoPtr->channel); |
| 1655 |
return; |
| 1656 |
} |
| 1657 |
if (Tcl_SetChannelOption(NULL, newInfoPtr->channel, "-eofchar", "") |
| 1658 |
== TCL_ERROR) { |
| 1659 |
Tcl_Close((Tcl_Interp *) NULL, newInfoPtr->channel); |
| 1660 |
return; |
| 1661 |
} |
| 1662 |
|
| 1663 |
/* |
| 1664 |
* Invoke the accept callback procedure. |
| 1665 |
*/ |
| 1666 |
|
| 1667 |
if (infoPtr->acceptProc != NULL) { |
| 1668 |
(infoPtr->acceptProc) (infoPtr->acceptProcData, |
| 1669 |
newInfoPtr->channel, |
| 1670 |
winSock.inet_ntoa(addr.sin_addr), |
| 1671 |
winSock.ntohs(addr.sin_port)); |
| 1672 |
} |
| 1673 |
} |
| 1674 |
|
| 1675 |
/* |
| 1676 |
*---------------------------------------------------------------------- |
| 1677 |
* |
| 1678 |
* TcpInputProc -- |
| 1679 |
* |
| 1680 |
* This procedure is called by the generic IO level to read data from |
| 1681 |
* a socket based channel. |
| 1682 |
* |
| 1683 |
* Results: |
| 1684 |
* The number of bytes read or -1 on error. |
| 1685 |
* |
| 1686 |
* Side effects: |
| 1687 |
* Consumes input from the socket. |
| 1688 |
* |
| 1689 |
*---------------------------------------------------------------------- |
| 1690 |
*/ |
| 1691 |
|
| 1692 |
static int |
| 1693 |
TcpInputProc(instanceData, buf, toRead, errorCodePtr) |
| 1694 |
ClientData instanceData; /* The socket state. */ |
| 1695 |
char *buf; /* Where to store data. */ |
| 1696 |
int toRead; /* Maximum number of bytes to read. */ |
| 1697 |
int *errorCodePtr; /* Where to store error codes. */ |
| 1698 |
{ |
| 1699 |
SocketInfo *infoPtr = (SocketInfo *) instanceData; |
| 1700 |
int bytesRead; |
| 1701 |
DWORD error; |
| 1702 |
ThreadSpecificData *tsdPtr = |
| 1703 |
(ThreadSpecificData *)TclThreadDataKeyGet(&dataKey); |
| 1704 |
|
| 1705 |
*errorCodePtr = 0; |
| 1706 |
|
| 1707 |
/* |
| 1708 |
* Check that WinSock is initialized; do not call it if not, to |
| 1709 |
* prevent system crashes. This can happen at exit time if the exit |
| 1710 |
* handler for WinSock ran before other exit handlers that want to |
| 1711 |
* use sockets. |
| 1712 |
*/ |
| 1713 |
|
| 1714 |
if (!SocketsEnabled()) { |
| 1715 |
*errorCodePtr = EFAULT; |
| 1716 |
return -1; |
| 1717 |
} |
| 1718 |
|
| 1719 |
/* |
| 1720 |
* First check to see if EOF was already detected, to prevent |
| 1721 |
* calling the socket stack after the first time EOF is detected. |
| 1722 |
*/ |
| 1723 |
|
| 1724 |
if (infoPtr->flags & SOCKET_EOF) { |
| 1725 |
return 0; |
| 1726 |
} |
| 1727 |
|
| 1728 |
/* |
| 1729 |
* Check to see if the socket is connected before trying to read. |
| 1730 |
*/ |
| 1731 |
|
| 1732 |
/* |
| 1733 |
* Check to see if the socket is connected before trying to read. |
| 1734 |
*/ |
| 1735 |
|
| 1736 |
if ((infoPtr->flags & SOCKET_ASYNC_CONNECT) |
| 1737 |
&& ! WaitForSocketEvent(infoPtr, FD_CONNECT, errorCodePtr)) { |
| 1738 |
return -1; |
| 1739 |
} |
| 1740 |
|
| 1741 |
/* |
| 1742 |
* No EOF, and it is connected, so try to read more from the socket. |
| 1743 |
* Note that we clear the FD_READ bit because read events are level |
| 1744 |
* triggered so a new event will be generated if there is still data |
| 1745 |
* available to be read. We have to simulate blocking behavior here |
| 1746 |
* since we are always using non-blocking sockets. |
| 1747 |
*/ |
| 1748 |
|
| 1749 |
while (1) { |
| 1750 |
SendMessage(tsdPtr->hwnd, SOCKET_SELECT, |
| 1751 |
(WPARAM) UNSELECT, (LPARAM) infoPtr); |
| 1752 |
bytesRead = winSock.recv(infoPtr->socket, buf, toRead, 0); |
| 1753 |
infoPtr->readyEvents &= ~(FD_READ); |
| 1754 |
|
| 1755 |
/* |
| 1756 |
* Check for end-of-file condition or successful read. |
| 1757 |
*/ |
| 1758 |
|
| 1759 |
if (bytesRead == 0) { |
| 1760 |
infoPtr->flags |= SOCKET_EOF; |
| 1761 |
} |
| 1762 |
if (bytesRead != SOCKET_ERROR) { |
| 1763 |
break; |
| 1764 |
} |
| 1765 |
|
| 1766 |
/* |
| 1767 |
* If an error occurs after the FD_CLOSE has arrived, |
| 1768 |
* then ignore the error and report an EOF. |
| 1769 |
*/ |
| 1770 |
|
| 1771 |
if (infoPtr->readyEvents & FD_CLOSE) { |
| 1772 |
infoPtr->flags |= SOCKET_EOF; |
| 1773 |
bytesRead = 0; |
| 1774 |
break; |
| 1775 |
} |
| 1776 |
|
| 1777 |
/* |
| 1778 |
* Check for error condition or underflow in non-blocking case. |
| 1779 |
*/ |
| 1780 |
|
| 1781 |
error = winSock.WSAGetLastError(); |
| 1782 |
if ((infoPtr->flags & SOCKET_ASYNC) || (error != WSAEWOULDBLOCK)) { |
| 1783 |
TclWinConvertWSAError(error); |
| 1784 |
*errorCodePtr = Tcl_GetErrno(); |
| 1785 |
bytesRead = -1; |
| 1786 |
break; |
| 1787 |
} |
| 1788 |
|
| 1789 |
/* |
| 1790 |
* In the blocking case, wait until the file becomes readable |
| 1791 |
* or closed and try again. |
| 1792 |
*/ |
| 1793 |
|
| 1794 |
if (!WaitForSocketEvent(infoPtr, FD_READ|FD_CLOSE, errorCodePtr)) { |
| 1795 |
bytesRead = -1; |
| 1796 |
break; |
| 1797 |
} |
| 1798 |
} |
| 1799 |
|
| 1800 |
SendMessage(tsdPtr->hwnd, SOCKET_SELECT, |
| 1801 |
(WPARAM) SELECT, (LPARAM) infoPtr); |
| 1802 |
|
| 1803 |
return bytesRead; |
| 1804 |
} |
| 1805 |
|
| 1806 |
/* |
| 1807 |
*---------------------------------------------------------------------- |
| 1808 |
* |
| 1809 |
* TcpOutputProc -- |
| 1810 |
* |
| 1811 |
* This procedure is called by the generic IO level to write data |
| 1812 |
* to a socket based channel. |
| 1813 |
* |
| 1814 |
* Results: |
| 1815 |
* The number of bytes written or -1 on failure. |
| 1816 |
* |
| 1817 |
* Side effects: |
| 1818 |
* Produces output on the socket. |
| 1819 |
* |
| 1820 |
*---------------------------------------------------------------------- |
| 1821 |
*/ |
| 1822 |
|
| 1823 |
static int |
| 1824 |
TcpOutputProc(instanceData, buf, toWrite, errorCodePtr) |
| 1825 |
ClientData instanceData; /* The socket state. */ |
| 1826 |
CONST char *buf; /* Where to get data. */ |
| 1827 |
int toWrite; /* Maximum number of bytes to write. */ |
| 1828 |
int *errorCodePtr; /* Where to store error codes. */ |
| 1829 |
{ |
| 1830 |
SocketInfo *infoPtr = (SocketInfo *) instanceData; |
| 1831 |
int bytesWritten; |
| 1832 |
DWORD error; |
| 1833 |
ThreadSpecificData *tsdPtr = |
| 1834 |
(ThreadSpecificData *)TclThreadDataKeyGet(&dataKey); |
| 1835 |
|
| 1836 |
*errorCodePtr = 0; |
| 1837 |
|
| 1838 |
/* |
| 1839 |
* Check that WinSock is initialized; do not call it if not, to |
| 1840 |
* prevent system crashes. This can happen at exit time if the exit |
| 1841 |
* handler for WinSock ran before other exit handlers that want to |
| 1842 |
* use sockets. |
| 1843 |
*/ |
| 1844 |
|
| 1845 |
if (!SocketsEnabled()) { |
| 1846 |
*errorCodePtr = EFAULT; |
| 1847 |
return -1; |
| 1848 |
} |
| 1849 |
|
| 1850 |
/* |
| 1851 |
* Check to see if the socket is connected before trying to write. |
| 1852 |
*/ |
| 1853 |
|
| 1854 |
if ((infoPtr->flags & SOCKET_ASYNC_CONNECT) |
| 1855 |
&& ! WaitForSocketEvent(infoPtr, FD_CONNECT, errorCodePtr)) { |
| 1856 |
return -1; |
| 1857 |
} |
| 1858 |
|
| 1859 |
while (1) { |
| 1860 |
SendMessage(tsdPtr->hwnd, SOCKET_SELECT, |
| 1861 |
(WPARAM) UNSELECT, (LPARAM) infoPtr); |
| 1862 |
|
| 1863 |
bytesWritten = winSock.send(infoPtr->socket, buf, toWrite, 0); |
| 1864 |
if (bytesWritten != SOCKET_ERROR) { |
| 1865 |
/* |
| 1866 |
* Since Windows won't generate a new write event until we hit |
| 1867 |
* an overflow condition, we need to force the event loop to |
| 1868 |
* poll until the condition changes. |
| 1869 |
*/ |
| 1870 |
|
| 1871 |
if (infoPtr->watchEvents & FD_WRITE) { |
| 1872 |
Tcl_Time blockTime = { 0, 0 }; |
| 1873 |
Tcl_SetMaxBlockTime(&blockTime); |
| 1874 |
} |
| 1875 |
break; |
| 1876 |
} |
| 1877 |
|
| 1878 |
/* |
| 1879 |
* Check for error condition or overflow. In the event of overflow, we |
| 1880 |
* need to clear the FD_WRITE flag so we can detect the next writable |
| 1881 |
* event. Note that Windows only sends a new writable event after a |
| 1882 |
* send fails with WSAEWOULDBLOCK. |
| 1883 |
*/ |
| 1884 |
|
| 1885 |
error = winSock.WSAGetLastError(); |
| 1886 |
if (error == WSAEWOULDBLOCK) { |
| 1887 |
infoPtr->readyEvents &= ~(FD_WRITE); |
| 1888 |
if (infoPtr->flags & SOCKET_ASYNC) { |
| 1889 |
*errorCodePtr = EWOULDBLOCK; |
| 1890 |
bytesWritten = -1; |
| 1891 |
break; |
| 1892 |
} |
| 1893 |
} else { |
| 1894 |
TclWinConvertWSAError(error); |
| 1895 |
*errorCodePtr = Tcl_GetErrno(); |
| 1896 |
bytesWritten = -1; |
| 1897 |
break; |
| 1898 |
} |
| 1899 |
|
| 1900 |
/* |
| 1901 |
* In the blocking case, wait until the file becomes writable |
| 1902 |
* or closed and try again. |
| 1903 |
*/ |
| 1904 |
|
| 1905 |
if (!WaitForSocketEvent(infoPtr, FD_WRITE|FD_CLOSE, errorCodePtr)) { |
| 1906 |
bytesWritten = -1; |
| 1907 |
break; |
| 1908 |
} |
| 1909 |
} |
| 1910 |
|
| 1911 |
SendMessage(tsdPtr->hwnd, SOCKET_SELECT, |
| 1912 |
(WPARAM) SELECT, (LPARAM) infoPtr); |
| 1913 |
|
| 1914 |
return bytesWritten; |
| 1915 |
} |
| 1916 |
|
| 1917 |
/* |
| 1918 |
*---------------------------------------------------------------------- |
| 1919 |
* |
| 1920 |
* TcpSetOptionProc -- |
| 1921 |
* |
| 1922 |
* Sets Tcp channel specific options. |
| 1923 |
* |
| 1924 |
* Results: |
| 1925 |
* None, unless an error happens. |
| 1926 |
* |
| 1927 |
* Side effects: |
| 1928 |
* Changes attributes of the socket at the system level. |
| 1929 |
* |
| 1930 |
*---------------------------------------------------------------------- |
| 1931 |
*/ |
| 1932 |
|
| 1933 |
static int |
| 1934 |
TcpSetOptionProc ( |
| 1935 |
ClientData instanceData, /* Socket state. */ |
| 1936 |
Tcl_Interp *interp, /* For error reporting - can be NULL. */ |
| 1937 |
CONST char *optionName, /* Name of the option to set. */ |
| 1938 |
CONST char *value) /* New value for option. */ |
| 1939 |
{ |
| 1940 |
SocketInfo *infoPtr; |
| 1941 |
SOCKET sock; |
| 1942 |
/* |
| 1943 |
BOOL val = FALSE; |
| 1944 |
int boolVar, rtn; |
| 1945 |
*/ |
| 1946 |
/* |
| 1947 |
* Check that WinSock is initialized; do not call it if not, to |
| 1948 |
* prevent system crashes. This can happen at exit time if the exit |
| 1949 |
* handler for WinSock ran before other exit handlers that want to |
| 1950 |
* use sockets. |
| 1951 |
*/ |
| 1952 |
|
| 1953 |
if (!SocketsEnabled()) { |
| 1954 |
if (interp) { |
| 1955 |
Tcl_AppendResult(interp, "winsock is not initialized", NULL); |
| 1956 |
} |
| 1957 |
return TCL_ERROR; |
| 1958 |
} |
| 1959 |
|
| 1960 |
infoPtr = (SocketInfo *) instanceData; |
| 1961 |
sock = infoPtr->socket; |
| 1962 |
|
| 1963 |
/* |
| 1964 |
if (!stricmp(optionName, "-keepalive")) { |
| 1965 |
if (Tcl_GetBoolean(interp, value, &boolVar) != TCL_OK) { |
| 1966 |
return TCL_ERROR; |
| 1967 |
} |
| 1968 |
if (boolVar) val = TRUE; |
| 1969 |
rtn = winSock.setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, |
| 1970 |
(const char *) &val, sizeof(BOOL)); |
| 1971 |
if (rtn != 0) { |
| 1972 |
TclWinConvertWSAError(winSock.WSAGetLastError()); |
| 1973 |
if (interp) { |
| 1974 |
Tcl_AppendResult(interp, "couldn't set socket option: ", |
| 1975 |
Tcl_PosixError(interp), NULL); |
| 1976 |
} |
| 1977 |
return TCL_ERROR; |
| 1978 |
} |
| 1979 |
return TCL_OK; |
| 1980 |
|
| 1981 |
} else if (!stricmp(optionName, "-nagle")) { |
| 1982 |
if (Tcl_GetBoolean(interp, value, &boolVar) != TCL_OK) { |
| 1983 |
return TCL_ERROR; |
| 1984 |
} |
| 1985 |
if (!boolVar) val = TRUE; |
| 1986 |
rtn = winSock.setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, |
| 1987 |
(const char *) &val, sizeof(BOOL)); |
| 1988 |
if (rtn != 0) { |
| 1989 |
TclWinConvertWSAError(winSock.WSAGetLastError()); |
| 1990 |
if (interp) { |
| 1991 |
Tcl_AppendResult(interp, "couldn't set socket option: ", |
| 1992 |
Tcl_PosixError(interp), NULL); |
| 1993 |
} |
| 1994 |
return TCL_ERROR; |
| 1995 |
} |
| 1996 |
return TCL_OK; |
| 1997 |
} |
| 1998 |
|
| 1999 |
return Tcl_BadChannelOption(interp, optionName, "keepalive nagle"); |
| 2000 |
*/ |
| 2001 |
return Tcl_BadChannelOption(interp, optionName, ""); |
| 2002 |
} |
| 2003 |
|
| 2004 |
/* |
| 2005 |
*---------------------------------------------------------------------- |
| 2006 |
* |
| 2007 |
* TcpGetOptionProc -- |
| 2008 |
* |
| 2009 |
* Computes an option value for a TCP socket based channel, or a |
| 2010 |
* list of all options and their values. |
| 2011 |
* |
| 2012 |
* Note: This code is based on code contributed by John Haxby. |
| 2013 |
* |
| 2014 |
* Results: |
| 2015 |
* A standard Tcl result. The value of the specified option or a |
| 2016 |
* list of all options and their values is returned in the |
| 2017 |
* supplied DString. |
| 2018 |
* |
| 2019 |
* Side effects: |
| 2020 |
* None. |
| 2021 |
* |
| 2022 |
*---------------------------------------------------------------------- |
| 2023 |
*/ |
| 2024 |
|
| 2025 |
static int |
| 2026 |
TcpGetOptionProc(instanceData, interp, optionName, dsPtr) |
| 2027 |
ClientData instanceData; /* Socket state. */ |
| 2028 |
Tcl_Interp *interp; /* For error reporting - can be NULL */ |
| 2029 |
CONST char *optionName; /* Name of the option to |
| 2030 |
* retrieve the value for, or |
| 2031 |
* NULL to get all options and |
| 2032 |
* their values. */ |
| 2033 |
Tcl_DString *dsPtr; /* Where to store the computed |
| 2034 |
* value; initialized by caller. */ |
| 2035 |
{ |
| 2036 |
SocketInfo *infoPtr; |
| 2037 |
SOCKADDR_IN sockname; |
| 2038 |
SOCKADDR_IN peername; |
| 2039 |
struct hostent *hostEntPtr; |
| 2040 |
SOCKET sock; |
| 2041 |
int size = sizeof(SOCKADDR_IN); |
| 2042 |
size_t len = 0; |
| 2043 |
char buf[TCL_INTEGER_SPACE]; |
| 2044 |
|
| 2045 |
/* |
| 2046 |
* Check that WinSock is initialized; do not call it if not, to |
| 2047 |
* prevent system crashes. This can happen at exit time if the exit |
| 2048 |
* handler for WinSock ran before other exit handlers that want to |
| 2049 |
* use sockets. |
| 2050 |
*/ |
| 2051 |
|
| 2052 |
if (!SocketsEnabled()) { |
| 2053 |
if (interp) { |
| 2054 |
Tcl_AppendResult(interp, "winsock is not initialized", NULL); |
| 2055 |
} |
| 2056 |
return TCL_ERROR; |
| 2057 |
} |
| 2058 |
|
| 2059 |
infoPtr = (SocketInfo *) instanceData; |
| 2060 |
sock = (int) infoPtr->socket; |
| 2061 |
if (optionName != (char *) NULL) { |
| 2062 |
len = strlen(optionName); |
| 2063 |
} |
| 2064 |
|
| 2065 |
if ((len > 1) && (optionName[1] == 'e') && |
| 2066 |
(strncmp(optionName, "-error", len) == 0)) { |
| 2067 |
int optlen; |
| 2068 |
DWORD err; |
| 2069 |
int ret; |
| 2070 |
|
| 2071 |
optlen = sizeof(int); |
| 2072 |
ret = TclWinGetSockOpt(sock, SOL_SOCKET, SO_ERROR, |
| 2073 |
(char *)&err, &optlen); |
| 2074 |
if (ret == SOCKET_ERROR) { |
| 2075 |
err = winSock.WSAGetLastError(); |
| 2076 |
} |
| 2077 |
if (err) { |
| 2078 |
TclWinConvertWSAError(err); |
| 2079 |
Tcl_DStringAppend(dsPtr, Tcl_ErrnoMsg(Tcl_GetErrno()), -1); |
| 2080 |
} |
| 2081 |
return TCL_OK; |
| 2082 |
} |
| 2083 |
|
| 2084 |
if ((len == 0) || |
| 2085 |
((len > 1) && (optionName[1] == 'p') && |
| 2086 |
(strncmp(optionName, "-peername", len) == 0))) { |
| 2087 |
if (winSock.getpeername(sock, (LPSOCKADDR) &peername, &size) |
| 2088 |
== 0) { |
| 2089 |
if (len == 0) { |
| 2090 |
Tcl_DStringAppendElement(dsPtr, "-peername"); |
| 2091 |
Tcl_DStringStartSublist(dsPtr); |
| 2092 |
} |
| 2093 |
Tcl_DStringAppendElement(dsPtr, |
| 2094 |
winSock.inet_ntoa(peername.sin_addr)); |
| 2095 |
|
| 2096 |
if (peername.sin_addr.s_addr == 0) { |
| 2097 |
hostEntPtr = (struct hostent *) NULL; |
| 2098 |
} else { |
| 2099 |
hostEntPtr = winSock.gethostbyaddr( |
| 2100 |
(char *) &(peername.sin_addr), sizeof(peername.sin_addr), |
| 2101 |
AF_INET); |
| 2102 |
} |
| 2103 |
if (hostEntPtr != (struct hostent *) NULL) { |
| 2104 |
Tcl_DStringAppendElement(dsPtr, hostEntPtr->h_name); |
| 2105 |
} else { |
| 2106 |
Tcl_DStringAppendElement(dsPtr, |
| 2107 |
winSock.inet_ntoa(peername.sin_addr)); |
| 2108 |
} |
| 2109 |
TclFormatInt(buf, winSock.ntohs(peername.sin_port)); |
| 2110 |
Tcl_DStringAppendElement(dsPtr, buf); |
| 2111 |
if (len == 0) { |
| 2112 |
Tcl_DStringEndSublist(dsPtr); |
| 2113 |
} else { |
| 2114 |
return TCL_OK; |
| 2115 |
} |
| 2116 |
} else { |
| 2117 |
/* |
| 2118 |
* getpeername failed - but if we were asked for all the options |
| 2119 |
* (len==0), don't flag an error at that point because it could |
| 2120 |
* be an fconfigure request on a server socket. (which have |
| 2121 |
* no peer). {copied from unix/tclUnixChan.c} |
| 2122 |
*/ |
| 2123 |
if (len) { |
| 2124 |
TclWinConvertWSAError((DWORD) winSock.WSAGetLastError()); |
| 2125 |
if (interp) { |
| 2126 |
Tcl_AppendResult(interp, "can't get peername: ", |
| 2127 |
Tcl_PosixError(interp), |
| 2128 |
(char *) NULL); |
| 2129 |
} |
| 2130 |
return TCL_ERROR; |
| 2131 |
} |
| 2132 |
} |
| 2133 |
} |
| 2134 |
|
| 2135 |
if ((len == 0) || |
| 2136 |
((len > 1) && (optionName[1] == 's') && |
| 2137 |
(strncmp(optionName, "-sockname", len) == 0))) { |
| 2138 |
if (winSock.getsockname(sock, (LPSOCKADDR) &sockname, &size) |
| 2139 |
== 0) { |
| 2140 |
if (len == 0) { |
| 2141 |
Tcl_DStringAppendElement(dsPtr, "-sockname"); |
| 2142 |
Tcl_DStringStartSublist(dsPtr); |
| 2143 |
} |
| 2144 |
Tcl_DStringAppendElement(dsPtr, |
| 2145 |
winSock.inet_ntoa(sockname.sin_addr)); |
| 2146 |
if (sockname.sin_addr.s_addr == 0) { |
| 2147 |
hostEntPtr = (struct hostent *) NULL; |
| 2148 |
} else { |
| 2149 |
hostEntPtr = winSock.gethostbyaddr( |
| 2150 |
(char *) &(sockname.sin_addr), sizeof(peername.sin_addr), |
| 2151 |
AF_INET); |
| 2152 |
} |
| 2153 |
if (hostEntPtr != (struct hostent *) NULL) { |
| 2154 |
Tcl_DStringAppendElement(dsPtr, hostEntPtr->h_name); |
| 2155 |
} else { |
| 2156 |
Tcl_DStringAppendElement(dsPtr, |
| 2157 |
winSock.inet_ntoa(sockname.sin_addr)); |
| 2158 |
} |
| 2159 |
TclFormatInt(buf, winSock.ntohs(sockname.sin_port)); |
| 2160 |
Tcl_DStringAppendElement(dsPtr, buf); |
| 2161 |
if (len == 0) { |
| 2162 |
Tcl_DStringEndSublist(dsPtr); |
| 2163 |
} else { |
| 2164 |
return TCL_OK; |
| 2165 |
} |
| 2166 |
} else { |
| 2167 |
if (interp) { |
| 2168 |
TclWinConvertWSAError((DWORD) winSock.WSAGetLastError()); |
| 2169 |
Tcl_AppendResult(interp, "can't get sockname: ", |
| 2170 |
Tcl_PosixError(interp), |
| 2171 |
(char *) NULL); |
| 2172 |
} |
| 2173 |
return TCL_ERROR; |
| 2174 |
} |
| 2175 |
} |
| 2176 |
|
| 2177 |
/* |
| 2178 |
if (len == 0 || !strncmp(optionName, "-keepalive", len)) { |
| 2179 |
int optlen; |
| 2180 |
BOOL opt = FALSE; |
| 2181 |
|
| 2182 |
if (len == 0) { |
| 2183 |
Tcl_DStringAppendElement(dsPtr, "-keepalive"); |
| 2184 |
} |
| 2185 |
optlen = sizeof(BOOL); |
| 2186 |
winSock.getsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char *)&opt, |
| 2187 |
&optlen); |
| 2188 |
if (opt) { |
| 2189 |
Tcl_DStringAppendElement(dsPtr, "1"); |
| 2190 |
} else { |
| 2191 |
Tcl_DStringAppendElement(dsPtr, "0"); |
| 2192 |
} |
| 2193 |
if (len > 0) return TCL_OK; |
| 2194 |
} |
| 2195 |
|
| 2196 |
if (len == 0 || !strncmp(optionName, "-nagle", len)) { |
| 2197 |
int optlen; |
| 2198 |
BOOL opt = FALSE; |
| 2199 |
|
| 2200 |
if (len == 0) { |
| 2201 |
Tcl_DStringAppendElement(dsPtr, "-nagle"); |
| 2202 |
} |
| 2203 |
optlen = sizeof(BOOL); |
| 2204 |
winSock.getsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *)&opt, |
| 2205 |
&optlen); |
| 2206 |
if (opt) { |
| 2207 |
Tcl_DStringAppendElement(dsPtr, "0"); |
| 2208 |
} else { |
| 2209 |
Tcl_DStringAppendElement(dsPtr, "1"); |
| 2210 |
} |
| 2211 |
if (len > 0) return TCL_OK; |
| 2212 |
} |
| 2213 |
*/ |
| 2214 |
|
| 2215 |
if (len > 0) { |
| 2216 |
/*return Tcl_BadChannelOption(interp, optionName, "peername sockname keepalive nagle");*/ |
| 2217 |
return Tcl_BadChannelOption(interp, optionName, "peername sockname"); |
| 2218 |
} |
| 2219 |
|
| 2220 |
return TCL_OK; |
| 2221 |
} |
| 2222 |
|
| 2223 |
/* |
| 2224 |
*---------------------------------------------------------------------- |
| 2225 |
* |
| 2226 |
* TcpWatchProc -- |
| 2227 |
* |
| 2228 |
* Informs the channel driver of the events that the generic |
| 2229 |
* channel code wishes to receive on this socket. |
| 2230 |
* |
| 2231 |
* Results: |
| 2232 |
* None. |
| 2233 |
* |
| 2234 |
* Side effects: |
| 2235 |
* May cause the notifier to poll if any of the specified |
| 2236 |
* conditions are already true. |
| 2237 |
* |
| 2238 |
*---------------------------------------------------------------------- |
| 2239 |
*/ |
| 2240 |
|
| 2241 |
static void |
| 2242 |
TcpWatchProc(instanceData, mask) |
| 2243 |
ClientData instanceData; /* The socket state. */ |
| 2244 |
int mask; /* Events of interest; an OR-ed |
| 2245 |
* combination of TCL_READABLE, |
| 2246 |
* TCL_WRITABLE and TCL_EXCEPTION. */ |
| 2247 |
{ |
| 2248 |
SocketInfo *infoPtr = (SocketInfo *) instanceData; |
| 2249 |
|
| 2250 |
/* |
| 2251 |
* Update the watch events mask. Only if the socket is not a |
| 2252 |
* server socket. Fix for SF Tcl Bug #557878. |
| 2253 |
*/ |
| 2254 |
|
| 2255 |
if (!infoPtr->acceptProc) { |
| 2256 |
infoPtr->watchEvents = 0; |
| 2257 |
if (mask & TCL_READABLE) { |
| 2258 |
infoPtr->watchEvents |= (FD_READ|FD_CLOSE|FD_ACCEPT); |
| 2259 |
} |
| 2260 |
if (mask & TCL_WRITABLE) { |
| 2261 |
infoPtr->watchEvents |= (FD_WRITE|FD_CLOSE|FD_CONNECT); |
| 2262 |
} |
| 2263 |
|
| 2264 |
/* |
| 2265 |
* If there are any conditions already set, then tell the notifier to poll |
| 2266 |
* rather than block. |
| 2267 |
*/ |
| 2268 |
|
| 2269 |
if (infoPtr->readyEvents & infoPtr->watchEvents) { |
| 2270 |
Tcl_Time blockTime = { 0, 0 }; |
| 2271 |
Tcl_SetMaxBlockTime(&blockTime); |
| 2272 |
} |
| 2273 |
} |
| 2274 |
} |
| 2275 |
|
| 2276 |
/* |
| 2277 |
*---------------------------------------------------------------------- |
| 2278 |
* |
| 2279 |
* TcpGetProc -- |
| 2280 |
* |
| 2281 |
* Called from Tcl_GetChannelHandle to retrieve an OS handle from inside |
| 2282 |
* a TCP socket based channel. |
| 2283 |
* |
| 2284 |
* Results: |
| 2285 |
* Returns TCL_OK with the socket in handlePtr. |
| 2286 |
* |
| 2287 |
* Side effects: |
| 2288 |
* None. |
| 2289 |
* |
| 2290 |
*---------------------------------------------------------------------- |
| 2291 |
*/ |
| 2292 |
|
| 2293 |
static int |
| 2294 |
TcpGetHandleProc(instanceData, direction, handlePtr) |
| 2295 |
ClientData instanceData; /* The socket state. */ |
| 2296 |
int direction; /* Not used. */ |
| 2297 |
ClientData *handlePtr; /* Where to store the handle. */ |
| 2298 |
{ |
| 2299 |
SocketInfo *statePtr = (SocketInfo *) instanceData; |
| 2300 |
|
| 2301 |
*handlePtr = (ClientData) statePtr->socket; |
| 2302 |
return TCL_OK; |
| 2303 |
} |
| 2304 |
|
| 2305 |
/* |
| 2306 |
*---------------------------------------------------------------------- |
| 2307 |
* |
| 2308 |
* SocketThread -- |
| 2309 |
* |
| 2310 |
* Helper thread used to manage the socket event handling window. |
| 2311 |
* |
| 2312 |
* Results: |
| 2313 |
* 1 if unable to create socket event window, 0 otherwise. |
| 2314 |
* |
| 2315 |
* Side effects: |
| 2316 |
* None. |
| 2317 |
* |
| 2318 |
*---------------------------------------------------------------------- |
| 2319 |
*/ |
| 2320 |
|
| 2321 |
static DWORD WINAPI |
| 2322 |
SocketThread(LPVOID arg) |
| 2323 |
{ |
| 2324 |
MSG msg; |
| 2325 |
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)(arg); |
| 2326 |
|
| 2327 |
tsdPtr->hwnd = CreateWindow("TclSocket", "TclSocket", |
| 2328 |
WS_TILED, 0, 0, 0, 0, NULL, NULL, windowClass.hInstance, arg); |
| 2329 |
|
| 2330 |
/* |
| 2331 |
* Signal the main thread that the window has been created |
| 2332 |
* and that the socket thread is ready to go. |
| 2333 |
*/ |
| 2334 |
|
| 2335 |
SetEvent(tsdPtr->readyEvent); |
| 2336 |
|
| 2337 |
if (tsdPtr->hwnd == NULL) { |
| 2338 |
return 1; |
| 2339 |
} |
| 2340 |
|
| 2341 |
/* |
| 2342 |
* Process all messages on the socket window until WM_QUIT. |
| 2343 |
*/ |
| 2344 |
|
| 2345 |
while (GetMessage(&msg, NULL, 0, 0) > 0) { |
| 2346 |
DispatchMessage(&msg); |
| 2347 |
} |
| 2348 |
|
| 2349 |
return msg.wParam; |
| 2350 |
} |
| 2351 |
|
| 2352 |
|
| 2353 |
/* |
| 2354 |
*---------------------------------------------------------------------- |
| 2355 |
* |
| 2356 |
* SocketProc -- |
| 2357 |
* |
| 2358 |
* This function is called when WSAAsyncSelect has been used |
| 2359 |
* to register interest in a socket event, and the event has |
| 2360 |
* occurred. |
| 2361 |
* |
| 2362 |
* Results: |
| 2363 |
* 0 on success. |
| 2364 |
* |
| 2365 |
* Side effects: |
| 2366 |
* The flags for the given socket are updated to reflect the |
| 2367 |
* event that occured. |
| 2368 |
* |
| 2369 |
*---------------------------------------------------------------------- |
| 2370 |
*/ |
| 2371 |
|
| 2372 |
static LRESULT CALLBACK |
| 2373 |
SocketProc(hwnd, message, wParam, lParam) |
| 2374 |
HWND hwnd; |
| 2375 |
UINT message; |
| 2376 |
WPARAM wParam; |
| 2377 |
LPARAM lParam; |
| 2378 |
{ |
| 2379 |
int event, error; |
| 2380 |
SOCKET socket; |
| 2381 |
SocketInfo *infoPtr; |
| 2382 |
ThreadSpecificData *tsdPtr = |
| 2383 |
#ifdef _WIN64 |
| 2384 |
(ThreadSpecificData *) GetWindowLongPtr(hwnd, GWLP_USERDATA); |
| 2385 |
#else |
| 2386 |
(ThreadSpecificData *) GetWindowLong(hwnd, GWL_USERDATA); |
| 2387 |
#endif |
| 2388 |
|
| 2389 |
switch (message) { |
| 2390 |
|
| 2391 |
default: |
| 2392 |
return DefWindowProc(hwnd, message, wParam, lParam); |
| 2393 |
break; |
| 2394 |
|
| 2395 |
case WM_CREATE: |
| 2396 |
/* |
| 2397 |
* store the initial tsdPtr, it's from a different thread, so it's |
| 2398 |
* not directly accessible, but needed. |
| 2399 |
*/ |
| 2400 |
|
| 2401 |
#ifdef _WIN64 |
| 2402 |
SetWindowLongPtr(hwnd, GWLP_USERDATA, |
| 2403 |
(LONG_PTR) ((LPCREATESTRUCT)lParam)->lpCreateParams); |
| 2404 |
#else |
| 2405 |
SetWindowLong(hwnd, GWL_USERDATA, |
| 2406 |
(LONG) ((LPCREATESTRUCT)lParam)->lpCreateParams); |
| 2407 |
#endif |
| 2408 |
break; |
| 2409 |
|
| 2410 |
case WM_DESTROY: |
| 2411 |
PostQuitMessage(0); |
| 2412 |
break; |
| 2413 |
|
| 2414 |
case SOCKET_MESSAGE: |
| 2415 |
event = WSAGETSELECTEVENT(lParam); |
| 2416 |
error = WSAGETSELECTERROR(lParam); |
| 2417 |
socket = (SOCKET) wParam; |
| 2418 |
|
| 2419 |
/* |
| 2420 |
* Find the specified socket on the socket list and update its |
| 2421 |
* eventState flag. |
| 2422 |
*/ |
| 2423 |
|
| 2424 |
WaitForSingleObject(tsdPtr->socketListLock, INFINITE); |
| 2425 |
for (infoPtr = tsdPtr->socketList; infoPtr != NULL; |
| 2426 |
infoPtr = infoPtr->nextPtr) { |
| 2427 |
if (infoPtr->socket == socket) { |
| 2428 |
/* |
| 2429 |
* Update the socket state. |
| 2430 |
*/ |
| 2431 |
|
| 2432 |
/* |
| 2433 |
* A count of FD_ACCEPTS is stored, so if an FD_CLOSE |
| 2434 |
* event happens, then clear the FD_ACCEPT count. |
| 2435 |
* Otherwise, increment the count if the current |
| 2436 |
* event is an FD_ACCEPT. |
| 2437 |
*/ |
| 2438 |
|
| 2439 |
if (event & FD_CLOSE) { |
| 2440 |
infoPtr->acceptEventCount = 0; |
| 2441 |
infoPtr->readyEvents &= ~(FD_WRITE|FD_ACCEPT); |
| 2442 |
} else if (event & FD_ACCEPT) { |
| 2443 |
infoPtr->acceptEventCount++; |
| 2444 |
} |
| 2445 |
|
| 2446 |
if (event & FD_CONNECT) { |
| 2447 |
/* |
| 2448 |
* The socket is now connected, |
| 2449 |
* clear the async connect flag. |
| 2450 |
*/ |
| 2451 |
|
| 2452 |
infoPtr->flags &= ~(SOCKET_ASYNC_CONNECT); |
| 2453 |
|
| 2454 |
/* |
| 2455 |
* Remember any error that occurred so we can report |
| 2456 |
* connection failures. |
| 2457 |
*/ |
| 2458 |
|
| 2459 |
if (error != ERROR_SUCCESS) { |
| 2460 |
TclWinConvertWSAError((DWORD) error); |
| 2461 |
infoPtr->lastError = Tcl_GetErrno(); |
| 2462 |
} |
| 2463 |
|
| 2464 |
} |
| 2465 |
if(infoPtr->flags & SOCKET_ASYNC_CONNECT) { |
| 2466 |
infoPtr->flags &= ~(SOCKET_ASYNC_CONNECT); |
| 2467 |
if (error != ERROR_SUCCESS) { |
| 2468 |
TclWinConvertWSAError((DWORD) error); |
| 2469 |
infoPtr->lastError = Tcl_GetErrno(); |
| 2470 |
} |
| 2471 |
infoPtr->readyEvents |= FD_WRITE; |
| 2472 |
} |
| 2473 |
infoPtr->readyEvents |= event; |
| 2474 |
|
| 2475 |
/* |
| 2476 |
* Wake up the Main Thread. |
| 2477 |
*/ |
| 2478 |
SetEvent(tsdPtr->readyEvent); |
| 2479 |
Tcl_ThreadAlert(tsdPtr->threadId); |
| 2480 |
break; |
| 2481 |
} |
| 2482 |
} |
| 2483 |
SetEvent(tsdPtr->socketListLock); |
| 2484 |
break; |
| 2485 |
|
| 2486 |
case SOCKET_SELECT: |
| 2487 |
infoPtr = (SocketInfo *) lParam; |
| 2488 |
if (wParam == SELECT) { |
| 2489 |
|
| 2490 |
winSock.WSAAsyncSelect(infoPtr->socket, hwnd, |
| 2491 |
SOCKET_MESSAGE, infoPtr->selectEvents); |
| 2492 |
} else { |
| 2493 |
/* |
| 2494 |
* Clear the selection mask |
| 2495 |
*/ |
| 2496 |
|
| 2497 |
winSock.WSAAsyncSelect(infoPtr->socket, hwnd, 0, 0); |
| 2498 |
} |
| 2499 |
break; |
| 2500 |
|
| 2501 |
case SOCKET_TERMINATE: |
| 2502 |
DestroyWindow(hwnd); |
| 2503 |
break; |
| 2504 |
} |
| 2505 |
|
| 2506 |
return 0; |
| 2507 |
} |
| 2508 |
|
| 2509 |
/* |
| 2510 |
*---------------------------------------------------------------------- |
| 2511 |
* |
| 2512 |
* Tcl_GetHostName -- |
| 2513 |
* |
| 2514 |
* Returns the name of the local host. |
| 2515 |
* |
| 2516 |
* Results: |
| 2517 |
* A string containing the network name for this machine, or |
| 2518 |
* an empty string if we can't figure out the name. The caller |
| 2519 |
* must not modify or free this string. |
| 2520 |
* |
| 2521 |
* Side effects: |
| 2522 |
* None. |
| 2523 |
* |
| 2524 |
*---------------------------------------------------------------------- |
| 2525 |
*/ |
| 2526 |
|
| 2527 |
CONST char * |
| 2528 |
Tcl_GetHostName() |
| 2529 |
{ |
| 2530 |
DWORD length; |
| 2531 |
WCHAR wbuf[MAX_COMPUTERNAME_LENGTH + 1]; |
| 2532 |
|
| 2533 |
Tcl_MutexLock(&socketMutex); |
| 2534 |
InitSockets(); |
| 2535 |
|
| 2536 |
if (hostnameInitialized) { |
| 2537 |
Tcl_MutexUnlock(&socketMutex); |
| 2538 |
return hostname; |
| 2539 |
} |
| 2540 |
Tcl_MutexUnlock(&socketMutex); |
| 2541 |
|
| 2542 |
if (TclpHasSockets(NULL) == TCL_OK) { |
| 2543 |
/* |
| 2544 |
* INTL: bug |
| 2545 |
*/ |
| 2546 |
|
| 2547 |
if (winSock.gethostname(hostname, sizeof(hostname)) == 0) { |
| 2548 |
Tcl_MutexLock(&socketMutex); |
| 2549 |
hostnameInitialized = 1; |
| 2550 |
Tcl_MutexUnlock(&socketMutex); |
| 2551 |
return hostname; |
| 2552 |
} |
| 2553 |
} |
| 2554 |
Tcl_MutexLock(&socketMutex); |
| 2555 |
length = sizeof(hostname); |
| 2556 |
if ((*tclWinProcs->getComputerNameProc)(wbuf, &length) != 0) { |
| 2557 |
/* |
| 2558 |
* Convert string from native to UTF then change to lowercase. |
| 2559 |
*/ |
| 2560 |
|
| 2561 |
Tcl_DString ds; |
| 2562 |
|
| 2563 |
lstrcpynA(hostname, Tcl_WinTCharToUtf((TCHAR *) wbuf, -1, &ds), |
| 2564 |
sizeof(hostname)); |
| 2565 |
Tcl_DStringFree(&ds); |
| 2566 |
Tcl_UtfToLower(hostname); |
| 2567 |
} else { |
| 2568 |
hostname[0] = '\0'; |
| 2569 |
} |
| 2570 |
hostnameInitialized = 1; |
| 2571 |
Tcl_MutexUnlock(&socketMutex); |
| 2572 |
return hostname; |
| 2573 |
} |
| 2574 |
|
| 2575 |
/* |
| 2576 |
*---------------------------------------------------------------------- |
| 2577 |
* |
| 2578 |
* TclWinGetSockOpt, et al. -- |
| 2579 |
* |
| 2580 |
* These functions are wrappers that let us bind the WinSock |
| 2581 |
* API dynamically so we can run on systems that don't have |
| 2582 |
* the wsock32.dll. We need wrappers for these interfaces |
| 2583 |
* because they are called from the generic Tcl code. |
| 2584 |
* |
| 2585 |
* Results: |
| 2586 |
* As defined for each function. |
| 2587 |
* |
| 2588 |
* Side effects: |
| 2589 |
* As defined for each function. |
| 2590 |
* |
| 2591 |
*---------------------------------------------------------------------- |
| 2592 |
*/ |
| 2593 |
|
| 2594 |
int |
| 2595 |
TclWinGetSockOpt(SOCKET s, int level, int optname, char * optval, |
| 2596 |
int FAR *optlen) |
| 2597 |
{ |
| 2598 |
/* |
| 2599 |
* Check that WinSock is initialized; do not call it if not, to |
| 2600 |
* prevent system crashes. This can happen at exit time if the exit |
| 2601 |
* handler for WinSock ran before other exit handlers that want to |
| 2602 |
* use sockets. |
| 2603 |
*/ |
| 2604 |
|
| 2605 |
if (!SocketsEnabled()) { |
| 2606 |
return SOCKET_ERROR; |
| 2607 |
} |
| 2608 |
|
| 2609 |
return winSock.getsockopt(s, level, optname, optval, optlen); |
| 2610 |
} |
| 2611 |
|
| 2612 |
int |
| 2613 |
TclWinSetSockOpt(SOCKET s, int level, int optname, const char * optval, |
| 2614 |
int optlen) |
| 2615 |
{ |
| 2616 |
/* |
| 2617 |
* Check that WinSock is initialized; do not call it if not, to |
| 2618 |
* prevent system crashes. This can happen at exit time if the exit |
| 2619 |
* handler for WinSock ran before other exit handlers that want to |
| 2620 |
* use sockets. |
| 2621 |
*/ |
| 2622 |
if (!SocketsEnabled()) { |
| 2623 |
return SOCKET_ERROR; |
| 2624 |
} |
| 2625 |
|
| 2626 |
return winSock.setsockopt(s, level, optname, optval, optlen); |
| 2627 |
} |
| 2628 |
|
| 2629 |
u_short |
| 2630 |
TclWinNToHS(u_short netshort) |
| 2631 |
{ |
| 2632 |
/* |
| 2633 |
* Check that WinSock is initialized; do not call it if not, to |
| 2634 |
* prevent system crashes. This can happen at exit time if the exit |
| 2635 |
* handler for WinSock ran before other exit handlers that want to |
| 2636 |
* use sockets. |
| 2637 |
*/ |
| 2638 |
|
| 2639 |
if (!SocketsEnabled()) { |
| 2640 |
return (u_short) -1; |
| 2641 |
} |
| 2642 |
|
| 2643 |
return winSock.ntohs(netshort); |
| 2644 |
} |
| 2645 |
|
| 2646 |
struct servent * |
| 2647 |
TclWinGetServByName(const char * name, const char * proto) |
| 2648 |
{ |
| 2649 |
/* |
| 2650 |
* Check that WinSock is initialized; do not call it if not, to |
| 2651 |
* prevent system crashes. This can happen at exit time if the exit |
| 2652 |
* handler for WinSock ran before other exit handlers that want to |
| 2653 |
* use sockets. |
| 2654 |
*/ |
| 2655 |
if (!SocketsEnabled()) { |
| 2656 |
return (struct servent *) NULL; |
| 2657 |
} |
| 2658 |
|
| 2659 |
return winSock.getservbyname(name, proto); |
| 2660 |
} |
| 2661 |
|
| 2662 |
|