aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorLRN <lrn1986@gmail.com>2014-12-14 14:54:50 +0000
committerLRN <lrn1986@gmail.com>2014-12-14 14:54:50 +0000
commit287a26a5ca4f5ea014419f4c0000c64f4e3acbf3 (patch)
tree4273eb3a7560c4158faab583fc6d842eb000ce85 /src/util
parentf1b4a0c1eb8e41b85169204b1339a51cae9b1cbe (diff)
downloadgnunet-287a26a5ca4f5ea014419f4c0000c64f4e3acbf3.tar.gz
gnunet-287a26a5ca4f5ea014419f4c0000c64f4e3acbf3.zip
Grothoff's massive indentation and documentation fixes in util/network.c
Diffstat (limited to 'src/util')
-rw-r--r--src/util/network.c410
1 files changed, 292 insertions, 118 deletions
diff --git a/src/util/network.c b/src/util/network.c
index 84b659c28..47fdb91ab 100644
--- a/src/util/network.c
+++ b/src/util/network.c
@@ -40,6 +40,9 @@
40#endif 40#endif
41 41
42 42
43/**
44 * @brief handle to a socket
45 */
43struct GNUNET_NETWORK_Handle 46struct GNUNET_NETWORK_Handle
44{ 47{
45#ifndef MINGW 48#ifndef MINGW
@@ -165,11 +168,15 @@ GNUNET_NETWORK_socket_set_blocking (struct GNUNET_NETWORK_Handle *fd,
165 u_long mode; 168 u_long mode;
166 169
167 mode = !doBlock; 170 mode = !doBlock;
168 if (ioctlsocket (fd->fd, FIONBIO, &mode) == SOCKET_ERROR) 171 if (SOCKET_ERROR ==
172 ioctlsocket (fd->fd,
173 FIONBIO,
174 &mode))
169 175
170 { 176 {
171 SetErrnoFromWinsockError (WSAGetLastError ()); 177 SetErrnoFromWinsockError (WSAGetLastError ());
172 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "ioctlsocket"); 178 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
179 "ioctlsocket");
173 return GNUNET_SYSERR; 180 return GNUNET_SYSERR;
174 } 181 }
175 return GNUNET_OK; 182 return GNUNET_OK;
@@ -181,7 +188,8 @@ GNUNET_NETWORK_socket_set_blocking (struct GNUNET_NETWORK_Handle *fd,
181 if (flags == -1) 188 if (flags == -1)
182 189
183 { 190 {
184 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "fcntl"); 191 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
192 "fcntl");
185 return GNUNET_SYSERR; 193 return GNUNET_SYSERR;
186 } 194 }
187 if (doBlock) 195 if (doBlock)
@@ -189,10 +197,13 @@ GNUNET_NETWORK_socket_set_blocking (struct GNUNET_NETWORK_Handle *fd,
189 197
190 else 198 else
191 flags |= O_NONBLOCK; 199 flags |= O_NONBLOCK;
192 if (0 != fcntl (fd->fd, F_SETFL, flags)) 200 if (0 != fcntl (fd->fd,
201 F_SETFL,
202 flags))
193 203
194 { 204 {
195 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "fcntl"); 205 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
206 "fcntl");
196 return GNUNET_SYSERR; 207 return GNUNET_SYSERR;
197 } 208 }
198 return GNUNET_OK; 209 return GNUNET_OK;
@@ -267,8 +278,13 @@ socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h)
267#ifndef WINDOWS 278#ifndef WINDOWS
268 int value = 1; 279 int value = 1;
269 280
270 if (0 != setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, &value, sizeof (value))) 281 if (0 !=
271 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); 282 setsockopt (h->fd,
283 IPPROTO_TCP,
284 TCP_NODELAY,
285 &value, sizeof (value)))
286 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
287 "setsockopt");
272#else 288#else
273 const char *abs_value = "1"; 289 const char *abs_value = "1";
274 290
@@ -276,7 +292,8 @@ socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h)
276 setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, 292 setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY,
277 (const void *) abs_value, 293 (const void *) abs_value,
278 sizeof (abs_value))) 294 sizeof (abs_value)))
279 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); 295 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
296 "setsockopt");
280#endif 297#endif
281} 298}
282 299
@@ -295,7 +312,8 @@ socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h)
295 */ 312 */
296static int 313static int
297initialize_network_handle (struct GNUNET_NETWORK_Handle *h, 314initialize_network_handle (struct GNUNET_NETWORK_Handle *h,
298 int af, int type) 315 int af,
316 int type)
299{ 317{
300 h->af = af; 318 h->af = af;
301 h->type = type; 319 h->type = type;
@@ -358,7 +376,9 @@ GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
358 { 376 {
359 struct sockaddr name; 377 struct sockaddr name;
360 socklen_t namelen = sizeof (name); 378 socklen_t namelen = sizeof (name);
361 int gsn = getsockname (desc->fd, &name, &namelen); 379 int gsn = getsockname (desc->fd,
380 &name,
381 &namelen);
362 382
363 if (gsn == 0) 383 if (gsn == 0)
364 LOG (GNUNET_ERROR_TYPE_DEBUG, 384 LOG (GNUNET_ERROR_TYPE_DEBUG,
@@ -366,15 +386,18 @@ GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
366 GNUNET_a2s (&name, namelen)); 386 GNUNET_a2s (&name, namelen));
367 } 387 }
368#endif 388#endif
369 ret->fd = accept (desc->fd, address, address_len); 389 ret->fd = accept (desc->fd,
390 address,
391 address_len);
370 if (-1 == ret->fd) 392 if (-1 == ret->fd)
371 { 393 {
372 GNUNET_free (ret); 394 GNUNET_free (ret);
373 return NULL; 395 return NULL;
374 } 396 }
375 if (GNUNET_OK != initialize_network_handle (ret, 397 if (GNUNET_OK !=
376 (NULL != address) ? address->sa_family : desc->af, 398 initialize_network_handle (ret,
377 SOCK_STREAM)) 399 (NULL != address) ? address->sa_family : desc->af,
400 SOCK_STREAM))
378 return NULL; 401 return NULL;
379 return ret; 402 return ret;
380} 403}
@@ -404,7 +427,8 @@ GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
404 if (setsockopt (desc->fd, IPPROTO_IPV6, IPV6_V6ONLY, 427 if (setsockopt (desc->fd, IPPROTO_IPV6, IPV6_V6ONLY,
405 (const void *) &on, 428 (const void *) &on,
406 sizeof (on))) 429 sizeof (on)))
407 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG, "setsockopt"); 430 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG,
431 "setsockopt");
408 } 432 }
409#endif 433#endif
410#endif 434#endif
@@ -413,9 +437,13 @@ GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
413 const int on = 1; 437 const int on = 1;
414 438
415 /* This is required here for TCP sockets, but only on UNIX */ 439 /* This is required here for TCP sockets, but only on UNIX */
416 if ((SOCK_STREAM == desc->type) 440 if ( (SOCK_STREAM == desc->type) &&
417 && (0 != setsockopt (desc->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)))) 441 (0 != setsockopt (desc->fd,
418 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG, "setsockopt"); 442 SOL_SOCKET,
443 SO_REUSEADDR,
444 &on, sizeof (on))))
445 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG,
446 "setsockopt");
419 } 447 }
420#endif 448#endif
421#ifndef WINDOWS 449#ifndef WINDOWS
@@ -474,7 +502,9 @@ GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
474 error = WSAGetLastError (); 502 error = WSAGetLastError ();
475 SetErrnoFromWinsockError (error); 503 SetErrnoFromWinsockError (error);
476 LOG (GNUNET_ERROR_TYPE_DEBUG, 504 LOG (GNUNET_ERROR_TYPE_DEBUG,
477 "Closed 0x%x, closesocket() returned %d, GLE is %u\n", desc->fd, ret, 505 "Closed 0x%x, closesocket() returned %d, GLE is %u\n",
506 desc->fd,
507 ret,
478 error); 508 error);
479#else 509#else
480 ret = close (desc->fd); 510 ret = close (desc->fd);
@@ -484,9 +514,10 @@ GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
484 514
485 /* Cleanup the UNIX domain socket and its parent directories in case of non 515 /* Cleanup the UNIX domain socket and its parent directories in case of non
486 abstract sockets */ 516 abstract sockets */
487 if ((AF_UNIX == desc->af) && (NULL != desc->addr) 517 if ( (AF_UNIX == desc->af) &&
488 && (NULL != (un = (const struct sockaddr_un *) desc->addr)->sun_path) 518 (NULL != desc->addr) &&
489 && ('\0' != un->sun_path[0])) 519 (NULL != (un = (const struct sockaddr_un *) desc->addr)->sun_path) &&
520 ('\0' != un->sun_path[0]) )
490 { 521 {
491 char *dirname = GNUNET_strndup (un->sun_path, 522 char *dirname = GNUNET_strndup (un->sun_path,
492 sizeof (un->sun_path)); 523 sizeof (un->sun_path));
@@ -557,7 +588,11 @@ GNUNET_NETWORK_socket_box_native (SOCKTYPE fd)
557 unsigned long i; 588 unsigned long i;
558 DWORD d; 589 DWORD d;
559 /* FIXME: Find a better call to check that FD is valid */ 590 /* FIXME: Find a better call to check that FD is valid */
560 if (WSAIoctl (fd, FIONBIO, (void *) &i, sizeof (i), NULL, 0, &d, NULL, NULL) != 0) 591 if (0 !=
592 WSAIoctl (fd, FIONBIO,
593 (void *) &i, sizeof (i),
594 NULL, 0, &d,
595 NULL, NULL))
561 return NULL; /* invalid FD */ 596 return NULL; /* invalid FD */
562 ret = GNUNET_new (struct GNUNET_NETWORK_Handle); 597 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
563 ret->fd = fd; 598 ret->fd = fd;
@@ -589,8 +624,9 @@ GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
589{ 624{
590 int ret; 625 int ret;
591 626
592 ret = connect (desc->fd, address, address_len); 627 ret = connect (desc->fd,
593 628 address,
629 address_len);
594#ifdef MINGW 630#ifdef MINGW
595 if (SOCKET_ERROR == ret) 631 if (SOCKET_ERROR == ret)
596 { 632 {
@@ -615,17 +651,23 @@ GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
615 */ 651 */
616int 652int
617GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc, 653GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
618 int level, int optname, void *optval, 654 int level,
655 int optname,
656 void *optval,
619 socklen_t *optlen) 657 socklen_t *optlen)
620{ 658{
621 int ret; 659 int ret;
622 660
623 ret = getsockopt (desc->fd, level, optname, optval, optlen); 661 ret = getsockopt (desc->fd,
662 level,
663 optname,
664 optval, optlen);
624 665
625#ifdef MINGW 666#ifdef MINGW
626 if (ret == 0 && level == SOL_SOCKET && optname == SO_ERROR) 667 if ( (0 == ret) &&
668 (SOL_SOCKET == level) &&
669 (SO_ERROR == optname) )
627 *((int *) optval) = GetErrnoFromWinsockError (*((int *) optval)); 670 *((int *) optval) = GetErrnoFromWinsockError (*((int *) optval));
628
629 else if (SOCKET_ERROR == ret) 671 else if (SOCKET_ERROR == ret)
630 SetErrnoFromWinsockError (WSAGetLastError ()); 672 SetErrnoFromWinsockError (WSAGetLastError ());
631#endif 673#endif
@@ -646,12 +688,11 @@ GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
646{ 688{
647 int ret; 689 int ret;
648 690
649 ret = listen (desc->fd, backlog); 691 ret = listen (desc->fd,
650 692 backlog);
651#ifdef MINGW 693#ifdef MINGW
652 if (SOCKET_ERROR == ret) 694 if (SOCKET_ERROR == ret)
653 SetErrnoFromWinsockError (WSAGetLastError ()); 695 SetErrnoFromWinsockError (WSAGetLastError ());
654
655#endif 696#endif
656 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR; 697 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
657} 698}
@@ -664,8 +705,7 @@ GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
664 * @returns #GNUNET_NO if no data is available, or on error! 705 * @returns #GNUNET_NO if no data is available, or on error!
665 */ 706 */
666ssize_t 707ssize_t
667GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle * 708GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle *desc)
668 desc)
669{ 709{
670 int error; 710 int error;
671 711
@@ -673,14 +713,18 @@ GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle *
673#ifndef WINDOWS 713#ifndef WINDOWS
674 int pending; 714 int pending;
675 715
676 error = ioctl (desc->fd, FIONREAD, &pending); 716 error = ioctl (desc->fd,
717 FIONREAD,
718 &pending);
677 if (error == 0) 719 if (error == 0)
678 return (ssize_t) pending; 720 return (ssize_t) pending;
679 return GNUNET_NO; 721 return GNUNET_NO;
680#else 722#else
681 u_long pending; 723 u_long pending;
682 724
683 error = ioctlsocket (desc->fd, FIONREAD, &pending); 725 error = ioctlsocket (desc->fd,
726 FIONREAD,
727 &pending);
684 if (error != SOCKET_ERROR) 728 if (error != SOCKET_ERROR)
685 return (ssize_t) pending; 729 return (ssize_t) pending;
686 return GNUNET_NO; 730 return GNUNET_NO;
@@ -700,8 +744,10 @@ GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle *
700 */ 744 */
701ssize_t 745ssize_t
702GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle *desc, 746GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle *desc,
703 void *buffer, size_t length, 747 void *buffer,
704 struct sockaddr *src_addr, socklen_t *addrlen) 748 size_t length,
749 struct sockaddr *src_addr,
750 socklen_t *addrlen)
705{ 751{
706 int ret; 752 int ret;
707 int flags; 753 int flags;
@@ -712,7 +758,12 @@ GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle *desc,
712 flags |= MSG_DONTWAIT; 758 flags |= MSG_DONTWAIT;
713 759
714#endif 760#endif
715 ret = recvfrom (desc->fd, buffer, length, flags, src_addr, addrlen); 761 ret = recvfrom (desc->fd,
762 buffer,
763 length,
764 flags,
765 src_addr,
766 addrlen);
716#ifdef MINGW 767#ifdef MINGW
717 if (SOCKET_ERROR == ret) 768 if (SOCKET_ERROR == ret)
718 SetErrnoFromWinsockError (WSAGetLastError ()); 769 SetErrnoFromWinsockError (WSAGetLastError ());
@@ -727,10 +778,12 @@ GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle *desc,
727 * @param desc socket 778 * @param desc socket
728 * @param buffer buffer 779 * @param buffer buffer
729 * @param length length of @a buffer 780 * @param length length of @a buffer
781 * @return number of bytes received, -1 on error
730 */ 782 */
731ssize_t 783ssize_t
732GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc, 784GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle *desc,
733 void *buffer, size_t length) 785 void *buffer,
786 size_t length)
734{ 787{
735 int ret; 788 int ret;
736 int flags; 789 int flags;
@@ -740,7 +793,10 @@ GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc,
740#ifdef MSG_DONTWAIT 793#ifdef MSG_DONTWAIT
741 flags |= MSG_DONTWAIT; 794 flags |= MSG_DONTWAIT;
742#endif 795#endif
743 ret = recv (desc->fd, buffer, length, flags); 796 ret = recv (desc->fd,
797 buffer,
798 length,
799 flags);
744#ifdef MINGW 800#ifdef MINGW
745 if (SOCKET_ERROR == ret) 801 if (SOCKET_ERROR == ret)
746 SetErrnoFromWinsockError (WSAGetLastError ()); 802 SetErrnoFromWinsockError (WSAGetLastError ());
@@ -758,14 +814,14 @@ GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc,
758 * @return number of bytes sent, #GNUNET_SYSERR on error 814 * @return number of bytes sent, #GNUNET_SYSERR on error
759 */ 815 */
760ssize_t 816ssize_t
761GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc, 817GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle *desc,
762 const void *buffer, size_t length) 818 const void *buffer,
819 size_t length)
763{ 820{
764 int ret; 821 int ret;
765 int flags; 822 int flags;
766 823
767 flags = 0; 824 flags = 0;
768
769#ifdef MSG_DONTWAIT 825#ifdef MSG_DONTWAIT
770 flags |= MSG_DONTWAIT; 826 flags |= MSG_DONTWAIT;
771 827
@@ -774,8 +830,10 @@ GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc,
774 flags |= MSG_NOSIGNAL; 830 flags |= MSG_NOSIGNAL;
775 831
776#endif 832#endif
777 ret = send (desc->fd, buffer, length, flags); 833 ret = send (desc->fd,
778 834 buffer,
835 length,
836 flags);
779#ifdef MINGW 837#ifdef MINGW
780 if (SOCKET_ERROR == ret) 838 if (SOCKET_ERROR == ret)
781 SetErrnoFromWinsockError (WSAGetLastError ()); 839 SetErrnoFromWinsockError (WSAGetLastError ());
@@ -791,15 +849,16 @@ GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc,
791 * 849 *
792 * @param desc socket 850 * @param desc socket
793 * @param message data to send 851 * @param message data to send
794 * @param length size of the data 852 * @param length size of the @a message
795 * @param dest_addr destination address 853 * @param dest_addr destination address
796 * @param dest_len length of @a address 854 * @param dest_len length of @a address
797 * @return number of bytes sent, #GNUNET_SYSERR on error 855 * @return number of bytes sent, #GNUNET_SYSERR on error
798 */ 856 */
799ssize_t 857ssize_t
800GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle * desc, 858GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle *desc,
801 const void *message, size_t length, 859 const void *message,
802 const struct sockaddr * dest_addr, 860 size_t length,
861 const struct sockaddr *dest_addr,
803 socklen_t dest_len) 862 socklen_t dest_len)
804{ 863{
805 int ret; 864 int ret;
@@ -865,7 +924,9 @@ GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,
865 * @return new socket, NULL on error 924 * @return new socket, NULL on error
866 */ 925 */
867struct GNUNET_NETWORK_Handle * 926struct GNUNET_NETWORK_Handle *
868GNUNET_NETWORK_socket_create (int domain, int type, int protocol) 927GNUNET_NETWORK_socket_create (int domain,
928 int type,
929 int protocol)
869{ 930{
870 struct GNUNET_NETWORK_Handle *ret; 931 struct GNUNET_NETWORK_Handle *ret;
871 932
@@ -877,7 +938,9 @@ GNUNET_NETWORK_socket_create (int domain, int type, int protocol)
877 return NULL; 938 return NULL;
878 } 939 }
879 if (GNUNET_OK != 940 if (GNUNET_OK !=
880 initialize_network_handle (ret, domain, type)) 941 initialize_network_handle (ret,
942 domain,
943 type))
881 return NULL; 944 return NULL;
882 return ret; 945 return ret;
883} 946}
@@ -890,7 +953,8 @@ GNUNET_NETWORK_socket_create (int domain, int type, int protocol)
890 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise 953 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
891 */ 954 */
892int 955int
893GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc, int how) 956GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc,
957 int how)
894{ 958{
895 int ret; 959 int ret;
896 960
@@ -922,25 +986,43 @@ GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc)
922 986
923 if (0 != 987 if (0 !=
924 (ret = 988 (ret =
925 setsockopt (desc->fd, SOL_SOCKET, SO_SNDBUF, (char *) &value, 989 setsockopt (desc->fd,
990 SOL_SOCKET,
991 SO_SNDBUF,
992 (char *) &value,
926 sizeof (value)))) 993 sizeof (value))))
927 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); 994 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
995 "setsockopt");
928 if (0 != 996 if (0 !=
929 (ret = 997 (ret =
930 setsockopt (desc->fd, SOL_SOCKET, SO_RCVBUF, (char *) &value, 998 setsockopt (desc->fd,
999 SOL_SOCKET,
1000 SO_RCVBUF,
1001 (char *) &value,
931 sizeof (value)))) 1002 sizeof (value))))
932 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); 1003 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1004 "setsockopt");
933#elif LINUX 1005#elif LINUX
934 int value = 0; 1006 int value = 0;
935 1007
936 if (0 != 1008 if (0 !=
937 (ret = 1009 (ret =
938 setsockopt (desc->fd, SOL_SOCKET, SO_SNDBUF, &value, sizeof (value)))) 1010 setsockopt (desc->fd,
939 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); 1011 SOL_SOCKET,
1012 SO_SNDBUF,
1013 &value,
1014 sizeof (value))))
1015 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1016 "setsockopt");
940 if (0 != 1017 if (0 !=
941 (ret = 1018 (ret =
942 setsockopt (desc->fd, SOL_SOCKET, SO_RCVBUF, &value, sizeof (value)))) 1019 setsockopt (desc->fd,
943 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); 1020 SOL_SOCKET,
1021 SO_RCVBUF,
1022 &value,
1023 sizeof (value))))
1024 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1025 "setsockopt");
944#endif 1026#endif
945 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR; 1027 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
946} 1028}
@@ -972,9 +1054,10 @@ void
972GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds, 1054GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
973 const struct GNUNET_NETWORK_Handle *desc) 1055 const struct GNUNET_NETWORK_Handle *desc)
974{ 1056{
975 FD_SET (desc->fd, &fds->sds); 1057 FD_SET (desc->fd,
976 if (desc->fd + 1 > fds->nsds) 1058 &fds->sds);
977 fds->nsds = desc->fd + 1; 1059 fds->nsds = GNUNET_MAX (fds->nsds,
1060 desc->fd + 1);
978} 1061}
979 1062
980 1063
@@ -989,7 +1072,8 @@ int
989GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds, 1072GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
990 const struct GNUNET_NETWORK_Handle *desc) 1073 const struct GNUNET_NETWORK_Handle *desc)
991{ 1074{
992 return FD_ISSET (desc->fd, &fds->sds); 1075 return FD_ISSET (desc->fd,
1076 &fds->sds);
993} 1077}
994 1078
995 1079
@@ -1041,9 +1125,9 @@ void
1041GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to, 1125GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
1042 const struct GNUNET_NETWORK_FDSet *from) 1126 const struct GNUNET_NETWORK_FDSet *from)
1043{ 1127{
1044 FD_COPY (&from->sds, &to->sds); 1128 FD_COPY (&from->sds,
1129 &to->sds);
1045 to->nsds = from->nsds; 1130 to->nsds = from->nsds;
1046
1047#ifdef MINGW 1131#ifdef MINGW
1048 GNUNET_CONTAINER_slist_clear (to->handles); 1132 GNUNET_CONTAINER_slist_clear (to->handles);
1049 GNUNET_CONTAINER_slist_append (to->handles, from->handles); 1133 GNUNET_CONTAINER_slist_append (to->handles, from->handles);
@@ -1099,9 +1183,11 @@ GNUNET_NETWORK_get_addrlen (struct GNUNET_NETWORK_Handle *desc)
1099 */ 1183 */
1100void 1184void
1101GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to, 1185GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
1102 const fd_set * from, int nfds) 1186 const fd_set *from,
1187 int nfds)
1103{ 1188{
1104 FD_COPY (from, &to->sds); 1189 FD_COPY (from,
1190 &to->sds);
1105 to->nsds = nfds; 1191 to->nsds = nfds;
1106} 1192}
1107 1193
@@ -1113,11 +1199,13 @@ GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
1113 * @param nfd native FD to set 1199 * @param nfd native FD to set
1114 */ 1200 */
1115void 1201void
1116GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to, int nfd) 1202GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to,
1203 int nfd)
1117{ 1204{
1118 GNUNET_assert ((nfd >= 0) && (nfd < FD_SETSIZE)); 1205 GNUNET_assert ((nfd >= 0) && (nfd < FD_SETSIZE));
1119 FD_SET (nfd, &to->sds); 1206 FD_SET (nfd, &to->sds);
1120 to->nsds = GNUNET_MAX (nfd + 1, to->nsds); 1207 to->nsds = GNUNET_MAX (nfd + 1,
1208 to->nsds);
1121} 1209}
1122 1210
1123 1211
@@ -1132,7 +1220,8 @@ int
1132GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to, 1220GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
1133 int nfd) 1221 int nfd)
1134{ 1222{
1135 if ((nfd == -1) || (to == NULL)) 1223 if ( (-1 == nfd) ||
1224 (NULL == to) )
1136 return GNUNET_NO; 1225 return GNUNET_NO;
1137 return FD_ISSET (nfd, &to->sds) ? GNUNET_YES : GNUNET_NO; 1226 return FD_ISSET (nfd, &to->sds) ? GNUNET_YES : GNUNET_NO;
1138} 1227}
@@ -1155,17 +1244,20 @@ GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
1155#else 1244#else
1156 int fd; 1245 int fd;
1157 1246
1158 GNUNET_DISK_internal_file_handle_ (h, &fd, sizeof (int)); 1247 GNUNET_DISK_internal_file_handle_ (h,
1159 FD_SET (fd, &fds->sds); 1248 &fd,
1160 if (fd + 1 > fds->nsds) 1249 sizeof (int));
1161 fds->nsds = fd + 1; 1250 FD_SET (fd,
1162 1251 &fds->sds);
1252 fds->nsds = GNUNET_MAX (fd + 1,
1253 fds->nsds);
1163#endif 1254#endif
1164} 1255}
1165 1256
1166 1257
1167/** 1258/**
1168 * Check if a file handle is part of an fd set 1259 * Check if a file handle is part of an fd set
1260 *
1169 * @param fds fd set 1261 * @param fds fd set
1170 * @param h file handle 1262 * @param h file handle
1171 * @return #GNUNET_YES if the file handle is part of the set 1263 * @return #GNUNET_YES if the file handle is part of the set
@@ -1174,19 +1266,20 @@ int
1174GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds, 1266GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
1175 const struct GNUNET_DISK_FileHandle *h) 1267 const struct GNUNET_DISK_FileHandle *h)
1176{ 1268{
1177
1178#ifdef MINGW 1269#ifdef MINGW
1179 return GNUNET_CONTAINER_slist_contains (fds->handles, h, 1270 return GNUNET_CONTAINER_slist_contains (fds->handles, h,
1180 sizeof (struct 1271 sizeof (struct
1181 GNUNET_DISK_FileHandle)); 1272 GNUNET_DISK_FileHandle));
1182#else 1273#else
1183 return FD_ISSET (h->fd, &fds->sds); 1274 return FD_ISSET (h->fd,
1275 &fds->sds);
1184#endif 1276#endif
1185} 1277}
1186 1278
1187 1279
1188/** 1280/**
1189 * Checks if two fd sets overlap 1281 * Checks if two fd sets overlap
1282 *
1190 * @param fds1 first fd set 1283 * @param fds1 first fd set
1191 * @param fds2 second fd set 1284 * @param fds2 second fd set
1192 * @return #GNUNET_YES if they do overlap, #GNUNET_NO otherwise 1285 * @return #GNUNET_YES if they do overlap, #GNUNET_NO otherwise
@@ -1198,24 +1291,26 @@ GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
1198#ifndef MINGW 1291#ifndef MINGW
1199 int nfds; 1292 int nfds;
1200 1293
1201 nfds = fds1->nsds; 1294 nfds = GNUNET_MIN (fds1->nsds,
1202 if (nfds > fds2->nsds) 1295 fds2->nsds);
1203 nfds = fds2->nsds;
1204 while (nfds > 0) 1296 while (nfds > 0)
1205 { 1297 {
1206 nfds--; 1298 nfds--;
1207 if (FD_ISSET (nfds, &fds1->sds) && FD_ISSET (nfds, &fds2->sds)) 1299 if ( (FD_ISSET (nfds,
1300 &fds1->sds)) &&
1301 (FD_ISSET (nfds,
1302 &fds2->sds)) )
1208 return GNUNET_YES; 1303 return GNUNET_YES;
1209 } 1304 }
1305 return GNUNET_NO;
1210#else 1306#else
1211 struct GNUNET_CONTAINER_SList_Iterator it; 1307 struct GNUNET_CONTAINER_SList_Iterator it;
1212 struct GNUNET_DISK_FileHandle *h; 1308 struct GNUNET_DISK_FileHandle *h;
1213 int i; 1309 int i;
1214 int j; 1310 int j;
1215 1311
1216 /*This code is somewhat hacky, we are not supposed to know what's 1312 /* This code is somewhat hacky, we are not supposed to know what's
1217 * inside of fd_set; also the O(n^2) is really bad... */ 1313 * inside of fd_set; also the O(n^2) is really bad... */
1218
1219 for (i = 0; i < fds1->sds.fd_count; i++) 1314 for (i = 0; i < fds1->sds.fd_count; i++)
1220 { 1315 {
1221 for (j = 0; j < fds2->sds.fd_count; j++) 1316 for (j = 0; j < fds2->sds.fd_count; j++)
@@ -1253,8 +1348,8 @@ GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
1253 } 1348 }
1254 GNUNET_CONTAINER_slist_next (&it); 1349 GNUNET_CONTAINER_slist_next (&it);
1255 } 1350 }
1256#endif
1257 return GNUNET_NO; 1351 return GNUNET_NO;
1352#endif
1258} 1353}
1259 1354
1260 1355
@@ -1291,19 +1386,58 @@ GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
1291 GNUNET_free (fds); 1386 GNUNET_free (fds);
1292} 1387}
1293 1388
1389
1294#if MINGW 1390#if MINGW
1391/**
1392 * FIXME.
1393 */
1295struct _select_params 1394struct _select_params
1296{ 1395{
1396 /**
1397 * Read set.
1398 */
1297 fd_set *r; 1399 fd_set *r;
1400
1401 /**
1402 * Write set.
1403 */
1298 fd_set *w; 1404 fd_set *w;
1405
1406 /**
1407 * Except set.
1408 */
1299 fd_set *e; 1409 fd_set *e;
1410
1411 /**
1412 * Timeout for select().
1413 */
1300 struct timeval *tv; 1414 struct timeval *tv;
1415
1416 /**
1417 * FIXME.
1418 */
1301 HANDLE wakeup; 1419 HANDLE wakeup;
1420
1421 /**
1422 * FIXME.
1423 */
1302 HANDLE standby; 1424 HANDLE standby;
1425
1426 /**
1427 * FIXME.
1428 */
1303 SOCKET wakeup_socket; 1429 SOCKET wakeup_socket;
1430
1431 /**
1432 * Set to return value from select.
1433 */
1304 int status; 1434 int status;
1305}; 1435};
1306 1436
1437
1438/**
1439 * FIXME.
1440 */
1307static DWORD WINAPI 1441static DWORD WINAPI
1308_selector (LPVOID p) 1442_selector (LPVOID p)
1309{ 1443{
@@ -1311,12 +1445,19 @@ _selector (LPVOID p)
1311 1445
1312 while (1) 1446 while (1)
1313 { 1447 {
1314 WaitForSingleObject (sp->standby, INFINITE); 1448 WaitForSingleObject (sp->standby,
1449 INFINITE);
1315 ResetEvent (sp->standby); 1450 ResetEvent (sp->standby);
1316 sp->status = select (1, sp->r, sp->w, sp->e, sp->tv); 1451 sp->status = select (1,
1317 if (FD_ISSET (sp->wakeup_socket, sp->r)) 1452 sp->r,
1453 sp->w,
1454 sp->e,
1455 sp->tv);
1456 if (FD_ISSET (sp->wakeup_socket,
1457 sp->r))
1318 { 1458 {
1319 FD_CLR (sp->wakeup_socket, sp->r); 1459 FD_CLR (sp->wakeup_socket,
1460 sp->r);
1320 sp->status -= 1; 1461 sp->status -= 1;
1321 } 1462 }
1322 SetEvent (sp->wakeup); 1463 SetEvent (sp->wakeup);
@@ -1350,10 +1491,12 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1350 else 1491 else
1351 nfds = 0; 1492 nfds = 0;
1352 if (NULL != wfds) 1493 if (NULL != wfds)
1353 nfds = GNUNET_MAX (nfds, wfds->nsds); 1494 nfds = GNUNET_MAX (nfds,
1495 wfds->nsds);
1354 if (NULL != efds) 1496 if (NULL != efds)
1355 nfds = GNUNET_MAX (nfds, efds->nsds); 1497 nfds = GNUNET_MAX (nfds,
1356 if ((nfds == 0) && 1498 efds->nsds);
1499 if ((0 == nfds) &&
1357 (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)) 1500 (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us))
1358 { 1501 {
1359 GNUNET_break (0); 1502 GNUNET_break (0);
@@ -1877,18 +2020,24 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1877 nhandles); 2020 nhandles);
1878 handle_array[nhandles++] = select_finished_event; 2021 handle_array[nhandles++] = select_finished_event;
1879 if (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) 2022 if (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
2023 {
1880 sp.tv = NULL; 2024 sp.tv = NULL;
2025 }
1881 else 2026 else
1882 { 2027 {
1883 select_timeout.tv_sec = timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us; 2028 select_timeout.tv_sec = timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us;
1884 select_timeout.tv_usec =(timeout.rel_value_us - 2029 select_timeout.tv_usec = (timeout.rel_value_us -
1885 (select_timeout.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value_us)); 2030 (select_timeout.tv_sec *
2031 GNUNET_TIME_UNIT_SECONDS.rel_value_us));
1886 sp.tv = &select_timeout; 2032 sp.tv = &select_timeout;
1887 } 2033 }
1888 FD_SET (select_wakeup_socket, &aread); 2034 FD_SET (select_wakeup_socket, &aread);
1889 do 2035 do
1890 { 2036 {
1891 i = recv (select_wakeup_socket, (char *) &returnedpos, 1, 0); 2037 i = recv (select_wakeup_socket,
2038 (char *) &returnedpos,
2039 1,
2040 0);
1892 } while (i == 1); 2041 } while (i == 1);
1893 sp.r = &aread; 2042 sp.r = &aread;
1894 sp.w = &awrite; 2043 sp.w = &awrite;
@@ -1901,24 +2050,35 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1901 * but we don't use OOB data. 2050 * but we don't use OOB data.
1902 */ 2051 */
1903 for (i = 0; i < awrite.fd_count; i++) 2052 for (i = 0; i < awrite.fd_count; i++)
1904 FD_SET (awrite.fd_array[i], &aexcept); 2053 FD_SET (awrite.fd_array[i],
2054 &aexcept);
1905 ResetEvent (select_finished_event); 2055 ResetEvent (select_finished_event);
1906 SetEvent (select_standby_event); 2056 SetEvent (select_standby_event);
1907 } 2057 }
1908 2058
2059 /* NULL-terminate array */
1909 handle_array[nhandles] = NULL; 2060 handle_array[nhandles] = NULL;
1910 LOG (GNUNET_ERROR_TYPE_DEBUG, "nfds: %d, handles: %d, will wait: %llu mcs\n", 2061 LOG (GNUNET_ERROR_TYPE_DEBUG,
1911 nfds, nhandles, mcs_total); 2062 "nfds: %d, handles: %d, will wait: %llu mcs\n",
2063 nfds,
2064 nhandles,
2065 mcs_total);
1912 if (nhandles) 2066 if (nhandles)
1913 { 2067 {
1914 returncode = 2068 returncode
1915 WaitForMultipleObjects (nhandles, handle_array, FALSE, ms_rounded); 2069 = WaitForMultipleObjects (nhandles,
1916 LOG (GNUNET_ERROR_TYPE_DEBUG, "WaitForMultipleObjects Returned : %d\n", returncode); 2070 handle_array,
2071 FALSE,
2072 ms_rounded);
2073 LOG (GNUNET_ERROR_TYPE_DEBUG,
2074 "WaitForMultipleObjects Returned: %d\n",
2075 returncode);
1917 } 2076 }
1918 else if (nfds > 0) 2077 else if (nfds > 0)
1919 { 2078 {
1920 GNUNET_break (0); /* This branch shouldn't actually be executed...*/ 2079 GNUNET_break (0); /* This branch shouldn't actually be executed...*/
1921 i = (int) WaitForSingleObject (select_finished_event, INFINITE); 2080 i = (int) WaitForSingleObject (select_finished_event,
2081 INFINITE);
1922 returncode = WAIT_TIMEOUT; 2082 returncode = WAIT_TIMEOUT;
1923 } 2083 }
1924 else 2084 else
@@ -1932,16 +2092,25 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1932 /* Don't wake up select-thread when delay is 0, it should return immediately 2092 /* Don't wake up select-thread when delay is 0, it should return immediately
1933 * and wake up by itself. 2093 * and wake up by itself.
1934 */ 2094 */
1935 if (mcs_total != 0) 2095 if (0 != mcs_total)
1936 i = send (select_send_socket, (const char *) &returnedpos, 1, 0); 2096 i = send (select_send_socket,
1937 i = (int) WaitForSingleObject (select_finished_event, INFINITE); 2097 (const char *) &returnedpos,
1938 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished waiting for the select thread: %d %d\n", i, sp.status); 2098 1,
1939 if (mcs_total != 0) 2099 0);
2100 i = (int) WaitForSingleObject (select_finished_event,
2101 INFINITE);
2102 LOG (GNUNET_ERROR_TYPE_DEBUG,
2103 "Finished waiting for the select thread: %d %d\n",
2104 i,
2105 sp.status);
2106 if (0 != mcs_total)
1940 { 2107 {
1941 do 2108 do
1942 { 2109 {
1943 i = recv (select_wakeup_socket, (char *) &returnedpos, 1, 0); 2110 i = recv (select_wakeup_socket,
1944 } while (i == 1); 2111 (char *) &returnedpos,
2112 1, 0);
2113 } while (1 == i);
1945 } 2114 }
1946 /* Check aexcept, add its contents to awrite */ 2115 /* Check aexcept, add its contents to awrite */
1947 for (i = 0; i < aexcept.fd_count; i++) 2116 for (i = 0; i < aexcept.fd_count; i++)
@@ -1949,7 +2118,9 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1949 } 2118 }
1950 2119
1951 returnedpos = returncode - WAIT_OBJECT_0; 2120 returnedpos = returncode - WAIT_OBJECT_0;
1952 LOG (GNUNET_ERROR_TYPE_DEBUG, "return pos is : %d\n", returnedpos); 2121 LOG (GNUNET_ERROR_TYPE_DEBUG,
2122 "return pos is: %d\n",
2123 returnedpos);
1953 2124
1954 if (nhandles && (returnedpos < nhandles)) 2125 if (nhandles && (returnedpos < nhandles))
1955 { 2126 {
@@ -1962,9 +2133,12 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1962 { 2133 {
1963 GNUNET_CONTAINER_slist_append (handles_write, wfds->handles); 2134 GNUNET_CONTAINER_slist_append (handles_write, wfds->handles);
1964 retcode += write_handles; 2135 retcode += write_handles;
1965 LOG (GNUNET_ERROR_TYPE_DEBUG, "Added write pipe\n"); 2136 LOG (GNUNET_ERROR_TYPE_DEBUG,
2137 "Added write pipe\n");
1966 } 2138 }
1967 LOG (GNUNET_ERROR_TYPE_DEBUG, "ReadPipes is : %d\n", readPipes); 2139 LOG (GNUNET_ERROR_TYPE_DEBUG,
2140 "ReadPipes is: %d\n",
2141 readPipes);
1968 /* We have some pipes ready for read. */ 2142 /* We have some pipes ready for read. */
1969 if (returnedpos < readPipes) 2143 if (returnedpos < readPipes)
1970 { 2144 {
@@ -2002,8 +2176,9 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
2002 } 2176 }
2003 } 2177 }
2004 } 2178 }
2005 if (!nhandles || (returnedpos >= nhandles)) 2179 if (! nhandles || (returnedpos >= nhandles))
2006 LOG (GNUNET_ERROR_TYPE_DEBUG, "Returning from _select() with nothing!\n"); 2180 LOG (GNUNET_ERROR_TYPE_DEBUG,
2181 "Returning from _select() with nothing!\n");
2007 if (rfds) 2182 if (rfds)
2008 { 2183 {
2009 struct GNUNET_CONTAINER_SList_Iterator t; 2184 struct GNUNET_CONTAINER_SList_Iterator t;
@@ -2087,8 +2262,7 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
2087#endif 2262#endif
2088 if (nhandles && (returnedpos < nhandles)) 2263 if (nhandles && (returnedpos < nhandles))
2089 return retcode; 2264 return retcode;
2090 else 2265 return 0;
2091 return 0;
2092} 2266}
2093 2267
2094/* MINGW */ 2268/* MINGW */