GNUnet  0.10.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 (SOCKTYPE 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 84 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().

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

158 {
159  struct sockaddr_un dummy;
160  size_t slen;
161  char *end;
162  struct GNUNET_HashCode sh;
164  size_t upm;
165 
166  upm = sizeof (dummy.sun_path);
167  slen = strlen (unixpath);
168  if (slen < upm)
169  return unixpath; /* no shortening required */
170  GNUNET_CRYPTO_hash (unixpath, slen, &sh);
171  while (16 + strlen (unixpath) >= upm)
172  {
173  if (NULL == (end = strrchr (unixpath, '/')))
174  {
176  _("Unable to shorten unix path `%s' while keeping name unique\n"),
177  unixpath);
178  GNUNET_free (unixpath);
179  return NULL;
180  }
181  *end = '\0';
182  }
184  ae.encoding[16] = '\0';
185  strcat (unixpath, (char *) ae.encoding);
186  return unixpath;
187 }
static int end
Set if we are to shutdown all services (including ARM).
Definition: gnunet-arm.c:34
static struct in_addr dummy
Target "dummy" address.
#define _(String)
GNU gettext support macro.
Definition: platform.h:208
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:44
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:69
0-terminated ASCII encoding of a struct GNUNET_HashCode.
#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 201 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().

202 {
203  int s;
204  int eno;
205  struct stat sbuf;
206  int ret;
207 
208  s = socket (AF_UNIX, SOCK_STREAM, 0);
209  if (-1 == s)
210  {
212  "Failed to open AF_UNIX socket");
213  return;
214  }
215  ret = connect (s,
216  (struct sockaddr *) un,
217  sizeof (struct sockaddr_un));
218  eno = errno;
219  GNUNET_break (0 == close (s));
220  if (0 == ret)
221  return; /* another process is listening, do not remove! */
222  if (ECONNREFUSED != eno)
223  return; /* some other error, likely "no such file or directory" -- all well */
224  /* should unlink, but sanity checks first */
225  if (0 != stat (un->sun_path,
226  &sbuf))
227  return; /* failed to 'stat', likely does not exist after all */
228  if (S_IFSOCK != (S_IFMT & sbuf.st_mode))
229  return; /* refuse to unlink anything except sockets */
230  /* finally, really unlink */
232  "Removing left-over `%s' from previous exeuction\n",
233  un->sun_path);
234  if (0 != unlink (un->sun_path))
236  "unlink",
237  un->sun_path);
238 }
static int ret
Final status code.
Definition: gnunet-arm.c:89
#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 468 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().

471 {
472  struct GNUNET_NETWORK_Handle *ret;
473  int eno;
474 
475  ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
476 #if DEBUG_NETWORK
477  {
478  struct sockaddr_storage name;
479  socklen_t namelen = sizeof (name);
480 
481  int gsn = getsockname (desc->fd,
482  (struct sockaddr *) &name,
483  &namelen);
484 
485  if (0 == gsn)
487  "Accepting connection on `%s'\n",
488  GNUNET_a2s ((const struct sockaddr *) &name,
489  namelen));
490  }
491 #endif
492  ret->fd = accept (desc->fd,
493  address,
494  address_len);
495  if (-1 == ret->fd)
496  {
497  eno = errno;
498  GNUNET_free (ret);
499  errno = eno;
500  return NULL;
501  }
502  if (GNUNET_OK !=
504  (NULL != address) ? address->sa_family : desc->af,
505  SOCK_STREAM))
506  {
507  return NULL;
508  }
509  return ret;
510 }
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:408
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define GNUNET_new(type)
Allocate a struct or union of the given type.
static int ret
Final status code.
Definition: gnunet-arm.c:89
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:46
int af
Address family / domain.
Definition: network.c:57
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 ( SOCKTYPE  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 694 of file network.c.

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

Referenced by extract_handles(), get_server_addresses(), LEGACY_SERVICE_get_server_addresses(), and setup_service().

695 {
696  struct GNUNET_NETWORK_Handle *ret;
697 #if MINGW
698  unsigned long i;
699  DWORD d;
700  /* FIXME: Find a better call to check that FD is valid */
701  if (0 !=
702  WSAIoctl (fd, FIONBIO,
703  (void *) &i, sizeof (i),
704  NULL, 0, &d,
705  NULL, NULL))
706  return NULL; /* invalid FD */
707  ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
708  ret->fd = fd;
709  ret->af = AF_UNSPEC;
710  return ret;
711 #else
712  if (fcntl (fd, F_GETFD) < 0)
713  return NULL; /* invalid FD */
714  ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
715  ret->fd = fd;
716  ret->af = AF_UNSPEC;
717  return ret;
718 #endif
719 }
#define GNUNET_new(type)
Allocate a struct or union of the given type.
static int ret
Final status code.
Definition: gnunet-arm.c:89
handle to a socket
Definition: network.c:46
int af
Address family / domain.
Definition: network.c:57
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 256 of file network.c.

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

Referenced by initialize_network_handle(), and main().

258 {
259 
260 #if MINGW
261  u_long mode;
262 
263  mode = !doBlock;
264  if (SOCKET_ERROR ==
265  ioctlsocket (fd->fd,
266  FIONBIO,
267  &mode))
268 
269  {
270  SetErrnoFromWinsockError (WSAGetLastError ());
272  "ioctlsocket");
273  return GNUNET_SYSERR;
274  }
275  return GNUNET_OK;
276 
277 #else
278  /* not MINGW */
279  int flags = fcntl (fd->fd, F_GETFL);
280 
281  if (flags == -1)
282  {
284  "fcntl");
285  return GNUNET_SYSERR;
286  }
287  if (doBlock)
288  flags &= ~O_NONBLOCK;
289 
290  else
291  flags |= O_NONBLOCK;
292  if (0 != fcntl (fd->fd,
293  F_SETFL,
294  flags))
295 
296  {
298  "fcntl");
299  return GNUNET_SYSERR;
300  }
301  return GNUNET_OK;
302 #endif
303 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
#define LOG_STRERROR(kind, syscall)
Definition: network.c:33
static enum @11 mode
Should we do a PUT (mode = 0) or GET (mode = 1);.
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 522 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_device(), open_listen_socket(), open_socket(), run(), setup_sockets(), and unix_transport_server_start().

525 {
526  int ret;
527 
528 #ifdef IPV6_V6ONLY
529 #ifdef IPPROTO_IPV6
530  {
531  const int on = 1;
532 
533  if (AF_INET6 == desc->af)
534  if (setsockopt (desc->fd,
535  IPPROTO_IPV6,
536  IPV6_V6ONLY,
537  (const void *) &on,
538  sizeof (on)))
540  "setsockopt");
541  }
542 #endif
543 #endif
544 #ifndef WINDOWS
545  if (AF_UNIX == address->sa_family)
546  GNUNET_NETWORK_unix_precheck ((const struct sockaddr_un *) address);
547  {
548  const int on = 1;
549 
550  /* This is required here for TCP sockets, but only on UNIX */
551  if ( (SOCK_STREAM == desc->type) &&
552  (0 != setsockopt (desc->fd,
553  SOL_SOCKET,
554  SO_REUSEADDR,
555  &on, sizeof (on))))
557  "setsockopt");
558  }
559  {
560  /* set permissions of newly created non-abstract UNIX domain socket to
561  "user-only"; applications can choose to relax this later */
562  mode_t old_mask = 0; /* assigned to make compiler happy */
563  const struct sockaddr_un *un = (const struct sockaddr_un *) address;
564  int not_abstract = 0;
565 
566  if ((AF_UNIX == address->sa_family)
567  && ('\0' != un->sun_path[0]) ) /* Not an abstract socket */
568  not_abstract = 1;
569  if (not_abstract)
570  old_mask = umask (S_IWGRP | S_IRGRP | S_IXGRP | S_IWOTH | S_IROTH | S_IXOTH);
571 #endif
572 
573  ret = bind (desc->fd,
574  address,
575  address_len);
576 
577 #ifndef WINDOWS
578  if (not_abstract)
579  (void) umask (old_mask);
580  }
581 #endif
582 #ifdef MINGW
583  if (SOCKET_ERROR == ret)
584  SetErrnoFromWinsockError (WSAGetLastError ());
585 #endif
586  if (0 != ret)
587  return GNUNET_SYSERR;
588 #ifndef MINGW
589  desc->addr = GNUNET_malloc (address_len);
590  GNUNET_memcpy (desc->addr, address, address_len);
591  desc->addrlen = address_len;
592 #endif
593  return GNUNET_OK;
594 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
static int ret
Final status code.
Definition: gnunet-arm.c:89
#define GNUNET_memcpy(dst, src, n)
struct sockaddr * addr
Address we were bound to, or NULL.
Definition: network.c:72
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
socklen_t addrlen
Number of bytes in addr.
Definition: network.c:67
#define LOG_STRERROR(kind, syscall)
Definition: network.c:33
int af
Address family / domain.
Definition: network.c:57
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:201
static char * address
GNS address for this phone.
int type
Type of the socket.
Definition: network.c:62
#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 604 of file network.c.

References GNUNET_NETWORK_Handle::addr, GNUNET_NETWORK_Handle::af, DIR_SEPARATOR, GNUNET_NETWORK_Handle::fd, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_log_strerror_file, GNUNET_NETWORK_socket_free_memory_only_(), GNUNET_OK, GNUNET_strndup, GNUNET_SYSERR, len, LOG, 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().

605 {
606  int ret;
607 
608 #ifdef WINDOWS
609  DWORD error = 0;
610 
611  SetLastError (0);
612  ret = closesocket (desc->fd);
613  error = WSAGetLastError ();
614  SetErrnoFromWinsockError (error);
616  "Closed 0x%x, closesocket() returned %d, GLE is %u\n",
617  desc->fd,
618  ret,
619  error);
620 #else
621  ret = close (desc->fd);
622 #endif
623 #ifndef WINDOWS
624  const struct sockaddr_un *un = (const struct sockaddr_un *) desc->addr;
625 
626  /* Cleanup the UNIX domain socket and its parent directories in case of non
627  abstract sockets */
628  if ( (AF_UNIX == desc->af) &&
629  (NULL != desc->addr) &&
630  ('\0' != un->sun_path[0]) )
631  {
632  char *dirname = GNUNET_strndup (un->sun_path,
633  sizeof (un->sun_path));
634 
635  if (0 != unlink (dirname))
636  {
638  "unlink",
639  dirname);
640  }
641  else
642  {
643  size_t len;
644 
645  len = strlen (dirname);
646  while ((len > 0) && (dirname[len] != DIR_SEPARATOR))
647  len--;
648  dirname[len] = '\0';
649  if ((0 != len) && (0 != rmdir (dirname)))
650  {
651  switch (errno)
652  {
653  case EACCES:
654  case ENOTEMPTY:
655  case EPERM:
656  /* these are normal and can just be ignored */
657  break;
658  default:
660  "rmdir",
661  dirname);
662  break;
663  }
664  }
665  }
666  GNUNET_free (dirname);
667  }
668 #endif
670  return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
671 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
static int ret
Final status code.
Definition: gnunet-arm.c:89
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:680
#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
#define DIR_SEPARATOR
Definition: plibc.h:631
struct sockaddr * addr
Address we were bound to, or NULL.
Definition: network.c:72
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
#define GNUNET_strndup(a, length)
Wrapper around GNUNET_xstrndup_.
#define LOG(kind,...)
Definition: network.c:31
int af
Address family / domain.
Definition: network.c:57
#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 680 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().

681 {
682  GNUNET_free_non_null (desc->addr);
683  GNUNET_free (desc);
684 }
#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:72
#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 731 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().

734 {
735  int ret;
736 
737  ret = connect (desc->fd,
738  address,
739  address_len);
740 #ifdef MINGW
741  if (SOCKET_ERROR == ret)
742  {
743  SetErrnoFromWinsockError (WSAGetLastError ());
744  if (errno == EWOULDBLOCK)
745  errno = EINPROGRESS;
746  }
747 #endif
748  return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
749 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
static int ret
Final status code.
Definition: gnunet-arm.c:89
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
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 763 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().

768 {
769  int ret;
770 
771  ret = getsockopt (desc->fd,
772  level,
773  optname,
774  optval, optlen);
775 
776 #ifdef MINGW
777  if ( (0 == ret) &&
778  (SOL_SOCKET == level) &&
779  (SO_ERROR == optname) )
780  *((int *) optval) = GetErrnoFromWinsockError (*((int *) optval));
781  else if (SOCKET_ERROR == ret)
782  SetErrnoFromWinsockError (WSAGetLastError ());
783 #endif
784  return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
785 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
static int ret
Final status code.
Definition: gnunet-arm.c:89
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
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 796 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_device(), open_listen_socket(), and run().

798 {
799  int ret;
800 
801  ret = listen (desc->fd,
802  backlog);
803 #ifdef MINGW
804  if (SOCKET_ERROR == ret)
805  SetErrnoFromWinsockError (WSAGetLastError ());
806 #endif
807  return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
808 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
static int ret
Final status code.
Definition: gnunet-arm.c:89
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
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 818 of file network.c.

References GNUNET_NETWORK_Handle::fd, GNUNET_SYSERR, and pending.

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

819 {
820  int error;
821 
822  /* How much is there to be read? */
823 #ifndef WINDOWS
824  int pending;
825 
826  error = ioctl (desc->fd,
827  FIONREAD,
828  &pending);
829  if (0 == error)
830  return (ssize_t) pending;
831  return GNUNET_SYSERR;
832 #else
833  u_long pending;
834 
835  error = ioctlsocket (desc->fd,
836  FIONREAD,
837  &pending);
838  if (error != SOCKET_ERROR)
839  return (ssize_t) pending;
840  return GNUNET_SYSERR;
841 #endif
842 }
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
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 856 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().

861 {
862  int ret;
863  int flags;
864 
865  flags = 0;
866 
867 #ifdef MSG_DONTWAIT
868  flags |= MSG_DONTWAIT;
869 
870 #endif
871  ret = recvfrom (desc->fd,
872  buffer,
873  length,
874  flags,
875  src_addr,
876  addrlen);
877 #ifdef MINGW
878  if (SOCKET_ERROR == ret)
879  SetErrnoFromWinsockError (WSAGetLastError ());
880 #endif
881  return ret;
882 }
static int ret
Final status code.
Definition: gnunet-arm.c:89
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 894 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(), read_from_the_socket(), and receive_ready().

897 {
898  int ret;
899  int flags;
900 
901  flags = 0;
902 
903 #ifdef MSG_DONTWAIT
904  flags |= MSG_DONTWAIT;
905 #endif
906  ret = recv (desc->fd,
907  buffer,
908  length,
909  flags);
910 #ifdef MINGW
911  if (SOCKET_ERROR == ret)
912  SetErrnoFromWinsockError (WSAGetLastError ());
913 #endif
914  return ret;
915 }
static int ret
Final status code.
Definition: gnunet-arm.c:89
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 1812 of file network.c.

References _, FD_COPY, FD_SETSIZE, fh, GNUNET_assert, GNUNET_break, GNUNET_DISK_HANLDE_TYPE_EVENT, GNUNET_DISK_HANLDE_TYPE_FILE, GNUNET_DISK_HANLDE_TYPE_PIPE, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_MAX, GNUNET_NETWORK_fdset_copy_native(), GNUNET_NETWORK_fdset_zero(), GNUNET_NO, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_TIME_UNIT_MICROSECONDS, GNUNET_TIME_UNIT_MILLISECONDS, GNUNET_TIME_UNIT_SECONDS, GNUNET_YES, LOG, GNUNET_NETWORK_FDSet::nsds, GNUNET_TIME_Relative::rel_value_us, GNUNET_NETWORK_FDSet::sds, and timeout.

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

1816 {
1817  int nfds;
1818  struct timeval tv;
1819 
1820  if (NULL != rfds)
1821  nfds = rfds->nsds;
1822  else
1823  nfds = 0;
1824  if (NULL != wfds)
1825  nfds = GNUNET_MAX (nfds,
1826  wfds->nsds);
1827  if (NULL != efds)
1828  nfds = GNUNET_MAX (nfds,
1829  efds->nsds);
1830  if ((0 == nfds) &&
1831  (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us))
1832  {
1833  GNUNET_break (0);
1835  _("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1836  "select");
1837  }
1838  if (timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us > (unsigned long long) LONG_MAX)
1839  {
1840  tv.tv_sec = LONG_MAX;
1841  tv.tv_usec = 999999L;
1842  }
1843  else
1844  {
1845  tv.tv_sec = (long) (timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us);
1846  tv.tv_usec =
1847  (timeout.rel_value_us -
1848  (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value_us));
1849  }
1850  return select (nfds,
1851  (NULL != rfds) ? &rfds->sds : NULL,
1852  (NULL != wfds) ? &wfds->sds : NULL,
1853  (NULL != efds) ? &efds->sds : NULL,
1854  (timeout.rel_value_us ==
1855  GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) ? NULL : &tv);
1856 }
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:208
#define GNUNET_MAX(a, b)
Definition: gnunet_common.h:85
#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 call graph for this function:
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 927 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().

930 {
931  int ret;
932  int flags;
933 
934  flags = 0;
935 #ifdef MSG_DONTWAIT
936  flags |= MSG_DONTWAIT;
937 
938 #endif
939 #ifdef MSG_NOSIGNAL
940  flags |= MSG_NOSIGNAL;
941 
942 #endif
943  ret = send (desc->fd,
944  buffer,
945  length,
946  flags);
947 #ifdef MINGW
948  if (SOCKET_ERROR == ret)
949  SetErrnoFromWinsockError (WSAGetLastError ());
950 
951 #endif
952  return ret;
953 }
static int ret
Final status code.
Definition: gnunet-arm.c:89
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 968 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().

973 {
974  int ret;
975  int flags;
976 
977  flags = 0;
978 
979 #ifdef MSG_DONTWAIT
980  flags |= MSG_DONTWAIT;
981 #endif
982 #ifdef MSG_NOSIGNAL
983  flags |= MSG_NOSIGNAL;
984 #endif
985  ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len);
986 #ifdef MINGW
987  if (SOCKET_ERROR == ret)
988  SetErrnoFromWinsockError (WSAGetLastError ());
989 #endif
990  return ret;
991 }
static int ret
Final status code.
Definition: gnunet-arm.c:89
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 1005 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().

1010 {
1011  int ret;
1012 
1013  ret = setsockopt (fd->fd,
1014  level,
1015  option_name,
1016  option_value,
1017  option_len);
1018 #ifdef MINGW
1019  if (SOCKET_ERROR == ret)
1020  SetErrnoFromWinsockError (WSAGetLastError ());
1021 #endif
1022  return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
1023 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
static int ret
Final status code.
Definition: gnunet-arm.c:89
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
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 1065 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().

1067 {
1068  int ret;
1069 
1070  ret = shutdown (desc->fd, how);
1071 #ifdef MINGW
1072  if (0 != ret)
1073  SetErrnoFromWinsockError (WSAGetLastError ());
1074 #endif
1075  return (0 == ret) ? GNUNET_OK : GNUNET_SYSERR;
1076 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
static int ret
Final status code.
Definition: gnunet-arm.c:89
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
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 1089 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().

1090 {
1091  int ret = 0;
1092 
1093 #if WINDOWS
1094  int value = 0;
1095 
1096  if (0 !=
1097  (ret =
1098  setsockopt (desc->fd,
1099  SOL_SOCKET,
1100  SO_SNDBUF,
1101  (char *) &value,
1102  sizeof (value))))
1104  "setsockopt");
1105  if (0 !=
1106  (ret =
1107  setsockopt (desc->fd,
1108  SOL_SOCKET,
1109  SO_RCVBUF,
1110  (char *) &value,
1111  sizeof (value))))
1113  "setsockopt");
1114 #elif LINUX
1115  int value = 0;
1116 
1117  if (0 !=
1118  (ret =
1119  setsockopt (desc->fd,
1120  SOL_SOCKET,
1121  SO_SNDBUF,
1122  &value,
1123  sizeof (value))))
1125  "setsockopt");
1126  if (0 !=
1127  (ret =
1128  setsockopt (desc->fd,
1129  SOL_SOCKET,
1130  SO_RCVBUF,
1131  &value,
1132  sizeof (value))))
1134  "setsockopt");
1135 #endif
1136  return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
1137 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
static int ret
Final status code.
Definition: gnunet-arm.c:89
static char * value
Value of the record to add/remove.
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
#define LOG_STRERROR(kind, syscall)
Definition: network.c:33
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 1037 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().

1040 {
1041  struct GNUNET_NETWORK_Handle *ret;
1042  int fd;
1043 
1044  fd = socket (domain, type, protocol);
1045  if (-1 == fd)
1046  return NULL;
1047  ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
1048  ret->fd = fd;
1049  if (GNUNET_OK !=
1051  domain,
1052  type))
1053  return NULL;
1054  return ret;
1055 }
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:408
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define GNUNET_new(type)
Allocate a struct or union of the given type.
static int ret
Final status code.
Definition: gnunet-arm.c:89
handle to a socket
Definition: network.c:46
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 1146 of file network.c.

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

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

1147 {
1148  FD_ZERO (&fds->sds);
1149  fds->nsds = 0;
1150 #ifdef MINGW
1151  fds->handles_pos = 0;
1152 #endif
1153 }
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 1163 of file network.c.

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

Referenced by GNUNET_SCHEDULER_add_net_with_priority(), GNUNET_SERVER_resume(), main(), and schedule_read().

1165 {
1166  FD_SET (desc->fd,
1167  &fds->sds);
1168  fds->nsds = GNUNET_MAX (fds->nsds,
1169  desc->fd + 1);
1170 }
int nsds
Maximum number of any socket descriptor in the set (plus one)
#define GNUNET_MAX(a, b)
Definition: gnunet_common.h:85
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 1181 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().

1183 {
1184  return FD_ISSET (desc->fd,
1185  &fds->sds);
1186 }
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 1196 of file network.c.

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

1198 {
1199 #ifndef MINGW
1200  int nfds;
1201 
1202  for (nfds = src->nsds; nfds >= 0; nfds--)
1203  if (FD_ISSET (nfds, &src->sds))
1204  FD_SET (nfds, &dst->sds);
1205  dst->nsds = GNUNET_MAX (dst->nsds,
1206  src->nsds);
1207 #else
1208  /* This is MinGW32-specific implementation that relies on the code that
1209  * winsock2.h defines for FD_SET. Namely, it relies on FD_SET checking
1210  * that fd being added is not already in the set.
1211  * Also relies on us knowing what's inside fd_set (fd_count and fd_array).
1212  *
1213  * NOTE: I don't understand why the UNIX-logic wouldn't work
1214  * for the first part here as well. -CG
1215  */
1216  unsigned int i;
1217 
1218  for (i = 0; i < src->sds.fd_count; i++)
1219  FD_SET (src->sds.fd_array[i],
1220  &dst->sds);
1221  dst->nsds = GNUNET_MAX (src->nsds,
1222  dst->nsds);
1223 
1224  /* also copy over `struct GNUNET_DISK_FileHandle` array */
1225  if (dst->handles_pos + src->handles_pos > dst->handles_size)
1226  GNUNET_array_grow (dst->handles,
1227  dst->handles_size,
1228  ((dst->handles_pos + src->handles_pos) << 1));
1229  for (i = 0; i < src->handles_pos; i++)
1230  dst->handles[dst->handles_pos++] = src->handles[i];
1231 #endif
1232 }
int nsds
Maximum number of any socket descriptor in the set (plus one)
#define GNUNET_array_grow(arr, size, tsize)
Grow a well-typed (!) array.
#define GNUNET_MAX(a, b)
Definition: gnunet_common.h:85
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 1242 of file network.c.

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

1244 {
1245  FD_COPY (&from->sds,
1246  &to->sds);
1247  to->nsds = from->nsds;
1248 #ifdef MINGW
1249  if (from->handles_pos > to->handles_size)
1250  GNUNET_array_grow (to->handles,
1251  to->handles_size,
1252  from->handles_pos * 2);
1253  GNUNET_memcpy (to->handles,
1254  from->handles,
1255  from->handles_pos * sizeof (struct GNUNET_NETWORK_Handle *));
1256  to->handles_pos = from->handles_pos;
1257 #endif
1258 }
int nsds
Maximum number of any socket descriptor in the set (plus one)
#define GNUNET_array_grow(arr, size, tsize)
Grow a well-typed (!) array.
#define GNUNET_memcpy(dst, src, n)
fd_set sds
Bitset with the descriptors.
#define FD_COPY(s, d)
Definition: network.c:244
handle to a socket
Definition: network.c:46

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

References GNUNET_NETWORK_Handle::fd.

Referenced by add_without_sets(), check_crc_buf_osdep(), 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().

1269 {
1270  return desc->fd;
1271 }
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 1281 of file network.c.

References GNUNET_NETWORK_Handle::addr.

Referenced by do_accept(), and setup_data_transfer().

1282 {
1283  return desc->addr;
1284 }
struct sockaddr * addr
Address we were bound to, or NULL.
Definition: network.c:72
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 1294 of file network.c.

References GNUNET_NETWORK_Handle::addrlen.

Referenced by do_accept(), and setup_data_transfer().

1295 {
1296  return desc->addrlen;
1297 }
socklen_t addrlen
Number of bytes in addr.
Definition: network.c:67
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 1308 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(), GNUNET_NETWORK_socket_select(), prepare_daemon(), run_httpd(), schedule_httpd(), and server_schedule().

1311 {
1312  FD_COPY (from,
1313  &to->sds);
1314  to->nsds = nfds;
1315 }
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:244
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 1325 of file network.c.

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

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

1327 {
1328  GNUNET_assert ((nfd >= 0) && (nfd < FD_SETSIZE));
1329  FD_SET (nfd, &to->sds);
1330  to->nsds = GNUNET_MAX (nfd + 1,
1331  to->nsds);
1332 }
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 FD_SETSIZE
Definition: winproc.h:39
#define GNUNET_MAX(a, b)
Definition: gnunet_common.h:85
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 1343 of file network.c.

References GNUNET_NO, GNUNET_YES, and GNUNET_NETWORK_FDSet::sds.

Referenced by extract_handles(), and select_loop().

1345 {
1346  if ( (-1 == nfd) ||
1347  (NULL == to) )
1348  return GNUNET_NO;
1349  return FD_ISSET (nfd, &to->sds) ? GNUNET_YES : GNUNET_NO;
1350 }
#define GNUNET_NO
Definition: gnunet_common.h:81
fd_set sds
Bitset with the descriptors.
#define GNUNET_YES
Definition: gnunet_common.h:80
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 1359 of file network.c.

References GNUNET_NETWORK_Handle::fd, GNUNET_array_grow, 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_add_file_with_priority(), GNUNET_SCHEDULER_driver_init(), and main().

1361 {
1362 #ifdef MINGW
1363  if (fds->handles_pos == fds->handles_size)
1364  GNUNET_array_grow (fds->handles,
1365  fds->handles_size,
1366  fds->handles_size * 2 + 2);
1367  fds->handles[fds->handles_pos++] = h;
1368 #else
1369  int fd;
1370 
1373  &fd,
1374  sizeof (int)));
1375  FD_SET (fd,
1376  &fds->sds);
1377  fds->nsds = GNUNET_MAX (fd + 1,
1378  fds->nsds);
1379 #endif
1380 }
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:78
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:94
#define GNUNET_array_grow(arr, size, tsize)
Grow a well-typed (!) array.
#define GNUNET_MAX(a, b)
Definition: gnunet_common.h:85
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:2665
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 1389 of file network.c.

References GNUNET_array_grow, GNUNET_NETWORK_fdset_handle_set(), and h.

1391 {
1392 #ifdef MINGW
1393  if (fds->handles_pos == fds->handles_size)
1394  GNUNET_array_grow (fds->handles,
1395  fds->handles_size,
1396  fds->handles_size * 2 + 2);
1397  fds->handles[fds->handles_pos] = h;
1398  if (fds->handles[0] != h)
1399  {
1400  const struct GNUNET_DISK_FileHandle *bak = fds->handles[0];
1401  fds->handles[0] = h;
1402  fds->handles[fds->handles_pos] = bak;
1403  }
1404  fds->handles_pos++;
1405 #else
1407 #endif
1408 }
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:94
#define GNUNET_array_grow(arr, size, tsize)
Grow a well-typed (!) array.
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:1359
Handle used to access files (and pipes).
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 1419 of file network.c.

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

Referenced by cmd_read().

1421 {
1422 #ifdef MINGW
1423  unsigned int i;
1424 
1425  for (i=0;i<fds->handles_pos;i++)
1426  if (fds->handles[i] == h)
1427  return GNUNET_YES;
1428  return GNUNET_NO;
1429 #else
1430  return FD_ISSET (h->fd,
1431  &fds->sds);
1432 #endif
1433 }
#define GNUNET_NO
Definition: gnunet_common.h:81
fd_set sds
Bitset with the descriptors.
#define GNUNET_YES
Definition: gnunet_common.h:80
int fd
File handle on other OSes.
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 1466 of file network.c.

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

1468 {
1469 #ifndef MINGW
1470  int nfds;
1471 
1472  nfds = GNUNET_MIN (fds1->nsds,
1473  fds2->nsds);
1474  while (nfds > 0)
1475  {
1476  nfds--;
1477  if ( (FD_ISSET (nfds,
1478  &fds1->sds)) &&
1479  (FD_ISSET (nfds,
1480  &fds2->sds)) )
1481  return GNUNET_YES;
1482  }
1483  return GNUNET_NO;
1484 #else
1485  unsigned int i;
1486  unsigned int j;
1487 
1488  /* This code is somewhat hacky, we are not supposed to know what's
1489  * inside of fd_set; also the O(n^2) is really bad... */
1490  for (i = 0; i < fds1->sds.fd_count; i++)
1491  for (j = 0; j < fds2->sds.fd_count; j++)
1492  if (fds1->sds.fd_array[i] == fds2->sds.fd_array[j])
1493  return GNUNET_YES;
1494 
1495  /* take a short cut if possible */
1496  if ( (0 == fds1->handles_pos) ||
1497  (0 == fds2->handles_pos) )
1498  return GNUNET_NO;
1499 
1500  /* Sort file handles array to avoid quadratic complexity when
1501  checking for overlap */
1502  qsort (fds1->handles,
1503  fds1->handles_pos,
1504  sizeof (void *),
1505  &ptr_cmp);
1506  qsort (fds2->handles,
1507  fds2->handles_pos,
1508  sizeof (void *),
1509  &ptr_cmp);
1510  i = 0;
1511  j = 0;
1512  while ( (i < fds1->handles_pos) &&
1513  (j < fds2->handles_pos) )
1514  {
1515  switch (ptr_cmp (fds1->handles[i],
1516  fds2->handles[j]))
1517  {
1518  case -1:
1519  i++;
1520  break;
1521  case 0:
1522  return GNUNET_YES;
1523  case 1:
1524  j++;
1525  }
1526  }
1527  return GNUNET_NO;
1528 #endif
1529 }
int nsds
Maximum number of any socket descriptor in the set (plus one)
#define GNUNET_NO
Definition: gnunet_common.h:81
#define GNUNET_MIN(a, b)
Definition: gnunet_common.h:83
fd_set sds
Bitset with the descriptors.
#define GNUNET_YES
Definition: gnunet_common.h:80

◆ 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 1538 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_add_file_with_priority(), GNUNET_SCHEDULER_add_net_with_priority(), GNUNET_SCHEDULER_driver_init(), GNUNET_SERVER_resume(), listen_stdio(), main(), prepare_daemon(), run_httpd(), schedule_httpd(), schedule_read(), select_loop(), and server_schedule().

1539 {
1540  struct GNUNET_NETWORK_FDSet *fds;
1541 
1542  fds = GNUNET_new (struct GNUNET_NETWORK_FDSet);
1544  return fds;
1545 }
#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:1146
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 1554 of file network.c.

References e, GNUNET_array_grow, GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, LOG, p, res, SOCKET, and status.

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

1555 {
1556 #ifdef MINGW
1557  GNUNET_array_grow (fds->handles,
1558  fds->handles_size,
1559  0);
1560 #endif
1561  GNUNET_free (fds);
1562 }
#define GNUNET_array_grow(arr, size, tsize)
Grow a well-typed (!) array.
#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 1751 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.

1753 {
1754  struct GNUNET_NETWORK_Handle *socket;
1755  int bind_status;
1756  int socktype;
1757  char open_port_str[6];
1758  struct addrinfo hint;
1759  struct addrinfo *ret;
1760  struct addrinfo *ai;
1761 
1762  GNUNET_snprintf (open_port_str,
1763  sizeof (open_port_str),
1764  "%u",
1765  (unsigned int) port);
1766  socktype = (IPPROTO_TCP == ipproto) ? SOCK_STREAM : SOCK_DGRAM;
1767  ret = NULL;
1768  memset (&hint, 0, sizeof (hint));
1769  hint.ai_family = AF_UNSPEC; /* IPv4 and IPv6 */
1770  hint.ai_socktype = socktype;
1771  hint.ai_protocol = ipproto;
1772  hint.ai_addrlen = 0;
1773  hint.ai_addr = NULL;
1774  hint.ai_canonname = NULL;
1775  hint.ai_next = NULL;
1776  hint.ai_flags = AI_PASSIVE | AI_NUMERICSERV; /* Wild card address */
1777  GNUNET_assert (0 == getaddrinfo (NULL,
1778  open_port_str,
1779  &hint,
1780  &ret));
1781  bind_status = GNUNET_NO;
1782  for (ai = ret; NULL != ai; ai = ai->ai_next)
1783  {
1784  socket = GNUNET_NETWORK_socket_create (ai->ai_family,
1785  ai->ai_socktype,
1786  ai->ai_protocol);
1787  if (NULL == socket)
1788  continue;
1789  bind_status = GNUNET_NETWORK_socket_bind (socket,
1790  ai->ai_addr,
1791  ai->ai_addrlen);
1792  GNUNET_NETWORK_socket_close (socket);
1793  if (GNUNET_OK != bind_status)
1794  break;
1795  }
1796  freeaddrinfo (ret);
1797  return bind_status;
1798 }
#define AI_NUMERICSERV
AI_NUMERICSERV not defined in windows.
Definition: platform.h:261
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:522
#define GNUNET_NO
Definition: gnunet_common.h:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
static int ret
Final status code.
Definition: gnunet-arm.c:89
static struct GNUNET_TRANSPORT_AddressIdentifier * ai
Handle to the operation that publishes our address.
static uint16_t port
Port number.
Definition: gnunet-bcd.c:79
handle to a socket
Definition: network.c:46
int GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc)
Close a socket.
Definition: network.c:604
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_create(int domain, int type, int protocol)
Create a new socket.
Definition: network.c:1037
Here is the call graph for this function: