summaryrefslogtreecommitdiff
path: root/src/util/network.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/network.c')
-rw-r--r--src/util/network.c166
1 files changed, 9 insertions, 157 deletions
diff --git a/src/util/network.c b/src/util/network.c
index 7a1d27f0e..ec76424eb 100644
--- a/src/util/network.c
+++ b/src/util/network.c
@@ -44,11 +44,7 @@
44 * @brief handle to a socket 44 * @brief handle to a socket
45 */ 45 */
46struct GNUNET_NETWORK_Handle { 46struct GNUNET_NETWORK_Handle {
47#ifndef MINGW
48 int fd; 47 int fd;
49#else
50 _win_socket fd;
51#endif
52 48
53 /** 49 /**
54 * Address family / domain. 50 * Address family / domain.
@@ -119,11 +115,7 @@ GNUNET_NETWORK_test_pf(int pf)
119 } 115 }
120 else 116 else
121 { 117 {
122#if WINDOWS
123 closesocket(s);
124#else
125 close(s); 118 close(s);
126#endif
127 ret = GNUNET_OK; 119 ret = GNUNET_OK;
128 } 120 }
129 switch (pf) 121 switch (pf)
@@ -189,7 +181,6 @@ GNUNET_NETWORK_shorten_unixpath(char *unixpath)
189} 181}
190 182
191 183
192#ifndef WINDOWS
193/** 184/**
194 * If services crash, they can leave a unix domain socket file on the 185 * If services crash, they can leave a unix domain socket file on the
195 * disk. This needs to be manually removed, because otherwise both 186 * disk. This needs to be manually removed, because otherwise both
@@ -238,8 +229,6 @@ GNUNET_NETWORK_unix_precheck(const struct sockaddr_un *un)
238 "unlink", 229 "unlink",
239 un->sun_path); 230 un->sun_path);
240} 231}
241#endif
242
243 232
244 233
245#ifndef FD_COPY 234#ifndef FD_COPY
@@ -258,24 +247,6 @@ int
258GNUNET_NETWORK_socket_set_blocking(struct GNUNET_NETWORK_Handle *fd, 247GNUNET_NETWORK_socket_set_blocking(struct GNUNET_NETWORK_Handle *fd,
259 int doBlock) 248 int doBlock)
260{ 249{
261#if MINGW
262 u_long mode;
263
264 mode = !doBlock;
265 if (SOCKET_ERROR ==
266 ioctlsocket(fd->fd,
267 FIONBIO,
268 &mode))
269
270 {
271 SetErrnoFromWinsockError(WSAGetLastError());
272 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING,
273 "ioctlsocket");
274 return GNUNET_SYSERR;
275 }
276 return GNUNET_OK;
277#else
278 /* not MINGW */
279 int flags = fcntl(fd->fd, F_GETFL); 250 int flags = fcntl(fd->fd, F_GETFL);
280 251
281 if (flags == -1) 252 if (flags == -1)
@@ -299,7 +270,6 @@ GNUNET_NETWORK_socket_set_blocking(struct GNUNET_NETWORK_Handle *fd,
299 return GNUNET_SYSERR; 270 return GNUNET_SYSERR;
300 } 271 }
301 return GNUNET_OK; 272 return GNUNET_OK;
302#endif
303} 273}
304 274
305 275
@@ -313,7 +283,6 @@ GNUNET_NETWORK_socket_set_blocking(struct GNUNET_NETWORK_Handle *fd,
313static int 283static int
314socket_set_inheritable(const struct GNUNET_NETWORK_Handle *h) 284socket_set_inheritable(const struct GNUNET_NETWORK_Handle *h)
315{ 285{
316#ifndef MINGW
317 int i; 286 int i;
318 i = fcntl(h->fd, F_GETFD); 287 i = fcntl(h->fd, F_GETFD);
319 if (i < 0) 288 if (i < 0)
@@ -323,16 +292,7 @@ socket_set_inheritable(const struct GNUNET_NETWORK_Handle *h)
323 i |= FD_CLOEXEC; 292 i |= FD_CLOEXEC;
324 if (fcntl(h->fd, F_SETFD, i) < 0) 293 if (fcntl(h->fd, F_SETFD, i) < 0)
325 return GNUNET_SYSERR; 294 return GNUNET_SYSERR;
326#else 295
327 BOOL b;
328 SetLastError(0);
329 b = SetHandleInformation((HANDLE)h->fd, HANDLE_FLAG_INHERIT, 0);
330 if (!b)
331 {
332 SetErrnoFromWinsockError(WSAGetLastError());
333 return GNUNET_SYSERR;
334 }
335#endif
336 return GNUNET_OK; 296 return GNUNET_OK;
337} 297}
338 298
@@ -368,7 +328,6 @@ socket_set_nosigpipe(const struct GNUNET_NETWORK_Handle *h)
368static void 328static void
369socket_set_nodelay(const struct GNUNET_NETWORK_Handle *h) 329socket_set_nodelay(const struct GNUNET_NETWORK_Handle *h)
370{ 330{
371#ifndef WINDOWS
372 int value = 1; 331 int value = 1;
373 332
374 if (0 != 333 if (0 !=
@@ -378,18 +337,6 @@ socket_set_nodelay(const struct GNUNET_NETWORK_Handle *h)
378 &value, sizeof(value))) 337 &value, sizeof(value)))
379 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, 338 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING,
380 "setsockopt"); 339 "setsockopt");
381#else
382 const char *abs_value = "1";
383
384 if (0 !=
385 setsockopt(h->fd,
386 IPPROTO_TCP,
387 TCP_NODELAY,
388 (const void *)abs_value,
389 sizeof(abs_value)))
390 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING,
391 "setsockopt");
392#endif
393} 340}
394 341
395 342
@@ -416,22 +363,19 @@ initialize_network_handle(struct GNUNET_NETWORK_Handle *h,
416 h->type = type; 363 h->type = type;
417 if (h->fd == INVALID_SOCKET) 364 if (h->fd == INVALID_SOCKET)
418 { 365 {
419#ifdef MINGW
420 SetErrnoFromWinsockError(WSAGetLastError());
421#endif
422 eno = errno; 366 eno = errno;
423 GNUNET_free(h); 367 GNUNET_free(h);
424 errno = eno; 368 errno = eno;
425 return GNUNET_SYSERR; 369 return GNUNET_SYSERR;
426 } 370 }
427#ifndef MINGW 371
428 if (h->fd >= FD_SETSIZE) 372 if (h->fd >= FD_SETSIZE)
429 { 373 {
430 GNUNET_break(GNUNET_OK == GNUNET_NETWORK_socket_close(h)); 374 GNUNET_break(GNUNET_OK == GNUNET_NETWORK_socket_close(h));
431 errno = EMFILE; 375 errno = EMFILE;
432 return GNUNET_SYSERR; 376 return GNUNET_SYSERR;
433 } 377 }
434#endif 378
435 if (GNUNET_OK != socket_set_inheritable(h)) 379 if (GNUNET_OK != socket_set_inheritable(h))
436 LOG_STRERROR(GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 380 LOG_STRERROR(GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
437 "socket_set_inheritable"); 381 "socket_set_inheritable");
@@ -549,7 +493,6 @@ GNUNET_NETWORK_socket_bind(struct GNUNET_NETWORK_Handle *desc,
549 } 493 }
550#endif 494#endif
551#endif 495#endif
552#ifndef WINDOWS
553 if (AF_UNIX == address->sa_family) 496 if (AF_UNIX == address->sa_family)
554 GNUNET_NETWORK_unix_precheck((const struct sockaddr_un *)address); 497 GNUNET_NETWORK_unix_precheck((const struct sockaddr_un *)address);
555 { 498 {
@@ -576,28 +519,21 @@ GNUNET_NETWORK_socket_bind(struct GNUNET_NETWORK_Handle *desc,
576 not_abstract = 1; 519 not_abstract = 1;
577 if (not_abstract) 520 if (not_abstract)
578 old_mask = umask(S_IWGRP | S_IRGRP | S_IXGRP | S_IWOTH | S_IROTH | S_IXOTH); 521 old_mask = umask(S_IWGRP | S_IRGRP | S_IXGRP | S_IWOTH | S_IROTH | S_IXOTH);
579#endif
580 522
581 ret = bind(desc->fd, 523 ret = bind(desc->fd,
582 address, 524 address,
583 address_len); 525 address_len);
584 526
585#ifndef WINDOWS
586 if (not_abstract) 527 if (not_abstract)
587 (void)umask(old_mask); 528 (void)umask(old_mask);
588} 529}
589#endif
590#ifdef MINGW
591 if (SOCKET_ERROR == ret)
592 SetErrnoFromWinsockError(WSAGetLastError());
593#endif
594 if (0 != ret) 530 if (0 != ret)
595 return GNUNET_SYSERR; 531 return GNUNET_SYSERR;
596#ifndef MINGW 532
597 desc->addr = GNUNET_malloc(address_len); 533 desc->addr = GNUNET_malloc(address_len);
598 GNUNET_memcpy(desc->addr, address, address_len); 534 GNUNET_memcpy(desc->addr, address, address_len);
599 desc->addrlen = address_len; 535 desc->addrlen = address_len;
600#endif 536
601 return GNUNET_OK; 537 return GNUNET_OK;
602} 538}
603 539
@@ -613,22 +549,8 @@ GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc)
613{ 549{
614 int ret; 550 int ret;
615 551
616#ifdef WINDOWS
617 DWORD error = 0;
618
619 SetLastError(0);
620 ret = closesocket(desc->fd);
621 error = WSAGetLastError();
622 SetErrnoFromWinsockError(error);
623 LOG(GNUNET_ERROR_TYPE_DEBUG,
624 "Closed 0x%x, closesocket() returned %d, GLE is %u\n",
625 desc->fd,
626 ret,
627 error);
628#else
629 ret = close(desc->fd); 552 ret = close(desc->fd);
630#endif 553
631#ifndef WINDOWS
632 const struct sockaddr_un *un = (const struct sockaddr_un *)desc->addr; 554 const struct sockaddr_un *un = (const struct sockaddr_un *)desc->addr;
633 555
634 /* Cleanup the UNIX domain socket and its parent directories in case of non 556 /* Cleanup the UNIX domain socket and its parent directories in case of non
@@ -674,7 +596,6 @@ GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc)
674 } 596 }
675 GNUNET_free(dirname); 597 GNUNET_free(dirname);
676 } 598 }
677#endif
678 GNUNET_NETWORK_socket_free_memory_only_(desc); 599 GNUNET_NETWORK_socket_free_memory_only_(desc);
679 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR; 600 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
680} 601}
@@ -704,28 +625,12 @@ GNUNET_NETWORK_socket_box_native(SOCKTYPE fd)
704{ 625{
705 struct GNUNET_NETWORK_Handle *ret; 626 struct GNUNET_NETWORK_Handle *ret;
706 627
707#if MINGW
708 unsigned long i;
709 DWORD d;
710 /* FIXME: Find a better call to check that FD is valid */
711 if (0 !=
712 WSAIoctl(fd, FIONBIO,
713 (void *)&i, sizeof(i),
714 NULL, 0, &d,
715 NULL, NULL))
716 return NULL; /* invalid FD */
717 ret = GNUNET_new(struct GNUNET_NETWORK_Handle);
718 ret->fd = fd;
719 ret->af = AF_UNSPEC;
720 return ret;
721#else
722 if (fcntl(fd, F_GETFD) < 0) 628 if (fcntl(fd, F_GETFD) < 0)
723 return NULL; /* invalid FD */ 629 return NULL; /* invalid FD */
724 ret = GNUNET_new(struct GNUNET_NETWORK_Handle); 630 ret = GNUNET_new(struct GNUNET_NETWORK_Handle);
725 ret->fd = fd; 631 ret->fd = fd;
726 ret->af = AF_UNSPEC; 632 ret->af = AF_UNSPEC;
727 return ret; 633 return ret;
728#endif
729} 634}
730 635
731 636
@@ -747,14 +652,7 @@ GNUNET_NETWORK_socket_connect(const struct GNUNET_NETWORK_Handle *desc,
747 ret = connect(desc->fd, 652 ret = connect(desc->fd,
748 address, 653 address,
749 address_len); 654 address_len);
750#ifdef MINGW 655
751 if (SOCKET_ERROR == ret)
752 {
753 SetErrnoFromWinsockError(WSAGetLastError());
754 if (errno == EWOULDBLOCK)
755 errno = EINPROGRESS;
756 }
757#endif
758 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR; 656 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
759} 657}
760 658
@@ -783,14 +681,6 @@ GNUNET_NETWORK_socket_getsockopt(const struct GNUNET_NETWORK_Handle *desc,
783 optname, 681 optname,
784 optval, optlen); 682 optval, optlen);
785 683
786#ifdef MINGW
787 if ((0 == ret) &&
788 (SOL_SOCKET == level) &&
789 (SO_ERROR == optname))
790 *((int *)optval) = GetErrnoFromWinsockError(*((int *)optval));
791 else if (SOCKET_ERROR == ret)
792 SetErrnoFromWinsockError(WSAGetLastError());
793#endif
794 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR; 684 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
795} 685}
796 686
@@ -810,10 +700,7 @@ GNUNET_NETWORK_socket_listen(const struct GNUNET_NETWORK_Handle *desc,
810 700
811 ret = listen(desc->fd, 701 ret = listen(desc->fd,
812 backlog); 702 backlog);
813#ifdef MINGW 703
814 if (SOCKET_ERROR == ret)
815 SetErrnoFromWinsockError(WSAGetLastError());
816#endif
817 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR; 704 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
818} 705}
819 706
@@ -830,7 +717,6 @@ GNUNET_NETWORK_socket_recvfrom_amount(const struct GNUNET_NETWORK_Handle *desc)
830 int error; 717 int error;
831 718
832 /* How much is there to be read? */ 719 /* How much is there to be read? */
833#ifndef WINDOWS
834 int pending; 720 int pending;
835 721
836 error = ioctl(desc->fd, 722 error = ioctl(desc->fd,
@@ -839,16 +725,6 @@ GNUNET_NETWORK_socket_recvfrom_amount(const struct GNUNET_NETWORK_Handle *desc)
839 if (0 == error) 725 if (0 == error)
840 return (ssize_t)pending; 726 return (ssize_t)pending;
841 return GNUNET_SYSERR; 727 return GNUNET_SYSERR;
842#else
843 u_long pending;
844
845 error = ioctlsocket(desc->fd,
846 FIONREAD,
847 &pending);
848 if (error != SOCKET_ERROR)
849 return (ssize_t)pending;
850 return GNUNET_SYSERR;
851#endif
852} 728}
853 729
854 730
@@ -1096,28 +972,7 @@ GNUNET_NETWORK_socket_disable_corking(struct GNUNET_NETWORK_Handle *desc)
1096{ 972{
1097 int ret = 0; 973 int ret = 0;
1098 974
1099#if WINDOWS 975#if LINUX
1100 int value = 0;
1101
1102 if (0 !=
1103 (ret =
1104 setsockopt(desc->fd,
1105 SOL_SOCKET,
1106 SO_SNDBUF,
1107 (char *)&value,
1108 sizeof(value))))
1109 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING,
1110 "setsockopt");
1111 if (0 !=
1112 (ret =
1113 setsockopt(desc->fd,
1114 SOL_SOCKET,
1115 SO_RCVBUF,
1116 (char *)&value,
1117 sizeof(value))))
1118 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING,
1119 "setsockopt");
1120#elif LINUX
1121 int value = 0; 976 int value = 0;
1122 977
1123 if (0 != 978 if (0 !=
@@ -1153,9 +1008,6 @@ GNUNET_NETWORK_fdset_zero(struct GNUNET_NETWORK_FDSet *fds)
1153{ 1008{
1154 FD_ZERO(&fds->sds); 1009 FD_ZERO(&fds->sds);
1155 fds->nsds = 0; 1010 fds->nsds = 0;
1156#ifdef MINGW
1157 fds->handles_pos = 0;
1158#endif
1159} 1011}
1160 1012
1161 1013