Mon Sep 18 09:15:03 2006

Asterisk developer's documentation


dns.c File Reference

DNS Support for Asterisk. More...

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

Go to the source code of this file.

Data Structures

struct  dn_answer
struct  dns_HEADER

Defines

#define MAX_SIZE   4096

Functions

 AST_MUTEX_DEFINE_STATIC (res_lock)
int ast_search_dns (void *context, const char *dname, int class, int type, int(*callback)(void *context, char *answer, int len, char *fullanswer))
 Perform DNS lookup (used by enum and SRV lookups).
static int dns_parse_answer (void *context, int class, int type, char *answer, int len, int(*callback)(void *context, char *answer, int len, char *fullanswer))
static int skip_name (char *s, int len)

Variables

dn_answer __packed__


Detailed Description

DNS Support for Asterisk.

Author:
Thorsten Lockert <tholo@trollphone.org>

Definition in file dns.c.


Define Documentation

#define MAX_SIZE   4096

Definition at line 44 of file dns.c.

Referenced by ast_search_dns().


Function Documentation

AST_MUTEX_DEFINE_STATIC ( res_lock   ) 

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

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

Parameters:
context 
dname Domain name to lookup (host, SRV domain, TXT record name)
class Record Class (see "man res_search")
type Record type (see "man res_search")
callback Callback function for handling DNS result

Definition at line 186 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().

00189 {
00190 #ifdef HAS_RES_NINIT
00191    struct __res_state dnsstate;
00192 #endif
00193    char answer[MAX_SIZE];
00194    int res, ret = -1;
00195 
00196 #ifdef HAS_RES_NINIT
00197 #ifdef MAKE_VALGRIND_HAPPY
00198    memset(&dnsstate, 0, sizeof(dnsstate));
00199 #endif   
00200    res_ninit(&dnsstate);
00201    res = res_nsearch(&dnsstate, dname, class, type, (unsigned char *)answer, sizeof(answer));
00202 #else
00203    ast_mutex_lock(&res_lock);
00204    res_init();
00205    res = res_search(dname, class, type, answer, sizeof(answer));
00206 #endif
00207    if (res > 0) {
00208       if ((res = dns_parse_answer(context, class, type, answer, res, callback)) < 0) {
00209          ast_log(LOG_WARNING, "DNS Parse error for %s\n", dname);
00210          ret = -1;
00211       }
00212       else if (ret == 0) {
00213          ast_log(LOG_DEBUG, "No matches found in DNS for %s\n", dname);
00214          ret = 0;
00215       }
00216       else
00217          ret = 1;
00218    }
00219 #ifdef HAS_RES_NINIT
00220    res_nclose(&dnsstate);
00221 #else
00222 #ifndef __APPLE__
00223    res_close();
00224 #endif
00225    ast_mutex_unlock(&res_lock);
00226 #endif
00227    return ret;
00228 }

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

Definition at line 114 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().

00117 {
00118    char *fullanswer = answer;
00119    struct dn_answer *ans;
00120    dns_HEADER *h;
00121    int res;
00122    int x;
00123 
00124    h = (dns_HEADER *)answer;
00125    answer += sizeof(dns_HEADER);
00126    len -= sizeof(dns_HEADER);
00127 
00128    for (x = 0; x < ntohs(h->qdcount); x++) {
00129       if ((res = skip_name(answer, len)) < 0) {
00130          ast_log(LOG_WARNING, "Couldn't skip over name\n");
00131          return -1;
00132       }
00133       answer += res + 4;   /* Skip name and QCODE / QCLASS */
00134       len -= res + 4;
00135       if (len < 0) {
00136          ast_log(LOG_WARNING, "Strange query size\n");
00137          return -1;
00138       }
00139    }
00140 
00141    for (x = 0; x < ntohs(h->ancount); x++) {
00142       if ((res = skip_name(answer, len)) < 0) {
00143          ast_log(LOG_WARNING, "Failed skipping name\n");
00144          return -1;
00145       }
00146       answer += res;
00147       len -= res;
00148       ans = (struct dn_answer *)answer;
00149       answer += sizeof(struct dn_answer);
00150       len -= sizeof(struct dn_answer);
00151       if (len < 0) {
00152          ast_log(LOG_WARNING, "Strange result size\n");
00153          return -1;
00154       }
00155       if (len < 0) {
00156          ast_log(LOG_WARNING, "Length exceeds frame\n");
00157          return -1;
00158       }
00159 
00160       if (ntohs(ans->class) == class && ntohs(ans->rtype) == type) {
00161          if (callback) {
00162             if ((res = callback(context, answer, ntohs(ans->size), fullanswer)) < 0) {
00163                ast_log(LOG_WARNING, "Failed to parse result\n");
00164                return -1;
00165             }
00166             if (res > 0)
00167                return 1;
00168          }
00169       }
00170       answer += ntohs(ans->size);
00171       len -= ntohs(ans->size);
00172    }
00173    return 0;
00174 }

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

Definition at line 90 of file dns.c.

Referenced by dns_parse_answer().

00091 {
00092    int x = 0;
00093 
00094    while (x < len) {
00095       if (*s == '\0') {
00096          s++;
00097          x++;
00098          break;
00099       }
00100       if ((*s & 0xc0) == 0xc0) {
00101          s += 2;
00102          x += 2;
00103          break;
00104       }
00105       x += *s + 1;
00106       s += *s + 1;
00107    }
00108    if (x >= len)
00109       return -1;
00110    return x;
00111 }


Variable Documentation

struct dn_answer __packed__


Generated on Mon Sep 18 09:15:03 2006 for Asterisk - the Open Source PBX by  doxygen 1.4.7