eventdns.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878
  1. /* $Id$ */
  2. /* The original version of this module was written by Adam Langley; for
  3. * a history of modifications, check out the subversion logs.
  4. *
  5. * When editiing this module, try to keep it re-mergeable by Adam. Don't
  6. * reformat the whitespace, add Tor dependencies, or so on.
  7. *
  8. * TODO:
  9. * - Support IPv6 and PTR records.
  10. */
  11. /* Async DNS Library
  12. * Adam Langley <agl@imperialviolet.org>
  13. * http://www.imperialviolet.org/eventdns.html
  14. * Public Domain codenext
  15. *
  16. * This software is Public Domain. To view a copy of the public domain dedication,
  17. * visit http://creativecommons.org/licenses/publicdomain/ or send a letter to
  18. * Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
  19. *
  20. * I ask and expect, but do not require, that all derivative works contain an
  21. * attribution similar to:
  22. * Parts developed by Adam Langley <agl@imperialviolet.org>
  23. *
  24. * You may wish to replace the word "Parts" with something else depending on
  25. * the amount of original code.
  26. *
  27. * (Derivative works does not include programs which link against, run or include
  28. * the source verbatim in their source distributions)
  29. *
  30. * Version: 0.1b
  31. *
  32. *
  33. * Welcome, gentle reader
  34. *
  35. * Async DNS lookups are really a whole lot harder than they should be,
  36. * mostly stemming from the fact that the libc resolver has never been
  37. * very good at them. Before you use this library you should see if libc
  38. * can do the job for you with the modern async call getaddrinfo_r
  39. * (Google for it). Otherwise, please continue.
  40. *
  41. * [I googled for getaddrinfo_r and got only two hits, one of which was this
  42. * code. Did you mean something different? -NM]
  43. *
  44. * This code is based on libevent and you must call event_init before
  45. * any of the APIs in this file. You must also seed the OpenSSL random
  46. * source if you are using OpenSSL for ids (see below).
  47. *
  48. * This library is designed to be included and shipped with your source
  49. * code. You statically link with it. You should also test for the
  50. * existence of strtok_r and define HAVE_STRTOK_R if you have it.
  51. *
  52. * The DNS protocol requires a good source of id numbers and these
  53. * numbers should be unpredictable for spoofing reasons. There are
  54. * three methods for generating them here and you must define exactly
  55. * one of them. In increasing order of preference:
  56. *
  57. * DNS_USE_GETTIMEOFDAY_FOR_ID:
  58. * Using the bottom 16 bits of the usec result from gettimeofday. This
  59. * is a pretty poor solution but should work anywhere.
  60. * DNS_USE_CPU_CLOCK_FOR_ID:
  61. * Using the bottom 16 bits of the nsec result from the CPU's time
  62. * counter. This is better, but may not work everywhere. Requires
  63. * POSIX realtime support and you'll need to link against -lrt on
  64. * glibc systems at least.
  65. * DNS_USE_OPENSSL_FOR_ID:
  66. * Uses the OpenSSL RAND_bytes call to generate the data. You must
  67. * have seeded the pool before making any calls to this library.
  68. *
  69. * The library keeps track of the state of nameservers and will avoid
  70. * them when they go down. Otherwise it will round robin between them.
  71. *
  72. * Quick start guide:
  73. * #include "eventdns.h"
  74. * void callback(int result, char type, int count, int ttl,
  75. * void *addresses, void *arg);
  76. * eventdns_resolv_conf_parse(DNS_OPTIONS_ALL, "/etc/resolv.conf");
  77. * eventdns_resolve("www.hostname.com", 0, callback, NULL);
  78. *
  79. * When the lookup is complete the callback function is called. The
  80. * first argument will be one of the DNS_ERR_* defines in eventdns.h.
  81. * Hopefully it will be DNS_ERR_NONE, in which case type will be
  82. * DNS_IPv4_A, count will be the number of IP addresses, ttl is the time
  83. * which the data can be cached for (in seconds), addresses will point
  84. * to an array of uint32_t's and arg will be whatever you passed to
  85. * eventdns_resolve.
  86. *
  87. * Searching:
  88. *
  89. * In order for this library to be a good replacement for glibc's resolver it
  90. * supports searching. This involves setting a list of default domains, in
  91. * which names will be queried for. The number of dots in the query name
  92. * determines the order in which this list is used.
  93. *
  94. * Searching appears to be a single lookup from the point of view of the API,
  95. * although many DNS queries may be generated from a single call to
  96. * eventdns_resolve. Searching can also drastically slow down the resolution
  97. * of names.
  98. *
  99. * To disable searching:
  100. * 1. Never set it up. If you never call eventdns_resolv_conf_parse or
  101. * eventdns_search_add then no searching will occur.
  102. *
  103. * 2. If you do call eventdns_resolv_conf_parse then don't pass
  104. * DNS_OPTION_SEARCH (or DNS_OPTIONS_ALL, which implies it)
  105. *
  106. * 3. When calling eventdns_resolve, pass the DNS_QUERY_NO_SEARCH flag
  107. *
  108. * The order of searches depends on the number of dots in the name. If the
  109. * number is greater than the ndots setting then the names is first tried
  110. * globally. Otherwise each search domain is appended in turn.
  111. *
  112. * The ndots setting can either be set from a resolv.conf, or by calling
  113. * eventdns_search_ndots_set.
  114. *
  115. * For example, with ndots set to 1 (the default) and a search domain list of
  116. * ["myhome.net"]:
  117. * Query: www
  118. * Order: www.myhome.net, www.
  119. *
  120. * Query: www.abc
  121. * Order: www.abc., www.abc.myhome.net
  122. *
  123. * API reference:
  124. *
  125. * int eventdns_nameserver_add(unsigned long int address)
  126. * Add a nameserver. The address should be an IP address in
  127. * network byte order. The type of address is chosen so that
  128. * it matches in_addr.s_addr.
  129. * Returns non-zero on error.
  130. *
  131. * int eventdns_nameserver_ip_add(const char *ip_as_string)
  132. * This wraps the above function by parsing a string as an IP
  133. * address and adds it as a nameserver.
  134. * Returns non-zero on error
  135. *
  136. * int eventdns_resolve(const char *name, int flags,
  137. * eventdns_callback_type callback,
  138. * void *ptr)
  139. * Resolve a name. The name parameter should be a DNS name.
  140. * The flags parameter should be 0, or DNS_QUERY_NO_SEARCH
  141. * which disables searching for this query. (see defn of
  142. * searching above).
  143. *
  144. * The callback argument is a function which is called when
  145. * this query completes and ptr is an argument which is passed
  146. * to that callback function.
  147. *
  148. * Returns non-zero on error
  149. *
  150. * void eventdns_search_clear()
  151. * Clears the list of search domains
  152. *
  153. * void eventdns_search_add(const char *domain)
  154. * Add a domain to the list of search domains
  155. *
  156. * void eventdns_search_ndots_set(int ndots)
  157. * Set the number of dots which, when found in a name, causes
  158. * the first query to be without any search domain.
  159. *
  160. * int eventdns_resolv_conf_parse(int flags, const char *filename)
  161. * Parse a resolv.conf like file from the given filename.
  162. *
  163. * See the man page for resolv.conf for the format of this file.
  164. * The flags argument determines what information is parsed from
  165. * this file:
  166. * DNS_OPTION_SEARCH - domain, search and ndots options
  167. * DNS_OPTION_NAMESERVERS - nameserver lines
  168. * DNS_OPTION_MISC - timeout and attempts options
  169. * DNS_OPTIONS_ALL - all of the above
  170. * The following directives are not parsed from the file:
  171. * sortlist, rotate, no-check-names, inet6, debug
  172. *
  173. * Returns non-zero on error:
  174. * 0 no errors
  175. * 1 failed to open file
  176. * 2 failed to stat file
  177. * 3 file too large
  178. * 4 out of memory
  179. * 5 short read from file
  180. *
  181. * Internals:
  182. *
  183. * Requests are kept in two queues. The first is the inflight queue. In
  184. * this queue requests have an allocated transaction id and nameserver.
  185. * They will soon be transmitted if they haven't already been.
  186. *
  187. * The second is the waiting queue. The size of the inflight ring is
  188. * limited and all other requests wait in waiting queue for space. This
  189. * bounds the number of concurrent requests so that we don't flood the
  190. * nameserver. Several algorithms require a full walk of the inflight
  191. * queue and so bounding its size keeps thing going nicely under huge
  192. * (many thousands of requests) loads.
  193. *
  194. * If a nameserver looses too many requests it is considered down and we
  195. * try not to use it. After a while we send a probe to that nameserver
  196. * (a lookup for google.com) and, if it replies, we consider it working
  197. * again. If the nameserver fails a probe we wait longer to try again
  198. * with the next probe.
  199. */
  200. #include "eventdns.h"
  201. #include "eventdns_tor.h"
  202. //#define NDEBUG
  203. #ifndef DNS_USE_CPU_CLOCK_FOR_ID
  204. #ifndef DNS_USE_GETTIMEOFDAY_FOR_ID
  205. #ifndef DNS_USE_OPENSSL_FOR_ID
  206. #error Must configure at least one id generation method.
  207. #error Please see the documentation
  208. #endif
  209. #endif
  210. #endif
  211. // #define _POSIX_C_SOURCE 200507
  212. #define _GNU_SOURCE
  213. #ifdef DNS_USE_CPU_CLOCK_FOR_ID
  214. #ifdef DNS_USE_OPENSSL_FOR_ID
  215. #error Multiple id options selected
  216. #endif
  217. #ifdef DNS_USE_GETTIMEOFDAY_FOR_ID
  218. #error Multiple id options selected
  219. #endif
  220. #include <time.h>
  221. #endif
  222. #ifdef DNS_USE_OPENSSL_FOR_ID
  223. #ifdef DNS_USE_GETTIMEOFDAY_FOR_ID
  224. #error Multiple id options selected
  225. #endif
  226. #include <openssl/rand.h>
  227. #endif
  228. #define _FORTIFY_SOURCE 3
  229. #include <string.h>
  230. #include <sys/types.h>
  231. #include <fcntl.h>
  232. #include <sys/socket.h>
  233. #include <sys/time.h>
  234. #include <stdint.h>
  235. #include <stdlib.h>
  236. #include <string.h>
  237. #include <errno.h>
  238. #include <assert.h>
  239. #include <netinet/in.h>
  240. #include <arpa/inet.h>
  241. #include <unistd.h>
  242. #include <limits.h>
  243. #include <sys/stat.h>
  244. #include <ctype.h>
  245. #include <stdio.h>
  246. #include <stdarg.h>
  247. #ifndef HOST_NAME_MAX
  248. #define HOST_NAME_MAX 255
  249. #endif
  250. #ifndef NDEBUG
  251. #include <stdio.h>
  252. #endif
  253. #undef MIN
  254. #define MIN(a,b) ((a)<(b)?(a):(b))
  255. #ifdef __USE_ISOC99B
  256. // libevent doesn't work without this
  257. typedef uint8_t u_char;
  258. typedef unsigned int uint;
  259. #endif
  260. #include <event.h>
  261. #define u64 uint64_t
  262. #define u32 uint32_t
  263. #define u16 uint16_t
  264. #define u8 uint8_t
  265. #include "eventdns.h"
  266. #define MAX_ADDRS 4 // maximum number of addresses from a single packet
  267. // which we bother recording
  268. #define TYPE_A 1
  269. #define TYPE_CNAME 5
  270. #define TYPE_PTR 12
  271. #define TYPE_AAAA 28
  272. #define CLASS_INET 1
  273. struct request {
  274. u8 *request; // the dns packet data
  275. uint request_len;
  276. int reissue_count;
  277. int tx_count; // the number of times that this packet has been sent
  278. void *user_pointer; // the pointer given to us for this request
  279. eventdns_callback_type user_callback;
  280. struct nameserver *ns; // the server which we last sent it
  281. // elements used by the searching code
  282. int search_index;
  283. struct search_state *search_state;
  284. char *search_origname; // needs to be free()ed
  285. int search_flags;
  286. // these objects are kept in a circular list
  287. struct request *next, *prev;
  288. struct event timeout_event;
  289. u16 trans_id; // the transaction id
  290. char request_appended; // true if the request pointer is data which follows this struct
  291. char transmit_me; // needs to be transmitted
  292. };
  293. struct nameserver {
  294. int socket; // a connected UDP socket
  295. u32 address;
  296. int failed_times; // number of times which we have given this server a chance
  297. int timedout; // number of times in a row a request has timed out
  298. struct event event;
  299. // these objects are kept in a circular list
  300. struct nameserver *next, *prev;
  301. struct event timeout_event; // used to keep the timeout for
  302. // when we next probe this server.
  303. // Valid if state == 0
  304. char state; // zero if we think that this server is down
  305. char choaked; // true if we have an EAGAIN from this server's socket
  306. char write_waiting; // true if we are waiting for EV_WRITE events
  307. };
  308. static struct request *req_head = NULL, *req_waiting_head = NULL;
  309. static struct nameserver *server_head = NULL;
  310. // The number of good nameservers that we have
  311. static int global_good_nameservers = 0;
  312. // inflight requests are contained in the req_head list
  313. // and are actually going out across the network
  314. static int global_requests_inflight = 0;
  315. // requests which aren't inflight are in the waiting list
  316. // and are counted here
  317. static int global_requests_waiting = 0;
  318. static int global_max_requests_inflight = 64;
  319. static struct timeval global_timeout = {3, 0}; // 3 seconds
  320. static int global_max_reissues = 1; // a reissue occurs when we get some errors from the server
  321. static int global_max_retransmits = 3; // number of times we'll retransmit a request which timed out
  322. // number of timeouts in a row before we consider this server to be down
  323. static int global_max_nameserver_timeout = 3;
  324. // These are the timeout values for nameservers. If we find a nameserver is down
  325. // we try to probe it at intervals as given below. Values are in seconds.
  326. static const struct timeval global_nameserver_timeouts[] = {{10, 0}, {60, 0}, {300, 0}, {900, 0}, {3600, 0}};
  327. static const int global_nameserver_timeouts_length = sizeof(global_nameserver_timeouts)/sizeof(struct timeval);
  328. const char *const eventdns_error_strings[] = {"no error", "The name server was unable to interpret the query", "The name server suffered an internal error", "The requested domain name does not exist", "The name server refused to reply to the request"};
  329. static struct nameserver *nameserver_pick(void);
  330. static void eventdns_request_insert(struct request *req, struct request **head);
  331. static void nameserver_ready_callback(int fd, short events, void *arg);
  332. static int eventdns_transmit(void);
  333. static int eventdns_request_transmit(struct request *req);
  334. static void nameserver_send_probe(struct nameserver *const ns);
  335. static void search_request_finished(struct request *const);
  336. static int search_try_next(struct request *const req);
  337. static int search_request_new(const char *const name, int flags, eventdns_callback_type user_callback, void *user_arg);
  338. static void eventdns_requests_pump_waiting_queue(void);
  339. static u16 transaction_id_pick(void);
  340. static struct request *request_new(const char *name, int flags, eventdns_callback_type callback, void *ptr);
  341. static void request_submit(struct request *req);
  342. #ifdef MS_WINDOWS
  343. static int
  344. last_error(int sock)
  345. {
  346. int optval, optvallen=sizeof(optval);
  347. int err = WSAGetLastError();
  348. if (err == WSAEWOULDBLOCK && sock >= 0) {
  349. if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (void*)&optval,
  350. &optvallen))
  351. return err;
  352. if (optval)
  353. return optval;
  354. }
  355. return err;
  356. }
  357. static int
  358. error_is_eagain(int err)
  359. {
  360. return err == EAGAIN || err == WSAEWOULDBLOCK;
  361. }
  362. #else
  363. #define last_error(sock) (errno)
  364. #define error_is_eagain(err) ((err) == EAGAIN)
  365. #endif
  366. #define ISSPACE(c) isspace((int)(unsigned char)(c))
  367. #define ISDIGIT(c) isdigit((int)(unsigned char)(c))
  368. #ifndef NDEBUG
  369. static const char *
  370. debug_ntoa(u32 address)
  371. {
  372. static char buf[32];
  373. u32 a = ntohl(address);
  374. sprintf(buf, "%d.%d.%d.%d",
  375. (int)(u8)((a>>24)&0xff),
  376. (int)(u8)((a>>16)&0xff),
  377. (int)(u8)((a>>8 )&0xff),
  378. (int)(u8)((a )&0xff));
  379. return buf;
  380. }
  381. #endif
  382. static eventdns_debug_log_fn_type eventdns_log_fn = NULL;
  383. void
  384. eventdns_set_log_fn(eventdns_debug_log_fn_type fn)
  385. {
  386. eventdns_log_fn = fn;
  387. }
  388. static void
  389. _eventdns_log(const char *fmt, ...)
  390. {
  391. va_list args;
  392. static char buf[512];
  393. if (!eventdns_log_fn)
  394. return;
  395. va_start(args,fmt);
  396. #ifdef MS_WINDOWS
  397. _vsnprintf(buf, sizeof(buf), fmt, args);
  398. #else
  399. vsnprintf(buf, sizeof(buf), fmt, args);
  400. #endif
  401. buf[sizeof(buf)-1] = '\0';
  402. eventdns_log_fn(buf);
  403. va_end(args);
  404. }
  405. #define log _eventdns_log
  406. // This walks the list of inflight requests to find the
  407. // one with a matching transaction id. Returns NULL on
  408. // failure
  409. static struct request *
  410. request_find_from_trans_id(u16 trans_id) {
  411. struct request *req = req_head, *const started_at = req_head;
  412. if (req) {
  413. do {
  414. if (req->trans_id == trans_id) return req;
  415. req = req->next;
  416. } while (req != started_at);
  417. }
  418. return NULL;
  419. }
  420. // a libevent callback function which is called when a nameserver
  421. // has gone down and we want to test if it has came back to life yet
  422. static void
  423. nameserver_prod_callback(int fd, short events, void *arg) {
  424. struct nameserver *const ns = (struct nameserver *) arg;
  425. (void)fd;
  426. (void)events;
  427. nameserver_send_probe(ns);
  428. }
  429. // a libevent callback which is called when a nameserver probe (to see if
  430. // it has come back to life) times out. We increment the count of failed_times
  431. // and wait longer to send the next probe packet.
  432. static void
  433. nameserver_probe_failed(struct nameserver *const ns) {
  434. const struct timeval * timeout;
  435. assert(ns->state == 0);
  436. evtimer_del(&ns->timeout_event);
  437. timeout =
  438. &global_nameserver_timeouts[MIN(ns->failed_times,
  439. global_nameserver_timeouts_length - 1)];
  440. ns->failed_times++;
  441. evtimer_set(&ns->timeout_event, nameserver_prod_callback, ns);
  442. evtimer_add(&ns->timeout_event, (struct timeval *) timeout);
  443. }
  444. // called when a nameserver has been deemed to have failed. For example, too
  445. // many packets have timed out etc
  446. static void
  447. nameserver_failed(struct nameserver *const ns, const char *msg) {
  448. struct request *req, *started_at;
  449. // if this nameserver has already been marked as failed
  450. // then don't do anything
  451. if (!ns->state) return;
  452. log("Nameserver %s has failed: %s", debug_ntoa(ns->address), msg);
  453. global_good_nameservers--;
  454. assert(global_good_nameservers >= 0);
  455. if (global_good_nameservers == 0) {
  456. log("All nameservers have failed");
  457. }
  458. ns->state = 0;
  459. ns->failed_times = 1;
  460. evtimer_set(&ns->timeout_event, nameserver_prod_callback, ns);
  461. evtimer_add(&ns->timeout_event, (struct timeval *) &global_nameserver_timeouts[0]);
  462. // walk the list of inflight requests to see if any can be reassigned to
  463. // a different server. Requests in the waiting queue don't have a
  464. // nameserver assigned yet
  465. // if we don't have *any* good nameservers then there's no point
  466. // trying to reassign requests to one
  467. if (!global_good_nameservers) return;
  468. req = req_head;
  469. started_at = req_head;
  470. if (req) {
  471. do {
  472. if (req->tx_count == 0 && req->ns == ns) {
  473. // still waiting to go out, can be moved
  474. // to another server
  475. req->ns = nameserver_pick();
  476. }
  477. req = req->next;
  478. } while (req != started_at);
  479. }
  480. }
  481. static void
  482. nameserver_up(struct nameserver *const ns) {
  483. if (ns->state) return;
  484. log("Nameserver %s is back up", debug_ntoa(ns->address));
  485. evtimer_del(&ns->timeout_event);
  486. ns->state = 1;
  487. ns->failed_times = 0;
  488. global_good_nameservers++;
  489. }
  490. static void
  491. request_trans_id_set(struct request *const req, const u16 trans_id) {
  492. req->trans_id = trans_id;
  493. *((u16 *) req->request) = htons(trans_id);
  494. }
  495. // Called to remove a request from a list and dealloc it.
  496. // head is a pointer to the head of the list it should be
  497. // removed from or NULL if the request isn't in a list.
  498. static void
  499. request_finished(struct request *const req, struct request **head) {
  500. if (head) {
  501. if (req->next == req) {
  502. // only item in the list
  503. *head = NULL;
  504. } else {
  505. req->next->prev = req->prev;
  506. req->prev->next = req->next;
  507. if (*head == req) *head = req->next;
  508. }
  509. }
  510. log("Removing timeout for request %lx", (unsigned long) req);
  511. evtimer_del(&req->timeout_event);
  512. search_request_finished(req);
  513. global_requests_inflight--;
  514. if (!req->request_appended) {
  515. // need to free the request data on it's own
  516. free(req->request);
  517. } else {
  518. // the request data is appended onto the header
  519. // so everything gets free()ed when we:
  520. }
  521. free(req);
  522. eventdns_requests_pump_waiting_queue();
  523. }
  524. // This is called when a server returns a funny error code.
  525. // We try the request again with another server.
  526. //
  527. // return:
  528. // 0 ok
  529. // 1 failed/reissue is pointless
  530. static int
  531. request_reissue(struct request *req) {
  532. const struct nameserver *const last_ns = req->ns;
  533. // the last nameserver should have been marked as failing
  534. // by the caller of this function, therefore pick will try
  535. // not to return it
  536. req->ns = nameserver_pick();
  537. if (req->ns == last_ns) {
  538. // ... but pick did return it
  539. // not a lot of point in trying again with the
  540. // same server
  541. return 1;
  542. }
  543. req->reissue_count++;
  544. req->tx_count = 0;
  545. req->transmit_me = 1;
  546. return 0;
  547. }
  548. // this function looks for space on the inflight queue and promotes
  549. // requests from the waiting queue if it can.
  550. static void
  551. eventdns_requests_pump_waiting_queue(void) {
  552. while (global_requests_inflight < global_max_requests_inflight &&
  553. global_requests_waiting) {
  554. struct request *req;
  555. // move a request from the waiting queue to the inflight queue
  556. assert(req_waiting_head);
  557. if (req_waiting_head->next == req_waiting_head) {
  558. // only one item in the queue
  559. req = req_waiting_head;
  560. req_waiting_head = NULL;
  561. } else {
  562. req = req_waiting_head;
  563. req->next->prev = req->prev;
  564. req->prev->next = req->next;
  565. req_waiting_head = req->next;
  566. }
  567. global_requests_waiting--;
  568. global_requests_inflight++;
  569. req->ns = nameserver_pick();
  570. request_trans_id_set(req, transaction_id_pick());
  571. eventdns_request_insert(req, &req_head);
  572. eventdns_request_transmit(req);
  573. eventdns_transmit();
  574. }
  575. }
  576. // this processes a parsed reply packet
  577. static void
  578. reply_handle(u16 trans_id, u16 flags, u32 ttl, u32 addrcount, u32 *addresses) {
  579. int error;
  580. static const int error_codes[] = {DNS_ERR_FORMAT, DNS_ERR_SERVERFAILED, DNS_ERR_NOTEXIST, DNS_ERR_NOTIMPL, DNS_ERR_REFUSED};
  581. struct request *const req = request_find_from_trans_id(trans_id);
  582. if (!req) return;
  583. if (flags & 0x020f || !addrcount) {
  584. // there was an error
  585. if (flags & 0x0200) {
  586. error = DNS_ERR_TRUNCATED;
  587. } else {
  588. u16 error_code = (flags & 0x000f) - 1;
  589. if (error_code > 4) {
  590. error = DNS_ERR_UNKNOWN;
  591. } else {
  592. error = error_codes[error_code];
  593. }
  594. }
  595. switch(error) {
  596. case DNS_ERR_SERVERFAILED:
  597. case DNS_ERR_NOTIMPL:
  598. case DNS_ERR_REFUSED:
  599. // we regard these errors as marking a bad nameserver
  600. if (req->reissue_count < global_max_reissues) {
  601. char msg[64];
  602. snprintf(msg, sizeof(msg), "Bad response %d",
  603. error);
  604. nameserver_failed(req->ns, msg);
  605. if (!request_reissue(req)) return;
  606. }
  607. break;
  608. default:
  609. // we got a good reply from the nameserver
  610. nameserver_up(req->ns);
  611. }
  612. if (req->search_state) {
  613. // if we have a list of domains to search in, try the next one
  614. if (!search_try_next(req)) {
  615. // a new request was issued so this request is finished and
  616. // the user callback will be made when that request (or a
  617. // child of it) finishes.
  618. request_finished(req, &req_head);
  619. return;
  620. }
  621. }
  622. // all else failed. Pass the failure up
  623. req->user_callback(error, 0, 0, 0, NULL, req->user_pointer);
  624. request_finished(req, &req_head);
  625. } else {
  626. // all ok, tell the user
  627. req->user_callback(DNS_ERR_NONE, DNS_IPv4_A, addrcount, ttl, addresses, req->user_pointer);
  628. nameserver_up(req->ns);
  629. request_finished(req, &req_head);
  630. }
  631. }
  632. // parses a raw packet from the wire
  633. static void
  634. reply_parse(u8 *packet, int length) {
  635. int j = 0; // index into packet
  636. u16 _t; // used by the macros
  637. u32 _t32; // used by the macros
  638. #define GET32(x) do { if (j + 4 > length) return; memcpy(&_t32, packet + j, 4); j += 4; x = ntohl(_t32); } while(0);
  639. #define GET16(x) do { if (j + 2 > length) return; memcpy(&_t, packet + j, 2); j += 2; x = ntohs(_t); } while(0);
  640. #define GET8(x) do { if (j >= length) return; x = packet[j++]; } while(0);
  641. u16 trans_id, flags, questions, answers, authority, additional, datalength;
  642. u32 ttl, ttl_r = 0xffffffff;
  643. u32 addresses[MAX_ADDRS];
  644. int addresses_done = 0;
  645. uint i;
  646. GET16(trans_id);
  647. GET16(flags);
  648. GET16(questions);
  649. GET16(answers);
  650. GET16(authority);
  651. GET16(additional);
  652. if (!(flags & 0x8000)) return; // must be an answer
  653. if (flags & 0x020f) {
  654. // there was an error
  655. reply_handle(trans_id, flags, 0, 0, NULL);
  656. return;
  657. }
  658. // if (!answers) return; // must have an answer of some form
  659. // This macro skips a name in the DNS reply. Normally the
  660. // names are a series of length prefixed strings terminated with
  661. // a length of 0 (the lengths are u8's < 63).
  662. // However, the length can start with a pair of 1 bits and that
  663. // means that the next 14 bits are a pointer within the current
  664. // packet. The name stops after a pointer like that.
  665. #define SKIP_NAME \
  666. for(;;) { \
  667. u8 label_len; \
  668. GET8(label_len); \
  669. if (!label_len) break; \
  670. if (label_len & 0xc0) { \
  671. GET8(label_len); \
  672. break; \
  673. } \
  674. if (label_len > 63) return; \
  675. j += label_len; \
  676. }
  677. // skip over each question in the reply
  678. for (i = 0; i < questions; ++i) {
  679. // the question looks like
  680. // <label:name><u16:type><u16:class>
  681. SKIP_NAME;
  682. j += 4;
  683. }
  684. // now we have the answer section which looks like
  685. // <label:name><u16:type><u16:class><u32:ttl><u16:len><data...>
  686. for (i = 0; i < answers; ++i) {
  687. u16 type, class;
  688. SKIP_NAME;
  689. GET16(type);
  690. GET16(class);
  691. GET32(ttl);
  692. GET16(datalength);
  693. if (type == TYPE_A && class == CLASS_INET) {
  694. const int addrcount = datalength >> 2; // each IP address is 4 bytes
  695. // XXXX do something sane with malformed A answers.
  696. const int addrtocopy = MIN(MAX_ADDRS - addresses_done, addrcount);
  697. ttl_r = MIN(ttl_r, ttl);
  698. // we only bother with the first four addresses.
  699. if (j + 4*addrtocopy > length) return;
  700. memcpy(&addresses[addresses_done], packet + j, 4*addrtocopy);
  701. j += 4*addrtocopy;
  702. addresses_done += addrtocopy;
  703. if (addresses_done == MAX_ADDRS) break;
  704. } else {
  705. // skip over any other type of resource
  706. j += datalength;
  707. }
  708. }
  709. reply_handle(trans_id, flags, ttl_r, addresses_done, addresses);
  710. #undef SKIP_NAME
  711. #undef GET32
  712. #undef GET16
  713. #undef GET8
  714. }
  715. // Try to choose a strong transaction id which isn't already in flight
  716. static u16
  717. transaction_id_pick(void) {
  718. for (;;) {
  719. const struct request *req = req_head, *started_at;
  720. #ifdef DNS_USE_CPU_CLOCK_FOR_ID
  721. struct timespec ts;
  722. const u16 trans_id = ts.tv_nsec & 0xffff;
  723. if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts)) abort();
  724. #endif
  725. #ifdef DNS_USE_GETTIMEOFDAY_FOR_ID
  726. struct timeval tv;
  727. const u16 trans_id = tv.tv_usec & 0xffff;
  728. gettimeofday(&tv, NULL);
  729. #endif
  730. #ifdef DNS_USE_OPENSSL_FOR_ID
  731. u16 trans_id;
  732. if (RAND_pseudo_bytes((u8 *) &trans_id, 2) == -1) {
  733. /* // in the case that the RAND call fails we back
  734. // down to using gettimeofday.
  735. struct timeval tv;
  736. gettimeofday(&tv, NULL);
  737. trans_id = tv.tv_usec & 0xffff; */
  738. abort();
  739. }
  740. #endif
  741. if (trans_id == 0xffff) continue;
  742. // now check to see if that id is already inflight
  743. req = started_at = req_head;
  744. if (req) {
  745. do {
  746. if (req->trans_id == trans_id) break;
  747. req = req->next;
  748. } while (req != started_at);
  749. }
  750. // we didn't find it, so this is a good id
  751. if (req == started_at) return trans_id;
  752. }
  753. }
  754. // choose a namesever to use. This function will try to ignore
  755. // nameservers which we think are down and load balance across the rest
  756. // by updating the server_head global each time.
  757. static struct nameserver *
  758. nameserver_pick(void) {
  759. struct nameserver *started_at = server_head, *picked;
  760. if (!server_head) return NULL;
  761. // if we don't have any good nameservers then there's no
  762. // point in trying to find one.
  763. if (!global_good_nameservers) {
  764. server_head = server_head->next;
  765. return server_head;
  766. }
  767. // remember that nameservers are in a circular list
  768. for (;;) {
  769. if (server_head->state) {
  770. // we think this server is currently good
  771. picked = server_head;
  772. server_head = server_head->next;
  773. return picked;
  774. }
  775. server_head = server_head->next;
  776. if (server_head == started_at) {
  777. // all the nameservers seem to be down
  778. // so we just return this one and hope for the
  779. // best
  780. assert(global_good_nameservers == 0);
  781. picked = server_head;
  782. server_head = server_head->next;
  783. return picked;
  784. }
  785. }
  786. }
  787. // this is called when a namesever socket is ready for reading
  788. static void
  789. nameserver_read(struct nameserver *ns) {
  790. u8 packet[1500];
  791. for (;;) {
  792. const int r = recv(ns->socket, packet, sizeof(packet), 0);
  793. if (r < 0) {
  794. int err = last_error(ns_socket);
  795. if (error_is_eagain(err)) return;
  796. nameserver_failed(ns, strerror(err));
  797. return;
  798. }
  799. reply_parse(packet, r);
  800. }
  801. }
  802. // set if we are waiting for the ability to write to this server.
  803. // if waiting is true then we ask libevent for EV_WRITE events, otherwise
  804. // we stop these events.
  805. static void
  806. nameserver_write_waiting(struct nameserver *ns, char waiting) {
  807. if (ns->write_waiting == waiting) return;
  808. ns->write_waiting = waiting;
  809. event_del(&ns->event);
  810. event_set(&ns->event, ns->socket, EV_READ | (waiting ? EV_WRITE : 0) | EV_PERSIST,
  811. nameserver_ready_callback, ns);
  812. event_add(&ns->event, NULL);
  813. }
  814. // a callback function. Called by libevent when the kernel says that
  815. // a nameserver socket is ready for writing or reading
  816. static void
  817. nameserver_ready_callback(int fd, short events, void *arg) {
  818. struct nameserver *ns = (struct nameserver *) arg;
  819. (void)fd;
  820. if (events & EV_WRITE) {
  821. ns->choaked = 0;
  822. if (!eventdns_transmit()) {
  823. nameserver_write_waiting(ns, 0);
  824. }
  825. }
  826. if (events & EV_READ) {
  827. nameserver_read(ns);
  828. }
  829. }
  830. // Converts a string to a length-prefixed set of DNS labels.
  831. // @buf must be strlen(name)+2 or longer. name and buf must
  832. // not overlap. name_len should be the length of name
  833. //
  834. // Input: abc.def
  835. // Output: <3>abc<3>def<0>
  836. //
  837. // Returns the length of the data. negative on error
  838. // -1 label was > 63 bytes
  839. // -2 name was > 255 bytes
  840. static int
  841. dnsname_to_labels(u8 *const buf, const char *name, const int name_len) { \
  842. const char *end = name + name_len; \
  843. int j = 0; // current offset into buf
  844. if (name_len > 255) return -2;
  845. for (;;) {
  846. const char *const start = name;
  847. name = strchr(name, '.');
  848. if (!name) {
  849. const uint label_len = end - start;
  850. if (label_len > 63) return -1;
  851. buf[j++] = label_len;
  852. memcpy(buf + j, start, end - start);
  853. j += end - start;
  854. break;
  855. } else {
  856. // append length of the label.
  857. const uint label_len = name - start;
  858. if (label_len > 63) return -1;
  859. buf[j++] = label_len;
  860. memcpy(buf + j, start, name - start);
  861. j += name - start;
  862. // hop over the '.'
  863. name++;
  864. }
  865. }
  866. // the labels must be terminated by a 0.
  867. // It's possible that the name ended in a .
  868. // in which case the zero is already there
  869. if (!j || buf[j-1]) buf[j++] = 0;
  870. return j;
  871. }
  872. // Finds the length of a dns request for a DNS name of the given
  873. // length. The actual request may be smaller than the value returned
  874. // here
  875. static int
  876. eventdns_request_len(const int name_len) {
  877. return 96 + // length of the DNS standard header
  878. name_len + 2 +
  879. 4; // space for the resource type
  880. }
  881. // build a dns request packet into buf. buf should be at least as long
  882. // as eventdns_request_len told you it should be.
  883. //
  884. // Returns the amount of space used. Negative on error.
  885. static int
  886. eventdns_request_data_build(const char *const name, const int name_len, const u16 trans_id,
  887. const u16 type, const u16 class,
  888. u8 *const buf) {
  889. int j = 0; // current offset into buf
  890. u16 _t; // used by the macros
  891. u8 *labels;
  892. int labels_len;
  893. #define APPEND16(x) do { _t = htons(x); memcpy(buf + j, &_t, 2); j += 2; } while(0);
  894. APPEND16(trans_id);
  895. APPEND16(0x0100); // standard query, recusion needed
  896. APPEND16(1); // one question
  897. APPEND16(0); // no answers
  898. APPEND16(0); // no authority
  899. APPEND16(0); // no additional
  900. labels = (u8 *) malloc(name_len + 2);
  901. if (!labels) return -1;
  902. labels_len = dnsname_to_labels(labels, name, name_len);
  903. if (labels_len < 0) return labels_len;
  904. memcpy(buf + j, labels, labels_len);
  905. j += labels_len;
  906. APPEND16(type);
  907. APPEND16(class);
  908. #undef APPEND16
  909. return j;
  910. }
  911. // this is a libevent callback function which is called when a request
  912. // has timed out.
  913. static void
  914. eventdns_request_timeout_callback(int fd, short events, void *arg) {
  915. struct request *const req = (struct request *) arg;
  916. (void) fd;
  917. (void) events;
  918. log("Request %lx timed out", (unsigned long) arg);
  919. req->ns->timedout++;
  920. if (req->ns->timedout > global_max_nameserver_timeout) {
  921. nameserver_failed(req->ns, "request timed out.");
  922. }
  923. evtimer_del(&req->timeout_event);
  924. if (req->tx_count >= global_max_retransmits) {
  925. // this request has failed
  926. req->user_callback(DNS_ERR_TIMEOUT, 0, 0, 0, NULL, req->user_pointer);
  927. request_finished(req, &req_head);
  928. } else {
  929. // retransmit it
  930. eventdns_request_transmit(req);
  931. }
  932. }
  933. // try to send a request to a given server.
  934. //
  935. // return:
  936. // 0 ok
  937. // 1 temporary failure
  938. // 2 other failure
  939. static int
  940. eventdns_request_transmit_to(struct request *req, struct nameserver *server) {
  941. const int r = send(server->socket, req->request, req->request_len, 0);
  942. if (r < 0) {
  943. int err = last_error(server->socket);
  944. if (error_is_eagain(err)) return 1;
  945. nameserver_failed(req->ns, strerror(err));
  946. return 2;
  947. } else if (r != (int)req->request_len) {
  948. return 1; // short write
  949. } else {
  950. return 0;
  951. }
  952. }
  953. // try to send a request, updating the fields of the request
  954. // as needed
  955. //
  956. // return:
  957. // 0 ok
  958. // 1 failed
  959. static int
  960. eventdns_request_transmit(struct request *req) {
  961. int retcode = 0, r;
  962. // if we fail to send this packet then this flag marks it
  963. // for eventdns_transmit
  964. req->transmit_me = 1;
  965. if (req->trans_id == 0xffff) abort();
  966. if (req->ns->choaked) {
  967. // don't bother trying to write to a socket
  968. // which we have had EAGAIN from
  969. return 1;
  970. }
  971. r = eventdns_request_transmit_to(req, req->ns);
  972. switch (r) {
  973. case 1:
  974. // temp failure
  975. req->ns->choaked = 1;
  976. nameserver_write_waiting(req->ns, 1);
  977. return 1;
  978. case 2:
  979. // failed in some other way
  980. retcode = 1;
  981. // fall through
  982. default:
  983. // all ok
  984. log("Setting timeout for request %lx", (unsigned long) req);
  985. evtimer_set(&req->timeout_event, eventdns_request_timeout_callback, req);
  986. evtimer_add(&req->timeout_event, &global_timeout);
  987. req->tx_count++;
  988. req->transmit_me = 0;
  989. return retcode;
  990. }
  991. }
  992. static void
  993. nameserver_probe_callback(int result, char type, int count, int ttl, void *addresses, void *arg) {
  994. struct nameserver *const ns = (struct nameserver *) arg;
  995. (void) type;
  996. (void) count;
  997. (void) ttl;
  998. (void) addresses;
  999. if (result == DNS_ERR_NONE || result == DNS_ERR_NOTEXIST) {
  1000. // this is a good reply
  1001. nameserver_up(ns);
  1002. } else nameserver_probe_failed(ns);
  1003. }
  1004. static void
  1005. nameserver_send_probe(struct nameserver *const ns) {
  1006. struct request *req;
  1007. // here we need to send a probe to a given nameserver
  1008. // in the hope that it is up now.
  1009. log("Sending probe to %s", debug_ntoa(ns->address));
  1010. req = request_new("www.google.com", DNS_QUERY_NO_SEARCH, nameserver_probe_callback, ns);
  1011. if (!req) return;
  1012. // we force this into the inflight queue no matter what
  1013. request_trans_id_set(req, transaction_id_pick());
  1014. req->ns = ns;
  1015. request_submit(req);
  1016. }
  1017. // returns:
  1018. // 0 didn't try to transmit anything
  1019. // 1 tried to transmit something
  1020. static int
  1021. eventdns_transmit(void) {
  1022. char did_try_to_transmit = 0;
  1023. if (req_head) {
  1024. struct request *const started_at = req_head, *req = req_head;
  1025. // first transmit all the requests which are currently waiting
  1026. do {
  1027. if (req->transmit_me) {
  1028. did_try_to_transmit = 1;
  1029. eventdns_request_transmit(req);
  1030. }
  1031. req = req->next;
  1032. } while (req != started_at);
  1033. }
  1034. return did_try_to_transmit;
  1035. }
  1036. // exported function
  1037. int
  1038. eventdns_nameserver_add(unsigned long int address) {
  1039. // first check to see if we already have this nameserver
  1040. const struct nameserver *server = server_head, *const started_at = server_head;
  1041. struct nameserver *ns;
  1042. struct sockaddr_in sin;
  1043. int err = 0;
  1044. if (server) {
  1045. do {
  1046. if (server->address == address) return 3;
  1047. server = server->next;
  1048. } while (server != started_at);
  1049. }
  1050. ns = (struct nameserver *) malloc(sizeof(struct nameserver));
  1051. if (!ns) return -1;
  1052. memset(ns, 0, sizeof(struct nameserver));
  1053. ns->socket = socket(PF_INET, SOCK_DGRAM, 0);
  1054. if (ns->socket < 0) { err = 1; goto out1; }
  1055. #ifdef MS_WINDOWS
  1056. {
  1057. u_long nonblocking = 1;
  1058. ioctlsocket(ns->socket, FIONBIO, &nonblocking);
  1059. }
  1060. #else
  1061. fcntl(ns->socket, F_SETFL, O_NONBLOCK);
  1062. #endif
  1063. sin.sin_addr.s_addr = address;
  1064. sin.sin_port = htons(53);
  1065. sin.sin_family = AF_INET;
  1066. if (connect(ns->socket, (struct sockaddr *) &sin, sizeof(sin)) != 0) {
  1067. err = 2;
  1068. goto out2;
  1069. }
  1070. ns->address = address;
  1071. ns->state = 1;
  1072. event_set(&ns->event, ns->socket, EV_READ | EV_PERSIST, nameserver_ready_callback, ns);
  1073. event_add(&ns->event, NULL);
  1074. // insert this nameserver into the list of them
  1075. if (!server_head) {
  1076. ns->next = ns->prev = ns;
  1077. server_head = ns;
  1078. } else {
  1079. ns->next = server_head->next;
  1080. ns->prev = server_head;
  1081. server_head->next = ns;
  1082. if (server_head->prev == server_head) {
  1083. server_head->prev = ns;
  1084. }
  1085. }
  1086. global_good_nameservers++;
  1087. return 0;
  1088. out2:
  1089. #ifdef MS_WINDOWS
  1090. closesocket(ns->socket);
  1091. #else
  1092. close(ns->socket);
  1093. #endif
  1094. out1:
  1095. free(ns);
  1096. return err;
  1097. }
  1098. // exported function
  1099. int
  1100. eventdns_nameserver_ip_add(const char *ip_as_string) {
  1101. struct in_addr ina;
  1102. if (!inet_aton(ip_as_string, &ina)) return 4;
  1103. return eventdns_nameserver_add(ina.s_addr);
  1104. }
  1105. // insert into the tail of the queue
  1106. static void
  1107. eventdns_request_insert(struct request *req, struct request **head) {
  1108. if (!*head) {
  1109. *head = req;
  1110. req->next = req->prev = req;
  1111. return;
  1112. }
  1113. req->prev = (*head)->prev;
  1114. req->prev->next = req;
  1115. req->next = *head;
  1116. (*head)->prev = req;
  1117. }
  1118. static int
  1119. string_num_dots(const char *s) {
  1120. int count = 0;
  1121. while ((s = strchr(s, '.'))) {
  1122. s++;
  1123. count++;
  1124. }
  1125. return count;
  1126. }
  1127. static struct request *
  1128. request_new(const char *name, int flags, eventdns_callback_type callback, void *ptr) {
  1129. const char issuing_now = (global_requests_inflight < global_max_requests_inflight) ? 1 : 0;
  1130. const int name_len = strlen(name);
  1131. const int request_max_len = eventdns_request_len(name_len);
  1132. const u16 trans_id = issuing_now ? transaction_id_pick() : 0xffff;
  1133. // the request data is alloced in a single block with the header
  1134. struct request *const req = (struct request *) malloc(sizeof(struct request) + request_max_len);
  1135. int rlen;
  1136. (void) flags;
  1137. if (!req) return NULL;
  1138. memset(req, 0, sizeof(struct request));
  1139. // request data lives just after the header
  1140. req->request = ((u8 *) req) + sizeof(struct request);
  1141. req->request_appended = 1; // denotes that the request data shouldn't be free()ed
  1142. rlen = eventdns_request_data_build(name, name_len, trans_id, TYPE_A, CLASS_INET, req->request);
  1143. if (rlen < 0) goto err1;
  1144. req->request_len = rlen;
  1145. req->trans_id = trans_id;
  1146. req->tx_count = 0;
  1147. req->user_pointer = ptr;
  1148. req->user_callback = callback;
  1149. req->ns = issuing_now ? nameserver_pick() : NULL;
  1150. req->next = req->prev = NULL;
  1151. return req;
  1152. err1:
  1153. free(req->request);
  1154. return NULL;
  1155. }
  1156. static void
  1157. request_submit(struct request *const req) {
  1158. if (req->ns) {
  1159. // if it has a nameserver assigned then this is going
  1160. // straight into the inflight queue
  1161. eventdns_request_insert(req, &req_head);
  1162. global_requests_inflight++;
  1163. eventdns_request_transmit(req);
  1164. } else {
  1165. eventdns_request_insert(req, &req_waiting_head);
  1166. global_requests_waiting++;
  1167. }
  1168. }
  1169. // exported function
  1170. int eventdns_resolve(const char *name, int flags, eventdns_callback_type callback, void *ptr) {
  1171. log("Resolve requested for %s", name);
  1172. if (flags & DNS_QUERY_NO_SEARCH) {
  1173. struct request *const req = request_new(name, flags, callback, ptr);
  1174. if (!req) return 1;
  1175. request_submit(req);
  1176. return 0;
  1177. } else {
  1178. return search_request_new(name, flags, callback, ptr);
  1179. }
  1180. }
  1181. /////////////////////////////////////////////////////////////////////
  1182. // Search support
  1183. //
  1184. // the libc resolver has support for searching a number of domains
  1185. // to find a name. If nothing else then it takes the single domain
  1186. // from the gethostname() call.
  1187. //
  1188. // It can also be configured via the domain and search options in a
  1189. // resolv.conf.
  1190. //
  1191. // The ndots option controls how many dots it takes for the resolver
  1192. // to decide that a name is non-local and so try a raw lookup first.
  1193. struct search_domain {
  1194. int len;
  1195. struct search_domain *next;
  1196. // the text string is appended to this structure
  1197. };
  1198. struct search_state {
  1199. int refcount;
  1200. int ndots;
  1201. int num_domains;
  1202. struct search_domain *head;
  1203. };
  1204. static struct search_state *global_search_state = NULL;
  1205. static void
  1206. search_state_decref(struct search_state *const state) {
  1207. if (!state) return;
  1208. state->refcount--;
  1209. if (!state->refcount) {
  1210. struct search_domain *next, *dom;
  1211. for (dom = state->head; dom; dom = next) {
  1212. next = dom->next;
  1213. free(dom);
  1214. }
  1215. free(state);
  1216. }
  1217. };
  1218. static struct search_state *
  1219. search_state_new(void) {
  1220. struct search_state *state = (struct search_state *) malloc(sizeof(struct search_state));
  1221. if (!state) return NULL;
  1222. memset(state, 0, sizeof(struct search_state));
  1223. state->refcount = 1;
  1224. state->ndots = 1;
  1225. return state;
  1226. }
  1227. static void
  1228. search_postfix_clear(void) {
  1229. search_state_decref(global_search_state);
  1230. global_search_state = search_state_new();
  1231. }
  1232. // exported function
  1233. void
  1234. eventdns_search_clear(void) {
  1235. search_postfix_clear();
  1236. }
  1237. static void
  1238. search_postfix_add(const char *domain) {
  1239. int domain_len;
  1240. struct search_domain *sdomain;
  1241. while (domain[0] == '.') domain++;
  1242. domain_len = strlen(domain);
  1243. if (!global_search_state) global_search_state = search_state_new();
  1244. if (!global_search_state) return;
  1245. global_search_state->num_domains++;
  1246. sdomain = (struct search_domain *) malloc(sizeof(struct search_domain) + domain_len);
  1247. if (!sdomain) return;
  1248. memcpy( ((u8 *) sdomain) + sizeof(struct search_domain), domain, domain_len);
  1249. sdomain->next = global_search_state->head;
  1250. sdomain->len = domain_len;
  1251. global_search_state->head = sdomain;
  1252. }
  1253. // reverse the order of members in the postfix list. This is needed because,
  1254. // when parsing resolv.conf we push elements in the wrong order
  1255. static void
  1256. search_reverse(void) {
  1257. struct search_domain *cur, *prev = NULL, *next;
  1258. cur = global_search_state->head;
  1259. while (cur) {
  1260. next = cur->next;
  1261. cur->next = prev;
  1262. prev = cur;
  1263. cur = next;
  1264. }
  1265. global_search_state->head = prev;
  1266. }
  1267. // exported function
  1268. void
  1269. eventdns_search_add(const char *domain) {
  1270. search_postfix_add(domain);
  1271. }
  1272. // exported function
  1273. void
  1274. eventdns_search_ndots_set(const int ndots) {
  1275. if (!global_search_state) global_search_state = search_state_new();
  1276. if (!global_search_state) return;
  1277. global_search_state->ndots = ndots;
  1278. }
  1279. static void
  1280. search_set_from_hostname(void) {
  1281. char hostname[HOST_NAME_MAX + 1], *domainname;
  1282. search_postfix_clear();
  1283. if (gethostname(hostname, sizeof(hostname))) return;
  1284. domainname = strchr(hostname, '.');
  1285. if (!domainname) return;
  1286. search_postfix_add(domainname);
  1287. }
  1288. // warning: returns malloced string
  1289. static char *
  1290. search_make_new(const struct search_state *const state, int n, const char *const base_name) {
  1291. const int base_len = strlen(base_name);
  1292. const char need_to_append_dot = base_name[base_len - 1] == '.' ? 0 : 1;
  1293. struct search_domain *dom;
  1294. for (dom = state->head; dom; dom = dom->next) {
  1295. if (!n--) {
  1296. // this is the postfix we want
  1297. // the actual postfix string is kept at the end of the structure
  1298. const u8 *const postfix = ((u8 *) dom) + sizeof(struct search_domain);
  1299. const int postfix_len = dom->len;
  1300. char *const newname = (char *) malloc(base_len + need_to_append_dot + postfix_len + 1);
  1301. if (!newname) return NULL;
  1302. memcpy(newname, base_name, base_len);
  1303. if (need_to_append_dot) newname[base_len] = '.';
  1304. memcpy(newname + base_len + need_to_append_dot, postfix, postfix_len);
  1305. newname[base_len + need_to_append_dot + postfix_len] = 0;
  1306. return newname;
  1307. }
  1308. }
  1309. // we ran off the end of the list and still didn't find the requested string
  1310. abort();
  1311. }
  1312. static int
  1313. search_request_new(const char *const name, int flags, eventdns_callback_type user_callback, void *user_arg) {
  1314. if ( ((flags & DNS_QUERY_NO_SEARCH) == 0) &&
  1315. global_search_state &&
  1316. global_search_state->num_domains) {
  1317. // we have some domains to search
  1318. struct request *req;
  1319. if (string_num_dots(name) >= global_search_state->ndots) {
  1320. req = request_new(name, flags, user_callback, user_arg);
  1321. if (!req) return 1;
  1322. req->search_index = -1;
  1323. } else {
  1324. char *const new_name = search_make_new(global_search_state, 0, name);
  1325. if (!new_name) return 1;
  1326. req = request_new(new_name, flags, user_callback, user_arg);
  1327. free(new_name);
  1328. if (!req) return 1;
  1329. req->search_index = 0;
  1330. }
  1331. req->search_origname = strdup(name);
  1332. req->search_state = global_search_state;
  1333. req->search_flags = flags;
  1334. global_search_state->refcount++;
  1335. request_submit(req);
  1336. return 0;
  1337. } else {
  1338. struct request *const req = request_new(name, flags, user_callback, user_arg);
  1339. if (!req) return 1;
  1340. request_submit(req);
  1341. return 0;
  1342. }
  1343. }
  1344. // this is called when a request has failed to find a name. We need to check
  1345. // if it is part of a search and, if so, try the next name in the list
  1346. // returns:
  1347. // 0 another request has been submitted
  1348. // 1 no more requests needed
  1349. static int
  1350. search_try_next(struct request *const req) {
  1351. if (req->search_state) {
  1352. // it is part of a search
  1353. char *new_name;
  1354. struct request *newreq;
  1355. req->search_index++;
  1356. if (req->search_index >= req->search_state->num_domains) {
  1357. // no more postfixes to try, however we may need to try
  1358. // this name without a postfix
  1359. if (string_num_dots(req->search_origname) < req->search_state->ndots) {
  1360. // yep, we need to try it raw
  1361. struct request *const newreq = request_new(req->search_origname, req->search_flags, req->user_callback, req->user_pointer);
  1362. log("Search: trying raw query %s", req->search_origname);
  1363. if (newreq) {
  1364. request_submit(newreq);
  1365. return 0;
  1366. }
  1367. }
  1368. return 1;
  1369. }
  1370. new_name = search_make_new(req->search_state, req->search_index, req->search_origname);
  1371. if (!new_name) return 1;
  1372. log("Search: now trying %s (%d)", new_name, req->search_index);
  1373. newreq = request_new(new_name, req->search_flags, req->user_callback, req->user_pointer);
  1374. free(new_name);
  1375. if (!newreq) return 1;
  1376. newreq->search_origname = req->search_origname;
  1377. req->search_origname = NULL;
  1378. newreq->search_state = req->search_state;
  1379. newreq->search_flags = req->search_flags;
  1380. newreq->search_index = req->search_index;
  1381. newreq->search_state->refcount++;
  1382. request_submit(newreq);
  1383. return 0;
  1384. }
  1385. return 1;
  1386. }
  1387. static void
  1388. search_request_finished(struct request *const req) {
  1389. if (req->search_state) {
  1390. search_state_decref(req->search_state);
  1391. req->search_state = NULL;
  1392. }
  1393. if (req->search_origname) {
  1394. free(req->search_origname);
  1395. req->search_origname = NULL;
  1396. }
  1397. }
  1398. /////////////////////////////////////////////////////////////////////
  1399. // Parsing resolv.conf files
  1400. static void
  1401. eventdns_resolv_set_defaults(int flags) {
  1402. // if the file isn't found then we assume a local resolver
  1403. if (flags & DNS_OPTION_SEARCH) search_set_from_hostname();
  1404. if (flags & DNS_OPTION_NAMESERVERS) eventdns_nameserver_ip_add("127.0.0.1");
  1405. }
  1406. #ifndef HAVE_STRTOK_R
  1407. static char *
  1408. strtok_r(char *s, const char *delim, char **state) {
  1409. return strtok(s, delim);
  1410. }
  1411. #endif
  1412. // helper version of atoi which returns -1 on error
  1413. static int
  1414. strtoint(const char *const str) {
  1415. char *endptr;
  1416. const int r = strtol(str, &endptr, 10);
  1417. if (*endptr) return -1;
  1418. return r;
  1419. }
  1420. static void
  1421. resolv_conf_parse_line(char *const start, int flags) {
  1422. char *strtok_state;
  1423. static const char *const delims = " \t";
  1424. #define NEXT_TOKEN strtok_r(NULL, delims, &strtok_state)
  1425. char *const first_token = strtok_r(start, delims, &strtok_state);
  1426. if (!first_token) return;
  1427. if (!strcmp(first_token, "nameserver")) {
  1428. const char *const nameserver = NEXT_TOKEN;
  1429. struct in_addr ina;
  1430. if (inet_aton(nameserver, &ina)) {
  1431. // address is valid
  1432. eventdns_nameserver_add(ina.s_addr);
  1433. }
  1434. } else if (!strcmp(first_token, "domain") && (flags & DNS_OPTION_SEARCH)) {
  1435. const char *const domain = NEXT_TOKEN;
  1436. if (domain) {
  1437. search_postfix_clear();
  1438. search_postfix_add(domain);
  1439. }
  1440. } else if (!strcmp(first_token, "search") && (flags & DNS_OPTION_SEARCH)) {
  1441. const char *domain;
  1442. search_postfix_clear();
  1443. while ((domain = NEXT_TOKEN)) {
  1444. search_postfix_add(domain);
  1445. }
  1446. search_reverse();
  1447. } else if (!strcmp(first_token, "options")) {
  1448. const char *option;
  1449. while ((option = NEXT_TOKEN)) {
  1450. if (!strncmp(option, "ndots:", 6)) {
  1451. const int ndots = strtoint(&option[6]);
  1452. if (ndots == -1) continue;
  1453. if (!(flags & DNS_OPTION_SEARCH)) continue;
  1454. log("Setting ndots to %d", ndots);
  1455. if (!global_search_state) global_search_state = search_state_new();
  1456. if (!global_search_state) return;
  1457. global_search_state->ndots = ndots;
  1458. } else if (!strncmp(option, "timeout:", 8)) {
  1459. const int timeout = strtoint(&option[8]);
  1460. if (timeout == -1) continue;
  1461. if (!(flags & DNS_OPTION_MISC)) continue;
  1462. log("Setting timeout to %d", timeout);
  1463. global_timeout.tv_sec = timeout;
  1464. } else if (!strncmp(option, "attempts:", 9)) {
  1465. const int retries = strtoint(&option[9]);
  1466. if (retries == -1) continue;
  1467. if (!(flags & DNS_OPTION_MISC)) continue;
  1468. log("Setting retries to %d", retries);
  1469. global_max_retransmits = retries;
  1470. }
  1471. }
  1472. }
  1473. #undef NEXT_TOKEN
  1474. }
  1475. // exported function
  1476. // returns:
  1477. // 0 no errors
  1478. // 1 failed to open file
  1479. // 2 failed to stat file
  1480. // 3 file too large
  1481. // 4 out of memory
  1482. // 5 short read from file
  1483. int
  1484. eventdns_resolv_conf_parse(int flags, const char *const filename) {
  1485. struct stat st;
  1486. int fd;
  1487. u8 *resolv;
  1488. char *start;
  1489. int err = 0;
  1490. log("Parsing resolve.conf file %s", filename);
  1491. fd = open(filename, O_RDONLY);
  1492. if (fd < 0) {
  1493. eventdns_resolv_set_defaults(flags);
  1494. return 0;
  1495. }
  1496. if (fstat(fd, &st)) { err = 2; goto out1; }
  1497. if (!st.st_size) {
  1498. eventdns_resolv_set_defaults(flags);
  1499. err = 0;
  1500. goto out1;
  1501. }
  1502. if (st.st_size > 65535) { err = 3; goto out1; } // no resolv.conf should be any bigger
  1503. resolv = (u8 *) malloc(st.st_size + 1);
  1504. if (!resolv) { err = 4; goto out1; }
  1505. if (read(fd, resolv, st.st_size) != st.st_size) { err = 5; goto out2; }
  1506. resolv[st.st_size] = 0; // we malloced an extra byte
  1507. start = (char *) resolv;
  1508. for (;;) {
  1509. char *const newline = strchr(start, '\n');
  1510. if (!newline) {
  1511. resolv_conf_parse_line(start, flags);
  1512. break;
  1513. } else {
  1514. *newline = 0;
  1515. resolv_conf_parse_line(start, flags);
  1516. start = newline + 1;
  1517. }
  1518. }
  1519. if (!server_head && (flags & DNS_OPTION_NAMESERVERS)) {
  1520. // no nameservers were configured.
  1521. eventdns_nameserver_ip_add("127.0.0.1");
  1522. }
  1523. if (flags & DNS_OPTION_SEARCH && (!global_search_state || global_search_state->num_domains == 0)) {
  1524. search_set_from_hostname();
  1525. }
  1526. out2:
  1527. free(resolv);
  1528. out1:
  1529. close(fd);
  1530. return err;
  1531. }
  1532. #ifdef MS_WINDOWS
  1533. // Add multiple nameservers from a space-or-comma-separated list.
  1534. static int
  1535. eventdns_nameserver_ip_add_line(const char *ips) {
  1536. const char *addr;
  1537. char *buf;
  1538. int r;
  1539. while (*ips) {
  1540. while (ISSPACE(*ips) || *ips == ',' || *ips == '\t')
  1541. ++ips;
  1542. addr = ips;
  1543. while (ISDIGIT(*ips) || *ips == '.')
  1544. ++ips;
  1545. buf = malloc(ips-addr+1);
  1546. if (!buf) return 4;
  1547. memcpy(buf, addr, ips-addr);
  1548. buf[ips-addr] = '\0';
  1549. r = eventdns_nameserver_ip_add(buf);
  1550. free(buf);
  1551. if (r) return r;
  1552. }
  1553. return 0;
  1554. }
  1555. // Use the windows GetNetworkParams interface in iphlpapi.dll to
  1556. // figure out what our nameservers are.
  1557. static int
  1558. load_nameservers_with_getnetworkparams(void)
  1559. {
  1560. // Based on MSDN examples and inspection of c-ares code.
  1561. FIXED_INFO *fixed;
  1562. HMODULE handle = 0;
  1563. ULONG size = sizeof(FIXED_INFO);
  1564. void *buf = NULL;
  1565. int status = 0, r, added_any;
  1566. IP_ADDR_STRING *ns;
  1567. DWORD (WINAPI *fn)(FIXED_INFO*, DWORD*);
  1568. if (!(handle = LoadLibrary("iphlpapi.dll")))
  1569. goto done;
  1570. if (!(fn = GetProcAddress(handle, "GetNetworkParams")))
  1571. goto done;
  1572. buf = malloc(size);
  1573. if (!buf) { status = 4; goto done; }
  1574. fixed = buf;
  1575. r = fn(fixed, &size);
  1576. if (r != ERROR_SUCCESS && r != ERROR_BUFFER_OVERFLOW) {
  1577. status = -1;
  1578. goto done;
  1579. }
  1580. if (r != ERROR_SUCCESS) {
  1581. free(buf);
  1582. buf = malloc(size);
  1583. if (!buf) { status = 4; goto done; }
  1584. fixed = buf;
  1585. r = fn(fixed, &size);
  1586. if (r != ERROR_SUCCESS) { status = -1; goto done; }
  1587. }
  1588. assert(fixed);
  1589. added_any = 0;
  1590. ns = fixed->DnsServerList;
  1591. while (ns) {
  1592. r = eventdns_nameserver_ip_add_line(ns->IpAddress.String);
  1593. if (r) { status = r; goto done; }
  1594. added_any = 0;
  1595. ns = ns->next;
  1596. }
  1597. if (!added_any)
  1598. status = -1;
  1599. done:
  1600. if (buf)
  1601. free(buf);
  1602. if (handle)
  1603. FreeLibrary(handle);
  1604. return status;
  1605. }
  1606. static int
  1607. config_nameserver_from_reg_key(HKEY key, const char *subkey)
  1608. {
  1609. char *buf;
  1610. DWORD bufsz = 0, type = 0;
  1611. int status = 0;
  1612. if (RegQueryValueEx(key, subkey, 0, &type, NULL, &bufsz)
  1613. != ERROR_MORE_DATA)
  1614. return -1;
  1615. if (!(buf = malloc(bufsz)))
  1616. return -1;
  1617. if (RegQueryValueEx(key, subkey, 0, &type, (LPBYTE)buf, &bufsz)
  1618. == ERROR_SUCCESS && bufsz > 1) {
  1619. status = eventdns_nameserver_ip_add_line(buf);
  1620. }
  1621. free(buf);
  1622. return status;
  1623. }
  1624. static int
  1625. load_nameservers_from_registry(void)
  1626. {
  1627. int found = 0;
  1628. #define TRY(k, name) \
  1629. if (!found && config_nameserver_from_reg_key(k,name) == 0) { \
  1630. log("Found nameservers in %s/%s",#k,name); \
  1631. found = 1; \
  1632. }
  1633. if (IS_NT()) {
  1634. HKEY nt_key = 0, interfaces_key = 0;
  1635. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, WIN_NS_NT_KEY, 0,
  1636. KEY_READ, &nt_key) != ERROR_SUCCESS)
  1637. return -1;
  1638. RegOpenKeyEx(nt_key, "Interfaces", 0,
  1639. KEY_QUERY_VALUE|KEY_ENUMERATE_SUBKEYS,
  1640. &interfaces_key);
  1641. TRY(nt_key, NAMESERVER);
  1642. TRY(nt_key, DHCPNAMESERVER);
  1643. TRY(interfaces_key, NAMESERVER);
  1644. TRY(interfaces_key, DHCPNAMESERVER);
  1645. RegCloseKey(interfaces_key);
  1646. RegCloseKey(nt_key);
  1647. } else {
  1648. HKEY win_key = 0;
  1649. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, WIN_NS_9X, 0,
  1650. KEY_READ, &win_key) != ERROR_SUCCESS)
  1651. return -1;
  1652. TRY(win_key, NAMESERVER);
  1653. RegCloseKey(win_key);
  1654. }
  1655. return found ? 0 : -1;
  1656. #undef TRY
  1657. }
  1658. int
  1659. eventdns_config_windows_nameservers(void)
  1660. {
  1661. if (load_nameservers_with_getnetworkparams() == 0)
  1662. return 0;
  1663. return load_nameservers_from_registry();
  1664. }
  1665. #endif