aboutsummaryrefslogtreecommitdiff
path: root/src/util/network.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2013-12-13 15:53:37 +0000
committerChristian Grothoff <christian@grothoff.org>2013-12-13 15:53:37 +0000
commite03e468931f18fceed2a7bbcbe258b865c26ed26 (patch)
tree8a205a76bc11d6e000ef561e79dac9446fbcfeac /src/util/network.c
parent16fca3835b569ed8427d786e3cdb95dc01ceb342 (diff)
downloadgnunet-e03e468931f18fceed2a7bbcbe258b865c26ed26.tar.gz
gnunet-e03e468931f18fceed2a7bbcbe258b865c26ed26.zip
-clean up network select code, avoid insane #ifdefing for MINGW
Diffstat (limited to 'src/util/network.c')
-rw-r--r--src/util/network.c89
1 files changed, 60 insertions, 29 deletions
diff --git a/src/util/network.c b/src/util/network.c
index de53470c1..2ee1a73a0 100644
--- a/src/util/network.c
+++ b/src/util/network.c
@@ -157,7 +157,8 @@ GNUNET_NETWORK_shorten_unixpath (char *unixpath)
157 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 157 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
158 */ 158 */
159int 159int
160GNUNET_NETWORK_socket_set_blocking (struct GNUNET_NETWORK_Handle *fd, int doBlock) 160GNUNET_NETWORK_socket_set_blocking (struct GNUNET_NETWORK_Handle *fd,
161 int doBlock)
161{ 162{
162 163
163#if MINGW 164#if MINGW
@@ -869,7 +870,7 @@ GNUNET_NETWORK_socket_create (int domain, int type, int protocol)
869 * Shut down socket operations 870 * Shut down socket operations
870 * @param desc socket 871 * @param desc socket
871 * @param how type of shutdown 872 * @param how type of shutdown
872 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise 873 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
873 */ 874 */
874int 875int
875GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc, int how) 876GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc, int how)
@@ -1308,6 +1309,7 @@ _selector (LPVOID p)
1308#endif 1309#endif
1309 1310
1310 1311
1312#ifndef MINGW
1311/** 1313/**
1312 * Check if sockets or pipes meet certain conditions 1314 * Check if sockets or pipes meet certain conditions
1313 * 1315 *
@@ -1323,8 +1325,58 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1323 struct GNUNET_NETWORK_FDSet *efds, 1325 struct GNUNET_NETWORK_FDSet *efds,
1324 const struct GNUNET_TIME_Relative timeout) 1326 const struct GNUNET_TIME_Relative timeout)
1325{ 1327{
1328 int nfds;
1329 struct timeval tv;
1330
1331 if (NULL != rfds)
1332 nfds = rfds->nsds;
1333 else
1334 nfds = 0;
1335 if (NULL != wfds)
1336 nfds = GNUNET_MAX (nfds, wfds->nsds);
1337 if (NULL != efds)
1338 nfds = GNUNET_MAX (nfds, efds->nsds);
1339 if ((nfds == 0) &&
1340 (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us))
1341 {
1342 GNUNET_break (0);
1343 LOG (GNUNET_ERROR_TYPE_ERROR,
1344 _("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1345 "select");
1346 }
1347 tv.tv_sec = timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us;
1348 tv.tv_usec =
1349 (timeout.rel_value_us -
1350 (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value_us));
1351 return select (nfds,
1352 (NULL != rfds) ? &rfds->sds : NULL,
1353 (NULL != wfds) ? &wfds->sds : NULL,
1354 (NULL != efds) ? &efds->sds : NULL,
1355 (timeout.rel_value_us ==
1356 GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) ? NULL : &tv);
1357}
1358
1359
1360#else
1361/* MINGW */
1362
1363
1364/**
1365 * Check if sockets or pipes meet certain conditions, version for W32.
1366 *
1367 * @param rfds set of sockets or pipes to be checked for readability
1368 * @param wfds set of sockets or pipes to be checked for writability
1369 * @param efds set of sockets or pipes to be checked for exceptions
1370 * @param timeout relative value when to return
1371 * @return number of selected sockets or pipes, #GNUNET_SYSERR on error
1372 */
1373int
1374GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1375 struct GNUNET_NETWORK_FDSet *wfds,
1376 struct GNUNET_NETWORK_FDSet *efds,
1377 const struct GNUNET_TIME_Relative timeout)
1378{
1326 int nfds = 0; 1379 int nfds = 0;
1327#ifdef MINGW
1328 int handles = 0; 1380 int handles = 0;
1329 int ex_handles = 0; 1381 int ex_handles = 0;
1330 int read_handles = 0; 1382 int read_handles = 0;
@@ -1373,13 +1425,11 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1373 1425
1374 /* TODO: Make this growable */ 1426 /* TODO: Make this growable */
1375 struct GNUNET_DISK_FileHandle *readArray[50]; 1427 struct GNUNET_DISK_FileHandle *readArray[50];
1376#else
1377 struct timeval tv; 1428 struct timeval tv;
1378#endif 1429
1379 if (NULL != rfds) 1430 if (NULL != rfds)
1380 { 1431 {
1381 nfds = rfds->nsds; 1432 nfds = rfds->nsds;
1382#ifdef MINGW
1383 handles += read_handles = GNUNET_CONTAINER_slist_count (rfds->handles); 1433 handles += read_handles = GNUNET_CONTAINER_slist_count (rfds->handles);
1384#if DEBUG_NETWORK 1434#if DEBUG_NETWORK
1385 { 1435 {
@@ -1398,48 +1448,27 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1398 } 1448 }
1399 } 1449 }
1400#endif 1450#endif
1401#endif
1402 } 1451 }
1403 if (NULL != wfds) 1452 if (NULL != wfds)
1404 { 1453 {
1405 nfds = GNUNET_MAX (nfds, wfds->nsds); 1454 nfds = GNUNET_MAX (nfds, wfds->nsds);
1406#ifdef MINGW
1407 handles += write_handles = GNUNET_CONTAINER_slist_count (wfds->handles); 1455 handles += write_handles = GNUNET_CONTAINER_slist_count (wfds->handles);
1408#endif
1409 } 1456 }
1410 if (NULL != efds) 1457 if (NULL != efds)
1411 { 1458 {
1412 nfds = GNUNET_MAX (nfds, efds->nsds); 1459 nfds = GNUNET_MAX (nfds, efds->nsds);
1413#ifdef MINGW
1414 handles += ex_handles = GNUNET_CONTAINER_slist_count (efds->handles); 1460 handles += ex_handles = GNUNET_CONTAINER_slist_count (efds->handles);
1415#endif
1416 } 1461 }
1417 1462
1418 if ((nfds == 0) && 1463 if ((nfds == 0) &&
1419 (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) 1464 (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
1420#ifdef MINGW 1465 && (handles == 0) )
1421 && handles == 0
1422#endif
1423 )
1424 { 1466 {
1425 GNUNET_break (0); 1467 GNUNET_break (0);
1426 LOG (GNUNET_ERROR_TYPE_ERROR, 1468 LOG (GNUNET_ERROR_TYPE_ERROR,
1427 _("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"), 1469 _("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1428 "select"); 1470 "select");
1429 } 1471 }
1430#ifndef MINGW
1431 tv.tv_sec = timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us;
1432 tv.tv_usec =
1433 (timeout.rel_value_us -
1434 (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value_us));
1435 return select (nfds,
1436 (NULL != rfds) ? &rfds->sds : NULL,
1437 (NULL != wfds) ? &wfds->sds : NULL,
1438 (NULL != efds) ? &efds->sds : NULL,
1439 (timeout.rel_value_us ==
1440 GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) ? NULL : &tv);
1441
1442#else
1443#define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set)) 1472#define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
1444 /* calculate how long we need to wait in microseconds */ 1473 /* calculate how long we need to wait in microseconds */
1445 if (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) 1474 if (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
@@ -2042,8 +2071,10 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
2042 if (nhandles && (returnedpos < nhandles)) 2071 if (nhandles && (returnedpos < nhandles))
2043 return retcode; 2072 return retcode;
2044 else 2073 else
2045#endif
2046 return 0; 2074 return 0;
2047} 2075}
2048 2076
2077/* MINGW */
2078#endif
2079
2049/* end of network.c */ 2080/* end of network.c */