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.c1333
1 files changed, 0 insertions, 1333 deletions
diff --git a/src/util/network.c b/src/util/network.c
deleted file mode 100644
index 688c37665..000000000
--- a/src/util/network.c
+++ /dev/null
@@ -1,1333 +0,0 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2009-2013 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file util/network.c
23 * @brief basic, low-level networking interface
24 * @author Nils Durner
25 * @author Christian Grothoff
26 */
27#include "platform.h"
28#include "gnunet_util_lib.h"
29#include "disk.h"
30
31#define LOG(kind, ...) GNUNET_log_from (kind, "util-network", __VA_ARGS__)
32#define LOG_STRERROR_FILE(kind, syscall, \
33 filename) GNUNET_log_from_strerror_file (kind, \
34 "util-network", \
35 syscall, \
36 filename)
37#define LOG_STRERROR(kind, syscall) GNUNET_log_from_strerror (kind, \
38 "util-network", \
39 syscall)
40
41#define DEBUG_NETWORK GNUNET_EXTRA_LOGGING
42
43
44#ifndef INVALID_SOCKET
45#define INVALID_SOCKET -1
46#endif
47
48
49/**
50 * @brief handle to a socket
51 */
52struct GNUNET_NETWORK_Handle
53{
54 int fd;
55
56 /**
57 * Address family / domain.
58 */
59 int af;
60
61 /**
62 * Type of the socket
63 */
64 int type;
65
66 /**
67 * Number of bytes in addr.
68 */
69 socklen_t addrlen;
70
71 /**
72 * Address we were bound to, or NULL.
73 */
74 struct sockaddr *addr;
75};
76
77
78enum GNUNET_GenericReturnValue
79GNUNET_NETWORK_test_pf (int pf)
80{
81 static int cache_v4 = -1;
82 static int cache_v6 = -1;
83 static int cache_un = -1;
84 int s;
85 int ret;
86
87 switch (pf)
88 {
89 case PF_INET:
90 if (-1 != cache_v4)
91 return cache_v4;
92 break;
93
94 case PF_INET6:
95 if (-1 != cache_v6)
96 return cache_v6;
97 break;
98
99#ifdef PF_UNIX
100 case PF_UNIX:
101 if (-1 != cache_un)
102 return cache_un;
103 break;
104#endif
105 }
106 s = socket (pf, SOCK_STREAM, 0);
107 if (-1 == s)
108 {
109 if (EAFNOSUPPORT != errno)
110 {
111 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
112 "socket");
113 return GNUNET_SYSERR;
114 }
115 ret = GNUNET_NO;
116 }
117 else
118 {
119 GNUNET_break (0 == close (s));
120 ret = GNUNET_OK;
121 }
122 switch (pf)
123 {
124 case PF_INET:
125 cache_v4 = ret;
126 break;
127
128 case PF_INET6:
129 cache_v6 = ret;
130 break;
131
132#ifdef PF_UNIX
133 case PF_UNIX:
134 cache_un = ret;
135 break;
136#endif
137 }
138 return ret;
139}
140
141
142char *
143GNUNET_NETWORK_shorten_unixpath (char *unixpath)
144{
145 struct sockaddr_un dummy;
146 size_t slen;
147 char *end;
148 struct GNUNET_HashCode sh;
149 struct GNUNET_CRYPTO_HashAsciiEncoded ae;
150 size_t upm;
151
152 upm = sizeof(dummy.sun_path);
153 slen = strlen (unixpath);
154 if (slen < upm)
155 return unixpath; /* no shortening required */
156 GNUNET_CRYPTO_hash (unixpath, slen, &sh);
157 while (16 + strlen (unixpath) >= upm)
158 {
159 if (NULL == (end = strrchr (unixpath, '/')))
160 {
161 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
162 _ (
163 "Unable to shorten unix path `%s' while keeping name unique\n"),
164 unixpath);
165 GNUNET_free (unixpath);
166 return NULL;
167 }
168 *end = '\0';
169 }
170 GNUNET_CRYPTO_hash_to_enc (&sh, &ae);
171 ae.encoding[16] = '\0';
172 strcat (unixpath, (char *) ae.encoding);
173 return unixpath;
174}
175
176
177void
178GNUNET_NETWORK_unix_precheck (const struct sockaddr_un *un)
179{
180 int s;
181 int eno;
182 struct stat sbuf;
183 int ret;
184
185 s = socket (AF_UNIX, SOCK_STREAM, 0);
186 if (-1 == s)
187 {
188 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
189 "Failed to open AF_UNIX socket");
190 return;
191 }
192 ret = connect (s,
193 (struct sockaddr *) un,
194 sizeof(struct sockaddr_un));
195 eno = errno;
196 GNUNET_break (0 == close (s));
197 if (0 == ret)
198 return; /* another process is listening, do not remove! */
199 if (ECONNREFUSED != eno)
200 return; /* some other error, likely "no such file or directory" -- all well */
201 /* should unlink, but sanity checks first */
202 if (0 != stat (un->sun_path,
203 &sbuf))
204 return; /* failed to 'stat', likely does not exist after all */
205 if (S_IFSOCK != (S_IFMT & sbuf.st_mode))
206 return; /* refuse to unlink anything except sockets */
207 /* finally, really unlink */
208 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
209 "Removing left-over `%s' from previous exeuction\n",
210 un->sun_path);
211 if (0 != unlink (un->sun_path))
212 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
213 "unlink",
214 un->sun_path);
215}
216
217
218#ifndef FD_COPY
219#define FD_COPY(s, d) do { GNUNET_memcpy ((d), (s), sizeof(fd_set)); } while (0)
220#endif
221
222
223enum GNUNET_GenericReturnValue
224GNUNET_NETWORK_socket_set_blocking (struct GNUNET_NETWORK_Handle *fd,
225 int doBlock)
226{
227 int flags = fcntl (fd->fd, F_GETFL);
228
229 if (flags == -1)
230 {
231 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
232 "fcntl");
233 return GNUNET_SYSERR;
234 }
235 if (doBlock)
236 flags &= ~O_NONBLOCK;
237
238 else
239 flags |= O_NONBLOCK;
240 if (0 != fcntl (fd->fd,
241 F_SETFL,
242 flags))
243
244 {
245 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
246 "fcntl");
247 return GNUNET_SYSERR;
248 }
249 return GNUNET_OK;
250}
251
252
253/**
254 * Make a socket non-inheritable to child processes
255 *
256 * @param h the socket to make non-inheritable
257 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
258 * @warning Not implemented on Windows
259 */
260static int
261socket_set_inheritable (const struct GNUNET_NETWORK_Handle *h)
262{
263 int i;
264 i = fcntl (h->fd, F_GETFD);
265 if (i < 0)
266 return GNUNET_SYSERR;
267 if (i == (i | FD_CLOEXEC))
268 return GNUNET_OK;
269 i |= FD_CLOEXEC;
270 if (fcntl (h->fd, F_SETFD, i) < 0)
271 return GNUNET_SYSERR;
272
273 return GNUNET_OK;
274}
275
276
277#ifdef DARWIN
278/**
279 * The MSG_NOSIGNAL equivalent on Mac OS X
280 *
281 * @param h the socket to make non-delaying
282 */
283static int
284socket_set_nosigpipe (const struct GNUNET_NETWORK_Handle *h)
285{
286 int abs_value = 1;
287
288 if (0 !=
289 setsockopt (h->fd, SOL_SOCKET, SO_NOSIGPIPE,
290 (const void *) &abs_value,
291 sizeof(abs_value)))
292 return GNUNET_SYSERR;
293 return GNUNET_OK;
294}
295
296
297#endif
298
299
300/**
301 * Disable delays when sending data via the socket.
302 * (GNUnet makes sure that messages are as big as
303 * possible already).
304 *
305 * @param h the socket to make non-delaying
306 */
307static void
308socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h)
309{
310 int value = 1;
311
312 if (0 !=
313 setsockopt (h->fd,
314 IPPROTO_TCP,
315 TCP_NODELAY,
316 &value, sizeof(value)))
317 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
318 "setsockopt");
319}
320
321
322/**
323 * Perform proper canonical initialization for a network handle.
324 * Set it to non-blocking, make it non-inheritable to child
325 * processes, disable SIGPIPE, enable "nodelay" (if non-UNIX
326 * stream socket) and check that it is smaller than FD_SETSIZE.
327 *
328 * @param h socket to initialize
329 * @param af address family of the socket
330 * @param type socket type
331 * @return #GNUNET_OK on success, #GNUNET_SYSERR if initialization
332 * failed and the handle was destroyed
333 */
334static int
335initialize_network_handle (struct GNUNET_NETWORK_Handle *h,
336 int af,
337 int type)
338{
339 int eno;
340
341 h->af = af;
342 h->type = type;
343 if (h->fd == INVALID_SOCKET)
344 {
345 eno = errno;
346 GNUNET_free (h);
347 errno = eno;
348 return GNUNET_SYSERR;
349 }
350
351 if (h->fd >= FD_SETSIZE)
352 {
353 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (h));
354 errno = EMFILE;
355 return GNUNET_SYSERR;
356 }
357
358 if (GNUNET_OK != socket_set_inheritable (h))
359 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
360 "socket_set_inheritable");
361
362 if (GNUNET_SYSERR == GNUNET_NETWORK_socket_set_blocking (h, GNUNET_NO))
363 {
364 eno = errno;
365 GNUNET_break (0);
366 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (h));
367 errno = eno;
368 return GNUNET_SYSERR;
369 }
370#ifdef DARWIN
371 if (GNUNET_SYSERR == socket_set_nosigpipe (h))
372 {
373 eno = errno;
374 GNUNET_break (0);
375 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (h));
376 errno = eno;
377 return GNUNET_SYSERR;
378 }
379#endif
380 if ((type == SOCK_STREAM)
381#ifdef AF_UNIX
382 && (af != AF_UNIX)
383#endif
384 )
385 socket_set_nodelay (h);
386 return GNUNET_OK;
387}
388
389
390struct GNUNET_NETWORK_Handle *
391GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
392 struct sockaddr *address,
393 socklen_t *address_len)
394{
395 struct GNUNET_NETWORK_Handle *ret;
396 int eno;
397
398 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
399#if DEBUG_NETWORK
400 {
401 struct sockaddr_storage name;
402 socklen_t namelen = sizeof(name);
403
404 int gsn = getsockname (desc->fd,
405 (struct sockaddr *) &name,
406 &namelen);
407
408 if (0 == gsn)
409 LOG (GNUNET_ERROR_TYPE_DEBUG,
410 "Accepting connection on `%s'\n",
411 GNUNET_a2s ((const struct sockaddr *) &name,
412 namelen));
413 }
414#endif
415 ret->fd = accept (desc->fd,
416 address,
417 address_len);
418 if (-1 == ret->fd)
419 {
420 eno = errno;
421 GNUNET_free (ret);
422 errno = eno;
423 return NULL;
424 }
425 if (GNUNET_OK !=
426 initialize_network_handle (ret,
427 (NULL != address) ? address->sa_family :
428 desc->af,
429 SOCK_STREAM))
430 {
431 return NULL;
432 }
433 return ret;
434}
435
436
437enum GNUNET_GenericReturnValue
438GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
439 const struct sockaddr *address,
440 socklen_t address_len)
441{
442 int ret;
443
444#ifdef IPV6_V6ONLY
445#ifdef IPPROTO_IPV6
446 {
447 const int on = 1;
448
449 if (AF_INET6 == desc->af)
450 if (setsockopt (desc->fd,
451 IPPROTO_IPV6,
452 IPV6_V6ONLY,
453 (const void *) &on,
454 sizeof(on)))
455 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG,
456 "setsockopt");
457 }
458#endif
459#endif
460 if (AF_UNIX == address->sa_family)
461 GNUNET_NETWORK_unix_precheck ((const struct sockaddr_un *) address);
462
463 {
464 const int on = 1;
465
466 if ( (SOCK_STREAM == desc->type) &&
467 (0 != setsockopt (desc->fd,
468 SOL_SOCKET,
469 SO_REUSEADDR,
470 &on, sizeof(on))) )
471 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG,
472 "setsockopt");
473 }
474 {
475 /* set permissions of newly created non-abstract UNIX domain socket to
476 "user-only"; applications can choose to relax this later */
477 mode_t old_mask = 0; /* assigned to make compiler happy */
478 const struct sockaddr_un *un = (const struct sockaddr_un *) address;
479 int not_abstract = 0;
480
481 if ((AF_UNIX == address->sa_family)
482 && ('\0' != un->sun_path[0])) /* Not an abstract socket */
483 not_abstract = 1;
484 if (not_abstract)
485 old_mask = umask (S_IWGRP | S_IRGRP | S_IXGRP | S_IWOTH | S_IROTH
486 | S_IXOTH);
487
488 ret = bind (desc->fd,
489 address,
490 address_len);
491
492 if (not_abstract)
493 (void) umask (old_mask);
494 }
495 if (0 != ret)
496 return GNUNET_SYSERR;
497
498 desc->addr = GNUNET_malloc (address_len);
499 GNUNET_memcpy (desc->addr, address, address_len);
500 desc->addrlen = address_len;
501
502 return GNUNET_OK;
503}
504
505
506enum GNUNET_GenericReturnValue
507GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
508{
509 int ret;
510
511 ret = close (desc->fd);
512
513 const struct sockaddr_un *un = (const struct sockaddr_un *) desc->addr;
514
515 /* Cleanup the UNIX domain socket and its parent directories in case of non
516 abstract sockets */
517 if ((AF_UNIX == desc->af) &&
518 (NULL != desc->addr) &&
519 ('\0' != un->sun_path[0]))
520 {
521 char *dirname = GNUNET_strndup (un->sun_path,
522 sizeof(un->sun_path));
523
524 if (0 != unlink (dirname))
525 {
526 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
527 "unlink",
528 dirname);
529 }
530 else
531 {
532 size_t len;
533
534 len = strlen (dirname);
535 while ((len > 0) && (dirname[len] != DIR_SEPARATOR))
536 len--;
537 dirname[len] = '\0';
538 if ((0 != len) && (0 != rmdir (dirname)))
539 {
540 switch (errno)
541 {
542 case EACCES:
543 case ENOTEMPTY:
544 case EPERM:
545 /* these are normal and can just be ignored */
546 break;
547
548 default:
549 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
550 "rmdir",
551 dirname);
552 break;
553 }
554 }
555 }
556 GNUNET_free (dirname);
557 }
558 GNUNET_NETWORK_socket_free_memory_only_ (desc);
559 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
560}
561
562
563/**
564 * Only free memory of a socket, keep the file descriptor untouched.
565 *
566 * @param desc socket
567 */
568void
569GNUNET_NETWORK_socket_free_memory_only_ (struct GNUNET_NETWORK_Handle *desc)
570{
571 GNUNET_free (desc->addr);
572 GNUNET_free (desc);
573}
574
575
576/**
577 * Box a native socket (and check that it is a socket).
578 *
579 * @param fd socket to box
580 * @return NULL on error (including not supported on target platform)
581 */
582struct GNUNET_NETWORK_Handle *
583GNUNET_NETWORK_socket_box_native (int fd)
584{
585 struct GNUNET_NETWORK_Handle *ret;
586
587 if (fcntl (fd, F_GETFD) < 0)
588 return NULL; /* invalid FD */
589 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
590 ret->fd = fd;
591 ret->af = AF_UNSPEC;
592 return ret;
593}
594
595
596/**
597 * Connect a socket to some remote address.
598 *
599 * @param desc socket
600 * @param address peer address
601 * @param address_len length of @a address
602 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
603 */
604int
605GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
606 const struct sockaddr *address,
607 socklen_t address_len)
608{
609 int ret;
610
611 ret = connect (desc->fd,
612 address,
613 address_len);
614
615 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
616}
617
618
619/**
620 * Get socket options
621 *
622 * @param desc socket
623 * @param level protocol level of the option
624 * @param optname identifier of the option
625 * @param optval options
626 * @param optlen length of @a optval
627 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
628 */
629int
630GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
631 int level,
632 int optname,
633 void *optval,
634 socklen_t *optlen)
635{
636 int ret;
637
638 ret = getsockopt (desc->fd,
639 level,
640 optname,
641 optval, optlen);
642
643 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
644}
645
646
647/**
648 * Listen on a socket
649 *
650 * @param desc socket
651 * @param backlog length of the listen queue
652 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
653 */
654int
655GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
656 int backlog)
657{
658 int ret;
659
660 ret = listen (desc->fd,
661 backlog);
662
663 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
664}
665
666
667/**
668 * How much data is available to be read on this descriptor?
669 *
670 * @param desc socket
671 * @returns #GNUNET_SYSERR if no data is available, or on error!
672 */
673ssize_t
674GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle *desc)
675{
676 int error;
677
678 /* How much is there to be read? */
679 int pending;
680
681 error = ioctl (desc->fd,
682 FIONREAD,
683 &pending);
684 if (0 == error)
685 return (ssize_t) pending;
686 return GNUNET_SYSERR;
687}
688
689
690/**
691 * Read data from a socket (always non-blocking).
692 *
693 * @param desc socket
694 * @param buffer buffer
695 * @param length length of @a buffer
696 * @param src_addr either the source to recv from, or all zeroes
697 * to be filled in by recvfrom
698 * @param addrlen length of the @a src_addr
699 */
700ssize_t
701GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle *desc,
702 void *buffer,
703 size_t length,
704 struct sockaddr *src_addr,
705 socklen_t *addrlen)
706{
707 int flags = 0;
708
709#ifdef MSG_DONTWAIT
710 flags |= MSG_DONTWAIT;
711#endif
712 return recvfrom (desc->fd,
713 buffer,
714 length,
715 flags,
716 src_addr,
717 addrlen);
718}
719
720
721/**
722 * Read data from a connected socket (always non-blocking).
723 *
724 * @param desc socket
725 * @param buffer buffer
726 * @param length length of @a buffer
727 * @return number of bytes received, -1 on error
728 */
729ssize_t
730GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle *desc,
731 void *buffer,
732 size_t length)
733{
734 int ret;
735 int flags;
736
737 flags = 0;
738
739#ifdef MSG_DONTWAIT
740 flags |= MSG_DONTWAIT;
741#endif
742 ret = recv (desc->fd,
743 buffer,
744 length,
745 flags);
746 return ret;
747}
748
749
750/**
751 * Send data (always non-blocking).
752 *
753 * @param desc socket
754 * @param buffer data to send
755 * @param length size of the @a buffer
756 * @return number of bytes sent, #GNUNET_SYSERR on error
757 */
758ssize_t
759GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle *desc,
760 const void *buffer,
761 size_t length)
762{
763 int ret;
764 int flags;
765
766 flags = 0;
767#ifdef MSG_DONTWAIT
768 flags |= MSG_DONTWAIT;
769#endif
770#ifdef MSG_NOSIGNAL
771 flags |= MSG_NOSIGNAL;
772#endif
773 ret = send (desc->fd,
774 buffer,
775 length,
776 flags);
777 return ret;
778}
779
780
781/**
782 * Send data to a particular destination (always non-blocking).
783 * This function only works for UDP sockets.
784 *
785 * @param desc socket
786 * @param message data to send
787 * @param length size of the @a message
788 * @param dest_addr destination address
789 * @param dest_len length of @a address
790 * @return number of bytes sent, #GNUNET_SYSERR on error
791 */
792ssize_t
793GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle *desc,
794 const void *message,
795 size_t length,
796 const struct sockaddr *dest_addr,
797 socklen_t dest_len)
798{
799 int flags = 0;
800
801#ifdef MSG_DONTWAIT
802 flags |= MSG_DONTWAIT;
803#endif
804#ifdef MSG_NOSIGNAL
805 flags |= MSG_NOSIGNAL;
806#endif
807 return sendto (desc->fd,
808 message,
809 length,
810 flags,
811 dest_addr,
812 dest_len);
813}
814
815
816/**
817 * Set socket option
818 *
819 * @param fd socket
820 * @param level protocol level of the option
821 * @param option_name option identifier
822 * @param option_value value to set
823 * @param option_len size of @a option_value
824 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
825 */
826int
827GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,
828 int level,
829 int option_name,
830 const void *option_value,
831 socklen_t option_len)
832{
833 return (0 == setsockopt (fd->fd,
834 level,
835 option_name,
836 option_value,
837 option_len))
838 ? GNUNET_OK
839 : GNUNET_SYSERR;
840}
841
842
843/**
844 * Create a new socket. Configure it for non-blocking IO and
845 * mark it as non-inheritable to child processes (set the
846 * close-on-exec flag).
847 *
848 * @param domain domain of the socket
849 * @param type socket type
850 * @param protocol network protocol
851 * @return new socket, NULL on error
852 */
853struct GNUNET_NETWORK_Handle *
854GNUNET_NETWORK_socket_create (int domain,
855 int type,
856 int protocol)
857{
858 struct GNUNET_NETWORK_Handle *ret;
859 int fd;
860
861 fd = socket (domain, type, protocol);
862 if (-1 == fd)
863 return NULL;
864 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
865 ret->fd = fd;
866 if (GNUNET_OK !=
867 initialize_network_handle (ret,
868 domain,
869 type))
870 return NULL;
871 return ret;
872}
873
874
875/**
876 * Shut down socket operations
877 * @param desc socket
878 * @param how type of shutdown
879 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
880 */
881int
882GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc,
883 int how)
884{
885 int ret;
886
887 ret = shutdown (desc->fd, how);
888
889 return (0 == ret) ? GNUNET_OK : GNUNET_SYSERR;
890}
891
892
893/**
894 * Disable the "CORK" feature for communication with the given socket,
895 * forcing the OS to immediately flush the buffer on transmission
896 * instead of potentially buffering multiple messages. Essentially
897 * reduces the OS send buffers to zero.
898 *
899 * @param desc socket
900 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
901 */
902int
903GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc)
904{
905 int ret = 0;
906
907#ifdef __linux__
908 int value = 0;
909
910 if (0 !=
911 (ret =
912 setsockopt (desc->fd,
913 SOL_SOCKET,
914 SO_SNDBUF,
915 &value,
916 sizeof(value))))
917 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
918 "setsockopt");
919 if (0 !=
920 (ret =
921 setsockopt (desc->fd,
922 SOL_SOCKET,
923 SO_RCVBUF,
924 &value,
925 sizeof(value))))
926 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
927 "setsockopt");
928#endif
929 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
930}
931
932
933/**
934 * Reset FD set
935 *
936 * @param fds fd set
937 */
938void
939GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
940{
941 FD_ZERO (&fds->sds);
942 fds->nsds = 0;
943}
944
945
946/**
947 * Add a socket to the FD set
948 *
949 * @param fds fd set
950 * @param desc socket to add
951 */
952void
953GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
954 const struct GNUNET_NETWORK_Handle *desc)
955{
956 FD_SET (desc->fd,
957 &fds->sds);
958 fds->nsds = GNUNET_MAX (fds->nsds,
959 desc->fd + 1);
960}
961
962
963/**
964 * Check whether a socket is part of the fd set
965 *
966 * @param fds fd set
967 * @param desc socket
968 * @return 0 if the FD is not set
969 */
970int
971GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
972 const struct GNUNET_NETWORK_Handle *desc)
973{
974 return FD_ISSET (desc->fd,
975 &fds->sds);
976}
977
978
979/**
980 * Add one fd set to another
981 *
982 * @param dst the fd set to add to
983 * @param src the fd set to add from
984 */
985void
986GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
987 const struct GNUNET_NETWORK_FDSet *src)
988{
989 int nfds;
990
991 for (nfds = src->nsds; nfds >= 0; nfds--)
992 if (FD_ISSET (nfds, &src->sds))
993 FD_SET (nfds, &dst->sds);
994 dst->nsds = GNUNET_MAX (dst->nsds,
995 src->nsds);
996}
997
998
999/**
1000 * Copy one fd set to another
1001 *
1002 * @param to destination
1003 * @param from source
1004 */
1005void
1006GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
1007 const struct GNUNET_NETWORK_FDSet *from)
1008{
1009 FD_COPY (&from->sds,
1010 &to->sds);
1011 to->nsds = from->nsds;
1012}
1013
1014
1015/**
1016 * Return file descriptor for this network handle
1017 *
1018 * @param desc wrapper to process
1019 * @return POSIX file descriptor
1020 */
1021int
1022GNUNET_NETWORK_get_fd (const struct GNUNET_NETWORK_Handle *desc)
1023{
1024 return desc->fd;
1025}
1026
1027
1028/**
1029 * Return sockaddr for this network handle
1030 *
1031 * @param desc wrapper to process
1032 * @return sockaddr
1033 */
1034struct sockaddr*
1035GNUNET_NETWORK_get_addr (const struct GNUNET_NETWORK_Handle *desc)
1036{
1037 return desc->addr;
1038}
1039
1040
1041/**
1042 * Return sockaddr length for this network handle
1043 *
1044 * @param desc wrapper to process
1045 * @return socklen_t for sockaddr
1046 */
1047socklen_t
1048GNUNET_NETWORK_get_addrlen (const struct GNUNET_NETWORK_Handle *desc)
1049{
1050 return desc->addrlen;
1051}
1052
1053
1054/**
1055 * Copy a native fd set
1056 *
1057 * @param to destination
1058 * @param from native source set
1059 * @param nfds the biggest socket number in from + 1
1060 */
1061void
1062GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
1063 const fd_set *from,
1064 int nfds)
1065{
1066 FD_COPY (from,
1067 &to->sds);
1068 to->nsds = nfds;
1069}
1070
1071
1072/**
1073 * Set a native fd in a set
1074 *
1075 * @param to destination
1076 * @param nfd native FD to set
1077 */
1078void
1079GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to,
1080 int nfd)
1081{
1082 GNUNET_assert ((nfd >= 0) && (nfd < FD_SETSIZE));
1083 FD_SET (nfd, &to->sds);
1084 to->nsds = GNUNET_MAX (nfd + 1,
1085 to->nsds);
1086}
1087
1088
1089/**
1090 * Test native fd in a set
1091 *
1092 * @param to set to test, NULL for empty set
1093 * @param nfd native FD to test, or -1 for none
1094 * @return #GNUNET_YES if FD is set in the set
1095 */
1096int
1097GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
1098 int nfd)
1099{
1100 if ((-1 == nfd) ||
1101 (NULL == to))
1102 return GNUNET_NO;
1103 return FD_ISSET (nfd, &to->sds) ? GNUNET_YES : GNUNET_NO;
1104}
1105
1106
1107/**
1108 * Add a file handle to the fd set
1109 * @param fds fd set
1110 * @param h the file handle to add
1111 */
1112void
1113GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
1114 const struct GNUNET_DISK_FileHandle *h)
1115{
1116 int fd;
1117
1118 GNUNET_assert (GNUNET_OK ==
1119 GNUNET_DISK_internal_file_handle_ (h,
1120 &fd,
1121 sizeof(int)));
1122 FD_SET (fd,
1123 &fds->sds);
1124 fds->nsds = GNUNET_MAX (fd + 1,
1125 fds->nsds);
1126}
1127
1128
1129/**
1130 * Add a file handle to the fd set
1131 * @param fds fd set
1132 * @param h the file handle to add
1133 */
1134void
1135GNUNET_NETWORK_fdset_handle_set_first (struct GNUNET_NETWORK_FDSet *fds,
1136 const struct GNUNET_DISK_FileHandle *h)
1137{
1138 GNUNET_NETWORK_fdset_handle_set (fds, h);
1139}
1140
1141
1142/**
1143 * Check if a file handle is part of an fd set
1144 *
1145 * @param fds fd set
1146 * @param h file handle
1147 * @return #GNUNET_YES if the file handle is part of the set
1148 */
1149int
1150GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
1151 const struct GNUNET_DISK_FileHandle *h)
1152{
1153 return FD_ISSET (h->fd,
1154 &fds->sds);
1155}
1156
1157
1158/**
1159 * Checks if two fd sets overlap
1160 *
1161 * @param fds1 first fd set
1162 * @param fds2 second fd set
1163 * @return #GNUNET_YES if they do overlap, #GNUNET_NO otherwise
1164 */
1165int
1166GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
1167 const struct GNUNET_NETWORK_FDSet *fds2)
1168{
1169 int nfds;
1170
1171 nfds = GNUNET_MIN (fds1->nsds,
1172 fds2->nsds);
1173 while (nfds > 0)
1174 {
1175 nfds--;
1176 if ((FD_ISSET (nfds,
1177 &fds1->sds)) &&
1178 (FD_ISSET (nfds,
1179 &fds2->sds)))
1180 return GNUNET_YES;
1181 }
1182 return GNUNET_NO;
1183}
1184
1185
1186/**
1187 * Creates an fd set
1188 *
1189 * @return a new fd set
1190 */
1191struct GNUNET_NETWORK_FDSet *
1192GNUNET_NETWORK_fdset_create ()
1193{
1194 struct GNUNET_NETWORK_FDSet *fds;
1195
1196 fds = GNUNET_new (struct GNUNET_NETWORK_FDSet);
1197 GNUNET_NETWORK_fdset_zero (fds);
1198 return fds;
1199}
1200
1201
1202/**
1203 * Releases the associated memory of an fd set
1204 *
1205 * @param fds fd set
1206 */
1207void
1208GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
1209{
1210 GNUNET_free (fds);
1211}
1212
1213
1214/**
1215 * Test if the given @a port is available.
1216 *
1217 * @param ipproto transport protocol to test (e.g. IPPROTO_TCP)
1218 * @param port port number to test
1219 * @return #GNUNET_OK if the port is available, #GNUNET_NO if not
1220 */
1221int
1222GNUNET_NETWORK_test_port_free (int ipproto,
1223 uint16_t port)
1224{
1225 struct GNUNET_NETWORK_Handle *socket;
1226 int bind_status;
1227 int socktype;
1228 char open_port_str[6];
1229 struct addrinfo hint;
1230 struct addrinfo *ret;
1231 struct addrinfo *ai;
1232
1233 GNUNET_snprintf (open_port_str,
1234 sizeof(open_port_str),
1235 "%u",
1236 (unsigned int) port);
1237 socktype = (IPPROTO_TCP == ipproto) ? SOCK_STREAM : SOCK_DGRAM;
1238 ret = NULL;
1239 memset (&hint, 0, sizeof(hint));
1240 hint.ai_family = AF_UNSPEC; /* IPv4 and IPv6 */
1241 hint.ai_socktype = socktype;
1242 hint.ai_protocol = ipproto;
1243 hint.ai_addrlen = 0;
1244 hint.ai_addr = NULL;
1245 hint.ai_canonname = NULL;
1246 hint.ai_next = NULL;
1247 hint.ai_flags = AI_PASSIVE | AI_NUMERICSERV; /* Wild card address */
1248 GNUNET_assert (0 == getaddrinfo (NULL,
1249 open_port_str,
1250 &hint,
1251 &ret));
1252 bind_status = GNUNET_NO;
1253 for (ai = ret; NULL != ai; ai = ai->ai_next)
1254 {
1255 socket = GNUNET_NETWORK_socket_create (ai->ai_family,
1256 ai->ai_socktype,
1257 ai->ai_protocol);
1258 if (NULL == socket)
1259 continue;
1260 bind_status = GNUNET_NETWORK_socket_bind (socket,
1261 ai->ai_addr,
1262 ai->ai_addrlen);
1263 GNUNET_NETWORK_socket_close (socket);
1264 if (GNUNET_OK != bind_status)
1265 break;
1266 }
1267 freeaddrinfo (ret);
1268 return bind_status;
1269}
1270
1271
1272/**
1273 * Check if sockets or pipes meet certain conditions
1274 *
1275 * @param rfds set of sockets or pipes to be checked for readability
1276 * @param wfds set of sockets or pipes to be checked for writability
1277 * @param efds set of sockets or pipes to be checked for exceptions
1278 * @param timeout relative value when to return
1279 * @return number of selected sockets or pipes, #GNUNET_SYSERR on error
1280 */
1281int
1282GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1283 struct GNUNET_NETWORK_FDSet *wfds,
1284 struct GNUNET_NETWORK_FDSet *efds,
1285 const struct GNUNET_TIME_Relative timeout)
1286{
1287 int nfds;
1288 struct timeval tv;
1289
1290 if (NULL != rfds)
1291 nfds = rfds->nsds;
1292 else
1293 nfds = 0;
1294 if (NULL != wfds)
1295 nfds = GNUNET_MAX (nfds,
1296 wfds->nsds);
1297 if (NULL != efds)
1298 nfds = GNUNET_MAX (nfds,
1299 efds->nsds);
1300 if ((0 == nfds) &&
1301 (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us))
1302 {
1303 GNUNET_break (0);
1304 LOG (GNUNET_ERROR_TYPE_ERROR,
1305 _ (
1306 "Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1307 "select");
1308 }
1309 if (timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us > (unsigned
1310 long long)
1311 LONG_MAX)
1312 {
1313 tv.tv_sec = LONG_MAX;
1314 tv.tv_usec = 999999L;
1315 }
1316 else
1317 {
1318 tv.tv_sec = (long) (timeout.rel_value_us
1319 / GNUNET_TIME_UNIT_SECONDS.rel_value_us);
1320 tv.tv_usec =
1321 (timeout.rel_value_us
1322 - (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value_us));
1323 }
1324 return select (nfds,
1325 (NULL != rfds) ? &rfds->sds : NULL,
1326 (NULL != wfds) ? &wfds->sds : NULL,
1327 (NULL != efds) ? &efds->sds : NULL,
1328 (timeout.rel_value_us ==
1329 GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) ? NULL : &tv);
1330}
1331
1332
1333/* end of network.c */