summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoroehhar <harald.oehlmann@elmicron.de>2024-05-08 14:39:31 (GMT)
committeroehhar <harald.oehlmann@elmicron.de>2024-05-08 14:39:31 (GMT)
commit7f68f29ca2bd97c20f955962cbe4fd68519e2f8a (patch)
tree8d48d0d05edc7cae1a100de4823111511e4cb660
parent7eb7a346c38947ee1519c84274adf071762205fd (diff)
parent086176e9ceed573d12fab952a842d0533b80e5cc (diff)
downloadtcl-7f68f29ca2bd97c20f955962cbe4fd68519e2f8a.zip
tcl-7f68f29ca2bd97c20f955962cbe4fd68519e2f8a.tar.gz
tcl-7f68f29ca2bd97c20f955962cbe4fd68519e2f8a.tar.bz2
Backport [e0bdc14669] "Cleaning up tclWinSock.c"
-rw-r--r--win/tclWinSock.c312
1 files changed, 168 insertions, 144 deletions
diff --git a/win/tclWinSock.c b/win/tclWinSock.c
index 0dd7871..e427c39 100644
--- a/win/tclWinSock.c
+++ b/win/tclWinSock.c
@@ -62,10 +62,10 @@
#define SET_BITS(var, bits) ((var) |= (bits))
#define CLEAR_BITS(var, bits) ((var) &= ~(bits))
-#define GOT_BITS(var, bits) (((var) & (bits)) != 0)
+#define GOT_BITS(var, bits) (((var) & (bits)) != 0)
/* "sock" + a pointer in hex + \0 */
-#define SOCK_CHAN_LENGTH (16 + TCL_INTEGER_SPACE)
+#define SOCK_CHAN_LENGTH (16 + TCL_INTEGER_SPACE)
/*
* The following variable is used to tell whether this module has been
@@ -80,12 +80,19 @@ TCL_DECLARE_MUTEX(socketMutex)
/*
* The following defines declare the messages used on socket windows.
*/
+enum TclSocketMessages {
+ SOCKET_MESSAGE = WM_USER+1, /* Sent by OS: something happened. */
+ SOCKET_SELECT = WM_USER+2, /* Adjust select mask. */
+ SOCKET_TERMINATE = WM_USER+3/* Stop worker thread. */
+};
-#define SOCKET_MESSAGE WM_USER+1
-#define SOCKET_SELECT WM_USER+2
-#define SOCKET_TERMINATE WM_USER+3
-#define SELECT TRUE
-#define UNSELECT FALSE
+/*
+ * Operations used with a SOCKET_SELECT message.
+ */
+enum SocketSelectOperations {
+ SELECT = TRUE, /* Add socket to select. */
+ UNSELECT = FALSE /* Remove socket from select. */
+};
/*
* This is needed to comply with the strict aliasing rules of GCC, but it also
@@ -150,7 +157,7 @@ struct TcpState {
struct addrinfo *myaddrlist;/* Local address. */
struct addrinfo *myaddr; /* Iterator over myaddrlist. */
int connectError; /* Cache status of async socket. */
- int cachedBlocking; /* Cache blocking mode of async socket. */
+ int cachedBlocking; /* Cache blocking mode of async socket. */
volatile int notifierConnectError;
/* Async connect error set by notifier thread.
* This error is still a windows error code.
@@ -164,21 +171,20 @@ struct TcpState {
* structure.
*/
-#define TCP_NONBLOCKING (1<<0) /* Socket with non-blocking I/O */
-#define TCP_ASYNC_CONNECT (1<<1) /* Async connect in progress. */
-#define SOCKET_EOF (1<<2) /* A zero read happened on the
- * socket. */
-#define SOCKET_PENDING (1<<3) /* A message has been sent for this
- * socket */
-#define TCP_ASYNC_PENDING (1<<4) /* TcpConnect was called to
- * process an async connect. This
- * flag indicates that reentry is
- * still pending */
-#define TCP_ASYNC_FAILED (1<<5) /* An async connect finally failed */
-
-#define TCP_ASYNC_TEST_MODE (1<<8) /* Async testing activated. Do not
- * automatically continue connection
- * process */
+enum TcpStateFlags {
+ TCP_NONBLOCKING = (1<<0), /* Socket with non-blocking I/O. */
+ TCP_ASYNC_CONNECT = (1<<1), /* Async connect in progress. */
+ SOCKET_EOF = (1<<2), /* A zero read happened on the socket. */
+ SOCKET_PENDING = (1<<3), /* A message has been sent for this socket */
+ TCP_ASYNC_PENDING = (1<<4), /* TcpConnect was called to process an async
+ * connect. This flag indicates that reentry is
+ * still pending. */
+ TCP_ASYNC_FAILED = (1<<5), /* An async connect finally failed. */
+
+ TCP_ASYNC_TEST_MODE = (1<<8)/* Async testing activated. Do not
+ * automatically continue connection
+ * process */
+};
/*
* The following structure is what is added to the Tcl event queue when a
@@ -201,7 +207,10 @@ typedef struct {
#define TCP_BUFFER_SIZE 4096
-
+/*
+ * Per (main) thread data, holding list of things being waited upon and the
+ * various handles to things doing the waiting/notification.
+ */
typedef struct {
HWND hwnd; /* Handle to window for socket messages. */
HANDLE socketThread; /* Thread handling the window */
@@ -211,8 +220,7 @@ typedef struct {
* socketThread has been initialized and has
* started. */
HANDLE socketListLock; /* Win32 Event to lock the socketList */
- TcpState *pendingTcpState;
- /* This socket is opened but not jet in the
+ TcpState *pendingTcpState; /* This socket is opened but not jet in the
* list. This value is also checked by
* the event structure. */
TcpState *socketList; /* Every open socket in this thread has an
@@ -237,7 +245,7 @@ static void TcpAccept(TcpFdList *fds, SOCKET newSocket, address addr);
static int WaitForConnect(TcpState *statePtr, int *errorCodePtr);
static int WaitForSocketEvent(TcpState *statePtr, int events,
int *errorCodePtr);
-static void AddSocketInfoFd(TcpState *statePtr, SOCKET socket);
+static void AddSocketInfoFd(TcpState *statePtr, SOCKET socket);
static int FindFDInList(TcpState *statePtr, SOCKET socket);
static DWORD WINAPI SocketThread(LPVOID arg);
static void TcpThreadActionProc(void *instanceData,
@@ -276,7 +284,7 @@ static const Tcl_ChannelType tcpChannelType = {
TcpGetOptionProc, /* Get option proc. */
TcpWatchProc, /* Initialize notifier. */
TcpGetHandleProc, /* Get OS handles out of channel. */
- TcpClose2Proc, /* Close2 proc. */
+ TcpClose2Proc, /* New close2 proc. */
TcpBlockModeProc, /* Set blocking or non-blocking mode.*/
NULL, /* flush proc. */
NULL, /* handler proc. */
@@ -292,21 +300,32 @@ static const Tcl_ChannelType tcpChannelType = {
static TclInitProcessGlobalValueProc InitializeHostName;
static ProcessGlobalValue hostName =
{0, 0, NULL, NULL, InitializeHostName, NULL, NULL};
-
+
/*
- * Simple wrapper round the SendMessage syscall.
+ *----------------------------------------------------------------------
+ *
+ * SendSelectMessage --
+ *
+ * Simple wrapper round the SendMessage syscall with a SOCKET_SELECT
+ * message to add a bit of type safety.
+ *
+ *----------------------------------------------------------------------
*/
-
-#define SendSelectMessage(tsdPtr, message, payload) \
- SendMessageW((tsdPtr)->hwnd, SOCKET_SELECT, \
- (WPARAM) (message), (LPARAM) (payload))
-
+static inline void
+SendSelectMessage(
+ ThreadSpecificData *tsdPtr, /* Reference to this thread's worker. */
+ int operation, /* Whether to add or remove from the mask. */
+ TcpState *payload) /* What socket to add/remove. */
+{
+ SendMessageW(tsdPtr->hwnd, SOCKET_SELECT, (WPARAM) operation,
+ (LPARAM) payload);
+}
/*
* Address print debug functions
*/
#if 0
-void
+static inline void
printaddrinfo(
struct addrinfo *ai,
char *prefix)
@@ -315,10 +334,10 @@ printaddrinfo(
getnameinfo(ai->ai_addr, ai->ai_addrlen,
host, sizeof(host), port, sizeof(port),
- NI_NUMERICHOST|NI_NUMERICSERV);
+ NI_NUMERICHOST | NI_NUMERICSERV);
}
-void
+static void
printaddrinfolist(
struct addrinfo *addrlist,
char *prefix)
@@ -352,17 +371,17 @@ InitializeHostName(
Tcl_Encoding *encodingPtr)
{
WCHAR wbuf[256];
- DWORD length = sizeof(wbuf)/sizeof(WCHAR);
+ DWORD length = sizeof(wbuf) / sizeof(WCHAR);
Tcl_DString ds;
Tcl_DStringInit(&ds);
- if (GetComputerNameExW(ComputerNamePhysicalDnsFullyQualified, wbuf, &length) != 0) {
+ if (GetComputerNameExW(ComputerNamePhysicalDnsFullyQualified, wbuf,
+ &length) != 0) {
/*
* Convert string from native to UTF then change to lowercase.
*/
Tcl_UtfToLower(Tcl_WCharToUtfDString(wbuf, TCL_INDEX_NONE, &ds));
-
} else {
TclInitSockets();
/*
@@ -384,7 +403,7 @@ InitializeHostName(
*encodingPtr = Tcl_GetEncoding(NULL, "utf-8");
*lengthPtr = Tcl_DStringLength(&ds);
- *valuePtr = (char *)ckalloc(*lengthPtr + 1);
+ *valuePtr = (char *) ckalloc(*lengthPtr + 1);
memcpy(*valuePtr, Tcl_DStringValue(&ds), *lengthPtr + 1);
Tcl_DStringFree(&ds);
}
@@ -418,8 +437,8 @@ Tcl_GetHostName(void)
*
* TclInitSockets --
*
- * Initialization of sockets for the thread. Also creates message
- * handling window class for the process if needed.
+ * Initialization of sockets for the thread. Also creates message
+ * handling window class for the process if needed.
*
* Results:
* Nothing. Panics on failure.
@@ -437,7 +456,8 @@ TclInitSockets(void)
{
/* Then Per thread initialization. */
DWORD id;
- ThreadSpecificData *tsdPtr = (ThreadSpecificData *)TclThreadDataKeyGet(&dataKey);
+ ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
+ TclThreadDataKeyGet(&dataKey);
if (tsdPtr != NULL) {
return;
@@ -453,10 +473,10 @@ TclInitSockets(void)
tsdPtr = TCL_TSD_INIT(&dataKey);
tsdPtr->pendingTcpState = NULL;
- tsdPtr->socketList = NULL;
- tsdPtr->hwnd = NULL;
- tsdPtr->threadId = Tcl_GetCurrentThread();
- tsdPtr->readyEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
+ tsdPtr->socketList = NULL;
+ tsdPtr->hwnd = NULL;
+ tsdPtr->threadId = Tcl_GetCurrentThread();
+ tsdPtr->readyEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
if (tsdPtr->readyEvent == NULL) {
goto initFailure;
}
@@ -511,7 +531,8 @@ TclInitSockets(void)
void
TclpFinalizeSockets(void)
{
- ThreadSpecificData *tsdPtr = (ThreadSpecificData *)TclThreadDataKeyGet(&dataKey);
+ ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
+ TclThreadDataKeyGet(&dataKey);
/*
* Careful! This is a finalizer!
@@ -566,12 +587,12 @@ TclpFinalizeSockets(void)
static int
TcpBlockModeProc(
- void *instanceData, /* Socket state. */
+ void *instanceData, /* Socket state. */
int mode) /* The mode to set. Can be one of
* TCL_MODE_BLOCKING or
* TCL_MODE_NONBLOCKING. */
{
- TcpState *statePtr = (TcpState *)instanceData;
+ TcpState *statePtr = (TcpState *) instanceData;
if (mode == TCL_MODE_NONBLOCKING) {
SET_BITS(statePtr->flags, TCP_NONBLOCKING);
@@ -620,7 +641,6 @@ WaitForConnect(
{
int result;
int oldMode;
- ThreadSpecificData *tsdPtr;
/*
* Check if an async connect failed already and error reporting is
@@ -650,7 +670,7 @@ WaitForConnect(
if (GOT_BITS(statePtr->flags, TCP_ASYNC_TEST_MODE)
&& errorCodePtr != NULL
- && GOT_BITS(statePtr->flags, TCP_NONBLOCKING)) {
+ && GOT_BITS(statePtr->flags, TCP_NONBLOCKING)) {
*errorCodePtr = EWOULDBLOCK;
return -1;
}
@@ -670,7 +690,8 @@ WaitForConnect(
* Get the statePtr lock.
*/
- tsdPtr = (ThreadSpecificData *)TclThreadDataKeyGet(&dataKey);
+ ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
+ TclThreadDataKeyGet(&dataKey);
WaitForSingleObject(tsdPtr->socketListLock, INFINITE);
/*
@@ -743,11 +764,11 @@ WaitForConnect(
return -1;
}
- /*
- * Free list lock.
- */
+ /*
+ * Free list lock.
+ */
- SetEvent(tsdPtr->socketListLock);
+ SetEvent(tsdPtr->socketListLock);
/*
* Background operation returns with no action as there was no connect
@@ -797,7 +818,7 @@ WaitForConnect(
static int
TcpInputProc(
- void *instanceData, /* Socket state. */
+ void *instanceData, /* Socket state. */
char *buf, /* Where to store data read. */
int bufSize, /* How much space is available in the
* buffer? */
@@ -806,7 +827,8 @@ TcpInputProc(
TcpState *statePtr = (TcpState *)instanceData;
int bytesRead;
DWORD error;
- ThreadSpecificData *tsdPtr = (ThreadSpecificData *)TclThreadDataKeyGet(&dataKey);
+ ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
+ TclThreadDataKeyGet(&dataKey);
*errorCodePtr = 0;
@@ -888,7 +910,7 @@ TcpInputProc(
*/
if (GOT_BITS(statePtr->flags, TCP_NONBLOCKING)
- || (error != WSAEWOULDBLOCK)) {
+ || (error != WSAEWOULDBLOCK)) {
Tcl_WinConvertError(error);
*errorCodePtr = Tcl_GetErrno();
bytesRead = -1;
@@ -930,7 +952,7 @@ TcpInputProc(
static int
TcpOutputProc(
- void *instanceData, /* Socket state. */
+ void *instanceData, /* Socket state. */
const char *buf, /* The data buffer. */
int toWrite, /* How many bytes to write? */
int *errorCodePtr) /* Where to store error code. */
@@ -938,7 +960,8 @@ TcpOutputProc(
TcpState *statePtr = (TcpState *)instanceData;
int written;
DWORD error;
- ThreadSpecificData *tsdPtr = (ThreadSpecificData *)TclThreadDataKeyGet(&dataKey);
+ ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
+ TclThreadDataKeyGet(&dataKey);
*errorCodePtr = 0;
@@ -1034,7 +1057,7 @@ TcpOutputProc(
static int
TcpCloseProc(
- void *instanceData, /* The socket to close. */
+ void *instanceData, /* The socket to close. */
TCL_UNUSED(Tcl_Interp *))
{
TcpState *statePtr = (TcpState *)instanceData;
@@ -1060,10 +1083,10 @@ TcpCloseProc(
}
if (statePtr->addrlist != NULL) {
- freeaddrinfo(statePtr->addrlist);
+ freeaddrinfo(statePtr->addrlist);
}
if (statePtr->myaddrlist != NULL) {
- freeaddrinfo(statePtr->myaddrlist);
+ freeaddrinfo(statePtr->myaddrlist);
}
/*
@@ -1120,7 +1143,7 @@ TcpCloseProc(
static int
TcpClose2Proc(
- void *instanceData, /* The socket to close. */
+ void *instanceData, /* The socket to close. */
Tcl_Interp *interp, /* For error reporting. */
int flags) /* Flags that indicate which side to close. */
{
@@ -1132,7 +1155,7 @@ TcpClose2Proc(
* Shutdown the OS socket handle.
*/
- if ((flags & (TCL_CLOSE_READ|TCL_CLOSE_WRITE)) == 0) {
+ if ((flags & (TCL_CLOSE_READ | TCL_CLOSE_WRITE)) == 0) {
return TcpCloseProc(instanceData, interp);
}
@@ -1141,11 +1164,13 @@ TcpClose2Proc(
* TCL_WRITABLE so this should never be called for a server socket.
*/
- if ((flags & TCL_CLOSE_READ) && (shutdown(statePtr->sockets->fd, SD_RECEIVE) == SOCKET_ERROR)) {
+ if ((flags & TCL_CLOSE_READ)
+ && (shutdown(statePtr->sockets->fd, SD_RECEIVE) == SOCKET_ERROR)) {
Tcl_WinConvertError((DWORD) WSAGetLastError());
readError = Tcl_GetErrno();
}
- if ((flags & TCL_CLOSE_WRITE) && (shutdown(statePtr->sockets->fd, SD_SEND) == SOCKET_ERROR)) {
+ if ((flags & TCL_CLOSE_WRITE)
+ && (shutdown(statePtr->sockets->fd, SD_SEND) == SOCKET_ERROR)) {
Tcl_WinConvertError((DWORD) WSAGetLastError());
writeError = Tcl_GetErrno();
}
@@ -1170,7 +1195,7 @@ TcpClose2Proc(
static int
TcpSetOptionProc(
- void *instanceData, /* Socket state. */
+ void *instanceData, /* Socket state. */
Tcl_Interp *interp, /* For error reporting - can be NULL. */
const char *optionName, /* Name of the option to set. */
const char *value) /* New value for option. */
@@ -1253,7 +1278,7 @@ TcpSetOptionProc(
static int
TcpGetOptionProc(
- void *instanceData, /* Socket state. */
+ void *instanceData, /* Socket state. */
Tcl_Interp *interp, /* For error reporting - can be NULL. */
const char *optionName, /* Name of the option to retrieve the value
* for, or NULL to get all options and their
@@ -1267,6 +1292,9 @@ TcpGetOptionProc(
size_t len = 0;
int reverseDNS = 0;
#define SUPPRESS_RDNS_VAR "::tcl::unsupported::noReverseDNS"
+#define HAVE_OPTION(option) \
+ ((len > 1) && (optionName[1] == option[1]) && \
+ (strncmp(optionName, option, len) == 0))
/*
* Go one step in async connect
@@ -1284,8 +1312,7 @@ TcpGetOptionProc(
len = strlen(optionName);
}
- if ((len > 1) && (optionName[1] == 'e') &&
- (strncmp(optionName, "-error", len) == 0)) {
+ if (HAVE_OPTION("-error")) {
/*
* Do not return any errors if async connect is running.
*/
@@ -1300,7 +1327,8 @@ TcpGetOptionProc(
if (statePtr->connectError != 0) {
Tcl_DStringAppend(dsPtr,
- Tcl_ErrnoMsg(statePtr->connectError), TCL_INDEX_NONE);
+ Tcl_ErrnoMsg(statePtr->connectError),
+ TCL_INDEX_NONE);
statePtr->connectError = 0;
}
} else {
@@ -1335,19 +1363,19 @@ TcpGetOptionProc(
if (err) {
Tcl_WinConvertError(err);
- Tcl_DStringAppend(dsPtr, Tcl_ErrnoMsg(Tcl_GetErrno()), TCL_INDEX_NONE);
+ Tcl_DStringAppend(dsPtr, Tcl_ErrnoMsg(Tcl_GetErrno()),
+ TCL_INDEX_NONE);
}
}
}
return TCL_OK;
}
- if ((len > 1) && (optionName[1] == 'c') &&
- (strncmp(optionName, "-connecting", len) == 0)) {
+ if (HAVE_OPTION("-connecting")) {
Tcl_DStringAppend(dsPtr,
GOT_BITS(statePtr->flags, TCP_ASYNC_PENDING)
? "1" : "0", TCL_INDEX_NONE);
- return TCL_OK;
+ return TCL_OK;
}
if (interp != NULL
@@ -1355,8 +1383,7 @@ TcpGetOptionProc(
reverseDNS = NI_NUMERICHOST;
}
- if ((len == 0) || ((len > 1) && (optionName[1] == 'p') &&
- (strncmp(optionName, "-peername", len) == 0))) {
+ if (HAVE_OPTION("-peername")) {
address peername;
socklen_t size = sizeof(peername);
@@ -1414,8 +1441,7 @@ TcpGetOptionProc(
}
}
- if ((len == 0) || ((len > 1) && (optionName[1] == 's') &&
- (strncmp(optionName, "-sockname", len) == 0))) {
+ if ((len == 0) || HAVE_OPTION("-sockname")) {
TcpFdList *fds;
address sockname;
socklen_t size;
@@ -1487,8 +1513,7 @@ TcpGetOptionProc(
}
}
- if ((len == 0) || ((len > 1) && (optionName[1] == 'k') &&
- (strncmp(optionName, "-keepalive", len) == 0))) {
+ if ((len == 0) || HAVE_OPTION("-keepalive")) {
int optlen;
BOOL opt = FALSE;
@@ -1504,8 +1529,7 @@ TcpGetOptionProc(
}
}
- if ((len == 0) || ((len > 1) && (optionName[1] == 'n') &&
- (strncmp(optionName, "-nodelay", len) == 0))) {
+ if ((len == 0) || HAVE_OPTION("-nodelay")) {
int optlen;
BOOL opt = FALSE;
@@ -1549,7 +1573,7 @@ TcpGetOptionProc(
static void
TcpWatchProc(
- void *instanceData, /* The socket state. */
+ void *instanceData, /* The socket state. */
int mask) /* Events of interest; an OR-ed combination of
* TCL_READABLE, TCL_WRITABLE and
* TCL_EXCEPTION. */
@@ -1603,9 +1627,9 @@ TcpWatchProc(
static int
TcpGetHandleProc(
- void *instanceData, /* The socket state. */
+ void *instanceData, /* The socket state. */
TCL_UNUSED(int) /*direction*/,
- void **handlePtr) /* Where to store the handle. */
+ void **handlePtr) /* Where to store the handle. */
{
TcpState *statePtr = (TcpState *)instanceData;
@@ -1627,9 +1651,9 @@ TcpGetHandleProc(
* connect synchronously
*
* Results:
- * TCL_OK, if the socket was successfully connected or an asynchronous
- * connection is in progress. If an error occurs, TCL_ERROR is returned
- * and an error message is left in interp.
+ * TCL_OK, if the socket was successfully connected or an asynchronous
+ * connection is in progress. If an error occurs, TCL_ERROR is returned
+ * and an error message is left in interp.
*
* Side effects:
* Opens a socket.
@@ -1656,16 +1680,17 @@ TcpConnect(
{
DWORD error;
int async_connect = GOT_BITS(statePtr->flags, TCP_ASYNC_CONNECT);
- /* We are started with async connect and the
- * connect notification was not yet
- * received. */
+ /* We are started with async connect and the
+ * connect notification was not yet
+ * received. */
int async_callback = GOT_BITS(statePtr->flags, TCP_ASYNC_PENDING);
- /* We were called by the event procedure and
- * continue our loop. */
- ThreadSpecificData *tsdPtr = (ThreadSpecificData *)TclThreadDataKeyGet(&dataKey);
+ /* We were called by the event procedure and
+ * continue our loop. */
+ ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
+ TclThreadDataKeyGet(&dataKey);
if (async_callback) {
- goto reenter;
+ goto reenter;
}
for (statePtr->addr = statePtr->addrlist; statePtr->addr != NULL;
@@ -1682,10 +1707,10 @@ TcpConnect(
continue;
}
- /*
- * Close the socket if it is still open from the last unsuccessful
- * iteration.
- */
+ /*
+ * Close the socket if it is still open from the last unsuccessful
+ * iteration.
+ */
if (statePtr->sockets->fd != INVALID_SOCKET) {
closesocket(statePtr->sockets->fd);
@@ -1998,16 +2023,16 @@ Tcl_OpenTcpClient(
*/
if (!TclCreateSocketAddress(interp, &addrlist, host, port, 0, &errorMsg)
- || !TclCreateSocketAddress(interp, &myaddrlist, myaddr, myport, 1,
- &errorMsg)) {
- if (addrlist != NULL) {
- freeaddrinfo(addrlist);
- }
- if (interp != NULL) {
- Tcl_SetObjResult(interp, Tcl_ObjPrintf(
- "couldn't open socket: %s", errorMsg));
- }
- return NULL;
+ || !TclCreateSocketAddress(interp, &myaddrlist, myaddr, myport, 1,
+ &errorMsg)) {
+ if (addrlist != NULL) {
+ freeaddrinfo(addrlist);
+ }
+ if (interp != NULL) {
+ Tcl_SetObjResult(interp, Tcl_ObjPrintf(
+ "couldn't open socket: %s", errorMsg));
+ }
+ return NULL;
}
statePtr = NewSocketInfo(INVALID_SOCKET);
@@ -2060,13 +2085,10 @@ Tcl_Channel
Tcl_MakeTcpClientChannel(
void *sock) /* The socket to wrap up into a channel. */
{
- TcpState *statePtr;
- char channelName[SOCK_CHAN_LENGTH];
- ThreadSpecificData *tsdPtr;
-
TclInitSockets();
- tsdPtr = (ThreadSpecificData *)TclThreadDataKeyGet(&dataKey);
+ ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
+ TclThreadDataKeyGet(&dataKey);
/*
* Set kernel space buffering and non-blocking.
@@ -2074,7 +2096,7 @@ Tcl_MakeTcpClientChannel(
TclSockMinimumBuffers(sock, TCP_BUFFER_SIZE);
- statePtr = NewSocketInfo((SOCKET) sock);
+ TcpState *statePtr = NewSocketInfo((SOCKET) sock);
/*
* Start watching for read/write events on the socket.
@@ -2083,6 +2105,7 @@ Tcl_MakeTcpClientChannel(
statePtr->selectEvents = FD_READ | FD_CLOSE | FD_WRITE;
SendSelectMessage(tsdPtr, SELECT, statePtr);
+ char channelName[SOCK_CHAN_LENGTH];
TclWinGenerateChannelName(channelName, "sock", statePtr);
statePtr->channel = Tcl_CreateChannel(&tcpChannelType, channelName,
statePtr, (TCL_READABLE | TCL_WRITABLE));
@@ -2113,8 +2136,8 @@ Tcl_OpenTcpServerEx(
const char *service, /* Port number to open. */
const char *myHost, /* Name of local host. */
unsigned int flags, /* Flags. */
- int backlog, /* Length of OS listen backlog queue, or -1
- * for default. */
+ int backlog, /* Length of OS listen backlog queue, or -1
+ * for default. */
Tcl_TcpAcceptProc *acceptProc,
/* Callback for accepting connections from new
* clients. */
@@ -2148,7 +2171,7 @@ Tcl_OpenTcpServerEx(
for (addrPtr = addrlist; addrPtr != NULL; addrPtr = addrPtr->ai_next) {
sock = socket(addrPtr->ai_family, addrPtr->ai_socktype,
- addrPtr->ai_protocol);
+ addrPtr->ai_protocol);
if (sock == INVALID_SOCKET) {
Tcl_WinConvertError((DWORD) WSAGetLastError());
continue;
@@ -2225,9 +2248,9 @@ Tcl_OpenTcpServerEx(
* different, and there may be differences between TCP/IP stacks).
*/
- if (backlog < 0) {
- backlog = SOMAXCONN;
- }
+ if (backlog < 0) {
+ backlog = SOMAXCONN;
+ }
if (listen(sock, backlog) == SOCKET_ERROR) {
Tcl_WinConvertError((DWORD) WSAGetLastError());
closesocket(sock);
@@ -2251,7 +2274,8 @@ Tcl_OpenTcpServerEx(
}
if (statePtr != NULL) {
- ThreadSpecificData *tsdPtr = (ThreadSpecificData *)TclThreadDataKeyGet(&dataKey);
+ ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
+ TclThreadDataKeyGet(&dataKey);
statePtr->acceptProc = acceptProc;
statePtr->acceptProcData = acceptProcData;
@@ -2280,7 +2304,7 @@ Tcl_OpenTcpServerEx(
}
if (interp != NULL) {
- Tcl_SetObjResult(interp, Tcl_ObjPrintf(
+ Tcl_SetObjResult(interp, Tcl_ObjPrintf(
"couldn't open socket: %s",
(errorMsg ? errorMsg : Tcl_PosixError(interp))));
}
@@ -2318,7 +2342,8 @@ TcpAccept(
int len = sizeof(addr);
char channelName[SOCK_CHAN_LENGTH];
char host[NI_MAXHOST], port[NI_MAXSERV];
- ThreadSpecificData *tsdPtr = (ThreadSpecificData *)TclThreadDataKeyGet(&dataKey);
+ ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
+ TclThreadDataKeyGet(&dataKey);
/*
* Win-NT has a misfeature that sockets are inherited in child processes
@@ -2545,9 +2570,9 @@ SocketCheckProc(
statePtr = statePtr->nextPtr) {
if (GOT_BITS(statePtr->readyEvents,
statePtr->watchEvents | FD_CONNECT | FD_ACCEPT)
- && !GOT_BITS(statePtr->flags, SOCKET_PENDING)) {
+ && !GOT_BITS(statePtr->flags, SOCKET_PENDING)) {
SET_BITS(statePtr->flags, SOCKET_PENDING);
- evPtr = (SocketEvent *)ckalloc(sizeof(SocketEvent));
+ evPtr = (SocketEvent *) ckalloc(sizeof(SocketEvent));
evPtr->header.proc = SocketEventProc;
evPtr->socket = statePtr->sockets->fd;
Tcl_QueueEvent((Tcl_Event *) evPtr, TCL_QUEUE_TAIL);
@@ -2613,7 +2638,7 @@ SocketEventProc(
*/
if (!statePtr) {
- SetEvent(tsdPtr->socketListLock);
+ SetEvent(tsdPtr->socketListLock);
return 1;
}
@@ -2822,7 +2847,7 @@ AddSocketInfoFd(
* Add the first FD.
*/
- statePtr->sockets = (TcpFdList *)ckalloc(sizeof(TcpFdList));
+ statePtr->sockets = (TcpFdList *) ckalloc(sizeof(TcpFdList));
fds = statePtr->sockets;
} else {
/*
@@ -2833,7 +2858,7 @@ AddSocketInfoFd(
fds = fds->next;
}
- fds->next = (TcpFdList *)ckalloc(sizeof(TcpFdList));
+ fds->next = (TcpFdList *) ckalloc(sizeof(TcpFdList));
fds = fds->next;
}
@@ -2845,8 +2870,7 @@ AddSocketInfoFd(
fds->statePtr = statePtr;
fds->next = NULL;
}
-
-
+
/*
*----------------------------------------------------------------------
*
@@ -2864,9 +2888,10 @@ AddSocketInfoFd(
*/
static TcpState *
-NewSocketInfo(SOCKET socket)
+NewSocketInfo(
+ SOCKET socket)
{
- TcpState *statePtr = (TcpState *)ckalloc(sizeof(TcpState));
+ TcpState *statePtr = (TcpState *) ckalloc(sizeof(TcpState));
memset(statePtr, 0, sizeof(TcpState));
@@ -2901,15 +2926,15 @@ NewSocketInfo(SOCKET socket)
static int
WaitForSocketEvent(
- TcpState *statePtr, /* Information about this socket. */
+ TcpState *statePtr, /* Information about this socket. */
int events, /* Events to look for. May be one of
- * FD_READ or FD_WRITE.
- */
+ * FD_READ or FD_WRITE. */
int *errorCodePtr) /* Where to store errors? */
{
int result = 1;
int oldMode;
- ThreadSpecificData *tsdPtr = (ThreadSpecificData *)TclThreadDataKeyGet(&dataKey);
+ ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
+ TclThreadDataKeyGet(&dataKey);
/*
* Be sure to disable event servicing so we are truly modal.
@@ -3036,7 +3061,6 @@ SocketThread(
return msg.wParam;
}
-
/*
*----------------------------------------------------------------------