resolve.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. */
  37. #ifndef __resolve_h
  38. #define __resolve_h
  39. #include <sys/types.h>
  40. #include <sys/socket.h>
  41. #include <netdb.h>
  42. #include <arpa/nameser.h>
  43. #ifdef __OS_darwin
  44. #include <arpa/nameser_compat.h>
  45. #endif
  46. #include "ip_addr.h"
  47. #ifdef USE_DNS_CACHE
  48. #include "dns_wrappers.h"
  49. #endif
  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. /* get_record flags */
  56. #define RES_ONLY_TYPE 1 /* return only the specified type records */
  57. #define RES_AR 2 /* return also the additional records */
  58. /* query union*/
  59. union dns_query{
  60. HEADER hdr;
  61. unsigned char buff[MAX_QUERY_SIZE];
  62. };
  63. /* rdata struct*/
  64. struct rdata {
  65. unsigned short type;
  66. unsigned short class;
  67. unsigned int ttl;
  68. void* rdata;
  69. struct rdata* next;
  70. unsigned char name_len; /* name length w/o the terminating 0 */
  71. char name[1]; /* null terminated name (len=name_len+1) */
  72. };
  73. /* real size of the structure */
  74. #define RDATA_SIZE(s) (sizeof(struct rdata)+(s).name_len) /* +1-1 */
  75. /* srv rec. struct*/
  76. struct srv_rdata {
  77. unsigned short priority;
  78. unsigned short weight;
  79. unsigned short port;
  80. unsigned char name_len; /* name length w/o the terminating 0 */
  81. char name[1]; /* null terminated name (len=name_len+1) */
  82. };
  83. /* real size of the structure */
  84. #define SRV_RDATA_SIZE(s) (sizeof(struct srv_rdata)+(s).name_len)
  85. /* naptr rec. struct*/
  86. struct naptr_rdata {
  87. char* flags; /* points inside str_table */
  88. char* services; /* points inside str_table */
  89. char* regexp; /* points inside str_table */
  90. char* repl; /* points inside str_table, null terminated */
  91. unsigned short order;
  92. unsigned short pref;
  93. unsigned char flags_len;
  94. unsigned char services_len;
  95. unsigned char regexp_len;
  96. unsigned char repl_len; /* not currently used */
  97. char str_table[1]; /* contains all the strings */
  98. };
  99. /* real size of the structure */
  100. #define NAPTR_RDATA_SIZE(s) (sizeof(struct naptr_rdata) \
  101. + (s).flags_len \
  102. + (s).services_len \
  103. + (s).regexp_len \
  104. + (s).repl_len + 1 - 1)
  105. /* A rec. struct */
  106. struct a_rdata {
  107. unsigned char ip[4];
  108. };
  109. struct aaaa_rdata {
  110. unsigned char ip6[16];
  111. };
  112. /* cname rec. struct*/
  113. struct cname_rdata {
  114. unsigned char name_len; /* name length w/o the terminating 0 */
  115. char name[1]; /* null terminated name (len=name_len+1) */
  116. };
  117. /* real size of the structure */
  118. #define CNAME_RDATA_SIZE(s) (sizeof(struct cname_rdata)+(s).name_len)
  119. struct rdata* get_record(char* name, int type, int flags);
  120. void free_rdata_list(struct rdata* head);
  121. extern int dns_try_ipv6;
  122. #define rev_resolvehost(ip)\
  123. gethostbyaddr((char*)(ip)->u.addr, (ip)->len, (ip)->af);
  124. #define HEX2I(c) \
  125. ( (((c)>='0') && ((c)<='9'))? (c)-'0' : \
  126. (((c)>='A') && ((c)<='F'))? ((c)-'A')+10 : \
  127. (((c)>='a') && ((c)<='f'))? ((c)-'a')+10 : -1 )
  128. /* converts a str to an ipv4 address, returns the address or 0 on error
  129. Warning: the result is a pointer to a statically allocated structure */
  130. static inline struct ip_addr* str2ip(str* st)
  131. {
  132. int i;
  133. unsigned char *limit;
  134. static struct ip_addr ip;
  135. unsigned char* s;
  136. s=(unsigned char*)st->s;
  137. /*init*/
  138. ip.u.addr32[0]=0;
  139. i=0;
  140. limit=(unsigned char*)(st->s + st->len);
  141. for(;s<limit ;s++){
  142. if (*s=='.'){
  143. i++;
  144. if (i>3) goto error_dots;
  145. }else if ( (*s <= '9' ) && (*s >= '0') ){
  146. ip.u.addr[i]=ip.u.addr[i]*10+*s-'0';
  147. }else{
  148. //error unknown char
  149. goto error_char;
  150. }
  151. }
  152. if (i<3) goto error_dots;
  153. ip.af=AF_INET;
  154. ip.len=4;
  155. return &ip;
  156. error_dots:
  157. DBG("str2ip: ERROR: too %s dots in [%.*s]\n", (i>3)?"many":"few",
  158. st->len, st->s);
  159. return 0;
  160. error_char:
  161. /*
  162. DBG("str2ip: WARNING: unexpected char %c in [%.*s]\n", *s, st->len, st->s);
  163. */
  164. return 0;
  165. }
  166. /* returns an ip_addr struct.; on error returns 0
  167. * the ip_addr struct is static, so subsequent calls will destroy its content*/
  168. static inline struct ip_addr* str2ip6(str* st)
  169. {
  170. int i, idx1, rest;
  171. int no_colons;
  172. int double_colon;
  173. int hex;
  174. static struct ip_addr ip;
  175. unsigned short* addr_start;
  176. unsigned short addr_end[8];
  177. unsigned short* addr;
  178. unsigned char* limit;
  179. unsigned char* s;
  180. /* init */
  181. if ((st->len) && (st->s[0]=='[')){
  182. /* skip over [ ] */
  183. if (st->s[st->len-1]!=']') goto error_char;
  184. s=(unsigned char*)(st->s+1);
  185. limit=(unsigned char*)(st->s+st->len-1);
  186. }else{
  187. s=(unsigned char*)st->s;
  188. limit=(unsigned char*)(st->s+st->len);
  189. }
  190. i=idx1=rest=0;
  191. double_colon=0;
  192. no_colons=0;
  193. ip.af=AF_INET6;
  194. ip.len=16;
  195. addr_start=ip.u.addr16;
  196. addr=addr_start;
  197. memset(addr_start, 0 , 8*sizeof(unsigned short));
  198. memset(addr_end, 0 , 8*sizeof(unsigned short));
  199. for (; s<limit; s++){
  200. if (*s==':'){
  201. no_colons++;
  202. if (no_colons>7) goto error_too_many_colons;
  203. if (double_colon){
  204. idx1=i;
  205. i=0;
  206. if (addr==addr_end) goto error_colons;
  207. addr=addr_end;
  208. }else{
  209. double_colon=1;
  210. addr[i]=htons(addr[i]);
  211. i++;
  212. }
  213. }else if ((hex=HEX2I(*s))>=0){
  214. addr[i]=addr[i]*16+hex;
  215. double_colon=0;
  216. }else{
  217. /* error, unknown char */
  218. goto error_char;
  219. }
  220. }
  221. if (!double_colon){ /* not ending in ':' */
  222. addr[i]=htons(addr[i]);
  223. i++;
  224. }
  225. /* if address contained '::' fix it */
  226. if (addr==addr_end){
  227. rest=8-i-idx1;
  228. memcpy(addr_start+idx1+rest, addr_end, i*sizeof(unsigned short));
  229. }else{
  230. /* no double colons inside */
  231. if (no_colons<7) goto error_too_few_colons;
  232. }
  233. /*
  234. DBG("str2ip6: idx1=%d, rest=%d, no_colons=%d, hex=%x\n",
  235. idx1, rest, no_colons, hex);
  236. DBG("str2ip6: address %x:%x:%x:%x:%x:%x:%x:%x\n",
  237. addr_start[0], addr_start[1], addr_start[2],
  238. addr_start[3], addr_start[4], addr_start[5],
  239. addr_start[6], addr_start[7] );
  240. */
  241. return &ip;
  242. error_too_many_colons:
  243. DBG("str2ip6: ERROR: too many colons in [%.*s]\n", st->len, st->s);
  244. return 0;
  245. error_too_few_colons:
  246. DBG("str2ip6: ERROR: too few colons in [%.*s]\n", st->len, st->s);
  247. return 0;
  248. error_colons:
  249. DBG("str2ip6: ERROR: too many double colons in [%.*s]\n", st->len, st->s);
  250. return 0;
  251. error_char:
  252. /*
  253. DBG("str2ip6: WARNING: unexpected char %c in [%.*s]\n", *s, st->len,
  254. st->s);*/
  255. return 0;
  256. }
  257. struct hostent* _sip_resolvehost(str* name, unsigned short* port, int proto);
  258. /* gethostbyname wrapper, handles ip/ipv6 automatically */
  259. static inline struct hostent* _resolvehost(char* name)
  260. {
  261. static struct hostent* he=0;
  262. #ifdef HAVE_GETIPNODEBYNAME
  263. int err;
  264. static struct hostent* he2=0;
  265. #endif
  266. #ifndef DNS_IP_HACK
  267. #ifdef USE_IPV6
  268. int len;
  269. #endif
  270. #endif
  271. #ifdef DNS_IP_HACK
  272. struct ip_addr* ip;
  273. str s;
  274. s.s = (char*)name;
  275. s.len = strlen(name);
  276. /* check if it's an ip address */
  277. if ( ((ip=str2ip(&s))!=0)
  278. #ifdef USE_IPV6
  279. || ((ip=str2ip6(&s))!=0)
  280. #endif
  281. ){
  282. /* we are lucky, this is an ip address */
  283. return ip_addr2he(&s, ip);
  284. }
  285. #else /* DNS_IP_HACK */
  286. #ifdef USE_IPV6
  287. len=0;
  288. if (*name=='['){
  289. len=strlen(name);
  290. if (len && (name[len-1]==']')){
  291. name[len-1]=0; /* remove '[' */
  292. name++; /* skip '[' */
  293. goto skip_ipv4;
  294. }
  295. }
  296. #endif
  297. #endif
  298. /* ipv4 */
  299. he=gethostbyname(name);
  300. #ifdef USE_IPV6
  301. if(he==0 && dns_try_ipv6){
  302. #ifndef DNS_IP_HACK
  303. skip_ipv4:
  304. #endif
  305. /*try ipv6*/
  306. #ifdef HAVE_GETHOSTBYNAME2
  307. he=gethostbyname2(name, AF_INET6);
  308. #elif defined HAVE_GETIPNODEBYNAME
  309. /* on solaris 8 getipnodebyname has a memory leak,
  310. * after some time calls to it will fail with err=3
  311. * solution: patch your solaris 8 installation */
  312. if (he2) freehostent(he2);
  313. he=he2=getipnodebyname(name, AF_INET6, 0, &err);
  314. #else
  315. #error neither gethostbyname2 or getipnodebyname present
  316. #endif
  317. #ifndef DNS_IP_HACK
  318. if (len) name[len-2]=']'; /* restore */
  319. #endif
  320. }
  321. #endif
  322. return he;
  323. }
  324. int resolv_init();
  325. int sip_hostport2su(union sockaddr_union* su, str* host, unsigned short port,
  326. int proto);
  327. /* wrappers */
  328. #ifdef USE_DNS_CACHE
  329. #define resolvehost dns_resolvehost
  330. #define sip_resolvehost dns_sip_resolvehost
  331. #else
  332. #define resolvehost _resolvehost
  333. #define sip_resolvehost _sip_resolvehost
  334. #endif
  335. #endif