You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

resolver.c 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. /*
  2. * Copyright (c) 2014, Vsevolod Stakhov
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY AUTHOR ''AS IS'' AND ANY
  15. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
  18. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  21. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include <sys/socket.h>
  26. #include <netinet/in.h>
  27. #include <arpa/inet.h>
  28. #include <unistd.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <errno.h>
  32. #include <stdarg.h>
  33. #include "rdns.h"
  34. #include "dns_private.h"
  35. #include "ottery.h"
  36. #include "util.h"
  37. #include "packet.h"
  38. #include "parse.h"
  39. #include "logger.h"
  40. #include "compression.h"
  41. static int
  42. rdns_send_request (struct rdns_request *req, int fd, bool new_req)
  43. {
  44. int r;
  45. struct rdns_server *serv = req->io->srv;
  46. struct rdns_resolver *resolver = req->resolver;
  47. struct rdns_request *tmp;
  48. struct dns_header *header;
  49. const int max_id_cycles = 32;
  50. /* Find ID collision */
  51. if (new_req) {
  52. r = 0;
  53. HASH_FIND_INT (req->io->requests, &req->id, tmp);
  54. while (tmp != NULL) {
  55. /* Check for unique id */
  56. header = (struct dns_header *)req->packet;
  57. header->qid = rdns_permutor_generate_id ();
  58. req->id = header->qid;
  59. if (++r > max_id_cycles) {
  60. return -1;
  61. }
  62. HASH_FIND_INT (req->io->requests, &req->id, tmp);
  63. }
  64. }
  65. if (resolver->curve_plugin == NULL) {
  66. r = send (fd, req->packet, req->pos, 0);
  67. }
  68. else {
  69. r = resolver->curve_plugin->cb.curve_plugin.send_cb (req,
  70. resolver->curve_plugin->data);
  71. }
  72. if (r == -1) {
  73. if (errno == EAGAIN || errno == EINTR) {
  74. if (new_req) {
  75. /* Write when socket is ready */
  76. HASH_ADD_INT (req->io->requests, id, req);
  77. req->async_event = resolver->async->add_write (resolver->async->data,
  78. fd, req);
  79. }
  80. /*
  81. * If request is already processed then the calling function
  82. * should take care about events processing
  83. */
  84. return 0;
  85. }
  86. else {
  87. rdns_debug ("send failed: %s for server %s", strerror (errno), serv->name);
  88. return -1;
  89. }
  90. }
  91. if (new_req) {
  92. /* Add request to hash table */
  93. HASH_ADD_INT (req->io->requests, id, req);
  94. /* Fill timeout */
  95. req->async_event = resolver->async->add_timer (resolver->async->data,
  96. req->timeout, req);
  97. req->state = RDNS_REQUEST_SENT;
  98. }
  99. return 1;
  100. }
  101. static struct rdns_reply *
  102. rdns_make_reply (struct rdns_request *req, enum dns_rcode rcode)
  103. {
  104. struct rdns_reply *rep;
  105. rep = malloc (sizeof (struct rdns_reply));
  106. if (rep != NULL) {
  107. rep->request = req;
  108. rep->resolver = req->resolver;
  109. rep->entries = NULL;
  110. rep->code = rcode;
  111. req->reply = rep;
  112. }
  113. return rep;
  114. }
  115. static struct rdns_request *
  116. rdns_find_dns_request (uint8_t *in, struct rdns_io_channel *ioc)
  117. {
  118. struct dns_header *header = (struct dns_header *)in;
  119. struct rdns_request *req;
  120. int id;
  121. struct rdns_resolver *resolver = ioc->resolver;
  122. id = header->qid;
  123. HASH_FIND_INT (ioc->requests, &id, req);
  124. if (req == NULL) {
  125. /* No such requests found */
  126. rdns_debug ("DNS request with id %d has not been found for IO channel", (int)id);
  127. }
  128. return req;
  129. }
  130. static bool
  131. rdns_parse_reply (uint8_t *in, int r, struct rdns_request *req,
  132. struct rdns_reply **_rep)
  133. {
  134. struct dns_header *header = (struct dns_header *)in;
  135. struct rdns_reply *rep;
  136. struct rdns_reply_entry *elt;
  137. uint8_t *pos, *npos;
  138. struct rdns_resolver *resolver = req->resolver;
  139. uint16_t qdcount;
  140. int type;
  141. bool found = false;
  142. int i, t;
  143. /* First check header fields */
  144. if (header->qr == 0) {
  145. rdns_info ("got request while waiting for reply");
  146. return false;
  147. }
  148. qdcount = ntohs (header->qdcount);
  149. if (qdcount != req->qcount) {
  150. rdns_info ("request has %d queries, reply has %d queries", (int)req->qcount, (int)header->qdcount);
  151. return false;
  152. }
  153. /*
  154. * Now we have request and query data is now at the end of header, so compare
  155. * request QR section and reply QR section
  156. */
  157. req->pos = sizeof (struct dns_header);
  158. pos = in + sizeof (struct dns_header);
  159. t = r - sizeof (struct dns_header);
  160. for (i = 0; i < (int)qdcount; i ++) {
  161. if ((npos = rdns_request_reply_cmp (req, pos,t)) == NULL) {
  162. rdns_info ("DNS request with id %d is for different query, ignoring", (int)req->id);
  163. return false;
  164. }
  165. t -= npos - pos;
  166. pos = npos;
  167. }
  168. /*
  169. * Now pos is in answer section, so we should extract data and form reply
  170. */
  171. rep = rdns_make_reply (req, header->rcode);
  172. if (rep == NULL) {
  173. rdns_warn ("Cannot allocate memory for reply");
  174. return false;
  175. }
  176. type = req->requested_names[0].type;
  177. if (rep->code == RDNS_RC_NOERROR) {
  178. r -= pos - in;
  179. /* Extract RR records */
  180. for (i = 0; i < ntohs (header->ancount); i ++) {
  181. elt = malloc (sizeof (struct rdns_reply_entry));
  182. t = rdns_parse_rr (resolver, in, elt, &pos, rep, &r);
  183. if (t == -1) {
  184. free (elt);
  185. rdns_debug ("incomplete reply");
  186. break;
  187. }
  188. else if (t == 1) {
  189. DL_APPEND (rep->entries, elt);
  190. if (elt->type == type) {
  191. found = true;
  192. }
  193. }
  194. else {
  195. rdns_debug ("no matching reply for %s",
  196. req->requested_names[0].name);
  197. free (elt);
  198. }
  199. }
  200. }
  201. if (!found && type != RDNS_REQUEST_ANY) {
  202. /* We have not found the requested RR type */
  203. rep->code = RDNS_RC_NOREC;
  204. }
  205. *_rep = rep;
  206. return true;
  207. }
  208. static void
  209. rdns_request_unschedule (struct rdns_request *req)
  210. {
  211. req->async->del_timer (req->async->data,
  212. req->async_event);
  213. /* Remove from id hashes */
  214. HASH_DEL (req->io->requests, req);
  215. }
  216. void
  217. rdns_process_read (int fd, void *arg)
  218. {
  219. struct rdns_io_channel *ioc = arg;
  220. struct rdns_resolver *resolver;
  221. struct rdns_request *req = NULL;
  222. ssize_t r;
  223. struct rdns_reply *rep;
  224. uint8_t in[UDP_PACKET_SIZE];
  225. resolver = ioc->resolver;
  226. /* First read packet from socket */
  227. if (resolver->curve_plugin == NULL) {
  228. r = read (fd, in, sizeof (in));
  229. if (r > (int)(sizeof (struct dns_header) + sizeof (struct dns_query))) {
  230. req = rdns_find_dns_request (in, ioc);
  231. }
  232. }
  233. else {
  234. r = resolver->curve_plugin->cb.curve_plugin.recv_cb (ioc, in,
  235. sizeof (in), resolver->curve_plugin->data, &req);
  236. if (req == NULL &&
  237. r > (int)(sizeof (struct dns_header) + sizeof (struct dns_query))) {
  238. req = rdns_find_dns_request (in, ioc);
  239. }
  240. }
  241. if (req != NULL) {
  242. if (rdns_parse_reply (in, r, req, &rep)) {
  243. UPSTREAM_OK (req->io->srv);
  244. req->state = RDNS_REQUEST_REPLIED;
  245. rdns_request_unschedule (req);
  246. req->func (rep, req->arg);
  247. REF_RELEASE (req);
  248. }
  249. }
  250. else {
  251. /* Still want to increase uses */
  252. ioc->uses ++;
  253. }
  254. }
  255. void
  256. rdns_process_timer (void *arg)
  257. {
  258. struct rdns_request *req = (struct rdns_request *)arg;
  259. struct rdns_reply *rep;
  260. int r;
  261. bool renew = false;
  262. struct rdns_resolver *resolver;
  263. struct rdns_server *serv = NULL;
  264. req->retransmits --;
  265. resolver = req->resolver;
  266. if (req->retransmits == 0) {
  267. UPSTREAM_FAIL (req->io->srv, time (NULL));
  268. rep = rdns_make_reply (req, RDNS_RC_TIMEOUT);
  269. req->state = RDNS_REQUEST_REPLIED;
  270. rdns_request_unschedule (req);
  271. req->func (rep, req->arg);
  272. REF_RELEASE (req);
  273. return;
  274. }
  275. if (!req->io->active) {
  276. /* Do not reschedule IO requests on inactive sockets */
  277. rdns_debug ("reschedule request with id: %d", (int)req->id);
  278. rdns_request_unschedule (req);
  279. REF_RELEASE (req->io);
  280. UPSTREAM_SELECT_ROUND_ROBIN (resolver->servers, serv);
  281. if (serv == NULL) {
  282. rdns_warn ("cannot find suitable server for request");
  283. rep = rdns_make_reply (req, RDNS_RC_SERVFAIL);
  284. req->state = RDNS_REQUEST_REPLIED;
  285. req->func (rep, req->arg);
  286. REF_RELEASE (req);
  287. }
  288. /* Select random IO channel */
  289. req->io = serv->io_channels[ottery_rand_uint32 () % serv->io_cnt];
  290. req->io->uses ++;
  291. REF_RETAIN (req->io);
  292. renew = true;
  293. }
  294. r = rdns_send_request (req, req->io->sock, renew);
  295. if (r == 0) {
  296. /* Retransmit one more time */
  297. req->async->del_timer (req->async->data,
  298. req->async_event);
  299. req->async_event = req->async->add_write (req->async->data,
  300. req->io->sock, req);
  301. req->state = RDNS_REQUEST_REGISTERED;
  302. }
  303. else if (r == -1) {
  304. UPSTREAM_FAIL (req->io->srv, time (NULL));
  305. rep = rdns_make_reply (req, RDNS_RC_NETERR);
  306. req->state = RDNS_REQUEST_REPLIED;
  307. rdns_request_unschedule (req);
  308. req->func (rep, req->arg);
  309. REF_RELEASE (req);
  310. }
  311. else {
  312. req->async->repeat_timer (req->async->data, req->async_event);
  313. }
  314. }
  315. static void
  316. rdns_process_periodic (void *arg)
  317. {
  318. struct rdns_resolver *resolver = (struct rdns_resolver*)arg;
  319. UPSTREAM_RESCAN (resolver->servers, time (NULL));
  320. }
  321. static void
  322. rdns_process_ioc_refresh (void *arg)
  323. {
  324. struct rdns_resolver *resolver = (struct rdns_resolver*)arg;
  325. struct rdns_server *serv;
  326. struct rdns_io_channel *ioc, *nioc;
  327. unsigned int i;
  328. if (resolver->max_ioc_uses > 0) {
  329. UPSTREAM_FOREACH (resolver->servers, serv) {
  330. for (i = 0; i < serv->io_cnt; i ++) {
  331. ioc = serv->io_channels[i];
  332. if (ioc->uses > resolver->max_ioc_uses) {
  333. /* Schedule IOC removing */
  334. nioc = calloc (1, sizeof (struct rdns_io_channel));
  335. if (nioc == NULL) {
  336. rdns_err ("calloc fails to allocate rdns_io_channel");
  337. continue;
  338. }
  339. nioc->sock = rdns_make_client_socket (serv->name, serv->port,
  340. SOCK_DGRAM);
  341. if (nioc->sock == -1) {
  342. rdns_err ("cannot open socket to %s: %s", serv->name,
  343. strerror (errno));
  344. free (nioc);
  345. continue;
  346. }
  347. nioc->srv = serv;
  348. nioc->active = true;
  349. nioc->resolver = resolver;
  350. nioc->async_io = resolver->async->add_read (resolver->async->data,
  351. nioc->sock, nioc);
  352. REF_INIT_RETAIN (nioc, rdns_ioc_free);
  353. serv->io_channels[i] = nioc;
  354. rdns_debug ("scheduled io channel for server %s to be refreshed after "
  355. "%lu usages", serv->name, (unsigned long)ioc->uses);
  356. ioc->active = false;
  357. REF_RELEASE (ioc);
  358. }
  359. }
  360. }
  361. }
  362. }
  363. void
  364. rdns_process_retransmit (int fd, void *arg)
  365. {
  366. struct rdns_request *req = (struct rdns_request *)arg;
  367. struct rdns_resolver *resolver;
  368. struct rdns_reply *rep;
  369. int r;
  370. resolver = req->resolver;
  371. resolver->async->del_write (resolver->async->data,
  372. req->async_event);
  373. r = rdns_send_request (req, fd, false);
  374. if (r == 0) {
  375. /* Retransmit one more time */
  376. req->async_event = req->async->add_write (req->async->data,
  377. fd, req);
  378. req->state = RDNS_REQUEST_REGISTERED;
  379. }
  380. else if (r == -1) {
  381. UPSTREAM_FAIL (req->io->srv, time (NULL));
  382. rep = rdns_make_reply (req, RDNS_RC_NETERR);
  383. req->state = RDNS_REQUEST_REPLIED;
  384. req->func (rep, req->arg);
  385. REF_RELEASE (req);
  386. }
  387. else {
  388. req->async_event = req->async->add_timer (req->async->data,
  389. req->timeout, req);
  390. req->state = RDNS_REQUEST_SENT;
  391. }
  392. }
  393. struct rdns_request*
  394. rdns_make_request_full (
  395. struct rdns_resolver *resolver,
  396. dns_callback_type cb,
  397. void *cbdata,
  398. double timeout,
  399. unsigned int repeats,
  400. unsigned int queries,
  401. ...
  402. )
  403. {
  404. va_list args;
  405. struct rdns_request *req;
  406. struct rdns_server *serv;
  407. int r, type;
  408. unsigned int i, tlen = 0, clen = 0, cur;
  409. size_t olen;
  410. const char *cur_name, *last_name = NULL;
  411. struct rdns_compression_entry *comp = NULL;
  412. if (!resolver->initialized) {
  413. return NULL;
  414. }
  415. req = malloc (sizeof (struct rdns_request));
  416. if (req == NULL) {
  417. return NULL;
  418. }
  419. req->resolver = resolver;
  420. req->func = cb;
  421. req->arg = cbdata;
  422. req->reply = NULL;
  423. req->qcount = queries;
  424. req->io = NULL;
  425. req->state = RDNS_REQUEST_NEW;
  426. req->packet = NULL;
  427. req->requested_names = calloc (queries, sizeof (struct rdns_request_name));
  428. if (req->requested_names == NULL) {
  429. free (req);
  430. return NULL;
  431. }
  432. req->type = 0;
  433. #ifdef TWEETNACL
  434. req->curve_plugin_data = NULL;
  435. #endif
  436. REF_INIT_RETAIN (req, rdns_request_free);
  437. /* Calculate packet's total length based on records count */
  438. va_start (args, queries);
  439. for (i = 0; i < queries * 2; i += 2) {
  440. cur = i / 2;
  441. cur_name = va_arg (args, const char *);
  442. if (cur_name != NULL) {
  443. last_name = cur_name;
  444. clen = strlen (cur_name);
  445. if (clen == 0) {
  446. rdns_info ("got empty name to resolve");
  447. rdns_request_free (req);
  448. return NULL;
  449. }
  450. tlen += clen;
  451. }
  452. else if (last_name == NULL) {
  453. rdns_info ("got NULL as the first name to resolve");
  454. rdns_request_free (req);
  455. return NULL;
  456. }
  457. if (!rdns_format_dns_name (resolver, last_name, clen,
  458. &req->requested_names[cur].name, &olen)) {
  459. rdns_request_free (req);
  460. return NULL;
  461. }
  462. type = va_arg (args, int);
  463. req->requested_names[cur].type = type;
  464. req->requested_names[cur].len = olen;
  465. }
  466. va_end (args);
  467. rdns_allocate_packet (req, tlen);
  468. rdns_make_dns_header (req, queries);
  469. for (i = 0; i < queries; i ++) {
  470. cur_name = req->requested_names[i].name;
  471. clen = req->requested_names[i].len;
  472. type = req->requested_names[i].type;
  473. if (queries > 1) {
  474. if (!rdns_add_rr (req, cur_name, clen, type, &comp)) {
  475. REF_RELEASE (req);
  476. rnds_compression_free (comp);
  477. return NULL;
  478. }
  479. }
  480. else {
  481. if (!rdns_add_rr (req, cur_name, clen, type, NULL)) {
  482. REF_RELEASE (req);
  483. rnds_compression_free (comp);
  484. return NULL;
  485. }
  486. }
  487. }
  488. rnds_compression_free (comp);
  489. /* Add EDNS RR */
  490. rdns_add_edns0 (req);
  491. req->retransmits = repeats;
  492. req->timeout = timeout;
  493. req->state = RDNS_REQUEST_NEW;
  494. req->async = resolver->async;
  495. UPSTREAM_SELECT_ROUND_ROBIN (resolver->servers, serv);
  496. if (serv == NULL) {
  497. rdns_warn ("cannot find suitable server for request");
  498. REF_RELEASE (req);
  499. return NULL;
  500. }
  501. /* Select random IO channel */
  502. req->io = serv->io_channels[ottery_rand_uint32 () % serv->io_cnt];
  503. req->io->uses ++;
  504. /* Now send request to server */
  505. r = rdns_send_request (req, req->io->sock, true);
  506. if (r == -1) {
  507. REF_RELEASE (req);
  508. return NULL;
  509. }
  510. REF_RETAIN (req->io);
  511. REF_RETAIN (req->resolver);
  512. return req;
  513. }
  514. bool
  515. rdns_resolver_init (struct rdns_resolver *resolver)
  516. {
  517. unsigned int i;
  518. struct rdns_server *serv;
  519. struct rdns_io_channel *ioc;
  520. if (!resolver->async_binded) {
  521. return false;
  522. }
  523. if (resolver->servers == NULL) {
  524. return false;
  525. }
  526. /* Now init io channels to all servers */
  527. UPSTREAM_FOREACH (resolver->servers, serv) {
  528. serv->io_channels = calloc (serv->io_cnt, sizeof (struct rdns_io_channel *));
  529. for (i = 0; i < serv->io_cnt; i ++) {
  530. ioc = calloc (1, sizeof (struct rdns_io_channel));
  531. if (ioc == NULL) {
  532. rdns_err ("cannot allocate memory for the resolver");
  533. return false;
  534. }
  535. ioc->sock = rdns_make_client_socket (serv->name, serv->port, SOCK_DGRAM);
  536. ioc->active = true;
  537. if (ioc->sock == -1) {
  538. rdns_err ("cannot open socket to %s:%d %s", serv->name, serv->port, strerror (errno));
  539. free (ioc);
  540. return false;
  541. }
  542. else {
  543. ioc->srv = serv;
  544. ioc->resolver = resolver;
  545. ioc->async_io = resolver->async->add_read (resolver->async->data,
  546. ioc->sock, ioc);
  547. REF_INIT_RETAIN (ioc, rdns_ioc_free);
  548. serv->io_channels[i] = ioc;
  549. }
  550. }
  551. }
  552. if (resolver->async->add_periodic) {
  553. resolver->periodic = resolver->async->add_periodic (resolver->async->data,
  554. UPSTREAM_REVIVE_TIME, rdns_process_periodic, resolver);
  555. }
  556. resolver->initialized = true;
  557. return true;
  558. }
  559. void
  560. rdns_resolver_register_plugin (struct rdns_resolver *resolver,
  561. struct rdns_plugin *plugin)
  562. {
  563. if (resolver != NULL && plugin != NULL) {
  564. /* XXX: support only network plugin now, and only a single one */
  565. if (plugin->type == RDNS_PLUGIN_CURVE) {
  566. resolver->curve_plugin = plugin;
  567. }
  568. }
  569. }
  570. bool
  571. rdns_resolver_add_server (struct rdns_resolver *resolver,
  572. const char *name, unsigned int port,
  573. int priority, unsigned int io_cnt)
  574. {
  575. struct rdns_server *serv;
  576. union {
  577. struct in_addr v4;
  578. struct in6_addr v6;
  579. } addr;
  580. if (inet_pton (AF_INET, name, &addr) == 0 &&
  581. inet_pton (AF_INET6, name, &addr) == 0) {
  582. /* Invalid IP */
  583. return false;
  584. }
  585. if (io_cnt == 0) {
  586. return false;
  587. }
  588. if (port == 0 || port > UINT16_MAX) {
  589. return false;
  590. }
  591. serv = calloc (1, sizeof (struct rdns_server));
  592. if (serv == NULL) {
  593. return false;
  594. }
  595. serv->name = strdup (name);
  596. if (serv->name == NULL) {
  597. free (serv);
  598. return false;
  599. }
  600. serv->io_cnt = io_cnt;
  601. serv->port = port;
  602. UPSTREAM_ADD (resolver->servers, serv, priority);
  603. return true;
  604. }
  605. void
  606. rdns_resolver_set_logger (struct rdns_resolver *resolver,
  607. rdns_log_function logger, void *log_data)
  608. {
  609. resolver->logger = logger;
  610. resolver->log_data = log_data;
  611. }
  612. void
  613. rdns_resolver_set_log_level (struct rdns_resolver *resolver,
  614. enum rdns_log_level level)
  615. {
  616. resolver->log_level = level;
  617. }
  618. void
  619. rdns_resolver_set_max_io_uses (struct rdns_resolver *resolver,
  620. uint64_t max_ioc_uses, double check_time)
  621. {
  622. if (resolver->refresh_ioc_periodic != NULL) {
  623. resolver->async->del_periodic (resolver->async->data,
  624. resolver->refresh_ioc_periodic);
  625. resolver->refresh_ioc_periodic = NULL;
  626. }
  627. resolver->max_ioc_uses = max_ioc_uses;
  628. if (check_time > 0.0 && resolver->async->add_periodic) {
  629. resolver->refresh_ioc_periodic =
  630. resolver->async->add_periodic (resolver->async->data,
  631. check_time, rdns_process_ioc_refresh, resolver);
  632. }
  633. }
  634. static void
  635. rdns_resolver_free (struct rdns_resolver *resolver)
  636. {
  637. struct rdns_server *serv, *stmp;
  638. struct rdns_io_channel *ioc;
  639. unsigned int i;
  640. if (resolver->initialized) {
  641. if (resolver->periodic != NULL) {
  642. resolver->async->del_periodic (resolver->async->data, resolver->periodic);
  643. }
  644. if (resolver->refresh_ioc_periodic != NULL) {
  645. resolver->async->del_periodic (resolver->async->data,
  646. resolver->refresh_ioc_periodic);
  647. }
  648. if (resolver->curve_plugin != NULL && resolver->curve_plugin->dtor != NULL) {
  649. resolver->curve_plugin->dtor (resolver, resolver->curve_plugin->data);
  650. }
  651. /* Stop IO watch on all IO channels */
  652. UPSTREAM_FOREACH_SAFE (resolver->servers, serv, stmp) {
  653. for (i = 0; i < serv->io_cnt; i ++) {
  654. ioc = serv->io_channels[i];
  655. REF_RELEASE (ioc);
  656. }
  657. serv->io_cnt = 0;
  658. UPSTREAM_DEL (resolver->servers, serv);
  659. free (serv->io_channels);
  660. free (serv->name);
  661. free (serv);
  662. }
  663. }
  664. free (resolver->async);
  665. free (resolver);
  666. }
  667. struct rdns_resolver *
  668. rdns_resolver_new (void)
  669. {
  670. struct rdns_resolver *new;
  671. new = calloc (1, sizeof (struct rdns_resolver));
  672. REF_INIT_RETAIN (new, rdns_resolver_free);
  673. new->logger = rdns_logger_internal;
  674. new->log_data = new;
  675. return new;
  676. }
  677. void
  678. rdns_resolver_async_bind (struct rdns_resolver *resolver,
  679. struct rdns_async_context *ctx)
  680. {
  681. if (resolver != NULL && ctx != NULL) {
  682. resolver->async = ctx;
  683. resolver->async_binded = true;
  684. }
  685. }