Loading...
Searching...
No Matches
Socket convenience functions

Osmocom socket convenience functions. More...

## Files

file | socket.h
| Osmocom socket convenience functions.
file | socket.c

## Data Structures

struct | osmo_sockaddr

## Macros

#define | OSMO_SOCK_NAME_MAXLEN (2 + INET6_ADDRSTRLEN + 1 + 5 + 3 + 2 + INET6_ADDRSTRLEN + 1 + 5 + 1)
| maximum length of a socket name ("r=1.2.3.4:123<->l=5.6.7.8:987")
#define | OSMO_SOCK_F_CONNECT (1 << 0)
| connect the socket to a remote peer
#define | OSMO_SOCK_F_BIND (1 << 1)
| bind the socket to a local address/port
#define | OSMO_SOCK_F_NONBLOCK (1 << 2)
| switch socket to non-blocking mode
#define | OSMO_SOCK_F_NO_MCAST_LOOP (1 << 3)
| disable multiast loop (IP_MULTICAST_LOOP)
#define | OSMO_SOCK_F_NO_MCAST_ALL (1 << 4)
| disable receiving all multiast even for non-subscribed groups
#define | OSMO_SOCK_F_UDP_REUSEADDR (1 << 5)
| use SO_REUSEADDR on UDP ports (required for multicast)
#define | OSMO_SOCK_F_DSCP(x) (((x)&0x3f) << 24)
| use OSMO_SOCK_F_DSCP(x) to set IP DSCP 'x' for packets transmitted on the socket
#define | GET_OSMO_SOCK_F_DSCP(f) (((f) >> 24) & 0x3f)
#define | OSMO_SOCK_F_PRIO(x) (((x)&0xff) << 16)
| use OSMO_SOCK_F_PRIO(x) to set priority 'x' for packets transmitted on the socket
#define | GET_OSMO_SOCK_F_PRIO(f) (((f) >> 16) & 0xff)
#define | OSMO_SOCK_MAX_ADDRS 32
| maximum number of local or remote addresses supported by an osmo_sock instance
#define | _SOCKADDR_TO_STR(dest, sockaddr)

## Functions

int | osmo_sock_init (uint16_t family, uint16_t type, uint8_t proto, const char *host, uint16_t port, unsigned int flags)
| Initialize a socket (including bind/connect)
int | osmo_sock_init2 (uint16_t family, uint16_t type, uint8_t proto, const char *local_host, uint16_t local_port, const char *remote_host, uint16_t remote_port, unsigned int flags)
| Initialize a socket (including bind and/or connect)
int | osmo_sock_init2_multiaddr (uint16_t family, uint16_t type, uint8_t proto, const char **local_hosts, size_t local_hosts_cnt, uint16_t local_port, const char **remote_hosts, size_t remote_hosts_cnt, uint16_t remote_port, unsigned int flags)
| Initialize a socket (including bind and/or connect) with multiple local or remote addresses.
int | osmo_sock_init_osa (uint16_t type, uint8_t proto, const struct osmo_sockaddr *local, const struct osmo_sockaddr *remote, unsigned int flags)
| Initialize a socket (including bind and/or connect)
int | osmo_sock_init_ofd (struct osmo_fd *ofd, int family, int type, int proto, const char *host, uint16_t port, unsigned int flags)
| Initialize a socket and fill osmo_fd.
int | osmo_sock_init2_ofd (struct osmo_fd *ofd, int family, int type, int proto, const char *local_host, uint16_t local_port, const char *remote_host, uint16_t remote_port, unsigned int flags)
| Initialize a socket and fill osmo_fd.
int | osmo_sock_init_osa_ofd (struct osmo_fd *ofd, int type, int proto, const struct osmo_sockaddr *local, const struct osmo_sockaddr *remote, unsigned int flags)
int | osmo_sock_init_sa (struct sockaddr *ss, uint16_t type, uint8_t proto, unsigned int flags)
| Initialize a socket and fill sockaddr.
int | osmo_sockaddr_is_local (struct sockaddr *addr, unsigned int addrlen)
| Determine if the given address is a local address.
unsigned int | osmo_sockaddr_to_str_and_uint (char *addr, unsigned int addr_len, uint16_t *port, const struct sockaddr *sa)
| Convert sockaddr to IP address as char string and port as uint16_t.
size_t | osmo_sockaddr_in_to_str_and_uint (char *addr, unsigned int addr_len, uint16_t *port, const struct sockaddr_in *sin)
| Convert sockaddr_in to IP address as char string and port as uint16_t.
const char * | osmo_sockaddr_ntop (const struct sockaddr *sa, char *dst)
| inet_ntop() wrapper for a struct sockaddr.
uint16_t | osmo_sockaddr_port (const struct sockaddr *sa)
| Get sockaddr port content (in host byte order)
void | osmo_sockaddr_set_port (struct sockaddr *sa, uint16_t port)
| Set sockaddr port content (to network byte order).
int | osmo_sock_unix_init (uint16_t type, uint8_t proto, const char *socket_path, unsigned int flags)
| Initialize a unix domain socket (including bind/connect)
int | osmo_sock_unix_init_ofd (struct osmo_fd *ofd, uint16_t type, uint8_t proto, const char *socket_path, unsigned int flags)
| Initialize a unix domain socket and fill osmo_fd.
char * | osmo_sock_get_name (const void *ctx, int fd)
| Get address/port information on socket in dyn-alloc string like "(r=1.2.3.4:5<->l=6.7.8.9:10)".
const char * | osmo_sock_get_name2 (int fd)
| Get address/port information on socket in static string, like "r=1.2.3.4:5<->l=6.7.8.9:10".
char * | osmo_sock_get_name2_c (const void *ctx, int fd)
| Get address/port information on socket in static string, like "r=1.2.3.4:5<->l=6.7.8.9:10".
int | osmo_sock_get_name_buf (char *str, size_t str_len, int fd)
| Get address/port information on socket in provided string buffer, like "r=1.2.3.4:5<->l=6.7.8.9:10".
int | osmo_sock_get_ip_and_port (int fd, char *ip, size_t ip_len, char *port, size_t port_len, bool local)
| Get the IP and/or port number on socket in separate string buffers.
int | osmo_sock_get_local_ip (int fd, char *ip, size_t len)
| Get local IP address on socket.
int | osmo_sock_get_local_ip_port (int fd, char *port, size_t len)
| Get local port on socket.
int | osmo_sock_get_remote_ip (int fd, char *ip, size_t len)
| Get remote IP address on socket.
int | osmo_sock_get_remote_ip_port (int fd, char *port, size_t len)
| Get remote port on socket.
int | osmo_sock_mcast_loop_set (int fd, bool enable)
| Activate or de-activate local loop-back of transmitted multicast packets.
int | osmo_sock_mcast_ttl_set (int fd, uint8_t ttl)
| Set the TTL of outbound multicast packets.
int | osmo_sock_mcast_all_set (int fd, bool enable)
| Enable/disable receiving all multicast packets, even for non-subscribed groups.
int | osmo_sock_mcast_iface_set (int fd, const char *ifname)
| Set the network device to which we should bind the multicast socket.
int | osmo_sock_mcast_subscribe (int fd, const char *grp_addr)
| Subscribe to the given IP multicast group.
int | osmo_sock_local_ip (char *local_ip, const char *remote_ip)
| Determine the matching local IP-address for a given remote IP-Address.
int | osmo_sockaddr_local_ip (struct osmo_sockaddr *local_ip, const struct osmo_sockaddr *remote_ip)
| Determine the matching local address for a given remote address.
int | osmo_sockaddr_cmp (const struct osmo_sockaddr *a, const struct osmo_sockaddr *b)
| Compare two osmo_sockaddr.
int | osmo_sockaddr_to_octets (uint8_t *dst, size_t dst_maxlen, const struct osmo_sockaddr *os)
| Copy the addr part, the IP address octets in network byte order, to a buffer.
int | osmo_sockaddr_from_octets (struct osmo_sockaddr *os, const void *src, size_t src_len)
| Copy the addr part, the IP address octets in network byte order, from a buffer.
const char * | osmo_sockaddr_to_str (const struct osmo_sockaddr *sockaddr)
| string-format a given osmo_sockaddr address
char * | osmo_sockaddr_to_str_buf (char *buf, size_t buf_len, const struct osmo_sockaddr *sockaddr)
| string-format a given osmo_sockaddr address into a user-supplied buffer.
int | osmo_sockaddr_to_str_buf2 (char *buf, size_t buf_len, const struct osmo_sockaddr *sockaddr)
| string-format a given osmo_sockaddr address into a user-supplied buffer.
char * | osmo_sockaddr_to_str_c (void *ctx, const struct osmo_sockaddr *sockaddr)
| string-format a given osmo_sockaddr address into a talloc allocated buffer.
int | osmo_sock_set_dscp (int fd, uint8_t dscp)
| Set the DSCP (differentiated services code point) of a socket.
int | osmo_sock_set_priority (int fd, int prio)
| Set the priority value of a socket.
static struct addrinfo * | addrinfo_helper (uint16_t family, uint16_t type, uint8_t proto, const char *host, uint16_t port, bool passive)
static int | addrinfo_helper_multi (struct addrinfo **addrinfo, uint16_t family, uint16_t type, uint8_t proto, const char **hosts, size_t host_cnt, uint16_t port, bool passive)
| Retrieve an array of addrinfo with specified hints, one for each host in the hosts array.
static int | socket_helper_tail (int sfd, unsigned int flags)
static int | socket_helper (const struct addrinfo *rp, unsigned int flags)
static int | socket_helper_osa (const struct osmo_sockaddr *addr, uint16_t type, uint8_t proto, unsigned int flags)
static int | multiaddr_snprintf (char *buf, size_t buf_len, const char **hosts, size_t host_cnt)
static int | osmo_sock_init_tail (int fd, uint16_t type, unsigned int flags)
static void | addrinfo_has_v4v6addr (const struct addrinfo **result, size_t result_count, bool *has_v4, bool *has_v6)
static bool | addrinfo_has_in6addr_any (const struct addrinfo **result, size_t result_count)
static int | socket_helper_multiaddr (uint16_t family, uint16_t type, uint8_t proto, unsigned int flags)
static int | addrinfo_to_sockaddr (uint16_t family, const struct addrinfo **result, const char **hosts, unsigned int host_cont, uint8_t *addrs_buf, size_t addrs_buf_len)
static int | osmo_fd_init_ofd (struct osmo_fd *ofd, int sfd, unsigned int flags)
| fill osmo_fd for a give sfd
static int | sockaddr_equal (const struct sockaddr *a, const struct sockaddr *b, unsigned int len)
static int | sockaddr_is_local_routed (const struct sockaddr *a)
static int | sock_get_domain (int fd)

## Detailed Description

Osmocom socket convenience functions.

## Macro Definition Documentation

## _SOCKADDR_TO_STR

#define _SOCKADDR_TO_STR | ( |  | dest,
|  |  | sockaddr
| ) |  |

Value:
do { \
if (osmo_sockaddr_str_from_sockaddr(dest, &sockaddr->u.sas)) \
osmo_strlcpy((dest)->ip, "Invalid IP", 11); \
} while (0)
osmo_sockaddr_str_from_sockaddr
int osmo_sockaddr_str_from_sockaddr(struct osmo_sockaddr_str *sockaddr_str, const struct sockaddr_storage *src)
subsys
char subsys[16]
logging sub-system
Definition gsmtap.h:9

## GET_OSMO_SOCK_F_DSCP

#define GET_OSMO_SOCK_F_DSCP | ( |  | f | ) | (((f) >> 24) & 0x3f)

## GET_OSMO_SOCK_F_PRIO

#define GET_OSMO_SOCK_F_PRIO | ( |  | f | ) | (((f) >> 16) & 0xff)

## OSMO_SOCK_F_BIND

#define OSMO_SOCK_F_BIND (1 << 1)

bind the socket to a local address/port

## OSMO_SOCK_F_CONNECT

#define OSMO_SOCK_F_CONNECT (1 << 0)

connect the socket to a remote peer

## OSMO_SOCK_F_DSCP

#define OSMO_SOCK_F_DSCP | ( |  | x | ) | (((x)&0x3f) << 24)

use OSMO_SOCK_F_DSCP(x) to set IP DSCP 'x' for packets transmitted on the socket

## OSMO_SOCK_F_NO_MCAST_ALL

#define OSMO_SOCK_F_NO_MCAST_ALL (1 << 4)

disable receiving all multiast even for non-subscribed groups

## OSMO_SOCK_F_NO_MCAST_LOOP

#define OSMO_SOCK_F_NO_MCAST_LOOP (1 << 3)

disable multiast loop (IP_MULTICAST_LOOP)

## OSMO_SOCK_F_NONBLOCK

#define OSMO_SOCK_F_NONBLOCK (1 << 2)

switch socket to non-blocking mode

## OSMO_SOCK_F_PRIO

#define OSMO_SOCK_F_PRIO | ( |  | x | ) | (((x)&0xff) << 16)

use OSMO_SOCK_F_PRIO(x) to set priority 'x' for packets transmitted on the socket

## OSMO_SOCK_F_UDP_REUSEADDR

#define OSMO_SOCK_F_UDP_REUSEADDR (1 << 5)

use SO_REUSEADDR on UDP ports (required for multicast)

## OSMO_SOCK_MAX_ADDRS

#define OSMO_SOCK_MAX_ADDRS 32

maximum number of local or remote addresses supported by an osmo_sock instance

## OSMO_SOCK_NAME_MAXLEN

#define OSMO_SOCK_NAME_MAXLEN (2 + INET6_ADDRSTRLEN + 1 + 5 + 3 + 2 + INET6_ADDRSTRLEN + 1 + 5 + 1)

maximum length of a socket name ("r=1.2.3.4:123<->l=5.6.7.8:987")

## Function Documentation

## addrinfo_has_in6addr_any()

static bool addrinfo_has_in6addr_any | ( | const struct addrinfo ** | result,
|  | size_t | result_count
| ) |  |

static

References subsys.

Referenced by osmo_sock_init2_multiaddr().

## addrinfo_has_v4v6addr()

static void addrinfo_has_v4v6addr | ( | const struct addrinfo ** | result,
|  | size_t | result_count,
|  | bool * | has_v4,
|  | bool * | has_v6
| ) |  |

static

References subsys.

Referenced by osmo_sock_init2_multiaddr().

## addrinfo_helper()

static struct addrinfo * addrinfo_helper | ( | uint16_t | family,
|  | uint16_t | type,
|  | uint8_t | proto,
|  | const char * | host,
|  | uint16_t | port,
|  | bool | passive
| ) |  |

static

References DLGLOBAL, LOGL_ERROR, LOGP, proto, subsys, and type.

Referenced by addrinfo_helper_multi(), osmo_sock_init(), and osmo_sock_init2().

## addrinfo_helper_multi()

static int addrinfo_helper_multi | ( | struct addrinfo ** | addrinfo,
|  | uint16_t | family,
|  | uint16_t | type,
|  | uint8_t | proto,
|  | const char ** | hosts,
|  | size_t | host_cnt,
|  | uint16_t | port,
|  | bool | passive
| ) |  |

static

Retrieve an array of addrinfo with specified hints, one for each host in the hosts array.

Parameters

  [out] | addrinfo | array of addrinfo pointers, will be filled by the function on success. Its size must be at least the one of hosts.
  [in] | family | Socket family like AF_INET, AF_INET6.
  [in] | type | Socket type like SOCK_DGRAM, SOCK_STREAM.
  [in] | proto | Protocol like IPPROTO_TCP, IPPROTO_UDP.
  [in] | hosts | array of char pointers (strings) containing the addresses to query.
  [in] | host_cnt | length of the hosts array (in items).
  [in] | port | port number in host byte order.
  [in] | passive | whether to include the AI_PASSIVE flag in getaddrinfo() hints.

Returns
  0 is returned on success together with a filled addrinfo array; negative on error

References addrinfo_helper(), proto, subsys, and type.

Referenced by osmo_sock_init2_multiaddr().

## addrinfo_to_sockaddr()

static int addrinfo_to_sockaddr | ( | uint16_t | family,
|  | const struct addrinfo ** | result,
|  | const char ** | hosts,
|  | unsigned int | host_cont,
|  | uint8_t * | addrs_buf,
|  | size_t | addrs_buf_len
| ) |  |

static

References DLGLOBAL, LOGL_ERROR, LOGP, and subsys.

Referenced by osmo_sock_init2_multiaddr().

## multiaddr_snprintf()

static int multiaddr_snprintf | ( | char * | buf,
|  | size_t | buf_len,
|  | const char ** | hosts,
|  | size_t | host_cnt
| ) |  |

static

References len, OSMO_SNPRINTF_RET, and subsys.

Referenced by osmo_sock_init2_multiaddr().

## osmo_fd_init_ofd()

static int osmo_fd_init_ofd | ( | struct osmo_fd * | ofd,
|  | int | sfd,
|  | unsigned int | flags
| ) |  |

inline static

fill osmo_fd for a give sfd

Parameters

  [out] | ofd | file descriptor (will be filled in)
  [in] | sfd | socket file descriptor
  [in] | flags | flags like OSMO_SOCK_F_CONNECT

Returns
  socket fd on success; negative on error

This function fills the ofd structure.

References osmo_fd::fd, flags, OSMO_FD_READ, osmo_fd_register(), OSMO_FD_WRITE, OSMO_SOCK_F_CONNECT, OSMO_SOCK_F_NONBLOCK, subsys, and osmo_fd::when.

Referenced by osmo_sock_init2_ofd(), osmo_sock_init_ofd(), osmo_sock_init_osa_ofd(), and osmo_sock_unix_init_ofd().

## osmo_sock_get_ip_and_port()

int osmo_sock_get_ip_and_port | ( | int | fd,
|  | char * | ip,
|  | size_t | ip_len,
|  | char * | port,
|  | size_t | port_len,
|  | bool | local
| ) |  |

Get the IP and/or port number on socket in separate string buffers.

Parameters

  [in] | fd | file descriptor of socket
  [out] | ip | IP address (will be filled in when not NULL)
  [in] | ip_len | length of the ip buffer
  [out] | port | number (will be filled in when not NULL)
  [in] | port_len | length of the port buffer
  [in] | local | (true) or remote (false) name will get looked at

Returns
  0 on success; negative otherwise

References len, and subsys.

Referenced by fill_stats(), osmo_sock_get_local_ip(), osmo_sock_get_local_ip_port(), osmo_sock_get_name_buf(), osmo_sock_get_remote_ip(), and osmo_sock_get_remote_ip_port().

## osmo_sock_get_local_ip()

int osmo_sock_get_local_ip | ( | int | fd,
|  | char * | ip,
|  | size_t | len
| ) |  |

Get local IP address on socket.

Parameters

  [in] | fd | file descriptor of socket
  [out] | ip | IP address (will be filled in)
  [in] | len | length of the output buffer

Returns
  0 on success; negative otherwise

References len, and osmo_sock_get_ip_and_port().

## osmo_sock_get_local_ip_port()

int osmo_sock_get_local_ip_port | ( | int | fd,
|  | char * | port,
|  | size_t | len
| ) |  |

Get local port on socket.

Parameters

  [in] | fd | file descriptor of socket
  [out] | port | number (will be filled in)
  [in] | len | length of the output buffer

Returns
  0 on success; negative otherwise

References len, and osmo_sock_get_ip_and_port().

## osmo_sock_get_name()

char * osmo_sock_get_name | ( | const void * | ctx,
|  | int | fd
| ) |  |

Get address/port information on socket in dyn-alloc string like "(r=1.2.3.4:5<->l=6.7.8.9:10)".

Usually, it is better to use osmo_sock_get_name2() for a static string buffer or osmo_sock_get_name_buf() for a caller provided string buffer, to avoid the dynamic talloc allocation.

Parameters

  [in] | ctx | talloc context from which to allocate string buffer
  [in] | fd | file descriptor of socket

Returns
  string identifying the connection of this socket, talloc'd from ctx.

References osmo_sock_get_name_buf(), OSMO_SOCK_NAME_MAXLEN, and subsys.

## osmo_sock_get_name2()

const char * osmo_sock_get_name2 | ( | int | fd | ) |

Get address/port information on socket in static string, like "r=1.2.3.4:5<->l=6.7.8.9:10".

This does not include braces like osmo_sock_get_name().

Parameters

  [in] | fd | File descriptor of socket.

Returns
  Static string buffer containing the result.

References osmo_sock_get_name_buf(), OSMO_SOCK_NAME_MAXLEN, and subsys.

Referenced by fill_stats().

## osmo_sock_get_name2_c()

char * osmo_sock_get_name2_c | ( | const void * | ctx,
|  | int | fd
| ) |  |

Get address/port information on socket in static string, like "r=1.2.3.4:5<->l=6.7.8.9:10".

This does not include braces like osmo_sock_get_name().

Parameters

  [in] | fd | File descriptor of socket.

Returns
  Static string buffer containing the result.

References osmo_sock_get_name_buf(), OSMO_SOCK_NAME_MAXLEN, and subsys.

## osmo_sock_get_name_buf()

int osmo_sock_get_name_buf | ( | char * | str,
|  | size_t | str_len,
|  | int | fd
| ) |  |

Get address/port information on socket in provided string buffer, like "r=1.2.3.4:5<->l=6.7.8.9:10".

This does not include braces like osmo_sock_get_name().

Parameters

  [out] | str | Destination string buffer.
  [in] | str_len | sizeof(str).
  [in] | fd | File descriptor of socket.

Returns
  String length as returned by snprintf(), or negative on error.

References osmo_sock_get_ip_and_port(), osmo_strlcpy(), and subsys.

Referenced by osmo_sock_get_name(), osmo_sock_get_name2(), and osmo_sock_get_name2_c().

## osmo_sock_get_remote_ip()

int osmo_sock_get_remote_ip | ( | int | fd,
|  | char * | ip,
|  | size_t | len
| ) |  |

Get remote IP address on socket.

Parameters

  [in] | fd | file descriptor of socket
  [out] | ip | IP address (will be filled in)
  [in] | len | length of the output buffer

Returns
  0 on success; negative otherwise

References len, and osmo_sock_get_ip_and_port().

## osmo_sock_get_remote_ip_port()

int osmo_sock_get_remote_ip_port | ( | int | fd,
|  | char * | port,
|  | size_t | len
| ) |  |

Get remote port on socket.

Parameters

  [in] | fd | file descriptor of socket
  [out] | port | number (will be filled in)
  [in] | len | length of the output buffer

Returns
  0 on success; negative otherwise

References len, and osmo_sock_get_ip_and_port().

## osmo_sock_init()

int osmo_sock_init | ( | uint16_t | family,
|  | uint16_t | type,
|  | uint8_t | proto,
|  | const char * | host,
|  | uint16_t | port,
|  | unsigned int | flags
| ) |  |

Initialize a socket (including bind/connect)

Parameters

  [in] | family | Address Family like AF_INET, AF_INET6, AF_UNSPEC
  [in] | type | Socket type like SOCK_DGRAM, SOCK_STREAM
  [in] | proto | Protocol like IPPROTO_TCP, IPPROTO_UDP
  [in] | host | remote host name or IP address in string form
  [in] | port | remote port number in host byte order
  [in] | flags | flags like OSMO_SOCK_F_CONNECT

Returns
  socket file descriptor on success; negative on error

This function creates a new socket of the designated family, type and proto and optionally binds or connects it, depending on the value of flags parameter.

References addrinfo_helper(), DLGLOBAL, flags, LOGL_ERROR, LOGP, OSMO_SOCK_F_BIND, OSMO_SOCK_F_CONNECT, OSMO_SOCK_F_UDP_REUSEADDR, osmo_sock_init_tail(), proto, socket_helper(), subsys, and type.

Referenced by gsmtap_source_init_fd(), osmo_sock_init_ofd(), and osmo_sock_init_sa().

## osmo_sock_init2()

int osmo_sock_init2 | ( | uint16_t | family,
|  | uint16_t | type,
|  | uint8_t | proto,
|  | const char * | local_host,
|  | uint16_t | local_port,
|  | const char * | remote_host,
|  | uint16_t | remote_port,
|  | unsigned int | flags
| ) |  |

Initialize a socket (including bind and/or connect)

Parameters

  [in] | family | Address Family like AF_INET, AF_INET6, AF_UNSPEC
  [in] | type | Socket type like SOCK_DGRAM, SOCK_STREAM
  [in] | proto | Protocol like IPPROTO_TCP, IPPROTO_UDP
  [in] | local_host | local host name or IP address in string form
  [in] | local_port | local port number in host byte order
  [in] | remote_host | remote host name or IP address in string form
  [in] | remote_port | remote port number in host byte order
  [in] | flags | flags like OSMO_SOCK_F_CONNECT

Returns
  socket file descriptor on success; negative on error

This function creates a new socket of the designated family, type and proto and optionally binds it to the local_host and local_port as well as optionally connects it to the remote_host and \q remote_port, depending on the value * of flags parameter.

As opposed to osmo_sock_init(), this function allows to combine the OSMO_SOCK_F_BIND and OSMO_SOCK_F_CONNECT flags. This is useful if you want to connect to a remote host/port, but still want to bind that socket to either a specific local alias IP and/or a specific local source port.

You must specify either OSMO_SOCK_F_BIND, or OSMO_SOCK_F_CONNECT, or both.

If OSMO_SOCK_F_NONBLOCK is specified, the socket will be set to non-blocking mode.

References addrinfo_helper(), DLGLOBAL, flags, LOGL_ERROR, LOGP, OSMO_SOCK_F_BIND, OSMO_SOCK_F_CONNECT, OSMO_SOCK_F_UDP_REUSEADDR, osmo_sock_init_tail(), proto, socket_helper(), subsys, and type.

Referenced by osmo_sock_init2_ofd(), and osmo_sock_local_ip().

## osmo_sock_init2_multiaddr()

int osmo_sock_init2_multiaddr | ( | uint16_t | family,
|  | uint16_t | type,
|  | uint8_t | proto,
|  | const char ** | local_hosts,
|  | size_t | local_hosts_cnt,
|  | uint16_t | local_port,
|  | const char ** | remote_hosts,
|  | size_t | remote_hosts_cnt,
|  | uint16_t | remote_port,
|  | unsigned int | flags
| ) |  |

Initialize a socket (including bind and/or connect) with multiple local or remote addresses.

Parameters

  [in] | family | Address Family like AF_INET, AF_INET6, AF_UNSPEC
  [in] | type | Socket type like SOCK_DGRAM, SOCK_STREAM
  [in] | proto | Protocol like IPPROTO_TCP, IPPROTO_UDP
  [in] | local_hosts | array of char pointers (strings), each containing local host name or IP address in string form
  [in] | local_hosts_cnt | length of local_hosts (in items)
  [in] | local_port | local port number in host byte order
  [in] | remote_host | array of char pointers (strings), each containing remote host name or IP address in string form
  [in] | remote_hosts_cnt | length of remote_hosts (in items)
  [in] | remote_port | remote port number in host byte order
  [in] | flags | flags like OSMO_SOCK_F_CONNECT

Returns
  socket file descriptor on success; negative on error

This function is similar to osmo_sock_init2(), but can be passed an array of local or remote addresses for protocols supporting multiple addresses per socket, like SCTP (currently only one supported). This function should not be used by protocols not supporting this kind of features, but rather osmo_sock_init2() should be used instead. See osmo_sock_init2() for more information on flags and general behavior.

References addrinfo_has_in6addr_any(), addrinfo_has_v4v6addr(), addrinfo_helper_multi(), addrinfo_to_sockaddr(), DLGLOBAL, ENOTSUP, flags, LOGL_ERROR, LOGL_NOTICE, LOGP, multiaddr_snprintf(), OSMO_SOCK_F_BIND, OSMO_SOCK_F_CONNECT, osmo_sock_init_tail(), OSMO_SOCK_MAX_ADDRS, proto, socket_helper_multiaddr(), subsys, and type.

## osmo_sock_init2_ofd()

int osmo_sock_init2_ofd | ( | struct osmo_fd * | ofd,
|  | int | family,
|  | int | type,
|  | int | proto,
|  | const char * | local_host,
|  | uint16_t | local_port,
|  | const char * | remote_host,
|  | uint16_t | remote_port,
|  | unsigned int | flags
| ) |  |

Initialize a socket and fill osmo_fd.

Parameters

  [out] | ofd | file descriptor (will be filled in)
  [in] | family | Address Family like AF_INET, AF_INET6, AF_UNSPEC
  [in] | type | Socket type like SOCK_DGRAM, SOCK_STREAM
  [in] | proto | Protocol like IPPROTO_TCP, IPPROTO_UDP
  [in] | local_host | local host name or IP address in string form
  [in] | local_port | local port number in host byte order
  [in] | remote_host | remote host name or IP address in string form
  [in] | remote_port | remote port number in host byte order
  [in] | flags | flags like OSMO_SOCK_F_CONNECT

Returns
  socket fd on success; negative on error

This function creates (and optionall binds/connects) a socket using osmo_sock_init2, but also fills the ofd structure.

References flags, osmo_fd_init_ofd(), osmo_sock_init2(), proto, subsys, and type.

## osmo_sock_init_ofd()

int osmo_sock_init_ofd | ( | struct osmo_fd * | ofd,
|  | int | family,
|  | int | type,
|  | int | proto,
|  | const char * | host,
|  | uint16_t | port,
|  | unsigned int | flags
| ) |  |

Initialize a socket and fill osmo_fd.

Parameters

  [out] | ofd | file descriptor (will be filled in)
  [in] | family | Address Family like AF_INET, AF_INET6, AF_UNSPEC
  [in] | type | Socket type like SOCK_DGRAM, SOCK_STREAM
  [in] | proto | Protocol like IPPROTO_TCP, IPPROTO_UDP
  [in] | host | remote host name or IP address in string form
  [in] | port | remote port number in host byte order
  [in] | flags | flags like OSMO_SOCK_F_CONNECT

Returns
  socket fd on success; negative on error

This function creates (and optionall binds/connects) a socket using osmo_sock_init, but also fills the ofd structure.

References flags, osmo_fd_init_ofd(), osmo_sock_init(), proto, and type.

## osmo_sock_init_osa()

int osmo_sock_init_osa | ( | uint16_t | type,
|  | uint8_t | proto,
|  | const struct osmo_sockaddr * | local,
|  | const struct osmo_sockaddr * | remote,
|  | unsigned int | flags
| ) |  |

Initialize a socket (including bind and/or connect)

Parameters

  [in] | family | Address Family like AF_INET, AF_INET6, AF_UNSPEC
  [in] | type | Socket type like SOCK_DGRAM, SOCK_STREAM
  [in] | proto | Protocol like IPPROTO_TCP, IPPROTO_UDP
  [in] | local | local address
  [in] | remote | remote address
  [in] | flags | flags like OSMO_SOCK_F_CONNECT

Returns
  socket file descriptor on success; negative on error

This function creates a new socket of the type and proto and optionally binds it to the local as well as optionally connects it to the remote depending on the value * of flags parameter.

As opposed to osmo_sock_init(), this function allows to combine the OSMO_SOCK_F_BIND and OSMO_SOCK_F_CONNECT flags. This is useful if you want to connect to a remote host/port, but still want to bind that socket to either a specific local alias IP and/or a specific local source port.

You must specify either OSMO_SOCK_F_BIND, or OSMO_SOCK_F_CONNECT, or both.

If OSMO_SOCK_F_NONBLOCK is specified, the socket will be set to non-blocking mode.

References _SOCKADDR_TO_STR, DLGLOBAL, flags, LOGL_ERROR, LOGP, OSMO_SOCK_F_BIND, OSMO_SOCK_F_CONNECT, OSMO_SOCK_F_UDP_REUSEADDR, osmo_sock_init_tail(), OSMO_SOCKADDR_STR_FMT, OSMO_SOCKADDR_STR_FMT_ARGS, proto, osmo_sockaddr::sa, socket_helper_osa(), subsys, type, and osmo_sockaddr::u.

Referenced by osmo_sock_init_osa_ofd(), and osmo_sockaddr_local_ip().

## osmo_sock_init_osa_ofd()

int osmo_sock_init_osa_ofd | ( | struct osmo_fd * | ofd,
|  | int | type,
|  | int | proto,
|  | const struct osmo_sockaddr * | local,
|  | const struct osmo_sockaddr * | remote,
|  | unsigned int | flags
| ) |  |

References flags, osmo_fd_init_ofd(), osmo_sock_init_osa(), proto, and type.

## osmo_sock_init_sa()

int osmo_sock_init_sa | ( | struct sockaddr * | ss,
|  | uint16_t | type,
|  | uint8_t | proto,
|  | unsigned int | flags
| ) |  |

Initialize a socket and fill sockaddr.

Parameters

  [out] | ss | socket address (will be filled in)
  [in] | type | Socket type like SOCK_DGRAM, SOCK_STREAM
  [in] | proto | Protocol like IPPROTO_TCP, IPPROTO_UDP
  [in] | flags | flags like OSMO_SOCK_F_CONNECT

Returns
  socket fd on success; negative on error

This function creates (and optionall binds/connects) a socket using osmo_sock_init, but also fills the ss structure.

References DLGLOBAL, flags, LOGL_ERROR, LOGP, osmo_sock_init(), proto, subsys, and type.

Referenced by gsmtap_source_add_sink_fd().

## osmo_sock_init_tail()

static int osmo_sock_init_tail | ( | int | fd,
|  | uint16_t | type,
|  | unsigned int | flags
| ) |  |

static

References DLGLOBAL, flags, LOGL_ERROR, LOGP, OSMO_SOCK_F_BIND, OSMO_SOCK_F_CONNECT, OSMO_SOCK_F_NO_MCAST_ALL, OSMO_SOCK_F_NO_MCAST_LOOP, osmo_sock_mcast_all_set(), osmo_sock_mcast_loop_set(), subsys, and type.

Referenced by osmo_sock_init(), osmo_sock_init2(), osmo_sock_init2_multiaddr(), osmo_sock_init_osa(), and osmo_sock_unix_init().

## osmo_sock_local_ip()

int osmo_sock_local_ip | ( | char * | local_ip,
|  | const char * | remote_ip
| ) |  |

Determine the matching local IP-address for a given remote IP-Address.

Parameters

  [out] | local_ip | caller provided memory for resulting local IP-address
  [in] | remote_ip | remote IP-address

Returns
  0 on success; negative otherwise

The function accepts IPv4 and IPv6 address strings. The caller must provide at least INET6_ADDRSTRLEN bytes for local_ip if an IPv6 is expected as as result. For IPv4 addresses the required amount is INET_ADDRSTRLEN.

References OSMO_SOCK_F_CONNECT, osmo_sock_init2(), and subsys.

## osmo_sock_mcast_all_set()

int osmo_sock_mcast_all_set | ( | int | fd,
|  | bool | enable
| ) |  |

Enable/disable receiving all multicast packets, even for non-subscribed groups.

Parameters

  [in] | fd | file descriptor of related socket
  [in] | enable | Enable or Disable receiving of all packets

Returns
  0 on success; negative otherwise

References sock_get_domain(), and subsys.

Referenced by osmo_sock_init_tail().

## osmo_sock_mcast_iface_set()

int osmo_sock_mcast_iface_set | ( | int | fd,
|  | const char * | ifname
| ) |  |

Set the network device to which we should bind the multicast socket.

Parameters

  [in] | fd | file descriptor of related socket
  [in] | ifname | name of network interface to user for multicast

Returns
  0 on success; negative otherwise

References subsys.

## osmo_sock_mcast_loop_set()

int osmo_sock_mcast_loop_set | ( | int | fd,
|  | bool | enable
| ) |  |

Activate or de-activate local loop-back of transmitted multicast packets.

Parameters

  [in] | fd | file descriptor of related socket
  [in] | enable | Enable (true) or disable (false) loop-back

Returns
  0 on success; negative otherwise

References sock_get_domain(), and subsys.

Referenced by osmo_sock_init_tail().

## osmo_sock_mcast_subscribe()

int osmo_sock_mcast_subscribe | ( | int | fd,
|  | const char * | grp_addr
| ) |  |

Subscribe to the given IP multicast group.

Parameters

  [in] | fd | file descriptor of related scoket
  [in] | grp_addr | ASCII representation of the multicast group address

Returns
  0 on success; negative otherwise

References sock_get_domain(), and subsys.

## osmo_sock_mcast_ttl_set()

int osmo_sock_mcast_ttl_set | ( | int | fd,
|  | uint8_t | ttl
| ) |  |

Set the TTL of outbound multicast packets.

Parameters

  [in] | fd | file descriptor of related socket
  [in] | ttl | TTL of to-be-sent multicast packets

Returns
  0 on success; negative otherwise

References sock_get_domain(), and subsys.

## osmo_sock_set_dscp()

int osmo_sock_set_dscp | ( | int | fd,
|  | uint8_t | dscp
| ) |  |

Set the DSCP (differentiated services code point) of a socket.

Parameters

  [in] | dscp | DSCP value in range 0..63

Returns
  0 on success; negative on error.

References DLGLOBAL, LOGL_ERROR, LOGP, and subsys.

Referenced by socket_helper_tail().

## osmo_sock_set_priority()

int osmo_sock_set_priority | ( | int | fd,
|  | int | prio
| ) |  |

Set the priority value of a socket.

Parameters

  [in] | prio | priority value. Values outside 0..6 require CAP_NET_ADMIN.

Returns
  0 on success; negative on error.

References subsys.

Referenced by socket_helper_tail().

## osmo_sock_unix_init()

int osmo_sock_unix_init | ( | uint16_t | type,
|  | uint8_t | proto,
|  | const char * | socket_path,
|  | unsigned int | flags
| ) |  |

Initialize a unix domain socket (including bind/connect)

Parameters

  [in] | type | Socket type like SOCK_DGRAM, SOCK_STREAM
  [in] | proto | Protocol like IPPROTO_TCP, IPPROTO_UDP
  [in] | socket_path | path to identify the socket
  [in] | flags | flags like OSMO_SOCK_F_CONNECT

Returns
  socket fd on success; negative on error

This function creates a new unix domain socket, type and proto and optionally binds or connects it, depending on the value of flags parameter.

References DLGLOBAL, flags, LOGL_ERROR, LOGP, OSMO_SOCK_F_BIND, OSMO_SOCK_F_CONNECT, osmo_sock_init_tail(), osmo_strlcpy(), proto, socket_helper_tail(), subsys, and type.

Referenced by osmo_sock_unix_init_ofd().

## osmo_sock_unix_init_ofd()

int osmo_sock_unix_init_ofd | ( | struct osmo_fd * | ofd,
|  | uint16_t | type,
|  | uint8_t | proto,
|  | const char * | socket_path,
|  | unsigned int | flags
| ) |  |

Initialize a unix domain socket and fill osmo_fd.

Parameters

  [out] | ofd | file descriptor (will be filled in)
  [in] | type | Socket type like SOCK_DGRAM, SOCK_STREAM
  [in] | proto | Protocol like IPPROTO_TCP, IPPROTO_UDP
  [in] | socket_path | path to identify the socket
  [in] | flags | flags like OSMO_SOCK_F_CONNECT

Returns
  socket fd on success; negative on error

This function creates (and optionally binds/connects) a socket using osmo_sock_unix_init, but also fills the ofd structure.

References flags, osmo_fd_init_ofd(), osmo_sock_unix_init(), proto, subsys, and type.

## osmo_sockaddr_cmp()

int osmo_sockaddr_cmp | ( | const struct osmo_sockaddr * | a,
|  | const struct osmo_sockaddr * | b
| ) |  |

Compare two osmo_sockaddr.

Parameters

  [in] | a |
  [in] | b |

Returns
  0 if a and b are equal. Otherwise it follows memcmp()

References OSMO_CMP, and subsys.

## osmo_sockaddr_from_octets()

int osmo_sockaddr_from_octets | ( | struct osmo_sockaddr * | os,
|  | const void * | src,
|  | size_t | src_len
| ) |  |

Copy the addr part, the IP address octets in network byte order, from a buffer.

Useful for decoding network protocols.

Parameters

  [out] | os | Write IP address to this sockaddr.
  [in] | src | Source buffer to read IP address octets from.
  [in] | src_len | Number of octets to copy.

Returns
  number of octets read on success, negative on error.

References ENOTSUP, len, and subsys.

## osmo_sockaddr_in_to_str_and_uint()

size_t osmo_sockaddr_in_to_str_and_uint | ( | char * | addr,
|  | unsigned int | addr_len,
|  | uint16_t * | port,
|  | const struct sockaddr_in * | sin
| ) |  |

Convert sockaddr_in to IP address as char string and port as uint16_t.

Parameters

  [out] | addr | String buffer to write IP address to, or NULL.
  [out] | addr_len | Size of addr.
  [out] | port | Pointer to uint16_t to write the port number to, or NULL.
  [in] | sin | Sockaddr to convert.

Returns
  the required string buffer size, like osmo_strlcpy(), or 0 if addr is NULL.

References osmo_strlcpy(), and subsys.

Referenced by osmo_sockaddr_to_str_and_uint().

## osmo_sockaddr_is_local()

int osmo_sockaddr_is_local | ( | struct sockaddr * | addr,
|  | unsigned int | addrlen
| ) |  |

Determine if the given address is a local address.

Parameters

  [in] | addr | Socket Address
  [in] | addrlen | Length of socket address in bytes

Returns
  1 if address is local, 0 otherwise.

References DLGLOBAL, LOGL_ERROR, LOGP, sockaddr_equal(), sockaddr_is_local_routed(), and subsys.

Referenced by gsmtap_source_add_sink_fd().

## osmo_sockaddr_local_ip()

int osmo_sockaddr_local_ip | ( | struct osmo_sockaddr * | local_ip,
|  | const struct osmo_sockaddr * | remote_ip
| ) |  |

Determine the matching local address for a given remote address.

Parameters

  [out] | local_ip | caller provided memory for resulting local address
  [in] | remote_ip | remote address

Returns
  0 on success; negative otherwise

References OSMO_SOCK_F_CONNECT, osmo_sock_init_osa(), and subsys.

## osmo_sockaddr_ntop()

const char * osmo_sockaddr_ntop | ( | const struct sockaddr * | sa,
|  | char * | dst
| ) |  |

inet_ntop() wrapper for a struct sockaddr.

Parameters

  [in] | sa | source sockaddr to get the address from.
  [out] | dst | string buffer of at least INET6_ADDRSTRLEN size.

Returns
  returns a non-null pointer to dst. NULL is returned if there was an error, with errno set to indicate the error.

References osmo_sockaddr::sa, and subsys.

## osmo_sockaddr_port()

uint16_t osmo_sockaddr_port | ( | const struct sockaddr * | sa | ) |

Get sockaddr port content (in host byte order)

Parameters

  [in] | sa | source sockaddr to get the port from.

Returns
  returns the sockaddr port in host byte order

References osmo_sockaddr::sa, and subsys.

## osmo_sockaddr_set_port()

void osmo_sockaddr_set_port | ( | struct sockaddr * | sa,
|  | uint16_t | port
| ) |  |

Set sockaddr port content (to network byte order).

Parameters

  [out] | sa | sockaddr to set the port of.
  [in] | port | port nr to set.

References osmo_sockaddr::sa, osmo_sockaddr::sin6, subsys, and osmo_sockaddr::u.

## osmo_sockaddr_to_octets()

int osmo_sockaddr_to_octets | ( | uint8_t * | dst,
|  | size_t | dst_maxlen,
|  | const struct osmo_sockaddr * | os
| ) |  |

Copy the addr part, the IP address octets in network byte order, to a buffer.

Useful for encoding network protocols.

Parameters

  [out] | dst | Write octets to this buffer.
  [in] | dst_maxlen | Space available in buffer.
  [in] | os | Sockaddr to copy IP of.

Returns
  nr of octets written on success, negative on error.

References ENOTSUP, len, and subsys.

## osmo_sockaddr_to_str()

const char * osmo_sockaddr_to_str | ( | const struct osmo_sockaddr * | sockaddr | ) |

string-format a given osmo_sockaddr address

Parameters

  [in] | sockaddr | the osmo_sockaddr to print

Returns
  pointer to the string on success; NULL on error

References osmo_sockaddr_to_str_buf(), and subsys.

## osmo_sockaddr_to_str_and_uint()

unsigned int osmo_sockaddr_to_str_and_uint | ( | char * | addr,
|  | unsigned int | addr_len,
|  | uint16_t * | port,
|  | const struct sockaddr * | sa
| ) |  |

Convert sockaddr to IP address as char string and port as uint16_t.

Parameters

  [out] | addr | String buffer to write IP address to, or NULL.
  [out] | addr_len | Size of addr.
  [out] | port | Pointer to uint16_t to write the port number to, or NULL.
  [in] | sa | Sockaddr to convert.

Returns
  the required string buffer size, like osmo_strlcpy(), or 0 if addr is NULL.

References osmo_sockaddr_in_to_str_and_uint(), and subsys.

Referenced by osmo_sockaddr_to_str_buf2().

## osmo_sockaddr_to_str_buf()

char * osmo_sockaddr_to_str_buf | ( | char * | buf,
|  | size_t | buf_len,
|  | const struct osmo_sockaddr * | sockaddr
| ) |  |

string-format a given osmo_sockaddr address into a user-supplied buffer.

Like osmo_sockaddr_to_str_buf2() but returns buf, or NULL if too short.

Parameters

  [in] | buf | user-supplied output buffer
  [in] | buf_len | size of the user-supplied output buffer in bytes
  [in] | sockaddr | the osmo_sockaddr to print

Returns
  pointer to the string on success; NULL on error

References osmo_strbuf::buf, osmo_strbuf::chars_needed, osmo_sockaddr_to_str_buf2(), and subsys.

Referenced by osmo_sockaddr_to_str().

## osmo_sockaddr_to_str_buf2()

int osmo_sockaddr_to_str_buf2 | ( | char * | buf,
|  | size_t | buf_len,
|  | const struct osmo_sockaddr * | sockaddr
| ) |  |

string-format a given osmo_sockaddr address into a user-supplied buffer.

Same as osmo_sockaddr_to_str_buf() but returns a would-be length in snprintf() style.

Parameters

  [in] | buf | user-supplied output buffer
  [in] | buf_len | size of the user-supplied output buffer in bytes
  [in] | sockaddr | the osmo_sockaddr to print

Returns
  number of characters that would be written if the buffer is large enough, like snprintf().

References osmo_strbuf::buf, osmo_sockaddr_to_str_and_uint(), OSMO_STRBUF_APPEND, OSMO_STRBUF_PRINTF, and subsys.

Referenced by osmo_sockaddr_to_str_buf(), and osmo_sockaddr_to_str_c().

## osmo_sockaddr_to_str_c()

char * osmo_sockaddr_to_str_c | ( | void * | ctx,
|  | const struct osmo_sockaddr * | sockaddr
| ) |  |

string-format a given osmo_sockaddr address into a talloc allocated buffer.

Like osmo_sockaddr_to_str_buf2() but returns a talloc allocated string.

Parameters

  [in] | ctx | talloc context to allocate from, e.g. OTC_SELECT.
  [in] | sockaddr | the osmo_sockaddr to print.

Returns
  human readable string.

References OSMO_NAME_C_IMPL, osmo_sockaddr_to_str_buf2(), and subsys.

## sock_get_domain()

static int sock_get_domain | ( | int | fd | ) |

static

References subsys.

Referenced by osmo_sock_mcast_all_set(), osmo_sock_mcast_loop_set(), osmo_sock_mcast_subscribe(), and osmo_sock_mcast_ttl_set().

## sockaddr_equal()

static int sockaddr_equal | ( | const struct sockaddr * | a,
|  | const struct sockaddr * | b,
|  | unsigned int | len
| ) |  |

static

References subsys.

Referenced by osmo_sockaddr_is_local().

## sockaddr_is_local_routed()

static int sockaddr_is_local_routed | ( | const struct sockaddr * | a | ) |

static

References subsys.

Referenced by osmo_sockaddr_is_local().

## socket_helper()

static int socket_helper | ( | const struct addrinfo * | rp,
|  | unsigned int | flags
| ) |  |

static

References DLGLOBAL, flags, LOGL_ERROR, LOGP, socket_helper_tail(), and subsys.

Referenced by osmo_sock_init(), and osmo_sock_init2().

## socket_helper_multiaddr()

static int socket_helper_multiaddr | ( | uint16_t | family,
|  | uint16_t | type,
|  | uint8_t | proto,
|  | unsigned int | flags
| ) |  |

static

References DLGLOBAL, flags, LOGL_ERROR, LOGP, proto, socket_helper_tail(), subsys, and type.

Referenced by osmo_sock_init2_multiaddr().

## socket_helper_osa()

static int socket_helper_osa | ( | const struct osmo_sockaddr * | addr,
|  | uint16_t | type,
|  | uint8_t | proto,
|  | unsigned int | flags
| ) |  |

static

References DLGLOBAL, flags, LOGL_ERROR, LOGP, proto, osmo_sockaddr::sa, socket_helper_tail(), subsys, type, and osmo_sockaddr::u.

Referenced by osmo_sock_init_osa().

## socket_helper_tail()

static int socket_helper_tail | ( | int | sfd,
|  | unsigned int | flags
| ) |  |

static

References DLGLOBAL, flags, GET_OSMO_SOCK_F_DSCP, GET_OSMO_SOCK_F_PRIO, LOGL_ERROR, LOGP, OSMO_SOCK_F_NONBLOCK, osmo_sock_set_dscp(), osmo_sock_set_priority(), and subsys.

Referenced by osmo_sock_unix_init(), socket_helper(), socket_helper_multiaddr(), and socket_helper_osa().