Sockaddr

Overview

Definition of API for working with struct sockaddr. More…

// typedefs

typedef struct te_sockaddr_subnet te_sockaddr_subnet;

// structs

struct te_sockaddr_subnet;

// global functions

static te_bool te_sockaddr_is_af_supported(int af);
void te_sockaddr_clear_port(struct sockaddr* addr);
uint16_t* te_sockaddr_get_port_ptr(const struct sockaddr* addr);
static uint16_t te_sockaddr_get_port(const struct sockaddr* addr);
void te_sockaddr_set_port(struct sockaddr* addr, uint16_t port);
void* te_sockaddr_get_netaddr(const struct sockaddr* addr);
const char* te_sockaddr_get_ipstr(const struct sockaddr* addr);
char* te_ip2str(const struct sockaddr* addr);
te_errno te_ip_addr2te_str(te_string* str, const void* ip_addr, int af);
te_errno te_mac_addr2te_str(te_string* str, const uint8_t* mac_addr);
int te_sockaddr_set_netaddr(struct sockaddr* addr, const void* net_addr);
void te_sockaddr_set_wildcard(struct sockaddr* addr);
void te_sockaddr_set_loopback(struct sockaddr* addr);
void te_sockaddr_set_multicast(struct sockaddr* addr);
te_bool te_sockaddr_is_wildcard(const struct sockaddr* addr);
te_bool te_sockaddr_is_multicast(const struct sockaddr* addr);
size_t te_sockaddr_get_size_by_af(int af);
size_t te_sockaddr_get_size(const struct sockaddr* addr);
te_errno te_sockaddr_mask_by_prefix(struct sockaddr* mask, socklen_t masklen, int af, unsigned int prefix);
te_errno te_sockaddr_cleanup_to_prefix(struct sockaddr* addr, unsigned int prefix);
int te_sockaddrcmp(const struct sockaddr* a1, socklen_t a1len, const struct sockaddr* a2, socklen_t a2len);
int te_sockaddrcmp_no_ports(const struct sockaddr* a1, socklen_t a1len, const struct sockaddr* a2, socklen_t a2len);
int te_sockaddrncmp(const struct sockaddr* a1, socklen_t a1len, const struct sockaddr* a2, socklen_t a2len);
te_errno te_sockaddr2str_buf(const struct sockaddr* sa, char* buf, size_t len);
const char* te_sockaddr2str(const struct sockaddr* sa);
te_errno te_sockaddr_h2str_buf(const struct sockaddr* sa, char* buf, size_t len);
te_errno te_sockaddr_h2str(const struct sockaddr* sa, char** string);
te_errno te_sockaddr_str2h(const char* string, struct sockaddr* sa);
size_t te_netaddr_get_size(int af);
size_t te_netaddr_get_bitsize(int af);
void te_mreq_set_mr_multiaddr(int af, void* mreq, const void* addr);
void te_mreq_set_mr_interface(int af, void* mreq, const void* addr);
void te_mreq_set_mr_ifindex(int af, void* mreq, int ifindex);
te_errno te_sockaddr_netaddr_from_string(const char* addr_str, struct sockaddr* addr);
const char* te_sockaddr_netaddr_to_string(int af, const void* net_addr);
te_errno te_sockaddr_ip4_to_ip6_mapped(struct sockaddr* addr);

// macros

#define MASK2PREFIX(mask, prefix)
#define PREFIX2MASK(prefix)
#define TE_SOCKADDR_STR_LEN

Detailed Documentation

Definition of API for working with struct sockaddr.

Typedefs

typedef struct te_sockaddr_subnet te_sockaddr_subnet

An address with a network prefix.

Global Functions

static te_bool te_sockaddr_is_af_supported(int af)

Is address family is supported by this TAPI?

Parameters:

af

Address family

void te_sockaddr_clear_port(struct sockaddr* addr)

Set “port” part of corresponding struct sockaddr to zero (wildcard)

The function is not very safe because it does not get the length of the structure assuming that there is enough space for a structure defined from the value of “sa_family” field.

Parameters:

addr

Generic address structure

uint16_t* te_sockaddr_get_port_ptr(const struct sockaddr* addr)

Get pointer to “port” part of corresponding struct sockaddr.

The function is not very safe because it does not get the length of the structure assuming that there is enough space for a structure defined from the value of “sa_family” field.

Parameters:

addr

Generic address structure

Returns:

Pointer to “port” part

static uint16_t te_sockaddr_get_port(const struct sockaddr* addr)

Get “port” part of corresponding struct sockaddr.

The function is not very safe because it does not get the length of the structure assuming that there is enough space for a structure defined from the value of “sa_family” field.

Parameters:

addr

Generic address structure

Returns:

Port number in network byte order

void te_sockaddr_set_port(struct sockaddr* addr, uint16_t port)

Update “port” part of corresponding struct sockaddr.

The function is not very safe because it does not get the length of the structure assuming that there is enough space for a structure defined from the value of “sa_family” field.

Parameters:

addr

Generic address structure

port

A new port value (port should be in network byte order)

void* te_sockaddr_get_netaddr(const struct sockaddr* addr)

Returns pointer to network address part of sockaddr structure according to ‘sa_family’ field of the structure.

If addr is NULL, this function simply returns NULL.

Parameters:

addr

Generic address structure

Returns:

Pointer to corresponding network address or NULL.

const char* te_sockaddr_get_ipstr(const struct sockaddr* addr)

Returns pointer to the IP 4/6 address in human representation (without port).

non-rentarable

non-thread-safe

does not perform any checks

Parameters:

addr

address to be converted

Returns:

address string

See also:

te_ip2str, te_sockaddr2str

char* te_ip2str(const struct sockaddr* addr)

Convert IPv4 and IPv6 addresses from binary to text form.

Return value should be freed with free(3) when it is no longer needed.

Parameters:

addr

IP address.

Returns:

String representation of IP address, or NULL in case of error.

See also:

te_sockaddr_get_ipstr, te_sockaddr2str

te_errno te_ip_addr2te_str(te_string* str, const void* ip_addr, int af)

Append IPv4 or IPv6 address to TE string.

Parameters:

str

Pointer to TE string

ip_addr

Pointer to in_addr or in6_addr

af

AF_INET or AF_INET6

Returns:

Status code.

te_errno te_mac_addr2te_str(te_string* str, const uint8_t* mac_addr)

Append MAC address to TE string.

Parameters:

str

Pointer to the TE string

mac_addr

Pointer to the MAC address

Returns:

Status code.

int te_sockaddr_set_netaddr(struct sockaddr* addr, const void* net_addr)

Update network address part of sockaddr structure according to ‘sa_family’ field of the structure

Parameters:

addr

Generic address structure

net_addr

Pointer to the network address to be set

0

on success

-1

on failure

Returns:

Result of the operation

void te_sockaddr_set_wildcard(struct sockaddr* addr)

Set “network address” part of corresponding struct sockaddr to wildcard

The function is not very safe because it does not get the length of the structure assuming that there is enough space for a structure defined from the value of “sa_family” field.

Parameters:

addr

Generic address structure

void te_sockaddr_set_loopback(struct sockaddr* addr)

Set “network address” part of corresponding struct sockaddr to loopback

The function is not very safe because it does not get the length of the structure assuming that there is enough space for a structure defined from the value of “sa_family” field.

Parameters:

addr

Generic address structure

void te_sockaddr_set_multicast(struct sockaddr* addr)

Set “network address” part of corresponding struct sockaddr to randomly chosen multicast address.

The function is not very safe because it does not get the length of the structure assuming that there is enough space for an address.

Parameters:

addr

Generic address structure

te_bool te_sockaddr_is_wildcard(const struct sockaddr* addr)

Check if “network address” part of corresponding struct sockaddr is wildcard

The function is not very safe because it does not get the length of the structure assuming that there is enough space for a structure defined from the value of “sa_family” field.

Parameters:

addr

Generic address structure

Returns:

Is “network address” part wildcard?

te_bool te_sockaddr_is_multicast(const struct sockaddr* addr)

Check if “network address” part of corresponding struct sockaddr is multicast address

Parameters:

addr

Generic address struture

Returns:

TRUE/FALSE - multicast/other address

size_t te_sockaddr_get_size_by_af(int af)

Returns the size of a particular sockaddr structure according to its family.

Parameters:

af

Address family

Returns:

Address size

size_t te_sockaddr_get_size(const struct sockaddr* addr)

Returns the size of a particular sockaddr structure according to ‘sa_family’ field of the structure

Parameters:

addr

Generic address structure

Returns:

Address size

te_errno te_sockaddr_mask_by_prefix(struct sockaddr* mask, socklen_t masklen, int af, unsigned int prefix)

Create mask in ‘struct sockaddr’ by prefix length.

Parameters:

mask

Location for mask

masklen

Size of the memory for mask

af

Address family

prefix

Prefix length

Returns:

Status code.

te_errno te_sockaddr_cleanup_to_prefix(struct sockaddr* addr, unsigned int prefix)

Clean up network address part to be prefix of specified length.

Parameters:

addr

address

prefix

prefix length

Returns:

Status code.

int te_sockaddrcmp(const struct sockaddr* a1, socklen_t a1len, const struct sockaddr* a2, socklen_t a2len)

Compare ‘struct sockaddr’.

Parameters:

a1

the first address

a1len

the first address length

a2

the second address

a2len

the second address length

0

equal

-1

not equal

-2

comparison of addresses of unsupported family

int te_sockaddrcmp_no_ports(const struct sockaddr* a1, socklen_t a1len, const struct sockaddr* a2, socklen_t a2len)

Compare ‘struct sockaddr’, not taking ports into account.

Parameters:

a1

the first address

a1len

the first address length

a2

the second address

a2len

the second address length

0

equal

-1

not equal

-2

comparison of addresses of unsupported family

int te_sockaddrncmp(const struct sockaddr* a1, socklen_t a1len, const struct sockaddr* a2, socklen_t a2len)

Compare the content of two ‘struct sockaddr’ structures till minimum of two lengths a1len and a2len.

Parameters:

a1

the first address

a1len

the first address length

a2

the second address

a2len

the second address length

0

equal

-1

not equal

-2

comparison of addresses of unsupported family

te_errno te_sockaddr2str_buf(const struct sockaddr* sa, char* buf, size_t len)

Convert ‘struct sockaddr’ to string (it prints not only address but also port and other fields).

Parameters:

sa

Pointer to ‘struct sockaddr’.

buf

Where to save string.

len

Length of the buffer.

Returns:

Status code.

const char* te_sockaddr2str(const struct sockaddr* sa)

Convert ‘struct sockaddr’ to string (it prints not only address but also port and other fields).

Static buffer is used for return value.

Parameters:

sa

  • pointer to ‘struct sockaddr’

Returns:

null-terminated string

See also:

te_sockaddr_get_ipstr, te_ip2str

te_errno te_sockaddr_h2str_buf(const struct sockaddr* sa, char* buf, size_t len)

Convert sockaddr to string, save result in a provided buffer.

Parameters:

sa

Pointer to struct sockaddr.

buf

Where to save a string representation.

len

Number of bytes available for a string.

Returns:

Status code.

te_errno te_sockaddr_h2str(const struct sockaddr* sa, char** string)

Convert a sockaddr to a string or return an error.

Free allocated memory on string

Parameters:

sa

source sockaddr

string

result of conversion

0

successful

TE_EAFNOSUPPORT

family is not supported

value

of errno inet_ntop was fail

Returns:

status code

te_errno te_sockaddr_str2h(const char* string, struct sockaddr* sa)

Convert a string to a sockaddr or return an error.

Parameters:

string

source string

sa

result sockaddr

0

successful

TE_EAFNOSUPPORT

family is not supported

TE_EINVAL

string doesn’t contain a valid network address

Returns:

status code

size_t te_netaddr_get_size(int af)

Returns the size of network address from a particular family (in bytes).

Parameters:

af

Address family

Returns:

Number of bytes used under network address

size_t te_netaddr_get_bitsize(int af)

Returns the size of network address from a particular family (in bits).

Parameters:

af

Address family

Returns:

Number of bits required to store network address.

void te_mreq_set_mr_multiaddr(int af, void* mreq, const void* addr)

Set multicast address part of XXX_mreq(n) structure

Parameters:

af

Address family

mreq

Generic mreq structure

addr

Multicast address

void te_mreq_set_mr_interface(int af, void* mreq, const void* addr)

Set interface part of XXX_mreq(n) structure

Parameters:

af

Address family

mreq

Generic mreq structure

addr

Interface address

void te_mreq_set_mr_ifindex(int af, void* mreq, int ifindex)

Set interface index part of XXX_mreq(n) structure

Parameters:

af

Address family

mreq

Generic mreq structure

ifindex

Interface index

te_errno te_sockaddr_netaddr_from_string(const char* addr_str, struct sockaddr* addr)

Convert network address from string and put it in provided sockaddr structure. Set address family appropriately.

Parameters:

addr_str

Address in string format

addr

Location for the address (should be sufficient for sockadddr structure which corresponds to provided address in string format, e.g. struct te_sockaddr_storage)

Returns:

Status code.

const char* te_sockaddr_netaddr_to_string(int af, const void* net_addr)

Convert network address to its string representation. It returns statically allocated string using te_sockaddr_get_ipstr()

Parameters:

af

Address family

net_addr

Network address in form acceptable to te_sockaddr_set_netaddr()

Returns:

Network address string representation, or NULL in case of error

te_errno te_sockaddr_ip4_to_ip6_mapped(struct sockaddr* addr)

Convert IPv4 address to IPv4-mapped IPv6 one.

Parameters:

addr

Sockaddr structure containing address. It must be large enough to hold IPv6 address.

Returns:

Status code.

Macros

#define MASK2PREFIX(mask, prefix)

Fast conversion of IPv4 the network mask to prefix

#define PREFIX2MASK(prefix)

Fast conversion of the IPv4 prefix to network mask

#define TE_SOCKADDR_STR_LEN

Number of bytes which should be enough for string representation of sockaddr structure