2
0

resolve.c 45 KB

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