GNUnet  0.10.x
Data Structures | Macros | Functions
network.c File Reference

basic, low-level networking interface More...

#include "platform.h"
#include "gnunet_util_lib.h"
#include "disk.h"
Include dependency graph for network.c:

Go to the source code of this file.

Data Structures

struct  GNUNET_NETWORK_Handle
 handle to a socket More...
 

Macros

#define LOG(kind, ...)   GNUNET_log_from (kind, "util-network", __VA_ARGS__)
 
#define LOG_STRERROR_FILE(kind, syscall, filename)   GNUNET_log_from_strerror_file (kind, "util-network", syscall, filename)
 
#define LOG_STRERROR(kind, syscall)   GNUNET_log_from_strerror (kind, "util-network", syscall)
 
#define DEBUG_NETWORK   GNUNET_EXTRA_LOGGING
 
#define INVALID_SOCKET   -1
 
#define FD_COPY(s, d)   do { GNUNET_memcpy ((d), (s), sizeof (fd_set)); } while (0)
 

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...
 
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...
 
static int socket_set_inheritable (const struct GNUNET_NETWORK_Handle *h)
 Make a socket non-inheritable to child processes. More...
 
static void socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h)
 Disable delays when sending data via the socket. More...
 
static int initialize_network_handle (struct GNUNET_NETWORK_Handle *h, int af, int type)
 Perform proper canonical initialization for a network handle. 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...
 
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...
 
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_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...
 
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...
 
struct GNUNET_NETWORK_HandleGNUNET_NETWORK_socket_create (int domain, int type, int protocol)
 Create a new socket. 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...
 
void GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
 Reset FD set. 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. 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 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. 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. 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 ()
 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...
 
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 or pipes meet certain conditions. More...
 

Detailed Description

basic, low-level networking interface

Author
Nils Durner
Christian Grothoff

Definition in file network.c.

Macro Definition Documentation

◆ LOG

#define LOG (   kind,
  ... 
)    GNUNET_log_from (kind, "util-network", __VA_ARGS__)

◆ LOG_STRERROR_FILE

#define LOG_STRERROR_FILE (   kind,
  syscall,
  filename 
)    GNUNET_log_from_strerror_file (kind, "util-network", syscall, filename)

Definition at line 32 of file network.c.

Referenced by GNUNET_NETWORK_socket_close().

◆ LOG_STRERROR

#define LOG_STRERROR (   kind,
  syscall 
)    GNUNET_log_from_strerror (kind, "util-network", syscall)

◆ DEBUG_NETWORK

#define DEBUG_NETWORK   GNUNET_EXTRA_LOGGING

Definition at line 35 of file network.c.

◆ INVALID_SOCKET

#define INVALID_SOCKET   -1

◆ FD_COPY

#define FD_COPY (   s,
 
)    do { GNUNET_memcpy ((d), (s), sizeof (fd_set)); } while (0)

Function Documentation

◆ socket_set_inheritable()

static int socket_set_inheritable ( const struct GNUNET_NETWORK_Handle h)
static

Make a socket non-inheritable to child processes.

Parameters
hthe socket to make non-inheritable
Returns
GNUNET_OK on success, GNUNET_SYSERR otherwise
Warning
Not implemented on Windows

Definition at line 314 of file network.c.

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

Referenced by initialize_network_handle().

315 {
316 #ifndef MINGW
317  int i;
318  i = fcntl (h->fd, F_GETFD);
319  if (i < 0)
320  return GNUNET_SYSERR;
321  if (i == (i | FD_CLOEXEC))
322  return GNUNET_OK;
323  i |= FD_CLOEXEC;
324  if (fcntl (h->fd, F_SETFD, i) < 0)
325  return GNUNET_SYSERR;
326 #else
327  BOOL b;
328  SetLastError (0);
329  b = SetHandleInformation ((HANDLE) h->fd, HANDLE_FLAG_INHERIT, 0);
330  if (!b)
331  {
332  SetErrnoFromWinsockError (WSAGetLastError ());
333  return GNUNET_SYSERR;
334  }
335 #endif
336  return GNUNET_OK;
337 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
Here is the caller graph for this function:

◆ socket_set_nodelay()

static void socket_set_nodelay ( const struct GNUNET_NETWORK_Handle h)
static

Disable delays when sending data via the socket.

(GNUnet makes sure that messages are as big as possible already).

Parameters
hthe socket to make non-delaying

Definition at line 368 of file network.c.

References GNUNET_NETWORK_Handle::fd, GNUNET_ERROR_TYPE_WARNING, LOG_STRERROR, and value.

Referenced by initialize_network_handle().

369 {
370 #ifndef WINDOWS
371  int value = 1;
372 
373  if (0 !=
374  setsockopt (h->fd,
375  IPPROTO_TCP,
376  TCP_NODELAY,
377  &value, sizeof (value)))
379  "setsockopt");
380 #else
381  const char *abs_value = "1";
382 
383  if (0 !=
384  setsockopt (h->fd,
385  IPPROTO_TCP,
386  TCP_NODELAY,
387  (const void *) abs_value,
388  sizeof (abs_value)))
390  "setsockopt");
391 #endif
392 }
static char * value
Value of the record to add/remove.
#define LOG_STRERROR(kind, syscall)
Definition: network.c:33
Here is the caller graph for this function:

◆ initialize_network_handle()

static int initialize_network_handle ( struct GNUNET_NETWORK_Handle h,
int  af,
int  type 
)
static

Perform proper canonical initialization for a network handle.

Set it to non-blocking, make it non-inheritable to child processes, disable SIGPIPE, enable "nodelay" (if non-UNIX stream socket) and check that it is smaller than FD_SETSIZE.

Parameters
hsocket to initialize
afaddress family of the socket
typesocket type
Returns
GNUNET_OK on success, GNUNET_SYSERR if initialization failed and the handle was destroyed

Definition at line 408 of file network.c.

References GNUNET_NETWORK_Handle::af, GNUNET_NETWORK_Handle::fd, FD_SETSIZE, GNUNET_break, GNUNET_ERROR_TYPE_BULK, GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_NETWORK_socket_close(), GNUNET_NETWORK_socket_set_blocking(), GNUNET_NO, GNUNET_OK, GNUNET_SYSERR, INVALID_SOCKET, LOG_STRERROR, socket_set_inheritable(), socket_set_nodelay(), and GNUNET_NETWORK_Handle::type.

Referenced by GNUNET_NETWORK_socket_accept(), and GNUNET_NETWORK_socket_create().

411 {
412  int eno;
413 
414  h->af = af;
415  h->type = type;
416  if (h->fd == INVALID_SOCKET)
417  {
418 #ifdef MINGW
419  SetErrnoFromWinsockError (WSAGetLastError ());
420 #endif
421  eno = errno;
422  GNUNET_free (h);
423  errno = eno;
424  return GNUNET_SYSERR;
425  }
426 #ifndef MINGW
427  if (h->fd >= FD_SETSIZE)
428  {
430  errno = EMFILE;
431  return GNUNET_SYSERR;
432  }
433 #endif
436  "socket_set_inheritable");
437 
439  {
440  eno = errno;
441  GNUNET_break (0);
443  errno = eno;
444  return GNUNET_SYSERR;
445  }
446 #ifdef DARWIN
447  socket_set_nosigpipe (h);
448 #endif
449  if ( (type == SOCK_STREAM)
450 #ifdef AF_UNIX
451  && (af != AF_UNIX)
452 #endif
453  )
454  socket_set_nodelay (h);
455  return GNUNET_OK;
456 }
int GNUNET_NETWORK_socket_set_blocking(struct GNUNET_NETWORK_Handle *fd, int doBlock)
Set if a socket should use blocking or non-blocking IO.
Definition: network.c:256
#define GNUNET_NO
Definition: gnunet_common.h:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
static int socket_set_inheritable(const struct GNUNET_NETWORK_Handle *h)
Make a socket non-inheritable to child processes.
Definition: network.c:314
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static void socket_set_nodelay(const struct GNUNET_NETWORK_Handle *h)
Disable delays when sending data via the socket.
Definition: network.c:368
#define FD_SETSIZE
Definition: winproc.h:39
#define INVALID_SOCKET
Definition: network.c:39
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
#define LOG_STRERROR(kind, syscall)
Definition: network.c:33
int af
Address family / domain.
Definition: network.c:57
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
int GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc)
Close a socket.
Definition: network.c:604
int type
Type of the socket.
Definition: network.c:62
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function: