resolve.h 13 KB

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