aboutsummaryrefslogtreecommitdiff
path: root/src/util/network.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/network.c')
-rw-r--r--src/util/network.c1137
1 files changed, 579 insertions, 558 deletions
diff --git a/src/util/network.c b/src/util/network.c
index 4fa1ce27a..a62a57758 100644
--- a/src/util/network.c
+++ b/src/util/network.c
@@ -29,6 +29,10 @@
29#include "disk.h" 29#include "disk.h"
30#include "gnunet_container_lib.h" 30#include "gnunet_container_lib.h"
31 31
32#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
33
34#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
35
32#define DEBUG_NETWORK GNUNET_EXTRA_LOGGING 36#define DEBUG_NETWORK GNUNET_EXTRA_LOGGING
33 37
34#define DEBUG_W32_CYCLES GNUNET_EXTRA_LOGGING 38#define DEBUG_W32_CYCLES GNUNET_EXTRA_LOGGING
@@ -86,11 +90,11 @@ socket_set_blocking (struct GNUNET_NETWORK_Handle *fd, int doBlock)
86 mode = !doBlock; 90 mode = !doBlock;
87 if (ioctlsocket (fd->fd, FIONBIO, &mode) == SOCKET_ERROR) 91 if (ioctlsocket (fd->fd, FIONBIO, &mode) == SOCKET_ERROR)
88 92
89 { 93 {
90 SetErrnoFromWinsockError (WSAGetLastError ()); 94 SetErrnoFromWinsockError (WSAGetLastError ());
91 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctlsocket"); 95 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "ioctlsocket");
92 return GNUNET_SYSERR; 96 return GNUNET_SYSERR;
93 } 97 }
94 return GNUNET_OK; 98 return GNUNET_OK;
95 99
96#else 100#else
@@ -99,10 +103,10 @@ socket_set_blocking (struct GNUNET_NETWORK_Handle *fd, int doBlock)
99 103
100 if (flags == -1) 104 if (flags == -1)
101 105
102 { 106 {
103 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl"); 107 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "fcntl");
104 return GNUNET_SYSERR; 108 return GNUNET_SYSERR;
105 } 109 }
106 if (doBlock) 110 if (doBlock)
107 flags &= ~O_NONBLOCK; 111 flags &= ~O_NONBLOCK;
108 112
@@ -110,10 +114,10 @@ socket_set_blocking (struct GNUNET_NETWORK_Handle *fd, int doBlock)
110 flags |= O_NONBLOCK; 114 flags |= O_NONBLOCK;
111 if (0 != fcntl (fd->fd, F_SETFL, flags)) 115 if (0 != fcntl (fd->fd, F_SETFL, flags))
112 116
113 { 117 {
114 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl"); 118 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "fcntl");
115 return GNUNET_SYSERR; 119 return GNUNET_SYSERR;
116 } 120 }
117 return GNUNET_OK; 121 return GNUNET_OK;
118#endif 122#endif
119} 123}
@@ -158,8 +162,8 @@ socket_set_nosigpipe (const struct GNUNET_NETWORK_Handle *h)
158 162
159 if (0 != 163 if (0 !=
160 setsockopt (h->fd, SOL_SOCKET, SO_NOSIGPIPE, &abs_value, 164 setsockopt (h->fd, SOL_SOCKET, SO_NOSIGPIPE, &abs_value,
161 sizeof (abs_value))) 165 sizeof (abs_value)))
162 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); 166 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
163} 167}
164#endif 168#endif
165 169
@@ -177,15 +181,16 @@ socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h)
177#ifndef WINDOWS 181#ifndef WINDOWS
178 int value = 1; 182 int value = 1;
179 183
180 if (0 != setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, &value, sizeof (value))) 184 if (0 !=
181 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); 185 setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, &value, sizeof (value)))
186 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
182#else 187#else
183 const char *abs_value = "1"; 188 const char *abs_value = "1";
184 189
185 if (0 != 190 if (0 !=
186 setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, abs_value, 191 setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, abs_value,
187 sizeof (abs_value))) 192 sizeof (abs_value)))
188 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); 193 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
189#endif 194#endif
190} 195}
191 196
@@ -200,7 +205,8 @@ socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h)
200 */ 205 */
201struct GNUNET_NETWORK_Handle * 206struct GNUNET_NETWORK_Handle *
202GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc, 207GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
203 struct sockaddr *address, socklen_t * address_len) 208 struct sockaddr *address,
209 socklen_t * address_len)
204{ 210{
205 struct GNUNET_NETWORK_Handle *ret; 211 struct GNUNET_NETWORK_Handle *ret;
206 212
@@ -212,8 +218,8 @@ GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
212 int gsn = getsockname (desc->fd, &name, &namelen); 218 int gsn = getsockname (desc->fd, &name, &namelen);
213 219
214 if (gsn == 0) 220 if (gsn == 0)
215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Accepting connection on `%s'\n", 221 LOG (GNUNET_ERROR_TYPE_DEBUG, "Accepting connection on `%s'\n",
216 GNUNET_a2s (&name, namelen)); 222 GNUNET_a2s (&name, namelen));
217 } 223 }
218#endif 224#endif
219 ret->fd = accept (desc->fd, address, address_len); 225 ret->fd = accept (desc->fd, address, address_len);
@@ -222,36 +228,36 @@ GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
222 else 228 else
223 ret->af = desc->af; 229 ret->af = desc->af;
224 if (ret->fd == INVALID_SOCKET) 230 if (ret->fd == INVALID_SOCKET)
225 { 231 {
226#ifdef MINGW 232#ifdef MINGW
227 SetErrnoFromWinsockError (WSAGetLastError ()); 233 SetErrnoFromWinsockError (WSAGetLastError ());
228#endif 234#endif
229 GNUNET_free (ret); 235 GNUNET_free (ret);
230 return NULL; 236 return NULL;
231 } 237 }
232#ifndef MINGW 238#ifndef MINGW
233 if (ret->fd >= FD_SETSIZE) 239 if (ret->fd >= FD_SETSIZE)
234 { 240 {
235 GNUNET_break (0 == close (ret->fd)); 241 GNUNET_break (0 == close (ret->fd));
236 GNUNET_free (ret); 242 GNUNET_free (ret);
237 errno = EMFILE; 243 errno = EMFILE;
238 return NULL; 244 return NULL;
239 } 245 }
240#endif 246#endif
241 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO)) 247 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
242 248
243 { 249 {
244 250
245 /* we might want to treat this one as fatal... */ 251 /* we might want to treat this one as fatal... */
246 GNUNET_break (0); 252 GNUNET_break (0);
247 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret)); 253 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
248 return NULL; 254 return NULL;
249 } 255 }
250 256
251#ifndef MINGW 257#ifndef MINGW
252 if (GNUNET_OK != socket_set_inheritable (ret)) 258 if (GNUNET_OK != socket_set_inheritable (ret))
253 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 259 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
254 "socket_set_inheritable"); 260 "socket_set_inheritable");
255#endif 261#endif
256#ifdef DARWIN 262#ifdef DARWIN
257 socket_set_nosigpipe (ret); 263 socket_set_nosigpipe (ret);
@@ -273,8 +279,8 @@ GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
273 */ 279 */
274int 280int
275GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc, 281GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
276 const struct sockaddr *address, 282 const struct sockaddr *address,
277 socklen_t address_len) 283 socklen_t address_len)
278{ 284{
279 int ret; 285 int ret;
280 286
@@ -283,23 +289,24 @@ GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
283 const int on = 1; 289 const int on = 1;
284 290
285 if (desc->af == AF_INET6) 291 if (desc->af == AF_INET6)
286 if (0 != setsockopt (desc->fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on))) 292 if (0 !=
287 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "setsockopt"); 293 setsockopt (desc->fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)))
294 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG, "setsockopt");
288#endif 295#endif
289#endif 296#endif
290#ifndef WINDOWS 297#ifndef WINDOWS
291 /* This is required, and required here, but only on UNIX */ 298 /* This is required, and required here, but only on UNIX */
292 if (0 != setsockopt (desc->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on))) 299 if (0 != setsockopt (desc->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)))
293 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "setsockopt"); 300 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG, "setsockopt");
294#endif 301#endif
295#ifndef LINUX 302#ifndef LINUX
296#ifndef MINGW 303#ifndef MINGW
297 if (address->sa_family == AF_UNIX) 304 if (address->sa_family == AF_UNIX)
298 { 305 {
299 const struct sockaddr_un *un = (const struct sockaddr_un *) address; 306 const struct sockaddr_un *un = (const struct sockaddr_un *) address;
300 307
301 (void) unlink (un->sun_path); 308 (void) unlink (un->sun_path);
302 } 309 }
303#endif 310#endif
304#endif 311#endif
305 ret = bind (desc->fd, address, address_len); 312 ret = bind (desc->fd, address, address_len);
@@ -334,17 +341,17 @@ GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
334 DWORD error = 0; 341 DWORD error = 0;
335 342
336#if DEBUG_NETWORK 343#if DEBUG_NETWORK
337 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_NETWORK_socket_close", 344 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_NETWORK_socket_close",
338 "Closing 0x%x\n", desc->fd); 345 "Closing 0x%x\n", desc->fd);
339#endif 346#endif
340 SetLastError (0); 347 SetLastError (0);
341 ret = closesocket (desc->fd); 348 ret = closesocket (desc->fd);
342 error = WSAGetLastError (); 349 error = WSAGetLastError ();
343 SetErrnoFromWinsockError (error); 350 SetErrnoFromWinsockError (error);
344#if DEBUG_NETWORK 351#if DEBUG_NETWORK
345 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_NETWORK_socket_close", 352 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_NETWORK_socket_close",
346 "Closed 0x%x, closesocket() returned %d, GLE is %u\n", 353 "Closed 0x%x, closesocket() returned %d, GLE is %u\n",
347 desc->fd, ret, error); 354 desc->fd, ret, error);
348#endif 355#endif
349#else 356#else
350 ret = close (desc->fd); 357 ret = close (desc->fd);
@@ -352,13 +359,12 @@ GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
352#ifndef LINUX 359#ifndef LINUX
353#ifndef MINGW 360#ifndef MINGW
354 if ((desc->af == AF_UNIX) && (NULL != desc->addr)) 361 if ((desc->af == AF_UNIX) && (NULL != desc->addr))
355 { 362 {
356 const struct sockaddr_un *un = (const struct sockaddr_un *) desc->addr; 363 const struct sockaddr_un *un = (const struct sockaddr_un *) desc->addr;
357 364
358 if (0 != unlink (un->sun_path)) 365 if (0 != unlink (un->sun_path))
359 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", 366 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", un->sun_path);
360 un->sun_path); 367 }
361 }
362#endif 368#endif
363#endif 369#endif
364 GNUNET_free_non_null (desc->addr); 370 GNUNET_free_non_null (desc->addr);
@@ -382,7 +388,7 @@ GNUNET_NETWORK_socket_box_native (int fd)
382 struct GNUNET_NETWORK_Handle *ret; 388 struct GNUNET_NETWORK_Handle *ret;
383 389
384 if (fcntl (fd, F_GETFD) < 0) 390 if (fcntl (fd, F_GETFD) < 0)
385 return NULL; /* invalid FD */ 391 return NULL; /* invalid FD */
386 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle)); 392 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
387 ret->fd = fd; 393 ret->fd = fd;
388 ret->af = AF_UNSPEC; 394 ret->af = AF_UNSPEC;
@@ -400,8 +406,8 @@ GNUNET_NETWORK_socket_box_native (int fd)
400 */ 406 */
401int 407int
402GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc, 408GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
403 const struct sockaddr *address, 409 const struct sockaddr *address,
404 socklen_t address_len) 410 socklen_t address_len)
405{ 411{
406 int ret; 412 int ret;
407 413
@@ -409,11 +415,11 @@ GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
409 415
410#ifdef MINGW 416#ifdef MINGW
411 if (SOCKET_ERROR == ret) 417 if (SOCKET_ERROR == ret)
412 { 418 {
413 SetErrnoFromWinsockError (WSAGetLastError ()); 419 SetErrnoFromWinsockError (WSAGetLastError ());
414 if (errno == EWOULDBLOCK) 420 if (errno == EWOULDBLOCK)
415 errno = EINPROGRESS; 421 errno = EINPROGRESS;
416 } 422 }
417#endif 423#endif
418 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR; 424 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
419} 425}
@@ -431,8 +437,8 @@ GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
431 */ 437 */
432int 438int
433GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc, 439GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
434 int level, int optname, void *optval, 440 int level, int optname, void *optval,
435 socklen_t * optlen) 441 socklen_t * optlen)
436{ 442{
437 int ret; 443 int ret;
438 444
@@ -457,7 +463,7 @@ GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
457 */ 463 */
458int 464int
459GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc, 465GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
460 int backlog) 466 int backlog)
461{ 467{
462 int ret; 468 int ret;
463 469
@@ -480,7 +486,7 @@ GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
480 */ 486 */
481ssize_t 487ssize_t
482GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle * 488GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle *
483 desc) 489 desc)
484{ 490{
485 int error; 491 int error;
486 492
@@ -513,8 +519,9 @@ GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle *
513 */ 519 */
514ssize_t 520ssize_t
515GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle * desc, 521GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle * desc,
516 void *buffer, size_t length, 522 void *buffer, size_t length,
517 struct sockaddr * src_addr, socklen_t * addrlen) 523 struct sockaddr * src_addr,
524 socklen_t * addrlen)
518{ 525{
519 int ret; 526 int ret;
520 int flags; 527 int flags;
@@ -542,7 +549,7 @@ GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle * desc,
542 */ 549 */
543ssize_t 550ssize_t
544GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc, 551GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc,
545 void *buffer, size_t length) 552 void *buffer, size_t length)
546{ 553{
547 int ret; 554 int ret;
548 int flags; 555 int flags;
@@ -571,7 +578,7 @@ GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc,
571 */ 578 */
572ssize_t 579ssize_t
573GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc, 580GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc,
574 const void *buffer, size_t length) 581 const void *buffer, size_t length)
575{ 582{
576 int ret; 583 int ret;
577 int flags; 584 int flags;
@@ -610,9 +617,9 @@ GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc,
610 */ 617 */
611ssize_t 618ssize_t
612GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle * desc, 619GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle * desc,
613 const void *message, size_t length, 620 const void *message, size_t length,
614 const struct sockaddr * dest_addr, 621 const struct sockaddr * dest_addr,
615 socklen_t dest_len) 622 socklen_t dest_len)
616{ 623{
617 int ret; 624 int ret;
618 int flags; 625 int flags;
@@ -645,8 +652,8 @@ GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle * desc,
645 */ 652 */
646int 653int
647GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd, int level, 654GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd, int level,
648 int option_name, const void *option_value, 655 int option_name, const void *option_value,
649 socklen_t option_len) 656 socklen_t option_len)
650{ 657{
651 int ret; 658 int ret;
652 659
@@ -678,36 +685,36 @@ GNUNET_NETWORK_socket_create (int domain, int type, int protocol)
678 ret->af = domain; 685 ret->af = domain;
679 ret->fd = socket (domain, type, protocol); 686 ret->fd = socket (domain, type, protocol);
680 if (INVALID_SOCKET == ret->fd) 687 if (INVALID_SOCKET == ret->fd)
681 { 688 {
682#ifdef MINGW 689#ifdef MINGW
683 SetErrnoFromWinsockError (WSAGetLastError ()); 690 SetErrnoFromWinsockError (WSAGetLastError ());
684#endif 691#endif
685 GNUNET_free (ret); 692 GNUNET_free (ret);
686 return NULL; 693 return NULL;
687 } 694 }
688 695
689#ifndef MINGW 696#ifndef MINGW
690 if (ret->fd >= FD_SETSIZE) 697 if (ret->fd >= FD_SETSIZE)
691 { 698 {
692 GNUNET_break (0 == close (ret->fd)); 699 GNUNET_break (0 == close (ret->fd));
693 GNUNET_free (ret); 700 GNUNET_free (ret);
694 errno = EMFILE; 701 errno = EMFILE;
695 return NULL; 702 return NULL;
696 } 703 }
697 704
698#endif 705#endif
699 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO)) 706 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
700 { 707 {
701 /* we might want to treat this one as fatal... */ 708 /* we might want to treat this one as fatal... */
702 GNUNET_break (0); 709 GNUNET_break (0);
703 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret)); 710 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
704 return NULL; 711 return NULL;
705 } 712 }
706 713
707#ifndef MINGW 714#ifndef MINGW
708 if (GNUNET_OK != socket_set_inheritable (ret)) 715 if (GNUNET_OK != socket_set_inheritable (ret))
709 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 716 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
710 "socket_set_inheritable"); 717 "socket_set_inheritable");
711#endif 718#endif
712#ifdef DARWIN 719#ifdef DARWIN
713 socket_set_nosigpipe (ret); 720 socket_set_nosigpipe (ret);
@@ -716,7 +723,7 @@ GNUNET_NETWORK_socket_create (int domain, int type, int protocol)
716#ifdef AF_UNIX 723#ifdef AF_UNIX
717 && (domain != AF_UNIX) 724 && (domain != AF_UNIX)
718#endif 725#endif
719 ) 726 )
720 socket_set_nodelay (ret); 727 socket_set_nodelay (ret);
721 return ret; 728 return ret;
722} 729}
@@ -761,24 +768,24 @@ GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc)
761 if (0 != 768 if (0 !=
762 (ret = 769 (ret =
763 setsockopt (desc->fd, SOL_SOCKET, SO_SNDBUF, (char *) &value, 770 setsockopt (desc->fd, SOL_SOCKET, SO_SNDBUF, (char *) &value,
764 sizeof (value)))) 771 sizeof (value))))
765 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); 772 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
766 if (0 != 773 if (0 !=
767 (ret = 774 (ret =
768 setsockopt (desc->fd, SOL_SOCKET, SO_RCVBUF, (char *) &value, 775 setsockopt (desc->fd, SOL_SOCKET, SO_RCVBUF, (char *) &value,
769 sizeof (value)))) 776 sizeof (value))))
770 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); 777 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
771#elif LINUX 778#elif LINUX
772 int value = 0; 779 int value = 0;
773 780
774 if (0 != 781 if (0 !=
775 (ret = 782 (ret =
776 setsockopt (desc->fd, SOL_SOCKET, SO_SNDBUF, &value, sizeof (value)))) 783 setsockopt (desc->fd, SOL_SOCKET, SO_SNDBUF, &value, sizeof (value))))
777 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); 784 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
778 if (0 != 785 if (0 !=
779 (ret = 786 (ret =
780 setsockopt (desc->fd, SOL_SOCKET, SO_RCVBUF, &value, sizeof (value)))) 787 setsockopt (desc->fd, SOL_SOCKET, SO_RCVBUF, &value, sizeof (value))))
781 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); 788 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
782#endif 789#endif
783 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR; 790 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
784} 791}
@@ -805,7 +812,7 @@ GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
805 */ 812 */
806void 813void
807GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds, 814GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
808 const struct GNUNET_NETWORK_Handle *desc) 815 const struct GNUNET_NETWORK_Handle *desc)
809{ 816{
810 FD_SET (desc->fd, &fds->sds); 817 FD_SET (desc->fd, &fds->sds);
811 if (desc->fd + 1 > fds->nsds) 818 if (desc->fd + 1 > fds->nsds)
@@ -821,7 +828,7 @@ GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
821 */ 828 */
822int 829int
823GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds, 830GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
824 const struct GNUNET_NETWORK_Handle *desc) 831 const struct GNUNET_NETWORK_Handle *desc)
825{ 832{
826 return FD_ISSET (desc->fd, &fds->sds); 833 return FD_ISSET (desc->fd, &fds->sds);
827} 834}
@@ -834,18 +841,18 @@ GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
834 */ 841 */
835void 842void
836GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst, 843GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
837 const struct GNUNET_NETWORK_FDSet *src) 844 const struct GNUNET_NETWORK_FDSet *src)
838{ 845{
839 int nfds; 846 int nfds;
840 847
841 for (nfds = src->nsds; nfds > 0; nfds--) 848 for (nfds = src->nsds; nfds > 0; nfds--)
842 if (FD_ISSET (nfds, &src->sds)) 849 if (FD_ISSET (nfds, &src->sds))
843 850
844 { 851 {
845 FD_SET (nfds, &dst->sds); 852 FD_SET (nfds, &dst->sds);
846 if (nfds + 1 > dst->nsds) 853 if (nfds + 1 > dst->nsds)
847 dst->nsds = nfds + 1; 854 dst->nsds = nfds + 1;
848 } 855 }
849#ifdef MINGW 856#ifdef MINGW
850 GNUNET_CONTAINER_slist_append (dst->handles, src->handles); 857 GNUNET_CONTAINER_slist_append (dst->handles, src->handles);
851#endif 858#endif
@@ -860,7 +867,7 @@ GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
860 */ 867 */
861void 868void
862GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to, 869GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
863 const struct GNUNET_NETWORK_FDSet *from) 870 const struct GNUNET_NETWORK_FDSet *from)
864{ 871{
865 FD_COPY (&from->sds, &to->sds); 872 FD_COPY (&from->sds, &to->sds);
866 to->nsds = from->nsds; 873 to->nsds = from->nsds;
@@ -894,7 +901,7 @@ GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc)
894 */ 901 */
895void 902void
896GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to, 903GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
897 const fd_set * from, int nfds) 904 const fd_set * from, int nfds)
898{ 905{
899 FD_COPY (from, &to->sds); 906 FD_COPY (from, &to->sds);
900 to->nsds = nfds; 907 to->nsds = nfds;
@@ -925,7 +932,7 @@ GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to, int nfd)
925 */ 932 */
926int 933int
927GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to, 934GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
928 int nfd) 935 int nfd)
929{ 936{
930 if ((nfd == -1) || (to == NULL)) 937 if ((nfd == -1) || (to == NULL))
931 return GNUNET_NO; 938 return GNUNET_NO;
@@ -940,12 +947,12 @@ GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
940 */ 947 */
941void 948void
942GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds, 949GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
943 const struct GNUNET_DISK_FileHandle *h) 950 const struct GNUNET_DISK_FileHandle *h)
944{ 951{
945#ifdef MINGW 952#ifdef MINGW
946 GNUNET_CONTAINER_slist_add (fds->handles, 953 GNUNET_CONTAINER_slist_add (fds->handles,
947 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, h, 954 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, h,
948 sizeof (struct GNUNET_DISK_FileHandle)); 955 sizeof (struct GNUNET_DISK_FileHandle));
949 956
950#else 957#else
951 int fd; 958 int fd;
@@ -967,13 +974,13 @@ GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
967 */ 974 */
968int 975int
969GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds, 976GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
970 const struct GNUNET_DISK_FileHandle *h) 977 const struct GNUNET_DISK_FileHandle *h)
971{ 978{
972 979
973#ifdef MINGW 980#ifdef MINGW
974 return GNUNET_CONTAINER_slist_contains (fds->handles, h, 981 return GNUNET_CONTAINER_slist_contains (fds->handles, h,
975 sizeof (struct 982 sizeof (struct
976 GNUNET_DISK_FileHandle)); 983 GNUNET_DISK_FileHandle));
977#else 984#else
978 return FD_ISSET (h->fd, &fds->sds); 985 return FD_ISSET (h->fd, &fds->sds);
979#endif 986#endif
@@ -988,7 +995,7 @@ GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
988 */ 995 */
989int 996int
990GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1, 997GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
991 const struct GNUNET_NETWORK_FDSet *fds2) 998 const struct GNUNET_NETWORK_FDSet *fds2)
992{ 999{
993#ifndef MINGW 1000#ifndef MINGW
994 int nfds; 1001 int nfds;
@@ -997,11 +1004,11 @@ GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
997 if (nfds > fds2->nsds) 1004 if (nfds > fds2->nsds)
998 nfds = fds2->nsds; 1005 nfds = fds2->nsds;
999 while (nfds > 0) 1006 while (nfds > 0)
1000 { 1007 {
1001 nfds--; 1008 nfds--;
1002 if (FD_ISSET (nfds, &fds1->sds) && FD_ISSET (nfds, &fds2->sds)) 1009 if (FD_ISSET (nfds, &fds1->sds) && FD_ISSET (nfds, &fds2->sds))
1003 return GNUNET_YES; 1010 return GNUNET_YES;
1004 } 1011 }
1005#else 1012#else
1006 struct GNUNET_CONTAINER_SList_Iterator *it; 1013 struct GNUNET_CONTAINER_SList_Iterator *it;
1007 struct GNUNET_DISK_FileHandle *h; 1014 struct GNUNET_DISK_FileHandle *h;
@@ -1012,45 +1019,48 @@ GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
1012 * inside of fd_set; also the O(n^2) is really bad... */ 1019 * inside of fd_set; also the O(n^2) is really bad... */
1013 1020
1014 for (i = 0; i < fds1->sds.fd_count; i++) 1021 for (i = 0; i < fds1->sds.fd_count; i++)
1015 {
1016 for (j = 0; j < fds2->sds.fd_count; j++)
1017 { 1022 {
1018 if (fds1->sds.fd_array[i] == fds2->sds.fd_array[j]) 1023 for (j = 0; j < fds2->sds.fd_count; j++)
1019 return GNUNET_YES; 1024 {
1025 if (fds1->sds.fd_array[i] == fds2->sds.fd_array[j])
1026 return GNUNET_YES;
1027 }
1020 } 1028 }
1021 }
1022 it = GNUNET_CONTAINER_slist_begin (fds1->handles); 1029 it = GNUNET_CONTAINER_slist_begin (fds1->handles);
1023 while (GNUNET_CONTAINER_slist_end (it) != GNUNET_YES) 1030 while (GNUNET_CONTAINER_slist_end (it) != GNUNET_YES)
1024 { 1031 {
1025#if DEBUG_NETWORK 1032#if DEBUG_NETWORK
1026 struct GNUNET_CONTAINER_SList_Iterator *t; 1033 struct GNUNET_CONTAINER_SList_Iterator *t;
1027#endif 1034#endif
1028 h = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (it, NULL); 1035 h =
1036 (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (it,
1037 NULL);
1029#if DEBUG_NETWORK 1038#if DEBUG_NETWORK
1030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1039 LOG (GNUNET_ERROR_TYPE_DEBUG,
1031 "Checking that FD 0x%x is in another set:\n", h->h); 1040 "Checking that FD 0x%x is in another set:\n", h->h);
1032 for (t = GNUNET_CONTAINER_slist_begin (fds2->handles); 1041 for (t = GNUNET_CONTAINER_slist_begin (fds2->handles);
1033 GNUNET_CONTAINER_slist_end (t) != GNUNET_YES; 1042 GNUNET_CONTAINER_slist_end (t) != GNUNET_YES;
1034 GNUNET_CONTAINER_slist_next (t)) 1043 GNUNET_CONTAINER_slist_next (t))
1035 { 1044 {
1036 struct GNUNET_DISK_FileHandle *fh; 1045 struct GNUNET_DISK_FileHandle *fh;
1037 1046
1038 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (t, 1047 fh =
1039 NULL); 1048 (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (t,
1040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "0x%x\n", fh->h); 1049 NULL);
1041 } 1050 LOG (GNUNET_ERROR_TYPE_DEBUG, "0x%x\n", fh->h);
1042#endif 1051 }
1043 if (GNUNET_CONTAINER_slist_contains 1052#endif
1044 (fds2->handles, h, sizeof (struct GNUNET_DISK_FileHandle))) 1053 if (GNUNET_CONTAINER_slist_contains
1045 { 1054 (fds2->handles, h, sizeof (struct GNUNET_DISK_FileHandle)))
1055 {
1046#if DEBUG_NETWORK 1056#if DEBUG_NETWORK
1047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Match!\n"); 1057 LOG (GNUNET_ERROR_TYPE_DEBUG, "Match!\n");
1048#endif 1058#endif
1049 GNUNET_CONTAINER_slist_iter_destroy (it); 1059 GNUNET_CONTAINER_slist_iter_destroy (it);
1050 return GNUNET_YES; 1060 return GNUNET_YES;
1061 }
1062 GNUNET_CONTAINER_slist_next (it);
1051 } 1063 }
1052 GNUNET_CONTAINER_slist_next (it);
1053 }
1054 GNUNET_CONTAINER_slist_iter_destroy (it); 1064 GNUNET_CONTAINER_slist_iter_destroy (it);
1055#endif 1065#endif
1056 return GNUNET_NO; 1066 return GNUNET_NO;
@@ -1098,9 +1108,9 @@ GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
1098 */ 1108 */
1099int 1109int
1100GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds, 1110GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1101 struct GNUNET_NETWORK_FDSet *wfds, 1111 struct GNUNET_NETWORK_FDSet *wfds,
1102 struct GNUNET_NETWORK_FDSet *efds, 1112 struct GNUNET_NETWORK_FDSet *efds,
1103 const struct GNUNET_TIME_Relative timeout) 1113 const struct GNUNET_TIME_Relative timeout)
1104{ 1114{
1105 int nfds = 0; 1115 int nfds = 0;
1106 1116
@@ -1130,7 +1140,8 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1130 DWORD newretcode = 0; 1140 DWORD newretcode = 0;
1131 int returnedpos = 0; 1141 int returnedpos = 0;
1132 1142
1133 struct GNUNET_CONTAINER_SList *handles_read, *handles_write, *handles_except; 1143 struct GNUNET_CONTAINER_SList *handles_read, *handles_write,
1144 *handles_except;
1134 1145
1135 fd_set aread, awrite, aexcept; 1146 fd_set aread, awrite, aexcept;
1136 1147
@@ -1144,67 +1155,68 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1144 struct timeval tv; 1155 struct timeval tv;
1145#endif 1156#endif
1146 if (NULL != rfds) 1157 if (NULL != rfds)
1147 { 1158 {
1148 nfds = rfds->nsds; 1159 nfds = rfds->nsds;
1149#ifdef MINGW 1160#ifdef MINGW
1150 handles += read_handles = GNUNET_CONTAINER_slist_count (rfds->handles); 1161 handles += read_handles = GNUNET_CONTAINER_slist_count (rfds->handles);
1151#if DEBUG_NETWORK 1162#if DEBUG_NETWORK
1152 {
1153 struct GNUNET_CONTAINER_SList_Iterator *t;
1154
1155 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1156 GNUNET_CONTAINER_slist_end (t) != GNUNET_YES;
1157 GNUNET_CONTAINER_slist_next (t))
1158 { 1163 {
1159 struct GNUNET_DISK_FileHandle *fh; 1164 struct GNUNET_CONTAINER_SList_Iterator *t;
1160 1165
1161 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (t, 1166 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1162 NULL); 1167 GNUNET_CONTAINER_slist_end (t) != GNUNET_YES;
1163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x (0x%x) is SET in rfds\n", 1168 GNUNET_CONTAINER_slist_next (t))
1164 fh->h, fh); 1169 {
1170 struct GNUNET_DISK_FileHandle *fh;
1171
1172 fh =
1173 (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (t,
1174 NULL);
1175 LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x (0x%x) is SET in rfds\n",
1176 fh->h, fh);
1177 }
1165 } 1178 }
1166 }
1167#endif 1179#endif
1168#endif 1180#endif
1169 } 1181 }
1170 if (NULL != wfds) 1182 if (NULL != wfds)
1171 { 1183 {
1172 nfds = GNUNET_MAX (nfds, wfds->nsds); 1184 nfds = GNUNET_MAX (nfds, wfds->nsds);
1173#ifdef MINGW 1185#ifdef MINGW
1174 handles += write_handles = GNUNET_CONTAINER_slist_count (wfds->handles); 1186 handles += write_handles = GNUNET_CONTAINER_slist_count (wfds->handles);
1175#endif 1187#endif
1176 } 1188 }
1177 if (NULL != efds) 1189 if (NULL != efds)
1178 { 1190 {
1179 nfds = GNUNET_MAX (nfds, efds->nsds); 1191 nfds = GNUNET_MAX (nfds, efds->nsds);
1180#ifdef MINGW 1192#ifdef MINGW
1181 handles += ex_handles = GNUNET_CONTAINER_slist_count (efds->handles); 1193 handles += ex_handles = GNUNET_CONTAINER_slist_count (efds->handles);
1182#endif 1194#endif
1183 } 1195 }
1184 1196
1185 if ((nfds == 0) && 1197 if ((nfds == 0) &&
1186 (timeout.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value) 1198 (timeout.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1187#ifdef MINGW 1199#ifdef MINGW
1188 && handles == 0 1200 && handles == 0
1189#endif 1201#endif
1190 ) 1202 )
1191 { 1203 {
1192 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1204 LOG (GNUNET_ERROR_TYPE_ERROR,
1193 _ 1205 _
1194 ("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"), 1206 ("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1195 "select"); 1207 "select");
1196 GNUNET_break (0); 1208 GNUNET_break (0);
1197 } 1209 }
1198#ifndef MINGW 1210#ifndef MINGW
1199 tv.tv_sec = timeout.rel_value / GNUNET_TIME_UNIT_SECONDS.rel_value; 1211 tv.tv_sec = timeout.rel_value / GNUNET_TIME_UNIT_SECONDS.rel_value;
1200 tv.tv_usec = 1212 tv.tv_usec =
1201 1000 * (timeout.rel_value - 1213 1000 * (timeout.rel_value -
1202 (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value)); 1214 (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value));
1203 return select (nfds, (rfds != NULL) ? &rfds->sds : NULL, 1215 return select (nfds, (rfds != NULL) ? &rfds->sds : NULL,
1204 (wfds != NULL) ? &wfds->sds : NULL, 1216 (wfds != NULL) ? &wfds->sds : NULL,
1205 (efds != NULL) ? &efds->sds : NULL, 1217 (efds != NULL) ? &efds->sds : NULL,
1206 (timeout.rel_value == 1218 (timeout.rel_value ==
1207 GNUNET_TIME_UNIT_FOREVER_REL.rel_value) ? NULL : &tv); 1219 GNUNET_TIME_UNIT_FOREVER_REL.rel_value) ? NULL : &tv);
1208 1220
1209#else 1221#else
1210#define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set)) 1222#define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
@@ -1215,10 +1227,10 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1215 ms_total = timeout.rel_value / GNUNET_TIME_UNIT_MILLISECONDS.rel_value; 1227 ms_total = timeout.rel_value / GNUNET_TIME_UNIT_MILLISECONDS.rel_value;
1216 /* select() may be used as a portable way to sleep */ 1228 /* select() may be used as a portable way to sleep */
1217 if (!(rfds || wfds || efds)) 1229 if (!(rfds || wfds || efds))
1218 { 1230 {
1219 Sleep (ms_total); 1231 Sleep (ms_total);
1220 return 0; 1232 return 0;
1221 } 1233 }
1222 1234
1223 /* Events for sockets */ 1235 /* Events for sockets */
1224 if (!hEventRead) 1236 if (!hEventRead)
@@ -1254,212 +1266,217 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1254 FD_ZERO (&bexcept); 1266 FD_ZERO (&bexcept);
1255#endif 1267#endif
1256 if (rfds) 1268 if (rfds)
1257 { 1269 {
1258 FD_COPY (&rfds->sds, &aread); 1270 FD_COPY (&rfds->sds, &aread);
1259#if DEBUG_NETWORK 1271#if DEBUG_NETWORK
1260 FD_COPY (&rfds->sds, &bread); 1272 FD_COPY (&rfds->sds, &bread);
1261#endif 1273#endif
1262 } 1274 }
1263 if (wfds) 1275 if (wfds)
1264 { 1276 {
1265 FD_COPY (&wfds->sds, &awrite); 1277 FD_COPY (&wfds->sds, &awrite);
1266#if DEBUG_NETWORK 1278#if DEBUG_NETWORK
1267 FD_COPY (&wfds->sds, &bwrite); 1279 FD_COPY (&wfds->sds, &bwrite);
1268#endif 1280#endif
1269 } 1281 }
1270 if (efds) 1282 if (efds)
1271 { 1283 {
1272 FD_COPY (&efds->sds, &aexcept); 1284 FD_COPY (&efds->sds, &aexcept);
1273#if DEBUG_NETWORK 1285#if DEBUG_NETWORK
1274 FD_COPY (&efds->sds, &bexcept); 1286 FD_COPY (&efds->sds, &bexcept);
1275#endif 1287#endif
1276 } 1288 }
1277 /* We will first Add the PIPES to the events */ 1289 /* We will first Add the PIPES to the events */
1278 /* Read Pipes */ 1290 /* Read Pipes */
1279 if (rfds && read_handles) 1291 if (rfds && read_handles)
1280 {
1281 struct GNUNET_CONTAINER_SList_Iterator *i;
1282
1283 for (i = GNUNET_CONTAINER_slist_begin (rfds->handles);
1284 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
1285 GNUNET_CONTAINER_slist_next (i))
1286 { 1292 {
1287 struct GNUNET_DISK_FileHandle *fh; 1293 struct GNUNET_CONTAINER_SList_Iterator *i;
1288 1294
1289 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (i, 1295 for (i = GNUNET_CONTAINER_slist_begin (rfds->handles);
1290 NULL); 1296 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
1291 if (fh->type == GNUNET_PIPE) 1297 GNUNET_CONTAINER_slist_next (i))
1292 { 1298 {
1293 /* Read zero bytes to check the status of the pipe */ 1299 struct GNUNET_DISK_FileHandle *fh;
1300
1301 fh =
1302 (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (i,
1303 NULL);
1304 if (fh->type == GNUNET_PIPE)
1305 {
1306 /* Read zero bytes to check the status of the pipe */
1294#if DEBUG_NETWORK 1307#if DEBUG_NETWORK
1295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1308 LOG (GNUNET_ERROR_TYPE_DEBUG,
1296 "Reading 0 bytes from the pipe 0x%x\n", fh->h); 1309 "Reading 0 bytes from the pipe 0x%x\n", fh->h);
1297#endif 1310#endif
1298 if (!ReadFile (fh->h, NULL, 0, NULL, fh->oOverlapRead)) 1311 if (!ReadFile (fh->h, NULL, 0, NULL, fh->oOverlapRead))
1299 { 1312 {
1300 DWORD error_code = GetLastError (); 1313 DWORD error_code = GetLastError ();
1301 1314
1302 if (error_code == ERROR_IO_PENDING) 1315 if (error_code == ERROR_IO_PENDING)
1303 { 1316 {
1304#if DEBUG_NETWORK 1317#if DEBUG_NETWORK
1305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1318 LOG (GNUNET_ERROR_TYPE_DEBUG,
1306 "Adding the pipe's 0x%x overlapped event to the array as %d\n", 1319 "Adding the pipe's 0x%x overlapped event to the array as %d\n",
1307 fh->h, nhandles); 1320 fh->h, nhandles);
1308#endif 1321#endif
1309 handle_array[nhandles++] = fh->oOverlapRead->hEvent; 1322 handle_array[nhandles++] = fh->oOverlapRead->hEvent;
1310 readArray[readPipes++] = fh; 1323 readArray[readPipes++] = fh;
1311 } 1324 }
1312 /* 1325 /*
1313 * else 1326 * else
1314 * { 1327 * {
1315 * SetErrnoFromWinError (error_code); 1328 * SetErrnoFromWinError (error_code);
1316 * } 1329 * }
1317 */ 1330 */
1318 } 1331 }
1319 else 1332 else
1320 { 1333 {
1321#if DEBUG_NETWORK 1334#if DEBUG_NETWORK
1322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1335 LOG (GNUNET_ERROR_TYPE_DEBUG,
1323 "Adding the read ready event to the array as %d\n", 1336 "Adding the read ready event to the array as %d\n",
1324 nhandles); 1337 nhandles);
1325#endif 1338#endif
1326 handle_array[nhandles++] = hEventReadReady; 1339 handle_array[nhandles++] = hEventReadReady;
1327 readArray[readPipes++] = fh; 1340 readArray[readPipes++] = fh;
1328 } 1341 }
1329 } 1342 }
1330 else 1343 else
1331 { 1344 {
1332 GNUNET_CONTAINER_slist_add (handles_read, 1345 GNUNET_CONTAINER_slist_add (handles_read,
1333 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, 1346 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1334 fh, sizeof (struct GNUNET_DISK_FileHandle)); 1347 fh,
1335 } 1348 sizeof (struct
1349 GNUNET_DISK_FileHandle));
1350 }
1351 }
1352 GNUNET_CONTAINER_slist_iter_destroy (i);
1336 } 1353 }
1337 GNUNET_CONTAINER_slist_iter_destroy (i);
1338 }
1339 if (wfds && write_handles) 1354 if (wfds && write_handles)
1340 { 1355 {
1341#if DEBUG_NETWORK 1356#if DEBUG_NETWORK
1342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1357 LOG (GNUNET_ERROR_TYPE_DEBUG,
1343 "Adding the write ready event to the array as %d\n", nhandles); 1358 "Adding the write ready event to the array as %d\n", nhandles);
1344#endif 1359#endif
1345 handle_array[nhandles++] = hEventPipeWrite; 1360 handle_array[nhandles++] = hEventPipeWrite;
1346 writePipePos = nhandles; 1361 writePipePos = nhandles;
1347 } 1362 }
1348 if (efds && ex_handles) 1363 if (efds && ex_handles)
1349 {
1350 struct GNUNET_CONTAINER_SList_Iterator *i;
1351
1352 for (i = GNUNET_CONTAINER_slist_begin (efds->handles);
1353 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
1354 GNUNET_CONTAINER_slist_next (i))
1355 { 1364 {
1356 struct GNUNET_DISK_FileHandle *fh; 1365 struct GNUNET_CONTAINER_SList_Iterator *i;
1357 DWORD dwBytes; 1366
1358 1367 for (i = GNUNET_CONTAINER_slist_begin (efds->handles);
1359 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (i, 1368 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
1360 NULL); 1369 GNUNET_CONTAINER_slist_next (i))
1361 if (fh->type == GNUNET_PIPE) 1370 {
1362 { 1371 struct GNUNET_DISK_FileHandle *fh;
1363 if (!PeekNamedPipe (fh->h, NULL, 0, NULL, &dwBytes, NULL)) 1372 DWORD dwBytes;
1364 { 1373
1365 GNUNET_CONTAINER_slist_add (handles_except, 1374 fh =
1366 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, 1375 (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (i,
1367 fh, 1376 NULL);
1368 sizeof (struct GNUNET_DISK_FileHandle)); 1377 if (fh->type == GNUNET_PIPE)
1369 newretcode++; 1378 {
1370 } 1379 if (!PeekNamedPipe (fh->h, NULL, 0, NULL, &dwBytes, NULL))
1371 } 1380 {
1381 GNUNET_CONTAINER_slist_add (handles_except,
1382 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1383 fh,
1384 sizeof (struct
1385 GNUNET_DISK_FileHandle));
1386 newretcode++;
1387 }
1388 }
1389 }
1390 GNUNET_CONTAINER_slist_iter_destroy (i);
1372 } 1391 }
1373 GNUNET_CONTAINER_slist_iter_destroy (i);
1374 }
1375 if (nfds > 0) 1392 if (nfds > 0)
1376 {
1377 if (rfds)
1378 { 1393 {
1394 if (rfds)
1395 {
1379#if DEBUG_NETWORK 1396#if DEBUG_NETWORK
1380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1397 LOG (GNUNET_ERROR_TYPE_DEBUG,
1381 "Adding the socket read event to the array as %d\n", 1398 "Adding the socket read event to the array as %d\n", nhandles);
1382 nhandles); 1399#endif
1383#endif 1400 handle_array[nhandles++] = hEventRead;
1384 handle_array[nhandles++] = hEventRead; 1401 nSockEvents++;
1385 nSockEvents++; 1402 for (i = 0; i < rfds->sds.fd_count; i++)
1386 for (i = 0; i < rfds->sds.fd_count; i++) 1403 {
1387 { 1404 WSAEventSelect (rfds->sds.fd_array[i], hEventRead,
1388 WSAEventSelect (rfds->sds.fd_array[i], hEventRead, 1405 FD_ACCEPT | FD_READ | FD_CLOSE);
1389 FD_ACCEPT | FD_READ | FD_CLOSE); 1406 nsock++;
1390 nsock++; 1407 }
1391 } 1408 }
1392 } 1409 if (wfds)
1393 if (wfds) 1410 {
1394 { 1411 int wakeup = 0;
1395 int wakeup = 0;
1396 1412
1397#if DEBUG_NETWORK 1413#if DEBUG_NETWORK
1398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1414 LOG (GNUNET_ERROR_TYPE_DEBUG,
1399 "Adding the socket write event to the array as %d\n", 1415 "Adding the socket write event to the array as %d\n",
1400 nhandles); 1416 nhandles);
1401#endif 1417#endif
1402 handle_array[nhandles++] = hEventWrite; 1418 handle_array[nhandles++] = hEventWrite;
1403 nSockEvents++; 1419 nSockEvents++;
1404 for (i = 0; i < wfds->sds.fd_count; i++) 1420 for (i = 0; i < wfds->sds.fd_count; i++)
1405 { 1421 {
1406 DWORD error; 1422 DWORD error;
1407 int status; 1423 int status;
1408 1424
1409 status = send (wfds->sds.fd_array[i], NULL, 0, 0); 1425 status = send (wfds->sds.fd_array[i], NULL, 0, 0);
1410 error = GetLastError (); 1426 error = GetLastError ();
1411#if DEBUG_NETWORK 1427#if DEBUG_NETWORK
1412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1428 LOG (GNUNET_ERROR_TYPE_DEBUG,
1413 "pre-send to the socket %d returned %d (%u)\n", i, status, 1429 "pre-send to the socket %d returned %d (%u)\n", i, status,
1414 error); 1430 error);
1415#endif 1431#endif
1416 if (status == 0 || (error != WSAEWOULDBLOCK && error != WSAENOTCONN)) 1432 if (status == 0
1417 wakeup = 1; 1433 || (error != WSAEWOULDBLOCK && error != WSAENOTCONN))
1418 WSAEventSelect (wfds->sds.fd_array[i], hEventWrite, 1434 wakeup = 1;
1419 FD_WRITE | FD_CONNECT | FD_CLOSE); 1435 WSAEventSelect (wfds->sds.fd_array[i], hEventWrite,
1420 nsock++; 1436 FD_WRITE | FD_CONNECT | FD_CLOSE);
1421 } 1437 nsock++;
1422 if (wakeup) 1438 }
1423 SetEvent (hEventWrite); 1439 if (wakeup)
1424 } 1440 SetEvent (hEventWrite);
1425 if (efds) 1441 }
1426 { 1442 if (efds)
1443 {
1427#if DEBUG_NETWORK 1444#if DEBUG_NETWORK
1428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1445 LOG (GNUNET_ERROR_TYPE_DEBUG,
1429 "Adding the socket error event to the array as %d\n", 1446 "Adding the socket error event to the array as %d\n",
1430 nhandles); 1447 nhandles);
1431#endif 1448#endif
1432 handle_array[nhandles++] = hEventException; 1449 handle_array[nhandles++] = hEventException;
1433 nSockEvents++; 1450 nSockEvents++;
1434 for (i = 0; i < efds->sds.fd_count; i++) 1451 for (i = 0; i < efds->sds.fd_count; i++)
1435 { 1452 {
1436 WSAEventSelect (efds->sds.fd_array[i], hEventException, 1453 WSAEventSelect (efds->sds.fd_array[i], hEventException,
1437 FD_OOB | FD_CLOSE); 1454 FD_OOB | FD_CLOSE);
1438 nsock++; 1455 nsock++;
1439 } 1456 }
1457 }
1440 } 1458 }
1441 }
1442 1459
1443 handle_array[nhandles] = NULL; 1460 handle_array[nhandles] = NULL;
1444 1461
1445#if DEBUG_NETWORK 1462#if DEBUG_NETWORK
1446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Number nfds : %d\n", nfds); 1463 LOG (GNUNET_ERROR_TYPE_DEBUG, "Number nfds : %d\n", nfds);
1447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Number of handles : %d\n", nhandles); 1464 LOG (GNUNET_ERROR_TYPE_DEBUG, "Number of handles : %d\n", nhandles);
1448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "retcode : %d\n", newretcode); 1465 LOG (GNUNET_ERROR_TYPE_DEBUG, "retcode : %d\n", newretcode);
1449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Will wait : %d\n", ms_total); 1466 LOG (GNUNET_ERROR_TYPE_DEBUG, "Will wait : %d\n", ms_total);
1450#endif 1467#endif
1451 1468
1452 if (nhandles) 1469 if (nhandles)
1453 returncode = 1470 returncode =
1454 WaitForMultipleObjects (nhandles, handle_array, FALSE, ms_total); 1471 WaitForMultipleObjects (nhandles, handle_array, FALSE, ms_total);
1455#if DEBUG_NETWORK 1472#if DEBUG_NETWORK
1456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WaitForMultipleObjects Returned : %d\n", 1473 LOG (GNUNET_ERROR_TYPE_DEBUG, "WaitForMultipleObjects Returned : %d\n",
1457 returncode); 1474 returncode);
1458#endif 1475#endif
1459 1476
1460 returnedpos = returncode - WAIT_OBJECT_0; 1477 returnedpos = returncode - WAIT_OBJECT_0;
1461#if DEBUG_NETWORK 1478#if DEBUG_NETWORK
1462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return pos is : %d\n", returnedpos); 1479 LOG (GNUNET_ERROR_TYPE_DEBUG, "return pos is : %d\n", returnedpos);
1463#endif 1480#endif
1464 1481
1465 /* FIXME: THIS LINE IS WRONG !! We should add to handles only handles that fired the events, not all ! */ 1482 /* FIXME: THIS LINE IS WRONG !! We should add to handles only handles that fired the events, not all ! */
@@ -1468,237 +1485,241 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1468 * GNUNET_CONTAINER_slist_append (handles_read, rfds->handles); 1485 * GNUNET_CONTAINER_slist_append (handles_read, rfds->handles);
1469 */ 1486 */
1470 if (nhandles && (returnedpos < nhandles)) 1487 if (nhandles && (returnedpos < nhandles))
1471 {
1472 DWORD waitstatus;
1473
1474 /* Do the select */
1475 if (nfds)
1476 { 1488 {
1477 struct timeval tvslice; 1489 DWORD waitstatus;
1478 1490
1479 tvslice.tv_sec = 0; 1491 /* Do the select */
1480 tvslice.tv_usec = 10; 1492 if (nfds)
1481 retcode = select (nfds, &aread, &awrite, &aexcept, &tvslice); 1493 {
1482 if (retcode == -1) 1494 struct timeval tvslice;
1483 retcode = 0; 1495
1496 tvslice.tv_sec = 0;
1497 tvslice.tv_usec = 10;
1498 retcode = select (nfds, &aread, &awrite, &aexcept, &tvslice);
1499 if (retcode == -1)
1500 retcode = 0;
1484#if DEBUG_NETWORK 1501#if DEBUG_NETWORK
1485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Select retcode : %d\n", retcode); 1502 LOG (GNUNET_ERROR_TYPE_DEBUG, "Select retcode : %d\n", retcode);
1486#endif 1503#endif
1487 } 1504 }
1488 /* FIXME: <= writePipePos? Really? */ 1505 /* FIXME: <= writePipePos? Really? */
1489 if ((writePipePos != -1) && (returnedpos <= writePipePos)) 1506 if ((writePipePos != -1) && (returnedpos <= writePipePos))
1490 { 1507 {
1491 GNUNET_CONTAINER_slist_append (handles_write, wfds->handles); 1508 GNUNET_CONTAINER_slist_append (handles_write, wfds->handles);
1492 retcode += write_handles; 1509 retcode += write_handles;
1493#if DEBUG_NETWORK 1510#if DEBUG_NETWORK
1494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added write pipe\n"); 1511 LOG (GNUNET_ERROR_TYPE_DEBUG, "Added write pipe\n");
1495#endif 1512#endif
1496 } 1513 }
1497#if DEBUG_NETWORK 1514#if DEBUG_NETWORK
1498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ReadPipes is : %d\n", readPipes); 1515 LOG (GNUNET_ERROR_TYPE_DEBUG, "ReadPipes is : %d\n", readPipes);
1499#endif 1516#endif
1500 /* We have some pipes ready for read. */ 1517 /* We have some pipes ready for read. */
1501 /* FIXME: it is supposed to work !! Only choose the Pipes who fired the event, but it is not working */ 1518 /* FIXME: it is supposed to work !! Only choose the Pipes who fired the event, but it is not working */
1502 1519
1503 if (returnedpos < readPipes) 1520 if (returnedpos < readPipes)
1504 { 1521 {
1505 /* 1522 /*
1506 * for (i = 0; i < readPipes; i++) 1523 * for (i = 0; i < readPipes; i++)
1507 * { 1524 * {
1508 * waitstatus = WaitForSingleObject (handle_array[i], 0); 1525 * waitstatus = WaitForSingleObject (handle_array[i], 0);
1509 * GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Read pipe %d wait status is : %d\n", i, waitstatus); 1526 * LOG (GNUNET_ERROR_TYPE_DEBUG, "Read pipe %d wait status is : %d\n", i, waitstatus);
1510 * if (waitstatus != WAIT_OBJECT_0) 1527 * if (waitstatus != WAIT_OBJECT_0)
1511 * continue; 1528 * continue;
1512 * GNUNET_CONTAINER_slist_add (handles_read, 1529 * GNUNET_CONTAINER_slist_add (handles_read,
1513 * GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, 1530 * GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1514 * readArray[i], sizeof (struct GNUNET_DISK_FileHandle)); 1531 * readArray[i], sizeof (struct GNUNET_DISK_FileHandle));
1515 * retcode++; 1532 * retcode++;
1516 * GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added read Pipe\n"); 1533 * LOG (GNUNET_ERROR_TYPE_DEBUG, "Added read Pipe\n");
1517 * } 1534 * }
1518 */ 1535 */
1519 for (i = 0; i < readPipes; i++) 1536 for (i = 0; i < readPipes; i++)
1520 { 1537 {
1521 DWORD error; 1538 DWORD error;
1522 BOOL bret; 1539 BOOL bret;
1523 1540
1524 SetLastError (0); 1541 SetLastError (0);
1525 waitstatus = 0; 1542 waitstatus = 0;
1526 bret = 1543 bret =
1527 PeekNamedPipe (readArray[i]->h, NULL, 0, NULL, &waitstatus, NULL); 1544 PeekNamedPipe (readArray[i]->h, NULL, 0, NULL, &waitstatus,
1528 error = GetLastError (); 1545 NULL);
1546 error = GetLastError ();
1529#if DEBUG_NETWORK 1547#if DEBUG_NETWORK
1530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1548 LOG (GNUNET_ERROR_TYPE_DEBUG,
1531 "Peek at read pipe %d (0x%x) returned %d (%d bytes available) GLE %u\n", 1549 "Peek at read pipe %d (0x%x) returned %d (%d bytes available) GLE %u\n",
1532 i, readArray[i]->h, bret, waitstatus, error); 1550 i, readArray[i]->h, bret, waitstatus, error);
1533#endif 1551#endif
1534 if (bret == 0) 1552 if (bret == 0)
1535 { 1553 {
1536 if (error != ERROR_BROKEN_PIPE) 1554 if (error != ERROR_BROKEN_PIPE)
1537 continue; 1555 continue;
1538 } 1556 }
1539 else if (waitstatus <= 0) 1557 else if (waitstatus <= 0)
1540 continue; 1558 continue;
1541 GNUNET_CONTAINER_slist_add (handles_read, 1559 GNUNET_CONTAINER_slist_add (handles_read,
1542 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, 1560 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1543 readArray[i], 1561 readArray[i],
1544 sizeof (struct GNUNET_DISK_FileHandle)); 1562 sizeof (struct
1545 retcode++; 1563 GNUNET_DISK_FileHandle));
1564 retcode++;
1546#if DEBUG_NETWORK 1565#if DEBUG_NETWORK
1547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added read Pipe 0x%x (0x%x)\n", 1566 LOG (GNUNET_ERROR_TYPE_DEBUG, "Added read Pipe 0x%x (0x%x)\n",
1548 readArray[i], readArray[i]->h); 1567 readArray[i], readArray[i]->h);
1549#endif 1568#endif
1550 } 1569 }
1551 } 1570 }
1552 waitstatus = WaitForSingleObject (hEventWrite, 0); 1571 waitstatus = WaitForSingleObject (hEventWrite, 0);
1553#if DEBUG_NETWORK 1572#if DEBUG_NETWORK
1554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1573 LOG (GNUNET_ERROR_TYPE_DEBUG,
1555 "Wait for the write event returned %d\n", waitstatus); 1574 "Wait for the write event returned %d\n", waitstatus);
1556#endif 1575#endif
1557 if (waitstatus == WAIT_OBJECT_0) 1576 if (waitstatus == WAIT_OBJECT_0)
1558 { 1577 {
1559 for (i = 0; i < wfds->sds.fd_count; i++) 1578 for (i = 0; i < wfds->sds.fd_count; i++)
1560 { 1579 {
1561 DWORD error; 1580 DWORD error;
1562 int status; 1581 int status;
1563 int so_error = 0; 1582 int so_error = 0;
1564 int sizeof_so_error = sizeof (so_error); 1583 int sizeof_so_error = sizeof (so_error);
1565 int gso_result = 1584 int gso_result =
1566 getsockopt (wfds->sds.fd_array[i], SOL_SOCKET, SO_ERROR, 1585 getsockopt (wfds->sds.fd_array[i], SOL_SOCKET, SO_ERROR,
1567 (char *) &so_error, &sizeof_so_error); 1586 (char *) &so_error, &sizeof_so_error);
1568 1587
1569 status = send (wfds->sds.fd_array[i], NULL, 0, 0); 1588 status = send (wfds->sds.fd_array[i], NULL, 0, 0);
1570 error = GetLastError (); 1589 error = GetLastError ();
1571#if DEBUG_NETWORK 1590#if DEBUG_NETWORK
1572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1591 LOG (GNUNET_ERROR_TYPE_DEBUG,
1573 "send to the socket %d returned %d (%u)\n", i, status, 1592 "send to the socket %d returned %d (%u)\n", i, status,
1574 error); 1593 error);
1575#endif 1594#endif
1576 if (status == 0 || (error != WSAEWOULDBLOCK && error != WSAENOTCONN) || 1595 if (status == 0
1577 (status == -1 && gso_result == 0 && error == WSAENOTCONN && 1596 || (error != WSAEWOULDBLOCK && error != WSAENOTCONN)
1578 so_error == WSAECONNREFUSED)) 1597 || (status == -1 && gso_result == 0 && error == WSAENOTCONN
1579 { 1598 && so_error == WSAECONNREFUSED))
1580 FD_SET (wfds->sds.fd_array[i], &awrite); 1599 {
1581 retcode += 1; 1600 FD_SET (wfds->sds.fd_array[i], &awrite);
1582 } 1601 retcode += 1;
1583 } 1602 }
1603 }
1604 }
1584 } 1605 }
1585 }
1586#if DEBUG_NETWORK 1606#if DEBUG_NETWORK
1587 if (!nhandles || (returnedpos >= nhandles)) 1607 if (!nhandles || (returnedpos >= nhandles))
1588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1608 LOG (GNUNET_ERROR_TYPE_DEBUG, "Returning from _select() with nothing!\n");
1589 "Returning from _select() with nothing!\n");
1590#endif 1609#endif
1591 if (rfds) 1610 if (rfds)
1592 {
1593 struct GNUNET_CONTAINER_SList_Iterator *t;
1594
1595 for (i = 0; i < rfds->sds.fd_count; i++)
1596 { 1611 {
1597 WSAEventSelect (rfds->sds.fd_array[i], hEventRead, 0); 1612 struct GNUNET_CONTAINER_SList_Iterator *t;
1598 nsock++;
1599 }
1600 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1601 GNUNET_CONTAINER_slist_end (t) != GNUNET_YES;
1602 GNUNET_CONTAINER_slist_next (t))
1603 {
1604 struct GNUNET_DISK_FileHandle *fh;
1605 1613
1606 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (t, 1614 for (i = 0; i < rfds->sds.fd_count; i++)
1607 NULL); 1615 {
1608 if (fh->type == GNUNET_PIPE) 1616 WSAEventSelect (rfds->sds.fd_array[i], hEventRead, 0);
1609 { 1617 nsock++;
1610 CancelIo (fh->h); 1618 }
1611 } 1619 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1612 } 1620 GNUNET_CONTAINER_slist_end (t) != GNUNET_YES;
1613 GNUNET_CONTAINER_slist_iter_destroy (t); 1621 GNUNET_CONTAINER_slist_next (t))
1622 {
1623 struct GNUNET_DISK_FileHandle *fh;
1624
1625 fh =
1626 (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (t,
1627 NULL);
1628 if (fh->type == GNUNET_PIPE)
1629 {
1630 CancelIo (fh->h);
1631 }
1632 }
1633 GNUNET_CONTAINER_slist_iter_destroy (t);
1614#if DEBUG_NETWORK 1634#if DEBUG_NETWORK
1615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Zeroing rfds\n"); 1635 LOG (GNUNET_ERROR_TYPE_DEBUG, "Zeroing rfds\n");
1616#endif 1636#endif
1617 GNUNET_NETWORK_fdset_zero (rfds); 1637 GNUNET_NETWORK_fdset_zero (rfds);
1618 if (retcode != -1 && nhandles && (returnedpos < nhandles)) 1638 if (retcode != -1 && nhandles && (returnedpos < nhandles))
1619 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode); 1639 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode);
1620 GNUNET_CONTAINER_slist_append (rfds->handles, handles_read); 1640 GNUNET_CONTAINER_slist_append (rfds->handles, handles_read);
1621 } 1641 }
1622 if (wfds) 1642 if (wfds)
1623 {
1624 for (i = 0; i < wfds->sds.fd_count; i++)
1625 { 1643 {
1626 WSAEventSelect (wfds->sds.fd_array[i], hEventWrite, 0); 1644 for (i = 0; i < wfds->sds.fd_count; i++)
1627 nsock++; 1645 {
1628 } 1646 WSAEventSelect (wfds->sds.fd_array[i], hEventWrite, 0);
1647 nsock++;
1648 }
1629#if DEBUG_NETWORK 1649#if DEBUG_NETWORK
1630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Zeroing wfds\n"); 1650 LOG (GNUNET_ERROR_TYPE_DEBUG, "Zeroing wfds\n");
1631#endif 1651#endif
1632 GNUNET_NETWORK_fdset_zero (wfds); 1652 GNUNET_NETWORK_fdset_zero (wfds);
1633 if (retcode != -1 && nhandles && (returnedpos < nhandles)) 1653 if (retcode != -1 && nhandles && (returnedpos < nhandles))
1634 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode); 1654 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode);
1635 GNUNET_CONTAINER_slist_append (wfds->handles, handles_write); 1655 GNUNET_CONTAINER_slist_append (wfds->handles, handles_write);
1636 } 1656 }
1637 if (efds) 1657 if (efds)
1638 {
1639 for (i = 0; i < efds->sds.fd_count; i++)
1640 { 1658 {
1641 WSAEventSelect (efds->sds.fd_array[i], hEventException, 0); 1659 for (i = 0; i < efds->sds.fd_count; i++)
1642 nsock++; 1660 {
1643 } 1661 WSAEventSelect (efds->sds.fd_array[i], hEventException, 0);
1662 nsock++;
1663 }
1644#if DEBUG_NETWORK 1664#if DEBUG_NETWORK
1645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Zeroing efds\n"); 1665 LOG (GNUNET_ERROR_TYPE_DEBUG, "Zeroing efds\n");
1646#endif 1666#endif
1647 GNUNET_NETWORK_fdset_zero (efds); 1667 GNUNET_NETWORK_fdset_zero (efds);
1648 if (retcode != -1 && nhandles && (returnedpos < nhandles)) 1668 if (retcode != -1 && nhandles && (returnedpos < nhandles))
1649 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode); 1669 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode);
1650 GNUNET_CONTAINER_slist_append (efds->handles, handles_except); 1670 GNUNET_CONTAINER_slist_append (efds->handles, handles_except);
1651 } 1671 }
1652 GNUNET_CONTAINER_slist_destroy (handles_read); 1672 GNUNET_CONTAINER_slist_destroy (handles_read);
1653 GNUNET_CONTAINER_slist_destroy (handles_write); 1673 GNUNET_CONTAINER_slist_destroy (handles_write);
1654 GNUNET_CONTAINER_slist_destroy (handles_except); 1674 GNUNET_CONTAINER_slist_destroy (handles_except);
1655#if DEBUG_NETWORK 1675#if DEBUG_NETWORK
1656 if (rfds) 1676 if (rfds)
1657 {
1658 struct GNUNET_CONTAINER_SList_Iterator *t;
1659
1660 for (i = 0; i < bread.fd_count; i++)
1661 { 1677 {
1662 if (bread.fd_array[i] != 0) 1678 struct GNUNET_CONTAINER_SList_Iterator *t;
1663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in rfds\n",
1664 bread.fd_array[i],
1665 (SAFE_FD_ISSET (bread.fd_array[i], rfds)) ? "SET" :
1666 "NOT SET");
1667 }
1668 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1669 GNUNET_CONTAINER_slist_end (t) != GNUNET_YES;
1670 GNUNET_CONTAINER_slist_next (t))
1671 {
1672 struct GNUNET_DISK_FileHandle *fh;
1673 1679
1674 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (t, 1680 for (i = 0; i < bread.fd_count; i++)
1675 NULL); 1681 {
1676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is SET in rfds\n", fh->h); 1682 if (bread.fd_array[i] != 0)
1683 LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in rfds\n",
1684 bread.fd_array[i],
1685 (SAFE_FD_ISSET (bread.fd_array[i], rfds)) ? "SET" :
1686 "NOT SET");
1687 }
1688 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1689 GNUNET_CONTAINER_slist_end (t) != GNUNET_YES;
1690 GNUNET_CONTAINER_slist_next (t))
1691 {
1692 struct GNUNET_DISK_FileHandle *fh;
1693
1694 fh =
1695 (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (t,
1696 NULL);
1697 LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is SET in rfds\n", fh->h);
1698 }
1677 } 1699 }
1678 }
1679 if (wfds) 1700 if (wfds)
1680 {
1681 for (i = 0; i < bwrite.fd_count; i++)
1682 { 1701 {
1683 if (bwrite.fd_array[i] != 0) 1702 for (i = 0; i < bwrite.fd_count; i++)
1684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in wfds\n", 1703 {
1685 bwrite.fd_array[i], 1704 if (bwrite.fd_array[i] != 0)
1686 (SAFE_FD_ISSET (bwrite.fd_array[i], rfds)) ? "SET" : 1705 LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in wfds\n",
1687 "NOT SET"); 1706 bwrite.fd_array[i],
1707 (SAFE_FD_ISSET (bwrite.fd_array[i], rfds)) ? "SET" :
1708 "NOT SET");
1709 }
1688 } 1710 }
1689 }
1690 if (efds) 1711 if (efds)
1691 {
1692 for (i = 0; i < bexcept.fd_count; i++)
1693 { 1712 {
1694 if (bexcept.fd_array[i] != 0) 1713 for (i = 0; i < bexcept.fd_count; i++)
1695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in efds\n", 1714 {
1696 bexcept.fd_array[i], 1715 if (bexcept.fd_array[i] != 0)
1697 (SAFE_FD_ISSET (bexcept.fd_array[i], rfds)) ? "SET" : 1716 LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in efds\n",
1698 "NOT SET"); 1717 bexcept.fd_array[i],
1718 (SAFE_FD_ISSET (bexcept.fd_array[i], rfds)) ? "SET" :
1719 "NOT SET");
1720 }
1699 } 1721 }
1700 } 1722 LOG (GNUNET_ERROR_TYPE_DEBUG, "Returning %d or 0\n", retcode);
1701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Returning %d or 0\n", retcode);
1702#endif 1723#endif
1703 if (nhandles && (returnedpos < nhandles)) 1724 if (nhandles && (returnedpos < nhandles))
1704 return retcode; 1725 return retcode;