resolve.c 47 KB

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