GNUnet 0.21.1
Network library

Basic low-level networking interface. More...

Collaboration diagram for Network library:

Data Structures

struct  GNUNET_NETWORK_FDSet
 collection of IO descriptors More...
 

Functions

enum GNUNET_GenericReturnValue GNUNET_NETWORK_test_pf (int pf)
 Test if the given protocol family is supported by this system. More...
 
char * GNUNET_NETWORK_shorten_unixpath (char *unixpath)
 Given a unixpath that is too long (larger than UNIX_PATH_MAX), shorten it to an acceptable length while keeping it unique and making sure it remains a valid filename (if possible). More...
 
void GNUNET_NETWORK_unix_precheck (const struct sockaddr_un *un)
 If services crash, they can leave a unix domain socket file on the disk. More...
 
struct GNUNET_NETWORK_HandleGNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc, struct sockaddr *address, socklen_t *address_len)
 Accept a new connection on a socket. More...
 
struct GNUNET_NETWORK_HandleGNUNET_NETWORK_socket_box_native (int fd)
 Box a native socket (and check that it is a socket). More...
 
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_set_blocking (struct GNUNET_NETWORK_Handle *fd, int doBlock)
 Set if a socket should use blocking or non-blocking IO. More...
 
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc, const struct sockaddr *address, socklen_t address_len)
 Bind a socket to a particular address. More...
 
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
 Close a socket. More...
 
void GNUNET_NETWORK_socket_free_memory_only_ (struct GNUNET_NETWORK_Handle *desc)
 Only free memory of a socket, keep the file descriptor untouched. More...
 
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc, const struct sockaddr *address, socklen_t address_len)
 Connect a socket to some remote address. More...
 
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc, int level, int optname, void *optval, socklen_t *optlen)
 Get socket options. More...
 
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc, int backlog)
 Listen on a socket. More...
 
ssize_t GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle *desc)
 How much data is available to be read on this descriptor? More...
 
ssize_t GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle *desc, void *buffer, size_t length, struct sockaddr *src_addr, socklen_t *addrlen)
 Read data from a socket (always non-blocking). More...
 
ssize_t GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle *desc, void *buffer, size_t length)
 Read data from a connected socket (always non-blocking). More...
 
int GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds, struct GNUNET_NETWORK_FDSet *wfds, struct GNUNET_NETWORK_FDSet *efds, struct GNUNET_TIME_Relative timeout)
 Check if sockets meet certain conditions. More...
 
ssize_t GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle *desc, const void *buffer, size_t length)
 Send data (always non-blocking). More...
 
ssize_t GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle *desc, const void *message, size_t length, const struct sockaddr *dest_addr, socklen_t dest_len)
 Send data to a particular destination (always non-blocking). More...
 
int GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd, int level, int option_name, const void *option_value, socklen_t option_len)
 Set socket option. More...
 
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc, int how)
 Shut down socket operations. More...
 
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc)
 Disable the "CORK" feature for communication with the given socket, forcing the OS to immediately flush the buffer on transmission instead of potentially buffering multiple messages. More...
 
struct GNUNET_NETWORK_HandleGNUNET_NETWORK_socket_create (int domain, int type, int protocol)
 Create a new socket. More...
 
void GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
 Reset FD set (clears all file descriptors). More...
 
void GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds, const struct GNUNET_NETWORK_Handle *desc)
 Add a socket to the FD set. More...
 
int GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds, const struct GNUNET_NETWORK_Handle *desc)
 Check whether a socket is part of the fd set. More...
 
void GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst, const struct GNUNET_NETWORK_FDSet *src)
 Add one fd set to another (computes the union). More...
 
void GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to, const struct GNUNET_NETWORK_FDSet *from)
 Copy one fd set to another. More...
 
int GNUNET_NETWORK_get_fd (const struct GNUNET_NETWORK_Handle *desc)
 Return file descriptor for this network handle. More...
 
struct sockaddr * GNUNET_NETWORK_get_addr (const struct GNUNET_NETWORK_Handle *desc)
 Return the sockaddr for this network handle. More...
 
socklen_t GNUNET_NETWORK_get_addrlen (const struct GNUNET_NETWORK_Handle *desc)
 Return sockaddr length for this network handle. More...
 
void GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to, const fd_set *from, int nfds)
 Copy a native fd set into the GNUnet representation. More...
 
void GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to, int nfd)
 Set a native fd in a set. More...
 
int GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to, int nfd)
 Test native fd in a set. More...
 
void GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds, const struct GNUNET_DISK_FileHandle *h)
 Add a file handle to the fd set. More...
 
void GNUNET_NETWORK_fdset_handle_set_first (struct GNUNET_NETWORK_FDSet *fds, const struct GNUNET_DISK_FileHandle *h)
 Add a file handle to the fd set On W32: ensure that the handle is first in the array. More...
 
int GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds, const struct GNUNET_DISK_FileHandle *h)
 Check if a file handle is part of an fd set. More...
 
int GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1, const struct GNUNET_NETWORK_FDSet *fds2)
 Checks if two fd sets overlap. More...
 
struct GNUNET_NETWORK_FDSetGNUNET_NETWORK_fdset_create (void)
 Creates an fd set. More...
 
void GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
 Releases the associated memory of an fd set. More...
 
int GNUNET_NETWORK_test_port_free (int ipproto, uint16_t port)
 Test if the given port is available. More...
 

Detailed Description

Basic low-level networking interface.

Function Documentation

◆ GNUNET_NETWORK_test_pf()

enum GNUNET_GenericReturnValue GNUNET_NETWORK_test_pf ( int  pf)

Test if the given protocol family is supported by this system.

Parameters
pfprotocol family to test (PF_INET, PF_INET6, PF_UNIX)
Returns
GNUNET_OK if the PF is supported

Definition at line 79 of file network.c.

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 {
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}
static int ret
Final status code.
Definition: gnunet-arm.c:94
@ GNUNET_OK
@ GNUNET_NO
@ GNUNET_SYSERR
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
#define GNUNET_log_strerror(level, cmd)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the mess...
@ GNUNET_ERROR_TYPE_WARNING

References GNUNET_break, GNUNET_ERROR_TYPE_WARNING, GNUNET_log_strerror, GNUNET_NO, GNUNET_OK, GNUNET_SYSERR, and ret.

Referenced by get_server_addresses(), handle_gns_result(), parse_ip_options(), run(), and udp_address_to_sockaddr().

Here is the caller graph for this function:

◆ GNUNET_NETWORK_shorten_unixpath()

char * GNUNET_NETWORK_shorten_unixpath ( char *  unixpath)

Given a unixpath that is too long (larger than UNIX_PATH_MAX), shorten it to an acceptable length while keeping it unique and making sure it remains a valid filename (if possible).

Parameters
unixpathlong path, will be freed (or same pointer returned with moved 0-termination).
Returns
shortened unixpath, NULL on error

Definition at line 143 of file network.c.

144{
145 struct sockaddr_un dummy;
146 size_t slen;
147 char *end;
148 struct GNUNET_HashCode sh;
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 {
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 }
171 ae.encoding[16] = '\0';
172 strcat (unixpath, (char *) ae.encoding);
173 return unixpath;
174}
static int end
Set if we are to shutdown all services (including ARM).
Definition: gnunet-arm.c:34
static struct in_addr dummy
Target "dummy" address of the packet we pretend to respond to.
static struct GNUNET_IDENTITY_Handle * sh
Handle to IDENTITY service.
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:41
void GNUNET_CRYPTO_hash_to_enc(const struct GNUNET_HashCode *block, struct GNUNET_CRYPTO_HashAsciiEncoded *result)
Convert hash to ASCII encoding.
Definition: crypto_hash.c:55
#define GNUNET_log(kind,...)
@ GNUNET_ERROR_TYPE_ERROR
#define GNUNET_free(ptr)
Wrapper around free.
#define _(String)
GNU gettext support macro.
Definition: platform.h:178
0-terminated ASCII encoding of a struct GNUNET_HashCode.
A 512-bit hashcode.

References _, dummy, GNUNET_CRYPTO_HashAsciiEncoded::encoding, end, GNUNET_CRYPTO_hash(), GNUNET_CRYPTO_hash_to_enc(), GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_log, and sh.

Referenced by get_server_addresses(), and try_unixpath().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_NETWORK_unix_precheck()

void GNUNET_NETWORK_unix_precheck ( const struct sockaddr_un *  un)

If services crash, they can leave a unix domain socket file on the disk.

This needs to be manually removed, because otherwise both bind() and connect() for the respective address will fail. In this function, we test if such a left-over file exists, and if so, remove it (unless there is a listening service at the address).

Parameters
ununix domain socket address to check

Definition at line 178 of file network.c.

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 {
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 */
209 "Removing left-over `%s' from previous exeuction\n",
210 un->sun_path);
211 if (0 != unlink (un->sun_path))
213 "unlink",
214 un->sun_path);
215}
#define GNUNET_log_strerror_file(level, cmd, filename)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the mess...
@ GNUNET_ERROR_TYPE_INFO

References GNUNET_break, GNUNET_ERROR_TYPE_INFO, GNUNET_ERROR_TYPE_WARNING, GNUNET_log, GNUNET_log_strerror, GNUNET_log_strerror_file, and ret.

Referenced by create_listen_socket(), and GNUNET_NETWORK_socket_bind().

Here is the caller graph for this function:

◆ GNUNET_NETWORK_socket_accept()

struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_accept ( const struct GNUNET_NETWORK_Handle desc,
struct sockaddr *  address,
socklen_t *  address_len 
)

Accept a new connection on a socket.

Configure it for non-blocking IO and mark it as non-inheritable to child processes (set the close-on-exec flag).

Parameters
descbound socket
addressaddress of the connecting peer, may be NULL
address_lenlength of address
Returns
client socket

Definition at line 392 of file network.c.

395{
397 int eno;
398
400#if DEBUG_NETWORK
401 {
402 struct sockaddr_storage name;
403 socklen_t namelen = sizeof(name);
404
405 int gsn = getsockname (desc->fd,
406 (struct sockaddr *) &name,
407 &namelen);
408
409 if (0 == gsn)
411 "Accepting connection on `%s'\n",
412 GNUNET_a2s ((const struct sockaddr *) &name,
413 namelen));
414 }
415#endif
416 ret->fd = accept (desc->fd,
417 address,
418 address_len);
419 if (-1 == ret->fd)
420 {
421 eno = errno;
423 errno = eno;
424 return NULL;
425 }
426 if (GNUNET_OK !=
428 (NULL != address) ? address->sa_family :
429 desc->af,
430 SOCK_STREAM))
431 {
432 return NULL;
433 }
434 return ret;
435}
static char * address
GNS address for this phone.
static char * name
Name (label) of the records to list.
const char * GNUNET_a2s(const struct sockaddr *addr, socklen_t addrlen)
Convert a "struct sockaddr*" (IPv4 or IPv6 address) to a string (for printing debug messages).
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_new(type)
Allocate a struct or union of the given type.
static int initialize_network_handle(struct GNUNET_NETWORK_Handle *h, int af, int type)
Perform proper canonical initialization for a network handle.
Definition: network.c:335
#define LOG(kind,...)
Definition: network.c:31
handle to a socket
Definition: network.c:53
int af
Address family / domain.
Definition: network.c:59

References address, GNUNET_NETWORK_Handle::af, GNUNET_NETWORK_Handle::fd, GNUNET_a2s(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_new, GNUNET_OK, initialize_network_handle(), LOG, name, and ret.

Referenced by accept_client(), do_accept(), and listen_cb().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_NETWORK_socket_box_native()

struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_box_native ( int  fd)

Box a native socket (and check that it is a socket).

Parameters
fdsocket to box
Returns
NULL on error (including not supported on target platform)

Definition at line 579 of file network.c.

580{
582
583 if (fcntl (fd, F_GETFD) < 0)
584 return NULL; /* invalid FD */
586 ret->fd = fd;
587 ret->af = AF_UNSPEC;
588 return ret;
589}

References GNUNET_NETWORK_Handle::fd, GNUNET_new, and ret.

Referenced by DHTU_ip_init(), extract_handles(), scheduler_fd_cb(), and setup_service().

Here is the caller graph for this function:

◆ GNUNET_NETWORK_socket_set_blocking()

enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_set_blocking ( struct GNUNET_NETWORK_Handle fd,
int  doBlock 
)

Set if a socket should use blocking or non-blocking IO.

Parameters
fdsocket
doBlockblocking mode
Returns
GNUNET_OK on success, GNUNET_SYSERR on error

Definition at line 224 of file network.c.

226{
227 int flags = fcntl (fd->fd, F_GETFL);
228
229 if (flags == -1)
230 {
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 {
246 "fcntl");
247 return GNUNET_SYSERR;
248 }
249 return GNUNET_OK;
250}
#define LOG_STRERROR(kind, syscall)
Definition: network.c:37

References GNUNET_NETWORK_Handle::fd, GNUNET_ERROR_TYPE_WARNING, GNUNET_OK, GNUNET_SYSERR, and LOG_STRERROR.

Referenced by initialize_network_handle().

Here is the caller graph for this function:

◆ GNUNET_NETWORK_socket_bind()

enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_bind ( struct GNUNET_NETWORK_Handle desc,
const struct sockaddr *  address,
socklen_t  address_len 
)

Bind a socket to a particular address.

Parameters
descsocket to bind
addressaddress to be bound
address_lenlength of address
Returns
GNUNET_OK on success, GNUNET_SYSERR otherwise

Definition at line 439 of file network.c.

442{
443 int ret;
444
445#ifdef IPV6_V6ONLY
446#ifdef IPPROTO_IPV6
447 {
448 const int on = 1;
449
450 if (AF_INET6 == desc->af)
451 if (setsockopt (desc->fd,
452 IPPROTO_IPV6,
453 IPV6_V6ONLY,
454 (const void *) &on,
455 sizeof(on)))
457 "setsockopt");
458 }
459#endif
460#endif
461 if (AF_UNIX == address->sa_family)
462 GNUNET_NETWORK_unix_precheck ((const struct sockaddr_un *) address);
463
464 {
465 const int on = 1;
466
467 if ( (SOCK_STREAM == desc->type) &&
468 (0 != setsockopt (desc->fd,
469 SOL_SOCKET,
470 SO_REUSEADDR,
471 &on, sizeof(on))) )
473 "setsockopt");
474 }
475 {
476 /* set permissions of newly created non-abstract UNIX domain socket to
477 "user-only"; applications can choose to relax this later */
478 mode_t old_mask = 0; /* assigned to make compiler happy */
479 const struct sockaddr_un *un = (const struct sockaddr_un *) address;
480 int not_abstract = 0;
481
482 if ((AF_UNIX == address->sa_family)
483 && ('\0' != un->sun_path[0])) /* Not an abstract socket */
484 not_abstract = 1;
485 if (not_abstract)
486 old_mask = umask (S_IWGRP | S_IRGRP | S_IXGRP | S_IWOTH | S_IROTH
487 | S_IXOTH);
488
489 ret = bind (desc->fd,
490 address,
491 address_len);
492
493 if (not_abstract)
494 (void) umask (old_mask);
495 }
496 if (0 != ret)
497 return GNUNET_SYSERR;
498
499 desc->addr = GNUNET_malloc (address_len);
500 GNUNET_memcpy (desc->addr, address, address_len);
501 desc->addrlen = address_len;
502
503 return GNUNET_OK;
504}
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_malloc(size)
Wrapper around malloc.
void GNUNET_NETWORK_unix_precheck(const struct sockaddr_un *un)
If services crash, they can leave a unix domain socket file on the disk.
Definition: network.c:178
struct sockaddr * addr
Address we were bound to, or NULL.
Definition: network.c:74
int type
Type of the socket.
Definition: network.c:64
socklen_t addrlen
Number of bytes in addr.
Definition: network.c:69

References GNUNET_NETWORK_Handle::addr, address, GNUNET_NETWORK_Handle::addrlen, GNUNET_NETWORK_Handle::af, GNUNET_NETWORK_Handle::fd, GNUNET_ERROR_TYPE_DEBUG, GNUNET_malloc, GNUNET_memcpy, GNUNET_NETWORK_unix_precheck(), GNUNET_OK, GNUNET_SYSERR, LOG_STRERROR, ret, and GNUNET_NETWORK_Handle::type.

Referenced by bind_v4(), bind_v6(), create_listen_socket(), GNUNET_NAT_AUTO_test_start(), GNUNET_NAT_test_start(), GNUNET_NETWORK_test_port_free(), GNUNET_TESTING_reserve_port(), init_socket(), open_listen_socket(), open_socket(), and run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_NETWORK_socket_close()

enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_close ( struct GNUNET_NETWORK_Handle desc)

Close a socket.

Parameters
descsocket to close
Returns
GNUNET_OK on success, GNUNET_SYSERR otherwise

Definition at line 508 of file network.c.

509{
510 int ret;
511
512 ret = close (desc->fd);
513
514 const struct sockaddr_un *un = (const struct sockaddr_un *) desc->addr;
515
516 /* Cleanup the UNIX domain socket and its parent directories in case of non
517 abstract sockets */
518 if ((AF_UNIX == desc->af) &&
519 (NULL != desc->addr) &&
520 ('\0' != un->sun_path[0]))
521 {
522 char *dirname = GNUNET_strndup (un->sun_path,
523 sizeof(un->sun_path));
524
525 if (0 != unlink (dirname))
526 {
528 "unlink",
529 dirname);
530 }
531 else
532 {
533 size_t len;
534
535 len = strlen (dirname);
536 while ((len > 0) && (dirname[len] != DIR_SEPARATOR))
537 len--;
538 dirname[len] = '\0';
539 if ((0 != len) && (0 != rmdir (dirname)))
540 {
541 switch (errno)
542 {
543 case EACCES:
544 case ENOTEMPTY:
545 case EPERM:
546 /* these are normal and can just be ignored */
547 break;
548
549 default:
551 "rmdir",
552 dirname);
553 break;
554 }
555 }
556 }
557 GNUNET_free (dirname);
558 }
560 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
561}
#define GNUNET_strndup(a, length)
Wrapper around GNUNET_xstrndup_.
void GNUNET_NETWORK_socket_free_memory_only_(struct GNUNET_NETWORK_Handle *desc)
Only free memory of a socket, keep the file descriptor untouched.
Definition: network.c:565
#define LOG_STRERROR_FILE(kind, syscall, filename)
Definition: network.c:32
#define DIR_SEPARATOR
Definition: platform.h:165

References GNUNET_NETWORK_Handle::addr, GNUNET_NETWORK_Handle::af, DIR_SEPARATOR, GNUNET_NETWORK_Handle::fd, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_log_strerror_file, GNUNET_NETWORK_socket_free_memory_only_(), GNUNET_OK, GNUNET_strndup, GNUNET_SYSERR, LOG_STRERROR_FILE, and ret.

Referenced by accept_client(), bind_v4(), bind_v6(), cancel_aps(), cleanup_ar(), cleanup_rs(), cleanup_s5r(), connect_probe_continuation(), connection_client_destroy_impl(), create_listen_socket(), create_proto_queue(), DHTU_ip_done(), do_accept(), do_dns_read(), do_read(), do_shutdown(), do_udp_read(), finish_client_drop(), free_proto_queue(), get_lt_delete_it(), get_server_addresses(), GNUNET_NAT_AUTO_test_start(), GNUNET_NAT_AUTO_test_stop(), GNUNET_NAT_test_start(), GNUNET_NAT_test_stop(), GNUNET_NETWORK_test_port_free(), GNUNET_TESTING_reserve_port(), init_socket(), initialize_network_handle(), kill_httpd(), mq_init(), open_listen_socket(), open_socket(), queue_destroy(), run(), setup_service(), shutdown_task(), stop_stun(), tcp_send(), teardown_service(), try_connect_using_address(), try_send_tcp(), try_send_udp(), and try_unixpath().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_NETWORK_socket_free_memory_only_()

void GNUNET_NETWORK_socket_free_memory_only_ ( struct GNUNET_NETWORK_Handle desc)

Only free memory of a socket, keep the file descriptor untouched.

Parameters
descsocket

Definition at line 565 of file network.c.

566{
567 GNUNET_free (desc->addr);
568 GNUNET_free (desc);
569}

References GNUNET_NETWORK_Handle::addr, and GNUNET_free.

Referenced by cleanup_ar(), cleanup_s5r(), destroy_task(), finish_client_drop(), and GNUNET_NETWORK_socket_close().

Here is the caller graph for this function:

◆ GNUNET_NETWORK_socket_connect()

enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_connect ( const struct GNUNET_NETWORK_Handle desc,
const struct sockaddr *  address,
socklen_t  address_len 
)

Connect a socket to some remote address.

Parameters
descsocket to connect
addresspeer address
address_lenof address
Returns
GNUNET_OK on success, GNUNET_SYSERR otherwise
Parameters
descsocket
addresspeer address
address_lenlength of address
Returns
GNUNET_OK on success, GNUNET_SYSERR otherwise

Definition at line 601 of file network.c.

604{
605 int ret;
606
607 ret = connect (desc->fd,
608 address,
609 address_len);
610
611 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
612}

References address, GNUNET_NETWORK_Handle::fd, GNUNET_OK, GNUNET_SYSERR, and ret.

Referenced by create_proto_queue(), mq_init(), try_connect_using_address(), try_send_tcp(), and try_unixpath().

Here is the caller graph for this function:

◆ GNUNET_NETWORK_socket_getsockopt()

enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_getsockopt ( const struct GNUNET_NETWORK_Handle desc,
int  level,
int  optname,
void *  optval,
socklen_t *  optlen 
)

Get socket options.

Parameters
descsocket to inspect
levelprotocol level of the option
optnameidentifier of the option
optvaloptions
optlenlength of optval
Returns
GNUNET_OK on success, GNUNET_SYSERR otherwise
Parameters
descsocket
levelprotocol level of the option
optnameidentifier of the option
optvaloptions
optlenlength of optval
Returns
GNUNET_OK on success, GNUNET_SYSERR otherwise

Definition at line 626 of file network.c.

631{
632 int ret;
633
634 ret = getsockopt (desc->fd,
635 level,
636 optname,
637 optval, optlen);
638
639 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
640}

References GNUNET_NETWORK_Handle::fd, GNUNET_OK, GNUNET_SYSERR, and ret.

Referenced by connect_probe_continuation(), and select_write_cb().

Here is the caller graph for this function:

◆ GNUNET_NETWORK_socket_listen()

enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_listen ( const struct GNUNET_NETWORK_Handle desc,
int  backlog 
)

Listen on a socket.

Parameters
descsocket to start listening on
backloglength of the listen queue
Returns
GNUNET_OK on success, GNUNET_SYSERR otherwise
Parameters
descsocket
backloglength of the listen queue
Returns
GNUNET_OK on success, GNUNET_SYSERR otherwise

Definition at line 651 of file network.c.

653{
654 int ret;
655
656 ret = listen (desc->fd,
657 backlog);
658
659 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
660}

References GNUNET_NETWORK_Handle::fd, GNUNET_OK, GNUNET_SYSERR, and ret.

Referenced by create_listen_socket(), GNUNET_NAT_AUTO_test_start(), GNUNET_NAT_test_start(), init_socket(), open_listen_socket(), and run().

Here is the caller graph for this function:

◆ GNUNET_NETWORK_socket_recvfrom_amount()

ssize_t GNUNET_NETWORK_socket_recvfrom_amount ( const struct GNUNET_NETWORK_Handle desc)

How much data is available to be read on this descriptor?

Parameters
descsocket
Returns
GNUNET_SYSERR if no data is available, or on error!

Definition at line 670 of file network.c.

671{
672 int error;
673
674 /* How much is there to be read? */
675 int pending;
676
677 error = ioctl (desc->fd,
678 FIONREAD,
679 &pending);
680 if (0 == error)
681 return (ssize_t) pending;
682 return GNUNET_SYSERR;
683}
static unsigned int pending
The number of DNS queries that are outstanding.

References GNUNET_NETWORK_Handle::fd, GNUNET_SYSERR, and pending.

Referenced by read_dns4(), read_dns6(), and stun_read_task().

Here is the caller graph for this function:

◆ GNUNET_NETWORK_socket_recvfrom()

ssize_t GNUNET_NETWORK_socket_recvfrom ( const struct GNUNET_NETWORK_Handle desc,
void *  buffer,
size_t  length,
struct sockaddr *  src_addr,
socklen_t *  addrlen 
)

Read data from a socket (always non-blocking).

Parameters
descsocket
bufferbuffer
lengthlength of buffer
src_addreither the source to recv from, or all zeroes to be filled in by recvfrom
addrlenlength of the addr

Definition at line 687 of file network.c.

692{
693 int flags = 0;
694
695#ifdef MSG_DONTWAIT
696 flags |= MSG_DONTWAIT;
697#endif
698 return recvfrom (desc->fd,
699 buffer,
700 length,
701 flags,
702 src_addr,
703 addrlen);
704}

References GNUNET_NETWORK_Handle::addrlen, and GNUNET_NETWORK_Handle::fd.

Referenced by do_dns_read(), read_dns4(), read_dns6(), select_read_cb(), sock_read(), and stun_read_task().

Here is the caller graph for this function:

◆ GNUNET_NETWORK_socket_recv()

ssize_t GNUNET_NETWORK_socket_recv ( const struct GNUNET_NETWORK_Handle desc,
void *  buffer,
size_t  length 
)

Read data from a connected socket (always non-blocking).

Parameters
descsocket
bufferbuffer
lengthlength of buffer
Returns
number of bytes read
Parameters
descsocket
bufferbuffer
lengthlength of buffer
Returns
number of bytes received, -1 on error

Definition at line 716 of file network.c.

719{
720 int ret;
721 int flags;
722
723 flags = 0;
724
725#ifdef MSG_DONTWAIT
726 flags |= MSG_DONTWAIT;
727#endif
728 ret = recv (desc->fd,
729 buffer,
730 length,
731 flags);
732 return ret;
733}

References GNUNET_NETWORK_Handle::fd, and ret.

Referenced by do_read(), do_s5r_read(), do_udp_read(), GNUNET_MST_read(), proto_read_kx(), queue_read(), and queue_read_kx().

Here is the caller graph for this function:

◆ GNUNET_NETWORK_socket_select()

int GNUNET_NETWORK_socket_select ( struct GNUNET_NETWORK_FDSet rfds,
struct GNUNET_NETWORK_FDSet wfds,
struct GNUNET_NETWORK_FDSet efds,
const struct GNUNET_TIME_Relative  timeout 
)

Check if sockets meet certain conditions.

Parameters
rfdsset of sockets to be checked for readability
wfdsset of sockets to be checked for writability
efdsset of sockets to be checked for exceptions
timeoutrelative value when to return
Returns
number of selected sockets, GNUNET_SYSERR on error

Check if sockets meet certain conditions.

Parameters
rfdsset of sockets or pipes to be checked for readability
wfdsset of sockets or pipes to be checked for writability
efdsset of sockets or pipes to be checked for exceptions
timeoutrelative value when to return
Returns
number of selected sockets or pipes, GNUNET_SYSERR on error

Definition at line 1260 of file network.c.

1264{
1265 int nfds;
1266 struct timeval tv;
1267
1268 if (NULL != rfds)
1269 nfds = rfds->nsds;
1270 else
1271 nfds = 0;
1272 if (NULL != wfds)
1273 nfds = GNUNET_MAX (nfds,
1274 wfds->nsds);
1275 if (NULL != efds)
1276 nfds = GNUNET_MAX (nfds,
1277 efds->nsds);
1278 if ((0 == nfds) &&
1280 {
1281 GNUNET_break (0);
1283 _ (
1284 "Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1285 "select");
1286 }
1287 if (timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us > (unsigned
1288 long long)
1289 LONG_MAX)
1290 {
1291 tv.tv_sec = LONG_MAX;
1292 tv.tv_usec = 999999L;
1293 }
1294 else
1295 {
1296 tv.tv_sec = (long) (timeout.rel_value_us
1297 / GNUNET_TIME_UNIT_SECONDS.rel_value_us);
1298 tv.tv_usec =
1300 - (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value_us));
1301 }
1302 return select (nfds,
1303 (NULL != rfds) ? &rfds->sds : NULL,
1304 (NULL != wfds) ? &wfds->sds : NULL,
1305 (NULL != efds) ? &efds->sds : NULL,
1307 GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) ? NULL : &tv);
1308}
static struct GNUNET_TIME_Relative timeout
User defined timestamp for completing operations.
Definition: gnunet-arm.c:119
#define GNUNET_MAX(a, b)
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
#define GNUNET_TIME_UNIT_SECONDS
One second.
int nsds
Maximum number of any socket descriptor in the set (plus one)
fd_set sds
Bitset with the descriptors.
uint64_t rel_value_us
The actual value.

References _, GNUNET_break, GNUNET_ERROR_TYPE_ERROR, GNUNET_MAX, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_TIME_UNIT_SECONDS, LOG, GNUNET_NETWORK_FDSet::nsds, GNUNET_TIME_Relative::rel_value_us, GNUNET_NETWORK_FDSet::sds, and timeout.

Referenced by select_loop().

Here is the caller graph for this function:

◆ GNUNET_NETWORK_socket_send()

ssize_t GNUNET_NETWORK_socket_send ( const struct GNUNET_NETWORK_Handle desc,
const void *  buffer,
size_t  length 
)

Send data (always non-blocking).

Parameters
descsocket
bufferdata to send
lengthsize of the buffer
Returns
number of bytes sent, GNUNET_SYSERR on error

Definition at line 737 of file network.c.

740{
741 int ret;
742 int flags;
743
744 flags = 0;
745#ifdef MSG_DONTWAIT
746 flags |= MSG_DONTWAIT;
747#endif
748#ifdef MSG_NOSIGNAL
749 flags |= MSG_NOSIGNAL;
750#endif
751 ret = send (desc->fd,
752 buffer,
753 length,
754 flags);
755 return ret;
756}

References GNUNET_NETWORK_Handle::fd, and ret.

Referenced by do_send(), do_write(), proto_queue_write(), queue_write(), tcp_send(), and transmit_ready().

Here is the caller graph for this function:

◆ GNUNET_NETWORK_socket_sendto()

ssize_t GNUNET_NETWORK_socket_sendto ( const struct GNUNET_NETWORK_Handle desc,
const void *  message,
size_t  length,
const struct sockaddr *  dest_addr,
socklen_t  dest_len 
)

Send data to a particular destination (always non-blocking).

This function only works for UDP sockets.

Parameters
descsocket
messagedata to send
lengthsize of the data in message
dest_addrdestination address
dest_lenlength of dest_addr
Returns
number of bytes sent, GNUNET_SYSERR on error

This function only works for UDP sockets.

Parameters
descsocket
messagedata to send
lengthsize of the message
dest_addrdestination address
dest_lenlength of address
Returns
number of bytes sent, GNUNET_SYSERR on error

Definition at line 771 of file network.c.

776{
777 int flags = 0;
778
779#ifdef MSG_DONTWAIT
780 flags |= MSG_DONTWAIT;
781#endif
782#ifdef MSG_NOSIGNAL
783 flags |= MSG_NOSIGNAL;
784#endif
785 return sendto (desc->fd,
786 message,
787 length,
788 flags,
789 dest_addr,
790 dest_len);
791}

References GNUNET_NETWORK_Handle::fd.

Referenced by flush_egress(), ifc_broadcast(), ip_send(), mq_send_d(), select_write_cb(), send_msg_with_kx(), send_response(), sock_read(), stun_dns_callback(), transmit_query(), and try_send_udp().

Here is the caller graph for this function:

◆ GNUNET_NETWORK_socket_setsockopt()

int GNUNET_NETWORK_socket_setsockopt ( struct GNUNET_NETWORK_Handle fd,
int  level,
int  option_name,
const void *  option_value,
socklen_t  option_len 
)

Set socket option.

Parameters
fdsocket
levelprotocol level of the option
option_nameoption identifier
option_valuevalue to set
option_lensize of option_value
Returns
GNUNET_OK on success, GNUNET_SYSERR otherwise

Definition at line 805 of file network.c.

810{
811 return (0 == setsockopt (fd->fd,
812 level,
813 option_name,
814 option_value,
815 option_len))
816 ? GNUNET_OK
818}

References GNUNET_NETWORK_Handle::fd, GNUNET_OK, and GNUNET_SYSERR.

Referenced by bi_destroy(), create_listen_socket(), iface_proc(), ifc_broadcast(), and select_write_cb().

Here is the caller graph for this function:

◆ GNUNET_NETWORK_socket_shutdown()

enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_shutdown ( struct GNUNET_NETWORK_Handle desc,
int  how 
)

Shut down socket operations.

Parameters
descsocket
howtype of shutdown
Returns
GNUNET_OK on success, GNUNET_SYSERR otherwise

Definition at line 860 of file network.c.

862{
863 int ret;
864
865 ret = shutdown (desc->fd, how);
866
867 return (0 == ret) ? GNUNET_OK : GNUNET_SYSERR;
868}

References GNUNET_NETWORK_Handle::fd, GNUNET_OK, GNUNET_SYSERR, and ret.

Referenced by tcp_send().

Here is the caller graph for this function:

◆ GNUNET_NETWORK_socket_disable_corking()

enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_disable_corking ( struct GNUNET_NETWORK_Handle desc)

Disable the "CORK" feature for communication with the given socket, forcing the OS to immediately flush the buffer on transmission instead of potentially buffering multiple messages.

Essentially reduces the OS send buffers to zero.

Parameters
descsocket
Returns
GNUNET_OK on success, GNUNET_SYSERR otherwise

Definition at line 881 of file network.c.

882{
883 int ret = 0;
884
885#ifdef __linux__
886 int value = 0;
887
888 if (0 !=
889 (ret =
890 setsockopt (desc->fd,
891 SOL_SOCKET,
892 SO_SNDBUF,
893 &value,
894 sizeof(value))))
896 "setsockopt");
897 if (0 !=
898 (ret =
899 setsockopt (desc->fd,
900 SOL_SOCKET,
901 SO_RCVBUF,
902 &value,
903 sizeof(value))))
905 "setsockopt");
906#endif
907 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
908}
static char * value
Value of the record to add/remove.

References GNUNET_NETWORK_Handle::fd, GNUNET_ERROR_TYPE_WARNING, GNUNET_OK, GNUNET_SYSERR, LOG_STRERROR, ret, and value.

◆ GNUNET_NETWORK_socket_create()

struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_create ( int  domain,
int  type,
int  protocol 
)

Create a new socket.

Configure it for non-blocking IO and mark it as non-inheritable to child processes (set the close-on-exec flag).

Parameters
domaindomain of the socket
typesocket type
protocolnetwork protocol
Returns
new socket, NULL on error

Definition at line 832 of file network.c.

835{
837 int fd;
838
839 fd = socket (domain, type, protocol);
840 if (-1 == fd)
841 return NULL;
843 ret->fd = fd;
844 if (GNUNET_OK !=
846 domain,
847 type))
848 return NULL;
849 return ret;
850}
static uint32_t type
Type string converted to DNS type value.

References GNUNET_NETWORK_Handle::fd, GNUNET_new, GNUNET_OK, initialize_network_handle(), ret, and type.

Referenced by bind_v4(), bind_v6(), create_listen_socket(), create_proto_queue(), get_server_addresses(), GNUNET_NAT_AUTO_test_start(), GNUNET_NAT_test_start(), GNUNET_NETWORK_test_port_free(), GNUNET_TESTING_reserve_port(), init_socket(), mq_init(), open_listen_socket(), open_socket(), run(), try_connect_using_address(), try_send_tcp(), try_send_udp(), and try_unixpath().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_NETWORK_fdset_zero()

void GNUNET_NETWORK_fdset_zero ( struct GNUNET_NETWORK_FDSet fds)

Reset FD set (clears all file descriptors).

Parameters
fdsfd set to clear

Reset FD set (clears all file descriptors).

Parameters
fdsfd set

Definition at line 917 of file network.c.

918{
919 FD_ZERO (&fds->sds);
920 fds->nsds = 0;
921}

References GNUNET_NETWORK_FDSet::nsds, and GNUNET_NETWORK_FDSet::sds.

Referenced by GNUNET_NETWORK_fdset_create(), GNUNET_SCHEDULER_do_work(), and select_loop().

Here is the caller graph for this function:

◆ GNUNET_NETWORK_fdset_set()

void GNUNET_NETWORK_fdset_set ( struct GNUNET_NETWORK_FDSet fds,
const struct GNUNET_NETWORK_Handle desc 
)

Add a socket to the FD set.

Parameters
fdsfd set
descsocket to add

Definition at line 931 of file network.c.

933{
934 FD_SET (desc->fd,
935 &fds->sds);
936 fds->nsds = GNUNET_MAX (fds->nsds,
937 desc->fd + 1);
938}

References GNUNET_NETWORK_Handle::fd, GNUNET_MAX, GNUNET_NETWORK_FDSet::nsds, and GNUNET_NETWORK_FDSet::sds.

Referenced by schedule_read().

Here is the caller graph for this function:

◆ GNUNET_NETWORK_fdset_isset()

int GNUNET_NETWORK_fdset_isset ( const struct GNUNET_NETWORK_FDSet fds,
const struct GNUNET_NETWORK_Handle desc 
)

Check whether a socket is part of the fd set.

Parameters
fdsfd set
descsocket
Returns
GNUNET_YES if the socket is in the set
Parameters
fdsfd set
descsocket
Returns
0 if the FD is not set

Definition at line 949 of file network.c.

951{
952 return FD_ISSET (desc->fd,
953 &fds->sds);
954}

References GNUNET_NETWORK_Handle::fd, and GNUNET_NETWORK_FDSet::sds.

Referenced by do_read(), do_s5r_read(), do_udp_read(), read_response(), and tcp_send().

Here is the caller graph for this function:

◆ GNUNET_NETWORK_fdset_add()

void GNUNET_NETWORK_fdset_add ( struct GNUNET_NETWORK_FDSet dst,
const struct GNUNET_NETWORK_FDSet src 
)

Add one fd set to another (computes the union).

Parameters
dstthe fd set to add to
srcthe fd set to add from

Add one fd set to another (computes the union).

Parameters
dstthe fd set to add to
srcthe fd set to add from

Definition at line 964 of file network.c.

966{
967 int nfds;
968
969 for (nfds = src->nsds; nfds >= 0; nfds--)
970 if (FD_ISSET (nfds, &src->sds))
971 FD_SET (nfds, &dst->sds);
972 dst->nsds = GNUNET_MAX (dst->nsds,
973 src->nsds);
974}

References GNUNET_MAX, GNUNET_NETWORK_FDSet::nsds, and GNUNET_NETWORK_FDSet::sds.

◆ GNUNET_NETWORK_fdset_copy()

void GNUNET_NETWORK_fdset_copy ( struct GNUNET_NETWORK_FDSet to,
const struct GNUNET_NETWORK_FDSet from 
)

Copy one fd set to another.

Parameters
todestination
fromsource

Definition at line 984 of file network.c.

986{
987 FD_COPY (&from->sds,
988 &to->sds);
989 to->nsds = from->nsds;
990}
#define FD_COPY(s, d)
Definition: network.c:219

References FD_COPY, GNUNET_NETWORK_FDSet::nsds, and GNUNET_NETWORK_FDSet::sds.

◆ GNUNET_NETWORK_get_fd()

int GNUNET_NETWORK_get_fd ( const struct GNUNET_NETWORK_Handle desc)

Return file descriptor for this network handle.

Parameters
descwrapper to process
Returns
POSIX file descriptor

Definition at line 1000 of file network.c.

1001{
1002 return desc->fd;
1003}

References GNUNET_NETWORK_Handle::fd.

Referenced by add_without_sets(), do_accept(), do_dns_read(), GNUNET_SCHEDULER_add_net_with_priority(), init_fd_info(), init_socket(), mhd_connection_cb(), queue_destroy(), read_cb(), run(), setup_data_transfer(), and start_process().

Here is the caller graph for this function:

◆ GNUNET_NETWORK_get_addr()

struct sockaddr * GNUNET_NETWORK_get_addr ( const struct GNUNET_NETWORK_Handle desc)

Return the sockaddr for this network handle.

Parameters
descwrapper to process
Returns
POSIX file descriptor

Return the sockaddr for this network handle.

Parameters
descwrapper to process
Returns
sockaddr

Definition at line 1013 of file network.c.

1014{
1015 return desc->addr;
1016}

References GNUNET_NETWORK_Handle::addr.

Referenced by do_accept(), and setup_data_transfer().

Here is the caller graph for this function:

◆ GNUNET_NETWORK_get_addrlen()

socklen_t GNUNET_NETWORK_get_addrlen ( const struct GNUNET_NETWORK_Handle desc)

Return sockaddr length for this network handle.

Parameters
descwrapper to process
Returns
socklen_t for sockaddr

Definition at line 1026 of file network.c.

1027{
1028 return desc->addrlen;
1029}

References GNUNET_NETWORK_Handle::addrlen.

Referenced by do_accept(), and setup_data_transfer().

Here is the caller graph for this function:

◆ GNUNET_NETWORK_fdset_copy_native()

void GNUNET_NETWORK_fdset_copy_native ( struct GNUNET_NETWORK_FDSet to,
const fd_set *  from,
int  nfds 
)

Copy a native fd set into the GNUnet representation.

Parameters
todestination
fromnative source set
nfdsthe biggest socket number in from + 1

Copy a native fd set into the GNUnet representation.

Parameters
todestination
fromnative source set
nfdsthe biggest socket number in from + 1

Definition at line 1040 of file network.c.

1043{
1044 FD_COPY (from,
1045 &to->sds);
1046 to->nsds = nfds;
1047}

References FD_COPY, GNUNET_NETWORK_FDSet::nsds, and GNUNET_NETWORK_FDSet::sds.

Referenced by context_task(), curl_download_prepare(), download_prepare(), prepare_daemon(), run_httpd(), and schedule_httpd().

Here is the caller graph for this function:

◆ GNUNET_NETWORK_fdset_set_native()

void GNUNET_NETWORK_fdset_set_native ( struct GNUNET_NETWORK_FDSet to,
int  nfd 
)

Set a native fd in a set.

Parameters
todestination
nfdnative FD to set

Definition at line 1057 of file network.c.

1059{
1060 GNUNET_assert ((nfd >= 0) && (nfd < FD_SETSIZE));
1061 FD_SET (nfd, &to->sds);
1062 to->nsds = GNUNET_MAX (nfd + 1,
1063 to->nsds);
1064}
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.

References GNUNET_assert, GNUNET_MAX, GNUNET_NETWORK_FDSet::nsds, and GNUNET_NETWORK_FDSet::sds.

Referenced by GNUNET_SCHEDULER_do_work(), listen_stdio(), and select_loop().

Here is the caller graph for this function:

◆ GNUNET_NETWORK_fdset_test_native()

int GNUNET_NETWORK_fdset_test_native ( const struct GNUNET_NETWORK_FDSet to,
int  nfd 
)

Test native fd in a set.

Parameters
toset to test, NULL for empty set
nfdnative FD to test, -1 for none
Returns
GNUNET_YES if to contains nfd
Parameters
toset to test, NULL for empty set
nfdnative FD to test, or -1 for none
Returns
GNUNET_YES if FD is set in the set

Definition at line 1075 of file network.c.

1077{
1078 if ((-1 == nfd) ||
1079 (NULL == to))
1080 return GNUNET_NO;
1081 return FD_ISSET (nfd, &to->sds) ? GNUNET_YES : GNUNET_NO;
1082}
@ GNUNET_YES

References GNUNET_NO, GNUNET_YES, and GNUNET_NETWORK_FDSet::sds.

Referenced by extract_handles(), and select_loop().

Here is the caller graph for this function:

◆ GNUNET_NETWORK_fdset_handle_set()

void GNUNET_NETWORK_fdset_handle_set ( struct GNUNET_NETWORK_FDSet fds,
const struct GNUNET_DISK_FileHandle h 
)

Add a file handle to the fd set.

Parameters
fdsfd set
hthe file handle to add

Definition at line 1091 of file network.c.

1093{
1094 int fd;
1095
1098 &fd,
1099 sizeof(int)));
1100 FD_SET (fd,
1101 &fds->sds);
1102 fds->nsds = GNUNET_MAX (fd + 1,
1103 fds->nsds);
1104}
enum GNUNET_GenericReturnValue GNUNET_DISK_internal_file_handle_(const struct GNUNET_DISK_FileHandle *fh, void *dst, size_t dst_len)
Retrieve OS file handle.
Definition: disk.c:1634
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:99

References GNUNET_assert, GNUNET_DISK_internal_file_handle_(), GNUNET_MAX, GNUNET_OK, h, GNUNET_NETWORK_FDSet::nsds, and GNUNET_NETWORK_FDSet::sds.

Referenced by GNUNET_NETWORK_fdset_handle_set_first(), and GNUNET_SCHEDULER_driver_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_NETWORK_fdset_handle_set_first()

void GNUNET_NETWORK_fdset_handle_set_first ( struct GNUNET_NETWORK_FDSet fds,
const struct GNUNET_DISK_FileHandle h 
)

Add a file handle to the fd set On W32: ensure that the handle is first in the array.

Parameters
fdsfd set
hthe file handle to add

Add a file handle to the fd set On W32: ensure that the handle is first in the array.

Parameters
fdsfd set
hthe file handle to add

Definition at line 1113 of file network.c.

1115{
1117}
void GNUNET_NETWORK_fdset_handle_set(struct GNUNET_NETWORK_FDSet *fds, const struct GNUNET_DISK_FileHandle *h)
Add a file handle to the fd set.
Definition: network.c:1091

References GNUNET_NETWORK_fdset_handle_set(), and h.

Here is the call graph for this function:

◆ GNUNET_NETWORK_fdset_handle_isset()

int GNUNET_NETWORK_fdset_handle_isset ( const struct GNUNET_NETWORK_FDSet fds,
const struct GNUNET_DISK_FileHandle h 
)

Check if a file handle is part of an fd set.

Parameters
fdsfd set
hfile handle
Returns
GNUNET_YES if the file handle is part of the set

Definition at line 1128 of file network.c.

1130{
1131 return FD_ISSET (h->fd,
1132 &fds->sds);
1133}

References h, and GNUNET_NETWORK_FDSet::sds.

Referenced by cmd_read().

Here is the caller graph for this function:

◆ GNUNET_NETWORK_fdset_overlap()

int GNUNET_NETWORK_fdset_overlap ( const struct GNUNET_NETWORK_FDSet fds1,
const struct GNUNET_NETWORK_FDSet fds2 
)

Checks if two fd sets overlap.

Parameters
fds1first fd set
fds2second fd set
Returns
GNUNET_YES if they do overlap, GNUNET_NO otherwise

Definition at line 1144 of file network.c.

1146{
1147 int nfds;
1148
1149 nfds = GNUNET_MIN (fds1->nsds,
1150 fds2->nsds);
1151 while (nfds > 0)
1152 {
1153 nfds--;
1154 if ((FD_ISSET (nfds,
1155 &fds1->sds)) &&
1156 (FD_ISSET (nfds,
1157 &fds2->sds)))
1158 return GNUNET_YES;
1159 }
1160 return GNUNET_NO;
1161}
#define GNUNET_MIN(a, b)

References GNUNET_MIN, GNUNET_NO, GNUNET_YES, GNUNET_NETWORK_FDSet::nsds, and GNUNET_NETWORK_FDSet::sds.

◆ GNUNET_NETWORK_fdset_create()

struct GNUNET_NETWORK_FDSet * GNUNET_NETWORK_fdset_create ( void  )

Creates an fd set.

Returns
a new fd set

Definition at line 1170 of file network.c.

1171{
1172 struct GNUNET_NETWORK_FDSet *fds;
1173
1174 fds = GNUNET_new (struct GNUNET_NETWORK_FDSet);
1176 return fds;
1177}
void GNUNET_NETWORK_fdset_zero(struct GNUNET_NETWORK_FDSet *fds)
Reset FD set.
Definition: network.c:917
collection of IO descriptors

References GNUNET_NETWORK_fdset_zero(), and GNUNET_new.

Referenced by context_task(), curl_download_prepare(), download_prepare(), GNUNET_SCHEDULER_driver_init(), listen_stdio(), prepare_daemon(), run_httpd(), schedule_httpd(), schedule_read(), and select_loop().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_NETWORK_fdset_destroy()

void GNUNET_NETWORK_fdset_destroy ( struct GNUNET_NETWORK_FDSet fds)

Releases the associated memory of an fd set.

Parameters
fdsfd set

Definition at line 1186 of file network.c.

1187{
1188 GNUNET_free (fds);
1189}

References GNUNET_free.

Referenced by context_task(), curl_download_prepare(), download_prepare(), GNUNET_SCHEDULER_driver_done(), listen_stdio(), prepare_daemon(), run_httpd(), schedule_httpd(), schedule_read(), and select_loop().

Here is the caller graph for this function:

◆ GNUNET_NETWORK_test_port_free()

int GNUNET_NETWORK_test_port_free ( int  ipproto,
uint16_t  port 
)

Test if the given port is available.

Parameters
ipprototransport protocol to test (e.g. IPPROTO_TCP)
portport number to test
Returns
GNUNET_OK if the port is available, GNUNET_NO if not

Definition at line 1200 of file network.c.

1202{
1203 struct GNUNET_NETWORK_Handle *socket;
1204 int bind_status;
1205 int socktype;
1206 char open_port_str[6];
1207 struct addrinfo hint;
1208 struct addrinfo *ret;
1209 struct addrinfo *ai;
1210
1211 GNUNET_snprintf (open_port_str,
1212 sizeof(open_port_str),
1213 "%u",
1214 (unsigned int) port);
1215 socktype = (IPPROTO_TCP == ipproto) ? SOCK_STREAM : SOCK_DGRAM;
1216 ret = NULL;
1217 memset (&hint, 0, sizeof(hint));
1218 hint.ai_family = AF_UNSPEC; /* IPv4 and IPv6 */
1219 hint.ai_socktype = socktype;
1220 hint.ai_protocol = ipproto;
1221 hint.ai_addrlen = 0;
1222 hint.ai_addr = NULL;
1223 hint.ai_canonname = NULL;
1224 hint.ai_next = NULL;
1225 hint.ai_flags = AI_PASSIVE | AI_NUMERICSERV; /* Wild card address */
1226 GNUNET_assert (0 == getaddrinfo (NULL,
1227 open_port_str,
1228 &hint,
1229 &ret));
1230 bind_status = GNUNET_NO;
1231 for (ai = ret; NULL != ai; ai = ai->ai_next)
1232 {
1233 socket = GNUNET_NETWORK_socket_create (ai->ai_family,
1234 ai->ai_socktype,
1235 ai->ai_protocol);
1236 if (NULL == socket)
1237 continue;
1238 bind_status = GNUNET_NETWORK_socket_bind (socket,
1239 ai->ai_addr,
1240 ai->ai_addrlen);
1242 if (GNUNET_OK != bind_status)
1243 break;
1244 }
1245 freeaddrinfo (ret);
1246 return bind_status;
1247}
static uint16_t port
Port number.
Definition: gnunet-bcd.c:147
static struct GNUNET_TRANSPORT_AddressIdentifier * ai
Handle to the operation that publishes our address.
int GNUNET_snprintf(char *buf, size_t size, const char *format,...) __attribute__((format(printf
Like snprintf, just aborts if the buffer is of insufficient size.
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc)
Close a socket.
Definition: network.c:508
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_create(int domain, int type, int protocol)
Create a new socket.
Definition: network.c:832
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_bind(struct GNUNET_NETWORK_Handle *desc, const struct sockaddr *address, socklen_t address_len)
Bind a socket to a particular address.
Definition: network.c:439
#define AI_NUMERICSERV
AI_NUMERICSERV not defined in windows.
Definition: platform.h:219

References ai, AI_NUMERICSERV, GNUNET_assert, GNUNET_NETWORK_socket_bind(), GNUNET_NETWORK_socket_close(), GNUNET_NETWORK_socket_create(), GNUNET_NO, GNUNET_OK, GNUNET_snprintf(), port, and ret.

Here is the call graph for this function: