netsock2.h File Reference

Network socket handling. More...

#include <sys/socket.h>
#include <netinet/in.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...
#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.
#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.
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)

Defines

#define ast_sockaddr_port(addr)   _ast_sockaddr_port(addr, __FILE__, __LINE__, __PRETTY_FUNCTION__)
 Get the port number of a socket address.
#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.
#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.

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.
int ast_bind (int sockfd, const struct ast_sockaddr *addr)
 Wrapper around bind(2) that uses struct ast_sockaddr.
int ast_connect (int sockfd, const struct ast_sockaddr *addr)
 Wrapper around connect(2) that uses struct ast_sockaddr.
int ast_getsockname (int sockfd, struct ast_sockaddr *addr)
 Wrapper around getsockname(2) that uses struct ast_sockaddr.
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.
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.
int ast_set_qos (int sockfd, int tos, int cos, const char *desc)
 Set type of service.
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.
int ast_sockaddr_cidr_bits (const struct ast_sockaddr *sa)
 Count the 1 bits in a netmask.
int ast_sockaddr_cmp (const struct ast_sockaddr *a, const struct ast_sockaddr *b)
 Compares two ast_sockaddr structures.
int ast_sockaddr_cmp_addr (const struct ast_sockaddr *a, const struct ast_sockaddr *b)
 Compares the addresses of two ast_sockaddr structures.
static void ast_sockaddr_copy (struct ast_sockaddr *dst, const struct ast_sockaddr *src)
 Copies the data from one ast_sockaddr to another.
int ast_sockaddr_hash (const struct ast_sockaddr *addr)
 Computes a hash value from the address. The port is ignored.
uint32_t ast_sockaddr_ipv4 (const struct ast_sockaddr *addr)
 Get an IPv4 address of an ast_sockaddr.
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.
int ast_sockaddr_is_any (const struct ast_sockaddr *addr)
 Determine if the address type is unspecified, or "any" address.
int ast_sockaddr_is_ipv4 (const struct ast_sockaddr *addr)
 Determine if the address is an IPv4 address.
int ast_sockaddr_is_ipv4_mapped (const struct ast_sockaddr *addr)
 Determine if this is an IPv4-mapped IPv6 address.
int ast_sockaddr_is_ipv4_multicast (const struct ast_sockaddr *addr)
 Determine if an IPv4 address is a multicast address.
int ast_sockaddr_is_ipv6 (const struct ast_sockaddr *addr)
 Determine if this is an IPv6 address.
int ast_sockaddr_is_ipv6_link_local (const struct ast_sockaddr *addr)
 Determine if this is a link-local IPv6 address.
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.
int ast_sockaddr_parse (struct ast_sockaddr *addr, const char *str, int flags)
 Parse an IPv4 or IPv6 address string.
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.
static void ast_sockaddr_setnull (struct ast_sockaddr *addr)
 Sets address addr to null.
int ast_sockaddr_split_hostport (char *str, char **host, char **port, int flags)
 Splits a string into its host and port components.
static char * ast_sockaddr_stringify (const struct ast_sockaddr *addr)
 Wrapper around ast_sockaddr_stringify_fmt() with default format.
static char * ast_sockaddr_stringify_addr (const struct ast_sockaddr *addr)
 Wrapper around ast_sockaddr_stringify_fmt() to return an address only.
static char * ast_sockaddr_stringify_addr_remote (const struct ast_sockaddr *addr)
 Wrapper around ast_sockaddr_stringify_fmt() to return an address only.
char * ast_sockaddr_stringify_fmt (const struct ast_sockaddr *addr, int format)
 Convert a socket address to a string.
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).
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).
static char * ast_sockaddr_stringify_port (const struct ast_sockaddr *addr)
 Wrapper around ast_sockaddr_stringify_fmt() to return a port only.
static char * ast_sockaddr_stringify_remote (const struct ast_sockaddr *addr)
 Wrapper around ast_sockaddr_stringify_fmt() with default format.
const char * ast_transport2str (enum ast_transport transport)
 Returns a string representation of an ast_transport.


Detailed Description

Network socket handling.

Definition in file netsock2.h.


Define Documentation

#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 
sin The sockaddr_in to convert
Returns:
an ast_sockaddr structure

Definition at line 714 of file netsock2.h.

Referenced by ast_rtcp_read(), ast_rtp_read(), ast_rtp_stun_request(), ast_sockaddr_apply_netmask(), ast_sockaddr_ipv4_mapped(), build_gateway(), find_subchannel_and_lock(), handle_open_receive_channel_ack_message(), parse_cidr_mask(), process_sdp(), skinny_register(), and start_rtp().

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

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

#define AST_SOCKADDR_STR_ADDR   (1 << 0)

#define AST_SOCKADDR_STR_ADDR_REMOTE   (AST_SOCKADDR_STR_ADDR | AST_SOCKADDR_STR_REMOTE)

Definition at line 178 of file netsock2.h.

Referenced by ast_sockaddr_stringify_addr_remote().

#define AST_SOCKADDR_STR_BRACKETS   (1 << 2)

Definition at line 174 of file netsock2.h.

#define AST_SOCKADDR_STR_DEFAULT   (AST_SOCKADDR_STR_ADDR | AST_SOCKADDR_STR_PORT)

Definition at line 177 of file netsock2.h.

Referenced by ast_sockaddr_stringify(), ast_sockaddr_stringify_fmt(), and statsd_init().

#define AST_SOCKADDR_STR_DEFAULT_REMOTE   (AST_SOCKADDR_STR_DEFAULT | AST_SOCKADDR_STR_REMOTE)

Definition at line 180 of file netsock2.h.

Referenced by ast_sockaddr_stringify_remote().

#define AST_SOCKADDR_STR_FORMAT_MASK   (AST_SOCKADDR_STR_ADDR | AST_SOCKADDR_STR_PORT | AST_SOCKADDR_STR_BRACKETS)

Definition at line 181 of file netsock2.h.

Referenced by ast_sockaddr_stringify_fmt().

#define AST_SOCKADDR_STR_HOST   (AST_SOCKADDR_STR_ADDR | AST_SOCKADDR_STR_BRACKETS)

Definition at line 176 of file netsock2.h.

Referenced by ast_sockaddr_stringify_fmt(), and ast_sockaddr_stringify_host().

#define AST_SOCKADDR_STR_HOST_REMOTE   (AST_SOCKADDR_STR_HOST | AST_SOCKADDR_STR_REMOTE)

Definition at line 179 of file netsock2.h.

Referenced by ast_sockaddr_stringify_host_remote().

#define AST_SOCKADDR_STR_PORT   (1 << 1)

Definition at line 173 of file netsock2.h.

Referenced by ast_sockaddr_stringify_fmt(), and ast_sockaddr_stringify_port().

#define AST_SOCKADDR_STR_REMOTE   (1 << 3)

Definition at line 175 of file netsock2.h.

Referenced by ast_sockaddr_stringify_fmt().

#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:
addr The ast_sockaddr to convert
[out] sin The resulting sockaddr_in struct
Return values:
nonzero Success
zero Failure

Definition at line 701 of file netsock2.h.

Referenced by add_sdp(), ast_rtcp_read(), ast_rtp_read(), ast_rtp_stun_request(), build_gateway(), config_parse_variables(), find_rtp_port(), find_subchannel_and_lock(), handle_open_receive_channel_ack_message(), send_start_rtp(), skinny_set_rtp_peer(), transmit_modify_with_sdp(), and unistim_set_rtp_peer().

#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:
sin6 A pointer to a struct sockaddr_in6
index Which 32-bit chunk to operate on. Must be in the range 0-3.

Definition at line 63 of file netsock2.h.

Referenced by ast_sockaddr_apply_netmask(), and parse_cidr_mask().


Enumeration Type Documentation

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 39 of file netsock2.h.

00039      {
00040    AST_AF_UNSPEC = AF_UNSPEC,
00041    AST_AF_INET   = AF_INET,
00042    AST_AF_INET6  = AF_INET6,
00043 };

Enumerator:
AST_TRANSPORT_UDP 
AST_TRANSPORT_TCP 
AST_TRANSPORT_TLS 
AST_TRANSPORT_WS 
AST_TRANSPORT_WSS 

Definition at line 45 of file netsock2.h.

00045                    {
00046    AST_TRANSPORT_UDP   = 1,
00047    AST_TRANSPORT_TCP   = 1 << 1,
00048    AST_TRANSPORT_TLS   = 1 << 2,
00049    AST_TRANSPORT_WS    = 1 << 3,
00050    AST_TRANSPORT_WSS   = 1 << 4,
00051 };


Function Documentation

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 653 of file netsock2.c.

References __LOG_DEBUG, ast_log, ast_sockaddr::len, option_debug, and ast_sockaddr::ss.

00655 {
00656    memcpy(&addr->ss, sin, sizeof(*sin));
00657 
00658    if (addr->ss.ss_family != AF_INET && option_debug >= 1) {
00659       ast_log(__LOG_DEBUG, file, line, func, "Address family is not AF_INET\n");
00660    }
00661 
00662    addr->len = sizeof(*sin);
00663 }

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

Definition at line 434 of file netsock2.c.

References __LOG_DEBUG, ast_log, ast_sockaddr::len, option_debug, and ast_sockaddr::ss.

00435 {
00436    if (addr->ss.ss_family == AF_INET &&
00437        addr->len == sizeof(struct sockaddr_in)) {
00438       return ntohs(((struct sockaddr_in *)&addr->ss)->sin_port);
00439    } else if (addr->ss.ss_family == AF_INET6 &&
00440        addr->len == sizeof(struct sockaddr_in6)) {
00441       return ntohs(((struct sockaddr_in6 *)&addr->ss)->sin6_port);
00442    }
00443    if (option_debug >= 1) {
00444       ast_log(__LOG_DEBUG, file, line, func, "Not an IPv4 nor IPv6 address, cannot get port.\n");
00445    }
00446    return 0;
00447 }

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

Definition at line 449 of file netsock2.c.

References __LOG_DEBUG, ast_log, ast_sockaddr::len, option_debug, and ast_sockaddr::ss.

00450 {
00451    if (addr->ss.ss_family == AF_INET &&
00452        addr->len == sizeof(struct sockaddr_in)) {
00453       ((struct sockaddr_in *)&addr->ss)->sin_port = htons(port);
00454    } else if (addr->ss.ss_family == AF_INET6 &&
00455        addr->len == sizeof(struct sockaddr_in6)) {
00456       ((struct sockaddr_in6 *)&addr->ss)->sin6_port = htons(port);
00457    } else if (option_debug >= 1) {
00458       ast_log(__LOG_DEBUG, file, line, func,
00459          "Not an IPv4 nor IPv6 address, cannot set port.\n");
00460    }
00461 }

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 632 of file netsock2.c.

References __LOG_DEBUG, __LOG_ERROR, ast_log, ast_sockaddr_isnull(), ast_sockaddr::len, option_debug, and ast_sockaddr::ss.

00634 {
00635    if (ast_sockaddr_isnull(addr)) {
00636       memset(sin, 0, sizeof(*sin));
00637       return 1;
00638    }
00639 
00640    if (addr->len != sizeof(*sin)) {
00641       ast_log(__LOG_ERROR, file, line, func, "Bad address cast to IPv4\n");
00642       return 0;
00643    }
00644 
00645    if (addr->ss.ss_family != AF_INET && option_debug >= 1) {
00646       ast_log(__LOG_DEBUG, file, line, func, "Address family is not AF_INET\n");
00647    }
00648 
00649    *sin = *(struct sockaddr_in *)&addr->ss;
00650    return 1;
00651 }

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 548 of file netsock2.c.

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

Referenced by ast_tcptls_server_root().

00549 {
00550    addr->len = sizeof(addr->ss);
00551    return accept(sockfd, (struct sockaddr *)&addr->ss, &addr->len);
00552 }

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 554 of file netsock2.c.

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

Referenced by ast_netsock_bindaddr(), ast_rtp_new(), ast_rtp_prop_set(), ast_tcptls_client_create(), ast_tcptls_server_start(), ast_udptl_new_with_bindaddr(), check_srcaddr(), and reload_config().

00555 {
00556    return bind(sockfd, (const struct sockaddr *)&addr->ss, addr->len);
00557 }

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 559 of file netsock2.c.

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

Referenced by ast_ouraddrfor(), ast_tcptls_client_start(), launch_netscript(), and stun_monitor_request().

00560 {
00561    return connect(sockfd, (const struct sockaddr *)&addr->ss, addr->len);
00562 }

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 564 of file netsock2.c.

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

Referenced by ast_ouraddrfor(), and ast_set_qos().

00565 {
00566    addr->len = sizeof(addr->ss);
00567    return getsockname(sockfd, (struct sockaddr *)&addr->ss, &addr->len);
00568 }

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 570 of file netsock2.c.

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

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

00572 {
00573    src_addr->len = sizeof(src_addr->ss);
00574    return recvfrom(sockfd, buf, len, flags,
00575          (struct sockaddr *)&src_addr->ss, &src_addr->len);
00576 }

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 578 of file netsock2.c.

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

Referenced by __rtp_sendto(), __sip_xmit(), ast_statsd_log_full(), ast_udptl_write(), hep_queue_cb(), multicast_rtp_write(), multicast_send_control_packet(), raw_hangup(), send_apathetic_reply(), send_packet(), sip_send_keepalive(), and transmit_trunk().

00580 {
00581    return sendto(sockfd, buf, len, flags,
00582             (const struct sockaddr *)&dest_addr->ss, dest_addr->len);
00583 }

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:
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.
Return values:
0 Success
-1 Error, with errno set to an appropriate value

Definition at line 585 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_netsock_bindaddr(), ast_netsock_set_qos(), ast_rtp_qos_set(), ast_udptl_setqos(), config_load(), load_module(), and reload_config().

00586 {
00587    int res = 0;
00588    int set_tos;
00589    int set_tclass;
00590    struct ast_sockaddr addr;
00591 
00592    /* If the sock address is IPv6, the TCLASS field must be set. */
00593    set_tclass = !ast_getsockname(sockfd, &addr) && ast_sockaddr_is_ipv6(&addr) ? 1 : 0;
00594 
00595    /* If the the sock address is IPv4 or (IPv6 set to any address [::]) set TOS bits */
00596    set_tos = (!set_tclass || (set_tclass && ast_sockaddr_is_any(&addr))) ? 1 : 0;
00597 
00598    if (set_tos) {
00599       if ((res = setsockopt(sockfd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)))) {
00600          ast_log(LOG_WARNING, "Unable to set %s DSCP TOS value to %d (may be you have no "
00601             "root privileges): %s\n", desc, tos, strerror(errno));
00602       } else if (tos) {
00603          ast_verb(2, "Using %s TOS bits %d\n", desc, tos);
00604       }
00605    }
00606 
00607 #if defined(IPV6_TCLASS) && defined(IPPROTO_IPV6)
00608    if (set_tclass) {
00609       if (!ast_getsockname(sockfd, &addr) && ast_sockaddr_is_ipv6(&addr)) {
00610          if ((res = setsockopt(sockfd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)))) {
00611             ast_log(LOG_WARNING, "Unable to set %s DSCP TCLASS field to %d (may be you have no "
00612                "root privileges): %s\n", desc, tos, strerror(errno));
00613          } else if (tos) {
00614             ast_verb(2, "Using %s TOS bits %d in TCLASS field.\n", desc, tos);
00615          }
00616       }
00617    }
00618 #endif
00619 
00620 #ifdef linux
00621    if (setsockopt(sockfd, SOL_SOCKET, SO_PRIORITY, &cos, sizeof(cos))) {
00622       ast_log(LOG_WARNING, "Unable to set %s CoS to %d: %s\n", desc, cos,
00623          strerror(errno));
00624    } else if (cos) {
00625       ast_verb(2, "Using %s CoS mark %d\n", desc, cos);
00626    }
00627 #endif
00628 
00629    return res;
00630 }

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:
addr The IP address to apply the mask to.
netmask The netmask configured in the host access rule.
result The resultant address after applying the netmask to the given address
Return values:
0 Successfully applied netmask
-1 Failed to apply netmask

Definition at line 338 of file netsock2.c.

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

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

00340 {
00341    int res = 0;
00342 
00343    if (ast_sockaddr_is_ipv4(addr)) {
00344       struct sockaddr_in result4 = { 0, };
00345       struct sockaddr_in *addr4 = (struct sockaddr_in *) &addr->ss;
00346       struct sockaddr_in *mask4 = (struct sockaddr_in *) &netmask->ss;
00347       result4.sin_family = AF_INET;
00348       result4.sin_addr.s_addr = addr4->sin_addr.s_addr & mask4->sin_addr.s_addr;
00349       ast_sockaddr_from_sin(result, &result4);
00350    } else if (ast_sockaddr_is_ipv6(addr)) {
00351       struct sockaddr_in6 result6 = { 0, };
00352       struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &addr->ss;
00353       struct sockaddr_in6 *mask6 = (struct sockaddr_in6 *) &netmask->ss;
00354       int i;
00355       result6.sin6_family = AF_INET6;
00356       for (i = 0; i < 4; ++i) {
00357          V6_WORD(&result6, i) = V6_WORD(addr6, i) & V6_WORD(mask6, i);
00358       }
00359       memcpy(&result->ss, &result6, sizeof(result6));
00360       result->len = sizeof(result6);
00361    } else {
00362       /* Unsupported address scheme */
00363       res = -1;
00364    }
00365 
00366    return res;
00367 }

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 132 of file netsock2.c.

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().

00133 {
00134    struct ast_sockaddr sa_ipv4;
00135    const struct ast_sockaddr *sa_tmp;
00136    int bits = 0;
00137    int bytes;
00138    int i;
00139    int j;
00140    char *addr;
00141 
00142    if (ast_sockaddr_isnull(sa)) {
00143       return 0;
00144    }
00145 
00146    if (ast_sockaddr_ipv4_mapped(sa, &sa_ipv4)) {
00147       sa_tmp = &sa_ipv4;
00148    } else {
00149       sa_tmp = sa;
00150    }
00151 
00152    bytes = sa_tmp->len;
00153    addr = ((struct sockaddr *)&sa_tmp->ss)->sa_data;
00154 
00155    for (i = 0; i < bytes ; ++i) {
00156       for (j = 0; j < 8; ++j) {
00157          if ((addr[i] >> j) & 1) {
00158             bits++;
00159          }
00160       }
00161    }
00162 
00163    return bits;
00164 }

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

Compares two ast_sockaddr structures.

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

Definition at line 369 of file netsock2.c.

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

Referenced by __rtp_recvfrom(), add_sdp(), ast_netsock_find(), ast_rtcp_read(), ast_rtp_instance_get_and_cmp_local_address(), ast_rtp_instance_get_and_cmp_requested_target_address(), ast_rtp_read(), ast_tcptls_client_create(), ast_tcptls_server_start(), AST_TEST_DEFINE(), ast_udptl_read(), dnsmgr_refresh(), find_tpeer(), iax2_ack_registry(), iax2_getpeername(), iax2_getpeertrunk(), match(), parse_register_contact(), pjsip_log_test_addr(), realtime_peer_by_name(), registry_rerequest(), reload_config(), rtcp_debug_test_addr(), rtp_debug_test_addr(), sip_debug_test_addr(), sip_uri_domain_cmp(), socket_process_helper(), threadinfo_locate_cb(), transmit_register(), udptl_debug_test_addr(), and update_registry().

00370 {
00371    const struct ast_sockaddr *a_tmp, *b_tmp;
00372    struct ast_sockaddr ipv4_mapped;
00373 
00374    a_tmp = a;
00375    b_tmp = b;
00376 
00377    if (a_tmp->len != b_tmp->len) {
00378       if (ast_sockaddr_ipv4_mapped(a, &ipv4_mapped)) {
00379          a_tmp = &ipv4_mapped;
00380       } else if (ast_sockaddr_ipv4_mapped(b, &ipv4_mapped)) {
00381          b_tmp = &ipv4_mapped;
00382       }
00383    }
00384 
00385    if (a_tmp->len < b_tmp->len) {
00386       return -1;
00387    } else if (a_tmp->len > b_tmp->len) {
00388       return 1;
00389    }
00390 
00391    return memcmp(&a_tmp->ss, &b_tmp->ss, a_tmp->len);
00392 }

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:
-1 a is lexicographically smaller than b
0 a is equal to b
1 b is lexicographically smaller than a

Definition at line 394 of file netsock2.c.

References ast_sockaddr_ipv4_mapped(), 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(), check_for_nat(), get_our_media_address(), iax_outputframe(), peer_ipcmp_cb_full(), peercnt_cmp_cb(), pjsip_log_test_addr(), realtime_peer(), realtime_user(), rtcp_debug_test_addr(), rtp_debug_test_addr(), sip_debug_test_addr(), socket_read(), and udptl_debug_test_addr().

00395 {
00396    const struct ast_sockaddr *a_tmp, *b_tmp;
00397    struct ast_sockaddr ipv4_mapped;
00398    const struct in_addr *ip4a, *ip4b;
00399    const struct in6_addr *ip6a, *ip6b;
00400    int ret = -1;
00401 
00402    a_tmp = a;
00403    b_tmp = b;
00404 
00405    if (a_tmp->len != b_tmp->len) {
00406       if (ast_sockaddr_ipv4_mapped(a, &ipv4_mapped)) {
00407          a_tmp = &ipv4_mapped;
00408       } else if (ast_sockaddr_ipv4_mapped(b, &ipv4_mapped)) {
00409          b_tmp = &ipv4_mapped;
00410       }
00411    }
00412 
00413    if (a->len < b->len) {
00414       ret = -1;
00415    } else if (a->len > b->len) {
00416       ret = 1;
00417    }
00418 
00419    switch (a_tmp->ss.ss_family) {
00420    case AF_INET:
00421       ip4a = &((const struct sockaddr_in*)&a_tmp->ss)->sin_addr;
00422       ip4b = &((const struct sockaddr_in*)&b_tmp->ss)->sin_addr;
00423       ret = memcmp(ip4a, ip4b, sizeof(*ip4a));
00424       break;
00425    case AF_INET6:
00426       ip6a = &((const struct sockaddr_in6*)&a_tmp->ss)->sin6_addr;
00427       ip6b = &((const struct sockaddr_in6*)&b_tmp->ss)->sin6_addr;
00428       ret = memcmp(ip6a, ip6b, sizeof(*ip6a));
00429       break;
00430    }
00431    return ret;
00432 }

static void ast_sockaddr_copy ( struct ast_sockaddr dst,
const struct ast_sockaddr src 
) [inline, static]

Copies the data from one ast_sockaddr to another.

Since:
1.8
Parameters:
dst The destination ast_sockaddr
src The source ast_sockaddr
Return values:
void 

Definition at line 141 of file netsock2.h.

References ast_sockaddr::len.

Referenced by __ast_http_load(), __find_callno(), __init_manager(), __rtp_recvfrom(), __sip_alloc(), add_sdp(), app_exec(), ast_copy_ha(), ast_find_ourip(), ast_netsock_bindaddr(), ast_rtcp_read(), ast_rtcp_write_report(), 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_incoming_source_address(), ast_rtp_instance_set_local_address(), ast_rtp_instance_set_requested_target_address(), ast_rtp_new(), ast_rtp_read(), ast_rtp_remote_address_set(), ast_sip_ouraddrfor(), 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(), check_peer_ok(), configure_local_rtp(), create_addr(), dialog_initialize_rtp(), dnsmgr_refresh(), find_tpeer(), get_our_media_address(), handle_cli_iax2_set_debug(), handle_cli_iax2_show_callno_limits(), handle_cli_udptl_set_debug(), handle_request_do(), iax2_ack_registry(), iax2_provision(), manager_http_callback(), mxml_http_callback(), on_dns_update_mwi(), on_dns_update_peer(), on_dns_update_registry(), parse_register_contact(), peercnt_add(), peercnt_modify(), peercnt_remove(), peercnt_remove_by_addr(), rawman_http_callback(), reg_source_db(), reload_config(), resolve_first(), sched_delay_remove(), session_do(), set_peercnt_limit(), sip_do_debug_peer(), sip_find_peer_full(), sip_prepare_socket(), socket_process_helper(), socket_read(), transmit_response_using_temp(), try_transfer(), update_registry(), websocket_client_args_create(), and websocket_client_connect().

00143 {
00144    memcpy(dst, src, src->len);
00145    dst->len = src->len;
00146 };

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:
0 Unknown address family
other A 32-bit hash derived from the address

Definition at line 512 of file netsock2.c.

References ast_log, LOG_ERROR, and ast_sockaddr::ss.

Referenced by addr_range_hash_cb(), peer_iphash_cb(), peercnt_hash_cb(), and threadt_hash_cb().

00513 {
00514    /*
00515     * For IPv4, return the IP address as-is. For IPv6, return the last 32
00516     * bits.
00517     */
00518    switch (addr->ss.ss_family) {
00519    case AF_INET:
00520       return ((const struct sockaddr_in *)&addr->ss)->sin_addr.s_addr;
00521    case AF_INET6:
00522       return ((uint32_t *)&((const struct sockaddr_in6 *)&addr->ss)->sin6_addr)[3];
00523    default:
00524       ast_log(LOG_ERROR, "Unknown address family '%d'.\n",
00525          addr->ss.ss_family);
00526       return 0;
00527    }
00528 }

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 463 of file netsock2.c.

References ast_sockaddr::ss.

Referenced by ast_sockaddr_is_ipv4_multicast(), and multicast_send_control_packet().

00464 {
00465    const struct sockaddr_in *sin = (struct sockaddr_in *)&addr->ss;
00466    return ntohl(sin->sin_addr.s_addr);
00467 }

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:
addr The IPv4-mapped address to convert
ast_mapped The resulting IPv4 address
Return values:
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_rtcp_read(), ast_rtp_read(), ast_sockaddr_cidr_bits(), ast_sockaddr_cmp(), ast_sockaddr_cmp_addr(), and ast_sockaddr_stringify_fmt().

00040 {
00041    const struct sockaddr_in6 *sin6;
00042    struct sockaddr_in sin4;
00043 
00044    if (!ast_sockaddr_is_ipv6(addr)) {
00045       return 0;
00046    }
00047 
00048    if (!ast_sockaddr_is_ipv4_mapped(addr)) {
00049       return 0;
00050    }
00051 
00052    sin6 = (const struct sockaddr_in6*)&addr->ss;
00053 
00054    memset(&sin4, 0, sizeof(sin4));
00055    sin4.sin_family = AF_INET;
00056    sin4.sin_port = sin6->sin6_port;
00057    sin4.sin_addr.s_addr = ((uint32_t *)&sin6->sin6_addr)[3];
00058 
00059    ast_sockaddr_from_sin(ast_mapped, &sin4);
00060 
00061    return 1;
00062 }

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:
1 This is an "any" address
0 This is not an "any" address

Definition at line 498 of file netsock2.c.

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

Referenced by apply_negotiated_sdp_stream(), ast_find_ourip(), ast_set_qos(), ast_sip_ouraddrfor(), get_address_family_filter(), get_our_media_address(), reload_config(), sip_show_settings(), and sockaddr_is_null_or_any().

00499 {
00500    union {
00501       struct sockaddr_storage ss;
00502       struct sockaddr_in sin;
00503       struct sockaddr_in6 sin6;
00504    } tmp_addr = {
00505       .ss = addr->ss,
00506    };
00507 
00508    return (ast_sockaddr_is_ipv4(addr) && (tmp_addr.sin.sin_addr.s_addr == INADDR_ANY)) ||
00509        (ast_sockaddr_is_ipv6(addr) && IN6_IS_ADDR_UNSPECIFIED(&tmp_addr.sin6.sin6_addr));
00510 }

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:
1 This is an IPv4 address
0 This is an IPv6 or IPv4-mapped IPv6 address

Definition at line 469 of file netsock2.c.

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

Referenced by ast_append_ha(), ast_apply_ha(), ast_json_ipaddr(), ast_rtcp_read(), ast_rtp_new(), ast_rtp_prop_set(), ast_rtp_read(), ast_sockaddr_apply_netmask(), ast_sockaddr_is_any(), dump_addr(), hep_queue_cb(), jingle_interpret_ice_udp_transport(), and negotiate_incoming_sdp_stream().

00470 {
00471    return addr->ss.ss_family == AF_INET &&
00472        addr->len == sizeof(struct sockaddr_in);
00473 }

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:
1 This is an IPv4-mapped IPv6 address.
0 This is not an IPv4-mapped IPv6 address.

Definition at line 475 of file netsock2.c.

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

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

00476 {
00477    const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&addr->ss;
00478    return addr->len && IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr);
00479 }

int ast_sockaddr_is_ipv4_multicast ( const struct ast_sockaddr addr  ) 

Determine if an IPv4 address is a multicast address.

Since:
10.0
Parameters:
addr the 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 481 of file netsock2.c.

References ast_sockaddr_ipv4().

Referenced by process_via().

00482 {
00483    return ((ast_sockaddr_ipv4(addr) & 0xf0000000) == 0xe0000000);
00484 }

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:
1 This is an IPv6 or IPv4-mapped IPv6 address.
0 This is an IPv4 address.

Definition at line 492 of file netsock2.c.

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

Referenced by add_sdp(), ast_apply_ha(), ast_netsock_bindaddr(), ast_ouraddrfor(), ast_rtp_new(), ast_rtp_prop_set(), ast_set_qos(), ast_sip_ouraddrfor(), 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(), get_address_family_filter(), hepv3_data_alloc(), multicast_send_control_packet(), negotiate_incoming_sdp_stream(), reload_config(), and sip_show_settings().

00493 {
00494    return addr->ss.ss_family == AF_INET6 &&
00495        addr->len == sizeof(struct sockaddr_in6);
00496 }

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:
1 This is a link-local IPv6 address.
0 This is link-local IPv6 address.

Definition at line 486 of file netsock2.c.

References ast_sockaddr_is_ipv6(), and ast_sockaddr::ss.

Referenced by ast_sockaddr_stringify_fmt().

00487 {
00488    const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&addr->ss;
00489    return ast_sockaddr_is_ipv6(addr) && IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr);
00490 }

static int ast_sockaddr_isnull ( const struct ast_sockaddr addr  )  [inline, static]

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

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

Definition at line 113 of file netsock2.h.

References ast_sockaddr::len.

Referenced by __ast_http_load(), __iax2_do_register_s(), __iax2_show_peers(), __init_manager(), __rtp_recvfrom(), _ast_sockaddr_to_sin(), _sip_show_peers_one(), acf_channel_read(), add_ice_to_sdp(), add_ice_to_stream(), add_sdp(), apply_negotiated_sdp_stream(), ast_rtcp_write_report(), ast_rtp_dtmf_begin(), ast_rtp_dtmf_continuation(), ast_rtp_dtmf_end_with_duration(), ast_rtp_raw_write(), ast_rtp_read(), ast_rtp_remote_address_set(), ast_rtp_sendcng(), ast_rtp_write(), ast_sip_ouraddrfor(), 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(), check_rtp_timeout(), create_addr(), create_addr_from_peer(), create_outgoing_sdp_stream(), dialog_initialize_rtp(), do_monitor(), expire_register(), function_iaxpeer(), get_our_media_address(), handle_cli_iax2_show_registry(), handle_response_invite(), handle_show_http(), iax2_ack_registry(), iax2_devicestate(), iax2_do_register(), iax2_poke_peer(), iax2_prov_app(), iax_outputframe(), jingle_interpret_ice_udp_transport(), manager_iax2_show_registry(), multicast_send_control_packet(), nat_on_tx_message(), on_dns_update_mwi(), on_dns_update_peer(), on_dns_update_registry(), ooh323_indicate(), ooh323_set_rtp_peer(), parse_register_contact(), peer_iphash_cb(), peercnt_hash_cb(), pjsip_log_test_addr(), process_sdp(), realtime_peer(), realtime_update_peer(), realtime_user(), register_verify(), reload_config(), rtcp_debug_test_addr(), rtp_debug_test_addr(), set_caps(), sip_acf_channel_read(), sip_debug_test_addr(), sip_devicestate(), sip_do_debug_peer(), sip_poke_peer(), sip_prune_realtime(), sip_send_keepalive(), sip_send_mwi_to_peer(), sip_set_rtp_peer(), sip_show_channel(), sip_show_settings(), sockaddr_is_null_or_any(), socket_process_helper(), transmit_register(), try_transfer(), udptl_debug_test_addr(), and update_registry().

00114 {
00115    return !addr || addr->len == 0;
00116 }

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] [a:b:c:...:d]:port

Host names are NOT allowed.

Parameters:
[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.
Return values:
1 Success
0 Failure

Definition at line 232 of file netsock2.c.

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

Referenced by __init_manager(), apply_acl(), ast_append_ha(), ast_netsock_bind(), ast_parse_arg(), AST_TEST_DEFINE(), build_peer(), change_outgoing_sdp_stream_media_address(), 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(), hepv3_data_alloc(), internal_dnsmgr_lookup(), ip_identify(), jingle_alloc(), jingle_enable_video(), jingle_interpret_google_transport(), jingle_interpret_ice_udp_transport(), load_module(), logging_on_rx_msg(), logging_on_tx_msg(), multicast_rtp_request(), nat_on_tx_message(), peer_set_srcaddr(), pjsip_log_test_addr(), process_ice_attributes(), process_sdp_a_ice(), proxy_update(), realtime_peer(), reg_source_db(), reload_config(), rtcp_do_debug_ip(), rtcp_message_handler(), rtp_do_debug_ip(), security_event_populate(), session_outgoing_nat_hook(), set_config(), sip_sanitized_host(), sip_uri_domain_cmp(), and unicast_rtp_request().

00233 {
00234    struct addrinfo hints;
00235    struct addrinfo   *res;
00236    char *s;
00237    char *host;
00238    char *port;
00239    int   e;
00240 
00241    s = ast_strdupa(str);
00242    if (!ast_sockaddr_split_hostport(s, &host, &port, flags)) {
00243       return 0;
00244    }
00245 
00246    memset(&hints, 0, sizeof(hints));
00247    /* Hint to get only one entry from getaddrinfo */
00248    hints.ai_socktype = SOCK_DGRAM;
00249 
00250 #ifdef AI_NUMERICSERV
00251    hints.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;
00252 #else
00253    hints.ai_flags = AI_NUMERICHOST;
00254 #endif
00255    if ((e = getaddrinfo(host, port, &hints, &res))) {
00256       if (e != EAI_NONAME) { /* if this was just a host name rather than a ip address, don't print error */
00257          ast_log(LOG_ERROR, "getaddrinfo(\"%s\", \"%s\", ...): %s\n",
00258             host, S_OR(port, "(null)"), gai_strerror(e));
00259       }
00260       return 0;
00261    }
00262 
00263    /*
00264     * I don't see how this could be possible since we're not resolving host
00265     * names. But let's be careful...
00266     */
00267    if (res->ai_next != NULL) {
00268       ast_log(LOG_WARNING, "getaddrinfo() returned multiple "
00269          "addresses. Ignoring all but the first.\n");
00270    }
00271 
00272    if (addr) {
00273       addr->len = res->ai_addrlen;
00274       memcpy(&addr->ss, res->ai_addr, addr->len);
00275    }
00276 
00277    freeaddrinfo(res);
00278 
00279    return 1;
00280 }

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] [a:b:c:...:d]:port

Parameters:
[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_AF_UNSPEC to get addresses of all families.
Return values:
0 Failure
non-zero The number of elements in addrs array.

Definition at line 282 of file netsock2.c.

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

Referenced by __ast_http_load(), app_exec(), apply_negotiated_sdp_stream(), ast_sockaddr_resolve_first_af(), extract_contact_addr(), handle_cli_udptl_set_debug(), ip_identify_match_handler(), launch_netscript(), negotiate_incoming_sdp_stream(), realtime_peer(), realtime_peer_by_name(), realtime_user(), resolve_first(), and websocket_client_args_create().

00284 {
00285    struct addrinfo hints, *res, *ai;
00286    char *s, *host, *port;
00287    int   e, i, res_cnt;
00288 
00289    if (!str) {
00290       *addrs = NULL;
00291       return 0;
00292    }
00293 
00294    s = ast_strdupa(str);
00295    if (!ast_sockaddr_split_hostport(s, &host, &port, flags)) {
00296       *addrs = NULL;
00297       return 0;
00298    }
00299 
00300    memset(&hints, 0, sizeof(hints));
00301    hints.ai_family = family;
00302    hints.ai_socktype = SOCK_DGRAM;
00303 
00304    if ((e = getaddrinfo(host, port, &hints, &res))) {
00305       ast_log(LOG_ERROR, "getaddrinfo(\"%s\", \"%s\", ...): %s\n",
00306          host, S_OR(port, "(null)"), gai_strerror(e));
00307       *addrs = NULL;
00308       return 0;
00309    }
00310 
00311    res_cnt = 0;
00312    for (ai = res; ai; ai = ai->ai_next) {
00313       res_cnt++;
00314    }
00315 
00316    if (res_cnt == 0) {
00317       *addrs = NULL;
00318       goto cleanup;
00319    }
00320 
00321    if ((*addrs = ast_malloc(res_cnt * sizeof(struct ast_sockaddr))) == NULL) {
00322       res_cnt = 0;
00323       goto cleanup;
00324    }
00325 
00326    i = 0;
00327    for (ai = res; ai; ai = ai->ai_next) {
00328       (*addrs)[i].len = ai->ai_addrlen;
00329       memcpy(&(*addrs)[i].ss, ai->ai_addr, ai->ai_addrlen);
00330       ++i;
00331    }
00332 
00333 cleanup:
00334    freeaddrinfo(res);
00335    return res_cnt;
00336 }

static void ast_sockaddr_setnull ( struct ast_sockaddr addr  )  [inline, static]

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] str The string to parse. May be modified by writing a NUL at the end of the host part.
[out] host Pointer to the host component within str.
[out] port 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.
Return values:
1 Success
0 Failure

Definition at line 166 of file netsock2.c.

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

Referenced by ast_sockaddr_parse(), ast_sockaddr_resolve(), extract_host_from_hostport(), peer_set_srcaddr(), rtcp_do_debug_ip(), rtp_do_debug_ip(), setup_stunaddr(), and sip_parse_host().

00167 {
00168    char *s = str;
00169    char *orig_str = str;/* Original string in case the port presence is incorrect. */
00170    char *host_end = NULL;/* Delay terminating the host in case the port presence is incorrect. */
00171 
00172    ast_debug(5, "Splitting '%s' into...\n", str);
00173    *host = NULL;
00174    *port = NULL;
00175    if (*s == '[') {
00176       *host = ++s;
00177       for (; *s && *s != ']'; ++s) {
00178       }
00179       if (*s == ']') {
00180          host_end = s;
00181          ++s;
00182       }
00183       if (*s == ':') {
00184          *port = s + 1;
00185       }
00186    } else {
00187       *host = s;
00188       for (; *s; ++s) {
00189          if (*s == ':') {
00190             if (*port) {
00191                *port = NULL;
00192                break;
00193             } else {
00194                *port = s;
00195             }
00196          }
00197       }
00198       if (*port) {
00199          host_end = *port;
00200          ++*port;
00201       }
00202    }
00203 
00204    switch (flags & PARSE_PORT_MASK) {
00205    case PARSE_PORT_IGNORE:
00206       *port = NULL;
00207       break;
00208    case PARSE_PORT_REQUIRE:
00209       if (*port == NULL) {
00210          ast_log(LOG_WARNING, "Port missing in %s\n", orig_str);
00211          return 0;
00212       }
00213       break;
00214    case PARSE_PORT_FORBID:
00215       if (*port != NULL) {
00216          ast_log(LOG_WARNING, "Port disallowed in %s\n", orig_str);
00217          return 0;
00218       }
00219       break;
00220    }
00221 
00222    /* Can terminate the host string now if needed. */
00223    if (host_end) {
00224       *host_end = '\0';
00225    }
00226    ast_debug(5, "...host '%s' and port '%s'.\n", *host, *port ? *port : "");
00227    return 1;
00228 }

static char* ast_sockaddr_stringify ( const struct ast_sockaddr addr  )  [inline, static]

Wrapper around ast_sockaddr_stringify_fmt() with default format.

Since:
1.8
Returns:
same as ast_sockaddr_stringify_fmt()

Definition at line 230 of file netsock2.h.

References AST_SOCKADDR_STR_DEFAULT, and ast_sockaddr_stringify_fmt().

Referenced by __ast_http_load(), __find_callno(), __rtp_recvfrom(), __sip_xmit(), _sip_show_peer(), add_sdp(), apply_acl(), apply_contact_acl(), ast_append_ha(), ast_apply_ha(), ast_netsock_bindaddr(), ast_parse_arg(), ast_rtcp_read(), ast_rtcp_write_report(), ast_rtp_dtmf_begin(), ast_rtp_dtmf_continuation(), ast_rtp_dtmf_end_with_duration(), ast_rtp_raw_write(), ast_rtp_read(), ast_rtp_sendcng(), ast_sip_ouraddrfor(), ast_tcptls_client_create(), ast_tcptls_client_start(), ast_tcptls_server_start(), AST_TEST_DEFINE(), ast_udptl_read(), ast_udptl_write(), ast_websocket_uri_cb(), bridge_p2p_rtp_write(), channel_read_rtp(), check_peer_ok(), check_via(), create_dtmf_frame(), dnsmgr_refresh(), do_message_auth(), dump_addr(), dump_ipaddr(), find_tpeer(), handle_call_token(), handle_cli_iax2_show_callno_limits(), handle_cli_iax2_show_registry(), handle_cli_udptl_set_debug(), handle_connection(), handle_incoming(), handle_manager_show_settings(), handle_request_bye(), handle_request_do(), handle_request_invite(), handle_request_register(), handle_request_subscribe(), handle_response(), handle_response_info(), handle_response_message(), handle_response_notify(), handle_response_refer(), handle_show_http(), iax2_ack_registry(), iax2_devicestate(), iax2_prov_app(), iax2_trunk_queue(), iax_showframe(), ip_identify(), ip_identify_match_check(), launch_netscript(), manager_iax2_show_registry(), multicast_rtp_write(), on_dns_update_mwi(), on_dns_update_peer(), on_dns_update_registry(), parse_register_contact(), process_cn_rfc3389(), process_dtmf_rfc2833(), process_sdp(), raw_hangup(), receive_message(), reg_source_db(), register_verify(), registry_rerequest(), reload_config(), retrans_pkt(), rtcp_do_debug_ip(), rtp_do_debug_ip(), send_packet(), send_request(), send_response(), session_destroy_fn(), set_config(), set_destination(), set_peercnt_limit(), sip_acf_channel_read(), sip_allow_anyrtp_remote(), sip_send_keepalive(), sip_set_rtp_peer(), sip_show_channel(), sip_show_settings(), sip_show_tcp(), sip_tcptls_read(), sockaddr_handler_fn(), socket_process_helper(), socket_process_meta(), stun_monitor_request(), timing_read(), transport_create(), transport_read(), and update_registry().

00231 {
00232    return ast_sockaddr_stringify_fmt(addr, AST_SOCKADDR_STR_DEFAULT);
00233 }

static char* ast_sockaddr_stringify_addr ( const struct ast_sockaddr addr  )  [inline, static]

Wrapper around ast_sockaddr_stringify_fmt() to return an address only.

Since:
1.8
Returns:
same as ast_sockaddr_stringify_fmt()

Definition at line 260 of file netsock2.h.

References AST_SOCKADDR_STR_ADDR, and ast_sockaddr_stringify_fmt().

Referenced by __attempt_transmit(), __init_manager(), _iax2_show_peers_one(), _sip_show_peer(), _sip_show_peers_one(), acf_channel_read(), action_hangup(), action_login(), add_sdp(), ast_apply_acl(), ast_ha_join(), ast_ha_join_cidr(), ast_json_ipaddr(), ast_ouraddrfor(), auth_http_callback(), authenticate(), build_peer(), build_user(), calltoken_required(), check_access(), check_for_nat(), cli_display_named_acl(), cli_print_body(), configure_local_rtp(), do_message(), function_iaxpeer(), function_sippeer(), generic_http_callback(), get_input(), handle_cli_iax2_show_channels(), handle_cli_iax2_show_peer(), handle_showmanconn(), hep_queue_cb(), httpstatus_callback(), ip_identify_match_handler(), localnet_to_vl(), match_to_var_list(), ooh323_get_rtp_peer(), ooh323_set_rtp_peer(), parse_register_contact(), peercnt_add(), peercnt_modify(), peercnt_remove(), peers_data_provider_get(), pjsip_enable_logger_host(), purge_sessions(), realtime_peer(), realtime_update_peer(), realtime_user(), register_verify(), reload_config(), sched_delay_remove(), session_do(), show_channels_cb(), show_chanstats_cb(), sip_acf_channel_read(), sip_do_debug_ip(), sip_do_debug_peer(), sip_show_channel(), sip_show_settings(), unicast_rtp_request(), and update_registry().

00261 {
00262    return ast_sockaddr_stringify_fmt(addr, AST_SOCKADDR_STR_ADDR);
00263 }

static char* ast_sockaddr_stringify_addr_remote ( const struct ast_sockaddr addr  )  [inline, static]

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 287 of file netsock2.h.

References AST_SOCKADDR_STR_ADDR_REMOTE, and ast_sockaddr_stringify_fmt().

Referenced by add_ice_to_stream(), add_sdp(), and copy_via_headers().

00288 {
00289    return ast_sockaddr_stringify_fmt(addr, AST_SOCKADDR_STR_ADDR_REMOTE);
00290 }

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:
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
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
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(), e, host, 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().

00068 {
00069    struct ast_sockaddr sa_ipv4;
00070    const struct ast_sockaddr *sa_tmp;
00071    char host[NI_MAXHOST];
00072    char port[NI_MAXSERV];
00073    struct ast_str *str;
00074    int e;
00075    static const size_t size = sizeof(host) - 1 + sizeof(port) - 1 + 4;
00076 
00077 
00078    if (ast_sockaddr_isnull(sa)) {
00079       return "(null)";
00080    }
00081 
00082    if (!(str = ast_str_thread_get(&ast_sockaddr_stringify_buf, size))) {
00083       return "";
00084    }
00085 
00086    if (ast_sockaddr_ipv4_mapped(sa, &sa_ipv4)) {
00087       sa_tmp = &sa_ipv4;
00088    } else {
00089       sa_tmp = sa;
00090    }
00091 
00092    if ((e = getnameinfo((struct sockaddr *)&sa_tmp->ss, sa_tmp->len,
00093               format & AST_SOCKADDR_STR_ADDR ? host : NULL,
00094               format & AST_SOCKADDR_STR_ADDR ? sizeof(host) : 0,
00095               format & AST_SOCKADDR_STR_PORT ? port : 0,
00096               format & AST_SOCKADDR_STR_PORT ? sizeof(port): 0,
00097               NI_NUMERICHOST | NI_NUMERICSERV))) {
00098       ast_log(LOG_ERROR, "getnameinfo(): %s\n", gai_strerror(e));
00099       return "";
00100    }
00101 
00102    if ((format & AST_SOCKADDR_STR_REMOTE) == AST_SOCKADDR_STR_REMOTE) {
00103       char *p;
00104       if (ast_sockaddr_is_ipv6_link_local(sa) && (p = strchr(host, '%'))) {
00105          *p = '\0';
00106       }
00107    }
00108 
00109    switch ((format & AST_SOCKADDR_STR_FORMAT_MASK))  {
00110    case AST_SOCKADDR_STR_DEFAULT:
00111       ast_str_set(&str, 0, sa_tmp->ss.ss_family == AF_INET6 ?
00112             "[%s]:%s" : "%s:%s", host, port);
00113       break;
00114    case AST_SOCKADDR_STR_ADDR:
00115       ast_str_set(&str, 0, "%s", host);
00116       break;
00117    case AST_SOCKADDR_STR_HOST:
00118       ast_str_set(&str, 0,
00119              sa_tmp->ss.ss_family == AF_INET6 ? "[%s]" : "%s", host);
00120       break;
00121    case AST_SOCKADDR_STR_PORT:
00122       ast_str_set(&str, 0, "%s", port);
00123       break;
00124    default:
00125       ast_log(LOG_ERROR, "Invalid format\n");
00126       return "";
00127    }
00128 
00129    return ast_str_buffer(str);
00130 }

static char* ast_sockaddr_stringify_host ( const struct ast_sockaddr addr  )  [inline, static]

static char* ast_sockaddr_stringify_host_remote ( const struct ast_sockaddr addr  )  [inline, static]

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 319 of file netsock2.h.

References AST_SOCKADDR_STR_HOST_REMOTE, and ast_sockaddr_stringify_fmt().

Referenced by add_diversion(), add_rpid(), build_callid_registry(), build_reply_digest(), create_addr_from_peer(), initreqprep(), sip_poke_peer(), sip_sanitized_host(), and transmit_notify_with_mwi().

00320 {
00321    return ast_sockaddr_stringify_fmt(addr, AST_SOCKADDR_STR_HOST_REMOTE);
00322 }

static char* ast_sockaddr_stringify_port ( const struct ast_sockaddr addr  )  [inline, static]

static char* ast_sockaddr_stringify_remote ( const struct ast_sockaddr addr  )  [inline, static]

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 247 of file netsock2.h.

References AST_SOCKADDR_STR_DEFAULT_REMOTE, and ast_sockaddr_stringify_fmt().

Referenced by build_callid_pvt(), build_contact(), build_via(), and generate_uri().

00248 {
00249    return ast_sockaddr_stringify_fmt(addr, AST_SOCKADDR_STR_DEFAULT_REMOTE);
00250 }

const char* ast_transport2str ( enum ast_transport  transport  ) 

Returns a string representation of an ast_transport.

Since:
12.3
Return values:
Name of the tranpsort if it is defined
Undefined if the transport is undefined

Definition at line 530 of file netsock2.c.

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

Referenced by sip_show_channel().

00531 {
00532    switch (transport) {
00533    case AST_TRANSPORT_TLS:
00534       return "TLS";
00535    case AST_TRANSPORT_UDP:
00536       return "UDP";
00537    case AST_TRANSPORT_TCP:
00538       return "TCP";
00539    case AST_TRANSPORT_WS:
00540       return "WS";
00541    case AST_TRANSPORT_WSS:
00542       return "WSS";
00543    }
00544 
00545    return "Undefined";
00546 }


Generated on Thu Apr 16 06:32:05 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6