summaryrefslogtreecommitdiffstats
path: root/win/tclWinSock.c
diff options
context:
space:
mode:
Diffstat (limited to 'win/tclWinSock.c')
-rw-r--r--win/tclWinSock.c1518
1 files changed, 651 insertions, 867 deletions
diff --git a/win/tclWinSock.c b/win/tclWinSock.c
index 15b4029..8f565b9 100644
--- a/win/tclWinSock.c
+++ b/win/tclWinSock.c
@@ -20,11 +20,11 @@
* co-thread of Tcl's thread.
*
* - The whole structure is set up by InitSockets() which is called for each
- * Tcl thread. The implementation of the co-thread is in SocketThread(), and
- * the messages are handled by SocketProc(). The connection between both is
- * not directly visible, it is done through a Win32 window class. This
- * class is initialized by InitSockets() as well, and used in the creation
- * of the message receiver windows.
+ * Tcl thread. The implementation of the co-thread is in SocketThread(),
+ * and the messages are handled by SocketProc(). The connection between
+ * both is not directly visible, it is done through a Win32 window class.
+ * This class is initialized by InitSockets() as well, and used in the
+ * creation of the message receiver windows.
*
* - An important thing to note is that *both* thread and co-thread have
* access to the list of sockets maintained in the private TSD data of the
@@ -47,13 +47,6 @@
#include "tclWinInt.h"
-/*
- * Which version of the winsock API do we want?
- */
-
-#define WSA_VERSION_MAJOR 1
-#define WSA_VERSION_MINOR 1
-
#ifdef _MSC_VER
# pragma comment (lib, "ws2_32")
#endif
@@ -81,7 +74,6 @@
*/
static int initialized = 0;
-static const TCHAR classname[] = TEXT("TclSocket");
TCL_DECLARE_MUTEX(socketMutex)
/*
@@ -97,65 +89,43 @@ static ProcessGlobalValue hostName = {
* The following defines declare the messages used on socket windows.
*/
-#define SOCKET_MESSAGE WM_USER+1
-#define SOCKET_SELECT WM_USER+2
-#define SOCKET_TERMINATE WM_USER+3
-#define SELECT TRUE
-#define UNSELECT FALSE
-
-/*
- * This is needed to comply with the strict aliasing rules of GCC, but it also
- * simplifies casting between the different sockaddr types.
- */
-
-typedef union {
- struct sockaddr sa;
- struct sockaddr_in sa4;
- struct sockaddr_in6 sa6;
- struct sockaddr_storage sas;
-} address;
-
-#ifndef IN6_ARE_ADDR_EQUAL
-#define IN6_ARE_ADDR_EQUAL IN6_ADDR_EQUAL
-#endif
-
-typedef struct SocketInfo SocketInfo;
-
-typedef struct TcpFdList {
- SocketInfo *infoPtr;
- SOCKET fd;
- struct TcpFdList *next;
-} TcpFdList;
+#define SOCKET_MESSAGE WM_USER+1
+#define SOCKET_SELECT WM_USER+2
+#define SOCKET_TERMINATE WM_USER+3
+#define SELECT TRUE
+#define UNSELECT FALSE
/*
* The following structure is used to store the data associated with each
* socket.
+ * All members modified by the notifier thread are defined as volatile.
*/
-struct SocketInfo {
+typedef struct SocketInfo {
Tcl_Channel channel; /* Channel associated with this socket. */
- struct TcpFdList *sockets; /* Windows SOCKET handle. */
- int flags; /* Bit field comprised of the flags described
+ SOCKET socket; /* Windows SOCKET handle. */
+ volatile int flags; /* Bit field comprised of the flags described
* below. */
int watchEvents; /* OR'ed combination of FD_READ, FD_WRITE,
* FD_CLOSE, FD_ACCEPT and FD_CONNECT that
* indicate which events are interesting. */
- int readyEvents; /* OR'ed combination of FD_READ, FD_WRITE,
+ volatile int readyEvents; /* OR'ed combination of FD_READ, FD_WRITE,
* FD_CLOSE, FD_ACCEPT and FD_CONNECT that
* indicate which events have occurred. */
int selectEvents; /* OR'ed combination of FD_READ, FD_WRITE,
* FD_CLOSE, FD_ACCEPT and FD_CONNECT that
* indicate which events are currently being
* selected. */
- int acceptEventCount; /* Count of the current number of FD_ACCEPTs
+ volatile int acceptEventCount;
+ /* Count of the current number of FD_ACCEPTs
* that have arrived and not yet processed. */
Tcl_TcpAcceptProc *acceptProc;
/* Proc to call on accept. */
ClientData acceptProcData; /* The data for the accept proc. */
- int lastError; /* Error code from last message. */
+ volatile int lastError; /* Error code from last message. */
struct SocketInfo *nextPtr; /* The next socket on the per-thread socket
* list. */
-};
+} SocketInfo;
/*
* The following structure is what is added to the Tcl event queue when a
@@ -199,6 +169,10 @@ typedef struct {
* socketThread has been initialized and has
* started. */
HANDLE socketListLock; /* Win32 Event to lock the socketList */
+ SocketInfo *pendingSocketInfo;
+ /* This socket is opened but not jet in the
+ * list. This value is also checked by
+ * the event structure. */
SocketInfo *socketList; /* Every open socket in this thread has an
* entry on this list. */
} ThreadSpecificData;
@@ -213,13 +187,15 @@ static WNDCLASS windowClass;
static SocketInfo * CreateSocket(Tcl_Interp *interp, int port,
const char *host, int server, const char *myaddr,
int myport, int async);
+static int CreateSocketAddress(LPSOCKADDR_IN sockaddrPtr,
+ const char *host, int port);
static void InitSockets(void);
static SocketInfo * NewSocketInfo(SOCKET socket);
static void SocketExitHandler(ClientData clientData);
static LRESULT CALLBACK SocketProc(HWND hwnd, UINT message, WPARAM wParam,
LPARAM lParam);
static int SocketsEnabled(void);
-static void TcpAccept(TcpFdList *fds, SOCKET newSocket, address addr);
+static void TcpAccept(SocketInfo *infoPtr);
static int WaitForSocketEvent(SocketInfo *infoPtr, int events,
int *errorCodePtr);
static DWORD WINAPI SocketThread(LPVOID arg);
@@ -229,10 +205,8 @@ static void TcpThreadActionProc(ClientData instanceData,
static Tcl_EventCheckProc SocketCheckProc;
static Tcl_EventProc SocketEventProc;
static Tcl_EventSetupProc SocketSetupProc;
-
static Tcl_DriverBlockModeProc TcpBlockProc;
static Tcl_DriverCloseProc TcpCloseProc;
-static Tcl_DriverClose2Proc TcpClose2Proc;
static Tcl_DriverSetOptionProc TcpSetOptionProc;
static Tcl_DriverGetOptionProc TcpGetOptionProc;
static Tcl_DriverInputProc TcpInputProc;
@@ -245,7 +219,7 @@ static Tcl_DriverGetHandleProc TcpGetHandleProc;
* based IO.
*/
-static const Tcl_ChannelType tcpChannelType = {
+static Tcl_ChannelType tcpChannelType = {
"tcp", /* Type name. */
TCL_CHANNEL_VERSION_5, /* v5 channel */
TcpCloseProc, /* Close proc. */
@@ -256,13 +230,13 @@ static const Tcl_ChannelType tcpChannelType = {
TcpGetOptionProc, /* Get option proc. */
TcpWatchProc, /* Set up notifier to watch this channel. */
TcpGetHandleProc, /* Get an OS handle from channel. */
- TcpClose2Proc, /* Close2proc. */
+ NULL, /* close2proc. */
TcpBlockProc, /* Set socket into (non-)blocking mode. */
NULL, /* flush proc. */
NULL, /* handler proc. */
NULL, /* wide seek proc */
TcpThreadActionProc, /* thread action proc */
- NULL /* truncate */
+ NULL, /* truncate */
};
/*
@@ -288,13 +262,13 @@ static const Tcl_ChannelType tcpChannelType = {
static void
InitSockets(void)
{
- DWORD id, err;
- WSADATA wsaData;
- ThreadSpecificData *tsdPtr = TclThreadDataKeyGet(&dataKey);
+ DWORD id;
+ ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
+ TclThreadDataKeyGet(&dataKey);
if (!initialized) {
initialized = 1;
- TclCreateLateExitHandler(SocketExitHandler, NULL);
+ TclCreateLateExitHandler(SocketExitHandler, (ClientData) NULL);
/*
* Create the async notification window with a new class. We must
@@ -309,89 +283,57 @@ InitSockets(void)
windowClass.hInstance = TclWinGetTclInstance();
windowClass.hbrBackground = NULL;
windowClass.lpszMenuName = NULL;
- windowClass.lpszClassName = classname;
+ windowClass.lpszClassName = "TclSocket";
windowClass.lpfnWndProc = SocketProc;
windowClass.hIcon = NULL;
windowClass.hCursor = NULL;
- if (!RegisterClass(&windowClass)) {
+ if (!RegisterClassA(&windowClass)) {
TclWinConvertError(GetLastError());
goto initFailure;
}
- /*
- * Initialize the winsock library and check the interface version
- * actually loaded. We only ask for the 1.1 interface and do require
- * that it not be less than 1.1.
- */
-
- err = WSAStartup((WORD) MAKEWORD(WSA_VERSION_MAJOR,WSA_VERSION_MINOR),
- &wsaData);
- if (err != 0) {
- TclWinConvertError(err);
- goto initFailure;
- }
-
- /*
- * Note the byte positions ae swapped for the comparison, so that
- * 0x0002 (2.0, MAKEWORD(2,0)) doesn't look less than 0x0101 (1.1). We
- * want the comparison to be 0x0200 < 0x0101.
- */
-
- if (MAKEWORD(HIBYTE(wsaData.wVersion), LOBYTE(wsaData.wVersion))
- < MAKEWORD(WSA_VERSION_MINOR, WSA_VERSION_MAJOR)) {
- TclWinConvertError(WSAVERNOTSUPPORTED);
- WSACleanup();
- goto initFailure;
- }
}
/*
* Check for per-thread initialization.
*/
- if (tsdPtr != NULL) {
- return;
- }
-
- /*
- * OK, this thread has never done anything with sockets before. Construct
- * a worker thread to handle asynchronous events related to sockets
- * assigned to _this_ thread.
- */
+ if (tsdPtr == NULL) {
+ tsdPtr = TCL_TSD_INIT(&dataKey);
+ tsdPtr->pendingSocketInfo = NULL;
+ tsdPtr->socketList = NULL;
+ tsdPtr->hwnd = NULL;
+ tsdPtr->threadId = Tcl_GetCurrentThread();
+ tsdPtr->readyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
+ if (tsdPtr->readyEvent == NULL) {
+ goto initFailure;
+ }
+ tsdPtr->socketListLock = CreateEvent(NULL, FALSE, TRUE, NULL);
+ if (tsdPtr->socketListLock == NULL) {
+ goto initFailure;
+ }
+ tsdPtr->socketThread = CreateThread(NULL, 256, SocketThread, tsdPtr,
+ 0, &id);
+ if (tsdPtr->socketThread == NULL) {
+ goto initFailure;
+ }
- tsdPtr = TCL_TSD_INIT(&dataKey);
- tsdPtr->socketList = NULL;
- tsdPtr->hwnd = NULL;
- tsdPtr->threadId = Tcl_GetCurrentThread();
- tsdPtr->readyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
- if (tsdPtr->readyEvent == NULL) {
- goto initFailure;
- }
- tsdPtr->socketListLock = CreateEvent(NULL, FALSE, TRUE, NULL);
- if (tsdPtr->socketListLock == NULL) {
- goto initFailure;
- }
- tsdPtr->socketThread = CreateThread(NULL, 256, SocketThread, tsdPtr, 0,
- &id);
- if (tsdPtr->socketThread == NULL) {
- goto initFailure;
- }
+ SetThreadPriority(tsdPtr->socketThread, THREAD_PRIORITY_HIGHEST);
- SetThreadPriority(tsdPtr->socketThread, THREAD_PRIORITY_HIGHEST);
+ /*
+ * Wait for the thread to signal when the window has been created and
+ * if it is ready to go.
+ */
- /*
- * Wait for the thread to signal when the window has been created and if
- * it is ready to go.
- */
+ WaitForSingleObject(tsdPtr->readyEvent, INFINITE);
- WaitForSingleObject(tsdPtr->readyEvent, INFINITE);
+ if (tsdPtr->hwnd == NULL) {
+ goto initFailure; /* Trouble creating the window */
+ }
- if (tsdPtr->hwnd == NULL) {
- goto initFailure; /* Trouble creating the window. */
+ Tcl_CreateEventSource(SocketSetupProc, SocketCheckProc, NULL);
}
-
- Tcl_CreateEventSource(SocketSetupProc, SocketCheckProc, NULL);
return;
initFailure:
@@ -421,7 +363,6 @@ static int
SocketsEnabled(void)
{
int enabled;
-
Tcl_MutexLock(&socketMutex);
enabled = (initialized == 1);
Tcl_MutexUnlock(&socketMutex);
@@ -452,15 +393,13 @@ SocketExitHandler(
ClientData clientData) /* Not used. */
{
Tcl_MutexLock(&socketMutex);
-
/*
* Make sure the socket event handling window is cleaned-up for, at
* most, this thread.
*/
TclpFinalizeSockets();
- UnregisterClass(classname, TclWinGetTclInstance());
- WSACleanup();
+ UnregisterClass("TclSocket", TclWinGetTclInstance());
initialized = 0;
Tcl_MutexUnlock(&socketMutex);
}
@@ -487,40 +426,34 @@ SocketExitHandler(
void
TclpFinalizeSockets(void)
{
- ThreadSpecificData *tsdPtr = TclThreadDataKeyGet(&dataKey);
-
- /*
- * Careful! This is a finalizer!
- */
-
- if (tsdPtr == NULL) {
- return;
- }
-
- if (tsdPtr->socketThread != NULL) {
- if (tsdPtr->hwnd != NULL) {
- PostMessage(tsdPtr->hwnd, SOCKET_TERMINATE, 0, 0);
-
- /*
- * Wait for the thread to exit. This ensures that we are
- * completely cleaned up before we leave this function.
- */
+ ThreadSpecificData *tsdPtr;
- WaitForSingleObject(tsdPtr->readyEvent, INFINITE);
- tsdPtr->hwnd = NULL;
+ tsdPtr = (ThreadSpecificData *) TclThreadDataKeyGet(&dataKey);
+ if (tsdPtr != NULL) {
+ if (tsdPtr->socketThread != NULL) {
+ if (tsdPtr->hwnd != NULL) {
+ if (PostMessage(tsdPtr->hwnd, SOCKET_TERMINATE, 0, 0)) {
+ /*
+ * Wait for the thread to exit. This ensures that we are
+ * completely cleaned up before we leave this function.
+ */
+ WaitForSingleObject(tsdPtr->readyEvent, INFINITE);
+ }
+ tsdPtr->hwnd = NULL;
+ }
+ CloseHandle(tsdPtr->socketThread);
+ tsdPtr->socketThread = NULL;
}
- CloseHandle(tsdPtr->socketThread);
- tsdPtr->socketThread = NULL;
- }
- if (tsdPtr->readyEvent != NULL) {
- CloseHandle(tsdPtr->readyEvent);
- tsdPtr->readyEvent = NULL;
- }
- if (tsdPtr->socketListLock != NULL) {
- CloseHandle(tsdPtr->socketListLock);
- tsdPtr->socketListLock = NULL;
+ if (tsdPtr->readyEvent != NULL) {
+ CloseHandle(tsdPtr->readyEvent);
+ tsdPtr->readyEvent = NULL;
+ }
+ if (tsdPtr->socketListLock != NULL) {
+ CloseHandle(tsdPtr->socketListLock);
+ tsdPtr->socketListLock = NULL;
+ }
+ Tcl_DeleteEventSource(SocketSetupProc, SocketCheckProc, NULL);
}
- Tcl_DeleteEventSource(SocketSetupProc, SocketCheckProc, NULL);
}
/*
@@ -558,8 +491,8 @@ TclpHasSockets(
return TCL_OK;
}
if (interp != NULL) {
- Tcl_SetObjResult(interp, Tcl_NewStringObj(
- "sockets are not available on this system", TCL_STRLEN));
+ Tcl_AppendResult(interp, "sockets are not available on this system",
+ NULL);
}
return TCL_ERROR;
}
@@ -651,9 +584,9 @@ SocketCheckProc(
if ((infoPtr->readyEvents & infoPtr->watchEvents)
&& !(infoPtr->flags & SOCKET_PENDING)) {
infoPtr->flags |= SOCKET_PENDING;
- evPtr = ckalloc(sizeof(SocketEvent));
+ evPtr = (SocketEvent *) ckalloc(sizeof(SocketEvent));
evPtr->header.proc = SocketEventProc;
- evPtr->socket = infoPtr->sockets->fd;
+ evPtr->socket = infoPtr->socket;
Tcl_QueueEvent((Tcl_Event *) evPtr, TCL_QUEUE_TAIL);
}
}
@@ -689,12 +622,9 @@ SocketEventProc(
{
SocketInfo *infoPtr;
SocketEvent *eventPtr = (SocketEvent *) evPtr;
- int mask = 0, events;
+ int mask = 0;
+ int events;
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
- TcpFdList *fds;
- SOCKET newSocket;
- address addr;
- int len;
if (!(flags & TCL_FILE_EVENTS)) {
return 0;
@@ -707,17 +637,17 @@ SocketEventProc(
WaitForSingleObject(tsdPtr->socketListLock, INFINITE);
for (infoPtr = tsdPtr->socketList; infoPtr != NULL;
infoPtr = infoPtr->nextPtr) {
- if (infoPtr->sockets->fd == eventPtr->socket) {
+ if (infoPtr->socket == eventPtr->socket) {
break;
}
}
+ SetEvent(tsdPtr->socketListLock);
/*
* Discard events that have gone stale.
*/
if (!infoPtr) {
- SetEvent(tsdPtr->socketListLock);
return 1;
}
@@ -728,72 +658,10 @@ SocketEventProc(
*/
if (infoPtr->readyEvents & FD_ACCEPT) {
- for (fds = infoPtr->sockets; fds != NULL; fds = fds->next) {
-
- /*
- * Accept the incoming connection request.
- */
- len = sizeof(address);
-
- newSocket = accept(fds->fd, &(addr.sa), &len);
-
- /*
- * On Tcl server sockets with multiple OS fds we loop over the fds
- * trying an accept() on each, so we expect INVALID_SOCKET. There
- * are also other network stack conditions that can result in
- * FD_ACCEPT but a subsequent failure on accept() by the time we
- * get around to it. Access to sockets (acceptEventCount,
- * readyEvents) in socketList is still protected by the lock
- * (prevents reintroduction of SF Tcl Bug 3056775.
- */
-
- if (newSocket == INVALID_SOCKET) {
- /* int err = WSAGetLastError(); */
- continue;
- }
-
- /*
- * It is possible that more than one FD_ACCEPT has been sent, so
- * an extra count must be kept. Decrement the count, and reset the
- * readyEvent bit if the count is no longer > 0.
- */
-
- infoPtr->acceptEventCount--;
-
- if (infoPtr->acceptEventCount <= 0) {
- infoPtr->readyEvents &= ~(FD_ACCEPT);
- }
-
- SetEvent(tsdPtr->socketListLock);
-
- /*
- * Caution: TcpAccept() has the side-effect of evaluating the
- * server accept script (via AcceptCallbackProc() in tclIOCmd.c),
- * which can close the server socket and invalidate infoPtr and
- * fds. If TcpAccept() accepts a socket we must return immediately
- * and let SocketCheckProc queue additional FD_ACCEPT events.
- */
-
- TcpAccept(fds, newSocket, addr);
- return 1;
- }
-
- /*
- * Loop terminated with no sockets accepted; clear the ready mask so
- * we can detect the next connection request. Note that connection
- * requests are level triggered, so if there is a request already
- * pending, a new event will be generated.
- */
-
- infoPtr->acceptEventCount = 0;
- infoPtr->readyEvents &= ~(FD_ACCEPT);
-
- SetEvent(tsdPtr->socketListLock);
+ TcpAccept(infoPtr);
return 1;
}
- SetEvent(tsdPtr->socketListLock);
-
/*
* Mask off unwanted events and compute the read/write mask so we can
* notify the channel.
@@ -813,48 +681,55 @@ SocketEventProc(
*/
Tcl_Time blockTime = { 0, 0 };
-
Tcl_SetMaxBlockTime(&blockTime);
mask |= TCL_READABLE|TCL_WRITABLE;
} else if (events & FD_READ) {
- fd_set readFds;
- struct timeval timeout;
-
/*
- * We must check to see if data is really available, since someone
- * could have consumed the data in the meantime. Turn off async
- * notification so select will work correctly. If the socket is still
- * readable, notify the channel driver, otherwise reset the async
- * select handler and keep waiting.
+ * Throw the readable event if an async connect failed.
*/
- SendMessage(tsdPtr->hwnd, SOCKET_SELECT,
- (WPARAM) UNSELECT, (LPARAM) infoPtr);
-
- FD_ZERO(&readFds);
- FD_SET(infoPtr->sockets->fd, &readFds);
- timeout.tv_usec = 0;
- timeout.tv_sec = 0;
+ if (infoPtr->lastError) {
- if (select(0, &readFds, NULL, NULL, &timeout) != 0) {
mask |= TCL_READABLE;
+
} else {
- infoPtr->readyEvents &= ~(FD_READ);
- SendMessage(tsdPtr->hwnd, SOCKET_SELECT,
- (WPARAM) SELECT, (LPARAM) infoPtr);
- }
- }
- if (events & (FD_WRITE | FD_CONNECT)) {
- mask |= TCL_WRITABLE;
- if (events & FD_CONNECT && infoPtr->lastError != NO_ERROR) {
+ fd_set readFds;
+ struct timeval timeout;
+
/*
- * Connect errors should also fire the readable handler.
+ * We must check to see if data is really available, since someone
+ * could have consumed the data in the meantime. Turn off async
+ * notification so select will work correctly. If the socket is still
+ * readable, notify the channel driver, otherwise reset the async
+ * select handler and keep waiting.
*/
- mask |= TCL_READABLE;
+ SendMessage(tsdPtr->hwnd, SOCKET_SELECT,
+ (WPARAM) UNSELECT, (LPARAM) infoPtr);
+
+ FD_ZERO(&readFds);
+ FD_SET(infoPtr->socket, &readFds);
+ timeout.tv_usec = 0;
+ timeout.tv_sec = 0;
+
+ if (select(0, &readFds, NULL, NULL, &timeout) != 0) {
+ mask |= TCL_READABLE;
+ } else {
+ infoPtr->readyEvents &= ~(FD_READ);
+ SendMessage(tsdPtr->hwnd, SOCKET_SELECT,
+ (WPARAM) SELECT, (LPARAM) infoPtr);
+ }
}
}
+ /*
+ * writable event
+ */
+
+ if (events & FD_WRITE) {
+ mask |= TCL_WRITABLE;
+ }
+
if (mask) {
Tcl_NotifyChannel(infoPtr->channel, mask);
}
@@ -883,7 +758,7 @@ TcpBlockProc(
int mode) /* TCL_MODE_BLOCKING or
* TCL_MODE_NONBLOCKING. */
{
- SocketInfo *infoPtr = instanceData;
+ SocketInfo *infoPtr = (SocketInfo *) instanceData;
if (mode == TCL_MODE_NONBLOCKING) {
infoPtr->flags |= SOCKET_ASYNC;
@@ -917,10 +792,10 @@ TcpCloseProc(
ClientData instanceData, /* The socket to close. */
Tcl_Interp *interp) /* Unused. */
{
- SocketInfo *infoPtr = instanceData;
+ SocketInfo *infoPtr = (SocketInfo *) instanceData;
/* TIP #218 */
int errorCode = 0;
- /* ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); */
+ ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
/*
* Check that WinSock is initialized; do not call it if not, to prevent
@@ -935,134 +810,43 @@ TcpCloseProc(
* background.
*/
- while ( infoPtr->sockets != NULL ) {
- TcpFdList *thisfd = infoPtr->sockets;
- infoPtr->sockets = thisfd->next;
-
- if (closesocket(thisfd->fd) == SOCKET_ERROR) {
- TclWinConvertError((DWORD) WSAGetLastError());
- errorCode = Tcl_GetErrno();
- }
- ckfree(thisfd);
+ if (closesocket(infoPtr->socket) == SOCKET_ERROR) {
+ TclWinConvertWSAError((DWORD) WSAGetLastError());
+ errorCode = Tcl_GetErrno();
}
}
/*
- * TIP #218. Removed the code removing the structure from the global
- * socket list. This is now done by the thread action callbacks, and only
- * there. This happens before this code is called. We can free without
- * fear of damaging the list.
+ * Clear an eventual tsd info list pointer.
+ * This may be called, if an async socket connect fails or is closed
+ * between connect and thread action callback.
*/
+ if (tsdPtr->pendingSocketInfo != NULL
+ && tsdPtr->pendingSocketInfo == infoPtr) {
- ckfree(infoPtr);
- return errorCode;
-}
-
-/*
- *----------------------------------------------------------------------
- *
- * TcpClose2Proc --
- *
- * This function is called by the generic IO level to perform the channel
- * type specific part of a half-close: namely, a shutdown() on a socket.
- *
- * Results:
- * 0 if successful, the value of errno if failed.
- *
- * Side effects:
- * Shuts down one side of the socket.
- *
- *----------------------------------------------------------------------
- */
-
-static int
-TcpClose2Proc(
- ClientData instanceData, /* The socket to close. */
- Tcl_Interp *interp, /* For error reporting. */
- int flags) /* Flags that indicate which side to close. */
-{
- SocketInfo *infoPtr = instanceData;
- int errorCode = 0, sd;
+ /* get infoPtr lock, because this concerns the notifier thread */
+ WaitForSingleObject(tsdPtr->socketListLock, INFINITE);
- /*
- * Shutdown the OS socket handle.
- */
+ tsdPtr->pendingSocketInfo = NULL;
- switch (flags) {
- case TCL_CLOSE_READ:
- sd = SD_RECEIVE;
- break;
- case TCL_CLOSE_WRITE:
- sd = SD_SEND;
- break;
- default:
- if (interp) {
- Tcl_SetObjResult(interp, Tcl_NewStringObj(
- "Socket close2proc called bidirectionally", TCL_STRLEN));
- }
- return TCL_ERROR;
+ /* Free list lock */
+ SetEvent(tsdPtr->socketListLock);
}
/*
- * Single fd operation: Tcl_OpenTcpServer() does not set TCL_READABLE or
- * TCL_WRITABLE so this should never be called for a server socket.
+ * TIP #218. Removed the code removing the structure from the global
+ * socket list. This is now done by the thread action callbacks, and only
+ * there. This happens before this code is called. We can free without
+ * fear of damaging the list.
*/
- if (shutdown(infoPtr->sockets->fd, sd) == SOCKET_ERROR) {
- TclWinConvertError((DWORD) WSAGetLastError());
- errorCode = Tcl_GetErrno();
- }
-
+ ckfree((char *) infoPtr);
return errorCode;
}
/*
*----------------------------------------------------------------------
*
- * AddSocketInfoFd --
- *
- * This function adds a SOCKET file descriptor to the 'sockets' linked
- * list of a SocketInfo structure.
- *
- * Results:
- * None.
- *
- * Side effects:
- * None, except for allocation of memory.
- *
- *----------------------------------------------------------------------
- */
-
-static void
-AddSocketInfoFd(
- SocketInfo *infoPtr,
- SOCKET socket)
-{
- TcpFdList *fds = infoPtr->sockets;
-
- if ( fds == NULL ) {
- /* Add the first FD */
- infoPtr->sockets = ckalloc(sizeof(TcpFdList));
- fds = infoPtr->sockets;
- } else {
- /* Find end of list and append FD */
- while ( fds->next != NULL ) {
- fds = fds->next;
- }
-
- fds->next = ckalloc(sizeof(TcpFdList));
- fds = fds->next;
- }
-
- /* Populate new FD */
- fds->fd = socket;
- fds->infoPtr = infoPtr;
- fds->next = NULL;
-}
-
-/*
- *----------------------------------------------------------------------
- *
* NewSocketInfo --
*
* This function allocates and initializes a new SocketInfo structure.
@@ -1080,11 +864,12 @@ static SocketInfo *
NewSocketInfo(
SOCKET socket)
{
- SocketInfo *infoPtr = ckalloc(sizeof(SocketInfo));
-
+ SocketInfo *infoPtr;
/* ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); */
+
+ infoPtr = (SocketInfo *) ckalloc((unsigned) sizeof(SocketInfo));
infoPtr->channel = 0;
- infoPtr->sockets = NULL;
+ infoPtr->socket = socket;
infoPtr->flags = 0;
infoPtr->watchEvents = 0;
infoPtr->readyEvents = 0;
@@ -1102,8 +887,6 @@ NewSocketInfo(
infoPtr->nextPtr = NULL;
- AddSocketInfoFd(infoPtr, socket);
-
return infoPtr;
}
@@ -1137,17 +920,12 @@ CreateSocket(
* asynchronously. */
{
u_long flag = 1; /* Indicates nonblocking mode. */
- int asyncConnect = 0; /* Will be 1 if async connect is in
- * progress. */
- unsigned short chosenport = 0;
- struct addrinfo *addrlist = NULL, *addrPtr;
- /* Socket address to connect to. */
- struct addrinfo *myaddrlist = NULL, *myaddrPtr;
- /* Socket address for our side. */
- const char *errorMsg = NULL;
+ SOCKADDR_IN sockaddr; /* Socket address */
+ SOCKADDR_IN mysockaddr; /* Socket address for client */
SOCKET sock = INVALID_SOCKET;
- SocketInfo *infoPtr = NULL; /* The returned value. */
- ThreadSpecificData *tsdPtr = TclThreadDataKeyGet(&dataKey);
+ SocketInfo *infoPtr=NULL; /* The returned value. */
+ ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
+ TclThreadDataKeyGet(&dataKey);
/*
* Check that WinSock is initialized; do not call it if not, to prevent
@@ -1159,245 +937,270 @@ CreateSocket(
return NULL;
}
- /*
- * Construct the addresses for each end of the socket.
- */
-
- if (!TclCreateSocketAddress(interp, &addrlist, host, port, server,
- &errorMsg)) {
+ if (!CreateSocketAddress(&sockaddr, host, port)) {
goto error;
}
- if (!TclCreateSocketAddress(interp, &myaddrlist, myaddr, myport, 1,
- &errorMsg)) {
+ if ((myaddr != NULL || myport != 0) &&
+ !CreateSocketAddress(&mysockaddr, myaddr, myport)) {
goto error;
}
- if (server) {
- for (addrPtr = addrlist; addrPtr != NULL; addrPtr = addrPtr->ai_next) {
- sock = socket(addrPtr->ai_family, SOCK_STREAM, 0);
- if (sock == INVALID_SOCKET) {
- TclWinConvertError((DWORD) WSAGetLastError());
- continue;
- }
-
- /*
- * Win-NT has a misfeature that sockets are inherited in child
- * processes by default. Turn off the inherit bit.
- */
-
- SetHandleInformation((HANDLE) sock, HANDLE_FLAG_INHERIT, 0);
+ sock = socket(AF_INET, SOCK_STREAM, 0);
+ if (sock == INVALID_SOCKET) {
+ goto error;
+ }
- /*
- * Set kernel space buffering
- */
+ /*
+ * Win-NT has a misfeature that sockets are inherited in child processes
+ * by default. Turn off the inherit bit.
+ */
- TclSockMinimumBuffers((void *)sock, TCP_BUFFER_SIZE);
+ SetHandleInformation((HANDLE) sock, HANDLE_FLAG_INHERIT, 0);
- /*
- * Make sure we use the same port when opening two server sockets
- * for IPv4 and IPv6.
- *
- * As sockaddr_in6 uses the same offset and size for the port
- * member as sockaddr_in, we can handle both through the IPv4 API.
- */
+ /*
+ * Set kernel space buffering
+ */
- if (port == 0 && chosenport != 0) {
- ((struct sockaddr_in *) addrPtr->ai_addr)->sin_port =
- htons(chosenport);
- }
+ TclSockMinimumBuffers((void *)sock, TCP_BUFFER_SIZE);
- /*
- * Bind to the specified port. Note that we must not call
- * setsockopt with SO_REUSEADDR because Microsoft allows addresses
- * to be reused even if they are still in use.
- *
- * Bind should not be affected by the socket having already been
- * set into nonblocking mode. If there is trouble, this is one
- * place to look for bugs.
- */
+ if (server) {
+ /*
+ * Bind to the specified port. Note that we must not call setsockopt
+ * with SO_REUSEADDR because Microsoft allows addresses to be reused
+ * even if they are still in use.
+ *
+ * Bind should not be affected by the socket having already been set
+ * into nonblocking mode. If there is trouble, this is one place to
+ * look for bugs.
+ */
- if (bind(sock, addrPtr->ai_addr, addrPtr->ai_addrlen)
- == SOCKET_ERROR) {
- TclWinConvertError((DWORD) WSAGetLastError());
- closesocket(sock);
- continue;
- }
- if (port == 0 && chosenport == 0) {
- address sockname;
- socklen_t namelen = sizeof(sockname);
+ if (bind(sock, (SOCKADDR *) &sockaddr, sizeof(SOCKADDR_IN))
+ == SOCKET_ERROR) {
+ goto error;
+ }
- /*
- * Synchronize port numbers when binding to port 0 of multiple
- * addresses.
- */
+ /*
+ * Set the maximum number of pending connect requests to the max value
+ * allowed on each platform (Win32 and Win32s may be different, and
+ * there may be differences between TCP/IP stacks).
+ */
- if (getsockname(sock, &sockname.sa, &namelen) >= 0) {
- chosenport = ntohs(sockname.sa4.sin_port);
- }
- }
+ if (listen(sock, SOMAXCONN) == SOCKET_ERROR) {
+ goto error;
+ }
- /*
- * Set the maximum number of pending connect requests to the max
- * value allowed on each platform (Win32 and Win32s may be
- * different, and there may be differences between TCP/IP stacks).
- */
+ /*
+ * Add this socket to the global list of sockets.
+ */
- if (listen(sock, SOMAXCONN) == SOCKET_ERROR) {
- TclWinConvertError((DWORD) WSAGetLastError());
- closesocket(sock);
- continue;
- }
+ infoPtr = NewSocketInfo(sock);
- if (infoPtr == NULL) {
- /*
- * Add this socket to the global list of sockets.
- */
+ /*
+ * Set up the select mask for connection request events.
+ */
- infoPtr = NewSocketInfo(sock);
+ infoPtr->selectEvents = FD_ACCEPT;
+ infoPtr->watchEvents |= FD_ACCEPT;
- /*
- * Set up the select mask for connection request events.
- */
+ /*
+ * Register for interest in events in the select mask. Note that this
+ * automatically places the socket into non-blocking mode.
+ */
+
+ ioctlsocket(sock, (long) FIONBIO, &flag);
+ SendMessage(tsdPtr->hwnd, SOCKET_SELECT, (WPARAM) SELECT,
+ (LPARAM) infoPtr);
- infoPtr->selectEvents = FD_ACCEPT;
- infoPtr->watchEvents |= FD_ACCEPT;
+ } else {
+ /*
+ * Try to bind to a local port, if specified.
+ */
- } else {
- AddSocketInfoFd( infoPtr, sock );
+ if (myaddr != NULL || myport != 0) {
+ if (bind(sock, (SOCKADDR *) &mysockaddr, sizeof(SOCKADDR_IN))
+ == SOCKET_ERROR) {
+ goto error;
}
}
- } else {
- for (addrPtr = addrlist; addrPtr != NULL;
- addrPtr = addrPtr->ai_next) {
- for (myaddrPtr = myaddrlist; myaddrPtr != NULL;
- myaddrPtr = myaddrPtr->ai_next) {
- /*
- * No need to try combinations of local and remote addresses
- * of different families.
- */
- if (myaddrPtr->ai_family != addrPtr->ai_family) {
- continue;
- }
+ /*
+ * Allocate socket info structure
+ */
- sock = socket(myaddrPtr->ai_family, SOCK_STREAM, 0);
- if (sock == INVALID_SOCKET) {
- TclWinConvertError((DWORD) WSAGetLastError());
- continue;
- }
+ infoPtr = NewSocketInfo(sock);
- /*
- * Win-NT has a misfeature that sockets are inherited in child
- * processes by default. Turn off the inherit bit.
- */
+ /*
+ * Set the socket into nonblocking mode if the connect should be done
+ * in the background. Activate connect notification.
+ */
- SetHandleInformation((HANDLE) sock, HANDLE_FLAG_INHERIT, 0);
+ if (async) {
- /*
- * Set kernel space buffering
- */
+ /* get infoPtr lock */
+ WaitForSingleObject(tsdPtr->socketListLock, INFINITE);
- TclSockMinimumBuffers((void *) sock, TCP_BUFFER_SIZE);
+ /*
+ * Buffer new infoPtr in the tsd memory as long as it is not in
+ * the info list. This allows the event procedure to process the
+ * event.
+ * Bugfig for 336441ed59 to not ignore notifications until the
+ * infoPtr is in the list..
+ */
- /*
- * Try to bind to a local port.
- */
+ tsdPtr->pendingSocketInfo = infoPtr;
- if (bind(sock, myaddrPtr->ai_addr, myaddrPtr->ai_addrlen)
- == SOCKET_ERROR) {
- TclWinConvertError((DWORD) WSAGetLastError());
- goto looperror;
- }
+ /*
+ * Set connect mask to connect events
+ * This is activated by a SOCKET_SELECT message to the notifier
+ * thread.
+ */
- /*
- * Set the socket into nonblocking mode if the connect should
- * be done in the background.
- */
+ infoPtr->selectEvents |= FD_CONNECT | FD_READ | FD_WRITE | FD_CLOSE;
+ infoPtr->flags |= SOCKET_ASYNC_CONNECT;
+
+ /*
+ * Free list lock
+ */
+ SetEvent(tsdPtr->socketListLock);
- if (async && ioctlsocket(sock, (long) FIONBIO, &flag)
- == SOCKET_ERROR) {
- TclWinConvertError((DWORD) WSAGetLastError());
- goto looperror;
- }
+ /*
+ * Activate accept notification and put in async mode
+ * Bug 336441ed59: activate notification before connect
+ * so we do not miss a notification of a fialed connect.
+ */
+ ioctlsocket(sock, (long) FIONBIO, &flag);
+ SendMessage(tsdPtr->hwnd, SOCKET_SELECT, (WPARAM) SELECT,
+ (LPARAM) infoPtr);
- /*
- * Attempt to connect to the remote socket.
- */
+ }
- if (connect(sock, addrPtr->ai_addr, addrPtr->ai_addrlen)
- == SOCKET_ERROR) {
- TclWinConvertError((DWORD) WSAGetLastError());
- if (Tcl_GetErrno() != EAGAIN) {
- goto looperror;
- }
+ /*
+ * Attempt to connect to the remote socket.
+ */
- /*
- * The connection is progressing in the background.
- */
+ if (connect(sock, (SOCKADDR *) &sockaddr,
+ sizeof(SOCKADDR_IN)) == SOCKET_ERROR) {
+ TclWinConvertWSAError((DWORD) WSAGetLastError());
+ if (Tcl_GetErrno() != EWOULDBLOCK) {
+ goto error;
+ }
- asyncConnect = 1;
- }
- goto connected;
+ /*
+ * The connection is progressing in the background.
+ */
- looperror:
- if (sock != INVALID_SOCKET) {
- closesocket(sock);
- sock = INVALID_SOCKET;
- }
- }
- }
- goto error;
+ } else {
- connected:
- /*
- * Add this socket to the global list of sockets.
- */
+ /*
+ * Set up the select mask for read/write events. If the connect
+ * attempt has not completed, include connect events.
+ */
- infoPtr = NewSocketInfo(sock);
+ infoPtr->selectEvents = FD_READ | FD_WRITE | FD_CLOSE;
- /*
- * Set up the select mask for read/write events. If the connect
- * attempt has not completed, include connect events.
- */
+ /*
+ * Register for interest in events in the select mask. Note that this
+ * automatically places the socket into non-blocking mode.
+ */
- infoPtr->selectEvents = FD_READ | FD_WRITE | FD_CLOSE;
- if (asyncConnect) {
- infoPtr->flags |= SOCKET_ASYNC_CONNECT;
- infoPtr->selectEvents |= FD_CONNECT;
+ ioctlsocket(sock, (long) FIONBIO, &flag);
+ SendMessage(tsdPtr->hwnd, SOCKET_SELECT, (WPARAM) SELECT,
+ (LPARAM) infoPtr);
}
}
+ return infoPtr;
+
error:
- if (addrlist == NULL) {
- freeaddrinfo(addrlist);
+ TclWinConvertWSAError((DWORD) WSAGetLastError());
+ if (interp != NULL) {
+ Tcl_AppendResult(interp, "couldn't open socket: ",
+ Tcl_PosixError(interp), NULL);
}
- if (myaddrlist == NULL) {
- freeaddrinfo(myaddrlist);
+ if (infoPtr != NULL) {
+ /*
+ * Free the allocated socket info structure and close the socket
+ */
+ TcpCloseProc(infoPtr, interp);
+ } else if (sock != INVALID_SOCKET) {
+ /*
+ * No socket structure jet - just close
+ */
+ closesocket(sock);
}
+ return NULL;
+}
+
+/*
+ *----------------------------------------------------------------------
+ *
+ * CreateSocketAddress --
+ *
+ * This function initializes a sockaddr structure for a host and port.
+ *
+ * Results:
+ * 1 if the host was valid, 0 if the host could not be converted to an IP
+ * address.
+ *
+ * Side effects:
+ * Fills in the *sockaddrPtr structure.
+ *
+ *----------------------------------------------------------------------
+ */
+
+static int
+CreateSocketAddress(
+ LPSOCKADDR_IN sockaddrPtr, /* Socket address */
+ const char *host, /* Host. NULL implies INADDR_ANY */
+ int port) /* Port number */
+{
+ struct hostent *hostent; /* Host database entry */
+ struct in_addr addr; /* For 64/32 bit madness */
/*
- * Register for interest in events in the select mask. Note that this
- * automatically places the socket into non-blocking mode.
+ * Check that WinSock is initialized; do not call it if not, to prevent
+ * system crashes. This can happen at exit time if the exit handler for
+ * WinSock ran before other exit handlers that want to use sockets.
*/
- if (infoPtr != NULL) {
- ioctlsocket(sock, (long) FIONBIO, &flag);
- SendMessage(tsdPtr->hwnd, SOCKET_SELECT, (WPARAM) SELECT,
- (LPARAM) infoPtr);
-
- return infoPtr;
+ if (!SocketsEnabled()) {
+ Tcl_SetErrno(EFAULT);
+ return 0;
}
- if (interp != NULL) {
- Tcl_SetObjResult(interp, Tcl_ObjPrintf(
- "couldn't open socket: %s",
- (errorMsg ? errorMsg : Tcl_PosixError(interp))));
+ ZeroMemory(sockaddrPtr, sizeof(SOCKADDR_IN));
+ sockaddrPtr->sin_family = AF_INET;
+ sockaddrPtr->sin_port = htons((unsigned short) (port & 0xFFFF));
+ if (host == NULL) {
+ addr.s_addr = INADDR_ANY;
+ } else {
+ addr.s_addr = inet_addr(host);
+ if (addr.s_addr == INADDR_NONE) {
+ hostent = gethostbyname(host);
+ if (hostent != NULL) {
+ memcpy(&addr, hostent->h_addr, (size_t) hostent->h_length);
+ } else {
+#ifdef EHOSTUNREACH
+ Tcl_SetErrno(EHOSTUNREACH);
+#else
+#ifdef ENXIO
+ Tcl_SetErrno(ENXIO);
+#endif
+#endif
+ return 0; /* Error. */
+ }
+ }
}
- if (sock != INVALID_SOCKET) {
- closesocket(sock);
- }
- return NULL;
+ /*
+ * NOTE: On 64 bit machines the assignment below is rumored to not do the
+ * right thing. Please report errors related to this if you observe
+ * incorrect behavior on 64 bit machines such as DEC Alphas. Should we
+ * modify this code to do an explicit memcpy?
+ */
+
+ sockaddrPtr->sin_addr.s_addr = addr.s_addr;
+ return 1; /* Success. */
}
/*
@@ -1425,7 +1228,8 @@ WaitForSocketEvent(
{
int result = 1;
int oldMode;
- ThreadSpecificData *tsdPtr = TclThreadDataKeyGet(&dataKey);
+ ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
+ TclThreadDataKeyGet(&dataKey);
/*
* Be sure to disable event servicing so we are truly modal.
@@ -1435,12 +1239,17 @@ WaitForSocketEvent(
/*
* Reset WSAAsyncSelect so we have a fresh set of events pending.
+ * Don't do that if we are waiting for a connect as we may miss
+ * a connect (bug 336441ed59).
*/
- SendMessage(tsdPtr->hwnd, SOCKET_SELECT, (WPARAM) UNSELECT,
- (LPARAM) infoPtr);
- SendMessage(tsdPtr->hwnd, SOCKET_SELECT, (WPARAM) SELECT,
- (LPARAM) infoPtr);
+ if ( 0 == (events & FD_CONNECT) ) {
+ SendMessage(tsdPtr->hwnd, SOCKET_SELECT, (WPARAM) UNSELECT,
+ (LPARAM) infoPtr);
+
+ SendMessage(tsdPtr->hwnd, SOCKET_SELECT, (WPARAM) SELECT,
+ (LPARAM) infoPtr);
+ }
while (1) {
if (infoPtr->lastError) {
@@ -1450,7 +1259,7 @@ WaitForSocketEvent(
} else if (infoPtr->readyEvents & events) {
break;
} else if (infoPtr->flags & SOCKET_ASYNC) {
- *errorCodePtr = EAGAIN;
+ *errorCodePtr = EWOULDBLOCK;
result = 0;
break;
}
@@ -1509,18 +1318,19 @@ Tcl_OpenTcpClient(
return NULL;
}
- sprintf(channelName, "sock%" TCL_I_MODIFIER "u", (size_t) infoPtr->sockets->fd);
+ sprintf(channelName, "sock%" TCL_I_MODIFIER "u", (size_t)infoPtr->socket);
infoPtr->channel = Tcl_CreateChannel(&tcpChannelType, channelName,
- infoPtr, (TCL_READABLE | TCL_WRITABLE));
- if (TCL_ERROR == Tcl_SetChannelOption(NULL, infoPtr->channel,
- "-translation", "auto crlf")) {
- Tcl_Close(NULL, infoPtr->channel);
- return NULL;
- } else if (TCL_ERROR == Tcl_SetChannelOption(NULL, infoPtr->channel,
- "-eofchar", "")) {
- Tcl_Close(NULL, infoPtr->channel);
- return NULL;
+ (ClientData) infoPtr, (TCL_READABLE | TCL_WRITABLE));
+ if (Tcl_SetChannelOption(interp, infoPtr->channel, "-translation",
+ "auto crlf") == TCL_ERROR) {
+ Tcl_Close((Tcl_Interp *) NULL, infoPtr->channel);
+ return (Tcl_Channel) NULL;
+ }
+ if (Tcl_SetChannelOption(NULL, infoPtr->channel, "-eofchar", "")
+ == TCL_ERROR) {
+ Tcl_Close((Tcl_Interp *) NULL, infoPtr->channel);
+ return (Tcl_Channel) NULL;
}
return infoPtr->channel;
}
@@ -1555,7 +1365,7 @@ Tcl_MakeTcpClientChannel(
return NULL;
}
- tsdPtr = TclThreadDataKeyGet(&dataKey);
+ tsdPtr = (ThreadSpecificData *)TclThreadDataKeyGet(&dataKey);
/*
* Set kernel space buffering and non-blocking.
@@ -1570,11 +1380,12 @@ Tcl_MakeTcpClientChannel(
*/
infoPtr->selectEvents = FD_READ | FD_CLOSE | FD_WRITE;
- SendMessage(tsdPtr->hwnd, SOCKET_SELECT, (WPARAM)SELECT, (LPARAM)infoPtr);
+ SendMessage(tsdPtr->hwnd, SOCKET_SELECT,
+ (WPARAM) SELECT, (LPARAM) infoPtr);
- sprintf(channelName, "sock%" TCL_I_MODIFIER "u", (size_t) infoPtr->sockets->fd);
+ sprintf(channelName, "sock%" TCL_I_MODIFIER "u", (size_t)infoPtr->socket);
infoPtr->channel = Tcl_CreateChannel(&tcpChannelType, channelName,
- infoPtr, (TCL_READABLE | TCL_WRITABLE));
+ (ClientData) infoPtr, (TCL_READABLE | TCL_WRITABLE));
Tcl_SetChannelOption(NULL, infoPtr->channel, "-translation", "auto crlf");
return infoPtr->channel;
}
@@ -1625,14 +1436,14 @@ Tcl_OpenTcpServer(
infoPtr->acceptProc = acceptProc;
infoPtr->acceptProcData = acceptProcData;
- sprintf(channelName, "sock%" TCL_I_MODIFIER "u", (size_t) infoPtr->sockets->fd);
+ sprintf(channelName, "sock%" TCL_I_MODIFIER "u", (size_t)infoPtr->socket);
infoPtr->channel = Tcl_CreateChannel(&tcpChannelType, channelName,
- infoPtr, 0);
+ (ClientData) infoPtr, 0);
if (Tcl_SetChannelOption(interp, infoPtr->channel, "-eofchar", "")
== TCL_ERROR) {
- Tcl_Close(NULL, infoPtr->channel);
- return NULL;
+ Tcl_Close((Tcl_Interp *) NULL, infoPtr->channel);
+ return (Tcl_Channel) NULL;
}
return infoPtr->channel;
@@ -1643,9 +1454,8 @@ Tcl_OpenTcpServer(
*
* TcpAccept --
*
- * Creates a channel for a newly accepted socket connection. This is
- * called by SocketEventProc and it in turns calls the registered
- * accept function.
+ * Accept a TCP socket connection. This is called by SocketEventProc and
+ * it in turns calls the registered accept function.
*
* Results:
* None.
@@ -1658,16 +1468,58 @@ Tcl_OpenTcpServer(
static void
TcpAccept(
- TcpFdList *fds, /* Server socket that accepted newSocket. */
- SOCKET newSocket, /* Newly accepted socket. */
- address addr) /* Address of new socket. */
+ SocketInfo *infoPtr) /* Socket to accept. */
{
+ SOCKET newSocket;
SocketInfo *newInfoPtr;
- SocketInfo *infoPtr = fds->infoPtr;
- int len = sizeof(addr);
+ SOCKADDR_IN addr;
+ int len;
char channelName[16 + TCL_INTEGER_SPACE];
- char host[NI_MAXHOST], port[NI_MAXSERV];
- ThreadSpecificData *tsdPtr = TclThreadDataKeyGet(&dataKey);
+ ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
+ TclThreadDataKeyGet(&dataKey);
+
+ /*
+ * Accept the incoming connection request.
+ */
+
+ len = sizeof(SOCKADDR_IN);
+
+ newSocket = accept(infoPtr->socket, (SOCKADDR *)&addr,
+ &len);
+
+ /*
+ * Protect access to sockets (acceptEventCount, readyEvents) in socketList
+ * by the lock. Fix for SF Tcl Bug 3056775.
+ */
+ WaitForSingleObject(tsdPtr->socketListLock, INFINITE);
+
+ /*
+ * Clear the ready mask so we can detect the next connection request. Note
+ * that connection requests are level triggered, so if there is a request
+ * already pending, a new event will be generated.
+ */
+
+ if (newSocket == INVALID_SOCKET) {
+ infoPtr->acceptEventCount = 0;
+ infoPtr->readyEvents &= ~(FD_ACCEPT);
+
+ SetEvent(tsdPtr->socketListLock);
+ return;
+ }
+
+ /*
+ * It is possible that more than one FD_ACCEPT has been sent, so an extra
+ * count must be kept. Decrement the count, and reset the readyEvent bit
+ * if the count is no longer > 0.
+ */
+
+ infoPtr->acceptEventCount--;
+
+ if (infoPtr->acceptEventCount <= 0) {
+ infoPtr->readyEvents &= ~(FD_ACCEPT);
+ }
+
+ SetEvent(tsdPtr->socketListLock);
/*
* Win-NT has a misfeature that sockets are inherited in child processes
@@ -1677,7 +1529,7 @@ TcpAccept(
SetHandleInformation((HANDLE) newSocket, HANDLE_FLAG_INHERIT, 0);
/*
- * Add this socket to the global list of sockets.
+ * Allocate socket info structure
*/
newInfoPtr = NewSocketInfo(newSocket);
@@ -1687,20 +1539,20 @@ TcpAccept(
*/
newInfoPtr->selectEvents = (FD_READ | FD_WRITE | FD_CLOSE);
- SendMessage(tsdPtr->hwnd, SOCKET_SELECT, (WPARAM) SELECT,
- (LPARAM) newInfoPtr);
+ SendMessage(tsdPtr->hwnd, SOCKET_SELECT,
+ (WPARAM) SELECT, (LPARAM) newInfoPtr);
- sprintf(channelName, "sock%" TCL_I_MODIFIER "u", (size_t) newInfoPtr->sockets->fd);
+ sprintf(channelName, "sock%" TCL_I_MODIFIER "u", (size_t)newInfoPtr->socket);
newInfoPtr->channel = Tcl_CreateChannel(&tcpChannelType, channelName,
- newInfoPtr, (TCL_READABLE | TCL_WRITABLE));
+ (ClientData) newInfoPtr, (TCL_READABLE | TCL_WRITABLE));
if (Tcl_SetChannelOption(NULL, newInfoPtr->channel, "-translation",
"auto crlf") == TCL_ERROR) {
- Tcl_Close(NULL, newInfoPtr->channel);
+ Tcl_Close((Tcl_Interp *) NULL, newInfoPtr->channel);
return;
}
if (Tcl_SetChannelOption(NULL, newInfoPtr->channel, "-eofchar", "")
== TCL_ERROR) {
- Tcl_Close(NULL, newInfoPtr->channel);
+ Tcl_Close((Tcl_Interp *) NULL, newInfoPtr->channel);
return;
}
@@ -1709,10 +1561,8 @@ TcpAccept(
*/
if (infoPtr->acceptProc != NULL) {
- getnameinfo(&(addr.sa), len, host, sizeof(host), port, sizeof(port),
- NI_NUMERICHOST|NI_NUMERICSERV);
- infoPtr->acceptProc(infoPtr->acceptProcData, newInfoPtr->channel,
- host, atoi(port));
+ (infoPtr->acceptProc) (infoPtr->acceptProcData, newInfoPtr->channel,
+ inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
}
}
@@ -1733,17 +1583,18 @@ TcpAccept(
*----------------------------------------------------------------------
*/
-static ssize_t
+static int
TcpInputProc(
ClientData instanceData, /* The socket state. */
char *buf, /* Where to store data. */
- size_t toRead, /* Maximum number of bytes to read. */
+ int toRead, /* Maximum number of bytes to read. */
int *errorCodePtr) /* Where to store error codes. */
{
- SocketInfo *infoPtr = instanceData;
- ssize_t bytesRead;
+ SocketInfo *infoPtr = (SocketInfo *) instanceData;
+ int bytesRead;
DWORD error;
- ThreadSpecificData *tsdPtr = TclThreadDataKeyGet(&dataKey);
+ ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
+ TclThreadDataKeyGet(&dataKey);
*errorCodePtr = 0;
@@ -1787,13 +1638,7 @@ TcpInputProc(
while (1) {
SendMessage(tsdPtr->hwnd, SOCKET_SELECT,
(WPARAM) UNSELECT, (LPARAM) infoPtr);
-
- /*
- * Single fd operation: this proc is only called for a connected
- * socket.
- */
-
- bytesRead = recv(infoPtr->sockets->fd, buf, toRead, 0);
+ bytesRead = recv(infoPtr->socket, buf, toRead, 0);
infoPtr->readyEvents &= ~(FD_READ);
/*
@@ -1836,7 +1681,7 @@ TcpInputProc(
*/
if ((infoPtr->flags & SOCKET_ASYNC) || (error != WSAEWOULDBLOCK)) {
- TclWinConvertError(error);
+ TclWinConvertWSAError(error);
*errorCodePtr = Tcl_GetErrno();
bytesRead = -1;
break;
@@ -1853,7 +1698,8 @@ TcpInputProc(
}
}
- SendMessage(tsdPtr->hwnd, SOCKET_SELECT, (WPARAM)SELECT, (LPARAM)infoPtr);
+ SendMessage(tsdPtr->hwnd, SOCKET_SELECT,
+ (WPARAM) SELECT, (LPARAM) infoPtr);
return bytesRead;
}
@@ -1875,17 +1721,18 @@ TcpInputProc(
*----------------------------------------------------------------------
*/
-static ssize_t
+static int
TcpOutputProc(
ClientData instanceData, /* The socket state. */
const char *buf, /* Where to get data. */
- size_t toWrite, /* Maximum number of bytes to write. */
+ int toWrite, /* Maximum number of bytes to write. */
int *errorCodePtr) /* Where to store error codes. */
{
- SocketInfo *infoPtr = instanceData;
- ssize_t bytesWritten;
+ SocketInfo *infoPtr = (SocketInfo *) instanceData;
+ int bytesWritten;
DWORD error;
- ThreadSpecificData *tsdPtr = TclThreadDataKeyGet(&dataKey);
+ ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
+ TclThreadDataKeyGet(&dataKey);
*errorCodePtr = 0;
@@ -1913,12 +1760,7 @@ TcpOutputProc(
SendMessage(tsdPtr->hwnd, SOCKET_SELECT,
(WPARAM) UNSELECT, (LPARAM) infoPtr);
- /*
- * Single fd operation: this proc is only called for a connected
- * socket.
- */
-
- bytesWritten = send(infoPtr->sockets->fd, buf, toWrite, 0);
+ bytesWritten = send(infoPtr->socket, buf, toWrite, 0);
if (bytesWritten != SOCKET_ERROR) {
/*
* Since Windows won't generate a new write event until we hit an
@@ -1944,12 +1786,12 @@ TcpOutputProc(
if (error == WSAEWOULDBLOCK) {
infoPtr->readyEvents &= ~(FD_WRITE);
if (infoPtr->flags & SOCKET_ASYNC) {
- *errorCodePtr = EAGAIN;
+ *errorCodePtr = EWOULDBLOCK;
bytesWritten = -1;
break;
}
} else {
- TclWinConvertError(error);
+ TclWinConvertWSAError(error);
*errorCodePtr = Tcl_GetErrno();
bytesWritten = -1;
break;
@@ -1966,7 +1808,8 @@ TcpOutputProc(
}
}
- SendMessage(tsdPtr->hwnd, SOCKET_SELECT, (WPARAM)SELECT, (LPARAM)infoPtr);
+ SendMessage(tsdPtr->hwnd, SOCKET_SELECT,
+ (WPARAM) SELECT, (LPARAM) infoPtr);
return bytesWritten;
}
@@ -1995,9 +1838,9 @@ TcpSetOptionProc(
const char *value) /* New value for option. */
{
#ifdef TCL_FEATURE_KEEPALIVE_NAGLE
- SocketInfo *infoPtr = instanceData;
+ SocketInfo *infoPtr;
SOCKET sock;
-#endif /*TCL_FEATURE_KEEPALIVE_NAGLE*/
+#endif
/*
* Check that WinSock is initialized; do not call it if not, to prevent
@@ -2007,15 +1850,14 @@ TcpSetOptionProc(
if (!SocketsEnabled()) {
if (interp) {
- Tcl_SetObjResult(interp, Tcl_NewStringObj(
- "winsock is not initialized", TCL_STRLEN));
+ Tcl_AppendResult(interp, "winsock is not initialized", NULL);
}
return TCL_ERROR;
}
#ifdef TCL_FEATURE_KEEPALIVE_NAGLE
- #error "TCL_FEATURE_KEEPALIVE_NAGLE not reviewed for whether to treat infoPtr->sockets as single fd or list"
- sock = infoPtr->sockets->fd;
+ infoPtr = (SocketInfo *) instanceData;
+ sock = infoPtr->socket;
if (!strcasecmp(optionName, "-keepalive")) {
BOOL val = FALSE;
@@ -2030,11 +1872,10 @@ TcpSetOptionProc(
rtn = setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
(const char *) &val, sizeof(BOOL));
if (rtn != 0) {
- TclWinConvertError(WSAGetLastError());
+ TclWinConvertWSAError(WSAGetLastError());
if (interp) {
- Tcl_SetObjResult(interp, Tcl_ObjPrintf(
- "couldn't set socket option: %s",
- Tcl_PosixError(interp)));
+ Tcl_AppendResult(interp, "couldn't set socket option: ",
+ Tcl_PosixError(interp), NULL);
}
return TCL_ERROR;
}
@@ -2052,11 +1893,10 @@ TcpSetOptionProc(
rtn = setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
(const char *) &val, sizeof(BOOL));
if (rtn != 0) {
- TclWinConvertError(WSAGetLastError());
+ TclWinConvertWSAError(WSAGetLastError());
if (interp) {
- Tcl_SetObjResult(interp, Tcl_ObjPrintf(
- "couldn't set socket option: %s",
- Tcl_PosixError(interp)));
+ Tcl_AppendResult(interp, "couldn't set socket option: ",
+ Tcl_PosixError(interp), NULL);
}
return TCL_ERROR;
}
@@ -2099,12 +1939,14 @@ TcpGetOptionProc(
Tcl_DString *dsPtr) /* Where to store the computed value;
* initialized by caller. */
{
- SocketInfo *infoPtr = instanceData;
- char host[NI_MAXHOST], port[NI_MAXSERV];
+ SocketInfo *infoPtr;
+ SOCKADDR_IN sockname;
+ SOCKADDR_IN peername;
+ struct hostent *hostEntPtr;
SOCKET sock;
+ int size = sizeof(SOCKADDR_IN);
size_t len = 0;
- int reverseDNS = 0;
-#define SUPPRESS_RDNS_VAR "::tcl::unsupported::noReverseDNS"
+ char buf[TCL_INTEGER_SPACE];
/*
* Check that WinSock is initialized; do not call it if not, to prevent
@@ -2114,13 +1956,13 @@ TcpGetOptionProc(
if (!SocketsEnabled()) {
if (interp) {
- Tcl_SetObjResult(interp, Tcl_NewStringObj(
- "winsock is not initialized", TCL_STRLEN));
+ Tcl_AppendResult(interp, "winsock is not initialized", NULL);
}
return TCL_ERROR;
}
- sock = infoPtr->sockets->fd;
+ infoPtr = (SocketInfo *) instanceData;
+ sock = (int) infoPtr->socket;
if (optionName != NULL) {
len = strlen(optionName);
}
@@ -2132,40 +1974,40 @@ TcpGetOptionProc(
int ret;
optlen = sizeof(int);
- ret = TclWinGetSockOpt(sock, SOL_SOCKET, SO_ERROR,
+ ret = TclWinGetSockOpt((int)sock, SOL_SOCKET, SO_ERROR,
(char *)&err, &optlen);
if (ret == SOCKET_ERROR) {
err = WSAGetLastError();
}
if (err) {
- TclWinConvertError(err);
- Tcl_DStringAppend(dsPtr, Tcl_ErrnoMsg(Tcl_GetErrno()),TCL_STRLEN);
+ TclWinConvertWSAError(err);
+ Tcl_DStringAppend(dsPtr, Tcl_ErrnoMsg(Tcl_GetErrno()), -1);
}
return TCL_OK;
}
- if (interp != NULL && Tcl_GetVar(interp, SUPPRESS_RDNS_VAR, 0) != NULL) {
- reverseDNS = NI_NUMERICHOST;
- }
-
if ((len == 0) || ((len > 1) && (optionName[1] == 'p') &&
(strncmp(optionName, "-peername", len) == 0))) {
- address peername;
- socklen_t size = sizeof(peername);
-
- if (getpeername(sock, (LPSOCKADDR) &(peername.sa), &size) == 0) {
+ if (getpeername(sock, (LPSOCKADDR) &peername, &size) == 0) {
if (len == 0) {
Tcl_DStringAppendElement(dsPtr, "-peername");
Tcl_DStringStartSublist(dsPtr);
}
+ Tcl_DStringAppendElement(dsPtr, inet_ntoa(peername.sin_addr));
- getnameinfo(&(peername.sa), size, host, sizeof(host),
- NULL, 0, NI_NUMERICHOST);
- Tcl_DStringAppendElement(dsPtr, host);
- getnameinfo(&(peername.sa), size, host, sizeof(host),
- port, sizeof(port), reverseDNS | NI_NUMERICSERV);
- Tcl_DStringAppendElement(dsPtr, host);
- Tcl_DStringAppendElement(dsPtr, port);
+ if (peername.sin_addr.s_addr == 0) {
+ hostEntPtr = NULL;
+ } else {
+ hostEntPtr = gethostbyaddr((char *) &(peername.sin_addr),
+ sizeof(peername.sin_addr), AF_INET);
+ }
+ if (hostEntPtr != NULL) {
+ Tcl_DStringAppendElement(dsPtr, hostEntPtr->h_name);
+ } else {
+ Tcl_DStringAppendElement(dsPtr, inet_ntoa(peername.sin_addr));
+ }
+ TclFormatInt(buf, ntohs(peername.sin_port));
+ Tcl_DStringAppendElement(dsPtr, buf);
if (len == 0) {
Tcl_DStringEndSublist(dsPtr);
} else {
@@ -2180,11 +2022,10 @@ TcpGetOptionProc(
*/
if (len) {
- TclWinConvertError((DWORD) WSAGetLastError());
+ TclWinConvertWSAError((DWORD) WSAGetLastError());
if (interp) {
- Tcl_SetObjResult(interp, Tcl_ObjPrintf(
- "can't get peername: %s",
- Tcl_PosixError(interp)));
+ Tcl_AppendResult(interp, "can't get peername: ",
+ Tcl_PosixError(interp), NULL);
}
return TCL_ERROR;
}
@@ -2193,53 +2034,25 @@ TcpGetOptionProc(
if ((len == 0) || ((len > 1) && (optionName[1] == 's') &&
(strncmp(optionName, "-sockname", len) == 0))) {
- TcpFdList *fds;
- address sockname;
- socklen_t size;
- int found = 0;
-
- if (len == 0) {
- Tcl_DStringAppendElement(dsPtr, "-sockname");
- Tcl_DStringStartSublist(dsPtr);
- }
- for (fds = infoPtr->sockets; fds != NULL; fds = fds->next) {
- sock = fds->fd;
- size = sizeof(sockname);
- if (getsockname(sock, &(sockname.sa), &size) >= 0) {
- int flags = reverseDNS;
-
- found = 1;
- getnameinfo(&sockname.sa, size, host, sizeof(host),
- NULL, 0, NI_NUMERICHOST);
- Tcl_DStringAppendElement(dsPtr, host);
-
- /*
- * We don't want to resolve INADDR_ANY and sin6addr_any; they
- * can sometimes cause problems (and never have a name).
- */
- flags |= NI_NUMERICSERV;
- if (sockname.sa.sa_family == AF_INET) {
- if (sockname.sa4.sin_addr.s_addr == INADDR_ANY) {
- flags |= NI_NUMERICHOST;
- }
- } else if (sockname.sa.sa_family == AF_INET6) {
- if ((IN6_ARE_ADDR_EQUAL(&sockname.sa6.sin6_addr,
- &in6addr_any)) ||
- (IN6_IS_ADDR_V4MAPPED(&sockname.sa6.sin6_addr)
- && sockname.sa6.sin6_addr.s6_addr[12] == 0
- && sockname.sa6.sin6_addr.s6_addr[13] == 0
- && sockname.sa6.sin6_addr.s6_addr[14] == 0
- && sockname.sa6.sin6_addr.s6_addr[15] == 0)) {
- flags |= NI_NUMERICHOST;
- }
- }
- getnameinfo(&sockname.sa, size, host, sizeof(host),
- port, sizeof(port), flags);
- Tcl_DStringAppendElement(dsPtr, host);
- Tcl_DStringAppendElement(dsPtr, port);
+ if (getsockname(sock, (LPSOCKADDR) &sockname, &size) == 0) {
+ if (len == 0) {
+ Tcl_DStringAppendElement(dsPtr, "-sockname");
+ Tcl_DStringStartSublist(dsPtr);
}
- }
- if (found) {
+ Tcl_DStringAppendElement(dsPtr, inet_ntoa(sockname.sin_addr));
+ if (sockname.sin_addr.s_addr == 0) {
+ hostEntPtr = NULL;
+ } else {
+ hostEntPtr = gethostbyaddr((char *) &(sockname.sin_addr),
+ sizeof(peername.sin_addr), AF_INET);
+ }
+ if (hostEntPtr != NULL) {
+ Tcl_DStringAppendElement(dsPtr, hostEntPtr->h_name);
+ } else {
+ Tcl_DStringAppendElement(dsPtr, inet_ntoa(sockname.sin_addr));
+ }
+ TclFormatInt(buf, ntohs(sockname.sin_port));
+ Tcl_DStringAppendElement(dsPtr, buf);
if (len == 0) {
Tcl_DStringEndSublist(dsPtr);
} else {
@@ -2247,9 +2060,9 @@ TcpGetOptionProc(
}
} else {
if (interp) {
- TclWinConvertError((DWORD) WSAGetLastError());
- Tcl_SetObjResult(interp, Tcl_ObjPrintf(
- "can't get sockname: %s", Tcl_PosixError(interp)));
+ TclWinConvertWSAError((DWORD) WSAGetLastError());
+ Tcl_AppendResult(interp, "can't get sockname: ",
+ Tcl_PosixError(interp), NULL);
}
return TCL_ERROR;
}
@@ -2283,7 +2096,8 @@ TcpGetOptionProc(
Tcl_DStringAppendElement(dsPtr, "-nagle");
}
optlen = sizeof(BOOL);
- getsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *)&opt, &optlen);
+ getsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *)&opt,
+ &optlen);
if (opt) {
Tcl_DStringAppendElement(dsPtr, "0");
} else {
@@ -2332,11 +2146,11 @@ TcpWatchProc(
* TCL_READABLE, TCL_WRITABLE and
* TCL_EXCEPTION. */
{
- SocketInfo *infoPtr = instanceData;
+ SocketInfo *infoPtr = (SocketInfo *) instanceData;
/*
* Update the watch events mask. Only if the socket is not a server
- * socket. [Bug 557878]
+ * socket. Fix for SF Tcl Bug #557878.
*/
if (!infoPtr->acceptProc) {
@@ -2355,7 +2169,6 @@ TcpWatchProc(
if (infoPtr->readyEvents & infoPtr->watchEvents) {
Tcl_Time blockTime = { 0, 0 };
-
Tcl_SetMaxBlockTime(&blockTime);
}
}
@@ -2384,9 +2197,9 @@ TcpGetHandleProc(
int direction, /* Not used. */
ClientData *handlePtr) /* Where to store the handle. */
{
- SocketInfo *statePtr = instanceData;
+ SocketInfo *statePtr = (SocketInfo *) instanceData;
- *handlePtr = INT2PTR(statePtr->sockets->fd);
+ *handlePtr = (ClientData) statePtr->socket;
return TCL_OK;
}
@@ -2411,14 +2224,14 @@ SocketThread(
LPVOID arg)
{
MSG msg;
- ThreadSpecificData *tsdPtr = arg;
+ ThreadSpecificData *tsdPtr = (ThreadSpecificData *)(arg);
/*
* Create a dummy window receiving socket events.
*/
- tsdPtr->hwnd = CreateWindow(classname, classname, WS_TILED, 0, 0, 0, 0,
- NULL, NULL, windowClass.hInstance, arg);
+ tsdPtr->hwnd = CreateWindow("TclSocket", "TclSocket",
+ WS_TILED, 0, 0, 0, 0, NULL, NULL, windowClass.hInstance, arg);
/*
* Signalize thread creator that we are done creating the window.
@@ -2482,7 +2295,7 @@ SocketProc(
int event, error;
SOCKET socket;
SocketInfo *infoPtr;
- TcpFdList *fds = NULL;
+ int info_found = 0;
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
#ifdef _WIN64
GetWindowLongPtr(hwnd, GWLP_USERDATA);
@@ -2527,80 +2340,91 @@ SocketProc(
WaitForSingleObject(tsdPtr->socketListLock, INFINITE);
for (infoPtr = tsdPtr->socketList; infoPtr != NULL;
infoPtr = infoPtr->nextPtr) {
- for (fds = infoPtr->sockets; fds != NULL; fds = fds->next) {
- if (fds->fd == socket) {
- /*
- * Update the socket state.
- *
- * A count of FD_ACCEPTS is stored, so if an FD_CLOSE
- * event happens, then clear the FD_ACCEPT count.
- * Otherwise, increment the count if the current event is
- * an FD_ACCEPT.
- */
+ if (infoPtr->socket == socket) {
+ info_found = 1;
+ break;
+ }
+ }
+ /*
+ * Check if there is a pending info structure not jet in the
+ * list
+ */
+ if ( !info_found
+ && tsdPtr->pendingSocketInfo != NULL
+ && tsdPtr->pendingSocketInfo->socket ==socket ) {
+ infoPtr = tsdPtr->pendingSocketInfo;
+ info_found = 1;
+ }
+ if (info_found) {
- if (event & FD_CLOSE) {
- infoPtr->acceptEventCount = 0;
- infoPtr->readyEvents &= ~(FD_WRITE|FD_ACCEPT);
- } else if (event & FD_ACCEPT) {
- infoPtr->acceptEventCount++;
- }
-
- if (event & FD_CONNECT) {
- /*
- * The socket is now connected, clear the async
- * connect flag.
- */
-
- infoPtr->flags &= ~(SOCKET_ASYNC_CONNECT);
-
- /*
- * Remember any error that occurred so we can report
- * connection failures.
- */
-
- if (error != ERROR_SUCCESS) {
- TclWinConvertError((DWORD) error);
- infoPtr->lastError = Tcl_GetErrno();
- }
- }
-
- if (infoPtr->flags & SOCKET_ASYNC_CONNECT) {
- infoPtr->flags &= ~(SOCKET_ASYNC_CONNECT);
- if (error != ERROR_SUCCESS) {
- TclWinConvertError((DWORD) error);
- infoPtr->lastError = Tcl_GetErrno();
- }
- infoPtr->readyEvents |= FD_WRITE;
- }
- infoPtr->readyEvents |= event;
+ /*
+ * Update the socket state.
+ *
+ * A count of FD_ACCEPTS is stored, so if an FD_CLOSE event
+ * happens, then clear the FD_ACCEPT count. Otherwise,
+ * increment the count if the current event is an FD_ACCEPT.
+ */
- /*
- * Wake up the Main Thread.
- */
+ if (event & FD_CLOSE) {
+ infoPtr->acceptEventCount = 0;
+ infoPtr->readyEvents &= ~(FD_WRITE|FD_ACCEPT);
+ } else if (event & FD_ACCEPT) {
+ infoPtr->acceptEventCount++;
+ }
+
+ if (event & FD_CONNECT) {
+ /*
+ * The socket is now connected, clear the async connect
+ * flag.
+ */
+
+ infoPtr->flags &= ~(SOCKET_ASYNC_CONNECT);
- SetEvent(tsdPtr->readyEvent);
- Tcl_ThreadAlert(tsdPtr->threadId);
- break;
+ /*
+ * Remember any error that occurred so we can report
+ * connection failures.
+ */
+
+ if (error != ERROR_SUCCESS) {
+ /* Async Connect error */
+ TclWinConvertWSAError((DWORD) error);
+ infoPtr->lastError = Tcl_GetErrno();
+ /* Fire also readable event on connect failure */
+ infoPtr->readyEvents |= FD_READ;
}
+
+ /* fire writable event on connect */
+ infoPtr->readyEvents |= FD_WRITE;
+
}
+
+ infoPtr->readyEvents |= event;
+
+ /*
+ * Wake up the Main Thread.
+ */
+
+ SetEvent(tsdPtr->readyEvent);
+ Tcl_ThreadAlert(tsdPtr->threadId);
}
SetEvent(tsdPtr->socketListLock);
break;
case SOCKET_SELECT:
infoPtr = (SocketInfo *) lParam;
- for (fds = infoPtr->sockets; fds != NULL; fds = fds->next) {
- infoPtr = (SocketInfo *) lParam;
- if (wParam == SELECT) {
- WSAAsyncSelect(fds->fd, hwnd,
- SOCKET_MESSAGE, infoPtr->selectEvents);
- } else {
- /*
- * Clear the selection mask
- */
+ if (wParam == SELECT) {
+ /*
+ * Start notification by windows messages on socket events
+ */
- WSAAsyncSelect(fds->fd, hwnd, 0, 0);
- }
+ WSAAsyncSelect(infoPtr->socket, hwnd,
+ SOCKET_MESSAGE, infoPtr->selectEvents);
+ } else {
+ /*
+ * UNSELECT: Clear the selection mask
+ */
+
+ WSAAsyncSelect(infoPtr->socket, hwnd, 0, 0);
}
break;
@@ -2655,16 +2479,17 @@ InitializeHostName(
int *lengthPtr,
Tcl_Encoding *encodingPtr)
{
- TCHAR tbuf[MAX_COMPUTERNAME_LENGTH + 1];
- DWORD length = MAX_COMPUTERNAME_LENGTH + 1;
+ WCHAR wbuf[MAX_COMPUTERNAME_LENGTH + 1];
+ DWORD length = sizeof(wbuf) / sizeof(WCHAR);
Tcl_DString ds;
- if (GetComputerName(tbuf, &length) != 0) {
+ if ((*tclWinProcs->getComputerNameProc)(wbuf, &length) != 0) {
/*
* Convert string from native to UTF then change to lowercase.
*/
- Tcl_UtfToLower(Tcl_WinTCharToUtf(tbuf, TCL_STRLEN, &ds));
+ Tcl_UtfToLower(Tcl_WinTCharToUtf((TCHAR *) wbuf, -1, &ds));
+
} else {
Tcl_DStringInit(&ds);
if (TclpHasSockets(NULL) == TCL_OK) {
@@ -2679,8 +2504,8 @@ InitializeHostName(
Tcl_DStringSetLength(&inDs, 256);
if (gethostname(Tcl_DStringValue(&inDs),
Tcl_DStringLength(&inDs)) == 0) {
- Tcl_ExternalToUtfDString(NULL, Tcl_DStringValue(&inDs),
- TCL_STRLEN, &ds);
+ Tcl_ExternalToUtfDString(NULL, Tcl_DStringValue(&inDs), -1,
+ &ds);
}
Tcl_DStringFree(&inDs);
}
@@ -2688,7 +2513,7 @@ InitializeHostName(
*encodingPtr = Tcl_GetEncoding(NULL, "utf-8");
*lengthPtr = Tcl_DStringLength(&ds);
- *valuePtr = ckalloc((*lengthPtr) + 1);
+ *valuePtr = ckalloc((unsigned int) (*lengthPtr)+1);
memcpy(*valuePtr, Tcl_DStringValue(&ds), (size_t)(*lengthPtr)+1);
Tcl_DStringFree(&ds);
}
@@ -2712,80 +2537,34 @@ InitializeHostName(
*----------------------------------------------------------------------
*/
+#undef TclWinGetSockOpt
int
-TclWinGetSockOpt(
- SOCKET s,
- int level,
- int optname,
- char *optval,
- int *optlen)
+TclWinGetSockOpt(SOCKET s, int level, int optname, char *optval,
+ int *optlen)
{
- /*
- * Check that WinSock is initialized; do not call it if not, to prevent
- * system crashes. This can happen at exit time if the exit handler for
- * WinSock ran before other exit handlers that want to use sockets.
- */
-
- if (!SocketsEnabled()) {
- return SOCKET_ERROR;
- }
-
return getsockopt(s, level, optname, optval, optlen);
}
+#undef TclWinSetSockOpt
int
-TclWinSetSockOpt(
- SOCKET s,
- int level,
- int optname,
- const char *optval,
+TclWinSetSockOpt(SOCKET s, int level, int optname, const char *optval,
int optlen)
{
- /*
- * Check that WinSock is initialized; do not call it if not, to prevent
- * system crashes. This can happen at exit time if the exit handler for
- * WinSock ran before other exit handlers that want to use sockets.
- */
-
- if (!SocketsEnabled()) {
- return SOCKET_ERROR;
- }
-
return setsockopt(s, level, optname, optval, optlen);
}
char *
-TclpInetNtoa(
- struct in_addr addr)
+TclpInetNtoa(struct in_addr addr)
{
- /*
- * Check that WinSock is initialized; do not call it if not, to prevent
- * system crashes. This can happen at exit time if the exit handler for
- * WinSock ran before other exit handlers that want to use sockets.
- */
-
- if (!SocketsEnabled()) {
- return NULL;
- }
-
return inet_ntoa(addr);
}
+#undef TclWinGetServByName
struct servent *
TclWinGetServByName(
const char *name,
const char *proto)
{
- /*
- * Check that WinSock is initialized; do not call it if not, to prevent
- * system crashes. This can happen at exit time if the exit handler for
- * WinSock ran before other exit handlers that want to use sockets.
- */
-
- if (!SocketsEnabled()) {
- return NULL;
- }
-
return getservbyname(name, proto);
}
@@ -2811,7 +2590,7 @@ TcpThreadActionProc(
int action)
{
ThreadSpecificData *tsdPtr;
- SocketInfo *infoPtr = instanceData;
+ SocketInfo *infoPtr = (SocketInfo *) instanceData;
int notifyCmd;
if (action == TCL_CHANNEL_THREAD_INSERT) {
@@ -2829,6 +2608,11 @@ TcpThreadActionProc(
WaitForSingleObject(tsdPtr->socketListLock, INFINITE);
infoPtr->nextPtr = tsdPtr->socketList;
tsdPtr->socketList = infoPtr;
+
+ if (infoPtr == tsdPtr->pendingSocketInfo) {
+ tsdPtr->pendingSocketInfo = NULL;
+ }
+
SetEvent(tsdPtr->socketListLock);
notifyCmd = SELECT;