Network socket handling. More...
#include "asterisk.h"
#include "asterisk/config.h"
#include "asterisk/netsock2.h"
#include "asterisk/utils.h"
#include "asterisk/threadstorage.h"
Go to the source code of this file.
Functions | |
static void | __init_ast_sockaddr_stringify_buf (void) |
void | _ast_sockaddr_from_sin (struct ast_sockaddr *addr, const struct sockaddr_in *sin, const char *file, int line, const char *func) |
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_sockaddr_to_sin (const struct ast_sockaddr *addr, struct sockaddr_in *sin, 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_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... | |
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_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... | |
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_split_hostport (char *str, char **host, char **port, int flags) |
Splits a string into its host and port components. More... | |
char * | ast_sockaddr_stringify_fmt (const struct ast_sockaddr *sa, int format) |
Convert a socket address to a string. More... | |
Variables | |
static struct ast_threadstorage | ast_sockaddr_stringify_buf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_ast_sockaddr_stringify_buf , .custom_init = NULL , } |
Network socket handling.
Definition in file netsock2.c.
|
static |
Definition at line 65 of file netsock2.c.
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 561 of file netsock2.c.
References __LOG_DEBUG, ast_log(), ast_sockaddr::len, option_debug, and ast_sockaddr::ss.
uint16_t _ast_sockaddr_port | ( | const struct ast_sockaddr * | addr, |
const char * | file, | ||
int | line, | ||
const char * | func | ||
) |
Definition at line 365 of file netsock2.c.
References __LOG_DEBUG, ast_log(), ast_sockaddr::len, option_debug, and ast_sockaddr::ss.
void _ast_sockaddr_set_port | ( | struct ast_sockaddr * | addr, |
uint16_t | port, | ||
const char * | file, | ||
int | line, | ||
const char * | func | ||
) |
Definition at line 380 of file netsock2.c.
References __LOG_DEBUG, ast_log(), ast_sockaddr::len, option_debug, and ast_sockaddr::ss.
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 540 of file netsock2.c.
References __LOG_DEBUG, __LOG_ERROR, ast_log(), ast_sockaddr_isnull(), ast_sockaddr::len, option_debug, and ast_sockaddr::ss.
int ast_accept | ( | int | sockfd, |
struct ast_sockaddr * | addr | ||
) |
Wrapper around accept(2) that uses struct ast_sockaddr.
For parameter and return information, see the man page for accept(2).
Definition at line 456 of file netsock2.c.
References ast_sockaddr::len, and ast_sockaddr::ss.
Referenced by ast_tcptls_server_root().
int ast_bind | ( | int | sockfd, |
const struct ast_sockaddr * | addr | ||
) |
Wrapper around bind(2) that uses struct ast_sockaddr.
For parameter and return information, see the man page for bind(2).
Definition at line 462 of file netsock2.c.
References ast_sockaddr::len, and ast_sockaddr::ss.
Referenced by ast_rtp_new(), ast_rtp_prop_set(), ast_tcptls_client_create(), ast_tcptls_server_start(), ast_udptl_new_with_bindaddr(), and reload_config().
int ast_connect | ( | int | sockfd, |
const struct ast_sockaddr * | addr | ||
) |
Wrapper around connect(2) that uses struct ast_sockaddr.
For parameter and return information, see the man page for connect(2).
Definition at line 467 of file netsock2.c.
References ast_sockaddr::len, and ast_sockaddr::ss.
Referenced by ast_ouraddrfor(), ast_tcptls_client_start(), gtalk_update_externip(), and stun_monitor_request().
int ast_getsockname | ( | int | sockfd, |
struct ast_sockaddr * | addr | ||
) |
Wrapper around getsockname(2) that uses struct ast_sockaddr.
For parameter and return information, see the man page for getsockname(2).
Definition at line 472 of file netsock2.c.
References ast_sockaddr::len, and ast_sockaddr::ss.
Referenced by ast_ouraddrfor(), and ast_set_qos().
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.
For parameter and return information, see the man page for recvfrom(2).
Definition at line 478 of file netsock2.c.
References ast_sockaddr::len, and ast_sockaddr::ss.
Referenced by __rtp_recvfrom(), ast_udptl_read(), and sipsock_read().
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.
For parameter and return information, see the man page for sendto(2)
Definition at line 486 of file netsock2.c.
References ast_sockaddr::len, and ast_sockaddr::ss.
Referenced by __rtp_sendto(), __sip_xmit(), ast_udptl_write(), multicast_rtp_write(), and multicast_send_control_packet().
int ast_set_qos | ( | int | sockfd, |
int | tos, | ||
int | cos, | ||
const char * | desc | ||
) |
Set type of service.
Set ToS ("Type of Service for IPv4 and "Traffic Class for IPv6) and CoS (Linux's SO_PRIORITY)
sockfd | File descriptor for socket on which to set the parameters |
tos | The type of service for the socket |
cos | The cost of service for the socket |
desc | A text description of the socket in question. |
0 | Success |
-1 | Error, with errno set to an appropriate value |
Definition at line 493 of file netsock2.c.
References ast_getsockname(), ast_log(), ast_sockaddr_is_any(), ast_sockaddr_is_ipv6(), ast_verb, errno, and LOG_WARNING.
Referenced by ast_rtp_qos_set(), and reload_config().
int ast_sockaddr_cmp | ( | const struct ast_sockaddr * | a, |
const struct ast_sockaddr * | b | ||
) |
Compares two ast_sockaddr structures.
-1 | a is lexicographically smaller than b |
0 | a is equal to b |
1 | b is lexicographically smaller than a |
Definition at line 300 of file netsock2.c.
References ast_sockaddr_ipv4_mapped(), ast_sockaddr::len, and ast_sockaddr::ss.
Referenced by add_sdp(), ast_rtcp_read(), ast_rtp_instance_get_and_cmp_local_address(), ast_rtp_instance_get_and_cmp_remote_address(), ast_rtp_read(), ast_tcptls_client_create(), ast_tcptls_server_start(), ast_udptl_bridge(), ast_udptl_read(), dnsmgr_refresh(), parse_register_contact(), realtime_peer_by_name(), reload_config(), remote_bridge_loop(), rtcp_debug_test_addr(), rtp_debug_test_addr(), sip_debug_test_addr(), sip_uri_domain_cmp(), threadinfo_locate_cb(), transmit_register(), udptl_debug_test_addr(), and update_registry().
int ast_sockaddr_cmp_addr | ( | const struct ast_sockaddr * | a, |
const struct ast_sockaddr * | b | ||
) |
Compares the addresses of two ast_sockaddr structures.
-1 | a is lexicographically smaller than b |
0 | a is equal to b |
1 | b is lexicographically smaller than a |
Definition at line 325 of file netsock2.c.
References ast_sockaddr_ipv4_mapped(), ast_sockaddr::len, and ast_sockaddr::ss.
Referenced by addr_range_cmp_cb(), ast_apply_ha(), get_our_media_address(), peer_ipcmp_cb(), rtcp_debug_test_addr(), rtp_debug_test_addr(), sip_debug_test_addr(), and udptl_debug_test_addr().
int ast_sockaddr_hash | ( | const struct ast_sockaddr * | addr | ) |
Computes a hash value from the address. The port is ignored.
0 | Unknown address family |
other | A 32-bit hash derived from the address |
Definition at line 438 of file netsock2.c.
References ast_log(), LOG_ERROR, and ast_sockaddr::ss.
Referenced by peer_iphash_cb(), and threadt_hash_cb().
uint32_t ast_sockaddr_ipv4 | ( | const struct ast_sockaddr * | addr | ) |
Get an IPv4 address of an ast_sockaddr.
Definition at line 394 of file netsock2.c.
References ast_sockaddr::ss.
Referenced by addr_is_multicast(), iax2_devicestate(), iax2_do_register(), iax2_poke_peer(), jingle_create_candidates(), load_module(), and multicast_send_control_packet().
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.
addr | The IPv4-mapped address to convert |
mapped_addr | The resulting IPv4 address |
0 | Unable to make the conversion |
1 | Successful conversion |
Definition at line 39 of file netsock2.c.
References ast_sockaddr_from_sin, ast_sockaddr_is_ipv4_mapped(), ast_sockaddr_is_ipv6(), and ast_sockaddr::ss.
Referenced by ast_append_ha(), ast_apply_ha(), ast_rtp_read(), ast_sockaddr_cmp(), ast_sockaddr_cmp_addr(), and ast_sockaddr_stringify_fmt().
int ast_sockaddr_is_any | ( | const struct ast_sockaddr * | addr | ) |
Determine if the address type is unspecified, or "any" address.
For IPv4, this would be the address 0.0.0.0, and for IPv6, this would be the address ::. The port number is ignored.
1 | This is an "any" address |
0 | This is not an "any" address |
Definition at line 424 of file netsock2.c.
References ast_sockaddr_is_ipv4(), ast_sockaddr_is_ipv6(), and ast_sockaddr::ss.
Referenced by ast_find_ourip(), ast_set_qos(), ast_sip_ouraddrfor(), get_address_family_filter(), get_our_media_address(), gtalk_get_local_ip(), reload_config(), sip_show_settings(), and sockaddr_is_null_or_any().
int ast_sockaddr_is_ipv4 | ( | const struct ast_sockaddr * | addr | ) |
Determine if the address is an IPv4 address.
1 | This is an IPv4 address |
0 | This is an IPv6 or IPv4-mapped IPv6 address |
Definition at line 400 of file netsock2.c.
References ast_sockaddr::len, and ast_sockaddr::ss.
Referenced by apply_netmask(), ast_append_ha(), ast_apply_ha(), ast_rtp_new(), ast_rtp_prop_set(), ast_rtp_read(), and ast_sockaddr_is_any().
int ast_sockaddr_is_ipv4_mapped | ( | const struct ast_sockaddr * | addr | ) |
Determine if this is an IPv4-mapped IPv6 address.
1 | This is an IPv4-mapped IPv6 address. |
0 | This is not an IPv4-mapped IPv6 address. |
Definition at line 406 of file netsock2.c.
References ast_sockaddr::len, and ast_sockaddr::ss.
Referenced by add_sdp(), ast_apply_ha(), ast_rtp_instance_bridge(), and ast_sockaddr_ipv4_mapped().
int ast_sockaddr_is_ipv6 | ( | const struct ast_sockaddr * | addr | ) |
Determine if this is an IPv6 address.
1 | This is an IPv6 or IPv4-mapped IPv6 address. |
0 | This is an IPv4 address. |
Definition at line 418 of file netsock2.c.
References ast_sockaddr::len, and ast_sockaddr::ss.
Referenced by add_sdp(), apply_netmask(), ast_apply_ha(), ast_ouraddrfor(), ast_rtp_new(), ast_rtp_prop_set(), ast_set_qos(), ast_sip_ouraddrfor(), 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(), get_address_family_filter(), multicast_send_control_packet(), reload_config(), and sip_show_settings().
int ast_sockaddr_is_ipv6_link_local | ( | const struct ast_sockaddr * | addr | ) |
Determine if this is a link-local IPv6 address.
1 | This is a link-local IPv6 address. |
0 | This is link-local IPv6 address. |
Definition at line 412 of file netsock2.c.
References ast_sockaddr_is_ipv6(), and ast_sockaddr::ss.
Referenced by ast_sockaddr_stringify_fmt().
int ast_sockaddr_parse | ( | struct ast_sockaddr * | addr, |
const char * | str, | ||
int | flags | ||
) |
Parse an IPv4 or IPv6 address string.
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.
[out] | addr | The resulting ast_sockaddr. This MAY be NULL from functions that are performing validity checks only, e.g. ast_parse_arg(). |
str | The string to parse | |
flags | If 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. |
1 | Success |
0 | Failure |
Definition at line 198 of file netsock2.c.
References ast_log(), ast_sockaddr_split_hostport(), ast_strdupa, ast_sockaddr::len, LOG_ERROR, LOG_WARNING, S_OR, and ast_sockaddr::ss.
Referenced by ast_append_ha(), ast_parse_arg(), build_peer(), internal_dnsmgr_lookup(), multicast_rtp_request(), proxy_update(), realtime_peer(), reg_source_db(), reload_config(), rtcp_do_debug_ip(), rtp_do_debug_ip(), sip_sanitized_host(), and sip_uri_domain_cmp().
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.
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]
[out] | addrs | The resulting array of ast_sockaddrs |
str | The string to parse | |
flags | If 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. | |
family | Only addresses of the given family will be returned. Use 0 or AST_SOCKADDR_UNSPEC to get addresses of all families. |
0 | Failure |
non-zero | The number of elements in addrs array. |
Definition at line 248 of file netsock2.c.
References ast_log(), ast_malloc, ast_sockaddr_split_hostport(), ast_strdupa, cleanup(), LOG_ERROR, and S_OR.
Referenced by ast_sockaddr_resolve_first_af(), gtalk_get_local_ip(), handle_cli_udptl_set_debug(), realtime_peer_by_name(), and resolve_first().
int ast_sockaddr_split_hostport | ( | char * | str, |
char ** | host, | ||
char ** | port, | ||
int | flags | ||
) |
Splits a string into its host and port components.
str[in] | The string to parse. May be modified by writing a NUL at the end of the host part. |
host[out] | Pointer to the host component within str. |
port[out] | Pointer to the port component within str. |
flags | If 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. |
1 | Success |
0 | Failure |
Definition at line 132 of file netsock2.c.
References ast_debug, ast_log(), LOG_WARNING, PARSE_PORT_FORBID, PARSE_PORT_IGNORE, PARSE_PORT_MASK, PARSE_PORT_REQUIRE, and str.
Referenced by ast_sockaddr_parse(), ast_sockaddr_resolve(), extract_host_from_hostport(), rtcp_do_debug_ip(), rtp_do_debug_ip(), setup_stunaddr(), and sip_parse_host().
char* ast_sockaddr_stringify_fmt | ( | const struct ast_sockaddr * | addr, |
int | format | ||
) |
Convert a socket address to a string.
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.
addr | The input to be stringified |
format | one 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 |
(null) | addr is null |
"" | An error occurred during processing |
string | The stringified form of the address |
Definition at line 67 of file netsock2.c.
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, 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(), and ast_sockaddr_stringify_remote().
|
static |
Definition at line 65 of file netsock2.c.
Referenced by ast_sockaddr_stringify_fmt().