eventdns.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953
  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. #define CLOSE_SOCKET(x) closesocket(x)
  363. #else
  364. #define last_error(sock) (errno)
  365. #define error_is_eagain(err) ((err) == EAGAIN)
  366. #define CLOSE_SOCKET(x) close(x)
  367. #endif
  368. #define ISSPACE(c) isspace((int)(unsigned char)(c))
  369. #define ISDIGIT(c) isdigit((int)(unsigned char)(c))
  370. #ifndef NDEBUG
  371. static const char *
  372. debug_ntoa(u32 address)
  373. {
  374. static char buf[32];
  375. u32 a = ntohl(address);
  376. sprintf(buf, "%d.%d.%d.%d",
  377. (int)(u8)((a>>24)&0xff),
  378. (int)(u8)((a>>16)&0xff),
  379. (int)(u8)((a>>8 )&0xff),
  380. (int)(u8)((a )&0xff));
  381. return buf;
  382. }
  383. #endif
  384. static eventdns_debug_log_fn_type eventdns_log_fn = NULL;
  385. void
  386. eventdns_set_log_fn(eventdns_debug_log_fn_type fn)
  387. {
  388. eventdns_log_fn = fn;
  389. }
  390. #ifdef __GNUC__
  391. #define EVENTDNS_LOG_CHECK __attribute__ ((format(printf, 1, 2)))
  392. #else
  393. #define EVENTDNS_LOG_CHECK
  394. #endif
  395. static void _eventdns_log(const char *fmt, ...) EVENTDNS_LOG_CHECK;
  396. static void
  397. _eventdns_log(const char *fmt, ...)
  398. {
  399. va_list args;
  400. static char buf[512];
  401. if (!eventdns_log_fn)
  402. return;
  403. va_start(args,fmt);
  404. #ifdef MS_WINDOWS
  405. _vsnprintf(buf, sizeof(buf), fmt, args);
  406. #else
  407. vsnprintf(buf, sizeof(buf), fmt, args);
  408. #endif
  409. buf[sizeof(buf)-1] = '\0';
  410. eventdns_log_fn(buf);
  411. va_end(args);
  412. }
  413. #define log _eventdns_log
  414. // This walks the list of inflight requests to find the
  415. // one with a matching transaction id. Returns NULL on
  416. // failure
  417. static struct request *
  418. request_find_from_trans_id(u16 trans_id) {
  419. struct request *req = req_head, *const started_at = req_head;
  420. if (req) {
  421. do {
  422. if (req->trans_id == trans_id) return req;
  423. req = req->next;
  424. } while (req != started_at);
  425. }
  426. return NULL;
  427. }
  428. // a libevent callback function which is called when a nameserver
  429. // has gone down and we want to test if it has came back to life yet
  430. static void
  431. nameserver_prod_callback(int fd, short events, void *arg) {
  432. struct nameserver *const ns = (struct nameserver *) arg;
  433. (void)fd;
  434. (void)events;
  435. nameserver_send_probe(ns);
  436. }
  437. // a libevent callback which is called when a nameserver probe (to see if
  438. // it has come back to life) times out. We increment the count of failed_times
  439. // and wait longer to send the next probe packet.
  440. static void
  441. nameserver_probe_failed(struct nameserver *const ns) {
  442. const struct timeval * timeout;
  443. assert(ns->state == 0);
  444. evtimer_del(&ns->timeout_event);
  445. timeout =
  446. &global_nameserver_timeouts[MIN(ns->failed_times,
  447. global_nameserver_timeouts_length - 1)];
  448. ns->failed_times++;
  449. evtimer_set(&ns->timeout_event, nameserver_prod_callback, ns);
  450. evtimer_add(&ns->timeout_event, (struct timeval *) timeout);
  451. }
  452. // called when a nameserver has been deemed to have failed. For example, too
  453. // many packets have timed out etc
  454. static void
  455. nameserver_failed(struct nameserver *const ns, const char *msg) {
  456. struct request *req, *started_at;
  457. // if this nameserver has already been marked as failed
  458. // then don't do anything
  459. if (!ns->state) return;
  460. log("Nameserver %s has failed: %s", debug_ntoa(ns->address), msg);
  461. global_good_nameservers--;
  462. assert(global_good_nameservers >= 0);
  463. if (global_good_nameservers == 0) {
  464. log("All nameservers have failed");
  465. }
  466. ns->state = 0;
  467. ns->failed_times = 1;
  468. evtimer_set(&ns->timeout_event, nameserver_prod_callback, ns);
  469. evtimer_add(&ns->timeout_event, (struct timeval *) &global_nameserver_timeouts[0]);
  470. // walk the list of inflight requests to see if any can be reassigned to
  471. // a different server. Requests in the waiting queue don't have a
  472. // nameserver assigned yet
  473. // if we don't have *any* good nameservers then there's no point
  474. // trying to reassign requests to one
  475. if (!global_good_nameservers) return;
  476. req = req_head;
  477. started_at = req_head;
  478. if (req) {
  479. do {
  480. if (req->tx_count == 0 && req->ns == ns) {
  481. // still waiting to go out, can be moved
  482. // to another server
  483. req->ns = nameserver_pick();
  484. }
  485. req = req->next;
  486. } while (req != started_at);
  487. }
  488. }
  489. static void
  490. nameserver_up(struct nameserver *const ns) {
  491. if (ns->state) return;
  492. log("Nameserver %s is back up", debug_ntoa(ns->address));
  493. evtimer_del(&ns->timeout_event);
  494. ns->state = 1;
  495. ns->failed_times = 0;
  496. global_good_nameservers++;
  497. }
  498. static void
  499. request_trans_id_set(struct request *const req, const u16 trans_id) {
  500. req->trans_id = trans_id;
  501. *((u16 *) req->request) = htons(trans_id);
  502. }
  503. // Called to remove a request from a list and dealloc it.
  504. // head is a pointer to the head of the list it should be
  505. // removed from or NULL if the request isn't in a list.
  506. static void
  507. request_finished(struct request *const req, struct request **head) {
  508. if (head) {
  509. if (req->next == req) {
  510. // only item in the list
  511. *head = NULL;
  512. } else {
  513. req->next->prev = req->prev;
  514. req->prev->next = req->next;
  515. if (*head == req) *head = req->next;
  516. }
  517. }
  518. log("Removing timeout for request %lx", (unsigned long) req);
  519. evtimer_del(&req->timeout_event);
  520. search_request_finished(req);
  521. global_requests_inflight--;
  522. if (!req->request_appended) {
  523. // need to free the request data on it's own
  524. free(req->request);
  525. } else {
  526. // the request data is appended onto the header
  527. // so everything gets free()ed when we:
  528. }
  529. free(req);
  530. eventdns_requests_pump_waiting_queue();
  531. }
  532. // This is called when a server returns a funny error code.
  533. // We try the request again with another server.
  534. //
  535. // return:
  536. // 0 ok
  537. // 1 failed/reissue is pointless
  538. static int
  539. request_reissue(struct request *req) {
  540. const struct nameserver *const last_ns = req->ns;
  541. // the last nameserver should have been marked as failing
  542. // by the caller of this function, therefore pick will try
  543. // not to return it
  544. req->ns = nameserver_pick();
  545. if (req->ns == last_ns) {
  546. // ... but pick did return it
  547. // not a lot of point in trying again with the
  548. // same server
  549. return 1;
  550. }
  551. req->reissue_count++;
  552. req->tx_count = 0;
  553. req->transmit_me = 1;
  554. return 0;
  555. }
  556. // this function looks for space on the inflight queue and promotes
  557. // requests from the waiting queue if it can.
  558. static void
  559. eventdns_requests_pump_waiting_queue(void) {
  560. while (global_requests_inflight < global_max_requests_inflight &&
  561. global_requests_waiting) {
  562. struct request *req;
  563. // move a request from the waiting queue to the inflight queue
  564. assert(req_waiting_head);
  565. if (req_waiting_head->next == req_waiting_head) {
  566. // only one item in the queue
  567. req = req_waiting_head;
  568. req_waiting_head = NULL;
  569. } else {
  570. req = req_waiting_head;
  571. req->next->prev = req->prev;
  572. req->prev->next = req->next;
  573. req_waiting_head = req->next;
  574. }
  575. global_requests_waiting--;
  576. global_requests_inflight++;
  577. req->ns = nameserver_pick();
  578. request_trans_id_set(req, transaction_id_pick());
  579. eventdns_request_insert(req, &req_head);
  580. eventdns_request_transmit(req);
  581. eventdns_transmit();
  582. }
  583. }
  584. // this processes a parsed reply packet
  585. static void
  586. reply_handle(u16 trans_id, u16 flags, u32 ttl, u32 addrcount, u32 *addresses) {
  587. int error;
  588. static const int error_codes[] = {DNS_ERR_FORMAT, DNS_ERR_SERVERFAILED, DNS_ERR_NOTEXIST, DNS_ERR_NOTIMPL, DNS_ERR_REFUSED};
  589. struct request *const req = request_find_from_trans_id(trans_id);
  590. if (!req) return;
  591. if (flags & 0x020f || !addrcount) {
  592. // there was an error
  593. if (flags & 0x0200) {
  594. error = DNS_ERR_TRUNCATED;
  595. } else {
  596. u16 error_code = (flags & 0x000f) - 1;
  597. if (error_code > 4) {
  598. error = DNS_ERR_UNKNOWN;
  599. } else {
  600. error = error_codes[error_code];
  601. }
  602. }
  603. switch(error) {
  604. case DNS_ERR_SERVERFAILED:
  605. case DNS_ERR_NOTIMPL:
  606. case DNS_ERR_REFUSED:
  607. // we regard these errors as marking a bad nameserver
  608. if (req->reissue_count < global_max_reissues) {
  609. char msg[64];
  610. snprintf(msg, sizeof(msg), "Bad response %d",
  611. error);
  612. nameserver_failed(req->ns, msg);
  613. if (!request_reissue(req)) return;
  614. }
  615. break;
  616. default:
  617. // we got a good reply from the nameserver
  618. nameserver_up(req->ns);
  619. }
  620. if (req->search_state) {
  621. // if we have a list of domains to search in, try the next one
  622. if (!search_try_next(req)) {
  623. // a new request was issued so this request is finished and
  624. // the user callback will be made when that request (or a
  625. // child of it) finishes.
  626. request_finished(req, &req_head);
  627. return;
  628. }
  629. }
  630. // all else failed. Pass the failure up
  631. req->user_callback(error, 0, 0, 0, NULL, req->user_pointer);
  632. request_finished(req, &req_head);
  633. } else {
  634. // all ok, tell the user
  635. req->user_callback(DNS_ERR_NONE, DNS_IPv4_A, addrcount, ttl, addresses, req->user_pointer);
  636. nameserver_up(req->ns);
  637. request_finished(req, &req_head);
  638. }
  639. }
  640. // parses a raw packet from the wire
  641. static void
  642. reply_parse(u8 *packet, int length) {
  643. int j = 0; // index into packet
  644. u16 _t; // used by the macros
  645. u32 _t32; // used by the macros
  646. #define GET32(x) do { if (j + 4 > length) return; memcpy(&_t32, packet + j, 4); j += 4; x = ntohl(_t32); } while(0);
  647. #define GET16(x) do { if (j + 2 > length) return; memcpy(&_t, packet + j, 2); j += 2; x = ntohs(_t); } while(0);
  648. #define GET8(x) do { if (j >= length) return; x = packet[j++]; } while(0);
  649. u16 trans_id, flags, questions, answers, authority, additional, datalength;
  650. u32 ttl, ttl_r = 0xffffffff;
  651. u32 addresses[MAX_ADDRS];
  652. int addresses_done = 0;
  653. uint i;
  654. GET16(trans_id);
  655. GET16(flags);
  656. GET16(questions);
  657. GET16(answers);
  658. GET16(authority);
  659. GET16(additional);
  660. if (!(flags & 0x8000)) return; // must be an answer
  661. if (flags & 0x020f) {
  662. // there was an error
  663. reply_handle(trans_id, flags, 0, 0, NULL);
  664. return;
  665. }
  666. // if (!answers) return; // must have an answer of some form
  667. // This macro skips a name in the DNS reply. Normally the
  668. // names are a series of length prefixed strings terminated with
  669. // a length of 0 (the lengths are u8's < 63).
  670. // However, the length can start with a pair of 1 bits and that
  671. // means that the next 14 bits are a pointer within the current
  672. // packet. The name stops after a pointer like that.
  673. #define SKIP_NAME \
  674. for(;;) { \
  675. u8 label_len; \
  676. GET8(label_len); \
  677. if (!label_len) break; \
  678. if (label_len & 0xc0) { \
  679. GET8(label_len); \
  680. break; \
  681. } \
  682. if (label_len > 63) return; \
  683. j += label_len; \
  684. }
  685. // skip over each question in the reply
  686. for (i = 0; i < questions; ++i) {
  687. // the question looks like
  688. // <label:name><u16:type><u16:class>
  689. SKIP_NAME;
  690. j += 4;
  691. }
  692. // now we have the answer section which looks like
  693. // <label:name><u16:type><u16:class><u32:ttl><u16:len><data...>
  694. for (i = 0; i < answers; ++i) {
  695. u16 type, class;
  696. SKIP_NAME;
  697. GET16(type);
  698. GET16(class);
  699. GET32(ttl);
  700. GET16(datalength);
  701. if (type == TYPE_A && class == CLASS_INET) {
  702. const int addrcount = datalength >> 2; // each IP address is 4 bytes
  703. // XXXX do something sane with malformed A answers.
  704. const int addrtocopy = MIN(MAX_ADDRS - addresses_done, addrcount);
  705. ttl_r = MIN(ttl_r, ttl);
  706. // we only bother with the first four addresses.
  707. if (j + 4*addrtocopy > length) return;
  708. memcpy(&addresses[addresses_done], packet + j, 4*addrtocopy);
  709. j += 4*addrtocopy;
  710. addresses_done += addrtocopy;
  711. if (addresses_done == MAX_ADDRS) break;
  712. } else {
  713. // skip over any other type of resource
  714. j += datalength;
  715. }
  716. }
  717. reply_handle(trans_id, flags, ttl_r, addresses_done, addresses);
  718. #undef SKIP_NAME
  719. #undef GET32
  720. #undef GET16
  721. #undef GET8
  722. }
  723. // Try to choose a strong transaction id which isn't already in flight
  724. static u16
  725. transaction_id_pick(void) {
  726. for (;;) {
  727. const struct request *req = req_head, *started_at;
  728. #ifdef DNS_USE_CPU_CLOCK_FOR_ID
  729. struct timespec ts;
  730. const u16 trans_id = ts.tv_nsec & 0xffff;
  731. if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts)) abort();
  732. #endif
  733. #ifdef DNS_USE_GETTIMEOFDAY_FOR_ID
  734. struct timeval tv;
  735. const u16 trans_id = tv.tv_usec & 0xffff;
  736. gettimeofday(&tv, NULL);
  737. #endif
  738. #ifdef DNS_USE_OPENSSL_FOR_ID
  739. u16 trans_id;
  740. if (RAND_pseudo_bytes((u8 *) &trans_id, 2) == -1) {
  741. /* // in the case that the RAND call fails we back
  742. // down to using gettimeofday.
  743. struct timeval tv;
  744. gettimeofday(&tv, NULL);
  745. trans_id = tv.tv_usec & 0xffff; */
  746. abort();
  747. }
  748. #endif
  749. if (trans_id == 0xffff) continue;
  750. // now check to see if that id is already inflight
  751. req = started_at = req_head;
  752. if (req) {
  753. do {
  754. if (req->trans_id == trans_id) break;
  755. req = req->next;
  756. } while (req != started_at);
  757. }
  758. // we didn't find it, so this is a good id
  759. if (req == started_at) return trans_id;
  760. }
  761. }
  762. // choose a namesever to use. This function will try to ignore
  763. // nameservers which we think are down and load balance across the rest
  764. // by updating the server_head global each time.
  765. static struct nameserver *
  766. nameserver_pick(void) {
  767. struct nameserver *started_at = server_head, *picked;
  768. if (!server_head) return NULL;
  769. // if we don't have any good nameservers then there's no
  770. // point in trying to find one.
  771. if (!global_good_nameservers) {
  772. server_head = server_head->next;
  773. return server_head;
  774. }
  775. // remember that nameservers are in a circular list
  776. for (;;) {
  777. if (server_head->state) {
  778. // we think this server is currently good
  779. picked = server_head;
  780. server_head = server_head->next;
  781. return picked;
  782. }
  783. server_head = server_head->next;
  784. if (server_head == started_at) {
  785. // all the nameservers seem to be down
  786. // so we just return this one and hope for the
  787. // best
  788. assert(global_good_nameservers == 0);
  789. picked = server_head;
  790. server_head = server_head->next;
  791. return picked;
  792. }
  793. }
  794. }
  795. // this is called when a namesever socket is ready for reading
  796. static void
  797. nameserver_read(struct nameserver *ns) {
  798. u8 packet[1500];
  799. for (;;) {
  800. const int r = recv(ns->socket, packet, sizeof(packet), 0);
  801. if (r < 0) {
  802. int err = last_error(ns_socket);
  803. if (error_is_eagain(err)) return;
  804. nameserver_failed(ns, strerror(err));
  805. return;
  806. }
  807. reply_parse(packet, r);
  808. }
  809. }
  810. // set if we are waiting for the ability to write to this server.
  811. // if waiting is true then we ask libevent for EV_WRITE events, otherwise
  812. // we stop these events.
  813. static void
  814. nameserver_write_waiting(struct nameserver *ns, char waiting) {
  815. if (ns->write_waiting == waiting) return;
  816. ns->write_waiting = waiting;
  817. event_del(&ns->event);
  818. event_set(&ns->event, ns->socket, EV_READ | (waiting ? EV_WRITE : 0) | EV_PERSIST,
  819. nameserver_ready_callback, ns);
  820. event_add(&ns->event, NULL);
  821. }
  822. // a callback function. Called by libevent when the kernel says that
  823. // a nameserver socket is ready for writing or reading
  824. static void
  825. nameserver_ready_callback(int fd, short events, void *arg) {
  826. struct nameserver *ns = (struct nameserver *) arg;
  827. (void)fd;
  828. if (events & EV_WRITE) {
  829. ns->choaked = 0;
  830. if (!eventdns_transmit()) {
  831. nameserver_write_waiting(ns, 0);
  832. }
  833. }
  834. if (events & EV_READ) {
  835. nameserver_read(ns);
  836. }
  837. }
  838. // Converts a string to a length-prefixed set of DNS labels.
  839. // @buf must be strlen(name)+2 or longer. name and buf must
  840. // not overlap. name_len should be the length of name
  841. //
  842. // Input: abc.def
  843. // Output: <3>abc<3>def<0>
  844. //
  845. // Returns the length of the data. negative on error
  846. // -1 label was > 63 bytes
  847. // -2 name was > 255 bytes
  848. static int
  849. dnsname_to_labels(u8 *const buf, const char *name, const int name_len) { \
  850. const char *end = name + name_len; \
  851. int j = 0; // current offset into buf
  852. if (name_len > 255) return -2;
  853. for (;;) {
  854. const char *const start = name;
  855. name = strchr(name, '.');
  856. if (!name) {
  857. const uint label_len = end - start;
  858. if (label_len > 63) return -1;
  859. buf[j++] = label_len;
  860. memcpy(buf + j, start, end - start);
  861. j += end - start;
  862. break;
  863. } else {
  864. // append length of the label.
  865. const uint label_len = name - start;
  866. if (label_len > 63) return -1;
  867. buf[j++] = label_len;
  868. memcpy(buf + j, start, name - start);
  869. j += name - start;
  870. // hop over the '.'
  871. name++;
  872. }
  873. }
  874. // the labels must be terminated by a 0.
  875. // It's possible that the name ended in a .
  876. // in which case the zero is already there
  877. if (!j || buf[j-1]) buf[j++] = 0;
  878. return j;
  879. }
  880. // Finds the length of a dns request for a DNS name of the given
  881. // length. The actual request may be smaller than the value returned
  882. // here
  883. static int
  884. eventdns_request_len(const int name_len) {
  885. return 96 + // length of the DNS standard header
  886. name_len + 2 +
  887. 4; // space for the resource type
  888. }
  889. // build a dns request packet into buf. buf should be at least as long
  890. // as eventdns_request_len told you it should be.
  891. //
  892. // Returns the amount of space used. Negative on error.
  893. static int
  894. eventdns_request_data_build(const char *const name, const int name_len, const u16 trans_id,
  895. const u16 type, const u16 class,
  896. u8 *const buf) {
  897. int j = 0; // current offset into buf
  898. u16 _t; // used by the macros
  899. u8 *labels;
  900. int labels_len;
  901. #define APPEND16(x) do { _t = htons(x); memcpy(buf + j, &_t, 2); j += 2; } while(0);
  902. APPEND16(trans_id);
  903. APPEND16(0x0100); // standard query, recusion needed
  904. APPEND16(1); // one question
  905. APPEND16(0); // no answers
  906. APPEND16(0); // no authority
  907. APPEND16(0); // no additional
  908. labels = (u8 *) malloc(name_len + 2);
  909. if (!labels) return -1;
  910. labels_len = dnsname_to_labels(labels, name, name_len);
  911. if (labels_len < 0) return labels_len;
  912. memcpy(buf + j, labels, labels_len);
  913. j += labels_len;
  914. APPEND16(type);
  915. APPEND16(class);
  916. #undef APPEND16
  917. return j;
  918. }
  919. // this is a libevent callback function which is called when a request
  920. // has timed out.
  921. static void
  922. eventdns_request_timeout_callback(int fd, short events, void *arg) {
  923. struct request *const req = (struct request *) arg;
  924. (void) fd;
  925. (void) events;
  926. log("Request %lx timed out", (unsigned long) arg);
  927. req->ns->timedout++;
  928. if (req->ns->timedout > global_max_nameserver_timeout) {
  929. nameserver_failed(req->ns, "request timed out.");
  930. }
  931. evtimer_del(&req->timeout_event);
  932. if (req->tx_count >= global_max_retransmits) {
  933. // this request has failed
  934. req->user_callback(DNS_ERR_TIMEOUT, 0, 0, 0, NULL, req->user_pointer);
  935. request_finished(req, &req_head);
  936. } else {
  937. // retransmit it
  938. eventdns_request_transmit(req);
  939. }
  940. }
  941. // try to send a request to a given server.
  942. //
  943. // return:
  944. // 0 ok
  945. // 1 temporary failure
  946. // 2 other failure
  947. static int
  948. eventdns_request_transmit_to(struct request *req, struct nameserver *server) {
  949. const int r = send(server->socket, req->request, req->request_len, 0);
  950. if (r < 0) {
  951. int err = last_error(server->socket);
  952. if (error_is_eagain(err)) return 1;
  953. nameserver_failed(req->ns, strerror(err));
  954. return 2;
  955. } else if (r != (int)req->request_len) {
  956. return 1; // short write
  957. } else {
  958. return 0;
  959. }
  960. }
  961. // try to send a request, updating the fields of the request
  962. // as needed
  963. //
  964. // return:
  965. // 0 ok
  966. // 1 failed
  967. static int
  968. eventdns_request_transmit(struct request *req) {
  969. int retcode = 0, r;
  970. // if we fail to send this packet then this flag marks it
  971. // for eventdns_transmit
  972. req->transmit_me = 1;
  973. if (req->trans_id == 0xffff) abort();
  974. if (req->ns->choaked) {
  975. // don't bother trying to write to a socket
  976. // which we have had EAGAIN from
  977. return 1;
  978. }
  979. r = eventdns_request_transmit_to(req, req->ns);
  980. switch (r) {
  981. case 1:
  982. // temp failure
  983. req->ns->choaked = 1;
  984. nameserver_write_waiting(req->ns, 1);
  985. return 1;
  986. case 2:
  987. // failed in some other way
  988. retcode = 1;
  989. // fall through
  990. default:
  991. // all ok
  992. log("Setting timeout for request %lx", (unsigned long) req);
  993. evtimer_set(&req->timeout_event, eventdns_request_timeout_callback, req);
  994. evtimer_add(&req->timeout_event, &global_timeout);
  995. req->tx_count++;
  996. req->transmit_me = 0;
  997. return retcode;
  998. }
  999. }
  1000. static void
  1001. nameserver_probe_callback(int result, char type, int count, int ttl, void *addresses, void *arg) {
  1002. struct nameserver *const ns = (struct nameserver *) arg;
  1003. (void) type;
  1004. (void) count;
  1005. (void) ttl;
  1006. (void) addresses;
  1007. if (result == DNS_ERR_NONE || result == DNS_ERR_NOTEXIST) {
  1008. // this is a good reply
  1009. nameserver_up(ns);
  1010. } else nameserver_probe_failed(ns);
  1011. }
  1012. static void
  1013. nameserver_send_probe(struct nameserver *const ns) {
  1014. struct request *req;
  1015. // here we need to send a probe to a given nameserver
  1016. // in the hope that it is up now.
  1017. log("Sending probe to %s", debug_ntoa(ns->address));
  1018. req = request_new("www.google.com", DNS_QUERY_NO_SEARCH, nameserver_probe_callback, ns);
  1019. if (!req) return;
  1020. // we force this into the inflight queue no matter what
  1021. request_trans_id_set(req, transaction_id_pick());
  1022. req->ns = ns;
  1023. request_submit(req);
  1024. }
  1025. // returns:
  1026. // 0 didn't try to transmit anything
  1027. // 1 tried to transmit something
  1028. static int
  1029. eventdns_transmit(void) {
  1030. char did_try_to_transmit = 0;
  1031. if (req_head) {
  1032. struct request *const started_at = req_head, *req = req_head;
  1033. // first transmit all the requests which are currently waiting
  1034. do {
  1035. if (req->transmit_me) {
  1036. did_try_to_transmit = 1;
  1037. eventdns_request_transmit(req);
  1038. }
  1039. req = req->next;
  1040. } while (req != started_at);
  1041. }
  1042. return did_try_to_transmit;
  1043. }
  1044. // exported function
  1045. int
  1046. eventdns_count_nameservers(void)
  1047. {
  1048. const struct nameserver *server = server_head;
  1049. int n = 0;
  1050. if (!server)
  1051. return 0;
  1052. do {
  1053. ++n;
  1054. server = server->next;
  1055. } while (server != server_head);
  1056. return n;
  1057. }
  1058. // exported function
  1059. int
  1060. eventdns_clear_nameservers_and_suspend(void)
  1061. {
  1062. struct nameserver *server = server_head, *started_at = server_head;
  1063. struct request *req = req_head, *req_started_at = req_head;
  1064. if (!server)
  1065. return 0;
  1066. while (1) {
  1067. struct nameserver *next = server->next;
  1068. event_del(&server->event);
  1069. evtimer_del(&server->timeout_event);
  1070. if (server->socket >= 0)
  1071. CLOSE_SOCKET(server->socket);
  1072. free(server);
  1073. if (next == started_at)
  1074. break;
  1075. server = next;
  1076. }
  1077. server_head = NULL;
  1078. while (req) {
  1079. struct request *next = req->next;
  1080. req->next = req->prev = NULL;
  1081. req->tx_count = req->reissue_count = 0;
  1082. req->ns = NULL;
  1083. // ???? What to do about searches?
  1084. evtimer_del(&req->timeout_event);
  1085. req->trans_id = 0;
  1086. req->transmit_me = 0;
  1087. eventdns_request_insert(req, &req_waiting_head);
  1088. if (next == req_started_at)
  1089. break;
  1090. req = next;
  1091. }
  1092. req_head = NULL;
  1093. return 0;
  1094. }
  1095. // exported function
  1096. int
  1097. eventdns_resume(void)
  1098. {
  1099. eventdns_requests_pump_waiting_queue();
  1100. return 0;
  1101. }
  1102. // exported function
  1103. int
  1104. eventdns_nameserver_add(unsigned long int address) {
  1105. // first check to see if we already have this nameserver
  1106. const struct nameserver *server = server_head, *const started_at = server_head;
  1107. struct nameserver *ns;
  1108. struct sockaddr_in sin;
  1109. int err = 0;
  1110. if (server) {
  1111. do {
  1112. if (server->address == address) return 3;
  1113. server = server->next;
  1114. } while (server != started_at);
  1115. }
  1116. ns = (struct nameserver *) malloc(sizeof(struct nameserver));
  1117. if (!ns) return -1;
  1118. memset(ns, 0, sizeof(struct nameserver));
  1119. ns->socket = socket(PF_INET, SOCK_DGRAM, 0);
  1120. if (ns->socket < 0) { err = 1; goto out1; }
  1121. #ifdef MS_WINDOWS
  1122. {
  1123. u_long nonblocking = 1;
  1124. ioctlsocket(ns->socket, FIONBIO, &nonblocking);
  1125. }
  1126. #else
  1127. fcntl(ns->socket, F_SETFL, O_NONBLOCK);
  1128. #endif
  1129. sin.sin_addr.s_addr = address;
  1130. sin.sin_port = htons(53);
  1131. sin.sin_family = AF_INET;
  1132. if (connect(ns->socket, (struct sockaddr *) &sin, sizeof(sin)) != 0) {
  1133. err = 2;
  1134. goto out2;
  1135. }
  1136. ns->address = address;
  1137. ns->state = 1;
  1138. event_set(&ns->event, ns->socket, EV_READ | EV_PERSIST, nameserver_ready_callback, ns);
  1139. event_add(&ns->event, NULL);
  1140. log("Added nameserver %s", debug_ntoa(address));
  1141. // insert this nameserver into the list of them
  1142. if (!server_head) {
  1143. ns->next = ns->prev = ns;
  1144. server_head = ns;
  1145. } else {
  1146. ns->next = server_head->next;
  1147. ns->prev = server_head;
  1148. server_head->next = ns;
  1149. if (server_head->prev == server_head) {
  1150. server_head->prev = ns;
  1151. }
  1152. }
  1153. global_good_nameservers++;
  1154. return 0;
  1155. out2:
  1156. CLOSE_SOCKET(ns->socket);
  1157. #endif
  1158. out1:
  1159. free(ns);
  1160. log("Unable to add nameserver %s: error %d", debug_ntoa(address), err);
  1161. return err;
  1162. }
  1163. // exported function
  1164. int
  1165. eventdns_nameserver_ip_add(const char *ip_as_string) {
  1166. struct in_addr ina;
  1167. if (!inet_aton(ip_as_string, &ina)) return 4;
  1168. return eventdns_nameserver_add(ina.s_addr);
  1169. }
  1170. // insert into the tail of the queue
  1171. static void
  1172. eventdns_request_insert(struct request *req, struct request **head) {
  1173. if (!*head) {
  1174. *head = req;
  1175. req->next = req->prev = req;
  1176. return;
  1177. }
  1178. req->prev = (*head)->prev;
  1179. req->prev->next = req;
  1180. req->next = *head;
  1181. (*head)->prev = req;
  1182. }
  1183. static int
  1184. string_num_dots(const char *s) {
  1185. int count = 0;
  1186. while ((s = strchr(s, '.'))) {
  1187. s++;
  1188. count++;
  1189. }
  1190. return count;
  1191. }
  1192. static struct request *
  1193. request_new(const char *name, int flags, eventdns_callback_type callback, void *ptr) {
  1194. const char issuing_now = (global_requests_inflight < global_max_requests_inflight) ? 1 : 0;
  1195. const int name_len = strlen(name);
  1196. const int request_max_len = eventdns_request_len(name_len);
  1197. const u16 trans_id = issuing_now ? transaction_id_pick() : 0xffff;
  1198. // the request data is alloced in a single block with the header
  1199. struct request *const req = (struct request *) malloc(sizeof(struct request) + request_max_len);
  1200. int rlen;
  1201. (void) flags;
  1202. if (!req) return NULL;
  1203. memset(req, 0, sizeof(struct request));
  1204. // request data lives just after the header
  1205. req->request = ((u8 *) req) + sizeof(struct request);
  1206. req->request_appended = 1; // denotes that the request data shouldn't be free()ed
  1207. rlen = eventdns_request_data_build(name, name_len, trans_id, TYPE_A, CLASS_INET, req->request);
  1208. if (rlen < 0) goto err1;
  1209. req->request_len = rlen;
  1210. req->trans_id = trans_id;
  1211. req->tx_count = 0;
  1212. req->user_pointer = ptr;
  1213. req->user_callback = callback;
  1214. req->ns = issuing_now ? nameserver_pick() : NULL;
  1215. req->next = req->prev = NULL;
  1216. return req;
  1217. err1:
  1218. free(req->request);
  1219. return NULL;
  1220. }
  1221. static void
  1222. request_submit(struct request *const req) {
  1223. if (req->ns) {
  1224. // if it has a nameserver assigned then this is going
  1225. // straight into the inflight queue
  1226. eventdns_request_insert(req, &req_head);
  1227. global_requests_inflight++;
  1228. eventdns_request_transmit(req);
  1229. } else {
  1230. eventdns_request_insert(req, &req_waiting_head);
  1231. global_requests_waiting++;
  1232. }
  1233. }
  1234. // exported function
  1235. int eventdns_resolve(const char *name, int flags, eventdns_callback_type callback, void *ptr) {
  1236. log("Resolve requested for %s", name);
  1237. if (flags & DNS_QUERY_NO_SEARCH) {
  1238. struct request *const req = request_new(name, flags, callback, ptr);
  1239. if (!req) return 1;
  1240. request_submit(req);
  1241. return 0;
  1242. } else {
  1243. return search_request_new(name, flags, callback, ptr);
  1244. }
  1245. }
  1246. /////////////////////////////////////////////////////////////////////
  1247. // Search support
  1248. //
  1249. // the libc resolver has support for searching a number of domains
  1250. // to find a name. If nothing else then it takes the single domain
  1251. // from the gethostname() call.
  1252. //
  1253. // It can also be configured via the domain and search options in a
  1254. // resolv.conf.
  1255. //
  1256. // The ndots option controls how many dots it takes for the resolver
  1257. // to decide that a name is non-local and so try a raw lookup first.
  1258. struct search_domain {
  1259. int len;
  1260. struct search_domain *next;
  1261. // the text string is appended to this structure
  1262. };
  1263. struct search_state {
  1264. int refcount;
  1265. int ndots;
  1266. int num_domains;
  1267. struct search_domain *head;
  1268. };
  1269. static struct search_state *global_search_state = NULL;
  1270. static void
  1271. search_state_decref(struct search_state *const state) {
  1272. if (!state) return;
  1273. state->refcount--;
  1274. if (!state->refcount) {
  1275. struct search_domain *next, *dom;
  1276. for (dom = state->head; dom; dom = next) {
  1277. next = dom->next;
  1278. free(dom);
  1279. }
  1280. free(state);
  1281. }
  1282. };
  1283. static struct search_state *
  1284. search_state_new(void) {
  1285. struct search_state *state = (struct search_state *) malloc(sizeof(struct search_state));
  1286. if (!state) return NULL;
  1287. memset(state, 0, sizeof(struct search_state));
  1288. state->refcount = 1;
  1289. state->ndots = 1;
  1290. return state;
  1291. }
  1292. static void
  1293. search_postfix_clear(void) {
  1294. search_state_decref(global_search_state);
  1295. global_search_state = search_state_new();
  1296. }
  1297. // exported function
  1298. void
  1299. eventdns_search_clear(void) {
  1300. search_postfix_clear();
  1301. }
  1302. static void
  1303. search_postfix_add(const char *domain) {
  1304. int domain_len;
  1305. struct search_domain *sdomain;
  1306. while (domain[0] == '.') domain++;
  1307. domain_len = strlen(domain);
  1308. if (!global_search_state) global_search_state = search_state_new();
  1309. if (!global_search_state) return;
  1310. global_search_state->num_domains++;
  1311. sdomain = (struct search_domain *) malloc(sizeof(struct search_domain) + domain_len);
  1312. if (!sdomain) return;
  1313. memcpy( ((u8 *) sdomain) + sizeof(struct search_domain), domain, domain_len);
  1314. sdomain->next = global_search_state->head;
  1315. sdomain->len = domain_len;
  1316. global_search_state->head = sdomain;
  1317. }
  1318. // reverse the order of members in the postfix list. This is needed because,
  1319. // when parsing resolv.conf we push elements in the wrong order
  1320. static void
  1321. search_reverse(void) {
  1322. struct search_domain *cur, *prev = NULL, *next;
  1323. cur = global_search_state->head;
  1324. while (cur) {
  1325. next = cur->next;
  1326. cur->next = prev;
  1327. prev = cur;
  1328. cur = next;
  1329. }
  1330. global_search_state->head = prev;
  1331. }
  1332. // exported function
  1333. void
  1334. eventdns_search_add(const char *domain) {
  1335. search_postfix_add(domain);
  1336. }
  1337. // exported function
  1338. void
  1339. eventdns_search_ndots_set(const int ndots) {
  1340. if (!global_search_state) global_search_state = search_state_new();
  1341. if (!global_search_state) return;
  1342. global_search_state->ndots = ndots;
  1343. }
  1344. static void
  1345. search_set_from_hostname(void) {
  1346. char hostname[HOST_NAME_MAX + 1], *domainname;
  1347. search_postfix_clear();
  1348. if (gethostname(hostname, sizeof(hostname))) return;
  1349. domainname = strchr(hostname, '.');
  1350. if (!domainname) return;
  1351. search_postfix_add(domainname);
  1352. }
  1353. // warning: returns malloced string
  1354. static char *
  1355. search_make_new(const struct search_state *const state, int n, const char *const base_name) {
  1356. const int base_len = strlen(base_name);
  1357. const char need_to_append_dot = base_name[base_len - 1] == '.' ? 0 : 1;
  1358. struct search_domain *dom;
  1359. for (dom = state->head; dom; dom = dom->next) {
  1360. if (!n--) {
  1361. // this is the postfix we want
  1362. // the actual postfix string is kept at the end of the structure
  1363. const u8 *const postfix = ((u8 *) dom) + sizeof(struct search_domain);
  1364. const int postfix_len = dom->len;
  1365. char *const newname = (char *) malloc(base_len + need_to_append_dot + postfix_len + 1);
  1366. if (!newname) return NULL;
  1367. memcpy(newname, base_name, base_len);
  1368. if (need_to_append_dot) newname[base_len] = '.';
  1369. memcpy(newname + base_len + need_to_append_dot, postfix, postfix_len);
  1370. newname[base_len + need_to_append_dot + postfix_len] = 0;
  1371. return newname;
  1372. }
  1373. }
  1374. // we ran off the end of the list and still didn't find the requested string
  1375. abort();
  1376. }
  1377. static int
  1378. search_request_new(const char *const name, int flags, eventdns_callback_type user_callback, void *user_arg) {
  1379. if ( ((flags & DNS_QUERY_NO_SEARCH) == 0) &&
  1380. global_search_state &&
  1381. global_search_state->num_domains) {
  1382. // we have some domains to search
  1383. struct request *req;
  1384. if (string_num_dots(name) >= global_search_state->ndots) {
  1385. req = request_new(name, flags, user_callback, user_arg);
  1386. if (!req) return 1;
  1387. req->search_index = -1;
  1388. } else {
  1389. char *const new_name = search_make_new(global_search_state, 0, name);
  1390. if (!new_name) return 1;
  1391. req = request_new(new_name, flags, user_callback, user_arg);
  1392. free(new_name);
  1393. if (!req) return 1;
  1394. req->search_index = 0;
  1395. }
  1396. req->search_origname = strdup(name);
  1397. req->search_state = global_search_state;
  1398. req->search_flags = flags;
  1399. global_search_state->refcount++;
  1400. request_submit(req);
  1401. return 0;
  1402. } else {
  1403. struct request *const req = request_new(name, flags, user_callback, user_arg);
  1404. if (!req) return 1;
  1405. request_submit(req);
  1406. return 0;
  1407. }
  1408. }
  1409. // this is called when a request has failed to find a name. We need to check
  1410. // if it is part of a search and, if so, try the next name in the list
  1411. // returns:
  1412. // 0 another request has been submitted
  1413. // 1 no more requests needed
  1414. static int
  1415. search_try_next(struct request *const req) {
  1416. if (req->search_state) {
  1417. // it is part of a search
  1418. char *new_name;
  1419. struct request *newreq;
  1420. req->search_index++;
  1421. if (req->search_index >= req->search_state->num_domains) {
  1422. // no more postfixes to try, however we may need to try
  1423. // this name without a postfix
  1424. if (string_num_dots(req->search_origname) < req->search_state->ndots) {
  1425. // yep, we need to try it raw
  1426. struct request *const newreq = request_new(req->search_origname, req->search_flags, req->user_callback, req->user_pointer);
  1427. log("Search: trying raw query %s", req->search_origname);
  1428. if (newreq) {
  1429. request_submit(newreq);
  1430. return 0;
  1431. }
  1432. }
  1433. return 1;
  1434. }
  1435. new_name = search_make_new(req->search_state, req->search_index, req->search_origname);
  1436. if (!new_name) return 1;
  1437. log("Search: now trying %s (%d)", new_name, req->search_index);
  1438. newreq = request_new(new_name, req->search_flags, req->user_callback, req->user_pointer);
  1439. free(new_name);
  1440. if (!newreq) return 1;
  1441. newreq->search_origname = req->search_origname;
  1442. req->search_origname = NULL;
  1443. newreq->search_state = req->search_state;
  1444. newreq->search_flags = req->search_flags;
  1445. newreq->search_index = req->search_index;
  1446. newreq->search_state->refcount++;
  1447. request_submit(newreq);
  1448. return 0;
  1449. }
  1450. return 1;
  1451. }
  1452. static void
  1453. search_request_finished(struct request *const req) {
  1454. if (req->search_state) {
  1455. search_state_decref(req->search_state);
  1456. req->search_state = NULL;
  1457. }
  1458. if (req->search_origname) {
  1459. free(req->search_origname);
  1460. req->search_origname = NULL;
  1461. }
  1462. }
  1463. /////////////////////////////////////////////////////////////////////
  1464. // Parsing resolv.conf files
  1465. static void
  1466. eventdns_resolv_set_defaults(int flags) {
  1467. // if the file isn't found then we assume a local resolver
  1468. if (flags & DNS_OPTION_SEARCH) search_set_from_hostname();
  1469. if (flags & DNS_OPTION_NAMESERVERS) eventdns_nameserver_ip_add("127.0.0.1");
  1470. }
  1471. #ifndef HAVE_STRTOK_R
  1472. static char *
  1473. strtok_r(char *s, const char *delim, char **state) {
  1474. return strtok(s, delim);
  1475. }
  1476. #endif
  1477. // helper version of atoi which returns -1 on error
  1478. static int
  1479. strtoint(const char *const str) {
  1480. char *endptr;
  1481. const int r = strtol(str, &endptr, 10);
  1482. if (*endptr) return -1;
  1483. return r;
  1484. }
  1485. static void
  1486. resolv_conf_parse_line(char *const start, int flags) {
  1487. char *strtok_state;
  1488. static const char *const delims = " \t";
  1489. #define NEXT_TOKEN strtok_r(NULL, delims, &strtok_state)
  1490. char *const first_token = strtok_r(start, delims, &strtok_state);
  1491. if (!first_token) return;
  1492. if (!strcmp(first_token, "nameserver")) {
  1493. const char *const nameserver = NEXT_TOKEN;
  1494. struct in_addr ina;
  1495. if (inet_aton(nameserver, &ina)) {
  1496. // address is valid
  1497. eventdns_nameserver_add(ina.s_addr);
  1498. }
  1499. } else if (!strcmp(first_token, "domain") && (flags & DNS_OPTION_SEARCH)) {
  1500. const char *const domain = NEXT_TOKEN;
  1501. if (domain) {
  1502. search_postfix_clear();
  1503. search_postfix_add(domain);
  1504. }
  1505. } else if (!strcmp(first_token, "search") && (flags & DNS_OPTION_SEARCH)) {
  1506. const char *domain;
  1507. search_postfix_clear();
  1508. while ((domain = NEXT_TOKEN)) {
  1509. search_postfix_add(domain);
  1510. }
  1511. search_reverse();
  1512. } else if (!strcmp(first_token, "options")) {
  1513. const char *option;
  1514. while ((option = NEXT_TOKEN)) {
  1515. if (!strncmp(option, "ndots:", 6)) {
  1516. const int ndots = strtoint(&option[6]);
  1517. if (ndots == -1) continue;
  1518. if (!(flags & DNS_OPTION_SEARCH)) continue;
  1519. log("Setting ndots to %d", ndots);
  1520. if (!global_search_state) global_search_state = search_state_new();
  1521. if (!global_search_state) return;
  1522. global_search_state->ndots = ndots;
  1523. } else if (!strncmp(option, "timeout:", 8)) {
  1524. const int timeout = strtoint(&option[8]);
  1525. if (timeout == -1) continue;
  1526. if (!(flags & DNS_OPTION_MISC)) continue;
  1527. log("Setting timeout to %d", timeout);
  1528. global_timeout.tv_sec = timeout;
  1529. } else if (!strncmp(option, "attempts:", 9)) {
  1530. const int retries = strtoint(&option[9]);
  1531. if (retries == -1) continue;
  1532. if (!(flags & DNS_OPTION_MISC)) continue;
  1533. log("Setting retries to %d", retries);
  1534. global_max_retransmits = retries;
  1535. }
  1536. }
  1537. }
  1538. #undef NEXT_TOKEN
  1539. }
  1540. // exported function
  1541. // returns:
  1542. // 0 no errors
  1543. // 1 failed to open file
  1544. // 2 failed to stat file
  1545. // 3 file too large
  1546. // 4 out of memory
  1547. // 5 short read from file
  1548. int
  1549. eventdns_resolv_conf_parse(int flags, const char *const filename) {
  1550. struct stat st;
  1551. int fd;
  1552. u8 *resolv;
  1553. char *start;
  1554. int err = 0;
  1555. log("Parsing resolve.conf file %s", filename);
  1556. fd = open(filename, O_RDONLY);
  1557. if (fd < 0) {
  1558. eventdns_resolv_set_defaults(flags);
  1559. return 0;
  1560. }
  1561. if (fstat(fd, &st)) { err = 2; goto out1; }
  1562. if (!st.st_size) {
  1563. eventdns_resolv_set_defaults(flags);
  1564. err = 0;
  1565. goto out1;
  1566. }
  1567. if (st.st_size > 65535) { err = 3; goto out1; } // no resolv.conf should be any bigger
  1568. resolv = (u8 *) malloc(st.st_size + 1);
  1569. if (!resolv) { err = 4; goto out1; }
  1570. if (read(fd, resolv, st.st_size) != st.st_size) { err = 5; goto out2; }
  1571. resolv[st.st_size] = 0; // we malloced an extra byte
  1572. start = (char *) resolv;
  1573. for (;;) {
  1574. char *const newline = strchr(start, '\n');
  1575. if (!newline) {
  1576. resolv_conf_parse_line(start, flags);
  1577. break;
  1578. } else {
  1579. *newline = 0;
  1580. resolv_conf_parse_line(start, flags);
  1581. start = newline + 1;
  1582. }
  1583. }
  1584. if (!server_head && (flags & DNS_OPTION_NAMESERVERS)) {
  1585. // no nameservers were configured.
  1586. eventdns_nameserver_ip_add("127.0.0.1");
  1587. }
  1588. if (flags & DNS_OPTION_SEARCH && (!global_search_state || global_search_state->num_domains == 0)) {
  1589. search_set_from_hostname();
  1590. }
  1591. out2:
  1592. free(resolv);
  1593. out1:
  1594. close(fd);
  1595. return err;
  1596. }
  1597. #ifdef MS_WINDOWS
  1598. // Add multiple nameservers from a space-or-comma-separated list.
  1599. static int
  1600. eventdns_nameserver_ip_add_line(const char *ips) {
  1601. const char *addr;
  1602. char *buf;
  1603. int r;
  1604. while (*ips) {
  1605. while (ISSPACE(*ips) || *ips == ',' || *ips == '\t')
  1606. ++ips;
  1607. addr = ips;
  1608. while (ISDIGIT(*ips) || *ips == '.')
  1609. ++ips;
  1610. buf = malloc(ips-addr+1);
  1611. if (!buf) return 4;
  1612. memcpy(buf, addr, ips-addr);
  1613. buf[ips-addr] = '\0';
  1614. r = eventdns_nameserver_ip_add(buf);
  1615. free(buf);
  1616. if (r) return r;
  1617. }
  1618. return 0;
  1619. }
  1620. // Use the windows GetNetworkParams interface in iphlpapi.dll to
  1621. // figure out what our nameservers are.
  1622. static int
  1623. load_nameservers_with_getnetworkparams(void)
  1624. {
  1625. // Based on MSDN examples and inspection of c-ares code.
  1626. FIXED_INFO *fixed;
  1627. HMODULE handle = 0;
  1628. ULONG size = sizeof(FIXED_INFO);
  1629. void *buf = NULL;
  1630. int status = 0, r, added_any;
  1631. IP_ADDR_STRING *ns;
  1632. DWORD (WINAPI *fn)(FIXED_INFO*, DWORD*);
  1633. if (!(handle = LoadLibrary("iphlpapi.dll")))
  1634. goto done;
  1635. if (!(fn = GetProcAddress(handle, "GetNetworkParams")))
  1636. goto done;
  1637. buf = malloc(size);
  1638. if (!buf) { status = 4; goto done; }
  1639. fixed = buf;
  1640. r = fn(fixed, &size);
  1641. if (r != ERROR_SUCCESS && r != ERROR_BUFFER_OVERFLOW) {
  1642. status = -1;
  1643. goto done;
  1644. }
  1645. if (r != ERROR_SUCCESS) {
  1646. free(buf);
  1647. buf = malloc(size);
  1648. if (!buf) { status = 4; goto done; }
  1649. fixed = buf;
  1650. r = fn(fixed, &size);
  1651. if (r != ERROR_SUCCESS) { status = -1; goto done; }
  1652. }
  1653. assert(fixed);
  1654. added_any = 0;
  1655. ns = fixed->DnsServerList;
  1656. while (ns) {
  1657. r = eventdns_nameserver_ip_add_line(ns->IpAddress.String);
  1658. if (r) { status = r; goto done; }
  1659. added_any = 0;
  1660. ns = ns->next;
  1661. }
  1662. if (!added_any)
  1663. status = -1;
  1664. done:
  1665. if (buf)
  1666. free(buf);
  1667. if (handle)
  1668. FreeLibrary(handle);
  1669. return status;
  1670. }
  1671. static int
  1672. config_nameserver_from_reg_key(HKEY key, const char *subkey)
  1673. {
  1674. char *buf;
  1675. DWORD bufsz = 0, type = 0;
  1676. int status = 0;
  1677. if (RegQueryValueEx(key, subkey, 0, &type, NULL, &bufsz)
  1678. != ERROR_MORE_DATA)
  1679. return -1;
  1680. if (!(buf = malloc(bufsz)))
  1681. return -1;
  1682. if (RegQueryValueEx(key, subkey, 0, &type, (LPBYTE)buf, &bufsz)
  1683. == ERROR_SUCCESS && bufsz > 1) {
  1684. status = eventdns_nameserver_ip_add_line(buf);
  1685. }
  1686. free(buf);
  1687. return status;
  1688. }
  1689. static int
  1690. load_nameservers_from_registry(void)
  1691. {
  1692. int found = 0;
  1693. #define TRY(k, name) \
  1694. if (!found && config_nameserver_from_reg_key(k,name) == 0) { \
  1695. log("Found nameservers in %s/%s",#k,name); \
  1696. found = 1; \
  1697. }
  1698. if (IS_NT()) {
  1699. HKEY nt_key = 0, interfaces_key = 0;
  1700. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, WIN_NS_NT_KEY, 0,
  1701. KEY_READ, &nt_key) != ERROR_SUCCESS)
  1702. return -1;
  1703. RegOpenKeyEx(nt_key, "Interfaces", 0,
  1704. KEY_QUERY_VALUE|KEY_ENUMERATE_SUBKEYS,
  1705. &interfaces_key);
  1706. TRY(nt_key, NAMESERVER);
  1707. TRY(nt_key, DHCPNAMESERVER);
  1708. TRY(interfaces_key, NAMESERVER);
  1709. TRY(interfaces_key, DHCPNAMESERVER);
  1710. RegCloseKey(interfaces_key);
  1711. RegCloseKey(nt_key);
  1712. } else {
  1713. HKEY win_key = 0;
  1714. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, WIN_NS_9X, 0,
  1715. KEY_READ, &win_key) != ERROR_SUCCESS)
  1716. return -1;
  1717. TRY(win_key, NAMESERVER);
  1718. RegCloseKey(win_key);
  1719. }
  1720. return found ? 0 : -1;
  1721. #undef TRY
  1722. }
  1723. int
  1724. eventdns_config_windows_nameservers(void)
  1725. {
  1726. if (load_nameservers_with_getnetworkparams() == 0)
  1727. return 0;
  1728. return load_nameservers_from_registry();
  1729. }
  1730. #endif