resolve.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. #ifdef __OS_darwin
  47. #include <arpa/nameser_compat.h>
  48. #endif
  49. #include "ip_addr.h"
  50. #ifdef USE_DNS_CACHE
  51. #include "dns_wrappers.h"
  52. #endif
  53. /* define RESOLVE_DBG for debugging info (very noisy) */
  54. #define RESOLVE_DBG
  55. /* define NAPTR_DBG for naptr related debugging info (very noisy) */
  56. #define NAPTR_DBG
  57. #define MAX_QUERY_SIZE 8192
  58. #define ANS_SIZE 8192
  59. #define DNS_HDR_SIZE 12
  60. #define MAX_DNS_NAME 256
  61. #define MAX_DNS_STRING 255
  62. #ifndef T_EBL
  63. /** not official yet - iEnum. */
  64. #define T_EBL 65300
  65. #endif
  66. /* get_record flags */
  67. #define RES_ONLY_TYPE 1 /* return only the specified type records */
  68. #define RES_AR 2 /* return also the additional records */
  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 class;
  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. #ifdef USE_IPV6
  215. /* returns an ip_addr struct.; on error returns 0
  216. * the ip_addr struct is static, so subsequent calls will destroy its content*/
  217. static inline struct ip_addr* str2ip6(str* st)
  218. {
  219. int i, idx1, rest;
  220. int no_colons;
  221. int double_colon;
  222. int hex;
  223. static struct ip_addr ip;
  224. unsigned short* addr_start;
  225. unsigned short addr_end[8];
  226. unsigned short* addr;
  227. unsigned char* limit;
  228. unsigned char* s;
  229. /* init */
  230. if ((st->len) && (st->s[0]=='[')){
  231. /* skip over [ ] */
  232. if (st->s[st->len-1]!=']') goto error_char;
  233. s=(unsigned char*)(st->s+1);
  234. limit=(unsigned char*)(st->s+st->len-1);
  235. }else{
  236. s=(unsigned char*)st->s;
  237. limit=(unsigned char*)(st->s+st->len);
  238. }
  239. i=idx1=rest=0;
  240. double_colon=0;
  241. no_colons=0;
  242. ip.af=AF_INET6;
  243. ip.len=16;
  244. addr_start=ip.u.addr16;
  245. addr=addr_start;
  246. memset(addr_start, 0 , 8*sizeof(unsigned short));
  247. memset(addr_end, 0 , 8*sizeof(unsigned short));
  248. for (; s<limit; s++){
  249. if (*s==':'){
  250. no_colons++;
  251. if (no_colons>7) goto error_too_many_colons;
  252. if (double_colon){
  253. idx1=i;
  254. i=0;
  255. if (addr==addr_end) goto error_colons;
  256. addr=addr_end;
  257. }else{
  258. double_colon=1;
  259. addr[i]=htons(addr[i]);
  260. i++;
  261. }
  262. }else if ((hex=HEX2I(*s))>=0){
  263. addr[i]=addr[i]*16+hex;
  264. double_colon=0;
  265. }else{
  266. /* error, unknown char */
  267. goto error_char;
  268. }
  269. }
  270. if (!double_colon){ /* not ending in ':' */
  271. addr[i]=htons(addr[i]);
  272. i++;
  273. }
  274. /* if address contained '::' fix it */
  275. if (addr==addr_end){
  276. rest=8-i-idx1;
  277. memcpy(addr_start+idx1+rest, addr_end, i*sizeof(unsigned short));
  278. }else{
  279. /* no double colons inside */
  280. if (no_colons<7) goto error_too_few_colons;
  281. }
  282. /*
  283. DBG("str2ip6: idx1=%d, rest=%d, no_colons=%d, hex=%x\n",
  284. idx1, rest, no_colons, hex);
  285. DBG("str2ip6: address %x:%x:%x:%x:%x:%x:%x:%x\n",
  286. addr_start[0], addr_start[1], addr_start[2],
  287. addr_start[3], addr_start[4], addr_start[5],
  288. addr_start[6], addr_start[7] );
  289. */
  290. return &ip;
  291. error_too_many_colons:
  292. #ifdef RESOLVE_DBG
  293. DBG("str2ip6: ERROR: too many colons in [%.*s]\n", st->len, st->s);
  294. #endif
  295. return 0;
  296. error_too_few_colons:
  297. #ifdef RESOLVE_DBG
  298. DBG("str2ip6: ERROR: too few colons in [%.*s]\n", st->len, st->s);
  299. #endif
  300. return 0;
  301. error_colons:
  302. #ifdef RESOLVE_DBG
  303. DBG("str2ip6: ERROR: too many double colons in [%.*s]\n", st->len, st->s);
  304. #endif
  305. return 0;
  306. error_char:
  307. /*
  308. DBG("str2ip6: WARNING: unexpected char %c in [%.*s]\n", *s, st->len,
  309. st->s);*/
  310. return 0;
  311. }
  312. #endif /* USE_IPV6 */
  313. struct hostent* _sip_resolvehost(str* name, unsigned short* port, char* proto);
  314. /* gethostbyname wrapper, handles ip/ipv6 automatically */
  315. static inline struct hostent* _resolvehost(char* name)
  316. {
  317. static struct hostent* he=0;
  318. #ifdef HAVE_GETIPNODEBYNAME
  319. #ifdef USE_IPV6
  320. int err;
  321. static struct hostent* he2=0;
  322. #endif
  323. #endif
  324. #ifndef DNS_IP_HACK
  325. #ifdef USE_IPV6
  326. int len;
  327. #endif
  328. #endif
  329. #ifdef DNS_IP_HACK
  330. struct ip_addr* ip;
  331. str s;
  332. s.s = (char*)name;
  333. s.len = strlen(name);
  334. /* check if it's an ip address */
  335. if ( ((ip=str2ip(&s))!=0)
  336. #ifdef USE_IPV6
  337. || ((ip=str2ip6(&s))!=0)
  338. #endif
  339. ){
  340. /* we are lucky, this is an ip address */
  341. return ip_addr2he(&s, ip);
  342. }
  343. #else /* DNS_IP_HACK */
  344. #ifdef USE_IPV6
  345. len=0;
  346. if (*name=='['){
  347. len=strlen(name);
  348. if (len && (name[len-1]==']')){
  349. name[len-1]=0; /* remove '[' */
  350. name++; /* skip '[' */
  351. goto skip_ipv4;
  352. }
  353. }
  354. #endif
  355. #endif
  356. /* ipv4 */
  357. he=gethostbyname(name);
  358. #ifdef USE_IPV6
  359. if(he==0 && cfg_get(core, core_cfg, dns_try_ipv6)){
  360. #ifndef DNS_IP_HACK
  361. skip_ipv4:
  362. #endif
  363. /*try ipv6*/
  364. #ifdef HAVE_GETHOSTBYNAME2
  365. he=gethostbyname2(name, AF_INET6);
  366. #elif defined HAVE_GETIPNODEBYNAME
  367. /* on solaris 8 getipnodebyname has a memory leak,
  368. * after some time calls to it will fail with err=3
  369. * solution: patch your solaris 8 installation */
  370. if (he2) freehostent(he2);
  371. he=he2=getipnodebyname(name, AF_INET6, 0, &err);
  372. #else
  373. #error neither gethostbyname2 or getipnodebyname present
  374. #endif
  375. #ifndef DNS_IP_HACK
  376. if (len) name[len-2]=']'; /* restore */
  377. #endif
  378. }
  379. #endif
  380. return he;
  381. }
  382. int resolv_init();
  383. /* callback/fixup functions executed by the configuration framework */
  384. void resolv_reinit(str *gname, str *name);
  385. int dns_reinit_fixup(void *handle, str *gname, str *name, void **val);
  386. int dns_try_ipv6_fixup(void *handle, str *gname, str *name, void **val);
  387. void reinit_naptr_proto_prefs(str *gname, str *name);
  388. #ifdef DNS_WATCHDOG_SUPPORT
  389. /* callback function that is called by the child processes
  390. * when they reinitialize the resolver
  391. *
  392. * Note, that this callback is called by each chiled process separately!!!
  393. * If the callback is registered after forking, only the child process
  394. * that installs the hook will call the callback.
  395. */
  396. typedef void (*on_resolv_reinit)(str*);
  397. int register_resolv_reinit_cb(on_resolv_reinit cb);
  398. #endif
  399. int sip_hostport2su(union sockaddr_union* su, str* host, unsigned short port,
  400. char* proto);
  401. /* wrappers */
  402. #ifdef USE_DNS_CACHE
  403. #define resolvehost dns_resolvehost
  404. #define sip_resolvehost dns_sip_resolvehost
  405. #else
  406. #define resolvehost _resolvehost
  407. #define sip_resolvehost _sip_resolvehost
  408. #endif
  409. #ifdef USE_NAPTR
  410. /* NAPTR helper functions */
  411. typedef unsigned int naptr_bmp_t; /* type used for keeping track of tried
  412. naptr records*/
  413. #define MAX_NAPTR_RRS (sizeof(naptr_bmp_t)*8)
  414. /* use before first call to naptr_sip_iterate */
  415. #define naptr_iterate_init(bmp) \
  416. do{ \
  417. *(bmp)=0; \
  418. }while(0) \
  419. struct rdata* naptr_sip_iterate(struct rdata* naptr_head,
  420. naptr_bmp_t* tried,
  421. str* srv_name, char* proto);
  422. /* returns sip proto if valis sip naptr record, .-1 otherwise */
  423. char naptr_get_sip_proto(struct naptr_rdata* n);
  424. /* returns true if new_proto is preferred over old_proto */
  425. int naptr_proto_preferred(char new_proto, char old_proto);
  426. /* returns true if we support the protocol */
  427. int naptr_proto_supported(char proto);
  428. /* choose between 2 naptr records, should take into account local
  429. * preferences too
  430. * returns 1 if the new record was selected, 0 otherwise */
  431. int naptr_choose (struct naptr_rdata** crt, char* crt_proto,
  432. struct naptr_rdata* n , char n_proto);
  433. #endif/* USE_NAPTR */
  434. #endif