Thu Apr 28 2011 17:16:03

Asterisk developer's documentation


dns.c File Reference

DNS Support for Asterisk. More...

#include "asterisk.h"
#include "asterisk/network.h"
#include <arpa/nameser.h>
#include <resolv.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 DETERMINED_BYTE_ORDER   __BIG_ENDIAN
#define DETERMINED_BYTE_ORDER   __LITTLE_ENDIAN
#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)

Variables

static ast_mutex_t res_lock = AST_MUTEX_INIT_VALUE

Detailed Description

DNS Support for Asterisk.

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

Definition in file dns.c.


Define Documentation

#define DETERMINED_BYTE_ORDER   __BIG_ENDIAN

Definition at line 55 of file dns.c.

#define DETERMINED_BYTE_ORDER   __LITTLE_ENDIAN

Definition at line 55 of file dns.c.

#define MAX_SIZE   4096

Definition at line 44 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 255 of file dns.c.

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

Referenced by ast_get_enum(), ast_get_srv(), blr_ebl(), and blr_txt().

{
#ifdef HAVE_RES_NINIT
   struct __res_state dnsstate;
#endif
   unsigned char answer[MAX_SIZE];
   int res, ret = -1;

#ifdef HAVE_RES_NINIT
   memset(&dnsstate, 0, sizeof(dnsstate));
   res_ninit(&dnsstate);
   res = res_nsearch(&dnsstate, dname, class, type, answer, sizeof(answer));
#else
   ast_mutex_lock(&res_lock);
   res_init();
   res = res_search(dname, class, type, answer, sizeof(answer));
#endif
   if (res > 0) {
      if ((res = dns_parse_answer(context, class, type, answer, res, callback)) < 0) {
         ast_log(LOG_WARNING, "DNS Parse error for %s\n", dname);
         ret = -1;
      } else if (res == 0) {
         ast_debug(1, "No matches found in DNS for %s\n", dname);
         ret = 0;
      } else
         ret = 1;
   }
#ifdef HAVE_RES_NINIT
#ifdef HAVE_RES_NDESTROY
   res_ndestroy(&dnsstate);
#else
   res_nclose(&dnsstate);
#endif
#else
#ifndef __APPLE__
   res_close();
#endif
   ast_mutex_unlock(&res_lock);
#endif

   return ret;
}
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 185 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().

{
   unsigned char *fullanswer = answer;
   struct dn_answer *ans;
   dns_HEADER *h;
   int ret = 0;
   int res;
   int x;

   h = (dns_HEADER *)answer;
   answer += sizeof(dns_HEADER);
   len -= sizeof(dns_HEADER);

   for (x = 0; x < ntohs(h->qdcount); x++) {
      if ((res = skip_name(answer, len)) < 0) {
         ast_log(LOG_WARNING, "Couldn't skip over name\n");
         return -1;
      }
      answer += res + 4;   /* Skip name and QCODE / QCLASS */
      len -= res + 4;
      if (len < 0) {
         ast_log(LOG_WARNING, "Strange query size\n");
         return -1;
      }
   }

   for (x = 0; x < ntohs(h->ancount); x++) {
      if ((res = skip_name(answer, len)) < 0) {
         ast_log(LOG_WARNING, "Failed skipping name\n");
         return -1;
      }
      answer += res;
      len -= res;
      ans = (struct dn_answer *)answer;
      answer += sizeof(struct dn_answer);
      len -= sizeof(struct dn_answer);
      if (len < 0) {
         ast_log(LOG_WARNING, "Strange result size\n");
         return -1;
      }
      if (len < 0) {
         ast_log(LOG_WARNING, "Length exceeds frame\n");
         return -1;
      }

      if (ntohs(ans->class) == class && ntohs(ans->rtype) == type) {
         if (callback) {
            if ((res = callback(context, answer, ntohs(ans->size), fullanswer)) < 0) {
               ast_log(LOG_WARNING, "Failed to parse result\n");
               return -1;
            }
            ret = 1;
         }
      }
      answer += ntohs(ans->size);
      len -= ntohs(ans->size);
   }
   return ret;
}
static int skip_name ( unsigned char *  s,
int  len 
) [static]

Definition at line 161 of file dns.c.

Referenced by dns_parse_answer().

{
   int x = 0;

   while (x < len) {
      if (*s == '\0') {
         s++;
         x++;
         break;
      }
      if ((*s & 0xc0) == 0xc0) {
         s += 2;
         x += 2;
         break;
      }
      x += *s + 1;
      s += *s + 1;
   }
   if (x >= len)
      return -1;
   return x;
}

Variable Documentation

ast_mutex_t res_lock = AST_MUTEX_INIT_VALUE [static]

Definition at line 248 of file dns.c.

Referenced by ast_search_dns().