srv.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * Funding provided by nic.at
00009  *
00010  * See http://www.asterisk.org for more information about
00011  * the Asterisk project. Please do not directly contact
00012  * any of the maintainers of this project for assistance;
00013  * the project provides a web site, mailing lists and IRC
00014  * channels for your use.
00015  *
00016  * This program is free software, distributed under the terms of
00017  * the GNU General Public License Version 2. See the LICENSE file
00018  * at the top of the source tree.
00019  */
00020 
00021 /*! \file
00022  *
00023  * \brief DNS SRV Record Lookup Support for Asterisk
00024  *
00025  * \author Mark Spencer <markster@digium.com>
00026  *
00027  * \arg See also \ref AstENUM
00028  *
00029  * \note Funding provided by nic.at
00030  */
00031 
00032 /*** MODULEINFO
00033    <support_level>core</support_level>
00034  ***/
00035 
00036 #include "asterisk.h"
00037 
00038 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 388045 $")
00039 
00040 #include <netinet/in.h>
00041 #include <arpa/nameser.h>
00042 #ifdef __APPLE__
00043 #include <arpa/nameser_compat.h>
00044 #endif
00045 #include <resolv.h>
00046 
00047 #include "asterisk/channel.h"
00048 #include "asterisk/srv.h"
00049 #include "asterisk/dns.h"
00050 #include "asterisk/utils.h"
00051 #include "asterisk/linkedlists.h"
00052 
00053 #ifdef __APPLE__
00054 #undef T_SRV
00055 #define T_SRV 33
00056 #endif
00057 
00058 struct srv_entry {
00059    unsigned short priority;
00060    unsigned short weight;
00061    unsigned short port;
00062    unsigned int weight_sum;
00063    AST_LIST_ENTRY(srv_entry) list;
00064    char host[1];
00065 };
00066 
00067 struct srv_context {
00068    unsigned int have_weights:1;
00069    struct srv_entry *prev;
00070    unsigned int num_records;
00071    AST_LIST_HEAD_NOLOCK(srv_entries, srv_entry) entries;
00072 };
00073 
00074 static int parse_srv(unsigned char *answer, int len, unsigned char *msg, struct srv_entry **result)
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 }
00114 
00115 static int srv_callback(void *context, unsigned char *answer, int len, unsigned char *fullanswer)
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(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 0;
00145 }
00146 
00147 /* Do the bizarre SRV record weight-handling algorithm
00148    involving sorting and random number generation...
00149 
00150    See RFC 2782 if you want know why this code does this
00151 */
00152 static void process_weights(struct srv_context *context)
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_MOVE_CURRENT(&temp_list, list);
00169       }
00170       AST_LIST_TRAVERSE_SAFE_END;
00171 
00172       while (AST_LIST_FIRST(&temp_list)) {
00173          weight_sum = 0;
00174          AST_LIST_TRAVERSE(&temp_list, current, list)
00175             current->weight_sum = weight_sum += current->weight;
00176 
00177          /* if all the remaining entries have weight == 0,
00178             then just append them to the result list and quit */
00179          if (weight_sum == 0) {
00180             AST_LIST_APPEND_LIST(&newlist, &temp_list, list);
00181             break;
00182          }
00183 
00184          random_weight = 1 + (unsigned int) ((float) weight_sum * (ast_random() / ((float) RAND_MAX + 1.0)));
00185 
00186          AST_LIST_TRAVERSE_SAFE_BEGIN(&temp_list, current, list) {
00187             if (current->weight < random_weight)
00188                continue;
00189 
00190             AST_LIST_MOVE_CURRENT(&newlist, list);
00191             break;
00192          }
00193          AST_LIST_TRAVERSE_SAFE_END;
00194       }
00195 
00196    }
00197 
00198    /* now that the new list has been ordered,
00199       put it in place */
00200 
00201    AST_LIST_APPEND_LIST(&context->entries, &newlist, list);
00202 }
00203 
00204 int ast_srv_lookup(struct srv_context **context, const char *service, const char **host, unsigned short *port)
00205 {
00206    struct srv_entry *cur;
00207 
00208    if (*context == NULL) {
00209       if (!(*context = ast_calloc(1, sizeof(struct srv_context)))) {
00210          return -1;
00211       }
00212       AST_LIST_HEAD_INIT_NOLOCK(&(*context)->entries);
00213 
00214       if ((ast_search_dns(*context, service, C_IN, T_SRV, srv_callback)) < 0) {
00215          ast_free(*context);
00216          *context = NULL;
00217          return -1;
00218       }
00219 
00220       if ((*context)->have_weights) {
00221          process_weights(*context);
00222       }
00223 
00224       (*context)->prev = AST_LIST_FIRST(&(*context)->entries);
00225       *host = (*context)->prev->host;
00226       *port = (*context)->prev->port;
00227       AST_LIST_TRAVERSE(&(*context)->entries, cur, list) {
00228          ++((*context)->num_records);
00229       }
00230       return 0;
00231    }
00232 
00233    if (((*context)->prev = AST_LIST_NEXT((*context)->prev, list))) {
00234       /* Retrieve next item in result */
00235       *host = (*context)->prev->host;
00236       *port = (*context)->prev->port;
00237       return 0;
00238    } else {
00239       /* No more results */
00240       while ((cur = AST_LIST_REMOVE_HEAD(&(*context)->entries, list))) {
00241          ast_free(cur);
00242       }
00243       ast_free(*context);
00244       *context = NULL;
00245       return 1;
00246    }
00247 }
00248 
00249 void ast_srv_cleanup(struct srv_context **context)
00250 {
00251    const char *host;
00252    unsigned short port;
00253 
00254    if (*context) {
00255       /* We have a context to clean up. */
00256       while (!(ast_srv_lookup(context, NULL, &host, &port))) {
00257       }
00258    }
00259 }
00260 
00261 int ast_get_srv(struct ast_channel *chan, char *host, int hostlen, int *port, const char *service)
00262 {
00263    struct srv_context context = { .entries = AST_LIST_HEAD_NOLOCK_INIT_VALUE };
00264    struct srv_entry *current;
00265    int ret;
00266 
00267    if (chan && ast_autoservice_start(chan) < 0) {
00268       return -1;
00269    }
00270 
00271    ret = ast_search_dns(&context, service, C_IN, T_SRV, srv_callback);
00272 
00273    if (context.have_weights) {
00274       process_weights(&context);
00275    }
00276 
00277    if (chan) {
00278       ret |= ast_autoservice_stop(chan);
00279    }
00280 
00281    /* TODO: there could be a "." entry in the returned list of
00282       answers... if so, this requires special handling */
00283 
00284    /* the list of entries will be sorted in the proper selection order
00285       already, so we just need the first one (if any) */
00286 
00287    if ((ret > 0) && (current = AST_LIST_REMOVE_HEAD(&context.entries, list))) {
00288       ast_copy_string(host, current->host, hostlen);
00289       *port = current->port;
00290       ast_free(current);
00291       ast_debug(4, "ast_get_srv: SRV lookup for '%s' mapped to host %s, port %d\n",
00292                 service, host, *port);
00293    } else {
00294       host[0] = '\0';
00295       *port = -1;
00296    }
00297 
00298    while ((current = AST_LIST_REMOVE_HEAD(&context.entries, list))) {
00299       ast_free(current);
00300    }
00301 
00302    return ret;
00303 }
00304 
00305 unsigned int ast_srv_get_record_count(struct srv_context *context)
00306 {
00307    return context->num_records;
00308 }
00309 
00310 int ast_srv_get_nth_record(struct srv_context *context, int record_num, const char **host,
00311       unsigned short *port, unsigned short *priority, unsigned short *weight)
00312 {
00313    int i = 1;
00314    int res = -1;
00315    struct srv_entry *entry;
00316 
00317    if (record_num < 1 || record_num > context->num_records) {
00318       return res;
00319    }
00320 
00321    AST_LIST_TRAVERSE(&context->entries, entry, list) {
00322       if (i == record_num) {
00323          *host = entry->host;
00324          *port = entry->port;
00325          *priority = entry->priority;
00326          *weight = entry->weight;
00327          res = 0;
00328          break;
00329       }
00330       ++i;
00331    }
00332 
00333    return res;
00334 }

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