resolve.c 14 KB

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