Asterisk - The Open Source Telephony Project GIT-master-5782b03
Data Structures | Macros | Enumerations | Functions
netsock2.h File Reference

Network socket handling. More...

#include <sys/socket.h>
#include <netinet/in.h>
#include "asterisk/logger.h"
Include dependency graph for netsock2.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ast_sockaddr
 Socket address structure. More...
 

Macros

#define AST_SOCKADDR_BUFLEN   (45 + 2 + 1 + 5 + 1)
 
#define ast_sockaddr_port(addr)   _ast_sockaddr_port(addr, __FILE__, __LINE__, __PRETTY_FUNCTION__)
 Get the port number of a socket address. More...
 
#define ast_sockaddr_set_port(addr, port)   _ast_sockaddr_set_port(addr,port,__FILE__,__LINE__,__PRETTY_FUNCTION__)
 Sets the port number of a socket address. More...
 
#define AST_SOCKADDR_STR_ADDR   (1 << 0)
 
#define AST_SOCKADDR_STR_ADDR_REMOTE   (AST_SOCKADDR_STR_ADDR | AST_SOCKADDR_STR_REMOTE)
 
#define AST_SOCKADDR_STR_BRACKETS   (1 << 2)
 
#define AST_SOCKADDR_STR_DEFAULT   (AST_SOCKADDR_STR_ADDR | AST_SOCKADDR_STR_PORT)
 
#define AST_SOCKADDR_STR_DEFAULT_REMOTE   (AST_SOCKADDR_STR_DEFAULT | AST_SOCKADDR_STR_REMOTE)
 
#define AST_SOCKADDR_STR_FORMAT_MASK   (AST_SOCKADDR_STR_ADDR | AST_SOCKADDR_STR_PORT | AST_SOCKADDR_STR_BRACKETS)
 
#define AST_SOCKADDR_STR_HOST   (AST_SOCKADDR_STR_ADDR | AST_SOCKADDR_STR_BRACKETS)
 
#define AST_SOCKADDR_STR_HOST_REMOTE   (AST_SOCKADDR_STR_HOST | AST_SOCKADDR_STR_REMOTE)
 
#define AST_SOCKADDR_STR_PORT   (1 << 1)
 
#define AST_SOCKADDR_STR_REMOTE   (1 << 3)
 
#define V6_WORD(sin6, index)   ((uint32_t *)&((sin6)->sin6_addr))[(index)]
 Isolate a 32-bit section of an IPv6 address. More...
 

Enumerations

enum  { AST_AF_UNSPEC = AF_UNSPEC , AST_AF_INET = AF_INET , AST_AF_INET6 = AF_INET6 }
 
enum  ast_transport {
  AST_TRANSPORT_UDP = 1 , AST_TRANSPORT_TCP = 1 << 1 , AST_TRANSPORT_TLS = 1 << 2 , AST_TRANSPORT_WS = 1 << 3 ,
  AST_TRANSPORT_WSS = 1 << 4
}
 

Functions

uint16_t _ast_sockaddr_port (const struct ast_sockaddr *addr, const char *file, int line, const char *func)
 
void _ast_sockaddr_set_port (struct ast_sockaddr *addr, uint16_t port, const char *file, int line, const char *func)
 
int ast_accept (int sockfd, struct ast_sockaddr *addr)
 Wrapper around accept(2) that uses struct ast_sockaddr. More...
 
int ast_bind (int sockfd, const struct ast_sockaddr *addr)
 Wrapper around bind(2) that uses struct ast_sockaddr. More...
 
int ast_connect (int sockfd, const struct ast_sockaddr *addr)
 Wrapper around connect(2) that uses struct ast_sockaddr. More...
 
int ast_getsockname (int sockfd, struct ast_sockaddr *addr)
 Wrapper around getsockname(2) that uses struct ast_sockaddr. More...
 
ssize_t ast_recvfrom (int sockfd, void *buf, size_t len, int flags, struct ast_sockaddr *src_addr)
 Wrapper around recvfrom(2) that uses struct ast_sockaddr. More...
 
ssize_t ast_sendto (int sockfd, const void *buf, size_t len, int flags, const struct ast_sockaddr *dest_addr)
 Wrapper around sendto(2) that uses ast_sockaddr. More...
 
int ast_set_qos (int sockfd, int tos, int cos, const char *desc)
 Set type of service. More...
 
int ast_sockaddr_apply_netmask (const struct ast_sockaddr *addr, const struct ast_sockaddr *netmask, struct ast_sockaddr *result)
 Apply a netmask to an address and store the result in a separate structure. More...
 
int ast_sockaddr_cidr_bits (const struct ast_sockaddr *sa)
 Count the 1 bits in a netmask. More...
 
int ast_sockaddr_cmp (const struct ast_sockaddr *a, const struct ast_sockaddr *b)
 Compares two ast_sockaddr structures. More...
 
int ast_sockaddr_cmp_addr (const struct ast_sockaddr *a, const struct ast_sockaddr *b)
 Compares the addresses of two ast_sockaddr structures. More...
 
static void ast_sockaddr_copy (struct ast_sockaddr *dst, const struct ast_sockaddr *src)
 Copies the data from one ast_sockaddr to another. More...
 
static void ast_sockaddr_copy_sockaddr (struct ast_sockaddr *dst, struct sockaddr *src, socklen_t len)
 Copies the data from a sockaddr to an ast_sockaddr. More...
 
int ast_sockaddr_hash (const struct ast_sockaddr *addr)
 Computes a hash value from the address. The port is ignored. More...
 
uint32_t ast_sockaddr_ipv4 (const struct ast_sockaddr *addr)
 Get an IPv4 address of an ast_sockaddr. More...
 
int ast_sockaddr_ipv4_mapped (const struct ast_sockaddr *addr, struct ast_sockaddr *ast_mapped)
 Convert an IPv4-mapped IPv6 address into an IPv4 address. More...
 
int ast_sockaddr_is_any (const struct ast_sockaddr *addr)
 Determine if the address type is unspecified, or "any" address. More...
 
int ast_sockaddr_is_ipv4 (const struct ast_sockaddr *addr)
 Determine if the address is an IPv4 address. More...
 
int ast_sockaddr_is_ipv4_mapped (const struct ast_sockaddr *addr)
 Determine if this is an IPv4-mapped IPv6 address. More...
 
int ast_sockaddr_is_ipv4_multicast (const struct ast_sockaddr *addr)
 Determine if an IPv4 address is a multicast address. More...
 
int ast_sockaddr_is_ipv6 (const struct ast_sockaddr *addr)
 Determine if this is an IPv6 address. More...
 
int ast_sockaddr_is_ipv6_link_local (const struct ast_sockaddr *addr)
 Determine if this is a link-local IPv6 address. More...
 
static int ast_sockaddr_isnull (const struct ast_sockaddr *addr)
 Checks if the ast_sockaddr is null. "null" in this sense essentially means uninitialized, or having a 0 length. More...
 
int ast_sockaddr_parse (struct ast_sockaddr *addr, const char *str, int flags)
 Parse an IPv4 or IPv6 address string. More...
 
int ast_sockaddr_resolve (struct ast_sockaddr **addrs, const char *str, int flags, int family)
 Parses a string with an IPv4 or IPv6 address and place results into an array. More...
 
int ast_sockaddr_resolve_first_af (struct ast_sockaddr *addr, const char *name, int flag, int family)
 Return the first entry from ast_sockaddr_resolve filtered by address family. More...
 
static void ast_sockaddr_setnull (struct ast_sockaddr *addr)
 Sets address addr to null. More...
 
int ast_sockaddr_split_hostport (char *str, char **host, char **port, int flags)
 Splits a string into its host and port components. More...
 
static char * ast_sockaddr_stringify (const struct ast_sockaddr *addr)
 Wrapper around ast_sockaddr_stringify_fmt() with default format. More...
 
static char * ast_sockaddr_stringify_addr (const struct ast_sockaddr *addr)
 Wrapper around ast_sockaddr_stringify_fmt() to return an address only. More...
 
static char * ast_sockaddr_stringify_addr_remote (const struct ast_sockaddr *addr)
 Wrapper around ast_sockaddr_stringify_fmt() to return an address only. More...
 
char * ast_sockaddr_stringify_fmt (const struct ast_sockaddr *addr, int format)
 Convert a socket address to a string. More...
 
static char * ast_sockaddr_stringify_host (const struct ast_sockaddr *addr)
 Wrapper around ast_sockaddr_stringify_fmt() to return an address only, suitable for a URL (with brackets for IPv6). More...
 
static char * ast_sockaddr_stringify_host_remote (const struct ast_sockaddr *addr)
 Wrapper around ast_sockaddr_stringify_fmt() to return an address only, suitable for a URL (with brackets for IPv6). More...
 
static char * ast_sockaddr_stringify_port (const struct ast_sockaddr *addr)
 Wrapper around ast_sockaddr_stringify_fmt() to return a port only. More...
 
static char * ast_sockaddr_stringify_remote (const struct ast_sockaddr *addr)
 Wrapper around ast_sockaddr_stringify_fmt() with default format. More...
 
const char * ast_transport2str (enum ast_transport transport)
 Returns a string representation of an ast_transport. More...
 
static int _ast_addressfamily_to_sockaddrsize (int af, const char *file, int line, const char *func)
 
void _ast_sockaddr_from_sin (struct ast_sockaddr *addr, const struct sockaddr_in *sin, const char *file, int line, const char *func)
 
int _ast_sockaddr_to_sin (const struct ast_sockaddr *addr, struct sockaddr_in *sin, const char *file, int line, const char *func)
 
#define ast_addressfamily_to_sockaddrsize(family)   _ast_addressfamily_to_sockaddrsize(family, __FILE__, __LINE__, __PRETTY_FUNCTION__)
 Takes an AF_XXX value as input and returns the size of the underlying sockaddr structure if known, or zero if not. More...
 
#define ast_sockaddr_from_sin(addr, sin)   _ast_sockaddr_from_sin(addr,sin, __FILE__, __LINE__, __PRETTY_FUNCTION__)
 Converts a struct sockaddr_in to a struct ast_sockaddr. More...
 
#define ast_sockaddr_from_sockaddr(addr, sa)   ast_sockaddr_copy_sockaddr(addr, sa, ast_addressfamily_to_sockaddrsize(((const struct sockaddr*)(sa))->sa_family))
 Converts a struct sockaddr to a struct ast_sockaddr. More...
 
#define ast_sockaddr_to_sin(addr, sin)   _ast_sockaddr_to_sin(addr,sin, __FILE__, __LINE__, __PRETTY_FUNCTION__)
 Converts a struct ast_sockaddr to a struct sockaddr_in. More...
 

Detailed Description

Network socket handling.

Definition in file netsock2.h.

Macro Definition Documentation

◆ ast_addressfamily_to_sockaddrsize

#define ast_addressfamily_to_sockaddrsize (   family)    _ast_addressfamily_to_sockaddrsize(family, __FILE__, __LINE__, __PRETTY_FUNCTION__)

Takes an AF_XXX value as input and returns the size of the underlying sockaddr structure if known, or zero if not.

Since
13.31.0, 16.8.0, 17.2.0
Parameters
familyThe AF_XXX value to determine the size of
Returns
Size of the applicable struct sockaddr.

Definition at line 791 of file netsock2.h.

◆ AST_SOCKADDR_BUFLEN

#define AST_SOCKADDR_BUFLEN   (45 + 2 + 1 + 5 + 1)

Definition at line 46 of file netsock2.h.

◆ ast_sockaddr_from_sin

#define ast_sockaddr_from_sin (   addr,
  sin 
)    _ast_sockaddr_from_sin(addr,sin, __FILE__, __LINE__, __PRETTY_FUNCTION__)

Converts a struct sockaddr_in to a struct ast_sockaddr.

Since
1.8
Parameters
addr
sinThe sockaddr_in to convert
Returns
an ast_sockaddr structure

Definition at line 778 of file netsock2.h.

◆ ast_sockaddr_from_sockaddr

#define ast_sockaddr_from_sockaddr (   addr,
  sa 
)    ast_sockaddr_copy_sockaddr(addr, sa, ast_addressfamily_to_sockaddrsize(((const struct sockaddr*)(sa))->sa_family))

Converts a struct sockaddr to a struct ast_sockaddr.

Since
13.31.0, 16.8.0, 17.2.0

Note that there is an underlying assumption that sockaddr data is valid, more specifically, if sa_family is set to AF_INET that it's actually a sockaddr_in, and in the case of AF_INET6 a valid sockaddr_in6 structure.

You can check for failure with ast_sockaddr_isnull.

Parameters
[out]addrThe address of the ast_sockaddr to store into
saThe sockaddr structure (sockaddr_in or sockaddr_in6) to convert

Definition at line 819 of file netsock2.h.

◆ ast_sockaddr_port

#define ast_sockaddr_port (   addr)    _ast_sockaddr_port(addr, __FILE__, __LINE__, __PRETTY_FUNCTION__)

Get the port number of a socket address.

Since
1.8
Warning
Do not use this function unless you really know what you are doing. And "I want the port number" is not knowing what you are doing.
Return values
0Address is null
non-zeroThe port number of the ast_sockaddr

Definition at line 517 of file netsock2.h.

◆ ast_sockaddr_set_port

#define ast_sockaddr_set_port (   addr,
  port 
)    _ast_sockaddr_set_port(addr,port,__FILE__,__LINE__,__PRETTY_FUNCTION__)

Sets the port number of a socket address.

Since
1.8
Warning
Do not use this function unless you really know what you are doing. And "I want the port number" is not knowing what you are doing.
Parameters
addrAddress on which to set the port
portThe port you wish to set the address to use

Definition at line 532 of file netsock2.h.

◆ AST_SOCKADDR_STR_ADDR

#define AST_SOCKADDR_STR_ADDR   (1 << 0)

Definition at line 198 of file netsock2.h.

◆ AST_SOCKADDR_STR_ADDR_REMOTE

#define AST_SOCKADDR_STR_ADDR_REMOTE   (AST_SOCKADDR_STR_ADDR | AST_SOCKADDR_STR_REMOTE)

Definition at line 204 of file netsock2.h.

◆ AST_SOCKADDR_STR_BRACKETS

#define AST_SOCKADDR_STR_BRACKETS   (1 << 2)

Definition at line 200 of file netsock2.h.

◆ AST_SOCKADDR_STR_DEFAULT

#define AST_SOCKADDR_STR_DEFAULT   (AST_SOCKADDR_STR_ADDR | AST_SOCKADDR_STR_PORT)

Definition at line 203 of file netsock2.h.

◆ AST_SOCKADDR_STR_DEFAULT_REMOTE

#define AST_SOCKADDR_STR_DEFAULT_REMOTE   (AST_SOCKADDR_STR_DEFAULT | AST_SOCKADDR_STR_REMOTE)

Definition at line 206 of file netsock2.h.

◆ AST_SOCKADDR_STR_FORMAT_MASK

#define AST_SOCKADDR_STR_FORMAT_MASK   (AST_SOCKADDR_STR_ADDR | AST_SOCKADDR_STR_PORT | AST_SOCKADDR_STR_BRACKETS)

Definition at line 207 of file netsock2.h.

◆ AST_SOCKADDR_STR_HOST

#define AST_SOCKADDR_STR_HOST   (AST_SOCKADDR_STR_ADDR | AST_SOCKADDR_STR_BRACKETS)

Definition at line 202 of file netsock2.h.

◆ AST_SOCKADDR_STR_HOST_REMOTE

#define AST_SOCKADDR_STR_HOST_REMOTE   (AST_SOCKADDR_STR_HOST | AST_SOCKADDR_STR_REMOTE)

Definition at line 205 of file netsock2.h.

◆ AST_SOCKADDR_STR_PORT

#define AST_SOCKADDR_STR_PORT   (1 << 1)

Definition at line 199 of file netsock2.h.

◆ AST_SOCKADDR_STR_REMOTE

#define AST_SOCKADDR_STR_REMOTE   (1 << 3)

Definition at line 201 of file netsock2.h.

◆ ast_sockaddr_to_sin

#define ast_sockaddr_to_sin (   addr,
  sin 
)    _ast_sockaddr_to_sin(addr,sin, __FILE__, __LINE__, __PRETTY_FUNCTION__)

Converts a struct ast_sockaddr to a struct sockaddr_in.

These are backward compatibility functions that may be used by subsystems that have not yet been converted to IPv6. They will be removed when all subsystems are IPv6-ready.

Since
1.8
Parameters
addrThe ast_sockaddr to convert
[out]sinThe resulting sockaddr_in struct
Return values
nonzeroSuccess
zeroFailure

Definition at line 765 of file netsock2.h.

◆ V6_WORD

#define V6_WORD (   sin6,
  index 
)    ((uint32_t *)&((sin6)->sin6_addr))[(index)]

Isolate a 32-bit section of an IPv6 address.

An IPv6 address can be divided into 4 32-bit chunks. This gives easy access to one of these chunks.

Parameters
sin6A pointer to a struct sockaddr_in6
indexWhich 32-bit chunk to operate on. Must be in the range 0-3.

Definition at line 77 of file netsock2.h.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum

Values for address families that we support. This is reproduced from socket.h because we do not want users to include that file. Only netsock2.c should ever include socket.h.

Enumerator
AST_AF_UNSPEC 
AST_AF_INET 
AST_AF_INET6 

Definition at line 53 of file netsock2.h.

53 {
54 AST_AF_UNSPEC = AF_UNSPEC,
55 AST_AF_INET = AF_INET,
56 AST_AF_INET6 = AF_INET6,
57};
@ AST_AF_INET6
Definition: netsock2.h:56
@ AST_AF_INET
Definition: netsock2.h:55
@ AST_AF_UNSPEC
Definition: netsock2.h:54

◆ ast_transport

Enumerator
AST_TRANSPORT_UDP 
AST_TRANSPORT_TCP 
AST_TRANSPORT_TLS 
AST_TRANSPORT_WS 
AST_TRANSPORT_WSS 

Definition at line 59 of file netsock2.h.

59 {
61 AST_TRANSPORT_TCP = 1 << 1,
62 AST_TRANSPORT_TLS = 1 << 2,
63 AST_TRANSPORT_WS = 1 << 3,
64 AST_TRANSPORT_WSS = 1 << 4,
65};
@ AST_TRANSPORT_WSS
Definition: netsock2.h:64
@ AST_TRANSPORT_WS
Definition: netsock2.h:63
@ AST_TRANSPORT_UDP
Definition: netsock2.h:60
@ AST_TRANSPORT_TLS
Definition: netsock2.h:62
@ AST_TRANSPORT_TCP
Definition: netsock2.h:61

Function Documentation

◆ _ast_addressfamily_to_sockaddrsize()

static int _ast_addressfamily_to_sockaddrsize ( int  af,
const char *  file,
int  line,
const char *  func 
)
inlinestatic

Definition at line 792 of file netsock2.h.

793{
794 switch (af) {
795 case AF_INET:
796 return sizeof(struct sockaddr_in);
797 case AF_INET6:
798 return sizeof(struct sockaddr_in6);
799 default:
800 ast_log(__LOG_WARNING, file, line, func, "Unknown address family %d encountered.\n", af);
801 return 0;
802 }
803}
#define ast_log
Definition: astobj2.c:42
#define __LOG_WARNING

References __LOG_WARNING, ast_log, and make_ari_stubs::file.

◆ _ast_sockaddr_from_sin()

void _ast_sockaddr_from_sin ( struct ast_sockaddr addr,
const struct sockaddr_in *  sin,
const char *  file,
int  line,
const char *  func 
)

Definition at line 689 of file netsock2.c.

691{
692 memcpy(&addr->ss, sin, sizeof(*sin));
693
694 if (addr->ss.ss_family != AF_INET && DEBUG_ATLEAST(1)) {
695 ast_log(__LOG_DEBUG, file, line, func, "Address family is not AF_INET\n");
696 }
697
698 addr->len = sizeof(*sin);
699}
#define DEBUG_ATLEAST(level)
#define __LOG_DEBUG
struct sockaddr_storage ss
Definition: netsock2.h:98
socklen_t len
Definition: netsock2.h:99

References __LOG_DEBUG, ast_log, DEBUG_ATLEAST, make_ari_stubs::file, ast_sockaddr::len, and ast_sockaddr::ss.

◆ _ast_sockaddr_port()

uint16_t _ast_sockaddr_port ( const struct ast_sockaddr addr,
const char *  file,
int  line,
const char *  func 
)

Definition at line 453 of file netsock2.c.

454{
455 /*
456 * Test addr->len first to be tolerant of an ast_sockaddr_setnull()
457 * addr. In that case addr->len might be the only value initialized.
458 */
459 if (addr->len == sizeof(struct sockaddr_in)
460 && addr->ss.ss_family == AF_INET) {
461 return ntohs(((struct sockaddr_in *)&addr->ss)->sin_port);
462 }
463 if (addr->len == sizeof(struct sockaddr_in6)
464 && addr->ss.ss_family == AF_INET6) {
465 return ntohs(((struct sockaddr_in6 *)&addr->ss)->sin6_port);
466 }
467 if (DEBUG_ATLEAST(1)) {
468 ast_log(__LOG_DEBUG, file, line, func, "Not an IPv4 nor IPv6 address, cannot get port.\n");
469 }
470 return 0;
471}

References __LOG_DEBUG, ast_log, DEBUG_ATLEAST, make_ari_stubs::file, ast_sockaddr::len, and ast_sockaddr::ss.

◆ _ast_sockaddr_set_port()

void _ast_sockaddr_set_port ( struct ast_sockaddr addr,
uint16_t  port,
const char *  file,
int  line,
const char *  func 
)

Definition at line 473 of file netsock2.c.

474{
475 /*
476 * Test addr->len first to be tolerant of an ast_sockaddr_setnull()
477 * addr. In that case addr->len might be the only value initialized.
478 */
479 if (addr->len == sizeof(struct sockaddr_in)
480 && addr->ss.ss_family == AF_INET) {
481 ((struct sockaddr_in *)&addr->ss)->sin_port = htons(port);
482 } else if (addr->len == sizeof(struct sockaddr_in6)
483 && addr->ss.ss_family == AF_INET6) {
484 ((struct sockaddr_in6 *)&addr->ss)->sin6_port = htons(port);
485 } else if (DEBUG_ATLEAST(1)) {
486 ast_log(__LOG_DEBUG, file, line, func,
487 "Not an IPv4 nor IPv6 address, cannot set port.\n");
488 }
489}

References __LOG_DEBUG, ast_log, DEBUG_ATLEAST, make_ari_stubs::file, ast_sockaddr::len, and ast_sockaddr::ss.

◆ _ast_sockaddr_to_sin()

int _ast_sockaddr_to_sin ( const struct ast_sockaddr addr,
struct sockaddr_in *  sin,
const char *  file,
int  line,
const char *  func 
)

Definition at line 668 of file netsock2.c.

670{
671 if (ast_sockaddr_isnull(addr)) {
672 memset(sin, 0, sizeof(*sin));
673 return 1;
674 }
675
676 if (addr->len != sizeof(*sin)) {
677 ast_log(__LOG_ERROR, file, line, func, "Bad address cast to IPv4\n");
678 return 0;
679 }
680
681 if (addr->ss.ss_family != AF_INET && DEBUG_ATLEAST(1)) {
682 ast_log(__LOG_DEBUG, file, line, func, "Address family is not AF_INET\n");
683 }
684
685 *sin = *(struct sockaddr_in *)&addr->ss;
686 return 1;
687}
#define __LOG_ERROR
static int ast_sockaddr_isnull(const struct ast_sockaddr *addr)
Checks if the ast_sockaddr is null. "null" in this sense essentially means uninitialized,...
Definition: netsock2.h:127

References __LOG_DEBUG, __LOG_ERROR, ast_log, ast_sockaddr_isnull(), DEBUG_ATLEAST, make_ari_stubs::file, ast_sockaddr::len, and ast_sockaddr::ss.

◆ ast_accept()

int ast_accept ( int  sockfd,
struct ast_sockaddr addr 
)

Wrapper around accept(2) that uses struct ast_sockaddr.

Since
1.8

For parameter and return information, see the man page for accept(2).

Definition at line 584 of file netsock2.c.

585{
586 addr->len = sizeof(addr->ss);
587 return accept(sockfd, (struct sockaddr *)&addr->ss, &addr->len);
588}

References ast_sockaddr::len, and ast_sockaddr::ss.

Referenced by ast_tcptls_server_root().

◆ ast_bind()

int ast_bind ( int  sockfd,
const struct ast_sockaddr addr 
)

Wrapper around bind(2) that uses struct ast_sockaddr.

Since
1.8

For parameter and return information, see the man page for bind(2).

Definition at line 590 of file netsock2.c.

591{
592 return bind(sockfd, (const struct sockaddr *)&addr->ss, addr->len);
593}

References ast_sockaddr::len, and ast_sockaddr::ss.

Referenced by ast_netsock_bindaddr(), ast_rtp_prop_set(), ast_tcptls_client_create(), ast_tcptls_server_start(), ast_udptl_new_with_bindaddr(), check_srcaddr(), load_module(), and rtp_allocate_transport().

◆ ast_connect()

int ast_connect ( int  sockfd,
const struct ast_sockaddr addr 
)

Wrapper around connect(2) that uses struct ast_sockaddr.

Since
1.8

For parameter and return information, see the man page for connect(2).

Definition at line 595 of file netsock2.c.

596{
597 return connect(sockfd, (const struct sockaddr *)&addr->ss, addr->len);
598}

References ast_sockaddr::len, and ast_sockaddr::ss.

Referenced by ast_audiosocket_connect(), ast_ouraddrfor(), launch_netscript(), socket_connect(), and stun_monitor_request().

◆ ast_getsockname()

int ast_getsockname ( int  sockfd,
struct ast_sockaddr addr 
)

Wrapper around getsockname(2) that uses struct ast_sockaddr.

Since
1.8

For parameter and return information, see the man page for getsockname(2).

Definition at line 600 of file netsock2.c.

601{
602 addr->len = sizeof(addr->ss);
603 return getsockname(sockfd, (struct sockaddr *)&addr->ss, &addr->len);
604}

References ast_sockaddr::len, and ast_sockaddr::ss.

Referenced by ast_ouraddrfor(), ast_set_qos(), and ast_websocket_uri_cb().

◆ ast_recvfrom()

ssize_t ast_recvfrom ( int  sockfd,
void *  buf,
size_t  len,
int  flags,
struct ast_sockaddr src_addr 
)

Wrapper around recvfrom(2) that uses struct ast_sockaddr.

Since
1.8

For parameter and return information, see the man page for recvfrom(2).

Definition at line 606 of file netsock2.c.

608{
609 src_addr->len = sizeof(src_addr->ss);
610 return recvfrom(sockfd, buf, len, flags,
611 (struct sockaddr *)&src_addr->ss, &src_addr->len);
612}
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)

References buf, len(), ast_sockaddr::len, and ast_sockaddr::ss.

Referenced by __rtp_recvfrom(), ast_udptl_read(), and socket_read().

◆ ast_sendto()

ssize_t ast_sendto ( int  sockfd,
const void *  buf,
size_t  len,
int  flags,
const struct ast_sockaddr dest_addr 
)

Wrapper around sendto(2) that uses ast_sockaddr.

Since
1.8

For parameter and return information, see the man page for sendto(2)

Definition at line 614 of file netsock2.c.

616{
617 return sendto(sockfd, buf, len, flags,
618 (const struct sockaddr *)&dest_addr->ss, dest_addr->len);
619}

References buf, len(), ast_sockaddr::len, and ast_sockaddr::ss.

Referenced by __rtp_sendto(), ast_statsd_log_string(), ast_udptl_write(), dundi_xmit(), hep_queue_cb(), multicast_send_control_packet(), raw_hangup(), rtp_raw_write(), send_apathetic_reply(), send_packet(), and transmit_trunk().

◆ ast_set_qos()

int ast_set_qos ( int  sockfd,
int  tos,
int  cos,
const char *  desc 
)

Set type of service.

Since
1.8

Set ToS ("Type of Service for IPv4 and "Traffic Class for IPv6) and CoS (Linux's SO_PRIORITY)

Parameters
sockfdFile descriptor for socket on which to set the parameters
tosThe type of service for the socket
cosThe cost of service for the socket
descA text description of the socket in question.
Return values
0Success
-1Error, with errno set to an appropriate value

Definition at line 621 of file netsock2.c.

622{
623 int res = 0;
624 int set_tos;
625 int set_tclass;
626 struct ast_sockaddr addr;
627
628 /* If the sock address is IPv6, the TCLASS field must be set. */
629 set_tclass = !ast_getsockname(sockfd, &addr) && ast_sockaddr_is_ipv6(&addr) ? 1 : 0;
630
631 /* If the sock address is IPv4 or (IPv6 set to any address [::]) set TOS bits */
632 set_tos = (!set_tclass || (set_tclass && ast_sockaddr_is_any(&addr))) ? 1 : 0;
633
634 if (set_tos) {
635 if ((res = setsockopt(sockfd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)))) {
636 ast_log(LOG_WARNING, "Unable to set %s DSCP TOS value to %d (may be you have no "
637 "root privileges): %s\n", desc, tos, strerror(errno));
638 } else if (tos) {
639 ast_verb(2, "Using %s TOS bits %d\n", desc, tos);
640 }
641 }
642
643#if defined(IPV6_TCLASS) && defined(IPPROTO_IPV6)
644 if (set_tclass) {
645 if (!ast_getsockname(sockfd, &addr) && ast_sockaddr_is_ipv6(&addr)) {
646 if ((res = setsockopt(sockfd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)))) {
647 ast_log(LOG_WARNING, "Unable to set %s DSCP TCLASS field to %d (may be you have no "
648 "root privileges): %s\n", desc, tos, strerror(errno));
649 } else if (tos) {
650 ast_verb(2, "Using %s TOS bits %d in TCLASS field.\n", desc, tos);
651 }
652 }
653 }
654#endif
655
656#ifdef linux
657 if (setsockopt(sockfd, SOL_SOCKET, SO_PRIORITY, &cos, sizeof(cos))) {
658 ast_log(LOG_WARNING, "Unable to set %s CoS to %d: %s\n", desc, cos,
659 strerror(errno));
660 } else if (cos) {
661 ast_verb(2, "Using %s CoS mark %d\n", desc, cos);
662 }
663#endif
664
665 return res;
666}
static const char desc[]
Definition: cdr_radius.c:84
unsigned int tos
Definition: chan_iax2.c:355
unsigned int cos
Definition: chan_iax2.c:356
#define ast_verb(level,...)
#define LOG_WARNING
int errno
int ast_getsockname(int sockfd, struct ast_sockaddr *addr)
Wrapper around getsockname(2) that uses struct ast_sockaddr.
Definition: netsock2.c:600
int ast_sockaddr_is_ipv6(const struct ast_sockaddr *addr)
Determine if this is an IPv6 address.
Definition: netsock2.c:524
int ast_sockaddr_is_any(const struct ast_sockaddr *addr)
Determine if the address type is unspecified, or "any" address.
Definition: netsock2.c:534
Socket address structure.
Definition: netsock2.h:97

References ast_getsockname(), ast_log, ast_sockaddr_is_any(), ast_sockaddr_is_ipv6(), ast_verb, cos, desc, errno, LOG_WARNING, and tos.

Referenced by ast_netsock_bindaddr(), ast_netsock_set_qos(), ast_rtp_qos_set(), ast_udptl_setqos(), load_module(), and reload_config().

◆ ast_sockaddr_apply_netmask()

int ast_sockaddr_apply_netmask ( const struct ast_sockaddr addr,
const struct ast_sockaddr netmask,
struct ast_sockaddr result 
)

Apply a netmask to an address and store the result in a separate structure.

When dealing with IPv6 addresses, one cannot apply a netmask with a simple logical AND operation. Futhermore, the incoming address may be an IPv4 address and needs to be mapped properly before attempting to apply a rule.

Parameters
addrThe IP address to apply the mask to.
netmaskThe netmask configured in the host access rule.
resultThe resultant address after applying the netmask to the given address
Return values
0Successfully applied netmask
-1Failed to apply netmask

Definition at line 357 of file netsock2.c.

359{
360 int res = 0;
361
362 if (ast_sockaddr_is_ipv4(addr)) {
363 struct sockaddr_in result4 = { 0, };
364 struct sockaddr_in *addr4 = (struct sockaddr_in *) &addr->ss;
365 struct sockaddr_in *mask4 = (struct sockaddr_in *) &netmask->ss;
366 result4.sin_family = AF_INET;
367 result4.sin_addr.s_addr = addr4->sin_addr.s_addr & mask4->sin_addr.s_addr;
368 ast_sockaddr_from_sin(result, &result4);
369 } else if (ast_sockaddr_is_ipv6(addr)) {
370 struct sockaddr_in6 result6 = { 0, };
371 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &addr->ss;
372 struct sockaddr_in6 *mask6 = (struct sockaddr_in6 *) &netmask->ss;
373 int i;
374 result6.sin6_family = AF_INET6;
375 for (i = 0; i < 4; ++i) {
376 V6_WORD(&result6, i) = V6_WORD(addr6, i) & V6_WORD(mask6, i);
377 }
378 memcpy(&result->ss, &result6, sizeof(result6));
379 result->len = sizeof(result6);
380 } else {
381 /* Unsupported address scheme */
382 res = -1;
383 }
384
385 return res;
386}
static PGresult * result
Definition: cel_pgsql.c:84
int ast_sockaddr_is_ipv4(const struct ast_sockaddr *addr)
Determine if the address is an IPv4 address.
Definition: netsock2.c:497
#define V6_WORD(sin6, index)
Isolate a 32-bit section of an IPv6 address.
Definition: netsock2.h:77
#define ast_sockaddr_from_sin(addr, sin)
Converts a struct sockaddr_in to a struct ast_sockaddr.
Definition: netsock2.h:778

References ast_sockaddr_from_sin, ast_sockaddr_is_ipv4(), ast_sockaddr_is_ipv6(), result, ast_sockaddr::ss, and V6_WORD.

Referenced by addr_range_match_address_cb(), append_ha_core(), ast_apply_ha(), and authenticate_reply().

◆ ast_sockaddr_cidr_bits()

int ast_sockaddr_cidr_bits ( const struct ast_sockaddr sa)

Count the 1 bits in a netmask.

Since
12.4
Returns
number of 1 bits

Definition at line 130 of file netsock2.c.

131{
132 struct ast_sockaddr sa_ipv4;
133 const struct ast_sockaddr *sa_tmp;
134 int bits = 0;
135 int bytes;
136 int i;
137 int j;
138 char *addr;
139
140 if (ast_sockaddr_isnull(sa)) {
141 return 0;
142 }
143
144 if (ast_sockaddr_ipv4_mapped(sa, &sa_ipv4)) {
145 sa_tmp = &sa_ipv4;
146 } else {
147 sa_tmp = sa;
148 }
149
150 bytes = sa_tmp->len;
151 addr = ((struct sockaddr *)&sa_tmp->ss)->sa_data;
152
153 for (i = 0; i < bytes ; ++i) {
154 for (j = 0; j < 8; ++j) {
155 if ((addr[i] >> j) & 1) {
156 bits++;
157 }
158 }
159 }
160
161 return bits;
162}
int ast_sockaddr_ipv4_mapped(const struct ast_sockaddr *addr, struct ast_sockaddr *ast_mapped)
Convert an IPv4-mapped IPv6 address into an IPv4 address.
Definition: netsock2.c:37

References ast_sockaddr_ipv4_mapped(), ast_sockaddr_isnull(), ast_sockaddr::len, and ast_sockaddr::ss.

Referenced by ast_ha_join_cidr(), and cli_print_body().

◆ ast_sockaddr_cmp()

int ast_sockaddr_cmp ( const struct ast_sockaddr a,
const struct ast_sockaddr b 
)

Compares two ast_sockaddr structures.

Since
1.8
Return values
-1a is lexicographically smaller than b
0a is equal to b
1b is lexicographically smaller than a

Definition at line 388 of file netsock2.c.

389{
390 const struct ast_sockaddr *a_tmp, *b_tmp;
391 struct ast_sockaddr ipv4_mapped;
392
393 a_tmp = a;
394 b_tmp = b;
395
396 if (a_tmp->len != b_tmp->len) {
397 if (ast_sockaddr_ipv4_mapped(a, &ipv4_mapped)) {
398 a_tmp = &ipv4_mapped;
399 } else if (ast_sockaddr_ipv4_mapped(b, &ipv4_mapped)) {
400 b_tmp = &ipv4_mapped;
401 }
402 }
403
404 if (a_tmp->len < b_tmp->len) {
405 return -1;
406 } else if (a_tmp->len > b_tmp->len) {
407 return 1;
408 }
409
410 return memcmp(&a_tmp->ss, &b_tmp->ss, a_tmp->len);
411}
static struct test_val b
static struct test_val a

References a, ast_sockaddr_ipv4_mapped(), b, ast_sockaddr::len, and ast_sockaddr::ss.

Referenced by __rtp_recvfrom(), ast_netsock_find(), ast_rtcp_interpret(), ast_rtp_instance_get_and_cmp_local_address(), ast_rtp_instance_get_and_cmp_requested_target_address(), ast_rtp_read(), ast_rtp_remote_address_set(), ast_sockaddr_pj_sockaddr_cmp(), ast_tcptls_client_create(), ast_tcptls_server_start(), AST_TEST_DEFINE(), ast_udptl_read(), dnsmgr_refresh(), find_tpeer(), find_transaction(), handle_command_response(), has_state_changed(), iax2_ack_registry(), iax2_getpeername(), iax2_getpeertrunk(), match(), registry_rerequest(), rtcp_debug_test_addr(), rtp_debug_test_addr(), socket_process_helper(), udptl_debug_test_addr(), and update_registry().

◆ ast_sockaddr_cmp_addr()

int ast_sockaddr_cmp_addr ( const struct ast_sockaddr a,
const struct ast_sockaddr b 
)

Compares the addresses of two ast_sockaddr structures.

Since
1.8
Return values
-1a is lexicographically smaller than b
0a is equal to b
1b is lexicographically smaller than a

Definition at line 413 of file netsock2.c.

414{
415 const struct ast_sockaddr *a_tmp, *b_tmp;
416 struct ast_sockaddr ipv4_mapped;
417 const struct in_addr *ip4a, *ip4b;
418 const struct in6_addr *ip6a, *ip6b;
419 int ret = -1;
420
421 a_tmp = a;
422 b_tmp = b;
423
424 if (a_tmp->len != b_tmp->len) {
425 if (ast_sockaddr_ipv4_mapped(a, &ipv4_mapped)) {
426 a_tmp = &ipv4_mapped;
427 } else if (ast_sockaddr_ipv4_mapped(b, &ipv4_mapped)) {
428 b_tmp = &ipv4_mapped;
429 }
430 }
431
432 if (a->len < b->len) {
433 ret = -1;
434 } else if (a->len > b->len) {
435 ret = 1;
436 }
437
438 switch (a_tmp->ss.ss_family) {
439 case AF_INET:
440 ip4a = &((const struct sockaddr_in*)&a_tmp->ss)->sin_addr;
441 ip4b = &((const struct sockaddr_in*)&b_tmp->ss)->sin_addr;
442 ret = memcmp(ip4a, ip4b, sizeof(*ip4a));
443 break;
444 case AF_INET6:
445 ip6a = &((const struct sockaddr_in6*)&a_tmp->ss)->sin6_addr;
446 ip6b = &((const struct sockaddr_in6*)&b_tmp->ss)->sin6_addr;
447 ret = memcmp(ip6a, ip6b, sizeof(*ip6a));
448 break;
449 }
450 return ret;
451}

References a, ast_sockaddr_ipv4_mapped(), b, ast_sockaddr::len, and ast_sockaddr::ss.

Referenced by addr_range_cmp_cb(), addr_range_match_address_cb(), ast_apply_ha(), AST_TEST_DEFINE(), authenticate_reply(), iax_outputframe(), peercnt_cmp_cb(), realtime_peer(), realtime_user(), resolve_match_or_first(), rtcp_debug_test_addr(), rtp_debug_test_addr(), socket_read(), and udptl_debug_test_addr().

◆ ast_sockaddr_copy()

static void ast_sockaddr_copy ( struct ast_sockaddr dst,
const struct ast_sockaddr src 
)
inlinestatic

Copies the data from one ast_sockaddr to another.

Since
1.8
Parameters
dstThe destination ast_sockaddr
srcThe source ast_sockaddr

Definition at line 167 of file netsock2.h.

169{
170 memcpy(dst, src, src->len);
171 dst->len = src->len;
172};

References ast_sockaddr::len.

Referenced by __ast_http_load(), __find_callno(), __init_manager(), __rtp_recvfrom(), app_exec(), apply_peer(), ast_copy_ha(), ast_find_ourip(), ast_netsock_bindaddr(), ast_rtcp_interpret(), ast_rtcp_read(), ast_rtcp_write(), ast_rtp_instance_get_and_cmp_local_address(), ast_rtp_instance_get_and_cmp_requested_target_address(), ast_rtp_instance_get_incoming_source_address(), ast_rtp_instance_get_local_address(), ast_rtp_instance_get_requested_target_address(), ast_rtp_instance_new(), ast_rtp_instance_set_local_address(), ast_rtp_instance_set_requested_target_address(), ast_rtp_new(), ast_rtp_prop_set(), ast_rtp_read(), ast_rtp_remote_address_set(), ast_sockaddr_resolve_first_af(), ast_tcptls_client_create(), ast_tcptls_server_root(), ast_tcptls_server_start(), ast_udptl_get_peer(), ast_udptl_get_us(), ast_udptl_new_with_bindaddr(), ast_udptl_read(), ast_udptl_set_peer(), ast_websocket_uri_cb(), auth_manager_http_callback(), auth_mxml_http_callback(), auth_rawman_http_callback(), authenticate_reply(), build_mansession(), build_peer(), configure_local_rtp(), create_addr(), dnsmgr_refresh(), dundi_reject(), find_tpeer(), find_transaction(), handle_cli_iax2_set_debug(), handle_cli_iax2_show_callno_limits(), handle_cli_udptl_set_debug(), handle_command_response(), http_server_create(), iax2_ack_registry(), iax2_provision(), manager_http_callback(), mxml_http_callback(), peercnt_add(), peercnt_modify(), peercnt_remove(), peercnt_remove_by_addr(), populate_addr(), rawman_http_callback(), resolve_match_or_first(), rtp_allocate_transport(), rtp_instance_set_incoming_source_address_nolock(), rtp_reload(), rtp_transport_wide_cc_feedback_produce(), sched_delay_remove(), session_do(), set_peercnt_limit(), socket_process_helper(), socket_read(), try_transfer(), unicast_rtp_request(), update_registry(), websocket_client_args_create(), and websocket_client_connect().

◆ ast_sockaddr_copy_sockaddr()

static void ast_sockaddr_copy_sockaddr ( struct ast_sockaddr dst,
struct sockaddr *  src,
socklen_t  len 
)
inlinestatic

Copies the data from a sockaddr to an ast_sockaddr.

Parameters
dstThe destination ast_sockaddr
srcThe source sockaddr
lenLength of the value stored in sockaddr

Definition at line 151 of file netsock2.h.

153{
154 memcpy(dst, src, len);
155 dst->len = len;
156}

References len(), and ast_sockaddr::len.

Referenced by curl_open_socket_cb().

◆ ast_sockaddr_hash()

int ast_sockaddr_hash ( const struct ast_sockaddr addr)

Computes a hash value from the address. The port is ignored.

Since
1.8
Return values
0Unknown address family
otherA 32-bit hash derived from the address

Definition at line 548 of file netsock2.c.

549{
550 /*
551 * For IPv4, return the IP address as-is. For IPv6, return the last 32
552 * bits.
553 */
554 switch (addr->ss.ss_family) {
555 case AF_INET:
556 return ((const struct sockaddr_in *)&addr->ss)->sin_addr.s_addr;
557 case AF_INET6:
558 return ((uint32_t *)&((const struct sockaddr_in6 *)&addr->ss)->sin6_addr)[3];
559 default:
560 ast_log(LOG_ERROR, "Unknown address family '%d'.\n",
561 addr->ss.ss_family);
562 return 0;
563 }
564}
#define LOG_ERROR

References ast_log, LOG_ERROR, and ast_sockaddr::ss.

Referenced by addr_range_hash_cb(), and peercnt_hash_cb().

◆ ast_sockaddr_ipv4()

uint32_t ast_sockaddr_ipv4 ( const struct ast_sockaddr addr)

Get an IPv4 address of an ast_sockaddr.

Since
1.8
Warning
You should rarely need this function. Only use if you know what you're doing.
Returns
IPv4 address in network byte order

Definition at line 491 of file netsock2.c.

492{
493 const struct sockaddr_in *sin = (struct sockaddr_in *)&addr->ss;
494 return ntohl(sin->sin_addr.s_addr);
495}

References ast_sockaddr::ss.

Referenced by ast_sockaddr_is_ipv4_multicast(), iax_template_parse(), and multicast_send_control_packet().

◆ ast_sockaddr_ipv4_mapped()

int ast_sockaddr_ipv4_mapped ( const struct ast_sockaddr addr,
struct ast_sockaddr ast_mapped 
)

Convert an IPv4-mapped IPv6 address into an IPv4 address.

Warning
You should rarely need this function. Only call this if you know what you're doing.
Parameters
addrThe IPv4-mapped address to convert
ast_mappedThe resulting IPv4 address
Return values
0Unable to make the conversion
1Successful conversion

Definition at line 37 of file netsock2.c.

38{
39 const struct sockaddr_in6 *sin6;
40 struct sockaddr_in sin4;
41
42 if (!ast_sockaddr_is_ipv6(addr)) {
43 return 0;
44 }
45
46 if (!ast_sockaddr_is_ipv4_mapped(addr)) {
47 return 0;
48 }
49
50 sin6 = (const struct sockaddr_in6*)&addr->ss;
51
52 memset(&sin4, 0, sizeof(sin4));
53 sin4.sin_family = AF_INET;
54 sin4.sin_port = sin6->sin6_port;
55 sin4.sin_addr.s_addr = ((uint32_t *)&sin6->sin6_addr)[3];
56
57 ast_sockaddr_from_sin(ast_mapped, &sin4);
58
59 return 1;
60}
int ast_sockaddr_is_ipv4_mapped(const struct ast_sockaddr *addr)
Determine if this is an IPv4-mapped IPv6 address.
Definition: netsock2.c:507

References ast_sockaddr_from_sin, ast_sockaddr_is_ipv4_mapped(), ast_sockaddr_is_ipv6(), and ast_sockaddr::ss.

Referenced by append_ha_core(), ast_apply_ha(), ast_rtcp_read(), ast_rtp_read(), ast_sockaddr_cidr_bits(), ast_sockaddr_cmp(), ast_sockaddr_cmp_addr(), and ast_sockaddr_stringify_fmt().

◆ ast_sockaddr_is_any()

int ast_sockaddr_is_any ( const struct ast_sockaddr addr)

Determine if the address type is unspecified, or "any" address.

Since
1.8

For IPv4, this would be the address 0.0.0.0, and for IPv6, this would be the address ::. The port number is ignored.

Return values
1This is an "any" address
0This is not an "any" address

Definition at line 534 of file netsock2.c.

535{
536 union {
537 struct sockaddr_storage ss;
538 struct sockaddr_in sin;
539 struct sockaddr_in6 sin6;
540 } tmp_addr = {
541 .ss = addr->ss,
542 };
543
544 return (ast_sockaddr_is_ipv4(addr) && (tmp_addr.sin.sin_addr.s_addr == INADDR_ANY)) ||
545 (ast_sockaddr_is_ipv6(addr) && IN6_IS_ADDR_UNSPECIFIED(&tmp_addr.sin6.sin6_addr));
546}

References ast_sockaddr_is_ipv4(), ast_sockaddr_is_ipv6(), and ast_sockaddr::ss.

Referenced by ast_find_ourip(), ast_set_qos(), ast_tcptls_client_create(), and set_session_media_remotely_held().

◆ ast_sockaddr_is_ipv4()

int ast_sockaddr_is_ipv4 ( const struct ast_sockaddr addr)

Determine if the address is an IPv4 address.

Since
1.8
Warning
You should rarely need this function. Only use if you know what you're doing.
Return values
1This is an IPv4 address
0This is an IPv6 or IPv4-mapped IPv6 address

Definition at line 497 of file netsock2.c.

498{
499 /*
500 * Test addr->len first to be tolerant of an ast_sockaddr_setnull()
501 * addr. In that case addr->len might be the only value initialized.
502 */
503 return addr->len == sizeof(struct sockaddr_in)
504 && addr->ss.ss_family == AF_INET;
505}

References ast_sockaddr::len, and ast_sockaddr::ss.

Referenced by append_ha_core(), ast_apply_ha(), ast_json_ipaddr(), ast_rtcp_read(), ast_rtp_prop_set(), ast_rtp_read(), ast_sockaddr_apply_netmask(), ast_sockaddr_is_any(), dump_addr(), dundi_xmit(), hep_queue_cb(), jingle_interpret_ice_udp_transport(), load_module(), negotiate_incoming_sdp_stream(), and rtp_allocate_transport().

◆ ast_sockaddr_is_ipv4_mapped()

int ast_sockaddr_is_ipv4_mapped ( const struct ast_sockaddr addr)

Determine if this is an IPv4-mapped IPv6 address.

Since
1.8
Warning
You should rarely need this function. Only use if you know what you're doing.
Return values
1This is an IPv4-mapped IPv6 address.
0This is not an IPv4-mapped IPv6 address.

Definition at line 507 of file netsock2.c.

508{
509 const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&addr->ss;
510 return addr->len && IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr);
511}

References ast_sockaddr::len, and ast_sockaddr::ss.

Referenced by ast_apply_ha(), ast_json_ipaddr(), ast_sockaddr_ipv4_mapped(), build_peer(), and dump_addr().

◆ ast_sockaddr_is_ipv4_multicast()

int ast_sockaddr_is_ipv4_multicast ( const struct ast_sockaddr addr)

Determine if an IPv4 address is a multicast address.

Since
10.0
Parameters
addrthe address to check

This function checks if an address is in the 224.0.0.0/4 network block.

Returns
non-zero if this is a multicast address

Definition at line 513 of file netsock2.c.

514{
515 return ((ast_sockaddr_ipv4(addr) & 0xf0000000) == 0xe0000000);
516}
uint32_t ast_sockaddr_ipv4(const struct ast_sockaddr *addr)
Get an IPv4 address of an ast_sockaddr.
Definition: netsock2.c:491

References ast_sockaddr_ipv4().

◆ ast_sockaddr_is_ipv6()

int ast_sockaddr_is_ipv6 ( const struct ast_sockaddr addr)

Determine if this is an IPv6 address.

Since
1.8
Warning
You should rarely need this function. Only use if you know what you're doing.
Return values
1This is an IPv6 or IPv4-mapped IPv6 address.
0This is an IPv4 address.

Definition at line 524 of file netsock2.c.

525{
526 /*
527 * Test addr->len first to be tolerant of an ast_sockaddr_setnull()
528 * addr. In that case addr->len might be the only value initialized.
529 */
530 return addr->len == sizeof(struct sockaddr_in6)
531 && addr->ss.ss_family == AF_INET6;
532}

References ast_sockaddr::len, and ast_sockaddr::ss.

Referenced by ast_apply_ha(), ast_netsock_bindaddr(), ast_ouraddrfor(), ast_rtp_prop_set(), ast_set_qos(), ast_sockaddr_apply_netmask(), ast_sockaddr_ipv4_mapped(), ast_sockaddr_is_any(), ast_sockaddr_is_ipv6_link_local(), ast_tcptls_client_create(), ast_tcptls_server_start(), ast_udptl_new_with_bindaddr(), build_peer(), hepv3_data_alloc(), load_module(), multicast_send_control_packet(), negotiate_incoming_sdp_stream(), reload_config(), rtp_allocate_transport(), and set_host_ipaddr().

◆ ast_sockaddr_is_ipv6_link_local()

int ast_sockaddr_is_ipv6_link_local ( const struct ast_sockaddr addr)

Determine if this is a link-local IPv6 address.

Since
1.8
Warning
You should rarely need this function. Only use if you know what you're doing.
Return values
1This is a link-local IPv6 address.
0This is link-local IPv6 address.

Definition at line 518 of file netsock2.c.

519{
520 const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&addr->ss;
521 return ast_sockaddr_is_ipv6(addr) && IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr);
522}

References ast_sockaddr_is_ipv6(), and ast_sockaddr::ss.

Referenced by ast_sockaddr_stringify_fmt().

◆ ast_sockaddr_isnull()

static int ast_sockaddr_isnull ( const struct ast_sockaddr addr)
inlinestatic

Checks if the ast_sockaddr is null. "null" in this sense essentially means uninitialized, or having a 0 length.

Since
1.8
Parameters
addrPointer to the ast_sockaddr we wish to check
Return values
1addr is null
0addr is non-null.

Definition at line 127 of file netsock2.h.

128{
129 return !addr || addr->len == 0;
130}

References ast_sockaddr::len.

Referenced by __ast_http_load(), __iax2_do_register_s(), __iax2_show_peers(), __init_manager(), __rtp_recvfrom(), _ast_sockaddr_to_sin(), acf_channel_read(), add_ice_to_stream(), append_transaction(), apply_peer(), ast_rtcp_calculate_sr_rr_statistics(), ast_rtcp_generate_nack(), ast_rtcp_generate_report(), ast_rtcp_generate_sdes(), ast_rtp_dtmf_begin(), ast_rtp_dtmf_continuation(), ast_rtp_dtmf_end_with_duration(), ast_rtp_interpret(), ast_rtp_read(), ast_rtp_remote_address_set(), ast_rtp_sendcng(), ast_rtp_write(), ast_sip_rewrite_uri_to_local(), ast_sockaddr_cidr_bits(), ast_sockaddr_stringify_fmt(), ast_tcptls_client_create(), ast_tcptls_server_start(), ast_udptl_read(), ast_udptl_write(), authenticate_reply(), bridge_p2p_rtp_write(), build_peer(), check_for_rtp_changes(), create_addr(), create_outgoing_sdp_stream(), create_transaction(), do_monitor(), dundi_show_peer(), dundi_show_peers(), dundi_sockaddr_port(), dundi_sockaddr_stringify_host(), function_iaxpeer(), handle_cli_iax2_show_registry(), iax2_ack_registry(), iax2_devicestate(), iax2_do_register(), iax2_poke_peer(), iax2_prov_app(), iax_outputframe(), jingle_interpret_ice_udp_transport(), load_module(), manager_iax2_show_registry(), multicast_send_control_packet(), ooh323_indicate(), ooh323_set_rtp_peer(), peercnt_hash_cb(), process_nat(), realtime_peer(), realtime_update_peer(), realtime_user(), rtcp_debug_test_addr(), rtp_check_timeout(), rtp_debug_test_addr(), rtp_raw_write(), rtp_write_rtcp_fir(), rtp_write_rtcp_psfb(), send_keepalive(), set_caps(), set_config(), set_session_media_remotely_held(), socket_process_helper(), try_transfer(), udptl_debug_test_addr(), and update_registry().

◆ ast_sockaddr_parse()

int ast_sockaddr_parse ( struct ast_sockaddr addr,
const char *  str,
int  flags 
)

Parse an IPv4 or IPv6 address string.

Since
1.8

Parses a string containing an IPv4 or IPv6 address followed by an optional port (separated by a colon) into a struct ast_sockaddr. The allowed formats are the following:

a.b.c.d a.b.c.d:port a:b:c:...:d [a:b:c:...:d]

Host names are NOT allowed.

Parameters
[out]addrThe resulting ast_sockaddr. This MAY be NULL from functions that are performing validity checks only, e.g. ast_parse_arg().
strThe string to parse
flagsIf set to zero, a port MAY be present. If set to PARSE_PORT_IGNORE, a port MAY be present but will be ignored. If set to PARSE_PORT_REQUIRE, a port MUST be present. If set to PARSE_PORT_FORBID, a port MUST NOT be present.
Return values
1Success
0Failure

Definition at line 230 of file netsock2.c.

231{
232 struct addrinfo hints;
233 struct addrinfo *res;
234 char *s;
235 char *host;
236 char *port;
237 int e;
238
239 s = ast_strdupa(str);
240 if (!ast_sockaddr_split_hostport(s, &host, &port, flags)) {
241 return 0;
242 }
243
244 memset(&hints, 0, sizeof(hints));
245 /* Hint to get only one entry from getaddrinfo */
246 hints.ai_socktype = SOCK_DGRAM;
247
248#ifdef AI_NUMERICSERV
249 hints.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;
250#else
251 hints.ai_flags = AI_NUMERICHOST;
252#endif
253 if ((e = getaddrinfo(host, port, &hints, &res))) {
254 if (e != EAI_NONAME) { /* if this was just a host name rather than a ip address, don't print error */
255 ast_log(LOG_ERROR, "getaddrinfo(\"%s\", \"%s\", ...): %s\n",
256 host, S_OR(port, "(null)"), gai_strerror(e));
257 }
258 return 0;
259 }
260
261 /*
262 * I don't see how this could be possible since we're not resolving host
263 * names. But let's be careful...
264 */
265 if (res->ai_next != NULL) {
266 ast_log(LOG_WARNING, "getaddrinfo() returned multiple "
267 "addresses. Ignoring all but the first.\n");
268 }
269
270 if (addr) {
271 addr->len = res->ai_addrlen;
272 memcpy(&addr->ss, res->ai_addr, addr->len);
273 }
274
275 freeaddrinfo(res);
276
277 return 1;
278}
const char * str
Definition: app_jack.c:147
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
int ast_sockaddr_split_hostport(char *str, char **host, char **port, int flags)
Splits a string into its host and port components.
Definition: netsock2.c:164
#define NULL
Definition: resample.c:96
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one.
Definition: strings.h:80

References ast_log, ast_sockaddr_split_hostport(), ast_strdupa, ast_sockaddr::len, LOG_ERROR, LOG_WARNING, NULL, S_OR, ast_sockaddr::ss, and str.

Referenced by __init_manager(), append_ha_core(), apply_acl(), apply_endpoint_acl(), ast_netsock_bind(), ast_parse_arg(), ast_sip_get_transport_name(), ast_sip_rewrite_uri_to_local(), AST_TEST_DEFINE(), build_peer(), change_outgoing_sdp_stream_media_address(), corosync_node_alloc(), create_rtp(), evt_gen_auth_method_not_allowed(), evt_gen_chal_resp_failed(), evt_gen_chal_sent(), evt_gen_failed_acl(), evt_gen_inval_acct_id(), evt_gen_inval_password(), evt_gen_inval_transport(), evt_gen_load_avg(), evt_gen_mem_limit(), evt_gen_req_bad_format(), evt_gen_req_no_support(), evt_gen_req_not_allowed(), evt_gen_session_limit(), evt_gen_successful_auth(), evt_gen_unexpected_addr(), internal_dnsmgr_lookup(), ip_identify(), ip_identify_match_handler(), jingle_alloc(), jingle_enable_video(), jingle_interpret_google_transport(), jingle_interpret_ice_udp_transport(), load_module(), logging_on_rx_msg(), logging_on_tx_msg(), media_address_handler(), multicast_rtp_request(), peer_set_srcaddr(), pjsip_enable_logger_host(), pjsip_log_test_filter(), process_ice_attributes(), process_nat(), realtime_peer(), reg_source_db(), rtcp_do_debug_ip(), rtcp_message_handler(), rtp_do_debug_ip(), security_event_populate(), session_outgoing_nat_hook(), set_config(), set_loc_src(), t38_initialize_session(), test_init_rtp_instances(), unicast_rtp_request(), and validate_location_source().

◆ ast_sockaddr_resolve()

int ast_sockaddr_resolve ( struct ast_sockaddr **  addrs,
const char *  str,
int  flags,
int  family 
)

Parses a string with an IPv4 or IPv6 address and place results into an array.

Since
1.8

Parses a string containing a host name or an IPv4 or IPv6 address followed by an optional port (separated by a colon). The result is returned into a array of struct ast_sockaddr. Allowed formats for str are the following:

hostname:port host.example.com:port a.b.c.d a.b.c.d:port a:b:c:...:d [a:b:c:...:d]

Parameters
[out]addrsThe resulting array of ast_sockaddrs
strThe string to parse
flagsIf set to zero, a port MAY be present. If set to PARSE_PORT_IGNORE, a port MAY be present but will be ignored. If set to PARSE_PORT_REQUIRE, a port MUST be present. If set to PARSE_PORT_FORBID, a port MUST NOT be present.
familyOnly addresses of the given family will be returned. Use 0 or AST_AF_UNSPEC to get addresses of all families.
Return values
0Failure
non-zeroThe number of elements in addrs array.

Definition at line 280 of file netsock2.c.

282{
283 struct addrinfo hints, *res, *ai;
284 char *s, *host, *port;
285 int e, i, res_cnt;
286
287 if (!str) {
288 *addrs = NULL;
289 return 0;
290 }
291
292 s = ast_strdupa(str);
293 if (!ast_sockaddr_split_hostport(s, &host, &port, flags)) {
294 *addrs = NULL;
295 return 0;
296 }
297
298 memset(&hints, 0, sizeof(hints));
299 hints.ai_family = family;
300 hints.ai_socktype = SOCK_DGRAM;
301
302 if ((e = getaddrinfo(host, port, &hints, &res))) {
303 ast_log(LOG_ERROR, "getaddrinfo(\"%s\", \"%s\", ...): %s\n",
304 host, S_OR(port, "(null)"), gai_strerror(e));
305 *addrs = NULL;
306 return 0;
307 }
308
309 res_cnt = 0;
310 for (ai = res; ai; ai = ai->ai_next) {
311 res_cnt++;
312 }
313
314 if (res_cnt == 0) {
315 *addrs = NULL;
316 goto cleanup;
317 }
318
319 if ((*addrs = ast_malloc(res_cnt * sizeof(struct ast_sockaddr))) == NULL) {
320 res_cnt = 0;
321 goto cleanup;
322 }
323
324 i = 0;
325 for (ai = res; ai; ai = ai->ai_next) {
326 (*addrs)[i].len = ai->ai_addrlen;
327 memcpy(&(*addrs)[i].ss, ai->ai_addr, ai->ai_addrlen);
328 ++i;
329 }
330
331cleanup:
332 freeaddrinfo(res);
333 return res_cnt;
334}
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:191
static void * cleanup(void *unused)
Definition: pbx_realtime.c:124

References ast_log, ast_malloc, ast_sockaddr_split_hostport(), ast_strdupa, cleanup(), LOG_ERROR, NULL, S_OR, and str.

Referenced by app_exec(), apply_negotiated_sdp_stream(), ast_audiosocket_connect(), ast_sockaddr_resolve_first_af(), build_peer(), extract_contact_addr(), get_ipaddress(), handle_cli_udptl_set_debug(), http_server_get_by_host(), ip_identify_match_host_lookup(), launch_netscript(), negotiate_incoming_sdp_stream(), populate_addr(), realtime_peer(), realtime_user(), resolve_match_or_first(), and websocket_client_args_create().

◆ ast_sockaddr_resolve_first_af()

int ast_sockaddr_resolve_first_af ( struct ast_sockaddr addr,
const char *  name,
int  flag,
int  family 
)

Return the first entry from ast_sockaddr_resolve filtered by address family.

Since
16.0

Parses a string containing a host name or an IPv4 or IPv6 address followed by an optional port (separated by a colon). This function only returns the first address into the ast_sockaddr. Allowed formats for name are the following:

hostname:port host.example.com:port a.b.c.d a.b.c.d:port a:b:c:...:d [a:b:c:...:d]

Parameters
[out]addrThe resulting ast_sockaddr
nameThe string to parse
flagIf set to zero, a port MAY be present. If set to PARSE_PORT_IGNORE, a port MAY be present but will be ignored. If set to PARSE_PORT_REQUIRE, a port MUST be present. If set to PARSE_PORT_FORBID, a port MUST NOT be present.
familyOnly addresses of the given family will be returned. Use 0 or AST_AF_UNSPEC to specify any address family. Behavior is ultimately determined by getaddrinfo in how it orders return results. First result is selected to be returned.
Return values
0Success
non-zeroFailure
Warning
Using this function potentially means you have a faulty design.

Return the first entry from ast_sockaddr_resolve filtered by address family.

Definition at line 337 of file netsock2.c.

339{
340 struct ast_sockaddr *addrs;
341 int addrs_cnt;
342
343 addrs_cnt = ast_sockaddr_resolve(&addrs, name, flag, family);
344 if (addrs_cnt <= 0) {
345 return 1;
346 }
347 if (addrs_cnt > 1) {
348 ast_debug(1, "Multiple addresses resolving %s, using the first one only\n", name);
349 }
350
351 ast_sockaddr_copy(addr, &addrs[0]);
352
353 ast_free(addrs);
354 return 0;
355}
#define ast_free(a)
Definition: astmm.h:180
long int flag
Definition: f2c.h:83
static const char name[]
Definition: format_mp3.c:68
#define ast_debug(level,...)
Log a DEBUG message.
int ast_sockaddr_resolve(struct ast_sockaddr **addrs, const char *str, int flags, int family)
Parses a string with an IPv4 or IPv6 address and place results into an array.
Definition: netsock2.c:280
static void ast_sockaddr_copy(struct ast_sockaddr *dst, const struct ast_sockaddr *src)
Copies the data from one ast_sockaddr to another.
Definition: netsock2.h:167

References ast_debug, ast_free, ast_sockaddr_copy(), ast_sockaddr_resolve(), and name.

Referenced by ast_parse_arg(), audiosocket_request(), festival_exec(), hepv3_data_alloc(), iax_template_parse(), pjsip_enable_logger_host(), and reload_config().

◆ ast_sockaddr_setnull()

static void ast_sockaddr_setnull ( struct ast_sockaddr addr)
inlinestatic

◆ ast_sockaddr_split_hostport()

int ast_sockaddr_split_hostport ( char *  str,
char **  host,
char **  port,
int  flags 
)

Splits a string into its host and port components.

Since
1.8
Parameters
[in]strThe string to parse. May be modified by writing a NUL at the end of the host part.
[out]hostPointer to the host component within str.
[out]portPointer to the port component within str.
flagsIf set to zero, a port MAY be present. If set to PARSE_PORT_IGNORE, a port MAY be present but will be ignored. If set to PARSE_PORT_REQUIRE, a port MUST be present. If set to PARSE_PORT_FORBID, a port MUST NOT be present.
Return values
1Success
0Failure

Definition at line 164 of file netsock2.c.

165{
166 char *s = str;
167 char *orig_str = str;/* Original string in case the port presence is incorrect. */
168 char *host_end = NULL;/* Delay terminating the host in case the port presence is incorrect. */
169
170 ast_debug(5, "Splitting '%s' into...\n", str);
171 *host = NULL;
172 *port = NULL;
173 if (*s == '[') {
174 *host = ++s;
175 for (; *s && *s != ']'; ++s) {
176 }
177 if (*s == ']') {
178 host_end = s;
179 ++s;
180 }
181 if (*s == ':') {
182 *port = s + 1;
183 }
184 } else {
185 *host = s;
186 for (; *s; ++s) {
187 if (*s == ':') {
188 if (*port) {
189 *port = NULL;
190 break;
191 } else {
192 *port = s;
193 }
194 }
195 }
196 if (*port) {
197 host_end = *port;
198 ++*port;
199 }
200 }
201
202 switch (flags & PARSE_PORT_MASK) {
204 *port = NULL;
205 break;
207 if (*port == NULL) {
208 ast_log(LOG_WARNING, "Port missing in %s\n", orig_str);
209 return 0;
210 }
211 break;
213 if (*port != NULL) {
214 ast_log(LOG_WARNING, "Port disallowed in %s\n", orig_str);
215 return 0;
216 }
217 break;
218 }
219
220 /* Can terminate the host string now if needed. */
221 if (host_end) {
222 *host_end = '\0';
223 }
224 ast_debug(5, "...host '%s' and port '%s'.\n", *host, *port ? *port : "");
225 return 1;
226}
@ PARSE_PORT_REQUIRE

References ast_debug, ast_log, LOG_WARNING, NULL, PARSE_PORT_FORBID, PARSE_PORT_IGNORE, PARSE_PORT_MASK, PARSE_PORT_REQUIRE, and str.

Referenced by ast_ari_channels_external_media(), ast_sockaddr_parse(), ast_sockaddr_resolve(), peer_set_srcaddr(), rtcp_do_debug_ip(), rtp_do_debug_ip(), rtp_reload(), setup_stunaddr(), and unicast_rtp_request().

◆ ast_sockaddr_stringify()

static char * ast_sockaddr_stringify ( const struct ast_sockaddr addr)
inlinestatic

Wrapper around ast_sockaddr_stringify_fmt() with default format.

Since
1.8
Returns
same as ast_sockaddr_stringify_fmt()

Definition at line 256 of file netsock2.h.

257{
259}
char * ast_sockaddr_stringify_fmt(const struct ast_sockaddr *addr, int format)
Convert a socket address to a string.
Definition: netsock2.c:65
#define AST_SOCKADDR_STR_DEFAULT
Definition: netsock2.h:203

References AST_SOCKADDR_STR_DEFAULT, and ast_sockaddr_stringify_fmt().

Referenced by __find_callno(), __rtp_recvfrom(), append_ha_core(), apply_acl(), apply_contact_acl(), ast_apply_ha(), ast_ari_callback(), ast_ari_websocket_session_write(), ast_audiosocket_connect(), ast_ha_join(), ast_netsock_bindaddr(), ast_parse_arg(), ast_rtcp_calculate_sr_rr_statistics(), ast_rtcp_interpret(), ast_rtcp_read(), ast_rtcp_write(), ast_rtp_dtmf_begin(), ast_rtp_dtmf_continuation(), ast_rtp_dtmf_end_with_duration(), ast_rtp_interpret(), ast_rtp_prop_set(), ast_rtp_read(), ast_rtp_remote_address_set(), ast_rtp_sendcng(), ast_sockaddr_pj_sockaddr_cmp(), ast_tcptls_client_create(), ast_tcptls_client_start_timeout(), ast_tcptls_server_root(), ast_tcptls_server_start(), AST_TEST_DEFINE(), ast_udptl_read(), ast_udptl_write(), ast_websocket_close(), ast_websocket_uri_cb(), bridge_p2p_rtp_write(), channel_read_rtp(), cli_print_body(), create_dtmf_frame(), debug_ha_sense_appended(), dnsmgr_refresh(), dump_addr(), dump_ipaddr(), dundi_rexmit(), dundi_showframe(), dundi_xmit(), find_tpeer(), handle_audiosocket_connection(), handle_call_token(), handle_cli_iax2_show_callno_limits(), handle_cli_iax2_show_registry(), handle_cli_udptl_set_debug(), handle_command_response(), handle_connection(), handle_manager_show_settings(), handle_show_http(), handle_tcptls_connection(), http_server_get_by_addr(), httpstatus_callback(), iax2_ack_registry(), iax2_devicestate(), iax2_prov_app(), iax2_trunk_queue(), iax_showframe(), ip_identify_match_check(), ip_identify_match_host_lookup(), launch_netscript(), load_module(), manager_iax2_show_registry(), match_to_var_list_append(), pjsip_enable_logger_host(), process_cn_rfc3389(), process_dtmf_rfc2833(), raw_hangup(), reg_source_db(), registry_rerequest(), rtcp_do_debug_ip(), rtp_allocate_transport(), rtp_do_debug_ip(), rtp_raw_write(), rtp_transport_wide_cc_feedback_produce(), send_packet(), session_destroy_fn(), set_config(), set_peercnt_limit(), sockaddr_handler_fn(), socket_process_helper(), socket_process_meta(), stasis_app_message_handler(), stun_monitor_request(), timing_read(), transport_create(), transport_read(), and update_registry().

◆ ast_sockaddr_stringify_addr()

static char * ast_sockaddr_stringify_addr ( const struct ast_sockaddr addr)
inlinestatic

◆ ast_sockaddr_stringify_addr_remote()

static char * ast_sockaddr_stringify_addr_remote ( const struct ast_sockaddr addr)
inlinestatic

Wrapper around ast_sockaddr_stringify_fmt() to return an address only.

Since
1.8
Note
This address will be suitable for passing to a remote machine via the application layer. For example, the scope-id on a link-local IPv6 address will be stripped.
Returns
same as ast_sockaddr_stringify_fmt()

Definition at line 313 of file netsock2.h.

314{
316}
#define AST_SOCKADDR_STR_ADDR_REMOTE
Definition: netsock2.h:204

References AST_SOCKADDR_STR_ADDR_REMOTE, and ast_sockaddr_stringify_fmt().

Referenced by add_ice_to_stream(), change_outgoing_sdp_stream_media_address(), media_address_handler(), and session_outgoing_nat_hook().

◆ ast_sockaddr_stringify_fmt()

char * ast_sockaddr_stringify_fmt ( const struct ast_sockaddr addr,
int  format 
)

Convert a socket address to a string.

Since
1.8

This will be of the form a.b.c.d:xyz for IPv4 and [a:b:c:...:d]:xyz for IPv6.

This function is thread-safe. The returned string is on static thread-specific storage.

Parameters
addrThe input to be stringified
formatone of the following: AST_SOCKADDR_STR_DEFAULT: a.b.c.d:xyz for IPv4 [a:b:c:...:d]:xyz for IPv6. AST_SOCKADDR_STR_ADDR: address only a.b.c.d for IPv4 a:b:c:...:d for IPv6. AST_SOCKADDR_STR_HOST: address only, suitable for a URL a.b.c.d for IPv4 [a:b:c:...:d] for IPv6. AST_SOCKADDR_STR_PORT: port only
Note
The string pointer returned by this function will point to a string that will be changed whenever any form of ast_sockaddr_stringify_fmt is called on that thread. Because of this, it is important that if you use this function, you use the string before another use of this function is made elsewhere in the same thread. The easiest way to accomplish this is by immediately copying the string to a buffer with something like ast_strdupa.
Return values
(null)addr is null
""An error occurred during processing
Returns
string The stringified form of the address

Definition at line 65 of file netsock2.c.

66{
67 struct ast_sockaddr sa_ipv4;
68 const struct ast_sockaddr *sa_tmp;
69 char host[NI_MAXHOST];
70 char port[NI_MAXSERV];
71 struct ast_str *str;
72 int e;
73 static const size_t size = sizeof(host) - 1 + sizeof(port) - 1 + 4;
74
75
76 if (ast_sockaddr_isnull(sa)) {
77 return "(null)";
78 }
79
81 return "";
82 }
83
84 if (ast_sockaddr_ipv4_mapped(sa, &sa_ipv4)) {
85 sa_tmp = &sa_ipv4;
86 } else {
87 sa_tmp = sa;
88 }
89
90 if ((e = getnameinfo((struct sockaddr *)&sa_tmp->ss, sa_tmp->len,
91 format & AST_SOCKADDR_STR_ADDR ? host : NULL,
92 format & AST_SOCKADDR_STR_ADDR ? sizeof(host) : 0,
93 format & AST_SOCKADDR_STR_PORT ? port : 0,
94 format & AST_SOCKADDR_STR_PORT ? sizeof(port): 0,
95 NI_NUMERICHOST | NI_NUMERICSERV))) {
96 ast_log(LOG_ERROR, "getnameinfo(): %s\n", gai_strerror(e));
97 return "";
98 }
99
101 char *p;
102 if (ast_sockaddr_is_ipv6_link_local(sa) && (p = strchr(host, '%'))) {
103 *p = '\0';
104 }
105 }
106
107 switch ((format & AST_SOCKADDR_STR_FORMAT_MASK)) {
109 ast_str_set(&str, 0, sa_tmp->ss.ss_family == AF_INET6 ?
110 "[%s]:%s" : "%s:%s", host, port);
111 break;
113 ast_str_set(&str, 0, "%s", host);
114 break;
116 ast_str_set(&str, 0,
117 sa_tmp->ss.ss_family == AF_INET6 ? "[%s]" : "%s", host);
118 break;
120 ast_str_set(&str, 0, "%s", port);
121 break;
122 default:
123 ast_log(LOG_ERROR, "Invalid format\n");
124 return "";
125 }
126
127 return ast_str_buffer(str);
128}
static struct ast_threadstorage ast_sockaddr_stringify_buf
Definition: netsock2.c:63
int ast_sockaddr_is_ipv6_link_local(const struct ast_sockaddr *addr)
Determine if this is a link-local IPv6 address.
Definition: netsock2.c:518
#define AST_SOCKADDR_STR_HOST
Definition: netsock2.h:202
#define AST_SOCKADDR_STR_PORT
Definition: netsock2.h:199
#define AST_SOCKADDR_STR_FORMAT_MASK
Definition: netsock2.h:207
#define AST_SOCKADDR_STR_REMOTE
Definition: netsock2.h:201
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:761
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Definition: strings.h:1113
struct ast_str * ast_str_thread_get(struct ast_threadstorage *ts, size_t init_len)
Retrieve a thread locally stored dynamic string.
Definition: strings.h:909
Support for dynamic strings.
Definition: strings.h:623

References ast_log, ast_sockaddr_ipv4_mapped(), ast_sockaddr_is_ipv6_link_local(), ast_sockaddr_isnull(), AST_SOCKADDR_STR_ADDR, AST_SOCKADDR_STR_DEFAULT, AST_SOCKADDR_STR_FORMAT_MASK, AST_SOCKADDR_STR_HOST, AST_SOCKADDR_STR_PORT, AST_SOCKADDR_STR_REMOTE, ast_sockaddr_stringify_buf, ast_str_buffer(), ast_str_set(), ast_str_thread_get(), ast_sockaddr::len, LOG_ERROR, NULL, ast_sockaddr::ss, and str.

Referenced by ast_sockaddr_stringify(), ast_sockaddr_stringify_addr(), ast_sockaddr_stringify_addr_remote(), ast_sockaddr_stringify_host(), ast_sockaddr_stringify_host_remote(), ast_sockaddr_stringify_port(), ast_sockaddr_stringify_remote(), create_outgoing_sdp_stream(), and statsd_init().

◆ ast_sockaddr_stringify_host()

static char * ast_sockaddr_stringify_host ( const struct ast_sockaddr addr)
inlinestatic

◆ ast_sockaddr_stringify_host_remote()

static char * ast_sockaddr_stringify_host_remote ( const struct ast_sockaddr addr)
inlinestatic

Wrapper around ast_sockaddr_stringify_fmt() to return an address only, suitable for a URL (with brackets for IPv6).

Since
1.8
Note
This address will be suitable for passing to a remote machine via the application layer. For example, the scope-id on a link-local IPv6 address will be stripped.
Returns
same as ast_sockaddr_stringify_fmt()

Definition at line 345 of file netsock2.h.

346{
348}
#define AST_SOCKADDR_STR_HOST_REMOTE
Definition: netsock2.h:205

References AST_SOCKADDR_STR_HOST_REMOTE, and ast_sockaddr_stringify_fmt().

◆ ast_sockaddr_stringify_port()

static char * ast_sockaddr_stringify_port ( const struct ast_sockaddr addr)
inlinestatic

◆ ast_sockaddr_stringify_remote()

static char * ast_sockaddr_stringify_remote ( const struct ast_sockaddr addr)
inlinestatic

Wrapper around ast_sockaddr_stringify_fmt() with default format.

Since
1.8
Note
This address will be suitable for passing to a remote machine via the application layer. For example, the scope-id on a link-local IPv6 address will be stripped.
Returns
same as ast_sockaddr_stringify_fmt()

Definition at line 273 of file netsock2.h.

274{
276}
#define AST_SOCKADDR_STR_DEFAULT_REMOTE
Definition: netsock2.h:206

References AST_SOCKADDR_STR_DEFAULT_REMOTE, and ast_sockaddr_stringify_fmt().

◆ ast_transport2str()

const char * ast_transport2str ( enum ast_transport  transport)

Returns a string representation of an ast_transport.

Since
12.3
Returns
Name of the tranpsort if it is defined
Undefined if the transport is undefined

Definition at line 566 of file netsock2.c.

567{
568 switch (transport) {
570 return "TLS";
572 return "UDP";
574 return "TCP";
575 case AST_TRANSPORT_WS:
576 return "WS";
578 return "WSS";
579 }
580
581 return "Undefined";
582}

References AST_TRANSPORT_TCP, AST_TRANSPORT_TLS, AST_TRANSPORT_UDP, AST_TRANSPORT_WS, and AST_TRANSPORT_WSS.