resolve.c 42 KB

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