Thu Oct 11 06:43:24 2012

Asterisk developer's documentation


dns.c File Reference

DNS Support for Asterisk. More...

#include "asterisk.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>
#include <unistd.h>
#include "asterisk/logger.h"
#include "asterisk/channel.h"
#include "asterisk/dns.h"
#include "asterisk/endian.h"

Include dependency graph for dns.c:

Go to the source code of this file.

Data Structures

struct  dn_answer
struct  dns_HEADER

Defines

#define MAX_SIZE   4096

Functions

int ast_search_dns (void *context, const char *dname, int class, int type, int(*callback)(void *context, unsigned char *answer, int len, unsigned char *fullanswer))
 Lookup record in DNS.
static int dns_parse_answer (void *context, int class, int type, unsigned char *answer, int len, int(*callback)(void *context, unsigned char *answer, int len, unsigned char *fullanswer))
 Parse DNS lookup result, call callback.
static int skip_name (unsigned char *s, int len)


Detailed Description

DNS Support for Asterisk.

Author:
Thorsten Lockert <tholo@trollphone.org>
Reference

Definition in file dns.c.


Define Documentation

#define MAX_SIZE   4096

Definition at line 48 of file dns.c.

Referenced by ast_search_dns().


Function Documentation

int ast_search_dns ( void *  context,
const char *  dname,
int  class,
int  type,
int(*)(void *context, unsigned char *answer, int len, unsigned char *fullanswer)  callback 
)

Lookup record in DNS.

Perform DNS lookup (used by DNS, enum and SRV lookups).

Note:
Asterisk DNS is synchronus at this time. This means that if your DNS does not work properly, Asterisk might not start properly or a channel may lock.

Definition at line 247 of file dns.c.

References ast_log(), ast_mutex_lock(), ast_mutex_unlock(), dns_parse_answer(), LOG_DEBUG, LOG_WARNING, and MAX_SIZE.

Referenced by ast_get_enum(), ast_get_srv(), and ast_get_txt().

00250 {
00251 #ifdef HAVE_RES_NINIT
00252    struct __res_state dnsstate;
00253 #endif
00254    unsigned char answer[MAX_SIZE];
00255    int res, ret = -1;
00256 
00257 #ifdef HAVE_RES_NINIT
00258    memset(&dnsstate, 0, sizeof(dnsstate));
00259    res_ninit(&dnsstate);
00260    res = res_nsearch(&dnsstate, dname, class, type, answer, sizeof(answer));
00261 #else
00262    ast_mutex_lock(&res_lock);
00263    res_init();
00264    res = res_search(dname, class, type, answer, sizeof(answer));
00265 #endif
00266    if (res > 0) {
00267       if ((res = dns_parse_answer(context, class, type, answer, res, callback)) < 0) {
00268          ast_log(LOG_WARNING, "DNS Parse error for %s\n", dname);
00269          ret = -1;
00270       }
00271       else if (res == 0) {
00272          ast_log(LOG_DEBUG, "No matches found in DNS for %s\n", dname);
00273          ret = 0;
00274       }
00275       else
00276          ret = 1;
00277    }
00278 #ifdef HAVE_RES_NINIT
00279 #ifdef HAVE_RES_NDESTROY
00280    res_ndestroy(&dnsstate);
00281 #else
00282    res_nclose(&dnsstate);
00283 #endif
00284 #else
00285 #ifndef __APPLE__
00286    res_close();
00287 #endif
00288    ast_mutex_unlock(&res_lock);
00289 #endif
00290 
00291    return ret;
00292 }

static int dns_parse_answer ( void *  context,
int  class,
int  type,
unsigned char *  answer,
int  len,
int(*)(void *context, unsigned char *answer, int len, unsigned char *fullanswer)  callback 
) [static]

Parse DNS lookup result, call callback.

Definition at line 177 of file dns.c.

References dns_HEADER::ancount, ast_log(), dn_answer::class, LOG_WARNING, dns_HEADER::qdcount, dn_answer::rtype, dn_answer::size, and skip_name().

Referenced by ast_search_dns().

00180 {
00181    unsigned char *fullanswer = answer;
00182    struct dn_answer *ans;
00183    dns_HEADER *h;
00184    int res;
00185    int x;
00186 
00187    h = (dns_HEADER *)answer;
00188    answer += sizeof(dns_HEADER);
00189    len -= sizeof(dns_HEADER);
00190 
00191    for (x = 0; x < ntohs(h->qdcount); x++) {
00192       if ((res = skip_name(answer, len)) < 0) {
00193          ast_log(LOG_WARNING, "Couldn't skip over name\n");
00194          return -1;
00195       }
00196       answer += res + 4;   /* Skip name and QCODE / QCLASS */
00197       len -= res + 4;
00198       if (len < 0) {
00199          ast_log(LOG_WARNING, "Strange query size\n");
00200          return -1;
00201       }
00202    }
00203 
00204    for (x = 0; x < ntohs(h->ancount); x++) {
00205       if ((res = skip_name(answer, len)) < 0) {
00206          ast_log(LOG_WARNING, "Failed skipping name\n");
00207          return -1;
00208       }
00209       answer += res;
00210       len -= res;
00211       ans = (struct dn_answer *)answer;
00212       answer += sizeof(struct dn_answer);
00213       len -= sizeof(struct dn_answer);
00214       if (len < 0) {
00215          ast_log(LOG_WARNING, "Strange result size\n");
00216          return -1;
00217       }
00218       if (len < 0) {
00219          ast_log(LOG_WARNING, "Length exceeds frame\n");
00220          return -1;
00221       }
00222 
00223       if (ntohs(ans->class) == class && ntohs(ans->rtype) == type) {
00224          if (callback) {
00225             if ((res = callback(context, answer, ntohs(ans->size), fullanswer)) < 0) {
00226                ast_log(LOG_WARNING, "Failed to parse result\n");
00227                return -1;
00228             }
00229             if (res > 0)
00230                return 1;
00231          }
00232       }
00233       answer += ntohs(ans->size);
00234       len -= ntohs(ans->size);
00235    }
00236    return 0;
00237 }

static int skip_name ( unsigned char *  s,
int  len 
) [static]

Definition at line 153 of file dns.c.

Referenced by dns_parse_answer().

00154 {
00155    int x = 0;
00156 
00157    while (x < len) {
00158       if (*s == '\0') {
00159          s++;
00160          x++;
00161          break;
00162       }
00163       if ((*s & 0xc0) == 0xc0) {
00164          s += 2;
00165          x += 2;
00166          break;
00167       }
00168       x += *s + 1;
00169       s += *s + 1;
00170    }
00171    if (x >= len)
00172       return -1;
00173    return x;
00174 }


Generated on Thu Oct 11 06:43:24 2012 for Asterisk - the Open Source PBX by  doxygen 1.5.6