DNS SRV Record Lookup Support for Asterisk. More...
#include "asterisk.h"#include <netinet/in.h>#include <arpa/nameser.h>#include <resolv.h>#include "asterisk/channel.h"#include "asterisk/srv.h"#include "asterisk/dns.h"#include "asterisk/utils.h"#include "asterisk/linkedlists.h"
Go to the source code of this file.
Data Structures | |
| struct | srv_context |
| struct | srv_context::srv_entries |
| struct | srv_entry |
Functions | |
| int | ast_get_srv (struct ast_channel *chan, char *host, int hostlen, int *port, const char *service) |
| static int | parse_srv (unsigned char *answer, int len, unsigned char *msg, struct srv_entry **result) |
| static void | process_weights (struct srv_context *context) |
| static int | srv_callback (void *context, unsigned char *answer, int len, unsigned char *fullanswer) |
DNS SRV Record Lookup Support for Asterisk.
Definition in file srv.c.
| int ast_get_srv | ( | struct ast_channel * | chan, |
| char * | host, | ||
| int | hostlen, | ||
| int * | port, | ||
| const char * | service | ||
| ) |
Lookup entry in SRV records Returns 1 if found, 0 if not found, -1 on hangup Only do SRV record lookup if you get a domain without a port. If you get a port #, it's a DNS host name.
| chan | Ast channel |
| host | host name (return value) |
| hostlen | Length of string "host" |
| port | Port number (return value) |
| service | Service tag for SRV lookup (like "_sip._udp" or "_stun._udp" |
Definition at line 200 of file srv.c.
References ast_autoservice_start(), ast_autoservice_stop(), ast_copy_string(), ast_free, AST_LIST_HEAD_NOLOCK_INIT_VALUE, AST_LIST_REMOVE_HEAD, ast_search_dns(), ast_verb, srv_context::entries, srv_context::have_weights, srv_entry::host, srv_entry::port, process_weights(), and srv_callback().
Referenced by ast_get_ip_or_srv(), and create_addr().
{
struct srv_context context = { .entries = AST_LIST_HEAD_NOLOCK_INIT_VALUE };
struct srv_entry *current;
int ret;
if (chan && ast_autoservice_start(chan) < 0)
return -1;
ret = ast_search_dns(&context, service, C_IN, T_SRV, srv_callback);
if (context.have_weights)
process_weights(&context);
if (chan)
ret |= ast_autoservice_stop(chan);
/* TODO: there could be a "." entry in the returned list of
answers... if so, this requires special handling */
/* the list of entries will be sorted in the proper selection order
already, so we just need the first one (if any) */
if ((ret > 0) && (current = AST_LIST_REMOVE_HEAD(&context.entries, list))) {
ast_copy_string(host, current->host, hostlen);
*port = current->port;
ast_free(current);
ast_verb(4, "ast_get_srv: SRV lookup for '%s' mapped to host %s, port %d\n",
service, host, *port);
} else {
host[0] = '\0';
*port = -1;
}
while ((current = AST_LIST_REMOVE_HEAD(&context.entries, list)))
ast_free(current);
return ret;
}
| static int parse_srv | ( | unsigned char * | answer, |
| int | len, | ||
| unsigned char * | msg, | ||
| struct srv_entry ** | result | ||
| ) | [static] |
Definition at line 70 of file srv.c.
References ast_calloc, ast_log(), srv_entry::host, LOG_WARNING, srv_entry::port, srv_entry::priority, and srv_entry::weight.
Referenced by srv_callback().
{
struct srv {
unsigned short priority;
unsigned short weight;
unsigned short port;
} __attribute__((__packed__)) *srv = (struct srv *) answer;
int res = 0;
char repl[256] = "";
struct srv_entry *entry;
if (len < sizeof(*srv))
return -1;
answer += sizeof(*srv);
len -= sizeof(*srv);
if ((res = dn_expand(msg, answer + len, answer, repl, sizeof(repl) - 1)) <= 0) {
ast_log(LOG_WARNING, "Failed to expand hostname\n");
return -1;
}
/* the magic value "." for the target domain means that this service
is *NOT* available at the domain we searched */
if (!strcmp(repl, "."))
return -1;
if (!(entry = ast_calloc(1, sizeof(*entry) + strlen(repl))))
return -1;
entry->priority = ntohs(srv->priority);
entry->weight = ntohs(srv->weight);
entry->port = ntohs(srv->port);
strcpy(entry->host, repl);
*result = entry;
return 0;
}
| static void process_weights | ( | struct srv_context * | context | ) | [static] |
Definition at line 148 of file srv.c.
References AST_LIST_APPEND_LIST, AST_LIST_FIRST, AST_LIST_HEAD_NOLOCK_INIT_VALUE, AST_LIST_MOVE_CURRENT, AST_LIST_TRAVERSE, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, ast_random(), srv_context::entries, srv_entry::priority, srv_entry::weight, and srv_entry::weight_sum.
Referenced by ast_get_srv().
{
struct srv_entry *current;
struct srv_entries newlist = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
while (AST_LIST_FIRST(&context->entries)) {
unsigned int random_weight;
unsigned int weight_sum;
unsigned short cur_priority = AST_LIST_FIRST(&context->entries)->priority;
struct srv_entries temp_list = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
weight_sum = 0;
AST_LIST_TRAVERSE_SAFE_BEGIN(&context->entries, current, list) {
if (current->priority != cur_priority)
break;
AST_LIST_MOVE_CURRENT(&temp_list, list);
}
AST_LIST_TRAVERSE_SAFE_END;
while (AST_LIST_FIRST(&temp_list)) {
weight_sum = 0;
AST_LIST_TRAVERSE(&temp_list, current, list)
current->weight_sum = weight_sum += current->weight;
/* if all the remaining entries have weight == 0,
then just append them to the result list and quit */
if (weight_sum == 0) {
AST_LIST_APPEND_LIST(&newlist, &temp_list, list);
break;
}
random_weight = 1 + (unsigned int) ((float) weight_sum * (ast_random() / ((float) RAND_MAX + 1.0)));
AST_LIST_TRAVERSE_SAFE_BEGIN(&temp_list, current, list) {
if (current->weight < random_weight)
continue;
AST_LIST_MOVE_CURRENT(&newlist, list);
break;
}
AST_LIST_TRAVERSE_SAFE_END;
}
}
/* now that the new list has been ordered,
put it in place */
AST_LIST_APPEND_LIST(&context->entries, &newlist, list);
}
| static int srv_callback | ( | void * | context, |
| unsigned char * | answer, | ||
| int | len, | ||
| unsigned char * | fullanswer | ||
| ) | [static] |
Definition at line 111 of file srv.c.
References AST_LIST_INSERT_BEFORE_CURRENT, AST_LIST_INSERT_TAIL, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, srv_context::entries, srv_context::have_weights, parse_srv(), srv_entry::priority, and srv_entry::weight.
Referenced by ast_get_srv().
{
struct srv_context *c = (struct srv_context *) context;
struct srv_entry *entry = NULL;
struct srv_entry *current;
if (parse_srv(answer, len, fullanswer, &entry))
return -1;
if (entry->weight)
c->have_weights = 1;
AST_LIST_TRAVERSE_SAFE_BEGIN(&c->entries, current, list) {
/* insert this entry just before the first existing
entry with a higher priority */
if (current->priority <= entry->priority)
continue;
AST_LIST_INSERT_BEFORE_CURRENT(entry, list);
entry = NULL;
break;
}
AST_LIST_TRAVERSE_SAFE_END;
/* if we didn't find a place to insert the entry before an existing
entry, then just add it to the end */
if (entry)
AST_LIST_INSERT_TAIL(&c->entries, entry, list);
return 0;
}