GNUnet  0.11.x
Data Structures | Functions
Network library

Basic low-level networking interface. More...

Data Structures

struct  GNUNET_NETWORK_FDSet
 collection of IO descriptors More...
 

Functions

int 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...
 
int GNUNET_NETWORK_socket_set_blocking (struct GNUNET_NETWORK_Handle *fd, int doBlock)
 Set if a socket should use blocking or non-blocking IO. More...
 
int 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...
 
int 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...
 
int 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...
 
int GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc, int level, int optname, void *optval, socklen_t *optlen)
 Get socket options. More...
 
int 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...
 
int GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc, int how)
 Shut down socket operations. More...
 
int 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()

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

References 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(), tcp_address_to_sockaddr(), and udp_address_to_sockaddr().

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

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

Referenced by get_server_addresses(), LEGACY_SERVICE_get_server_addresses(), and try_unixpath().

159 {
160  struct sockaddr_un dummy;
161  size_t slen;
162  char *end;
163  struct GNUNET_HashCode sh;
165  size_t upm;
166 
167  upm = sizeof(dummy.sun_path);
168  slen = strlen (unixpath);
169  if (slen < upm)
170  return unixpath; /* no shortening required */
171  GNUNET_CRYPTO_hash (unixpath, slen, &sh);
172  while (16 + strlen (unixpath) >= upm)
173  {
174  if (NULL == (end = strrchr (unixpath, '/')))
175  {
177  _ (
178  "Unable to shorten unix path `%s' while keeping name unique\n"),
179  unixpath);
180  GNUNET_free (unixpath);
181  return NULL;
182  }
183  *end = '\0';
184  }
186  ae.encoding[16] = '\0';
187  strcat (unixpath, (char *) ae.encoding);
188  return unixpath;
189 }
static int end
Set if we are to shutdown all services (including ARM).
Definition: gnunet-arm.c:34
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:48
static struct SolverHandle * sh
A 512-bit hashcode.
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:73
0-terminated ASCII encoding of a struct GNUNET_HashCode.
static struct in_addr dummy
Target "dummy" address of the packet we pretend to respond to.
#define GNUNET_log(kind,...)
#define GNUNET_free(ptr)
Wrapper around free.
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 202 of file network.c.

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().

203 {
204  int s;
205  int eno;
206  struct stat sbuf;
207  int ret;
208 
209  s = socket (AF_UNIX, SOCK_STREAM, 0);
210  if (-1 == s)
211  {
213  "Failed to open AF_UNIX socket");
214  return;
215  }
216  ret = connect (s,
217  (struct sockaddr *) un,
218  sizeof(struct sockaddr_un));
219  eno = errno;
220  GNUNET_break (0 == close (s));
221  if (0 == ret)
222  return; /* another process is listening, do not remove! */
223  if (ECONNREFUSED != eno)
224  return; /* some other error, likely "no such file or directory" -- all well */
225  /* should unlink, but sanity checks first */
226  if (0 != stat (un->sun_path,
227  &sbuf))
228  return; /* failed to 'stat', likely does not exist after all */
229  if (S_IFSOCK != (S_IFMT & sbuf.st_mode))
230  return; /* refuse to unlink anything except sockets */
231  /* finally, really unlink */
233  "Removing left-over `%s' from previous exeuction\n",
234  un->sun_path);
235  if (0 != unlink (un->sun_path))
237  "unlink",
238  un->sun_path);
239 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#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 &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
#define GNUNET_log_strerror_file(level, cmd, filename)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
#define GNUNET_log(kind,...)
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

Accept a new connection on a socket.

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

Definition at line 430 of file network.c.

References 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().

433 {
434  struct GNUNET_NETWORK_Handle *ret;
435  int eno;
436 
437  ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
438 #if DEBUG_NETWORK
439  {
440  struct sockaddr_storage name;
441  socklen_t namelen = sizeof(name);
442 
443  int gsn = getsockname (desc->fd,
444  (struct sockaddr *) &name,
445  &namelen);
446 
447  if (0 == gsn)
449  "Accepting connection on `%s'\n",
450  GNUNET_a2s ((const struct sockaddr *) &name,
451  namelen));
452  }
453 #endif
454  ret->fd = accept (desc->fd,
455  address,
456  address_len);
457  if (-1 == ret->fd)
458  {
459  eno = errno;
460  GNUNET_free (ret);
461  errno = eno;
462  return NULL;
463  }
464  if (GNUNET_OK !=
466  (NULL != address) ? address->sa_family :
467  desc->af,
468  SOCK_STREAM))
469  {
470  return NULL;
471  }
472  return ret;
473 }
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:366
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_new(type)
Allocate a struct or union of the given type.
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)...
const char * name
#define LOG(kind,...)
Definition: network.c:31
handle to a socket
Definition: network.c:52
int af
Address family / domain.
Definition: network.c:59
static char * address
GNS address for this phone.
#define GNUNET_free(ptr)
Wrapper around free.
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 636 of file network.c.

References GNUNET_NETWORK_Handle::af, GNUNET_NETWORK_Handle::fd, GNUNET_new, and ret.

Referenced by extract_handles(), and setup_service().

637 {
638  struct GNUNET_NETWORK_Handle *ret;
639 
640  if (fcntl (fd, F_GETFD) < 0)
641  return NULL; /* invalid FD */
642  ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
643  ret->fd = fd;
644  ret->af = AF_UNSPEC;
645  return ret;
646 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_new(type)
Allocate a struct or union of the given type.
handle to a socket
Definition: network.c:52
int af
Address family / domain.
Definition: network.c:59
Here is the caller graph for this function:

◆ GNUNET_NETWORK_socket_set_blocking()

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

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

Referenced by initialize_network_handle(), and main().

257 {
258  int flags = fcntl (fd->fd, F_GETFL);
259 
260  if (flags == -1)
261  {
263  "fcntl");
264  return GNUNET_SYSERR;
265  }
266  if (doBlock)
267  flags &= ~O_NONBLOCK;
268 
269  else
270  flags |= O_NONBLOCK;
271  if (0 != fcntl (fd->fd,
272  F_SETFL,
273  flags))
274 
275  {
277  "fcntl");
278  return GNUNET_SYSERR;
279  }
280  return GNUNET_OK;
281 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
#define LOG_STRERROR(kind, syscall)
Definition: network.c:37
Here is the caller graph for this function:

◆ GNUNET_NETWORK_socket_bind()

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

References GNUNET_NETWORK_Handle::addr, 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(), open_listen_socket(), open_socket(), run(), setup_sockets(), and unix_transport_server_start().

488 {
489  int ret;
490 
491 #ifdef IPV6_V6ONLY
492 #ifdef IPPROTO_IPV6
493  {
494  const int on = 1;
495 
496  if (AF_INET6 == desc->af)
497  if (setsockopt (desc->fd,
498  IPPROTO_IPV6,
499  IPV6_V6ONLY,
500  (const void *) &on,
501  sizeof(on)))
503  "setsockopt");
504  }
505 #endif
506 #endif
507  if (AF_UNIX == address->sa_family)
508  GNUNET_NETWORK_unix_precheck ((const struct sockaddr_un *) address);
509  {
510  const int on = 1;
511 
512  /* This is required here for TCP sockets, but only on UNIX */
513  if ((SOCK_STREAM == desc->type) &&
514  (0 != setsockopt (desc->fd,
515  SOL_SOCKET,
516  SO_REUSEADDR,
517  &on, sizeof(on))))
519  "setsockopt");
520  }
521  {
522  /* set permissions of newly created non-abstract UNIX domain socket to
523  "user-only"; applications can choose to relax this later */
524  mode_t old_mask = 0; /* assigned to make compiler happy */
525  const struct sockaddr_un *un = (const struct sockaddr_un *) address;
526  int not_abstract = 0;
527 
528  if ((AF_UNIX == address->sa_family)
529  && ('\0' != un->sun_path[0])) /* Not an abstract socket */
530  not_abstract = 1;
531  if (not_abstract)
532  old_mask = umask (S_IWGRP | S_IRGRP | S_IXGRP | S_IWOTH | S_IROTH
533  | S_IXOTH);
534 
535  ret = bind (desc->fd,
536  address,
537  address_len);
538 
539  if (not_abstract)
540  (void) umask (old_mask);
541  }
542  if (0 != ret)
543  return GNUNET_SYSERR;
544 
545  desc->addr = GNUNET_malloc (address_len);
546  GNUNET_memcpy (desc->addr, address, address_len);
547  desc->addrlen = address_len;
548 
549  return GNUNET_OK;
550 }
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
struct sockaddr * addr
Address we were bound to, or NULL.
Definition: network.c:74
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
socklen_t addrlen
Number of bytes in addr.
Definition: network.c:69
#define LOG_STRERROR(kind, syscall)
Definition: network.c:37
int af
Address family / domain.
Definition: network.c:59
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:202
static char * address
GNS address for this phone.
int type
Type of the socket.
Definition: network.c:64
#define GNUNET_malloc(size)
Wrapper around malloc.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_NETWORK_socket_close()

int GNUNET_NETWORK_socket_close ( struct GNUNET_NETWORK_Handle desc)

Close a socket.

Parameters
descsocket to close
Returns
GNUNET_OK on success, GNUNET_SYSERR otherwise
Parameters
descsocket
Returns
GNUNET_OK on success, GNUNET_SYSERR otherwise

Definition at line 560 of file network.c.

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, len, LOG_STRERROR_FILE, and ret.

Referenced by accept_client(), bind_v4(), bind_v6(), cancel_aps(), cleanup_rs(), cleanup_s5r(), connect_probe_continuation(), connection_client_destroy_impl(), create_listen_socket(), do_dns_read(), do_read(), do_shutdown(), do_udp_read(), finish_client_drop(), free_proto_queue(), 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(), initialize_network_handle(), kill_httpd(), LEGACY_SERVICE_get_server_addresses(), libgnunet_plugin_transport_udp_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().

561 {
562  int ret;
563 
564  ret = close (desc->fd);
565 
566  const struct sockaddr_un *un = (const struct sockaddr_un *) desc->addr;
567 
568  /* Cleanup the UNIX domain socket and its parent directories in case of non
569  abstract sockets */
570  if ((AF_UNIX == desc->af) &&
571  (NULL != desc->addr) &&
572  ('\0' != un->sun_path[0]))
573  {
574  char *dirname = GNUNET_strndup (un->sun_path,
575  sizeof(un->sun_path));
576 
577  if (0 != unlink (dirname))
578  {
580  "unlink",
581  dirname);
582  }
583  else
584  {
585  size_t len;
586 
587  len = strlen (dirname);
588  while ((len > 0) && (dirname[len] != DIR_SEPARATOR))
589  len--;
590  dirname[len] = '\0';
591  if ((0 != len) && (0 != rmdir (dirname)))
592  {
593  switch (errno)
594  {
595  case EACCES:
596  case ENOTEMPTY:
597  case EPERM:
598  /* these are normal and can just be ignored */
599  break;
600 
601  default:
603  "rmdir",
604  dirname);
605  break;
606  }
607  }
608  }
609  GNUNET_free (dirname);
610  }
612  return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
613 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
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:622
#define GNUNET_log_strerror_file(level, cmd, filename)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
#define LOG_STRERROR_FILE(kind, syscall, filename)
Definition: network.c:32
struct sockaddr * addr
Address we were bound to, or NULL.
Definition: network.c:74
#define DIR_SEPARATOR
Definition: platform.h:167
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
#define GNUNET_strndup(a, length)
Wrapper around GNUNET_xstrndup_.
int af
Address family / domain.
Definition: network.c:59
#define GNUNET_free(ptr)
Wrapper around free.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the call 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 622 of file network.c.

References GNUNET_NETWORK_Handle::addr, GNUNET_free, and GNUNET_free_non_null.

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

623 {
624  GNUNET_free_non_null (desc->addr);
625  GNUNET_free (desc);
626 }
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
struct sockaddr * addr
Address we were bound to, or NULL.
Definition: network.c:74
#define GNUNET_free(ptr)
Wrapper around free.
Here is the caller graph for this function:

◆ GNUNET_NETWORK_socket_connect()

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

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

Referenced by GNUNET_CONNECTION_connect_socket(), GNUNET_CONNECTION_create_from_connect_to_unixpath(), main(), mq_init(), try_connect_using_address(), try_send_tcp(), and try_unixpath().

661 {
662  int ret;
663 
664  ret = connect (desc->fd,
665  address,
666  address_len);
667 
668  return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
669 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
static char * address
GNS address for this phone.
Here is the caller graph for this function:

◆ GNUNET_NETWORK_socket_getsockopt()

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

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

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

688 {
689  int ret;
690 
691  ret = getsockopt (desc->fd,
692  level,
693  optname,
694  optval, optlen);
695 
696  return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
697 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
Here is the caller graph for this function:

◆ GNUNET_NETWORK_socket_listen()

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

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

710 {
711  int ret;
712 
713  ret = listen (desc->fd,
714  backlog);
715 
716  return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
717 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
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 727 of file network.c.

References GNUNET_NETWORK_Handle::fd, GNUNET_SYSERR, and pending.

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

728 {
729  int error;
730 
731  /* How much is there to be read? */
732  int pending;
733 
734  error = ioctl (desc->fd,
735  FIONREAD,
736  &pending);
737  if (0 == error)
738  return (ssize_t) pending;
739  return GNUNET_SYSERR;
740 }
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
static unsigned int pending
The number of queries that are outstanding.
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
descsocket
bufferbuffer
lengthlength of buffer
src_addreither the source to recv from, or all zeroes to be filled in by recvfrom
addrlenlength of the src_addr

Definition at line 754 of file network.c.

References GNUNET_NETWORK_Handle::fd, and ret.

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().

759 {
760  int ret;
761  int flags;
762 
763  flags = 0;
764 
765 #ifdef MSG_DONTWAIT
766  flags |= MSG_DONTWAIT;
767 #endif
768  ret = recvfrom (desc->fd,
769  buffer,
770  length,
771  flags,
772  src_addr,
773  addrlen);
774  return ret;
775 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
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 787 of file network.c.

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(), queue_read_kx(), and receive_ready().

790 {
791  int ret;
792  int flags;
793 
794  flags = 0;
795 
796 #ifdef MSG_DONTWAIT
797  flags |= MSG_DONTWAIT;
798 #endif
799  ret = recv (desc->fd,
800  buffer,
801  length,
802  flags);
803  return ret;
804 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
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 1340 of file network.c.

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, and GNUNET_NETWORK_FDSet::sds.

Referenced by main(), select_loop(), and short_wait().

1344 {
1345  int nfds;
1346  struct timeval tv;
1347 
1348  if (NULL != rfds)
1349  nfds = rfds->nsds;
1350  else
1351  nfds = 0;
1352  if (NULL != wfds)
1353  nfds = GNUNET_MAX (nfds,
1354  wfds->nsds);
1355  if (NULL != efds)
1356  nfds = GNUNET_MAX (nfds,
1357  efds->nsds);
1358  if ((0 == nfds) &&
1359  (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us))
1360  {
1361  GNUNET_break (0);
1363  _ (
1364  "Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1365  "select");
1366  }
1367  if (timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us > (unsigned
1368  long long)
1369  LONG_MAX)
1370  {
1371  tv.tv_sec = LONG_MAX;
1372  tv.tv_usec = 999999L;
1373  }
1374  else
1375  {
1376  tv.tv_sec = (long) (timeout.rel_value_us
1377  / GNUNET_TIME_UNIT_SECONDS.rel_value_us);
1378  tv.tv_usec =
1379  (timeout.rel_value_us
1380  - (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value_us));
1381  }
1382  return select (nfds,
1383  (NULL != rfds) ? &rfds->sds : NULL,
1384  (NULL != wfds) ? &wfds->sds : NULL,
1385  (NULL != efds) ? &efds->sds : NULL,
1386  (timeout.rel_value_us ==
1387  GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) ? NULL : &tv);
1388 }
int nsds
Maximum number of any socket descriptor in the set (plus one)
uint64_t rel_value_us
The actual value.
#define GNUNET_TIME_UNIT_SECONDS
One second.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
#define GNUNET_MAX(a, b)
Definition: gnunet_common.h:82
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
fd_set sds
Bitset with the descriptors.
#define LOG(kind,...)
Definition: network.c:31
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
Parameters
descsocket
bufferdata to send
lengthsize of the buffer
Returns
number of bytes sent, GNUNET_SYSERR on error

Definition at line 816 of file network.c.

References GNUNET_NETWORK_Handle::fd, and ret.

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

819 {
820  int ret;
821  int flags;
822 
823  flags = 0;
824 #ifdef MSG_DONTWAIT
825  flags |= MSG_DONTWAIT;
826 #endif
827 #ifdef MSG_NOSIGNAL
828  flags |= MSG_NOSIGNAL;
829 #endif
830  ret = send (desc->fd,
831  buffer,
832  length,
833  flags);
834  return ret;
835 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
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 850 of file network.c.

References GNUNET_NETWORK_Handle::fd, and ret.

Referenced by ifc_broadcast(), mq_send(), select_write_cb(), send_response(), stun_dns_callback(), transmit_query(), try_send_udp(), udp_ipv4_broadcast_send(), udp_ipv6_broadcast_send(), udp_select_send(), and unix_real_send().

855 {
856  int ret;
857  int flags;
858 
859  flags = 0;
860 
861 #ifdef MSG_DONTWAIT
862  flags |= MSG_DONTWAIT;
863 #endif
864 #ifdef MSG_NOSIGNAL
865  flags |= MSG_NOSIGNAL;
866 #endif
867  ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len);
868  return ret;
869 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
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 883 of file network.c.

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

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

888 {
889  int ret;
890 
891  ret = setsockopt (fd->fd,
892  level,
893  option_name,
894  option_value,
895  option_len);
896 
897  return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
898 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
Here is the caller graph for this function:

◆ GNUNET_NETWORK_socket_shutdown()

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

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

Referenced by GNUNET_CONNECTION_create_from_accept(), GNUNET_CONNECTION_destroy(), signal_transmit_error(), and tcp_send().

942 {
943  int ret;
944 
945  ret = shutdown (desc->fd, how);
946 
947  return (0 == ret) ? GNUNET_OK : GNUNET_SYSERR;
948 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
Here is the caller graph for this function:

◆ GNUNET_NETWORK_socket_disable_corking()

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

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

Referenced by GNUNET_CONNECTION_disable_corking().

962 {
963  int ret = 0;
964 
965 #if LINUX
966  int value = 0;
967 
968  if (0 !=
969  (ret =
970  setsockopt (desc->fd,
971  SOL_SOCKET,
972  SO_SNDBUF,
973  &value,
974  sizeof(value))))
976  "setsockopt");
977  if (0 !=
978  (ret =
979  setsockopt (desc->fd,
980  SOL_SOCKET,
981  SO_RCVBUF,
982  &value,
983  sizeof(value))))
985  "setsockopt");
986 #endif
987  return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
988 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
static char * value
Value of the record to add/remove.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
#define LOG_STRERROR(kind, syscall)
Definition: network.c:37
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 912 of file network.c.

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

Referenced by bind_v4(), bind_v6(), create_listen_socket(), 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(), 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().

915 {
916  struct GNUNET_NETWORK_Handle *ret;
917  int fd;
918 
919  fd = socket (domain, type, protocol);
920  if (-1 == fd)
921  return NULL;
922  ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
923  ret->fd = fd;
924  if (GNUNET_OK !=
926  domain,
927  type))
928  return NULL;
929  return ret;
930 }
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:366
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_new(type)
Allocate a struct or union of the given type.
handle to a socket
Definition: network.c:52
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
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 997 of file network.c.

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

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

998 {
999  FD_ZERO (&fds->sds);
1000  fds->nsds = 0;
1001 }
int nsds
Maximum number of any socket descriptor in the set (plus one)
fd_set sds
Bitset with the descriptors.
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 1011 of file network.c.

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().

1013 {
1014  FD_SET (desc->fd,
1015  &fds->sds);
1016  fds->nsds = GNUNET_MAX (fds->nsds,
1017  desc->fd + 1);
1018 }
int nsds
Maximum number of any socket descriptor in the set (plus one)
#define GNUNET_MAX(a, b)
Definition: gnunet_common.h:82
fd_set sds
Bitset with the descriptors.
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 1029 of file network.c.

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().

1031 {
1032  return FD_ISSET (desc->fd,
1033  &fds->sds);
1034 }
fd_set sds
Bitset with the descriptors.
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 1044 of file network.c.

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

1046 {
1047  int nfds;
1048 
1049  for (nfds = src->nsds; nfds >= 0; nfds--)
1050  if (FD_ISSET (nfds, &src->sds))
1051  FD_SET (nfds, &dst->sds);
1052  dst->nsds = GNUNET_MAX (dst->nsds,
1053  src->nsds);
1054 }
int nsds
Maximum number of any socket descriptor in the set (plus one)
#define GNUNET_MAX(a, b)
Definition: gnunet_common.h:82
fd_set sds
Bitset with the descriptors.

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

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

1066 {
1067  FD_COPY (&from->sds,
1068  &to->sds);
1069  to->nsds = from->nsds;
1070 }
int nsds
Maximum number of any socket descriptor in the set (plus one)
fd_set sds
Bitset with the descriptors.
#define FD_COPY(s, d)
Definition: network.c:243

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

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(), mhd_connection_cb(), run(), setup_data_transfer(), and start_process().

1081 {
1082  return desc->fd;
1083 }
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 1093 of file network.c.

References GNUNET_NETWORK_Handle::addr.

Referenced by do_accept(), and setup_data_transfer().

1094 {
1095  return desc->addr;
1096 }
struct sockaddr * addr
Address we were bound to, or NULL.
Definition: network.c:74
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 1106 of file network.c.

References GNUNET_NETWORK_Handle::addrlen.

Referenced by do_accept(), and setup_data_transfer().

1107 {
1108  return desc->addrlen;
1109 }
socklen_t addrlen
Number of bytes in addr.
Definition: network.c:69
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 1120 of file network.c.

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().

1123 {
1124  FD_COPY (from,
1125  &to->sds);
1126  to->nsds = nfds;
1127 }
int nsds
Maximum number of any socket descriptor in the set (plus one)
fd_set sds
Bitset with the descriptors.
#define FD_COPY(s, d)
Definition: network.c:243
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 1137 of file network.c.

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().

1139 {
1140  GNUNET_assert ((nfd >= 0) && (nfd < FD_SETSIZE));
1141  FD_SET (nfd, &to->sds);
1142  to->nsds = GNUNET_MAX (nfd + 1,
1143  to->nsds);
1144 }
int nsds
Maximum number of any socket descriptor in the set (plus one)
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_MAX(a, b)
Definition: gnunet_common.h:82
fd_set sds
Bitset with the descriptors.
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 1155 of file network.c.

References GNUNET_NO, GNUNET_YES, and GNUNET_NETWORK_FDSet::sds.

Referenced by extract_handles(), and select_loop().

1157 {
1158  if ((-1 == nfd) ||
1159  (NULL == to))
1160  return GNUNET_NO;
1161  return FD_ISSET (nfd, &to->sds) ? GNUNET_YES : GNUNET_NO;
1162 }
#define GNUNET_NO
Definition: gnunet_common.h:78
fd_set sds
Bitset with the descriptors.
#define GNUNET_YES
Definition: gnunet_common.h:77
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 1171 of file network.c.

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

Referenced by GNUNET_NETWORK_fdset_handle_set_first(), GNUNET_SCHEDULER_driver_init(), and main().

1173 {
1174  int fd;
1175 
1178  &fd,
1179  sizeof(int)));
1180  FD_SET (fd,
1181  &fds->sds);
1182  fds->nsds = GNUNET_MAX (fd + 1,
1183  fds->nsds);
1184 }
int nsds
Maximum number of any socket descriptor in the set (plus one)
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_MAX(a, b)
Definition: gnunet_common.h:82
int GNUNET_DISK_internal_file_handle_(const struct GNUNET_DISK_FileHandle *fh, void *dst, size_t dst_len)
Retrieve OS file handle.
Definition: disk.c:1778
fd_set sds
Bitset with the descriptors.
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 1193 of file network.c.

References GNUNET_NETWORK_fdset_handle_set().

1195 {
1197 }
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:1171
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 1208 of file network.c.

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

Referenced by cmd_read().

1210 {
1211  return FD_ISSET (h->fd,
1212  &fds->sds);
1213 }
fd_set sds
Bitset with the descriptors.
int fd
File handle on Unix-like systems.
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 1224 of file network.c.

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

1226 {
1227  int nfds;
1228 
1229  nfds = GNUNET_MIN (fds1->nsds,
1230  fds2->nsds);
1231  while (nfds > 0)
1232  {
1233  nfds--;
1234  if ((FD_ISSET (nfds,
1235  &fds1->sds)) &&
1236  (FD_ISSET (nfds,
1237  &fds2->sds)))
1238  return GNUNET_YES;
1239  }
1240  return GNUNET_NO;
1241 }
int nsds
Maximum number of any socket descriptor in the set (plus one)
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_MIN(a, b)
Definition: gnunet_common.h:80
fd_set sds
Bitset with the descriptors.
#define GNUNET_YES
Definition: gnunet_common.h:77

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

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().

1251 {
1252  struct GNUNET_NETWORK_FDSet *fds;
1253 
1254  fds = GNUNET_new (struct GNUNET_NETWORK_FDSet);
1256  return fds;
1257 }
#define GNUNET_new(type)
Allocate a struct or union of the given type.
collection of IO descriptors
void GNUNET_NETWORK_fdset_zero(struct GNUNET_NETWORK_FDSet *fds)
Reset FD set.
Definition: network.c:997
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 1266 of file network.c.

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().

1267 {
1268  GNUNET_free (fds);
1269 }
#define GNUNET_free(ptr)
Wrapper around free.
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 (i.e. IPPROTO_TCP)
portport number to test
Returns
GNUNET_OK if the port is available, GNUNET_NO if not

Definition at line 1280 of file network.c.

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

1282 {
1283  struct GNUNET_NETWORK_Handle *socket;
1284  int bind_status;
1285  int socktype;
1286  char open_port_str[6];
1287  struct addrinfo hint;
1288  struct addrinfo *ret;
1289  struct addrinfo *ai;
1290 
1291  GNUNET_snprintf (open_port_str,
1292  sizeof(open_port_str),
1293  "%u",
1294  (unsigned int) port);
1295  socktype = (IPPROTO_TCP == ipproto) ? SOCK_STREAM : SOCK_DGRAM;
1296  ret = NULL;
1297  memset (&hint, 0, sizeof(hint));
1298  hint.ai_family = AF_UNSPEC; /* IPv4 and IPv6 */
1299  hint.ai_socktype = socktype;
1300  hint.ai_protocol = ipproto;
1301  hint.ai_addrlen = 0;
1302  hint.ai_addr = NULL;
1303  hint.ai_canonname = NULL;
1304  hint.ai_next = NULL;
1305  hint.ai_flags = AI_PASSIVE | AI_NUMERICSERV; /* Wild card address */
1306  GNUNET_assert (0 == getaddrinfo (NULL,
1307  open_port_str,
1308  &hint,
1309  &ret));
1310  bind_status = GNUNET_NO;
1311  for (ai = ret; NULL != ai; ai = ai->ai_next)
1312  {
1313  socket = GNUNET_NETWORK_socket_create (ai->ai_family,
1314  ai->ai_socktype,
1315  ai->ai_protocol);
1316  if (NULL == socket)
1317  continue;
1318  bind_status = GNUNET_NETWORK_socket_bind (socket,
1319  ai->ai_addr,
1320  ai->ai_addrlen);
1321  GNUNET_NETWORK_socket_close (socket);
1322  if (GNUNET_OK != bind_status)
1323  break;
1324  }
1325  freeaddrinfo (ret);
1326  return bind_status;
1327 }
#define AI_NUMERICSERV
AI_NUMERICSERV not defined in windows.
Definition: platform.h:227
int GNUNET_snprintf(char *buf, size_t size, const char *format,...)
Like snprintf, just aborts if the buffer is of insufficient size.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
int 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:485
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
static struct GNUNET_TRANSPORT_AddressIdentifier * ai
Handle to the operation that publishes our address.
static uint16_t port
Port number.
Definition: gnunet-bcd.c:81
handle to a socket
Definition: network.c:52
int GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc)
Close a socket.
Definition: network.c:560
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_create(int domain, int type, int protocol)
Create a new socket.
Definition: network.c:912
Here is the call graph for this function: