resolve.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552
  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(void)
  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(void)
  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(void)
  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. else
  1406. #endif
  1407. res = srv_sip_resolvehost(name, 0, port, proto, 0, 0);
  1408. if( unlikely(!res) ){
  1409. /* failed DNS request */
  1410. counter_inc(dns_cnts_h.failed_dns_req);
  1411. }
  1412. return res;
  1413. }
  1414. /* resolve host, port, proto using sip rules (e.g. use SRV if port=0 a.s.o)
  1415. * and write the result in the sockaddr_union to
  1416. * returns -1 on error (resolve failed), 0 on success */
  1417. int sip_hostport2su(union sockaddr_union* su, str* name, unsigned short port,
  1418. char* proto)
  1419. {
  1420. struct hostent* he;
  1421. he=sip_resolvehost(name, &port, proto);
  1422. if (he==0){
  1423. ser_error=E_BAD_ADDRESS;
  1424. LOG(L_ERR, "ERROR: sip_hostport2su: could not resolve hostname:"
  1425. " \"%.*s\"\n", name->len, name->s);
  1426. goto error;
  1427. }
  1428. /* port filled by sip_resolvehost if empty*/
  1429. if (hostent2su(su, he, 0, port)<0){
  1430. ser_error=E_BAD_ADDRESS;
  1431. goto error;
  1432. }
  1433. return 0;
  1434. error:
  1435. return -1;
  1436. }