![]() |
Home page |
Mailing list |
Docs
Asterisk developer's documentation :: Codename Pineapple
dns.c File Reference
Definition in file dns.c.
#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 "asterisk/options.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 | |
| AST_MUTEX_DEFINE_STATIC (res_lock) | |
| 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)) |
| Perform DNS lookup (used by DNS, enum and SRV lookups). | |
| 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 | |
| dn_answer | __packed__ |
|
|
Definition at line 49 of file dns.c. Referenced by ast_search_dns(). |
|
|
|
|
||||||||||||||||||||||||
|
Perform DNS lookup (used by DNS, enum and SRV lookups).
Definition at line 189 of file dns.c. References ast_log(), ast_mutex_lock(), ast_mutex_unlock(), dns_parse_answer(), LOG_DEBUG, LOG_WARNING, MAX_SIZE, and option_debug. Referenced by ast_get_enum(), and ast_get_srv(). 00192 { 00193 #if HAVE_RES_NINIT 00194 struct __res_state dnsstate; 00195 #endif 00196 unsigned char answer[MAX_SIZE]; 00197 int res, ret = -1; 00198 00199 #if HAVE_RES_NINIT 00200 res_ninit(&dnsstate); 00201 res = res_nsearch(&dnsstate, dname, class, type, 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 if (option_debug) 00214 ast_log(LOG_DEBUG, "No matches found in DNS for %s\n", dname); 00215 ret = 0; 00216 } 00217 else 00218 ret = 1; 00219 } 00220 #if HAVE_RES_NINIT 00221 res_nclose(&dnsstate); 00222 #else 00223 #ifndef __APPLE__ 00224 res_close(); 00225 #endif 00226 ast_mutex_unlock(&res_lock); 00227 #endif 00228 00229 return ret; 00230 }
|
|
||||||||||||||||||||||||||||
|
Parse DNS lookup result, call callback.
Definition at line 119 of file dns.c. References ast_log(), LOG_WARNING, dns_HEADER::qdcount, and skip_name(). Referenced by ast_search_dns(). 00122 { 00123 unsigned char *fullanswer = answer; 00124 struct dn_answer *ans; 00125 dns_HEADER *h; 00126 int res; 00127 int x; 00128 00129 h = (dns_HEADER *)answer; 00130 answer += sizeof(dns_HEADER); 00131 len -= sizeof(dns_HEADER); 00132 00133 for (x = 0; x < ntohs(h->qdcount); x++) { 00134 if ((res = skip_name(answer, len)) < 0) { 00135 ast_log(LOG_WARNING, "Couldn't skip over name\n"); 00136 return -1; 00137 } 00138 answer += res + 4; /* Skip name and QCODE / QCLASS */ 00139 len -= res + 4; 00140 if (len < 0) { 00141 ast_log(LOG_WARNING, "Strange query size\n"); 00142 return -1; 00143 } 00144 } 00145 00146 for (x = 0; x < ntohs(h->ancount); x++) { 00147 if ((res = skip_name(answer, len)) < 0) { 00148 ast_log(LOG_WARNING, "Failed skipping name\n"); 00149 return -1; 00150 } 00151 answer += res; 00152 len -= res; 00153 ans = (struct dn_answer *)answer; 00154 answer += sizeof(struct dn_answer); 00155 len -= sizeof(struct dn_answer); 00156 if (len < 0) { 00157 ast_log(LOG_WARNING, "Strange result size\n"); 00158 return -1; 00159 } 00160 if (len < 0) { 00161 ast_log(LOG_WARNING, "Length exceeds frame\n"); 00162 return -1; 00163 } 00164 00165 if (ntohs(ans->class) == class && ntohs(ans->rtype) == type) { 00166 if (callback) { 00167 if ((res = callback(context, answer, ntohs(ans->size), fullanswer)) < 0) { 00168 ast_log(LOG_WARNING, "Failed to parse result\n"); 00169 return -1; 00170 } 00171 if (res > 0) 00172 return 1; 00173 } 00174 } 00175 answer += ntohs(ans->size); 00176 len -= ntohs(ans->size); 00177 } 00178 return 0; 00179 }
|
|
||||||||||||
|
Definition at line 95 of file dns.c. Referenced by dns_parse_answer(). 00096 { 00097 int x = 0; 00098 00099 while (x < len) { 00100 if (*s == '\0') { 00101 s++; 00102 x++; 00103 break; 00104 } 00105 if ((*s & 0xc0) == 0xc0) { 00106 s += 2; 00107 x += 2; 00108 break; 00109 } 00110 x += *s + 1; 00111 s += *s + 1; 00112 } 00113 if (x >= len) 00114 return -1; 00115 return x; 00116 }
|
|
|
|