resolve.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  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. * 2005-07-11 added resolv_init (timeouts a.s.o) (andrei)
  33. * 2006-04-13 added sip_hostport2su() (andrei)
  34. * 2006-07-13 rdata structures put on diet (andrei)
  35. * 2006-07-17 rdata contains now also the record name (andrei)
  36. * 2006-08-18 get_record can append also the additional records to the
  37. * returned list (andrei)
  38. * 2007-06-15 naptr support (andrei)
  39. * 2007-10-10 short name resolution using search list supported (mma)
  40. * set dns_use_search_list=1 (default on)
  41. * new option dns_search_full_match (default on) controls
  42. * whether rest of the name is matched against search list
  43. * or blindly accepted (better performance but exploitable)
  44. */
  45. #include <sys/types.h>
  46. #include <netinet/in.h>
  47. #include <arpa/nameser.h>
  48. #include <resolv.h>
  49. #include <string.h>
  50. #include "resolve.h"
  51. #include "compiler_opt.h"
  52. #include "dprint.h"
  53. #include "mem/mem.h"
  54. #include "ip_addr.h"
  55. #include "error.h"
  56. #include "globals.h" /* tcp_disable, tls_disable a.s.o */
  57. #ifdef USE_DNS_CACHE
  58. #include "dns_cache.h"
  59. #endif
  60. /* mallocs for local stuff */
  61. #define local_malloc pkg_malloc
  62. #define local_free pkg_free
  63. #ifdef USE_IPV6
  64. int dns_try_ipv6=1; /* default on */
  65. #else
  66. int dns_try_ipv6=0; /* off, if no ipv6 support */
  67. #endif
  68. int dns_try_naptr=0; /* off by default */
  69. int dns_udp_pref=3; /* udp transport preference (for naptr) */
  70. int dns_tcp_pref=2; /* tcp transport preference (for naptr) */
  71. int dns_tls_pref=1; /* tls transport preference (for naptr) */
  72. #ifdef USE_NAPTR
  73. #define PROTO_LAST PROTO_SCTP
  74. static int naptr_proto_pref[PROTO_LAST];
  75. #endif
  76. /* declared in globals.h */
  77. int dns_retr_time=-1;
  78. int dns_retr_no=-1;
  79. int dns_servers_no=-1;
  80. int dns_search_list=-1;
  81. int dns_search_fmatch=-1;
  82. #ifdef USE_NAPTR
  83. void init_naptr_proto_prefs()
  84. {
  85. if ((PROTO_UDP >= PROTO_LAST) || (PROTO_TCP >= PROTO_LAST) ||
  86. (PROTO_TLS >= PROTO_LAST)){
  87. BUG("init_naptr_proto_prefs: array too small \n");
  88. return;
  89. }
  90. naptr_proto_pref[PROTO_UDP]=dns_udp_pref;
  91. naptr_proto_pref[PROTO_TCP]=dns_tcp_pref;
  92. naptr_proto_pref[PROTO_TLS]=dns_tls_pref;
  93. }
  94. #endif /* USE_NAPTR */
  95. /* init. the resolver
  96. * params: retr_time - time before retransmitting (must be >0)
  97. * retr_no - retransmissions number
  98. * servers_no - how many dns servers will be used
  99. * (from the one listed in /etc/resolv.conf)
  100. * search - if 0 the search list in /etc/resolv.conf will
  101. * be ignored (HINT: even if you don't have a
  102. * search list in resolv.conf, it's still better
  103. * to set search to 0, because an empty seachlist
  104. * means in fact search "" => it takes more time)
  105. * If any of the parameters <0, the default (system specific) value
  106. * will be used. See also resolv.conf(5).
  107. * returns: 0 on success, -1 on error
  108. */
  109. int resolv_init()
  110. {
  111. res_init();
  112. #ifdef HAVE_RESOLV_RES
  113. if (dns_retr_time>0)
  114. _res.retrans=dns_retr_time;
  115. if (dns_retr_no>0)
  116. _res.retry=dns_retr_no;
  117. if (dns_servers_no>=0)
  118. _res.nscount=dns_servers_no;
  119. if (dns_search_list==0)
  120. _res.options&=~(RES_DEFNAMES|RES_DNSRCH);
  121. #else
  122. #warning "no resolv timeout support"
  123. LOG(L_WARN, "WARNING: resolv_init: no resolv options support - resolv"
  124. " options will be ignored\n");
  125. #endif
  126. #ifdef USE_NAPTR
  127. init_naptr_proto_prefs();
  128. #endif
  129. return 0;
  130. }
  131. /* skips over a domain name in a dns message
  132. * (it can be a sequence of labels ending in \0, a pointer or
  133. * a sequence of labels ending in a pointer -- see rfc1035
  134. * returns pointer after the domain name or null on error*/
  135. unsigned char* dns_skipname(unsigned char* p, unsigned char* end)
  136. {
  137. while(p<end){
  138. /* check if \0 (root label length) */
  139. if (*p==0){
  140. p+=1;
  141. break;
  142. }
  143. /* check if we found a pointer */
  144. if (((*p)&0xc0)==0xc0){
  145. /* if pointer skip over it (2 bytes) & we found the end */
  146. p+=2;
  147. break;
  148. }
  149. /* normal label */
  150. p+=*p+1;
  151. }
  152. return (p>end)?0:p;
  153. }
  154. /* parses the srv record into a srv_rdata structure
  155. * msg - pointer to the dns message
  156. * end - pointer to the end of the message
  157. * rdata - pointer to the rdata part of the srv answer
  158. * returns 0 on error, or a dyn. alloc'ed srv_rdata structure */
  159. /* SRV rdata format:
  160. * 111111
  161. * 0123456789012345
  162. * +----------------+
  163. * | priority |
  164. * |----------------|
  165. * | weight |
  166. * |----------------|
  167. * | port number |
  168. * |----------------|
  169. * | |
  170. * ~ name ~
  171. * | |
  172. * +----------------+
  173. */
  174. struct srv_rdata* dns_srv_parser( unsigned char* msg, unsigned char* end,
  175. unsigned char* rdata)
  176. {
  177. struct srv_rdata* srv;
  178. unsigned short priority;
  179. unsigned short weight;
  180. unsigned short port;
  181. int len;
  182. char name[MAX_DNS_NAME];
  183. srv=0;
  184. if ((rdata+6+1)>end) goto error;
  185. memcpy((void*)&priority, rdata, 2);
  186. memcpy((void*)&weight, rdata+2, 2);
  187. memcpy((void*)&port, rdata+4, 2);
  188. rdata+=6;
  189. if (dn_expand(msg, end, rdata, name, MAX_DNS_NAME-1)<0)
  190. goto error;
  191. len=strlen(name);
  192. if (len>255)
  193. goto error;
  194. /* alloc enought space for the struct + null terminated name */
  195. srv=local_malloc(sizeof(struct srv_rdata)-1+len+1);
  196. if (srv==0){
  197. LOG(L_ERR, "ERROR: dns_srv_parser: out of memory\n");
  198. goto error;
  199. }
  200. srv->priority=ntohs(priority);
  201. srv->weight=ntohs(weight);
  202. srv->port=ntohs(port);
  203. srv->name_len=len;
  204. memcpy(srv->name, name, srv->name_len);
  205. srv->name[srv->name_len]=0;
  206. return srv;
  207. error:
  208. if (srv) local_free(srv);
  209. return 0;
  210. }
  211. /* parses the naptr record into a naptr_rdata structure
  212. * msg - pointer to the dns message
  213. * end - pointer to the end of the message
  214. * rdata - pointer to the rdata part of the naptr answer
  215. * returns 0 on error, or a dyn. alloc'ed naptr_rdata structure */
  216. /* NAPTR rdata format:
  217. * 111111
  218. * 0123456789012345
  219. * +----------------+
  220. * | order |
  221. * |----------------|
  222. * | preference |
  223. * |----------------|
  224. * ~ flags ~
  225. * | (string) |
  226. * |----------------|
  227. * ~ services ~
  228. * | (string) |
  229. * |----------------|
  230. * ~ regexp ~
  231. * | (string) |
  232. * |----------------|
  233. * ~ replacement ~
  234. | (name) |
  235. * +----------------+
  236. */
  237. struct naptr_rdata* dns_naptr_parser( unsigned char* msg, unsigned char* end,
  238. unsigned char* rdata)
  239. {
  240. struct naptr_rdata* naptr;
  241. unsigned char* flags;
  242. unsigned char* services;
  243. unsigned char* regexp;
  244. unsigned short order;
  245. unsigned short pref;
  246. unsigned char flags_len;
  247. unsigned char services_len;
  248. unsigned char regexp_len;
  249. int len;
  250. char repl[MAX_DNS_NAME];
  251. naptr = 0;
  252. if ((rdata + 7 + 1)>end) goto error;
  253. memcpy((void*)&order, rdata, 2);
  254. memcpy((void*)&pref, rdata + 2, 2);
  255. flags_len = rdata[4];
  256. if ((rdata + 7 + 1 + flags_len) > end)
  257. goto error;
  258. flags=rdata+5;
  259. services_len = rdata[5 + flags_len];
  260. if ((rdata + 7 + 1 + flags_len + services_len) > end)
  261. goto error;
  262. services=rdata + 6 + flags_len;
  263. regexp_len = rdata[6 + flags_len + services_len];
  264. if ((rdata + 7 +1 + flags_len + services_len + regexp_len) > end)
  265. goto error;
  266. regexp=rdata + 7 + flags_len + services_len;
  267. rdata = rdata + 7 + flags_len + services_len + regexp_len;
  268. if (dn_expand(msg, end, rdata, repl, MAX_DNS_NAME-1) == -1)
  269. goto error;
  270. len=strlen(repl);
  271. if (len>255)
  272. goto error;
  273. naptr=local_malloc(sizeof(struct naptr_rdata)+flags_len+services_len+
  274. regexp_len+len+1-1);
  275. if (naptr == 0){
  276. LOG(L_ERR, "ERROR: dns_naptr_parser: out of memory\n");
  277. goto error;
  278. }
  279. naptr->order=ntohs(order);
  280. naptr->pref=ntohs(pref);
  281. naptr->flags=&naptr->str_table[0];
  282. naptr->flags_len=flags_len;
  283. memcpy(naptr->flags, flags, naptr->flags_len);
  284. naptr->services=&naptr->str_table[flags_len];
  285. naptr->services_len=services_len;
  286. memcpy(naptr->services, services, naptr->services_len);
  287. naptr->regexp=&naptr->str_table[flags_len+services_len];
  288. naptr->regexp_len=regexp_len;
  289. memcpy(naptr->regexp, regexp, naptr->regexp_len);
  290. naptr->repl=&naptr->str_table[flags_len+services_len+regexp_len];
  291. naptr->repl_len=len;
  292. memcpy(naptr->repl, repl, len);
  293. naptr->repl[len]=0; /* null term. */
  294. return naptr;
  295. error:
  296. if (naptr) local_free(naptr);
  297. return 0;
  298. }
  299. /* parses a CNAME record into a cname_rdata structure */
  300. struct cname_rdata* dns_cname_parser( unsigned char* msg, unsigned char* end,
  301. unsigned char* rdata)
  302. {
  303. struct cname_rdata* cname;
  304. int len;
  305. char name[MAX_DNS_NAME];
  306. cname=0;
  307. if (dn_expand(msg, end, rdata, name, MAX_DNS_NAME-1)==-1)
  308. goto error;
  309. len=strlen(name);
  310. if (len>255)
  311. goto error;
  312. /* alloc sizeof struct + space for the null terminated name */
  313. cname=local_malloc(sizeof(struct cname_rdata)-1+len+1);
  314. if(cname==0){
  315. LOG(L_ERR, "ERROR: dns_cname_parser: out of memory\n");
  316. goto error;
  317. }
  318. cname->name_len=len;
  319. memcpy(cname->name, name, cname->name_len);
  320. cname->name[cname->name_len]=0;
  321. return cname;
  322. error:
  323. if (cname) local_free(cname);
  324. return 0;
  325. }
  326. /* parses an A record rdata into an a_rdata structure
  327. * returns 0 on error or a dyn. alloc'ed a_rdata struct
  328. */
  329. struct a_rdata* dns_a_parser(unsigned char* rdata, unsigned char* end)
  330. {
  331. struct a_rdata* a;
  332. if (rdata+4>end) goto error;
  333. a=(struct a_rdata*)local_malloc(sizeof(struct a_rdata));
  334. if (a==0){
  335. LOG(L_ERR, "ERROR: dns_a_parser: out of memory\n");
  336. goto error;
  337. }
  338. memcpy(a->ip, rdata, 4);
  339. return a;
  340. error:
  341. return 0;
  342. }
  343. /* parses an AAAA (ipv6) record rdata into an aaaa_rdata structure
  344. * returns 0 on error or a dyn. alloc'ed aaaa_rdata struct */
  345. struct aaaa_rdata* dns_aaaa_parser(unsigned char* rdata, unsigned char* end)
  346. {
  347. struct aaaa_rdata* aaaa;
  348. if (rdata+16>end) goto error;
  349. aaaa=(struct aaaa_rdata*)local_malloc(sizeof(struct aaaa_rdata));
  350. if (aaaa==0){
  351. LOG(L_ERR, "ERROR: dns_aaaa_parser: out of memory\n");
  352. goto error;
  353. }
  354. memcpy(aaaa->ip6, rdata, 16);
  355. return aaaa;
  356. error:
  357. return 0;
  358. }
  359. /* frees completely a struct rdata list */
  360. void free_rdata_list(struct rdata* head)
  361. {
  362. struct rdata* l;
  363. struct rdata* next_l;
  364. l=head;
  365. while (l != 0) {
  366. next_l = l->next;
  367. /* free the parsed rdata*/
  368. if (l->rdata) local_free(l->rdata);
  369. local_free(l);
  370. l = next_l;
  371. }
  372. }
  373. #ifdef HAVE_RESOLV_RES
  374. /* checks whether supplied name exists in the resolver search list
  375. * returns 1 if found
  376. * 0 if not found
  377. */
  378. int match_search_list(const struct __res_state* res, char* name) {
  379. int i;
  380. for (i=0; (i<MAXDNSRCH) && (res->dnsrch[i]); i++) {
  381. if (strcasecmp(name, res->dnsrch[i])==0)
  382. return 1;
  383. }
  384. return 0;
  385. }
  386. #endif
  387. /* gets the DNS records for name:type
  388. * returns a dyn. alloc'ed struct rdata linked list with the parsed responses
  389. * or 0 on error
  390. * see rfc1035 for the query/response format */
  391. struct rdata* get_record(char* name, int type, int flags)
  392. {
  393. int size;
  394. int skip;
  395. int qno, answers_no;
  396. int r;
  397. static union dns_query buff;
  398. unsigned char* p;
  399. unsigned char* end;
  400. static char rec_name[MAX_DNS_NAME]; /* placeholder for the record name */
  401. int rec_name_len;
  402. unsigned short rtype, class, rdlength;
  403. unsigned int ttl;
  404. struct rdata* head;
  405. struct rdata** crt;
  406. struct rdata** last;
  407. struct rdata* rd;
  408. struct srv_rdata* srv_rd;
  409. struct srv_rdata* crt_srv;
  410. int search_list_used;
  411. int name_len;
  412. struct rdata* fullname_rd;
  413. if (dns_search_list==0) {
  414. search_list_used=0;
  415. name_len=0;
  416. } else {
  417. search_list_used=1;
  418. name_len=strlen(name);
  419. }
  420. fullname_rd=0;
  421. size=res_search(name, C_IN, type, buff.buff, sizeof(buff));
  422. if (size<0) {
  423. DBG("get_record: lookup(%s, %d) failed\n", name, type);
  424. goto not_found;
  425. }
  426. else if (size > sizeof(buff)) size=sizeof(buff);
  427. head=rd=0;
  428. last=crt=&head;
  429. p=buff.buff+DNS_HDR_SIZE;
  430. end=buff.buff+size;
  431. if (p>=end) goto error_boundary;
  432. qno=ntohs((unsigned short)buff.hdr.qdcount);
  433. for (r=0; r<qno; r++){
  434. /* skip the name of the question */
  435. if ((p=dns_skipname(p, end))==0) {
  436. LOG(L_ERR, "ERROR: get_record: skipname==0\n");
  437. goto error;
  438. }
  439. p+=2+2; /* skip QCODE & QCLASS */
  440. #if 0
  441. for (;(p<end && (*p)); p++);
  442. p+=1+2+2; /* skip the ending '\0, QCODE and QCLASS */
  443. #endif
  444. if (p>end) {
  445. LOG(L_ERR, "ERROR: get_record: p>=end\n");
  446. goto error;
  447. }
  448. };
  449. answers_no=ntohs((unsigned short)buff.hdr.ancount);
  450. again:
  451. for (r=0; (r<answers_no) && (p<end); r++){
  452. #if 0
  453. /* ignore it the default domain name */
  454. if ((p=dns_skipname(p, end))==0) {
  455. LOG(L_ERR, "ERROR: get_record: skip_name=0 (#2)\n");
  456. goto error;
  457. }
  458. #else
  459. if ((skip=dn_expand(buff.buff, end, p, rec_name, MAX_DNS_NAME-1))==-1){
  460. LOG(L_ERR, "ERROR: get_record: dn_expand(rec_name) failed\n");
  461. goto error;
  462. }
  463. #endif
  464. p+=skip;
  465. rec_name_len=strlen(rec_name);
  466. if (rec_name_len>255){
  467. LOG(L_ERR, "ERROR: get_record: dn_expand(rec_name): name too"
  468. " long (%d)\n", rec_name_len);
  469. goto error;
  470. }
  471. /* check if enough space is left for type, class, ttl & size */
  472. if ((p+2+2+4+2)>end) goto error_boundary;
  473. /* get type */
  474. memcpy((void*) &rtype, (void*)p, 2);
  475. rtype=ntohs(rtype);
  476. p+=2;
  477. /* get class */
  478. memcpy((void*) &class, (void*)p, 2);
  479. class=ntohs(class);
  480. p+=2;
  481. /* get ttl*/
  482. memcpy((void*) &ttl, (void*)p, 4);
  483. ttl=ntohl(ttl);
  484. p+=4;
  485. /* get size */
  486. memcpy((void*)&rdlength, (void*)p, 2);
  487. rdlength=ntohs(rdlength);
  488. p+=2;
  489. if ((flags & RES_ONLY_TYPE) && (rtype!=type)){
  490. /* skip */
  491. p+=rdlength;
  492. continue;
  493. }
  494. /* expand the "type" record (rdata)*/
  495. rd=(struct rdata*) local_malloc(sizeof(struct rdata)+rec_name_len+1-1);
  496. if (rd==0){
  497. LOG(L_ERR, "ERROR: get_record: out of memory\n");
  498. goto error;
  499. }
  500. rd->type=rtype;
  501. rd->class=class;
  502. rd->ttl=ttl;
  503. rd->next=0;
  504. memcpy(rd->name, rec_name, rec_name_len);
  505. rd->name[rec_name_len]=0;
  506. rd->name_len=rec_name_len;
  507. /* check if full name matches */
  508. if ((search_list_used==1)&&(fullname_rd==0)&&
  509. (rec_name_len>=name_len)&&
  510. (strncasecmp(rec_name, name, name_len)==0)) {
  511. /* now we have record which's name is the same (up-to the name_len
  512. * with the searched one
  513. * if the length is the same - we found full match, no fake cname
  514. * needed, just clear the flag
  515. * if the length of the name differs - it has matched using search list
  516. * remember the rd, so we can create fake CNAME record when all answers
  517. * are used and no better match found
  518. */
  519. if (rec_name_len==name_len)
  520. search_list_used=0;
  521. /* this is safe.... here was rec_name_len > name_len */
  522. else if (rec_name[name_len]=='.') {
  523. #ifdef HAVE_RESOLV_RES
  524. if ((dns_search_fmatch==0) ||
  525. (match_search_list(&_res, rec_name+name_len+1)!=0))
  526. #endif
  527. fullname_rd=rd;
  528. }
  529. }
  530. switch(rtype){
  531. case T_SRV:
  532. srv_rd= dns_srv_parser(buff.buff, end, p);
  533. rd->rdata=(void*)srv_rd;
  534. if (srv_rd==0) goto error_parse;
  535. /* insert sorted into the list */
  536. for (crt=&head; *crt; crt= &((*crt)->next)){
  537. if ((*crt)->type!=T_SRV)
  538. continue;
  539. crt_srv=(struct srv_rdata*)(*crt)->rdata;
  540. if ((srv_rd->priority < crt_srv->priority) ||
  541. ( (srv_rd->priority == crt_srv->priority) &&
  542. (srv_rd->weight > crt_srv->weight) ) ){
  543. /* insert here */
  544. goto skip;
  545. }
  546. }
  547. last=&(rd->next); /*end of for => this will be the last elem*/
  548. skip:
  549. /* insert here */
  550. rd->next=*crt;
  551. *crt=rd;
  552. break;
  553. case T_A:
  554. rd->rdata=(void*) dns_a_parser(p,end);
  555. if (rd->rdata==0) goto error_parse;
  556. *last=rd; /* last points to the last "next" or the list head*/
  557. last=&(rd->next);
  558. break;
  559. case T_AAAA:
  560. rd->rdata=(void*) dns_aaaa_parser(p,end);
  561. if (rd->rdata==0) goto error_parse;
  562. *last=rd;
  563. last=&(rd->next);
  564. break;
  565. case T_CNAME:
  566. rd->rdata=(void*) dns_cname_parser(buff.buff, end, p);
  567. if(rd->rdata==0) goto error_parse;
  568. *last=rd;
  569. last=&(rd->next);
  570. break;
  571. case T_NAPTR:
  572. rd->rdata=(void*) dns_naptr_parser(buff.buff, end, p);
  573. if(rd->rdata==0) goto error_parse;
  574. *last=rd;
  575. last=&(rd->next);
  576. break;
  577. default:
  578. LOG(L_ERR, "WARNING: get_record: unknown type %d\n", rtype);
  579. rd->rdata=0;
  580. *last=rd;
  581. last=&(rd->next);
  582. }
  583. p+=rdlength;
  584. }
  585. if (flags & RES_AR){
  586. flags&=~RES_AR;
  587. answers_no=ntohs((unsigned short)buff.hdr.nscount);
  588. #ifdef RESOLVE_DBG
  589. DBG("get_record: skipping %d NS (p=%p, end=%p)\n", answers_no, p, end);
  590. #endif
  591. for (r=0; (r<answers_no) && (p<end); r++){
  592. /* skip over the ns records */
  593. if ((p=dns_skipname(p, end))==0) {
  594. LOG(L_ERR, "ERROR: get_record: skip_name=0 (#3)\n");
  595. goto error;
  596. }
  597. /* check if enough space is left for type, class, ttl & size */
  598. if ((p+2+2+4+2)>end) goto error_boundary;
  599. memcpy((void*)&rdlength, (void*)p+2+2+4, 2);
  600. p+=2+2+4+2+ntohs(rdlength);
  601. }
  602. answers_no=ntohs((unsigned short)buff.hdr.arcount);
  603. #ifdef RESOLVE_DBG
  604. DBG("get_record: parsing %d ARs (p=%p, end=%p)\n", answers_no, p, end);
  605. #endif
  606. goto again; /* add also the additional records */
  607. }
  608. /* if the name was expanded using DNS search list
  609. * create fake CNAME record to convert the short name
  610. * (queried) to long name (answered)
  611. */
  612. if ((search_list_used==1)&&(fullname_rd!=0)) {
  613. rd=(struct rdata*) local_malloc(sizeof(struct rdata)+name_len+1-1);
  614. if (rd==0){
  615. LOG(L_ERR, "ERROR: get_record: out of memory\n");
  616. goto error;
  617. }
  618. rd->type=T_CNAME;
  619. rd->class=fullname_rd->class;
  620. rd->ttl=fullname_rd->ttl;
  621. rd->next=head;
  622. memcpy(rd->name, name, name_len);
  623. rd->name[name_len]=0;
  624. rd->name_len=name_len;
  625. /* alloc sizeof struct + space for the null terminated name */
  626. rd->rdata=(void*)local_malloc(sizeof(struct cname_rdata)-1+head->name_len+1);
  627. if(rd->rdata==0){
  628. LOG(L_ERR, "ERROR: get_record: out of memory\n");
  629. goto error_rd;
  630. }
  631. ((struct cname_rdata*)(rd->rdata))->name_len=fullname_rd->name_len;
  632. memcpy(((struct cname_rdata*)(rd->rdata))->name, fullname_rd->name, fullname_rd->name_len);
  633. ((struct cname_rdata*)(rd->rdata))->name[head->name_len]=0;
  634. head=rd;
  635. }
  636. return head;
  637. error_boundary:
  638. LOG(L_ERR, "ERROR: get_record: end of query buff reached\n");
  639. if (head) free_rdata_list(head);
  640. return 0;
  641. error_parse:
  642. LOG(L_ERR, "ERROR: get_record: rdata parse error (%s, %d), %p-%p"
  643. " rtype=%d, class=%d, ttl=%d, rdlength=%d \n",
  644. name, type,
  645. p, end, rtype, class, ttl, rdlength);
  646. error_rd:
  647. if (rd) local_free(rd); /* rd->rdata=0 & rd is not linked yet into
  648. the list */
  649. error:
  650. LOG(L_ERR, "ERROR: get_record \n");
  651. if (head) free_rdata_list(head);
  652. not_found:
  653. return 0;
  654. }
  655. #ifdef USE_NAPTR
  656. /* service matching constants, lowercase */
  657. #define SIP_SCH 0x2b706973
  658. #define SIPS_SCH 0x73706973
  659. #define SIP_D2U 0x00753264
  660. #define SIP_D2T 0x00743264
  661. #define SIP_D2S 0x00733264
  662. #define SIPS_D2T 0x7432642b
  663. /* get protocol from a naptr rdata and check for validity
  664. * returns > 0 (PROTO_UDP, PROTO_TCP, PROTO_SCTP or PROTO_TLS)
  665. * <=0 on error
  666. */
  667. char naptr_get_sip_proto(struct naptr_rdata* n)
  668. {
  669. unsigned int s;
  670. char proto;
  671. proto=-1;
  672. if ((n->flags_len!=1) || (*n->flags!='s'))
  673. return -1;
  674. if (n->regexp_len!=0)
  675. return -1;
  676. /* SIP+D2U, SIP+D2T, SIP+D2S, SIPS+D2T */
  677. if (n->services_len==7){ /* SIP+D2X */
  678. s=n->services[0]+(n->services[1]<<8)+(n->services[2]<<16)+
  679. (n->services[3]<<24);
  680. s|=0x20202020;
  681. if (s==SIP_SCH){
  682. s=n->services[4]+(n->services[5]<<8)+(n->services[6]<<16);
  683. s|=0x00202020;
  684. switch(s){
  685. case SIP_D2U:
  686. proto=PROTO_UDP;
  687. break;
  688. case SIP_D2T:
  689. proto=PROTO_TCP;
  690. break;
  691. case SIP_D2S:
  692. proto=PROTO_SCTP;
  693. break;
  694. default:
  695. return -1;
  696. }
  697. }else{
  698. return -1;
  699. }
  700. }else if (n->services_len==8){ /*SIPS+D2T */
  701. s=n->services[0]+(n->services[1]<<8)+(n->services[2]<<16)+
  702. (n->services[3]<<24);
  703. s|=0x20202020;
  704. if (s==SIPS_SCH){
  705. s=n->services[4]+(n->services[5]<<8)+(n->services[6]<<16)+
  706. (n->services[7]<<24);
  707. s|=0x20202020;
  708. if (s==SIPS_D2T){
  709. proto=PROTO_TLS;
  710. }
  711. }else{
  712. return -1;
  713. }
  714. }else{
  715. return -1;
  716. }
  717. return proto;
  718. }
  719. inline static int proto_pref_score(char proto)
  720. {
  721. if ((proto>=PROTO_UDP) && (proto<= PROTO_TLS))
  722. return naptr_proto_pref[(int)proto];
  723. return 0;
  724. }
  725. /* returns true if we support the protocol */
  726. int naptr_proto_supported(char proto)
  727. {
  728. if (proto_pref_score(proto)<0)
  729. return 0;
  730. switch(proto){
  731. case PROTO_UDP:
  732. return 1;
  733. #ifdef USE_TCP
  734. case PROTO_TCP:
  735. return !tcp_disable;
  736. #ifdef USE_TLS
  737. case PROTO_TLS:
  738. return !tls_disable;
  739. #endif /* USE_TLS */
  740. #endif /* USE_TCP */
  741. case PROTO_SCTP:
  742. return 0; /* not supported */
  743. }
  744. return 0;
  745. }
  746. /* returns true if new_proto is preferred over old_proto */
  747. int naptr_proto_preferred(char new_proto, char old_proto)
  748. {
  749. return proto_pref_score(new_proto)>proto_pref_score(old_proto);
  750. }
  751. /* choose between 2 naptr records, should take into account local
  752. * preferences too
  753. * returns 1 if the new record was selected, 0 otherwise */
  754. int naptr_choose (struct naptr_rdata** crt, char* crt_proto,
  755. struct naptr_rdata* n , char n_proto)
  756. {
  757. #ifdef NAPTR_DBG
  758. DBG("naptr_choose(o: %d w: %d p:%d , o: %d w:%d p:%d)\n",
  759. *crt?(int)(*crt)->order:-1, *crt?(int)(*crt)->pref:-1,
  760. (int)*crt_proto,
  761. (int)n->order, (int)n->pref, (int)n_proto);
  762. #endif
  763. if ((*crt==0) || ((*crt_proto!=n_proto) &&
  764. ( naptr_proto_preferred(n_proto, *crt_proto))) )
  765. goto change;
  766. if ((n->order<(*crt)->order) || ((n->order== (*crt)->order) &&
  767. (n->pref < (*crt)->pref))){
  768. goto change;
  769. }
  770. #ifdef NAPTR_DBG
  771. DBG("naptr_choose: no change\n");
  772. #endif
  773. return 0;
  774. change:
  775. #ifdef NAPTR_DBG
  776. DBG("naptr_choose: changed\n");
  777. #endif
  778. *crt_proto=n_proto;
  779. *crt=n;
  780. return 1;
  781. }
  782. #endif /* USE_NAPTR */
  783. /* internal sip srv resolver: resolves a host name trying:
  784. * - SRV lookup if the address is not an ip *port==0. The result of the SRV
  785. * query will be used for an A/AAAA lookup.
  786. * - normal A/AAAA lookup (either fallback from the above or if *port!=0
  787. * and *proto!=0 or port==0 && proto==0)
  788. * when performing SRV lookup (*port==0) it will use *proto to look for
  789. * tcp or udp hosts, otherwise proto is unused; if proto==0 => no SRV lookup
  790. * If zt is set, name will be assumed to be 0 terminated and some copy
  791. * operations will be avoided.
  792. * If is_srv is set it will assume name has the srv prefixes for sip already
  793. * appended and it's already 0-term'ed; if not it will append them internally.
  794. * If ars !=0, it will first try to look through them and only if the SRV
  795. * record is not found it will try doing a DNS query (ars will not be
  796. * freed, the caller should take care of them)
  797. * returns: hostent struct & *port filled with the port from the SRV record;
  798. * 0 on error
  799. */
  800. struct hostent* srv_sip_resolvehost(str* name, int zt, unsigned short* port,
  801. char* proto, int is_srv, struct rdata* ars)
  802. {
  803. struct hostent* he;
  804. struct ip_addr* ip;
  805. static char tmp[MAX_DNS_NAME]; /* tmp. buff. for SRV lookups and
  806. null. term strings */
  807. struct rdata* l;
  808. struct srv_rdata* srv;
  809. struct rdata* srv_head;
  810. char* srv_target;
  811. char srv_proto;
  812. /* init */
  813. srv_head=0;
  814. srv_target=0;
  815. if (name->len >= MAX_DNS_NAME) {
  816. LOG(L_ERR, "sip_resolvehost: domain name too long\n");
  817. he=0;
  818. goto end;
  819. }
  820. #ifdef RESOLVE_DBG
  821. DBG("srv_sip_resolvehost: %.*s:%d proto=%d\n", name->len, name->s,
  822. port?(int)*port:-1, proto?(int)*proto:-1);
  823. #endif
  824. if (is_srv){
  825. /* skip directly to srv resolving */
  826. srv_proto=(proto)?*proto:0;
  827. *port=(srv_proto==PROTO_TLS)?SIPS_PORT:SIP_PORT;
  828. if (zt){
  829. srv_target=name->s; /* name.s must be 0 terminated in
  830. this case */
  831. }else{
  832. memcpy(tmp, name->s, name->len);
  833. tmp[name->len] = '\0';
  834. srv_target=tmp;
  835. }
  836. goto do_srv; /* skip to the actual srv query */
  837. }
  838. if (proto){ /* makes sure we have a protocol set*/
  839. if (*proto==0)
  840. *proto=srv_proto=PROTO_UDP; /* default */
  841. else
  842. srv_proto=*proto;
  843. }else{
  844. srv_proto=PROTO_UDP;
  845. }
  846. /* try SRV if no port specified (draft-ietf-sip-srv-06) */
  847. if ((port)&&(*port==0)){
  848. *port=(srv_proto==PROTO_TLS)?SIPS_PORT:SIP_PORT; /* just in case we
  849. don't find another */
  850. /* check if it's an ip address */
  851. if (((ip=str2ip(name))!=0)
  852. #ifdef USE_IPV6
  853. || ((ip=str2ip6(name))!=0)
  854. #endif
  855. ){
  856. /* we are lucky, this is an ip address */
  857. he=ip_addr2he(name, ip);
  858. goto end;
  859. }
  860. if ((name->len+SRV_MAX_PREFIX_LEN+1)>MAX_DNS_NAME){
  861. LOG(L_WARN, "WARNING: sip_resolvehost: domain name too long (%d),"
  862. " unable to perform SRV lookup\n", name->len);
  863. }else{
  864. switch(srv_proto){
  865. case PROTO_NONE: /* no proto specified, use udp */
  866. if (proto)
  867. *proto=PROTO_UDP;
  868. /* no break */
  869. case PROTO_UDP:
  870. memcpy(tmp, SRV_UDP_PREFIX, SRV_UDP_PREFIX_LEN);
  871. memcpy(tmp+SRV_UDP_PREFIX_LEN, name->s, name->len);
  872. tmp[SRV_UDP_PREFIX_LEN + name->len] = '\0';
  873. break;
  874. case PROTO_TCP:
  875. memcpy(tmp, SRV_TCP_PREFIX, SRV_TCP_PREFIX_LEN);
  876. memcpy(tmp+SRV_TCP_PREFIX_LEN, name->s, name->len);
  877. tmp[SRV_TCP_PREFIX_LEN + name->len] = '\0';
  878. break;
  879. case PROTO_TLS:
  880. memcpy(tmp, SRV_TLS_PREFIX, SRV_TLS_PREFIX_LEN);
  881. memcpy(tmp+SRV_TLS_PREFIX_LEN, name->s, name->len);
  882. tmp[SRV_TLS_PREFIX_LEN + name->len] = '\0';
  883. break;
  884. default:
  885. LOG(L_CRIT, "BUG: sip_resolvehost: unknown proto %d\n",
  886. srv_proto);
  887. he=0;
  888. goto end;
  889. }
  890. srv_target=tmp;
  891. do_srv:
  892. /* try to find the SRV records inside previous ARs first*/
  893. for (l=ars; l; l=l->next){
  894. if (l->type!=T_SRV) continue;
  895. srv=(struct srv_rdata*) l->rdata;
  896. if (srv==0){
  897. LOG(L_CRIT, "sip_resolvehost: BUG: null rdata\n");
  898. /* cleanup on exit only */
  899. break;
  900. }
  901. he=resolvehost(srv->name);
  902. if (he!=0){
  903. /* we found it*/
  904. #ifdef RESOLVE_DBG
  905. DBG("sip_resolvehost: found SRV(%s) = %s:%d in AR\n",
  906. srv_target, srv->name, srv->port);
  907. #endif
  908. *port=srv->port;
  909. /* cleanup on exit */
  910. goto end;
  911. }
  912. }
  913. srv_head=get_record(srv_target, T_SRV, RES_ONLY_TYPE);
  914. for(l=srv_head; l; l=l->next){
  915. if (l->type!=T_SRV) continue; /*should never happen*/
  916. srv=(struct srv_rdata*) l->rdata;
  917. if (srv==0){
  918. LOG(L_CRIT, "sip_resolvehost: BUG: null rdata\n");
  919. /* cleanup on exit only */
  920. break;
  921. }
  922. he=resolvehost(srv->name);
  923. if (he!=0){
  924. /* we found it*/
  925. #ifdef RESOLVE_DBG
  926. DBG("sip_resolvehost: SRV(%s) = %s:%d\n",
  927. srv_target, srv->name, srv->port);
  928. #endif
  929. *port=srv->port;
  930. /* cleanup on exit */
  931. goto end;
  932. }
  933. }
  934. if (is_srv){
  935. /* if the name was already into SRV format it doesn't make
  936. * any sense to fall back to A/AAAA */
  937. he=0;
  938. goto end;
  939. }
  940. /* cleanup on exit */
  941. #ifdef RESOLVE_DBG
  942. DBG("sip_resolvehost: no SRV record found for %.*s,"
  943. " trying 'normal' lookup...\n", name->len, name->s);
  944. #endif
  945. }
  946. }
  947. /*skip_srv:*/
  948. if (likely(!zt)){
  949. memcpy(tmp, name->s, name->len);
  950. tmp[name->len] = '\0';
  951. he=resolvehost(tmp);
  952. }else{
  953. he=resolvehost(name->s);
  954. }
  955. end:
  956. #ifdef RESOLVE_DBG
  957. DBG("srv_sip_resolvehost: returning %p (%.*s:%d proto=%d)\n",
  958. he, name->len, name->s,
  959. port?(int)*port:-1, proto?(int)*proto:-1);
  960. #endif
  961. if (srv_head)
  962. free_rdata_list(srv_head);
  963. return he;
  964. }
  965. #ifdef USE_NAPTR
  966. /* iterates over a naptr rr list, returning each time a "good" naptr record
  967. * is found.( srv type, no regex and a supported protocol)
  968. * params:
  969. * naptr_head - naptr rr list head
  970. * tried - bitmap used to keep track of the already tried records
  971. * (no more then sizeof(tried)*8 valid records are
  972. * ever walked
  973. * srv_name - if succesfull, it will be set to the selected record
  974. * srv name (naptr repl.)
  975. * proto - if succesfull it will be set to the selected record
  976. * protocol
  977. * returns 0 if no more records found or a pointer to the selected record
  978. * and sets protocol and srv_name
  979. * WARNING: when calling first time make sure you run first
  980. * naptr_iterate_init(&tried)
  981. */
  982. struct rdata* naptr_sip_iterate(struct rdata* naptr_head,
  983. naptr_bmp_t* tried,
  984. str* srv_name, char* proto)
  985. {
  986. int i, idx;
  987. struct rdata* l;
  988. struct rdata* l_saved;
  989. struct naptr_rdata* naptr;
  990. struct naptr_rdata* naptr_saved;
  991. char saved_proto;
  992. char naptr_proto;
  993. idx=0;
  994. naptr_proto=PROTO_NONE;
  995. naptr_saved=0;
  996. l_saved=0;
  997. saved_proto=0;
  998. i=0;
  999. for(l=naptr_head; l && (i<MAX_NAPTR_RRS); l=l->next){
  1000. if (l->type!=T_NAPTR) continue;
  1001. naptr=(struct naptr_rdata*) l->rdata;
  1002. if (naptr==0){
  1003. LOG(L_CRIT, "naptr_iterate: BUG: null rdata\n");
  1004. goto end;
  1005. }
  1006. /* check if valid and get proto */
  1007. if ((naptr_proto=naptr_get_sip_proto(naptr))<=0) continue;
  1008. if (*tried& (1<<i)){
  1009. i++;
  1010. continue; /* already tried */
  1011. }
  1012. #ifdef NAPTR_DBG
  1013. DBG("naptr_iterate: found a valid sip NAPTR rr %.*s,"
  1014. " proto %d\n", naptr->repl_len, naptr->repl,
  1015. (int)naptr_proto);
  1016. #endif
  1017. if ((naptr_proto_supported(naptr_proto))){
  1018. if (naptr_choose(&naptr_saved, &saved_proto,
  1019. naptr, naptr_proto))
  1020. idx=i;
  1021. l_saved=l;
  1022. }
  1023. i++;
  1024. }
  1025. if (naptr_saved){
  1026. /* found something */
  1027. #ifdef NAPTR_DBG
  1028. DBG("naptr_iterate: choosed NAPTR rr %.*s, proto %d"
  1029. " tried: 0x%x\n", naptr_saved->repl_len,
  1030. naptr_saved->repl, (int)saved_proto, *tried);
  1031. #endif
  1032. *tried|=1<<idx;
  1033. *proto=saved_proto;
  1034. srv_name->s=naptr_saved->repl;
  1035. srv_name->len=naptr_saved->repl_len;
  1036. return l_saved;
  1037. }
  1038. end:
  1039. return 0;
  1040. }
  1041. /* internal sip naptr resolver function: resolves a host name trying:
  1042. * - NAPTR lookup if the address is not an ip and *proto==0 and *port==0.
  1043. * The result of the NAPTR query will be used for a SRV lookup
  1044. * - SRV lookup if the address is not an ip *port==0. The result of the SRV
  1045. * query will be used for an A/AAAA lookup.
  1046. * - normal A/AAAA lookup (either fallback from the above or if *port!=0
  1047. * and *proto!=0 or port==0 && proto==0)
  1048. * when performing SRV lookup (*port==0) it will use proto to look for
  1049. * tcp or udp hosts, otherwise proto is unused; if proto==0 => no SRV lookup
  1050. * returns: hostent struct & *port filled with the port from the SRV record;
  1051. * 0 on error
  1052. */
  1053. struct hostent* naptr_sip_resolvehost(str* name, unsigned short* port,
  1054. char* proto)
  1055. {
  1056. struct hostent* he;
  1057. struct ip_addr* ip;
  1058. static char tmp[MAX_DNS_NAME]; /* tmp. buff. for SRV lookups and
  1059. null. term strings */
  1060. struct rdata* l;
  1061. struct rdata* naptr_head;
  1062. char n_proto;
  1063. str srv_name;
  1064. naptr_bmp_t tried_bmp; /* tried bitmap */
  1065. naptr_head=0;
  1066. he=0;
  1067. if (name->len >= MAX_DNS_NAME) {
  1068. LOG(L_ERR, "naptr_sip_resolvehost: domain name too long\n");
  1069. goto end;
  1070. }
  1071. /* try NAPTR if no port or protocol is specified and NAPTR lookup is
  1072. * enabled */
  1073. if (port && proto && (*proto==0) && (*port==0)){
  1074. *proto=PROTO_UDP; /* just in case we don't find another */
  1075. if ( ((ip=str2ip(name))!=0)
  1076. #ifdef USE_IPV6
  1077. || ((ip=str2ip6(name))!=0)
  1078. #endif
  1079. ){
  1080. /* we are lucky, this is an ip address */
  1081. he=ip_addr2he(name,ip);
  1082. *port=SIP_PORT;
  1083. goto end;
  1084. }
  1085. memcpy(tmp, name->s, name->len);
  1086. tmp[name->len] = '\0';
  1087. naptr_head=get_record(tmp, T_NAPTR, RES_AR);
  1088. naptr_iterate_init(&tried_bmp);
  1089. while((l=naptr_sip_iterate(naptr_head, &tried_bmp,
  1090. &srv_name, &n_proto))!=0){
  1091. if ((he=srv_sip_resolvehost(&srv_name, 1, port, proto, 1, l))!=0){
  1092. *proto=n_proto;
  1093. return he;
  1094. }
  1095. }
  1096. /*clean up on exit*/
  1097. #ifdef RESOLVE_DBG
  1098. DBG("naptr_sip_resolvehost: no NAPTR record found for %.*s,"
  1099. " trying SRV lookup...\n", name->len, name->s);
  1100. #endif
  1101. }
  1102. /* fallback to normal srv lookup */
  1103. he=srv_sip_resolvehost(name, 0, port, proto, 0, 0);
  1104. end:
  1105. if (naptr_head)
  1106. free_rdata_list(naptr_head);
  1107. return he;
  1108. }
  1109. #endif /* USE_NAPTR */
  1110. /* resolves a host name trying:
  1111. * - NAPTR lookup if enabled, the address is not an ip and *proto==0 and
  1112. * *port==0. The result of the NAPTR query will be used for a SRV lookup
  1113. * - SRV lookup if the address is not an ip *port==0. The result of the SRV
  1114. * query will be used for an A/AAAA lookup.
  1115. * - normal A/AAAA lookup (either fallback from the above or if *port!=0
  1116. * and *proto!=0 or port==0 && proto==0)
  1117. * when performing SRV lookup (*port==0) it will use *proto to look for
  1118. * tcp or udp hosts, otherwise proto is unused; if proto==0 => no SRV lookup
  1119. *
  1120. * returns: hostent struct & *port filled with the port from the SRV record;
  1121. * 0 on error
  1122. */
  1123. struct hostent* _sip_resolvehost(str* name, unsigned short* port, char* proto)
  1124. {
  1125. #ifdef USE_NAPTR
  1126. if (dns_try_naptr)
  1127. return naptr_sip_resolvehost(name, port, proto);
  1128. #endif
  1129. return srv_sip_resolvehost(name, 0, port, proto, 0, 0);
  1130. }
  1131. /* resolve host, port, proto using sip rules (e.g. use SRV if port=0 a.s.o)
  1132. * and write the result in the sockaddr_union to
  1133. * returns -1 on error (resolve failed), 0 on success */
  1134. int sip_hostport2su(union sockaddr_union* su, str* name, unsigned short port,
  1135. char* proto)
  1136. {
  1137. struct hostent* he;
  1138. he=sip_resolvehost(name, &port, proto);
  1139. if (he==0){
  1140. ser_error=E_BAD_ADDRESS;
  1141. LOG(L_ERR, "ERROR: sip_hostport2su: could not resolve hostname:"
  1142. " \"%.*s\"\n", name->len, name->s);
  1143. goto error;
  1144. }
  1145. /* port filled by sip_resolvehost if empty*/
  1146. if (hostent2su(su, he, 0, port)<0){
  1147. ser_error=E_BAD_ADDRESS;
  1148. goto error;
  1149. }
  1150. return 0;
  1151. error:
  1152. return -1;
  1153. }