resolve.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734
  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 i, 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. char c;
  652. #ifdef USE_DNSSEC
  653. val_status_t val_status;
  654. #endif
  655. name_len=strlen(name);
  656. for (i = 0; i < name_len; i++) {
  657. c = name[i];
  658. if (((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')) ||
  659. ((c >= '0') && (c <= '9')) || (name[i] == '.') ||
  660. (name[i] == '-') || (name[i] == '_'))
  661. continue;
  662. LM_DBG("'%s' is not domain name\n", name);
  663. return 0;
  664. }
  665. if (cfg_get(core, core_cfg, dns_search_list)==0) {
  666. search_list_used=0;
  667. name_len=0;
  668. } else {
  669. search_list_used=1;
  670. }
  671. fullname_rd=0;
  672. #ifndef USE_DNSSEC
  673. size=res_search(name, C_IN, type, buff.buff, sizeof(buff));
  674. #else
  675. size=val_res_query((val_context_t *) NULL,
  676. (char *) name,
  677. (int) C_IN,
  678. (int) type,
  679. (unsigned char *) buff.buff,
  680. (int) sizeof(buff),
  681. &val_status);
  682. if(!val_istrusted(val_status)){
  683. LOG(L_INFO, "INFO: got not trusted record when resolving %s\n",name);
  684. }
  685. #endif
  686. if (unlikely(size<0)) {
  687. DBG("get_record: lookup(%s, %d) failed\n", name, type);
  688. goto not_found;
  689. }
  690. else if (unlikely(size > sizeof(buff))) size=sizeof(buff);
  691. head=rd=0;
  692. last=crt=&head;
  693. p=buff.buff+DNS_HDR_SIZE;
  694. end=buff.buff+size;
  695. if (unlikely(p>=end)) goto error_boundary;
  696. qno=ntohs((unsigned short)buff.hdr.qdcount);
  697. for (r=0; r<qno; r++){
  698. /* skip the name of the question */
  699. if (unlikely((p=dns_skipname(p, end))==0)) {
  700. LOG(L_ERR, "ERROR: get_record: skipname==0\n");
  701. goto error;
  702. }
  703. p+=2+2; /* skip QCODE & QCLASS */
  704. #if 0
  705. for (;(p<end && (*p)); p++);
  706. p+=1+2+2; /* skip the ending '\0, QCODE and QCLASS */
  707. #endif
  708. if (unlikely(p>end)) {
  709. LOG(L_ERR, "ERROR: get_record: p>=end\n");
  710. goto error;
  711. }
  712. };
  713. answers_no=ntohs((unsigned short)buff.hdr.ancount);
  714. again:
  715. for (r=0; (r<answers_no) && (p<end); r++){
  716. #if 0
  717. /* ignore it the default domain name */
  718. if ((p=dns_skipname(p, end))==0) {
  719. LOG(L_ERR, "ERROR: get_record: skip_name=0 (#2)\n");
  720. goto error;
  721. }
  722. #else
  723. if (unlikely((skip=dn_expand(buff.buff, end, p, rec_name,
  724. MAX_DNS_NAME-1))==-1)){
  725. LOG(L_ERR, "ERROR: get_record: dn_expand(rec_name) failed\n");
  726. goto error;
  727. }
  728. #endif
  729. p+=skip;
  730. rec_name_len=strlen(rec_name);
  731. if (unlikely(rec_name_len>255)){
  732. LOG(L_ERR, "ERROR: get_record: dn_expand(rec_name): name too"
  733. " long (%d)\n", rec_name_len);
  734. goto error;
  735. }
  736. /* check if enough space is left for type, class, ttl & size */
  737. if (unlikely((p+2+2+4+2)>end)) goto error_boundary;
  738. /* get type */
  739. memcpy((void*) &rtype, (void*)p, 2);
  740. rtype=ntohs(rtype);
  741. p+=2;
  742. /* get class */
  743. memcpy((void*) &class, (void*)p, 2);
  744. class=ntohs(class);
  745. p+=2;
  746. /* get ttl*/
  747. memcpy((void*) &ttl, (void*)p, 4);
  748. ttl=ntohl(ttl);
  749. p+=4;
  750. /* get size */
  751. memcpy((void*)&rdlength, (void*)p, 2);
  752. rdlength=ntohs(rdlength);
  753. p+=2;
  754. rd_end=p+rdlength;
  755. if (unlikely((rd_end)>end)) goto error_boundary;
  756. if ((flags & RES_ONLY_TYPE) && (rtype!=type)){
  757. /* skip */
  758. p=rd_end;
  759. continue;
  760. }
  761. /* expand the "type" record (rdata)*/
  762. rd=(struct rdata*) local_malloc(sizeof(struct rdata)+rec_name_len+
  763. 1-1);
  764. if (rd==0){
  765. LOG(L_ERR, "ERROR: get_record: out of memory\n");
  766. goto error;
  767. }
  768. rd->type=rtype;
  769. rd->pclass=class;
  770. rd->ttl=ttl;
  771. rd->next=0;
  772. memcpy(rd->name, rec_name, rec_name_len);
  773. rd->name[rec_name_len]=0;
  774. rd->name_len=rec_name_len;
  775. /* check if full name matches */
  776. if ((search_list_used==1)&&(fullname_rd==0)&&
  777. (rec_name_len>=name_len)&&
  778. (strncasecmp(rec_name, name, name_len)==0)) {
  779. /* now we have record whose name is the same (up-to the
  780. * name_len with the searched one):
  781. * if the length is the same - we found full match, no fake
  782. * cname needed, just clear the flag
  783. * if the length of the name differs - it has matched using
  784. * search list remember the rd, so we can create fake CNAME
  785. * record when all answers are used and no better match found
  786. */
  787. if (rec_name_len==name_len)
  788. search_list_used=0;
  789. /* this is safe.... here was rec_name_len > name_len */
  790. else if (rec_name[name_len]=='.') {
  791. #ifdef HAVE_RESOLV_RES
  792. if ((cfg_get(core, core_cfg, dns_search_fmatch)==0) ||
  793. (match_search_list(&_res, rec_name+name_len+1)!=0))
  794. #endif
  795. fullname_rd=rd;
  796. }
  797. }
  798. switch(rtype){
  799. case T_SRV:
  800. srv_rd= dns_srv_parser(buff.buff, end, rd_end, p);
  801. rd->rdata=(void*)srv_rd;
  802. if (unlikely(srv_rd==0)) goto error_parse;
  803. /* insert sorted into the list */
  804. for (crt=&head; *crt; crt= &((*crt)->next)){
  805. if ((*crt)->type!=T_SRV)
  806. continue;
  807. crt_srv=(struct srv_rdata*)(*crt)->rdata;
  808. if ((srv_rd->priority < crt_srv->priority) ||
  809. ( (srv_rd->priority == crt_srv->priority) &&
  810. (srv_rd->weight > crt_srv->weight) ) ){
  811. /* insert here */
  812. goto skip;
  813. }
  814. }
  815. last=&(rd->next); /*end of for => this will be the last
  816. element*/
  817. skip:
  818. /* insert here */
  819. rd->next=*crt;
  820. *crt=rd;
  821. break;
  822. case T_A:
  823. rd->rdata=(void*) dns_a_parser(p, rd_end);
  824. if (unlikely(rd->rdata==0)) goto error_parse;
  825. *last=rd; /* last points to the last "next" or the list
  826. head*/
  827. last=&(rd->next);
  828. break;
  829. case T_AAAA:
  830. rd->rdata=(void*) dns_aaaa_parser(p, rd_end);
  831. if (unlikely(rd->rdata==0)) goto error_parse;
  832. *last=rd;
  833. last=&(rd->next);
  834. break;
  835. case T_CNAME:
  836. rd->rdata=(void*) dns_cname_parser(buff.buff, end, p);
  837. if(unlikely(rd->rdata==0)) goto error_parse;
  838. *last=rd;
  839. last=&(rd->next);
  840. break;
  841. case T_NAPTR:
  842. rd->rdata=(void*)dns_naptr_parser(buff.buff, end, rd_end, p);
  843. if(unlikely(rd->rdata==0)) goto error_parse;
  844. *last=rd;
  845. last=&(rd->next);
  846. break;
  847. case T_TXT:
  848. rd->rdata= dns_txt_parser(buff.buff, rd_end, p);
  849. if (rd->rdata==0) goto error_parse;
  850. *last=rd;
  851. last=&(rd->next);
  852. break;
  853. case T_EBL:
  854. rd->rdata= dns_ebl_parser(buff.buff, end, rd_end, p);
  855. if (rd->rdata==0) goto error_parse;
  856. *last=rd;
  857. last=&(rd->next);
  858. break;
  859. case T_PTR:
  860. rd->rdata=(void*) dns_ptr_parser(buff.buff, end, p);
  861. if(unlikely(rd->rdata==0)) goto error_parse;
  862. *last=rd;
  863. last=&(rd->next);
  864. break;
  865. default:
  866. LOG(L_ERR, "WARNING: get_record: unknown type %d\n", rtype);
  867. rd->rdata=0;
  868. *last=rd;
  869. last=&(rd->next);
  870. }
  871. p+=rdlength;
  872. }
  873. if (flags & RES_AR){
  874. flags&=~RES_AR;
  875. answers_no=ntohs((unsigned short)buff.hdr.nscount);
  876. #ifdef RESOLVE_DBG
  877. DBG("get_record: skipping %d NS (p=%p, end=%p)\n", answers_no, p,
  878. end);
  879. #endif
  880. for (r=0; (r<answers_no) && (p<end); r++){
  881. /* skip over the ns records */
  882. if ((p=dns_skipname(p, end))==0) {
  883. LOG(L_ERR, "ERROR: get_record: skip_name=0 (#3)\n");
  884. goto error;
  885. }
  886. /* check if enough space is left for type, class, ttl & size */
  887. if (unlikely((p+2+2+4+2)>end)) goto error_boundary;
  888. memcpy((void*)&rdlength, (void*)p+2+2+4, 2);
  889. p+=2+2+4+2+ntohs(rdlength);
  890. }
  891. answers_no=ntohs((unsigned short)buff.hdr.arcount);
  892. #ifdef RESOLVE_DBG
  893. DBG("get_record: parsing %d ARs (p=%p, end=%p)\n", answers_no, p,
  894. end);
  895. #endif
  896. goto again; /* add also the additional records */
  897. }
  898. /* if the name was expanded using DNS search list
  899. * create fake CNAME record to convert the short name
  900. * (queried) to long name (answered)
  901. */
  902. if ((search_list_used==1)&&(fullname_rd!=0)) {
  903. rd=(struct rdata*) local_malloc(sizeof(struct rdata)+name_len+1-1);
  904. if (unlikely(rd==0)){
  905. LOG(L_ERR, "ERROR: get_record: out of memory\n");
  906. goto error;
  907. }
  908. rd->type=T_CNAME;
  909. rd->pclass=fullname_rd->pclass;
  910. rd->ttl=fullname_rd->ttl;
  911. rd->next=head;
  912. memcpy(rd->name, name, name_len);
  913. rd->name[name_len]=0;
  914. rd->name_len=name_len;
  915. /* alloc sizeof struct + space for the null terminated name */
  916. rd->rdata=(void*)local_malloc(sizeof(struct cname_rdata)-1+
  917. head->name_len+1);
  918. if(unlikely(rd->rdata==0)){
  919. LOG(L_ERR, "ERROR: get_record: out of memory\n");
  920. goto error_rd;
  921. }
  922. ((struct cname_rdata*)(rd->rdata))->name_len=fullname_rd->name_len;
  923. memcpy(((struct cname_rdata*)(rd->rdata))->name, fullname_rd->name,
  924. fullname_rd->name_len);
  925. ((struct cname_rdata*)(rd->rdata))->name[head->name_len]=0;
  926. head=rd;
  927. }
  928. return head;
  929. error_boundary:
  930. LOG(L_ERR, "ERROR: get_record: end of query buff reached\n");
  931. if (head) free_rdata_list(head);
  932. return 0;
  933. error_parse:
  934. LOG(L_ERR, "ERROR: get_record: rdata parse error (%s, %d), %p-%p"
  935. " rtype=%d, class=%d, ttl=%d, rdlength=%d \n",
  936. name, type,
  937. p, end, rtype, class, ttl, rdlength);
  938. error_rd:
  939. if (rd) local_free(rd); /* rd->rdata=0 & rd is not linked yet into
  940. the list */
  941. error:
  942. LOG(L_ERR, "ERROR: get_record \n");
  943. if (head) free_rdata_list(head);
  944. not_found:
  945. /* increment error counter */
  946. counter_inc(dns_cnts_h.failed_dns_req);
  947. return 0;
  948. }
  949. #ifdef USE_NAPTR
  950. /* service matching constants, lowercase */
  951. #define SIP_SCH 0x2b706973
  952. #define SIPS_SCH 0x73706973
  953. #define SIP_D2U 0x00753264
  954. #define SIP_D2T 0x00743264
  955. #define SIP_D2S 0x00733264
  956. #define SIPS_D2T 0x7432642b
  957. /* get protocol from a naptr rdata and check for validity
  958. * returns > 0 (PROTO_UDP, PROTO_TCP, PROTO_SCTP or PROTO_TLS)
  959. * <=0 on error
  960. */
  961. char naptr_get_sip_proto(struct naptr_rdata* n)
  962. {
  963. unsigned int s;
  964. char proto;
  965. proto=-1;
  966. if ((n->flags_len!=1) || ((*n->flags | 0x20 )!='s'))
  967. return -1;
  968. if (n->regexp_len!=0)
  969. return -1;
  970. /* SIP+D2U, SIP+D2T, SIP+D2S, SIPS+D2T */
  971. if (n->services_len==7){ /* SIP+D2X */
  972. s=n->services[0]+(n->services[1]<<8)+(n->services[2]<<16)+
  973. (n->services[3]<<24);
  974. s|=0x20202020;
  975. if (s==SIP_SCH){
  976. s=n->services[4]+(n->services[5]<<8)+(n->services[6]<<16);
  977. s|=0x00202020;
  978. switch(s){
  979. case SIP_D2U:
  980. proto=PROTO_UDP;
  981. break;
  982. case SIP_D2T:
  983. proto=PROTO_TCP;
  984. break;
  985. case SIP_D2S:
  986. proto=PROTO_SCTP;
  987. break;
  988. default:
  989. return -1;
  990. }
  991. }else{
  992. return -1;
  993. }
  994. }else if (n->services_len==8){ /*SIPS+D2T */
  995. s=n->services[0]+(n->services[1]<<8)+(n->services[2]<<16)+
  996. (n->services[3]<<24);
  997. s|=0x20202020;
  998. if (s==SIPS_SCH){
  999. s=n->services[4]+(n->services[5]<<8)+(n->services[6]<<16)+
  1000. (n->services[7]<<24);
  1001. s|=0x20202020;
  1002. if (s==SIPS_D2T){
  1003. proto=PROTO_TLS;
  1004. }
  1005. }else{
  1006. return -1;
  1007. }
  1008. }else{
  1009. return -1;
  1010. }
  1011. return proto;
  1012. }
  1013. inline static int proto_pref_score(char proto)
  1014. {
  1015. if ((proto>=PROTO_UDP) && (proto<= PROTO_LAST))
  1016. return naptr_proto_pref[(int)proto];
  1017. return 0;
  1018. }
  1019. /* returns true if we support the protocol */
  1020. int naptr_proto_supported(char proto)
  1021. {
  1022. if (proto_pref_score(proto)<0)
  1023. return 0;
  1024. switch(proto){
  1025. case PROTO_UDP:
  1026. return 1;
  1027. #ifdef USE_TCP
  1028. case PROTO_TCP:
  1029. return !tcp_disable;
  1030. #ifdef USE_TLS
  1031. case PROTO_TLS:
  1032. return !tls_disable;
  1033. #endif /* USE_TLS */
  1034. #endif /* USE_TCP */
  1035. #ifdef USE_SCTP
  1036. case PROTO_SCTP:
  1037. return !sctp_disable;
  1038. #endif
  1039. }
  1040. return 0;
  1041. }
  1042. /* returns true if new_proto is preferred over old_proto */
  1043. int naptr_proto_preferred(char new_proto, char old_proto)
  1044. {
  1045. return proto_pref_score(new_proto)>proto_pref_score(old_proto);
  1046. }
  1047. /* choose between 2 naptr records, should take into account local
  1048. * preferences too
  1049. * returns 1 if the new record was selected, 0 otherwise */
  1050. int naptr_choose (struct naptr_rdata** crt, char* crt_proto,
  1051. struct naptr_rdata* n , char n_proto)
  1052. {
  1053. #ifdef NAPTR_DBG
  1054. DBG("naptr_choose(o: %d w: %d p:%d , o: %d w:%d p:%d)\n",
  1055. *crt?(int)(*crt)->order:-1, *crt?(int)(*crt)->pref:-1,
  1056. (int)*crt_proto,
  1057. (int)n->order, (int)n->pref, (int)n_proto);
  1058. #endif
  1059. if ((*crt==0) || ((*crt_proto!=n_proto) &&
  1060. ( naptr_proto_preferred(n_proto, *crt_proto))) )
  1061. goto change;
  1062. if (!naptr_proto_preferred(*crt_proto, n_proto) &&
  1063. ((n->order<(*crt)->order) || ((n->order== (*crt)->order) &&
  1064. (n->pref < (*crt)->pref)))){
  1065. goto change;
  1066. }
  1067. #ifdef NAPTR_DBG
  1068. DBG("naptr_choose: no change\n");
  1069. #endif
  1070. return 0;
  1071. change:
  1072. #ifdef NAPTR_DBG
  1073. DBG("naptr_choose: changed\n");
  1074. #endif
  1075. *crt_proto=n_proto;
  1076. *crt=n;
  1077. return 1;
  1078. }
  1079. #endif /* USE_NAPTR */
  1080. /* internal sip srv resolver: resolves a host name trying:
  1081. * - SRV lookup if the address is not an ip *port==0. The result of the SRV
  1082. * query will be used for an A/AAAA lookup.
  1083. * - normal A/AAAA lookup (either fallback from the above or if *port!=0
  1084. * and *proto!=0 or port==0 && proto==0)
  1085. * when performing SRV lookup (*port==0) it will use *proto to look for
  1086. * tcp or udp hosts, otherwise proto is unused; if proto==0 => no SRV lookup
  1087. * If zt is set, name will be assumed to be 0 terminated and some copy
  1088. * operations will be avoided.
  1089. * If is_srv is set it will assume name has the srv prefixes for sip already
  1090. * appended and it's already 0-term'ed; if not it will append them internally.
  1091. * If ars !=0, it will first try to look through them and only if the SRV
  1092. * record is not found it will try doing a DNS query (ars will not be
  1093. * freed, the caller should take care of them)
  1094. * returns: hostent struct & *port filled with the port from the SRV record;
  1095. * 0 on error
  1096. */
  1097. struct hostent* srv_sip_resolvehost(str* name, int zt, unsigned short* port,
  1098. char* proto, int is_srv, struct rdata* ars)
  1099. {
  1100. struct hostent* he;
  1101. struct ip_addr* ip;
  1102. static char tmp[MAX_DNS_NAME]; /* tmp. buff. for SRV lookups and
  1103. null. term strings */
  1104. struct rdata* l;
  1105. struct srv_rdata* srv;
  1106. struct rdata* srv_head;
  1107. char* srv_target;
  1108. char srv_proto;
  1109. /* init */
  1110. srv_head=0;
  1111. srv_target=0;
  1112. if (name->len >= MAX_DNS_NAME) {
  1113. LOG(L_ERR, "sip_resolvehost: domain name too long\n");
  1114. he=0;
  1115. goto end;
  1116. }
  1117. #ifdef RESOLVE_DBG
  1118. DBG("srv_sip_resolvehost: %.*s:%d proto=%d\n", name->len, name->s,
  1119. port?(int)*port:-1, proto?(int)*proto:-1);
  1120. #endif
  1121. if (is_srv){
  1122. /* skip directly to srv resolving */
  1123. srv_proto=(proto)?*proto:0;
  1124. *port=(srv_proto==PROTO_TLS)?SIPS_PORT:SIP_PORT;
  1125. if (zt){
  1126. srv_target=name->s; /* name.s must be 0 terminated in
  1127. this case */
  1128. }else{
  1129. memcpy(tmp, name->s, name->len);
  1130. tmp[name->len] = '\0';
  1131. srv_target=tmp;
  1132. }
  1133. goto do_srv; /* skip to the actual srv query */
  1134. }
  1135. if (proto){ /* makes sure we have a protocol set*/
  1136. if (*proto==0)
  1137. *proto=srv_proto=PROTO_UDP; /* default */
  1138. else
  1139. srv_proto=*proto;
  1140. }else{
  1141. srv_proto=PROTO_UDP;
  1142. }
  1143. /* try SRV if no port specified (draft-ietf-sip-srv-06) */
  1144. if ((port)&&(*port==0)){
  1145. *port=(srv_proto==PROTO_TLS)?SIPS_PORT:SIP_PORT; /* just in case we
  1146. don't find another */
  1147. /* check if it's an ip address */
  1148. if (((ip=str2ip(name))!=0)
  1149. #ifdef USE_IPV6
  1150. || ((ip=str2ip6(name))!=0)
  1151. #endif
  1152. ){
  1153. /* we are lucky, this is an ip address */
  1154. he=ip_addr2he(name, ip);
  1155. goto end;
  1156. }
  1157. if ((name->len+SRV_MAX_PREFIX_LEN+1)>MAX_DNS_NAME){
  1158. LOG(L_WARN, "WARNING: sip_resolvehost: domain name too long (%d),"
  1159. " unable to perform SRV lookup\n", name->len);
  1160. }else{
  1161. switch(srv_proto){
  1162. case PROTO_NONE: /* no proto specified, use udp */
  1163. if (proto)
  1164. *proto=PROTO_UDP;
  1165. /* no break */
  1166. case PROTO_UDP:
  1167. memcpy(tmp, SRV_UDP_PREFIX, SRV_UDP_PREFIX_LEN);
  1168. memcpy(tmp+SRV_UDP_PREFIX_LEN, name->s, name->len);
  1169. tmp[SRV_UDP_PREFIX_LEN + name->len] = '\0';
  1170. break;
  1171. case PROTO_TCP:
  1172. memcpy(tmp, SRV_TCP_PREFIX, SRV_TCP_PREFIX_LEN);
  1173. memcpy(tmp+SRV_TCP_PREFIX_LEN, name->s, name->len);
  1174. tmp[SRV_TCP_PREFIX_LEN + name->len] = '\0';
  1175. break;
  1176. case PROTO_TLS:
  1177. memcpy(tmp, SRV_TLS_PREFIX, SRV_TLS_PREFIX_LEN);
  1178. memcpy(tmp+SRV_TLS_PREFIX_LEN, name->s, name->len);
  1179. tmp[SRV_TLS_PREFIX_LEN + name->len] = '\0';
  1180. break;
  1181. case PROTO_SCTP:
  1182. memcpy(tmp, SRV_SCTP_PREFIX, SRV_SCTP_PREFIX_LEN);
  1183. memcpy(tmp+SRV_SCTP_PREFIX_LEN, name->s, name->len);
  1184. tmp[SRV_SCTP_PREFIX_LEN + name->len] = '\0';
  1185. break;
  1186. default:
  1187. LOG(L_CRIT, "BUG: sip_resolvehost: unknown proto %d\n",
  1188. srv_proto);
  1189. he=0;
  1190. goto end;
  1191. }
  1192. srv_target=tmp;
  1193. do_srv:
  1194. /* try to find the SRV records inside previous ARs first*/
  1195. for (l=ars; l; l=l->next){
  1196. if (l->type!=T_SRV) continue;
  1197. srv=(struct srv_rdata*) l->rdata;
  1198. if (srv==0){
  1199. LOG(L_CRIT, "sip_resolvehost: BUG: null rdata\n");
  1200. /* cleanup on exit only */
  1201. break;
  1202. }
  1203. he=resolvehost(srv->name);
  1204. if (he!=0){
  1205. /* we found it*/
  1206. #ifdef RESOLVE_DBG
  1207. DBG("sip_resolvehost: found SRV(%s) = %s:%d in AR\n",
  1208. srv_target, srv->name, srv->port);
  1209. #endif
  1210. *port=srv->port;
  1211. /* cleanup on exit */
  1212. goto end;
  1213. }
  1214. }
  1215. srv_head=get_record(srv_target, T_SRV, RES_ONLY_TYPE);
  1216. for(l=srv_head; l; l=l->next){
  1217. if (l->type!=T_SRV) continue; /*should never happen*/
  1218. srv=(struct srv_rdata*) l->rdata;
  1219. if (srv==0){
  1220. LOG(L_CRIT, "sip_resolvehost: BUG: null rdata\n");
  1221. /* cleanup on exit only */
  1222. break;
  1223. }
  1224. he=resolvehost(srv->name);
  1225. if (he!=0){
  1226. /* we found it*/
  1227. #ifdef RESOLVE_DBG
  1228. DBG("sip_resolvehost: SRV(%s) = %s:%d\n",
  1229. srv_target, srv->name, srv->port);
  1230. #endif
  1231. *port=srv->port;
  1232. /* cleanup on exit */
  1233. goto end;
  1234. }
  1235. }
  1236. if (is_srv){
  1237. /* if the name was already into SRV format it doesn't make
  1238. * any sense to fall back to A/AAAA */
  1239. he=0;
  1240. goto end;
  1241. }
  1242. /* cleanup on exit */
  1243. #ifdef RESOLVE_DBG
  1244. DBG("sip_resolvehost: no SRV record found for %.*s,"
  1245. " trying 'normal' lookup...\n", name->len, name->s);
  1246. #endif
  1247. }
  1248. }
  1249. /*skip_srv:*/
  1250. if (likely(!zt)){
  1251. memcpy(tmp, name->s, name->len);
  1252. tmp[name->len] = '\0';
  1253. he=resolvehost(tmp);
  1254. }else{
  1255. he=resolvehost(name->s);
  1256. }
  1257. end:
  1258. #ifdef RESOLVE_DBG
  1259. DBG("srv_sip_resolvehost: returning %p (%.*s:%d proto=%d)\n",
  1260. he, name->len, name->s,
  1261. port?(int)*port:-1, proto?(int)*proto:-1);
  1262. #endif
  1263. if (srv_head)
  1264. free_rdata_list(srv_head);
  1265. return he;
  1266. }
  1267. #ifdef USE_NAPTR
  1268. /* iterates over a naptr rr list, returning each time a "good" naptr record
  1269. * is found.( srv type, no regex and a supported protocol)
  1270. * params:
  1271. * naptr_head - naptr rr list head
  1272. * tried - bitmap used to keep track of the already tried records
  1273. * (no more then sizeof(tried)*8 valid records are
  1274. * ever walked
  1275. * srv_name - if succesfull, it will be set to the selected record
  1276. * srv name (naptr repl.)
  1277. * proto - if succesfull it will be set to the selected record
  1278. * protocol
  1279. * returns 0 if no more records found or a pointer to the selected record
  1280. * and sets protocol and srv_name
  1281. * WARNING: when calling first time make sure you run first
  1282. * naptr_iterate_init(&tried)
  1283. */
  1284. struct rdata* naptr_sip_iterate(struct rdata* naptr_head,
  1285. naptr_bmp_t* tried,
  1286. str* srv_name, char* proto)
  1287. {
  1288. int i, idx;
  1289. struct rdata* l;
  1290. struct rdata* l_saved;
  1291. struct naptr_rdata* naptr;
  1292. struct naptr_rdata* naptr_saved;
  1293. char saved_proto;
  1294. char naptr_proto;
  1295. idx=0;
  1296. naptr_proto=PROTO_NONE;
  1297. naptr_saved=0;
  1298. l_saved=0;
  1299. saved_proto=0;
  1300. i=0;
  1301. for(l=naptr_head; l && (i<MAX_NAPTR_RRS); l=l->next){
  1302. if (l->type!=T_NAPTR) continue;
  1303. naptr=(struct naptr_rdata*) l->rdata;
  1304. if (naptr==0){
  1305. LOG(L_CRIT, "naptr_iterate: BUG: null rdata\n");
  1306. goto end;
  1307. }
  1308. /* check if valid and get proto */
  1309. if ((naptr_proto=naptr_get_sip_proto(naptr))<=0) continue;
  1310. if (*tried& (1<<i)){
  1311. i++;
  1312. continue; /* already tried */
  1313. }
  1314. #ifdef NAPTR_DBG
  1315. DBG("naptr_iterate: found a valid sip NAPTR rr %.*s,"
  1316. " proto %d\n", naptr->repl_len, naptr->repl,
  1317. (int)naptr_proto);
  1318. #endif
  1319. if ((naptr_proto_supported(naptr_proto))){
  1320. if (naptr_choose(&naptr_saved, &saved_proto,
  1321. naptr, naptr_proto))
  1322. idx=i;
  1323. l_saved=l;
  1324. }
  1325. i++;
  1326. }
  1327. if (naptr_saved){
  1328. /* found something */
  1329. #ifdef NAPTR_DBG
  1330. DBG("naptr_iterate: choosed NAPTR rr %.*s, proto %d"
  1331. " tried: 0x%x\n", naptr_saved->repl_len,
  1332. naptr_saved->repl, (int)saved_proto, *tried);
  1333. #endif
  1334. *tried|=1<<idx;
  1335. *proto=saved_proto;
  1336. srv_name->s=naptr_saved->repl;
  1337. srv_name->len=naptr_saved->repl_len;
  1338. return l_saved;
  1339. }
  1340. end:
  1341. return 0;
  1342. }
  1343. /* Resolves SRV if no naptr found.
  1344. * It reuse dns_pref values and according that resolves supported protocols.
  1345. * If dns_pref are equal then it use udp,tcp,tls,sctp order.
  1346. * returns: hostent struct & *port filled with the port from the SRV record;
  1347. * 0 on error
  1348. */
  1349. struct hostent* no_naptr_srv_sip_resolvehost(str* name, unsigned short* port, char* proto)
  1350. {
  1351. struct dns_srv_proto_t {
  1352. char proto;
  1353. int proto_pref;
  1354. } srv_proto_list[PROTO_LAST], tmp_srv_element;
  1355. struct hostent* he;
  1356. struct ip_addr* ip;
  1357. str srv_name;
  1358. static char tmp_srv[MAX_DNS_NAME]; /* tmp. buff. for SRV lookups */
  1359. int len;
  1360. unsigned char i,j,max,default_order=0,list_len=0;
  1361. /* init variables */
  1362. he=0;
  1363. len=0;
  1364. /* check if it's an ip address */
  1365. if (((ip=str2ip(name))!=0)
  1366. #ifdef USE_IPV6
  1367. || ((ip=str2ip6(name))!=0)
  1368. #endif
  1369. ){
  1370. /* we are lucky, this is an ip address */
  1371. /* set proto if needed - default udp */
  1372. if ((proto)&&(*proto==PROTO_NONE))
  1373. *proto=PROTO_UDP;
  1374. /* set port if needed - default 5060/5061 */
  1375. if ((port)&&(*port==0))
  1376. *port=((proto) && (*proto==PROTO_TLS))?SIPS_PORT:SIP_PORT;
  1377. he=ip_addr2he(name, ip);
  1378. return he;
  1379. }
  1380. if ((name->len+SRV_MAX_PREFIX_LEN+1)>MAX_DNS_NAME){
  1381. LOG(L_WARN, "WARNING: no_naptr_srv_sip_resolvehost: domain name too long"
  1382. " (%d), unable to perform SRV lookup\n", name->len);
  1383. } else {
  1384. /* if proto available, then add only the forced protocol to the list */
  1385. if (proto && *proto!=PROTO_NONE){
  1386. srv_proto_list[0].proto=*proto;
  1387. list_len=1;
  1388. } else {
  1389. /*get protocols and preference scores, and add availble protocol(s) and score(s) to the list*/
  1390. for (i=PROTO_UDP; i<PROTO_LAST;i++) {
  1391. tmp_srv_element.proto_pref = proto_pref_score(i);
  1392. /* if -1 so disabled continue with next protocol*/
  1393. if (naptr_proto_supported(i) == 0 ) {
  1394. continue;
  1395. } else {
  1396. srv_proto_list[i-1].proto_pref=tmp_srv_element.proto_pref;
  1397. srv_proto_list[i-1].proto=i;
  1398. list_len++;
  1399. }
  1400. };
  1401. /* if all protocol prefence scores equal, then set the perference to default values: udp,tcp,tls,sctp */
  1402. for (i=1; i<list_len;i++) {
  1403. if(srv_proto_list[0].proto_pref!=srv_proto_list[i].proto_pref){
  1404. default_order=0;
  1405. }
  1406. }
  1407. if (default_order){
  1408. for (i=0; i<list_len;i++) {
  1409. srv_proto_list[i].proto_pref=proto_pref_score(i);;
  1410. }
  1411. }
  1412. /* sorting the list */
  1413. for (i=0;i<list_len-1;i++) {
  1414. max=i;
  1415. for (j=i+1;j<list_len;j++) {
  1416. if (srv_proto_list[j].proto_pref>srv_proto_list[max].proto_pref) {
  1417. max=j;
  1418. }
  1419. }
  1420. if (i!=max) {
  1421. tmp_srv_element=srv_proto_list[i];
  1422. srv_proto_list[i]=srv_proto_list[max];
  1423. srv_proto_list[max]=tmp_srv_element;
  1424. }
  1425. }
  1426. }
  1427. /* looping on the ordered list until we found a protocol what has srv record */
  1428. for (i=0; i<list_len;i++) {
  1429. switch (srv_proto_list[i].proto) {
  1430. case PROTO_NONE: /* no proto specified, use udp */
  1431. if (proto)
  1432. *proto=PROTO_UDP;
  1433. /* no break */
  1434. case PROTO_UDP:
  1435. memcpy(tmp_srv, SRV_UDP_PREFIX, SRV_UDP_PREFIX_LEN);
  1436. memcpy(tmp_srv+SRV_UDP_PREFIX_LEN, name->s, name->len);
  1437. tmp_srv[SRV_UDP_PREFIX_LEN + name->len] = '\0';
  1438. len=SRV_UDP_PREFIX_LEN + name->len;
  1439. break;
  1440. case PROTO_TCP:
  1441. memcpy(tmp_srv, SRV_TCP_PREFIX, SRV_TCP_PREFIX_LEN);
  1442. memcpy(tmp_srv+SRV_TCP_PREFIX_LEN, name->s, name->len);
  1443. tmp_srv[SRV_TCP_PREFIX_LEN + name->len] = '\0';
  1444. len=SRV_TCP_PREFIX_LEN + name->len;
  1445. break;
  1446. case PROTO_TLS:
  1447. memcpy(tmp_srv, SRV_TLS_PREFIX, SRV_TLS_PREFIX_LEN);
  1448. memcpy(tmp_srv+SRV_TLS_PREFIX_LEN, name->s, name->len);
  1449. tmp_srv[SRV_TLS_PREFIX_LEN + name->len] = '\0';
  1450. len=SRV_TLS_PREFIX_LEN + name->len;
  1451. break;
  1452. case PROTO_SCTP:
  1453. memcpy(tmp_srv, SRV_SCTP_PREFIX, SRV_SCTP_PREFIX_LEN);
  1454. memcpy(tmp_srv+SRV_SCTP_PREFIX_LEN, name->s, name->len);
  1455. tmp_srv[SRV_SCTP_PREFIX_LEN + name->len] = '\0';
  1456. len=SRV_SCTP_PREFIX_LEN + name->len;
  1457. break;
  1458. default:
  1459. LOG(L_CRIT, "BUG: sip_resolvehost: unknown proto %d\n",
  1460. (int)srv_proto_list[i].proto);
  1461. return 0;
  1462. }
  1463. /* set default port */
  1464. if ((port)&&(*port==0)){
  1465. *port=(srv_proto_list[i].proto==PROTO_TLS)?SIPS_PORT:SIP_PORT; /* just in case we don't find another */
  1466. }
  1467. if ((proto)&&(*proto==0)){
  1468. *proto = PROTO_UDP;
  1469. }
  1470. srv_name.s=tmp_srv;
  1471. srv_name.len=len;
  1472. #ifdef USE_DNS_CACHE
  1473. he=dns_srv_get_he(&srv_name, port, dns_flags);
  1474. #else
  1475. he=srv_sip_resolvehost(&srv_name, 0, port, proto, 1, 0);
  1476. #endif
  1477. if (he!=0) {
  1478. return he;
  1479. }
  1480. }
  1481. }
  1482. return 0;
  1483. }
  1484. /* internal sip naptr resolver function: resolves a host name trying:
  1485. * - NAPTR lookup if the address is not an ip and *proto==0 and *port==0.
  1486. * The result of the NAPTR query will be used for a SRV lookup
  1487. * - SRV lookup if the address is not an ip *port==0. The result of the SRV
  1488. * query will be used for an A/AAAA lookup.
  1489. * - normal A/AAAA lookup (either fallback from the above or if *port!=0
  1490. * and *proto!=0 or port==0 && proto==0)
  1491. * when performing SRV lookup (*port==0) it will use proto to look for
  1492. * tcp or udp hosts, otherwise proto is unused; if proto==0 => no SRV lookup
  1493. * returns: hostent struct & *port filled with the port from the SRV record;
  1494. * 0 on error
  1495. */
  1496. struct hostent* naptr_sip_resolvehost(str* name, unsigned short* port,
  1497. char* proto)
  1498. {
  1499. struct hostent* he;
  1500. struct ip_addr* ip;
  1501. static char tmp[MAX_DNS_NAME]; /* tmp. buff. for SRV lookups and
  1502. null. term strings */
  1503. struct rdata* l;
  1504. struct rdata* naptr_head;
  1505. char n_proto;
  1506. str srv_name;
  1507. naptr_bmp_t tried_bmp; /* tried bitmap */
  1508. char origproto;
  1509. origproto = *proto;
  1510. naptr_head=0;
  1511. he=0;
  1512. if (name->len >= MAX_DNS_NAME) {
  1513. LOG(L_ERR, "naptr_sip_resolvehost: domain name too long\n");
  1514. goto end;
  1515. }
  1516. /* try NAPTR if no port or protocol is specified and NAPTR lookup is
  1517. * enabled */
  1518. if (port && proto && (*proto==0) && (*port==0)){
  1519. *proto=PROTO_UDP; /* just in case we don't find another */
  1520. if ( ((ip=str2ip(name))!=0)
  1521. #ifdef USE_IPV6
  1522. || ((ip=str2ip6(name))!=0)
  1523. #endif
  1524. ){
  1525. /* we are lucky, this is an ip address */
  1526. he=ip_addr2he(name,ip);
  1527. *port=SIP_PORT;
  1528. goto end;
  1529. }
  1530. memcpy(tmp, name->s, name->len);
  1531. tmp[name->len] = '\0';
  1532. naptr_head=get_record(tmp, T_NAPTR, RES_AR);
  1533. naptr_iterate_init(&tried_bmp);
  1534. while((l=naptr_sip_iterate(naptr_head, &tried_bmp,
  1535. &srv_name, &n_proto))!=0){
  1536. if ((he=srv_sip_resolvehost(&srv_name, 1, port, proto, 1, l))!=0){
  1537. *proto=n_proto;
  1538. return he;
  1539. }
  1540. }
  1541. /*clean up on exit*/
  1542. #ifdef RESOLVE_DBG
  1543. DBG("naptr_sip_resolvehost: no NAPTR record found for %.*s,"
  1544. " trying SRV lookup...\n", name->len, name->s);
  1545. #endif
  1546. }
  1547. /* fallback to srv lookup */
  1548. *proto = origproto;
  1549. he=no_naptr_srv_sip_resolvehost(name,port,proto);
  1550. /* fallback all the way down to A/AAAA */
  1551. if (he==0) {
  1552. he=dns_get_he(name,dns_flags);
  1553. }
  1554. end:
  1555. if (naptr_head)
  1556. free_rdata_list(naptr_head);
  1557. return he;
  1558. }
  1559. #endif /* USE_NAPTR */
  1560. /* resolves a host name trying:
  1561. * - NAPTR lookup if enabled, the address is not an ip and *proto==0 and
  1562. * *port==0. The result of the NAPTR query will be used for a SRV lookup
  1563. * - SRV lookup if the address is not an ip *port==0. The result of the SRV
  1564. * query will be used for an A/AAAA lookup.
  1565. * - normal A/AAAA lookup (either fallback from the above or if *port!=0
  1566. * and *proto!=0 or port==0 && proto==0)
  1567. * when performing SRV lookup (*port==0) it will use *proto to look for
  1568. * tcp or udp hosts, otherwise proto is unused; if proto==0 => no SRV lookup
  1569. *
  1570. * returns: hostent struct & *port filled with the port from the SRV record;
  1571. * 0 on error
  1572. */
  1573. struct hostent* _sip_resolvehost(str* name, unsigned short* port, char* proto)
  1574. {
  1575. struct hostent* res = NULL;
  1576. #ifdef USE_NAPTR
  1577. if (cfg_get(core, core_cfg, dns_try_naptr))
  1578. res = naptr_sip_resolvehost(name, port, proto);
  1579. else
  1580. #endif
  1581. res = srv_sip_resolvehost(name, 0, port, proto, 0, 0);
  1582. if( unlikely(!res) ){
  1583. /* failed DNS request */
  1584. counter_inc(dns_cnts_h.failed_dns_req);
  1585. }
  1586. return res;
  1587. }
  1588. /* resolve host, port, proto using sip rules (e.g. use SRV if port=0 a.s.o)
  1589. * and write the result in the sockaddr_union to
  1590. * returns -1 on error (resolve failed), 0 on success */
  1591. int sip_hostport2su(union sockaddr_union* su, str* name, unsigned short port,
  1592. char* proto)
  1593. {
  1594. struct hostent* he;
  1595. he=sip_resolvehost(name, &port, proto);
  1596. if (he==0){
  1597. ser_error=E_BAD_ADDRESS;
  1598. LOG(L_ERR, "ERROR: sip_hostport2su: could not resolve hostname:"
  1599. " \"%.*s\"\n", name->len, name->s);
  1600. goto error;
  1601. }
  1602. /* port filled by sip_resolvehost if empty*/
  1603. if (hostent2su(su, he, 0, port)<0){
  1604. ser_error=E_BAD_ADDRESS;
  1605. goto error;
  1606. }
  1607. return 0;
  1608. error:
  1609. return -1;
  1610. }