Diff
Not logged in

Differences From Artifact [d93ec7eb7a]:

To Artifact [c6951ea314]:


247
248
249
250
251
252
253


254
255
256
257
258
259
260
static TcpState *	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 int		WaitForConnect(TcpState *statePtr, int *errorCodePtr);


static int		WaitForSocketEvent(TcpState *statePtr, int events,
			    int *errorCodePtr);
static void		AddSocketInfoFd(TcpState *statePtr,  SOCKET socket);
static int		FindFDInList(TcpState *statePtr, SOCKET socket);
static DWORD WINAPI	SocketThread(LPVOID arg);
static void		TcpThreadActionProc(ClientData instanceData,
			    int action);







>
>







247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
static TcpState *	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 int		WaitForConnect(TcpState *statePtr, int *errorCodePtr);
static int		GetSocketError(TcpState *statePtr);

static int		WaitForSocketEvent(TcpState *statePtr, int events,
			    int *errorCodePtr);
static void		AddSocketInfoFd(TcpState *statePtr,  SOCKET socket);
static int		FindFDInList(TcpState *statePtr, SOCKET socket);
static DWORD WINAPI	SocketThread(LPVOID arg);
static void		TcpThreadActionProc(ClientData instanceData,
			    int action);
1155
1156
1157
1158
1159
1160
1161
1162
1163






1164


1165
1166
1167

1168























1169
1170
1171

1172















1173




1174





1175
















1176
1177
1178
1179
1180
1181
1182
    if (!SocketsEnabled()) {
	if (interp) {
	    Tcl_SetObjResult(interp, Tcl_NewStringObj(
		    "winsock is not initialized", -1));
	}
	return TCL_ERROR;
    }

    /*






     * Set socket test int value


     */
    if (!strcmp(optionName, "-unsupported1")) {
	int intValue;

	if (Tcl_GetInt(interp, value, &intValue) != TCL_OK) {























		return TCL_ERROR;
	}
	if (intValue & 1) {

		SET_BITS(statePtr->flags,TCP_ASYNC_TEST_MODE);















	} else {




		CLEAR_BITS(statePtr->flags,TCP_ASYNC_TEST_MODE);





	}
















	return TCL_OK;
    }

#ifdef TCL_FEATURE_KEEPALIVE_NAGLE
    #error "TCL_FEATURE_KEEPALIVE_NAGLE not reviewed for whether to treat statePtr->sockets as single fd or list"
    sock = statePtr->sockets->fd;









|
>
>
>
>
>
>
|
>
>

|
|
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|

|
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
|
>
>
>
>
>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
    if (!SocketsEnabled()) {
	if (interp) {
	    Tcl_SetObjResult(interp, Tcl_NewStringObj(
		    "winsock is not initialized", -1));
	}
	return TCL_ERROR;
    }

    /* 
     * Go one step in async connect
     * If any error is thrown save it as backround error to report eventually below
     */
    if (! (statePtr->flags & TCP_ASYNC_TEST_MODE) ) {
	WaitForConnect(statePtr, NULL);
    }

    /*
     * Option -error otherVar: Return socket error and socket error dict (TIP 428)
     */
    if (!strcmp(optionName, "-error")) {

	Tcl_Obj *errorDictPtr;

	/*
	 * Get error code and clear it
	 */
	int errorCode=GetSocketError(statePtr);
	
	/*
	 * Check for interpreter - otherwise we can not output
	 */
	if (!interp) {
	    return TCL_OK;
	}
	
	/*
	 * Clear any existing result
	 */
	Tcl_ResetResult(interp);
	
	/*
	 * Write -code key to dictionary with value 0/1
	 */
	errorDictPtr = Tcl_NewDictObj();
	if ( TCL_ERROR == Tcl_DictObjPut(interp, errorDictPtr,
		Tcl_NewStringObj("-code",-1), Tcl_NewBooleanObj(errorCode)) ) {
	    return TCL_ERROR;
	}

	if (0 != errorCode) {

	    /*
	     * Add key -errorcode with list value: POSIX id message
	     */
	    Tcl_Obj *errorMessagePtr;
	    Tcl_Obj *valuePtr = Tcl_NewObj();
	    errorMessagePtr = Tcl_NewStringObj(Tcl_ErrnoMsg(errorCode),-1);
	    Tcl_SetErrno(errorCode);
	    if (TCL_ERROR == Tcl_ListObjAppendElement(interp, valuePtr,
			Tcl_NewStringObj("POSSIX",-1)) ||
		    TCL_ERROR == Tcl_ListObjAppendElement(interp, valuePtr,
			Tcl_NewStringObj(Tcl_ErrnoId(),-1)) ||
		    TCL_ERROR == Tcl_ListObjAppendElement(interp, valuePtr,
			errorMessagePtr)) {
		return TCL_ERROR;
	    } 

	    if ( TCL_ERROR == Tcl_DictObjPut(interp, errorDictPtr,
		    Tcl_NewStringObj("-errorcode",-1), valuePtr) ) {
		return TCL_ERROR;
	    }

	    /*
	     * Set the result to the error message (shared with last list
	     * member of the -errorcode value).
	     */
	    Tcl_SetObjResult(interp,errorMessagePtr);
	}

	/*
	 * Save to specified variable
	 */
	if ( NULL ==
		Tcl_SetVar2Ex(interp, value, NULL, errorDictPtr, TCL_LEAVE_ERR_MSG ))
	{
	    /*
	     * Setting variable failed. This may also due to a variable name issue
	     * like an existing array with the same name.
	     * Thus treat this gracefully and clear temporary memory.
	     */
	    Tcl_DecrRefCount(errorDictPtr);
	    return TCL_ERROR;
	}
	
	return TCL_OK;
    }

#ifdef TCL_FEATURE_KEEPALIVE_NAGLE
    #error "TCL_FEATURE_KEEPALIVE_NAGLE not reviewed for whether to treat statePtr->sockets as single fd or list"
    sock = statePtr->sockets->fd;

1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
			Tcl_PosixError(interp)));
	    }
	    return TCL_ERROR;
	}
	return TCL_OK;
    }

    return Tcl_BadChannelOption(interp, optionName, "keepalive nagle");
#else
    return Tcl_BadChannelOption(interp, optionName, "");
#endif /*TCL_FEATURE_KEEPALIVE_NAGLE*/
}

/*
 *----------------------------------------------------------------------
 *
 * TcpGetOptionProc --







|

|







1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
			Tcl_PosixError(interp)));
	    }
	    return TCL_ERROR;
	}
	return TCL_OK;
    }

    return Tcl_BadChannelOption(interp, optionName, "error keepalive nagle");
#else
    return Tcl_BadChannelOption(interp, optionName, "error");
#endif /*TCL_FEATURE_KEEPALIVE_NAGLE*/
}

/*
 *----------------------------------------------------------------------
 *
 * TcpGetOptionProc --
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
    }

    sock = statePtr->sockets->fd;
    if (optionName != NULL) {
	len = strlen(optionName);
    }

    if ((len > 1)
	    && ((optionName[1] == 'e') && (strncmp(optionName, "-error", len) == 0)
	    || (optionName[1] == 'o')
		&& (strncmp(optionName, "-options", len) == 0))) {
	int errorCode=0;
	/*
	 * Do not return any errors if async connect is running
	 */
	if ( ! (statePtr->flags & TCP_ASYNC_PENDING) ) {


	    if ( statePtr->flags & TCP_ASYNC_FAILED ) {

		/*
		 * In case of a failed async connect, eventually report the
		 * connect error only once.
		 * Do not report the system error, as this comes again and again.
		 */

		errorCode = statePtr->connectError;
		statePtr->connectError = 0;

	    } else {

		/*
		 * Report an eventual last error of the socket system
		 */

		int optlen;
		int ret;
		DWORD err;

		/*
		 * Populater the err Variable with a possix error
		 */
		optlen = sizeof(int);
		ret = getsockopt(sock, SOL_SOCKET, SO_ERROR,
			(char *)&err, &optlen);
		/*
		 * The error was not returned directly but should be
		 * taken from WSA
		 */
		if (ret == SOCKET_ERROR) {
		    err = WSAGetLastError();
		}
		if (err) {
		    TclWinConvertError(err);
		    errorCode = Tcl_GetErrno();
		}
	    }
	}
	if (optionName[1] == 'e') {
	    /*
	     * Return error message
	     */
	    if (errorCode != 0) {
		Tcl_DStringAppend(dsPtr, Tcl_ErrnoMsg(errorCode), -1);
	    }
	} else {
	    /*
	     * Return error dict
	     */
	    Tcl_DStringAppendElement(dsPtr, "-code");
	    if (errorCode == 0) {
		Tcl_DStringAppendElement(dsPtr, "0");
	    } else {
		Tcl_DStringAppendElement(dsPtr, "1");
		Tcl_DStringAppendElement(dsPtr, "-errorcode");
		Tcl_DStringStartSublist(dsPtr);
		Tcl_DStringAppendElement(dsPtr, "POSIX");
		Tcl_SetErrno(errorCode);
		Tcl_DStringAppendElement(dsPtr, Tcl_ErrnoId());
		Tcl_DStringAppendElement(dsPtr, Tcl_ErrnoMsg(errorCode));
		Tcl_DStringEndSublist(dsPtr);
		Tcl_DStringAppendElement(dsPtr, "-message");
		Tcl_DStringAppendElement(dsPtr, Tcl_ErrnoMsg(errorCode));
	    }
	}
	return TCL_OK;
    }

    if ((len > 1) && (optionName[1] == 'c') &&
	    (strncmp(optionName, "-connecting", len) == 0)) {








|
|
<
<
<

<
<
<
|
<
<
<
<
<
<
<
|
|
<
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
|
|
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







1368
1369
1370
1371
1372
1373
1374
1375
1376



1377



1378







1379
1380





1381




























1382
1383
1384
1385




















1386
1387
1388
1389
1390
1391
1392
    }

    sock = statePtr->sockets->fd;
    if (optionName != NULL) {
	len = strlen(optionName);
    }

    if ((len > 1) && (optionName[1] == 'e') &&
	    (strncmp(optionName, "-error", len) == 0)) {



	/*



	 * Get error code and clear it







	 */
	int errorCode=GetSocketError(statePtr);





	/*




























	 * Return error message
	 */
	if (errorCode != 0) {
	    Tcl_DStringAppend(dsPtr, Tcl_ErrnoMsg(errorCode), -1);




















	}
	return TCL_OK;
    }

    if ((len > 1) && (optionName[1] == 'c') &&
	    (strncmp(optionName, "-connecting", len) == 0)) {

1565
1566
1567
1568
1569
1570
1571





































































1572
1573
1574
1575
1576
1577
1578
#else
	return Tcl_BadChannelOption(interp, optionName, "connecting peername sockname");
#endif /*TCL_FEATURE_KEEPALIVE_NAGLE*/
    }

    return TCL_OK;
}






































































/*
 *----------------------------------------------------------------------
 *
 * TcpWatchProc --
 *
 *	Informs the channel driver of the events that the generic channel code







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
#else
	return Tcl_BadChannelOption(interp, optionName, "connecting peername sockname");
#endif /*TCL_FEATURE_KEEPALIVE_NAGLE*/
    }

    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * GetSocketError --
 *
 *	Get the error code for fconfigure -error.
 *
 * Results:
 *	error code.
 *
 * Side effects:
 *	Resets the error state.
 *
 *----------------------------------------------------------------------
 */

static int
GetSocketError(
    TcpState *statePtr)		/* The socket state. */
{
    int errorCode = 0;
    
    /*
     * Do not return any errors if async connect is running
     */
    if ( (statePtr->flags & TCP_ASYNC_PENDING) ) {
	return 0;
    }
    if ( statePtr->flags & TCP_ASYNC_FAILED ) {

	/*
	 * In case of a failed async connect, eventually report the
	 * connect error only once.
	 * Do not report the system error, as this comes again and again.
	 */

	errorCode = statePtr->connectError;
	statePtr->connectError = 0;
    } else {

	/*
	 * Report an eventual last error of the socket system
	 */

	int optlen;
	int ret;
	DWORD err;

	/*
	 * Populater the err Variable with a possix error
	 */
	optlen = sizeof(int);
	ret = getsockopt(statePtr->sockets->fd, SOL_SOCKET, SO_ERROR,
		(char *)&err, &optlen);
	/*
	 * The error was not returned directly but should be
	 * taken from WSA
	 */
	if (ret == SOCKET_ERROR) {
	    err = WSAGetLastError();
	}
	if (err) {
	    TclWinConvertError(err);
	    errorCode = Tcl_GetErrno();
	}
    }
    return errorCode;
}

/*
 *----------------------------------------------------------------------
 *
 * TcpWatchProc --
 *
 *	Informs the channel driver of the events that the generic channel code