resolve.h 13 KB

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