resolve.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /* $Id$*/
  2. /*
  3. *
  4. * Copyright (C) 2001-2003 Fhg Fokus
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser 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. * For a license to use the ser software under conditions
  14. * other than those described here, or to purchase support for this
  15. * software, please contact iptel.org by e-mail at the following addresses:
  16. * [email protected]
  17. *
  18. * ser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. */
  27. #include <sys/types.h>
  28. #include <netinet/in.h>
  29. #include <arpa/nameser.h>
  30. #include <resolv.h>
  31. #include <string.h>
  32. #include "resolve.h"
  33. #include "dprint.h"
  34. #include "mem/mem.h"
  35. #include "ip_addr.h"
  36. /* mallocs for local stuff */
  37. #define local_malloc pkg_malloc
  38. #define local_free pkg_free
  39. /* skips over a domain name in a dns message
  40. * (it can be a sequence of labels ending in \0, a pointer or
  41. * a sequence of labels ending in a pointer -- see rfc1035
  42. * returns pointer after the domain name or null on error*/
  43. unsigned char* dns_skipname(unsigned char* p, unsigned char* end)
  44. {
  45. while(p<end){
  46. /* check if \0 (root label length) */
  47. if (*p==0){
  48. p+=1;
  49. break;
  50. }
  51. /* check if we found a pointer */
  52. if (((*p)&0xc0)==0xc0){
  53. /* if pointer skip over it (2 bytes) & we found the end */
  54. p+=2;
  55. break;
  56. }
  57. /* normal label */
  58. p+=*p+1;
  59. }
  60. return (p>=end)?0:p;
  61. }
  62. /* parses the srv record into a srv_rdata structure
  63. * msg - pointer to the dns message
  64. * end - pointer to the end of the message
  65. * rdata - pointer to the rdata part of the srv answer
  66. * returns 0 on error, or a dyn. alloc'ed srv_rdata structure */
  67. /* SRV rdata format:
  68. * 111111
  69. * 0123456789012345
  70. * +----------------+
  71. * | priority |
  72. * |----------------|
  73. * | weight |
  74. * |----------------|
  75. * | port number |
  76. * |----------------|
  77. * | |
  78. * ~ name ~
  79. * | |
  80. * +----------------+
  81. */
  82. struct srv_rdata* dns_srv_parser( unsigned char* msg, unsigned char* end,
  83. unsigned char* rdata)
  84. {
  85. struct srv_rdata* srv;
  86. int len;
  87. srv=0;
  88. if ((rdata+6)>=end) goto error;
  89. srv=(struct srv_rdata*)local_malloc(sizeof(struct srv_rdata));
  90. if (srv==0){
  91. LOG(L_ERR, "ERROR: dns_srv_parser: out of memory\n");
  92. goto error;
  93. }
  94. memcpy((void*)&srv->priority, rdata, 2);
  95. memcpy((void*)&srv->weight, rdata+2, 2);
  96. memcpy((void*)&srv->port, rdata+4, 2);
  97. rdata+=6;
  98. srv->priority=ntohs(srv->priority);
  99. srv->weight=ntohs(srv->weight);
  100. srv->port=ntohs(srv->port);
  101. if ((len=dn_expand(msg, end, rdata, srv->name, MAX_DNS_NAME-1))==-1)
  102. goto error;
  103. /* add terminating 0 ? (warning: len=compressed name len) */
  104. return srv;
  105. error:
  106. if (srv) local_free(srv);
  107. return 0;
  108. }
  109. /* parses the naptr record into a naptr_rdata structure
  110. * msg - pointer to the dns message
  111. * end - pointer to the end of the message
  112. * rdata - pointer to the rdata part of the naptr answer
  113. * returns 0 on error, or a dyn. alloc'ed naptr_rdata structure */
  114. /* NAPTR rdata format:
  115. * 111111
  116. * 0123456789012345
  117. * +----------------+
  118. * | order |
  119. * |----------------|
  120. * | preference |
  121. * |----------------|
  122. * ~ flags ~
  123. * | (string) |
  124. * |----------------|
  125. * ~ services ~
  126. * | (string) |
  127. * |----------------|
  128. * ~ regexp ~
  129. * | (string) |
  130. * |----------------|
  131. * ~ replacement ~
  132. | (name) |
  133. * +----------------+
  134. */
  135. struct naptr_rdata* dns_naptr_parser( unsigned char* msg, unsigned char* end,
  136. unsigned char* rdata)
  137. {
  138. struct naptr_rdata* naptr;
  139. int len;
  140. naptr = 0;
  141. if ((rdata + 7) >= end) goto error;
  142. naptr=(struct naptr_rdata*)local_malloc(sizeof(struct naptr_rdata));
  143. if (naptr == 0){
  144. LOG(L_ERR, "ERROR: dns_naptr_parser: out of memory\n");
  145. goto error;
  146. }
  147. memcpy((void*)&naptr->order, rdata, 2);
  148. naptr->order=ntohs(naptr->order);
  149. memcpy((void*)&naptr->pref, rdata + 2, 2);
  150. naptr->pref=ntohs(naptr->pref);
  151. naptr->flags_len = (int)rdata[4];
  152. if ((rdata + 7 + naptr->flags_len) >= end) goto error;
  153. memcpy((void*)&naptr->flags, rdata + 5, naptr->flags_len);
  154. naptr->services_len = (int)rdata[5 + naptr->flags_len];
  155. if ((rdata + 7 + naptr->flags_len + naptr->services_len) >= end) goto error;
  156. memcpy((void*)&naptr->services, rdata + 6 + naptr->flags_len, naptr->services_len);
  157. naptr->regexp_len = (int)rdata[6 + naptr->flags_len + naptr->services_len];
  158. if ((rdata + 7 + naptr->flags_len + naptr->services_len +
  159. naptr->regexp_len) >= end) goto error;
  160. memcpy((void*)&naptr->regexp, rdata + 7 + naptr->flags_len +
  161. naptr->services_len, naptr->regexp_len);
  162. rdata = rdata + 7 + naptr->flags_len + naptr->services_len +
  163. naptr->regexp_len;
  164. if ((len=dn_expand(msg, end, rdata, naptr->repl, MAX_DNS_NAME-1)) == -1)
  165. goto error;
  166. /* add terminating 0 ? (warning: len=compressed name len) */
  167. return naptr;
  168. error:
  169. if (naptr) local_free(naptr);
  170. return 0;
  171. }
  172. /* parses a CNAME record into a cname_rdata structure */
  173. struct cname_rdata* dns_cname_parser( unsigned char* msg, unsigned char* end,
  174. unsigned char* rdata)
  175. {
  176. struct cname_rdata* cname;
  177. int len;
  178. cname=0;
  179. cname=(struct cname_rdata*)local_malloc(sizeof(struct cname_rdata));
  180. if(cname==0){
  181. LOG(L_ERR, "ERROR: dns_cname_parser: out of memory\n");
  182. goto error;
  183. }
  184. if ((len=dn_expand(msg, end, rdata, cname->name, MAX_DNS_NAME-1))==-1)
  185. goto error;
  186. return cname;
  187. error:
  188. if (cname) local_free(cname);
  189. return 0;
  190. }
  191. /* parses an A record rdata into an a_rdata structure
  192. * returns 0 on error or a dyn. alloc'ed a_rdata struct
  193. */
  194. struct a_rdata* dns_a_parser(unsigned char* rdata, unsigned char* end)
  195. {
  196. struct a_rdata* a;
  197. if (rdata+4>=end) goto error;
  198. a=(struct a_rdata*)local_malloc(sizeof(struct a_rdata));
  199. if (a==0){
  200. LOG(L_ERR, "ERROR: dns_a_parser: out of memory\n");
  201. goto error;
  202. }
  203. memcpy(a->ip, rdata, 4);
  204. return a;
  205. error:
  206. return 0;
  207. }
  208. /* parses an AAAA (ipv6) record rdata into an aaaa_rdata structure
  209. * returns 0 on error or a dyn. alloc'ed aaaa_rdata struct */
  210. struct aaaa_rdata* dns_aaaa_parser(unsigned char* rdata, unsigned char* end)
  211. {
  212. struct aaaa_rdata* aaaa;
  213. if (rdata+16>=end) goto error;
  214. aaaa=(struct aaaa_rdata*)local_malloc(sizeof(struct aaaa_rdata));
  215. if (aaaa==0){
  216. LOG(L_ERR, "ERROR: dns_aaaa_parser: out of memory\n");
  217. goto error;
  218. }
  219. memcpy(aaaa->ip6, rdata, 16);
  220. return aaaa;
  221. error:
  222. return 0;
  223. }
  224. /* frees completely a struct rdata list */
  225. void free_rdata_list(struct rdata* head)
  226. {
  227. struct rdata* l;
  228. for(l=head; l; l=l->next){
  229. /* free the parsed rdata*/
  230. if (l->rdata) local_free(l->rdata);
  231. local_free(l);
  232. }
  233. }
  234. /* gets the DNS records for name:type
  235. * returns a dyn. alloc'ed struct rdata linked list with the parsed responses
  236. * or 0 on error
  237. * see rfc1035 for the query/response format */
  238. struct rdata* get_record(char* name, int type)
  239. {
  240. int size;
  241. int qno, answers_no;
  242. int r;
  243. int ans_len;
  244. static union dns_query buff;
  245. unsigned char* p;
  246. unsigned char* t;
  247. unsigned char* end;
  248. static unsigned char answer[ANS_SIZE];
  249. unsigned short rtype, class, rdlength;
  250. unsigned int ttl;
  251. struct rdata* head;
  252. struct rdata** crt;
  253. struct rdata** last;
  254. struct rdata* rd;
  255. struct srv_rdata* srv_rd;
  256. struct srv_rdata* crt_srv;
  257. size=res_search(name, C_IN, type, buff.buff, sizeof(buff));
  258. if (size<0) {
  259. DBG("get_record: lookup(%s, %d) failed\n", name, type);
  260. goto not_found;
  261. }
  262. else if (size > sizeof(buff)) size=sizeof(buff);
  263. head=rd=0;
  264. last=crt=&head;
  265. p=buff.buff+DNS_HDR_SIZE;
  266. end=buff.buff+size;
  267. if (p>=end) goto error_boundary;
  268. qno=ntohs((unsigned short)buff.hdr.qdcount);
  269. for (r=0; r<qno; r++){
  270. /* skip the name of the question */
  271. if ((p=dns_skipname(p, end))==0) {
  272. LOG(L_ERR, "ERROR: get_record: skipname==0\n");
  273. goto error;
  274. }
  275. p+=2+2; /* skip QCODE & QCLASS */
  276. #if 0
  277. for (;(p<end && (*p)); p++);
  278. p+=1+2+2; /* skip the ending '\0, QCODE and QCLASS */
  279. #endif
  280. if (p>=end) {
  281. LOG(L_ERR, "ERROR: get_record: p>=end\n");
  282. goto error;
  283. }
  284. };
  285. answers_no=ntohs((unsigned short)buff.hdr.ancount);
  286. ans_len=ANS_SIZE;
  287. t=answer;
  288. for (r=0; (r<answers_no) && (p<end); r++){
  289. /* ignore it the default domain name */
  290. if ((p=dns_skipname(p, end))==0) {
  291. LOG(L_ERR, "ERROR: get_record: skip_name=0 (#2)\n");
  292. goto error;
  293. }
  294. /*
  295. skip=dn_expand(buff.buff, end, p, t, ans_len);
  296. p+=skip;
  297. */
  298. /* check if enough space is left for type, class, ttl & size */
  299. if ((p+2+2+4+2)>=end) goto error_boundary;
  300. /* get type */
  301. memcpy((void*) &rtype, (void*)p, 2);
  302. rtype=ntohs(rtype);
  303. p+=2;
  304. /* get class */
  305. memcpy((void*) &class, (void*)p, 2);
  306. class=ntohs(class);
  307. p+=2;
  308. /* get ttl*/
  309. memcpy((void*) &ttl, (void*)p, 4);
  310. ttl=ntohl(ttl);
  311. p+=4;
  312. /* get size */
  313. memcpy((void*)&rdlength, (void*)p, 2);
  314. rdlength=ntohs(rdlength);
  315. p+=2;
  316. /* check for type */
  317. /*
  318. if (rtype!=type){
  319. LOG(L_ERR, "WARNING: get_record: wrong type in answer (%d!=%d)\n",
  320. rtype, type);
  321. p+=rdlength;
  322. continue;
  323. }
  324. */
  325. /* expand the "type" record (rdata)*/
  326. rd=(struct rdata*) local_malloc(sizeof(struct rdata));
  327. if (rd==0){
  328. LOG(L_ERR, "ERROR: get_record: out of memory\n");
  329. goto error;
  330. }
  331. rd->type=rtype;
  332. rd->class=class;
  333. rd->ttl=ttl;
  334. rd->next=0;
  335. switch(rtype){
  336. case T_SRV:
  337. srv_rd= dns_srv_parser(buff.buff, end, p);
  338. rd->rdata=(void*)srv_rd;
  339. if (srv_rd==0) goto error_parse;
  340. /* insert sorted into the list */
  341. for (crt=&head; *crt; crt= &((*crt)->next)){
  342. crt_srv=(struct srv_rdata*)(*crt)->rdata;
  343. if ((srv_rd->priority < crt_srv->priority) ||
  344. ( (srv_rd->priority == crt_srv->priority) &&
  345. (srv_rd->weight > crt_srv->weight) ) ){
  346. /* insert here */
  347. goto skip;
  348. }
  349. }
  350. last=&(rd->next); /*end of for => this will be the last elem*/
  351. skip:
  352. /* insert here */
  353. rd->next=*crt;
  354. *crt=rd;
  355. break;
  356. case T_A:
  357. rd->rdata=(void*) dns_a_parser(p,end);
  358. if (rd->rdata==0) goto error_parse;
  359. *last=rd; /* last points to the last "next" or the list head*/
  360. last=&(rd->next);
  361. break;
  362. case T_AAAA:
  363. rd->rdata=(void*) dns_aaaa_parser(p,end);
  364. if (rd->rdata==0) goto error_parse;
  365. *last=rd;
  366. last=&(rd->next);
  367. break;
  368. case T_CNAME:
  369. rd->rdata=(void*) dns_cname_parser(buff.buff, end, p);
  370. if(rd->rdata==0) goto error_parse;
  371. *last=rd;
  372. last=&(rd->next);
  373. break;
  374. case T_NAPTR:
  375. rd->rdata=(void*) dns_naptr_parser(buff.buff, end, p);
  376. if(rd->rdata==0) goto error_parse;
  377. *last=rd;
  378. last=&(rd->next);
  379. break;
  380. default:
  381. LOG(L_ERR, "WARNING: get_record: unknown type %d\n", rtype);
  382. rd->rdata=0;
  383. *last=rd;
  384. last=&(rd->next);
  385. }
  386. p+=rdlength;
  387. }
  388. return head;
  389. error_boundary:
  390. LOG(L_ERR, "ERROR: get_record: end of query buff reached\n");
  391. return 0;
  392. error_parse:
  393. LOG(L_ERR, "ERROR: get_record: rdata parse error \n");
  394. if (rd) local_free(rd); /* rd->rdata=0 & rd is not linked yet into
  395. the list */
  396. error:
  397. LOG(L_ERR, "ERROR: get_record \n");
  398. if (head) free_rdata_list(head);
  399. not_found:
  400. return 0;
  401. }
  402. /* resolves a host name trying SRV lookup if *port==0 or normal A/AAAA lookup
  403. * if *port!=0.
  404. * returns: hostent struct & *port filled with the port from the SRV record;
  405. * 0 on error
  406. */
  407. struct hostent* sip_resolvehost(str* name, unsigned short* port)
  408. {
  409. struct hostent* he;
  410. struct rdata* head;
  411. struct rdata* l;
  412. struct srv_rdata* srv;
  413. struct ip_addr* ip;
  414. static char tmp[MAX_DNS_NAME]; /* tmp. buff. for SRV lookups */
  415. /* try SRV if no port specified (draft-ietf-sip-srv-06) */
  416. if ((port)&&(*port==0)){
  417. *port=SIP_PORT; /* just in case we don't find another */
  418. if ((name->len+SRV_PREFIX_LEN+1)>MAX_DNS_NAME){
  419. LOG(L_WARN, "WARNING: sip_resolvehost: domain name too long (%d),"
  420. " unable to perform SRV lookup\n", name->len);
  421. }else{
  422. /* check if it's an ip address */
  423. if ( ((ip=str2ip(name))!=0)
  424. #ifdef USE_IPV6
  425. || ((ip=str2ip6(name))!=0)
  426. #endif
  427. ){
  428. /* we are lucky, this is an ip address */
  429. return ip_addr2he(name,ip);
  430. }
  431. memcpy(tmp, SRV_PREFIX, SRV_PREFIX_LEN);
  432. memcpy(tmp+SRV_PREFIX_LEN, name->s, name->len);
  433. tmp[SRV_PREFIX_LEN + name->len] = '\0';
  434. head=get_record(tmp, T_SRV);
  435. for(l=head; l; l=l->next){
  436. if (l->type!=T_SRV) continue; /*should never happen*/
  437. srv=(struct srv_rdata*) l->rdata;
  438. if (srv==0){
  439. LOG(L_CRIT, "sip_resolvehost: BUG: null rdata\n");
  440. free_rdata_list(head);
  441. break;
  442. }
  443. he=resolvehost(srv->name);
  444. if (he!=0){
  445. /* we found it*/
  446. DBG("sip_resolvehost: SRV(%s) = %s:%d\n",
  447. tmp, srv->name, srv->port);
  448. *port=srv->port;
  449. free_rdata_list(head); /*clean up*/
  450. return he;
  451. }
  452. }
  453. DBG("sip_resolvehost: not SRV record found for %.*s,"
  454. " trying 'normal' lookup...\n", name->len, name->s);
  455. }
  456. }
  457. if (name->len >= MAX_DNS_NAME) {
  458. LOG(L_ERR, "sip_resolvehost: domain name too long\n");
  459. return 0;
  460. }
  461. memcpy(tmp, name->s, name->len);
  462. tmp[name->len] = '\0';
  463. he=resolvehost(tmp);
  464. return he;
  465. }