Network socket handling. More...
#include "asterisk.h"#include <ifaddrs.h>#include "asterisk/netsock.h"#include "asterisk/netsock2.h"#include "asterisk/utils.h"#include "asterisk/astobj.h"
Go to the source code of this file.
Data Structures | |
| struct | ast_netsock |
| struct | ast_netsock_list |
Functions | |
| int | ast_eid_cmp (const struct ast_eid *eid1, const struct ast_eid *eid2) |
| Compare two EIDs. | |
| char * | ast_eid_to_str (char *s, int maxlen, struct ast_eid *eid) |
| Convert an EID to a string. | |
| struct ast_netsock * | ast_netsock_bind (struct ast_netsock_list *list, struct io_context *ioc, const char *bindinfo, int defaultport, int tos, int cos, ast_io_cb callback, void *data) |
| struct ast_netsock * | ast_netsock_bindaddr (struct ast_netsock_list *list, struct io_context *ioc, struct ast_sockaddr *bindaddr, int tos, int cos, ast_io_cb callback, void *data) |
| struct ast_sockaddr * | ast_netsock_boundaddr (const struct ast_netsock *ns) |
| void * | ast_netsock_data (const struct ast_netsock *ns) |
| static void | ast_netsock_destroy (struct ast_netsock *netsock) |
| struct ast_netsock * | ast_netsock_find (struct ast_netsock_list *list, struct ast_sockaddr *addr) |
| int | ast_netsock_init (struct ast_netsock_list *list) |
| struct ast_netsock_list * | ast_netsock_list_alloc (void) |
| int | ast_netsock_release (struct ast_netsock_list *list) |
| int | ast_netsock_set_qos (int sockfd, int tos, int cos, const char *desc) |
| int | ast_netsock_sockfd (const struct ast_netsock *ns) |
| void | ast_netsock_unref (struct ast_netsock *ns) |
| void | ast_set_default_eid (struct ast_eid *eid) |
| Fill in an ast_eid with the default eid of this machine. | |
| int | ast_str_to_eid (struct ast_eid *eid, const char *s) |
| Convert a string into an EID. | |
Network socket handling.
Definition in file netsock.c.
| int ast_eid_cmp | ( | const struct ast_eid * | eid1, |
| const struct ast_eid * | eid2 | ||
| ) |
Compare two EIDs.
Definition at line 307 of file netsock.c.
Referenced by aji_devstate_cb(), aji_handle_pubsub_event(), aji_mwi_cb(), ast_event_cb(), build_peer(), build_transactions(), cpg_deliver_cb(), destroy_trans(), dundi_answer_entity(), dundi_answer_query(), dundi_ie_append_eid_appropriately(), dundi_lookup_internal(), dundi_prop_precache(), dundi_query_thread(), find_peer(), handle_command_response(), optimize_transactions(), register_request(), xmpp_pubsub_devstate_cb(), xmpp_pubsub_handle_event(), and xmpp_pubsub_mwi_cb().
{
return memcmp(eid1, eid2, sizeof(*eid1));
}
| char* ast_eid_to_str | ( | char * | s, |
| int | maxlen, | ||
| struct ast_eid * | eid | ||
| ) |
Convert an EID to a string.
Definition at line 209 of file netsock.c.
References ast_eid::eid.
Referenced by aji_publish_device_state(), aji_publish_mwi(), append_transaction(), ast_event_cb(), ast_set_default_eid(), ast_str_retrieve_variable(), build_peer(), build_transactions(), cache_lookup(), cache_lookup_internal(), check_key(), complete_peer_helper(), cpg_deliver_cb(), destroy_trans(), do_autokill(), do_register(), do_register_expire(), dump_answer(), dump_eid(), dump_raw_ie(), dundi_answer_entity(), dundi_lookup_internal(), dundi_lookup_local(), dundi_lookup_thread(), dundi_precache_thread(), dundi_prop_precache(), dundi_query_thread(), dundi_send(), dundi_show_cache(), dundi_show_entityid(), dundi_show_hints(), dundi_show_peer(), dundi_show_peers(), dundi_show_requests(), handle_command_response(), handle_show_settings(), populate_addr(), register_request(), update_key(), xmpp_pubsub_publish_device_state(), and xmpp_pubsub_publish_mwi().
| struct ast_netsock* ast_netsock_bind | ( | struct ast_netsock_list * | list, |
| struct io_context * | ioc, | ||
| const char * | bindinfo, | ||
| int | defaultport, | ||
| int | tos, | ||
| int | cos, | ||
| ast_io_cb | callback, | ||
| void * | data | ||
| ) | [read] |
Definition at line 169 of file netsock.c.
References ast_log(), ast_netsock_bindaddr(), ast_sockaddr_is_ipv4(), ast_sockaddr_parse(), ast_sockaddr_port, ast_sockaddr_set_port, and LOG_WARNING.
Referenced by peer_set_srcaddr(), and set_config().
{
struct ast_sockaddr addr;
if (ast_sockaddr_parse(&addr, bindinfo, 0)) {
if (!ast_sockaddr_is_ipv4(&addr)) {
ast_log(LOG_WARNING, "Only IPv4 addresses are supported at this time.\n");
return NULL;
}
if (!ast_sockaddr_port(&addr)) {
ast_sockaddr_set_port(&addr, defaultport);
}
return ast_netsock_bindaddr(list, ioc, &addr, tos, cos, callback, data);
}
return NULL;
}
| struct ast_netsock* ast_netsock_bindaddr | ( | struct ast_netsock_list * | list, |
| struct io_context * | ioc, | ||
| struct ast_sockaddr * | bindaddr, | ||
| int | tos, | ||
| int | cos, | ||
| ast_io_cb | callback, | ||
| void * | data | ||
| ) | [read] |
Definition at line 111 of file netsock.c.
References ast_bind(), ast_calloc, ast_enable_packet_fragmentation(), ast_free, ast_io_add(), AST_IO_IN, ast_log(), ast_set_qos(), ast_sockaddr_stringify(), ASTOBJ_CONTAINER_LINK, ASTOBJ_INIT, ast_netsock::bindaddr, ast_netsock::data, errno, ast_netsock::ioc, ast_netsock::ioref, LOG_ERROR, LOG_WARNING, netsocket, and ast_netsock::sockfd.
Referenced by ast_netsock_bind().
{
int netsocket = -1;
int *ioref;
struct ast_netsock *ns;
const int reuseFlag = 1;
/* Make a UDP socket */
netsocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
if (netsocket < 0) {
ast_log(LOG_ERROR, "Unable to create network socket: %s\n", strerror(errno));
return NULL;
}
if (setsockopt(netsocket, SOL_SOCKET, SO_REUSEADDR, (char *)&reuseFlag, sizeof reuseFlag) < 0) {
ast_log(LOG_WARNING, "Error setting SO_REUSEADDR on sockfd '%d'\n", netsocket);
}
if (ast_bind(netsocket, bindaddr)) {
ast_log(LOG_ERROR,
"Unable to bind to %s: %s\n",
ast_sockaddr_stringify(bindaddr),
strerror(errno));
close(netsocket);
return NULL;
}
ast_set_qos(netsocket, tos, cos, "IAX2");
ast_enable_packet_fragmentation(netsocket);
if (!(ns = ast_calloc(1, sizeof(*ns)))) {
close(netsocket);
return NULL;
}
/* Establish I/O callback for socket read */
if (!(ioref = ast_io_add(ioc, netsocket, callback, AST_IO_IN, ns))) {
close(netsocket);
ast_free(ns);
return NULL;
}
ASTOBJ_INIT(ns);
ns->ioref = ioref;
ns->ioc = ioc;
ns->sockfd = netsocket;
ns->data = data;
memcpy(&ns->bindaddr, bindaddr, sizeof(ns->bindaddr));
ASTOBJ_CONTAINER_LINK(list, ns);
return ns;
}
| struct ast_sockaddr* ast_netsock_boundaddr | ( | const struct ast_netsock * | ns | ) | [read] |
Definition at line 194 of file netsock.c.
References ast_netsock::bindaddr.
{
return &ns->bindaddr;
}
| void* ast_netsock_data | ( | const struct ast_netsock * | ns | ) |
| static void ast_netsock_destroy | ( | struct ast_netsock * | netsock | ) | [static] |
Definition at line 67 of file netsock.c.
References ast_free, ast_io_remove(), ast_netsock::ioc, ast_netsock::ioref, and ast_netsock::sockfd.
Referenced by ast_netsock_release(), and ast_netsock_unref().
{
ast_io_remove(netsock->ioc, netsock->ioref);
close(netsock->sockfd);
ast_free(netsock);
}
| struct ast_netsock* ast_netsock_find | ( | struct ast_netsock_list * | list, |
| struct ast_sockaddr * | addr | ||
| ) | [read] |
Definition at line 96 of file netsock.c.
References ast_sockaddr_cmp(), ASTOBJ_CONTAINER_TRAVERSE, ASTOBJ_RDLOCK, and ASTOBJ_UNLOCK.
Referenced by peer_set_srcaddr().
{
struct ast_netsock *sock = NULL;
ASTOBJ_CONTAINER_TRAVERSE(list, !sock, {
ASTOBJ_RDLOCK(iterator);
if (!ast_sockaddr_cmp(&iterator->bindaddr, addr)) {
sock = iterator;
}
ASTOBJ_UNLOCK(iterator);
});
return sock;
}
| int ast_netsock_init | ( | struct ast_netsock_list * | list | ) |
Definition at line 79 of file netsock.c.
References ASTOBJ_CONTAINER_INIT.
Referenced by load_module(), and set_config().
{
memset(list, 0, sizeof(*list));
ASTOBJ_CONTAINER_INIT(list);
return 0;
}
| struct ast_netsock_list* ast_netsock_list_alloc | ( | void | ) | [read] |
Definition at line 74 of file netsock.c.
References ast_calloc.
Referenced by load_module(), and set_config().
{
return ast_calloc(1, sizeof(struct ast_netsock_list));
}
| int ast_netsock_release | ( | struct ast_netsock_list * | list | ) |
Definition at line 87 of file netsock.c.
References ast_free, ast_netsock_destroy(), ASTOBJ_CONTAINER_DESTROY, and ASTOBJ_CONTAINER_DESTROYALL.
Referenced by __unload_module(), and set_config().
{
ASTOBJ_CONTAINER_DESTROYALL(list, ast_netsock_destroy);
ASTOBJ_CONTAINER_DESTROY(list);
ast_free(list);
return 0;
}
| int ast_netsock_set_qos | ( | int | sockfd, |
| int | tos, | ||
| int | cos, | ||
| const char * | desc | ||
| ) |
Definition at line 164 of file netsock.c.
References ast_set_qos().
{
return ast_set_qos(sockfd, tos, cos, desc);
}
| int ast_netsock_sockfd | ( | const struct ast_netsock * | ns | ) |
Definition at line 189 of file netsock.c.
Referenced by peer_set_srcaddr(), and set_config().
{
return ns ? ns-> sockfd : -1;
}
| void ast_netsock_unref | ( | struct ast_netsock * | ns | ) |
Definition at line 204 of file netsock.c.
References ast_netsock_destroy(), and ASTOBJ_UNREF.
Referenced by peer_set_srcaddr(), and set_config().
{
ASTOBJ_UNREF(ns, ast_netsock_destroy);
}
| void ast_set_default_eid | ( | struct ast_eid * | eid | ) |
Fill in an ast_eid with the default eid of this machine.
Definition at line 226 of file netsock.c.
References ARRAY_LEN, ast_debug, ast_eid_to_str(), and ast_eid::eid.
Referenced by ast_readconfig().
{
#if defined(SIOCGIFHWADDR) && defined(HAVE_STRUCT_IFREQ_IFR_IFRU_IFRU_HWADDR)
int s, x = 0;
char eid_str[20];
struct ifreq ifr;
static const unsigned int MAXIF = 10;
s = socket(AF_INET, SOCK_STREAM, 0);
if (s < 0)
return;
for (x = 0; x < MAXIF; x++) {
static const char *prefixes[] = { "eth", "em" };
unsigned int i;
for (i = 0; i < ARRAY_LEN(prefixes); i++) {
memset(&ifr, 0, sizeof(ifr));
snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s%d", prefixes[i], x);
if (!ioctl(s, SIOCGIFHWADDR, &ifr)) {
break;
}
}
if (i == ARRAY_LEN(prefixes)) {
/* Try pciX#[1..N] */
for (i = 0; i < MAXIF; i++) {
memset(&ifr, 0, sizeof(ifr));
snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "pci%u#%u", x, i);
if (!ioctl(s, SIOCGIFHWADDR, &ifr)) {
break;
}
}
if (i == MAXIF) {
continue;
}
}
memcpy(eid, ((unsigned char *)&ifr.ifr_hwaddr) + 2, sizeof(*eid));
ast_debug(1, "Seeding global EID '%s' from '%s' using 'siocgifhwaddr'\n", ast_eid_to_str(eid_str, sizeof(eid_str), eid), ifr.ifr_name);
close(s);
return;
}
close(s);
#else
#if defined(ifa_broadaddr) && !defined(SOLARIS)
char eid_str[20];
struct ifaddrs *ifap;
if (getifaddrs(&ifap) == 0) {
struct ifaddrs *p;
for (p = ifap; p; p = p->ifa_next) {
if ((p->ifa_addr->sa_family == AF_LINK) && !(p->ifa_flags & IFF_LOOPBACK) && (p->ifa_flags & IFF_RUNNING)) {
struct sockaddr_dl* sdp = (struct sockaddr_dl*) p->ifa_addr;
memcpy(&(eid->eid), sdp->sdl_data + sdp->sdl_nlen, 6);
ast_debug(1, "Seeding global EID '%s' from '%s' using 'getifaddrs'\n", ast_eid_to_str(eid_str, sizeof(eid_str), eid), p->ifa_name);
freeifaddrs(ifap);
return;
}
}
freeifaddrs(ifap);
}
#endif
#endif
ast_debug(1, "No ethernet interface found for seeding global EID. You will have to set it manually.\n");
}
| int ast_str_to_eid | ( | struct ast_eid * | eid, |
| const char * | s | ||
| ) |
Convert a string into an EID.
This function expects an EID in the format: 00:11:22:33:44:55
Definition at line 292 of file netsock.c.
References ast_eid::eid.
Referenced by aji_handle_pubsub_event(), ast_readconfig(), build_peer(), dundi_do_query(), set_config(), and xmpp_pubsub_handle_event().
{
unsigned int eid_int[6];
int x;
if (sscanf(s, "%2x:%2x:%2x:%2x:%2x:%2x", &eid_int[0], &eid_int[1], &eid_int[2],
&eid_int[3], &eid_int[4], &eid_int[5]) != 6)
return -1;
for (x = 0; x < 6; x++)
eid->eid[x] = eid_int[x];
return 0;
}