resolve.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /*
  2. * resolver related functions
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * This file is part of Kamailio, a free SIP server.
  7. *
  8. * Kamailio is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * Kamailio is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /*!
  23. * \file
  24. * \brief Kamailio core :: DNS resolver
  25. * \author andrei
  26. * \ingroup core
  27. * Module: \ref core
  28. */
  29. #ifndef __resolve_h
  30. #define __resolve_h
  31. #include <sys/types.h>
  32. #include <sys/socket.h>
  33. #include <netinet/in.h>
  34. #include <netdb.h>
  35. #include <arpa/nameser.h>
  36. #include <resolv.h>
  37. #include "counters.h"
  38. #include "dns_func.h"
  39. #ifdef __OS_darwin
  40. #include <arpa/nameser_compat.h>
  41. #endif
  42. #include "ip_addr.h"
  43. #ifdef USE_DNS_CACHE
  44. #include "dns_wrappers.h"
  45. #endif
  46. /* define RESOLVE_DBG for debugging info (very noisy) */
  47. #define RESOLVE_DBG
  48. /* define NAPTR_DBG for naptr related debugging info (very noisy) */
  49. #define NAPTR_DBG
  50. #define MAX_QUERY_SIZE 8192
  51. #define ANS_SIZE 8192
  52. #define DNS_HDR_SIZE 12
  53. #define MAX_DNS_NAME 256
  54. #define MAX_DNS_STRING 255
  55. #ifndef T_EBL
  56. /** not official yet - iEnum. */
  57. #define T_EBL 65300
  58. #endif
  59. /* get_record flags */
  60. #define RES_ONLY_TYPE 1 /* return only the specified type records */
  61. #define RES_AR 2 /* return also the additional records */
  62. /* counter for failed DNS requests
  63. */
  64. struct dns_counters_h {
  65. counter_handle_t failed_dns_req;
  66. };
  67. extern struct dns_counters_h dns_cnts_h;
  68. extern struct dns_func_t dns_func;
  69. /* query union*/
  70. union dns_query{
  71. HEADER hdr;
  72. unsigned char buff[MAX_QUERY_SIZE];
  73. };
  74. /* rdata struct*/
  75. struct rdata {
  76. unsigned short type;
  77. unsigned short pclass;
  78. unsigned int ttl;
  79. void* rdata;
  80. struct rdata* next;
  81. unsigned char name_len; /* name length w/o the terminating 0 */
  82. char name[1]; /* null terminated name (len=name_len+1) */
  83. };
  84. /* real size of the structure */
  85. #define RDATA_SIZE(s) (sizeof(struct rdata)+(s).name_len) /* +1-1 */
  86. /* srv rec. struct*/
  87. struct srv_rdata {
  88. unsigned short priority;
  89. unsigned short weight;
  90. unsigned short port;
  91. unsigned char name_len; /* name length w/o the terminating 0 */
  92. char name[1]; /* null terminated name (len=name_len+1) */
  93. };
  94. /* real size of the structure */
  95. #define SRV_RDATA_SIZE(s) (sizeof(struct srv_rdata)+(s).name_len)
  96. /* naptr rec. struct*/
  97. struct naptr_rdata {
  98. char* flags; /* points inside str_table */
  99. char* services; /* points inside str_table */
  100. char* regexp; /* points inside str_table */
  101. char* repl; /* points inside str_table, null terminated */
  102. unsigned short order;
  103. unsigned short pref;
  104. unsigned char flags_len;
  105. unsigned char services_len;
  106. unsigned char regexp_len;
  107. unsigned char repl_len; /* not currently used */
  108. char str_table[1]; /* contains all the strings */
  109. };
  110. /* real size of the structure */
  111. #define NAPTR_RDATA_SIZE(s) (sizeof(struct naptr_rdata) \
  112. + (s).flags_len \
  113. + (s).services_len \
  114. + (s).regexp_len \
  115. + (s).repl_len + 1 - 1)
  116. /* A rec. struct */
  117. struct a_rdata {
  118. unsigned char ip[4];
  119. };
  120. struct aaaa_rdata {
  121. unsigned char ip6[16];
  122. };
  123. /* cname rec. struct*/
  124. struct cname_rdata {
  125. unsigned char name_len; /* name length w/o the terminating 0 */
  126. char name[1]; /* null terminated name (len=name_len+1) */
  127. };
  128. /* real size of the structure */
  129. #define CNAME_RDATA_SIZE(s) (sizeof(struct cname_rdata)+(s).name_len)
  130. /* dns character-string */
  131. struct dns_cstr{
  132. char* cstr; /* pointer to null term. string */
  133. unsigned char cstr_len;
  134. };
  135. /* txt rec. struct */
  136. struct txt_rdata {
  137. unsigned short cstr_no; /* number of strings */
  138. unsigned short tslen; /* total strings table len */
  139. struct dns_cstr txt[1]; /* at least 1 */
  140. /* all txt[*].cstr point inside a string table at the end of the struct.*/
  141. };
  142. #define TXT_RDATA_SIZE(s) \
  143. (sizeof(struct txt_rdata)+((s).cstr_no-1)*sizeof(struct dns_cstr)+\
  144. (s).tslen)
  145. /* ebl rec. struct, see
  146. http://tools.ietf.org/html/draft-ietf-enum-branch-location-record-03 */
  147. struct ebl_rdata {
  148. char* separator; /* points inside str_table */
  149. char* apex; /* point inside str_table */
  150. unsigned char separator_len; /* separator len w/o the terminating 0 */
  151. unsigned char apex_len; /* apex len w/p the terminating 0 */
  152. unsigned char position;
  153. char str_table[1]; /* contains the 2 strings: separator and apex */
  154. };
  155. #define EBL_RDATA_SIZE(s) \
  156. (sizeof(struct ebl_rdata)-1+(s).separator_len+1+(s).apex_len+1)
  157. struct ptr_rdata {
  158. unsigned char ptrdname_len; /* name length w/o the terminating 0 */
  159. char ptrdname[1]; /* null terminated name (len=name_len+1) */
  160. };
  161. /* real size of the structure */
  162. #define PTR_RDATA_SIZE(s) (sizeof(struct ptr_rdata)-1+(s).ptrdname_len+1)
  163. #ifdef HAVE_RESOLV_RES
  164. int match_search_list(const struct __res_state* res, char* name);
  165. #endif
  166. struct rdata* get_record(char* name, int type, int flags);
  167. void free_rdata_list(struct rdata* head);
  168. #define rev_resolvehost(ip)\
  169. gethostbyaddr((char*)(ip)->u.addr, (ip)->len, (ip)->af)
  170. #define HEX2I(c) \
  171. ( (((c)>='0') && ((c)<='9'))? (c)-'0' : \
  172. (((c)>='A') && ((c)<='F'))? ((c)-'A')+10 : \
  173. (((c)>='a') && ((c)<='f'))? ((c)-'a')+10 : -1 )
  174. /* converts a str to an ipv4 address, returns the address or 0 on error
  175. Warning: the result is a pointer to a statically allocated structure */
  176. static inline struct ip_addr* str2ip(str* st)
  177. {
  178. int i;
  179. unsigned char *limit;
  180. static struct ip_addr ip;
  181. unsigned char* s;
  182. s=(unsigned char*)st->s;
  183. /*init*/
  184. ip.u.addr32[0]=0;
  185. i=0;
  186. limit=(unsigned char*)(st->s + st->len);
  187. for(;s<limit ;s++){
  188. if (*s=='.'){
  189. i++;
  190. if (i>3) goto error_dots;
  191. }else if ( (*s <= '9' ) && (*s >= '0') ){
  192. ip.u.addr[i]=ip.u.addr[i]*10+*s-'0';
  193. }else{
  194. //error unknown char
  195. goto error_char;
  196. }
  197. }
  198. if (i<3) goto error_dots;
  199. ip.af=AF_INET;
  200. ip.len=4;
  201. return &ip;
  202. error_dots:
  203. #ifdef RESOLVE_DBG
  204. DBG("str2ip: ERROR: too %s dots in [%.*s]\n", (i>3)?"many":"few",
  205. st->len, st->s);
  206. #endif
  207. return 0;
  208. error_char:
  209. /*
  210. DBG("str2ip: WARNING: unexpected char %c in [%.*s]\n", *s, st->len, st->s);
  211. */
  212. return 0;
  213. }
  214. /* returns an ip_addr struct.; on error returns 0
  215. * the ip_addr struct is static, so subsequent calls will destroy its content*/
  216. static inline struct ip_addr* str2ip6(str* st)
  217. {
  218. int i, idx1, rest;
  219. int no_colons;
  220. int double_colon;
  221. int hex;
  222. static struct ip_addr ip;
  223. unsigned short* addr_start;
  224. unsigned short addr_end[8];
  225. unsigned short* addr;
  226. unsigned char* limit;
  227. unsigned char* s;
  228. /* init */
  229. if ((st->len) && (st->s[0]=='[')){
  230. /* skip over [ ] */
  231. if (st->s[st->len-1]!=']') goto error_char;
  232. s=(unsigned char*)(st->s+1);
  233. limit=(unsigned char*)(st->s+st->len-1);
  234. }else{
  235. s=(unsigned char*)st->s;
  236. limit=(unsigned char*)(st->s+st->len);
  237. }
  238. i=idx1=rest=0;
  239. double_colon=0;
  240. no_colons=0;
  241. ip.af=AF_INET6;
  242. ip.len=16;
  243. addr_start=ip.u.addr16;
  244. addr=addr_start;
  245. memset(addr_start, 0 , 8*sizeof(unsigned short));
  246. memset(addr_end, 0 , 8*sizeof(unsigned short));
  247. for (; s<limit; s++){
  248. if (*s==':'){
  249. no_colons++;
  250. if (no_colons>7) goto error_too_many_colons;
  251. if (double_colon){
  252. idx1=i;
  253. i=0;
  254. if (addr==addr_end) goto error_colons;
  255. addr=addr_end;
  256. }else{
  257. double_colon=1;
  258. addr[i]=htons(addr[i]);
  259. i++;
  260. }
  261. }else if ((hex=HEX2I(*s))>=0){
  262. addr[i]=addr[i]*16+hex;
  263. double_colon=0;
  264. }else{
  265. /* error, unknown char */
  266. goto error_char;
  267. }
  268. }
  269. if (!double_colon){ /* not ending in ':' */
  270. addr[i]=htons(addr[i]);
  271. i++;
  272. }
  273. /* if address contained '::' fix it */
  274. if (addr==addr_end){
  275. rest=8-i-idx1;
  276. memcpy(addr_start+idx1+rest, addr_end, i*sizeof(unsigned short));
  277. }else{
  278. /* no double colons inside */
  279. if (no_colons<7) goto error_too_few_colons;
  280. }
  281. /*
  282. DBG("str2ip6: idx1=%d, rest=%d, no_colons=%d, hex=%x\n",
  283. idx1, rest, no_colons, hex);
  284. DBG("str2ip6: address %x:%x:%x:%x:%x:%x:%x:%x\n",
  285. addr_start[0], addr_start[1], addr_start[2],
  286. addr_start[3], addr_start[4], addr_start[5],
  287. addr_start[6], addr_start[7] );
  288. */
  289. return &ip;
  290. error_too_many_colons:
  291. #ifdef RESOLVE_DBG
  292. DBG("str2ip6: ERROR: too many colons in [%.*s]\n", st->len, st->s);
  293. #endif
  294. return 0;
  295. error_too_few_colons:
  296. #ifdef RESOLVE_DBG
  297. DBG("str2ip6: ERROR: too few colons in [%.*s]\n", st->len, st->s);
  298. #endif
  299. return 0;
  300. error_colons:
  301. #ifdef RESOLVE_DBG
  302. DBG("str2ip6: ERROR: too many double colons in [%.*s]\n", st->len, st->s);
  303. #endif
  304. return 0;
  305. error_char:
  306. /*
  307. DBG("str2ip6: WARNING: unexpected char %c in [%.*s]\n", *s, st->len,
  308. st->s);*/
  309. return 0;
  310. }
  311. struct hostent* _sip_resolvehost(str* name, unsigned short* port, char* proto);
  312. /* gethostbyname wrapper, handles ip/ipv6 automatically */
  313. static inline struct hostent* _resolvehost(char* name)
  314. {
  315. static struct hostent* he=0;
  316. #ifdef HAVE_GETIPNODEBYNAME
  317. int err;
  318. static struct hostent* he2=0;
  319. #endif
  320. #ifndef DNS_IP_HACK
  321. int len;
  322. #endif
  323. #ifdef DNS_IP_HACK
  324. struct ip_addr* ip;
  325. str s;
  326. s.s = (char*)name;
  327. s.len = strlen(name);
  328. /* check if it's an ip address */
  329. if ( ((ip=str2ip(&s))!=0)
  330. || ((ip=str2ip6(&s))!=0)
  331. ){
  332. /* we are lucky, this is an ip address */
  333. return ip_addr2he(&s, ip);
  334. }
  335. #else /* DNS_IP_HACK */
  336. len=0;
  337. if (*name=='['){
  338. len=strlen(name);
  339. if (len && (name[len-1]==']')){
  340. name[len-1]=0; /* remove '[' */
  341. name++; /* skip '[' */
  342. goto skip_ipv4;
  343. }
  344. }
  345. #endif
  346. /* ipv4 */
  347. he=dns_func.sr_gethostbyname(name);
  348. if(he==0 && cfg_get(core, core_cfg, dns_try_ipv6)){
  349. #ifndef DNS_IP_HACK
  350. skip_ipv4:
  351. #endif
  352. /*try ipv6*/
  353. #ifdef HAVE_GETHOSTBYNAME2
  354. he=dns_func.sr_gethostbyname2(name, AF_INET6);
  355. #elif defined HAVE_GETIPNODEBYNAME
  356. /* on solaris 8 getipnodebyname has a memory leak,
  357. * after some time calls to it will fail with err=3
  358. * solution: patch your solaris 8 installation */
  359. if (he2) freehostent(he2);
  360. he=he2=getipnodebyname(name, AF_INET6, 0, &err);
  361. #else
  362. #error neither gethostbyname2 or getipnodebyname present
  363. #endif
  364. #ifndef DNS_IP_HACK
  365. if (len) name[len-2]=']'; /* restore */
  366. #endif
  367. }
  368. return he;
  369. }
  370. int resolv_init(void);
  371. /* callback/fixup functions executed by the configuration framework */
  372. void resolv_reinit(str *gname, str *name);
  373. int dns_reinit_fixup(void *handle, str *gname, str *name, void **val);
  374. int dns_try_ipv6_fixup(void *handle, str *gname, str *name, void **val);
  375. void reinit_proto_prefs(str *gname, str *name);
  376. struct dns_srv_proto {
  377. char proto;
  378. int proto_pref;
  379. };
  380. void create_srv_name(char proto, str *name, char *srv);
  381. size_t create_srv_pref_list(char *proto, struct dns_srv_proto *list);
  382. #ifdef DNS_WATCHDOG_SUPPORT
  383. /* callback function that is called by the child processes
  384. * when they reinitialize the resolver
  385. *
  386. * Note, that this callback is called by each chiled process separately!!!
  387. * If the callback is registered after forking, only the child process
  388. * that installs the hook will call the callback.
  389. */
  390. typedef void (*on_resolv_reinit)(str*);
  391. int register_resolv_reinit_cb(on_resolv_reinit cb);
  392. #endif
  393. int sip_hostport2su(union sockaddr_union* su, str* host, unsigned short port,
  394. char* proto);
  395. /* wrappers */
  396. #ifdef USE_DNS_CACHE
  397. #define resolvehost dns_resolvehost
  398. #define sip_resolvehost dns_sip_resolvehost
  399. #else
  400. #define resolvehost _resolvehost
  401. #define sip_resolvehost _sip_resolvehost
  402. #endif
  403. #ifdef USE_NAPTR
  404. /* NAPTR helper functions */
  405. typedef unsigned int naptr_bmp_t; /* type used for keeping track of tried
  406. naptr records*/
  407. #define MAX_NAPTR_RRS (sizeof(naptr_bmp_t)*8)
  408. /* use before first call to naptr_sip_iterate */
  409. #define naptr_iterate_init(bmp) \
  410. do{ \
  411. *(bmp)=0; \
  412. }while(0) \
  413. struct rdata* naptr_sip_iterate(struct rdata* naptr_head,
  414. naptr_bmp_t* tried,
  415. str* srv_name, char* proto);
  416. /* returns sip proto if valis sip naptr record, .-1 otherwise */
  417. char naptr_get_sip_proto(struct naptr_rdata* n);
  418. /* returns true if new_proto is preferred over old_proto */
  419. int naptr_proto_preferred(char new_proto, char old_proto);
  420. /* returns true if we support the protocol */
  421. int naptr_proto_supported(char proto);
  422. /* choose between 2 naptr records, should take into account local
  423. * preferences too
  424. * returns 1 if the new record was selected, 0 otherwise */
  425. int naptr_choose (struct naptr_rdata** crt, char* crt_proto,
  426. struct naptr_rdata* n , char n_proto);
  427. #endif/* USE_NAPTR */
  428. struct hostent* no_naptr_srv_sip_resolvehost(str* name, unsigned short* port,
  429. char* proto);
  430. #endif