resolve.c 36 KB

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