resolve.c 41 KB

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