Sat Aug 6 00:40:05 2011

Asterisk developer's documentation


srv.c File Reference

DNS SRV Record Lookup Support for Asterisk. More...

#include "asterisk.h"
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "asterisk/channel.h"
#include "asterisk/logger.h"
#include "asterisk/srv.h"
#include "asterisk/dns.h"
#include "asterisk/options.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)


Detailed Description

DNS SRV Record Lookup Support for Asterisk.

Author:
Mark Spencer <markster@digium.com>
Note:
Funding provided by nic.at

Definition in file srv.c.


Function Documentation

int ast_get_srv ( struct ast_channel chan,
char *  host,
int  hostlen,
int *  port,
const char *  service 
)

Parameters:
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 206 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_verbose(), context, srv_entry::host, srv_entry::list, option_verbose, srv_entry::port, process_weights(), srv_callback(), and VERBOSE_PREFIX_3.

Referenced by ast_get_ip_or_srv(), and create_addr().

00207 {
00208    struct srv_context context = { .entries = AST_LIST_HEAD_NOLOCK_INIT_VALUE };
00209    struct srv_entry *current;
00210    int ret;
00211 
00212    if (chan && ast_autoservice_start(chan) < 0)
00213       return -1;
00214 
00215    ret = ast_search_dns(&context, service, C_IN, T_SRV, srv_callback);
00216 
00217    if (context.have_weights)
00218       process_weights(&context);
00219 
00220    if (chan)
00221       ret |= ast_autoservice_stop(chan);
00222 
00223    /* TODO: there could be a "." entry in the returned list of
00224       answers... if so, this requires special handling */
00225 
00226    /* the list of entries will be sorted in the proper selection order
00227       already, so we just need the first one (if any) */
00228 
00229    if ((ret > 0) && (current = AST_LIST_REMOVE_HEAD(&context.entries, list))) {
00230       ast_copy_string(host, current->host, hostlen);
00231       *port = current->port;
00232       ast_free(current);
00233       if (option_verbose > 3) {
00234          ast_verbose(VERBOSE_PREFIX_3 "ast_get_srv: SRV lookup for '%s' mapped to host %s, port %d\n",
00235                 service, host, *port);
00236       }
00237    } else {
00238       host[0] = '\0';
00239       *port = -1;
00240    }
00241 
00242    while ((current = AST_LIST_REMOVE_HEAD(&context.entries, list)))
00243       ast_free(current);
00244 
00245    return ret;
00246 }

static int parse_srv ( unsigned char *  answer,
int  len,
unsigned char *  msg,
struct srv_entry **  result 
) [static]

Definition at line 74 of file srv.c.

References ast_calloc, ast_log(), LOG_WARNING, srv_entry::port, srv_entry::priority, and srv_entry::weight.

Referenced by srv_callback().

00075 {
00076    struct srv {
00077       unsigned short priority;
00078       unsigned short weight;
00079       unsigned short port;
00080    } __attribute__((__packed__)) *srv = (struct srv *) answer;
00081 
00082    int res = 0;
00083    char repl[256] = "";
00084    struct srv_entry *entry;
00085 
00086    if (len < sizeof(*srv))
00087       return -1;
00088 
00089    answer += sizeof(*srv);
00090    len -= sizeof(*srv);
00091 
00092    if ((res = dn_expand(msg, answer + len, answer, repl, sizeof(repl) - 1)) <= 0) {
00093       ast_log(LOG_WARNING, "Failed to expand hostname\n");
00094       return -1;
00095    }
00096 
00097    /* the magic value "." for the target domain means that this service
00098       is *NOT* available at the domain we searched */
00099    if (!strcmp(repl, "."))
00100       return -1;
00101 
00102    if (!(entry = ast_calloc(1, sizeof(*entry) + strlen(repl))))
00103       return -1;
00104    
00105    entry->priority = ntohs(srv->priority);
00106    entry->weight = ntohs(srv->weight);
00107    entry->port = ntohs(srv->port);
00108    strcpy(entry->host, repl);
00109 
00110    *result = entry;
00111    
00112    return 0;
00113 }

static void process_weights ( struct srv_context context  )  [static]

Definition at line 152 of file srv.c.

References AST_LIST_APPEND_LIST, AST_LIST_FIRST, AST_LIST_HEAD_NOLOCK_INIT_VALUE, AST_LIST_INSERT_TAIL, AST_LIST_REMOVE_CURRENT, AST_LIST_TRAVERSE, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, ast_random(), context, srv_entry::list, srv_entry::priority, srv_entry::weight, and srv_entry::weight_sum.

Referenced by ast_get_srv().

00153 {
00154    struct srv_entry *current;
00155    struct srv_entries newlist = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
00156 
00157    while (AST_LIST_FIRST(&context->entries)) {
00158       unsigned int random_weight;
00159       unsigned int weight_sum;
00160       unsigned short cur_priority = AST_LIST_FIRST(&context->entries)->priority;
00161       struct srv_entries temp_list = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
00162       weight_sum = 0;
00163 
00164       AST_LIST_TRAVERSE_SAFE_BEGIN(&context->entries, current, list) {
00165          if (current->priority != cur_priority)
00166             break;
00167 
00168          AST_LIST_REMOVE_CURRENT(&context->entries, list);
00169          AST_LIST_INSERT_TAIL(&temp_list, current, list);
00170       }
00171       AST_LIST_TRAVERSE_SAFE_END;
00172 
00173       while (AST_LIST_FIRST(&temp_list)) {
00174          weight_sum = 0;
00175          AST_LIST_TRAVERSE(&temp_list, current, list)
00176             current->weight_sum = weight_sum += current->weight;
00177 
00178          /* if all the remaining entries have weight == 0,
00179             then just append them to the result list and quit */
00180          if (weight_sum == 0) {
00181             AST_LIST_APPEND_LIST(&newlist, &temp_list, list);
00182             break;
00183          }
00184 
00185          random_weight = 1 + (unsigned int) ((float) weight_sum * (ast_random() / ((float) RAND_MAX + 1.0)));
00186 
00187          AST_LIST_TRAVERSE_SAFE_BEGIN(&temp_list, current, list) {
00188             if (current->weight < random_weight)
00189                continue;
00190 
00191             AST_LIST_REMOVE_CURRENT(&temp_list, list);
00192             AST_LIST_INSERT_TAIL(&newlist, current, list);
00193             break;
00194          }
00195          AST_LIST_TRAVERSE_SAFE_END;
00196       }
00197 
00198    }
00199 
00200    /* now that the new list has been ordered,
00201       put it in place */
00202 
00203    AST_LIST_APPEND_LIST(&context->entries, &newlist, list);
00204 }

static int srv_callback ( void *  context,
unsigned char *  answer,
int  len,
unsigned char *  fullanswer 
) [static]

Definition at line 115 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, srv_entry::list, parse_srv(), srv_entry::priority, and srv_entry::weight.

Referenced by ast_get_srv().

00116 {
00117    struct srv_context *c = (struct srv_context *) context;
00118    struct srv_entry *entry = NULL;
00119    struct srv_entry *current;
00120 
00121    if (parse_srv(answer, len, fullanswer, &entry))
00122       return -1;
00123 
00124    if (entry->weight)
00125       c->have_weights = 1;
00126 
00127    AST_LIST_TRAVERSE_SAFE_BEGIN(&c->entries, current, list) {
00128       /* insert this entry just before the first existing
00129          entry with a higher priority */
00130       if (current->priority <= entry->priority)
00131          continue;
00132 
00133       AST_LIST_INSERT_BEFORE_CURRENT(&c->entries, entry, list);
00134       entry = NULL;
00135       break;
00136    }
00137    AST_LIST_TRAVERSE_SAFE_END;
00138 
00139    /* if we didn't find a place to insert the entry before an existing
00140       entry, then just add it to the end */
00141    if (entry)
00142       AST_LIST_INSERT_TAIL(&c->entries, entry, list);
00143 
00144    return 1;
00145 }


Generated on Sat Aug 6 00:40:05 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7