resolve.c 46 KB

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