GNUnet  0.19.5
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 1 of file network.c.

81 {
82  static int cache_v4 = -1;
83  static int cache_v6 = -1;
84  static int cache_un = -1;
85  int s;
86  int ret;
87 
88  switch (pf)
89  {
90  case PF_INET:
91  if (-1 != cache_v4)
92  return cache_v4;
93  break;
94 
95  case PF_INET6:
96  if (-1 != cache_v6)
97  return cache_v6;
98  break;
99 
100 #ifdef PF_UNIX
101  case PF_UNIX:
102  if (-1 != cache_un)
103  return cache_un;
104  break;
105 #endif
106  }
107  s = socket (pf, SOCK_STREAM, 0);
108  if (-1 == s)
109  {
110  if (EAFNOSUPPORT != errno)
111  {
113  "socket");
114  return GNUNET_SYSERR;
115  }
116  ret = GNUNET_NO;
117  }
118  else
119  {
120  GNUNET_break (0 == close (s));
121  ret = GNUNET_OK;
122  }
123  switch (pf)
124  {
125  case PF_INET:
126  cache_v4 = ret;
127  break;
128 
129  case PF_INET6:
130  cache_v6 = ret;
131  break;
132 
133 #ifdef PF_UNIX
134  case PF_UNIX:
135  cache_un = ret;
136  break;
137 #endif
138  }
139  return ret;
140 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
@ 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

Referenced by get_server_addresses(), handle_gns_result(), parse_ip_options(), run(), tcp_address_to_sockaddr_port_only(), 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 144 of file network.c.

145 {
146  struct sockaddr_un dummy;
147  size_t slen;
148  char *end;
149  struct GNUNET_HashCode sh;
151  size_t upm;
152 
153  upm = sizeof(dummy.sun_path);
154  slen = strlen (unixpath);
155  if (slen < upm)
156  return unixpath; /* no shortening required */
157  GNUNET_CRYPTO_hash (unixpath, slen, &sh);
158  while (16 + strlen (unixpath) >= upm)
159  {
160  if (NULL == (end = strrchr (unixpath, '/')))
161  {
163  _ (
164  "Unable to shorten unix path `%s' while keeping name unique\n"),
165  unixpath);
166  GNUNET_free (unixpath);
167  return NULL;
168  }
169  *end = '\0';
170  }
172  ae.encoding[16] = '\0';
173  strcat (unixpath, (char *) ae.encoding);
174  return unixpath;
175 }
static int end
Set if we are to shutdown all services (including ARM).
Definition: gnunet-arm.c:34
static struct SolverHandle * sh
static struct in_addr dummy
Target "dummy" address of the packet we pretend to respond to.
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(), LEGACY_SERVICE_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 179 of file network.c.

180 {
181  int s;
182  int eno;
183  struct stat sbuf;
184  int ret;
185 
186  s = socket (AF_UNIX, SOCK_STREAM, 0);
187  if (-1 == s)
188  {
190  "Failed to open AF_UNIX socket");
191  return;
192  }
193  ret = connect (s,
194  (struct sockaddr *) un,
195  sizeof(struct sockaddr_un));
196  eno = errno;
197  GNUNET_break (0 == close (s));
198  if (0 == ret)
199  return; /* another process is listening, do not remove! */
200  if (ECONNREFUSED != eno)
201  return; /* some other error, likely "no such file or directory" -- all well */
202  /* should unlink, but sanity checks first */
203  if (0 != stat (un->sun_path,
204  &sbuf))
205  return; /* failed to 'stat', likely does not exist after all */
206  if (S_IFSOCK != (S_IFMT & sbuf.st_mode))
207  return; /* refuse to unlink anything except sockets */
208  /* finally, really unlink */
210  "Removing left-over `%s' from previous exeuction\n",
211  un->sun_path);
212  if (0 != unlink (un->sun_path))
214  "unlink",
215  un->sun_path);
216 }
#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

Referenced by create_listen_socket().

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 393 of file network.c.

396 {
397  struct GNUNET_NETWORK_Handle *ret;
398  int eno;
399 
401 #if DEBUG_NETWORK
402  {
403  struct sockaddr_storage name;
404  socklen_t namelen = sizeof(name);
405 
406  int gsn = getsockname (desc->fd,
407  (struct sockaddr *) &name,
408  &namelen);
409 
410  if (0 == gsn)
412  "Accepting connection on `%s'\n",
413  GNUNET_a2s ((const struct sockaddr *) &name,
414  namelen));
415  }
416 #endif
417  ret->fd = accept (desc->fd,
418  address,
419  address_len);
420  if (-1 == ret->fd)
421  {
422  eno = errno;
423  GNUNET_free (ret);
424  errno = eno;
425  return NULL;
426  }
427  if (GNUNET_OK !=
429  (NULL != address) ? address->sa_family :
430  desc->af,
431  SOCK_STREAM))
432  {
433  return NULL;
434  }
435  return ret;
436 }
static char * address
GNS address for this phone.
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:336
#define LOG(kind,...)
Definition: network.c:32
const char * name
handle to a socket
Definition: network.c:54
int af
Address family / domain.
Definition: network.c:60

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(), GNUNET_CONNECTION_create_from_accept(), listen_cb(), and main().

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 580 of file network.c.

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

References GNUNET_NETWORK_Handle::fd, GNUNET_new, and ret.

Referenced by extract_handles(), libgnunet_plugin_dhtu_ip_init(), 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 179 of file network.c.

227 {
228  int flags = fcntl (fd->fd, F_GETFL);
229 
230  if (flags == -1)
231  {
233  "fcntl");
234  return GNUNET_SYSERR;
235  }
236  if (doBlock)
237  flags &= ~O_NONBLOCK;
238 
239  else
240  flags |= O_NONBLOCK;
241  if (0 != fcntl (fd->fd,
242  F_SETFL,
243  flags))
244 
245  {
247  "fcntl");
248  return GNUNET_SYSERR;
249  }
250  return GNUNET_OK;
251 }
#define LOG_STRERROR(kind, syscall)
Definition: network.c:38

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

Referenced by initialize_network_handle(), and main().

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 393 of file network.c.

443 {
444  int ret;
445 
446 #ifdef IPV6_V6ONLY
447 #ifdef IPPROTO_IPV6
448  {
449  const int on = 1;
450 
451  if (AF_INET6 == desc->af)
452  if (setsockopt (desc->fd,
453  IPPROTO_IPV6,
454  IPV6_V6ONLY,
455  (const void *) &on,
456  sizeof(on)))
458  "setsockopt");
459  }
460 #endif
461 #endif
462  if (AF_UNIX == address->sa_family)
463  GNUNET_NETWORK_unix_precheck ((const struct sockaddr_un *) address);
464 
465  {
466  const int on = 1;
467 
468  if ( (SOCK_STREAM == desc->type) &&
469  (0 != setsockopt (desc->fd,
470  SOL_SOCKET,
471  SO_REUSEADDR,
472  &on, sizeof(on))) )
474  "setsockopt");
475  }
476  {
477  /* set permissions of newly created non-abstract UNIX domain socket to
478  "user-only"; applications can choose to relax this later */
479  mode_t old_mask = 0; /* assigned to make compiler happy */
480  const struct sockaddr_un *un = (const struct sockaddr_un *) address;
481  int not_abstract = 0;
482 
483  if ((AF_UNIX == address->sa_family)
484  && ('\0' != un->sun_path[0])) /* Not an abstract socket */
485  not_abstract = 1;
486  if (not_abstract)
487  old_mask = umask (S_IWGRP | S_IRGRP | S_IXGRP | S_IWOTH | S_IROTH
488  | S_IXOTH);
489 
490  ret = bind (desc->fd,
491  address,
492  address_len);
493 
494  if (not_abstract)
495  (void) umask (old_mask);
496  }
497  if (0 != ret)
498  return GNUNET_SYSERR;
499 
500  desc->addr = GNUNET_malloc (address_len);
501  GNUNET_memcpy (desc->addr, address, address_len);
502  desc->addrlen = address_len;
503 
504  return GNUNET_OK;
505 }
#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:179
struct sockaddr * addr
Address we were bound to, or NULL.
Definition: network.c:75
int type
Type of the socket.
Definition: network.c:65
socklen_t addrlen
Number of bytes in addr.
Definition: network.c:70

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(), run(), setup_sockets(), and unix_transport_server_start().

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 393 of file network.c.

510 {
511  int ret;
512 
513  ret = close (desc->fd);
514 
515  const struct sockaddr_un *un = (const struct sockaddr_un *) desc->addr;
516 
517  /* Cleanup the UNIX domain socket and its parent directories in case of non
518  abstract sockets */
519  if ((AF_UNIX == desc->af) &&
520  (NULL != desc->addr) &&
521  ('\0' != un->sun_path[0]))
522  {
523  char *dirname = GNUNET_strndup (un->sun_path,
524  sizeof(un->sun_path));
525 
526  if (0 != unlink (dirname))
527  {
529  "unlink",
530  dirname);
531  }
532  else
533  {
534  size_t len;
535 
536  len = strlen (dirname);
537  while ((len > 0) && (dirname[len] != DIR_SEPARATOR))
538  len--;
539  dirname[len] = '\0';
540  if ((0 != len) && (0 != rmdir (dirname)))
541  {
542  switch (errno)
543  {
544  case EACCES:
545  case ENOTEMPTY:
546  case EPERM:
547  /* these are normal and can just be ignored */
548  break;
549 
550  default:
552  "rmdir",
553  dirname);
554  break;
555  }
556  }
557  }
558  GNUNET_free (dirname);
559  }
561  return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
562 }
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
#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:566
#define LOG_STRERROR_FILE(kind, syscall, filename)
Definition: network.c:33
#define DIR_SEPARATOR
Definition: platform.h:165

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(), 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_CONNECTION_connect_socket(), GNUNET_CONNECTION_create_from_accept(), GNUNET_CONNECTION_create_from_connect_to_unixpath(), GNUNET_CONNECTION_destroy(), GNUNET_NAT_AUTO_test_start(), GNUNET_NAT_AUTO_test_stop(), GNUNET_NAT_test_start(), GNUNET_NAT_test_stop(), GNUNET_NETWORK_test_port_free(), GNUNET_SERVER_destroy(), GNUNET_SERVER_stop_listening(), GNUNET_TESTING_reserve_port(), init_socket(), initialize_network_handle(), kill_httpd(), LEGACY_SERVICE_get_server_addresses(), libgnunet_plugin_dhtu_ip_done(), libgnunet_plugin_transport_unix_done(), main(), mq_init(), open_listen_socket(), open_socket(), queue_destroy(), run(), server_check_ipv6_support(), setup_service(), setup_sockets(), shutdown_task(), signal_transmit_error(), stop_stun(), tcp_plugin_get_session(), tcp_send(), teardown_service(), try_connect_using_address(), try_send_tcp(), try_send_udp(), try_unixpath(), and unix_transport_server_start().

◆ 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 566 of file network.c.

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

References GNUNET_NETWORK_Handle::addr, and GNUNET_free.

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

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 580 of file network.c.

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

Referenced by create_proto_queue(), GNUNET_CONNECTION_connect_socket(), GNUNET_CONNECTION_create_from_connect_to_unixpath(), main(), 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 580 of file network.c.

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

Referenced by connect_probe_continuation(), select_write_cb(), and unix_plugin_get_network().

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 580 of file network.c.

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

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 671 of file network.c.

672 {
673  int error;
674 
675  /* How much is there to be read? */
676  int pending;
677 
678  error = ioctl (desc->fd,
679  FIONREAD,
680  &pending);
681  if (0 == error)
682  return (ssize_t) pending;
683  return GNUNET_SYSERR;
684 }
static unsigned int pending
The number of 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 688 of file network.c.

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

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

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

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 717 of file network.c.

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

References GNUNET_NETWORK_Handle::fd, and ret.

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

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 1261 of file network.c.

1265 {
1266  int nfds;
1267  struct timeval tv;
1268 
1269  if (NULL != rfds)
1270  nfds = rfds->nsds;
1271  else
1272  nfds = 0;
1273  if (NULL != wfds)
1274  nfds = GNUNET_MAX (nfds,
1275  wfds->nsds);
1276  if (NULL != efds)
1277  nfds = GNUNET_MAX (nfds,
1278  efds->nsds);
1279  if ((0 == nfds) &&
1281  {
1282  GNUNET_break (0);
1284  _ (
1285  "Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1286  "select");
1287  }
1288  if (timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us > (unsigned
1289  long long)
1290  LONG_MAX)
1291  {
1292  tv.tv_sec = LONG_MAX;
1293  tv.tv_usec = 999999L;
1294  }
1295  else
1296  {
1297  tv.tv_sec = (long) (timeout.rel_value_us
1298  / GNUNET_TIME_UNIT_SECONDS.rel_value_us);
1299  tv.tv_usec =
1301  - (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value_us));
1302  }
1303  return select (nfds,
1304  (NULL != rfds) ? &rfds->sds : NULL,
1305  (NULL != wfds) ? &wfds->sds : NULL,
1306  (NULL != efds) ? &efds->sds : NULL,
1307  (timeout.rel_value_us ==
1308  GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) ? NULL : &tv);
1309 }
static struct GNUNET_TIME_Relative timeout
Desired timeout for the lookup (default is no timeout).
Definition: gnunet-abd.c:61
#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 main(), and 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 738 of file network.c.

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

References GNUNET_NETWORK_Handle::fd, and ret.

Referenced by do_send(), do_write(), main(), proto_queue_write(), queue_write(), recv_message(), 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 772 of file network.c.

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

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(), try_send_udp(), udp_ipv4_broadcast_send(), udp_ipv6_broadcast_send(), udp_select_send(), and unix_plugin_get_network().

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 806 of file network.c.

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

References GNUNET_NETWORK_Handle::fd, GNUNET_OK, and GNUNET_SYSERR.

Referenced by bi_destroy(), create_listen_socket(), iface_proc(), ifc_broadcast(), libgnunet_plugin_transport_tcp_init(), select_write_cb(), setup_broadcast(), tcp_plugin_get_session(), and unix_plugin_get_network().

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 833 of file network.c.

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

Referenced by GNUNET_CONNECTION_create_from_accept(), GNUNET_CONNECTION_destroy(), signal_transmit_error(), and 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 833 of file network.c.

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

Referenced by GNUNET_CONNECTION_disable_corking().

Here is the caller graph for this function:

◆ 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 833 of file network.c.

836 {
837  struct GNUNET_NETWORK_Handle *ret;
838  int fd;
839 
840  fd = socket (domain, type, protocol);
841  if (-1 == fd)
842  return NULL;
844  ret->fd = fd;
845  if (GNUNET_OK !=
847  domain,
848  type))
849  return NULL;
850  return ret;
851 }
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model

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_CONNECTION_create_from_connect_to_unixpath(), GNUNET_CONNECTION_create_from_sockaddr(), GNUNET_NAT_AUTO_test_start(), GNUNET_NAT_test_start(), GNUNET_NETWORK_test_port_free(), GNUNET_TESTING_reserve_port(), init_socket(), LEGACY_SERVICE_get_server_addresses(), main(), mq_init(), open_listen_socket(), open_socket(), run(), server_check_ipv6_support(), setup_sockets(), tcp_plugin_get_session(), try_connect_using_address(), try_send_tcp(), try_send_udp(), try_unixpath(), and unix_transport_server_start().

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 918 of file network.c.

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

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

Referenced by GNUNET_NETWORK_fdset_create(), GNUNET_SCHEDULER_do_work(), main(), 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 932 of file network.c.

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

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

Referenced by GNUNET_SERVER_resume(), main(), and 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 950 of file network.c.

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

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

Referenced by do_read(), do_s5r_read(), do_udp_read(), main(), process_listen_socket(), read_response(), receive_ready(), tcp_send(), transmit_ready(), udp_plugin_select_v4(), and udp_plugin_select_v6().

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 965 of file network.c.

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

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 985 of file network.c.

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

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 1001 of file network.c.

1002 {
1003  return desc->fd;
1004 }

References GNUNET_NETWORK_Handle::fd.

Referenced by add_without_sets(), do_accept(), do_dns_read(), GNUNET_CONNECTION_create_from_accept(), 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 1014 of file network.c.

1015 {
1016  return desc->addr;
1017 }

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 1027 of file network.c.

1028 {
1029  return desc->addrlen;
1030 }

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 1041 of file network.c.

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

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

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

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 1058 of file network.c.

1060 {
1061  GNUNET_assert ((nfd >= 0) && (nfd < FD_SETSIZE));
1062  FD_SET (nfd, &to->sds);
1063  to->nsds = GNUNET_MAX (nfd + 1,
1064  to->nsds);
1065 }
#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 1076 of file network.c.

1078 {
1079  if ((-1 == nfd) ||
1080  (NULL == to))
1081  return GNUNET_NO;
1082  return FD_ISSET (nfd, &to->sds) ? GNUNET_YES : GNUNET_NO;
1083 }
@ 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 1092 of file network.c.

1094 {
1095  int fd;
1096 
1099  &fd,
1100  sizeof(int)));
1101  FD_SET (fd,
1102  &fds->sds);
1103  fds->nsds = GNUNET_MAX (fd + 1,
1104  fds->nsds);
1105 }
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(), GNUNET_SCHEDULER_driver_init(), and main().

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 1114 of file network.c.

1116 {
1118 }
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:1092

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 1129 of file network.c.

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

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 1145 of file network.c.

1147 {
1148  int nfds;
1149 
1150  nfds = GNUNET_MIN (fds1->nsds,
1151  fds2->nsds);
1152  while (nfds > 0)
1153  {
1154  nfds--;
1155  if ((FD_ISSET (nfds,
1156  &fds1->sds)) &&
1157  (FD_ISSET (nfds,
1158  &fds2->sds)))
1159  return GNUNET_YES;
1160  }
1161  return GNUNET_NO;
1162 }
#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 1171 of file network.c.

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

References GNUNET_NETWORK_fdset_zero(), and GNUNET_new.

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

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 1187 of file network.c.

1188 {
1189  GNUNET_free (fds);
1190 }

References GNUNET_free.

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

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 1201 of file network.c.

1203 {
1204  struct GNUNET_NETWORK_Handle *socket;
1205  int bind_status;
1206  int socktype;
1207  char open_port_str[6];
1208  struct addrinfo hint;
1209  struct addrinfo *ret;
1210  struct addrinfo *ai;
1211 
1212  GNUNET_snprintf (open_port_str,
1213  sizeof(open_port_str),
1214  "%u",
1215  (unsigned int) port);
1216  socktype = (IPPROTO_TCP == ipproto) ? SOCK_STREAM : SOCK_DGRAM;
1217  ret = NULL;
1218  memset (&hint, 0, sizeof(hint));
1219  hint.ai_family = AF_UNSPEC; /* IPv4 and IPv6 */
1220  hint.ai_socktype = socktype;
1221  hint.ai_protocol = ipproto;
1222  hint.ai_addrlen = 0;
1223  hint.ai_addr = NULL;
1224  hint.ai_canonname = NULL;
1225  hint.ai_next = NULL;
1226  hint.ai_flags = AI_PASSIVE | AI_NUMERICSERV; /* Wild card address */
1227  GNUNET_assert (0 == getaddrinfo (NULL,
1228  open_port_str,
1229  &hint,
1230  &ret));
1231  bind_status = GNUNET_NO;
1232  for (ai = ret; NULL != ai; ai = ai->ai_next)
1233  {
1234  socket = GNUNET_NETWORK_socket_create (ai->ai_family,
1235  ai->ai_socktype,
1236  ai->ai_protocol);
1237  if (NULL == socket)
1238  continue;
1239  bind_status = GNUNET_NETWORK_socket_bind (socket,
1240  ai->ai_addr,
1241  ai->ai_addrlen);
1242  GNUNET_NETWORK_socket_close (socket);
1243  if (GNUNET_OK != bind_status)
1244  break;
1245  }
1246  freeaddrinfo (ret);
1247  return bind_status;
1248 }
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:509
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_create(int domain, int type, int protocol)
Create a new socket.
Definition: network.c:833
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:440
#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: