visaljs 0.0.9 → 0.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (232) hide show
  1. package/README.md +12 -4
  2. package/THIRD_PARTY_LICENSES +31 -1
  3. package/cli.js +3745 -890
  4. package/node/include/js_node.h +12 -1
  5. package/node/prelude/_http_common.js +29 -0
  6. package/node/prelude/async_hooks.js +50 -11
  7. package/node/prelude/child_process.js +31 -4
  8. package/node/prelude/console.js +311 -143
  9. package/node/prelude/crypto.js +6 -4
  10. package/node/prelude/dgram.js +1169 -0
  11. package/node/prelude/diagnostics_channel.js +3 -1
  12. package/node/prelude/dns.js +250 -45
  13. package/node/prelude/dns_promises.js +8 -2
  14. package/node/prelude/fs_promises.js +9 -1
  15. package/node/prelude/http.js +405 -94
  16. package/node/prelude/https.js +13 -4
  17. package/node/prelude/internal/console_class.js +7 -0
  18. package/node/prelude/internal/console_global.js +25 -0
  19. package/node/prelude/internal/crypto/key_encryption.js +217 -0
  20. package/node/prelude/internal/crypto/keys.js +161 -26
  21. package/node/prelude/internal/crypto/random.js +24 -0
  22. package/node/prelude/internal/crypto/util.js +2 -1
  23. package/node/prelude/internal/crypto_x509.js +161 -0
  24. package/node/prelude/internal/dns_classes.js +6 -0
  25. package/node/prelude/internal/dns_resolver.js +371 -0
  26. package/node/prelude/internal/errors.js +9 -0
  27. package/node/prelude/internal/events_on.js +82 -37
  28. package/node/prelude/internal/features.js +8 -4
  29. package/node/prelude/internal/fs_filehandle.js +10 -4
  30. package/node/prelude/internal/http_chars.ts +22 -0
  31. package/node/prelude/internal/perf_histogram.js +1 -1
  32. package/node/prelude/internal/process_more.js +32 -17
  33. package/node/prelude/internal/process_signals.js +8 -2
  34. package/node/prelude/internal/punycode.ts +163 -0
  35. package/node/prelude/internal/readline.js +648 -274
  36. package/node/prelude/internal/readline_editor.js +1240 -0
  37. package/node/prelude/internal/string_width.js +43 -0
  38. package/node/prelude/internal/tls/common.js +145 -33
  39. package/node/prelude/internal/tls/wrap.js +182 -49
  40. package/node/prelude/internal/tls/x509.js +4 -1
  41. package/node/prelude/internal/udp.js +177 -0
  42. package/node/prelude/net.js +30 -14
  43. package/node/prelude/os.js +24 -6
  44. package/node/prelude/perf_hooks.js +37 -2
  45. package/node/prelude/punycode.js +2 -130
  46. package/node/prelude/readline.js +113 -4
  47. package/node/prelude/readline_promises.js +56 -32
  48. package/node/prelude/tls.js +21 -4
  49. package/node/prelude/util.js +3 -16
  50. package/node/src/host_dns.c +623 -0
  51. package/node/src/host_http.c +12 -0
  52. package/node/src/host_net.c +46 -0
  53. package/node/src/host_os.c +1 -6
  54. package/node/src/host_sys.c +79 -0
  55. package/node/src/host_udp.c +418 -0
  56. package/node/third_party/c-ares/LICENSE.md +24 -0
  57. package/node/third_party/c-ares/config/darwin/ares_config.h +573 -0
  58. package/node/third_party/c-ares/config/freebsd/ares_config.h +573 -0
  59. package/node/third_party/c-ares/config/linux/ares_config.h +573 -0
  60. package/node/third_party/c-ares/config/openbsd/ares_config.h +583 -0
  61. package/node/third_party/c-ares/include/ares.h +1237 -0
  62. package/node/third_party/c-ares/include/ares_build.h +202 -0
  63. package/node/third_party/c-ares/include/ares_dns.h +127 -0
  64. package/node/third_party/c-ares/include/ares_dns_record.h +1119 -0
  65. package/node/third_party/c-ares/include/ares_nameser.h +510 -0
  66. package/node/third_party/c-ares/include/ares_version.h +47 -0
  67. package/node/third_party/c-ares/src/lib/ares_addrinfo2hostent.c +305 -0
  68. package/node/third_party/c-ares/src/lib/ares_addrinfo_localhost.c +245 -0
  69. package/node/third_party/c-ares/src/lib/ares_android.c +496 -0
  70. package/node/third_party/c-ares/src/lib/ares_android.h +38 -0
  71. package/node/third_party/c-ares/src/lib/ares_cancel.c +87 -0
  72. package/node/third_party/c-ares/src/lib/ares_close_sockets.c +137 -0
  73. package/node/third_party/c-ares/src/lib/ares_conn.c +511 -0
  74. package/node/third_party/c-ares/src/lib/ares_conn.h +202 -0
  75. package/node/third_party/c-ares/src/lib/ares_cookie.c +461 -0
  76. package/node/third_party/c-ares/src/lib/ares_data.c +168 -0
  77. package/node/third_party/c-ares/src/lib/ares_data.h +92 -0
  78. package/node/third_party/c-ares/src/lib/ares_destroy.c +159 -0
  79. package/node/third_party/c-ares/src/lib/ares_free_hostent.c +54 -0
  80. package/node/third_party/c-ares/src/lib/ares_free_string.c +33 -0
  81. package/node/third_party/c-ares/src/lib/ares_freeaddrinfo.c +67 -0
  82. package/node/third_party/c-ares/src/lib/ares_getaddrinfo.c +806 -0
  83. package/node/third_party/c-ares/src/lib/ares_getenv.c +38 -0
  84. package/node/third_party/c-ares/src/lib/ares_getenv.h +34 -0
  85. package/node/third_party/c-ares/src/lib/ares_gethostbyaddr.c +229 -0
  86. package/node/third_party/c-ares/src/lib/ares_gethostbyname.c +341 -0
  87. package/node/third_party/c-ares/src/lib/ares_getnameinfo.c +437 -0
  88. package/node/third_party/c-ares/src/lib/ares_hosts_file.c +960 -0
  89. package/node/third_party/c-ares/src/lib/ares_inet_net_pton.h +31 -0
  90. package/node/third_party/c-ares/src/lib/ares_init.c +631 -0
  91. package/node/third_party/c-ares/src/lib/ares_ipv6.h +115 -0
  92. package/node/third_party/c-ares/src/lib/ares_library_init.c +190 -0
  93. package/node/third_party/c-ares/src/lib/ares_metrics.c +261 -0
  94. package/node/third_party/c-ares/src/lib/ares_options.c +492 -0
  95. package/node/third_party/c-ares/src/lib/ares_parse_into_addrinfo.c +179 -0
  96. package/node/third_party/c-ares/src/lib/ares_private.h +648 -0
  97. package/node/third_party/c-ares/src/lib/ares_process.c +1649 -0
  98. package/node/third_party/c-ares/src/lib/ares_qcache.c +442 -0
  99. package/node/third_party/c-ares/src/lib/ares_query.c +151 -0
  100. package/node/third_party/c-ares/src/lib/ares_search.c +623 -0
  101. package/node/third_party/c-ares/src/lib/ares_send.c +300 -0
  102. package/node/third_party/c-ares/src/lib/ares_set_socket_functions.c +590 -0
  103. package/node/third_party/c-ares/src/lib/ares_setup.h +224 -0
  104. package/node/third_party/c-ares/src/lib/ares_socket.c +427 -0
  105. package/node/third_party/c-ares/src/lib/ares_socket.h +163 -0
  106. package/node/third_party/c-ares/src/lib/ares_sortaddrinfo.c +447 -0
  107. package/node/third_party/c-ares/src/lib/ares_strerror.c +91 -0
  108. package/node/third_party/c-ares/src/lib/ares_sysconfig.c +642 -0
  109. package/node/third_party/c-ares/src/lib/ares_sysconfig_files.c +851 -0
  110. package/node/third_party/c-ares/src/lib/ares_sysconfig_mac.c +373 -0
  111. package/node/third_party/c-ares/src/lib/ares_sysconfig_win.c +673 -0
  112. package/node/third_party/c-ares/src/lib/ares_timeout.c +162 -0
  113. package/node/third_party/c-ares/src/lib/ares_update_servers.c +1376 -0
  114. package/node/third_party/c-ares/src/lib/ares_version.c +36 -0
  115. package/node/third_party/c-ares/src/lib/config-dos.h +114 -0
  116. package/node/third_party/c-ares/src/lib/config-win32.h +396 -0
  117. package/node/third_party/c-ares/src/lib/dsa/ares_array.c +409 -0
  118. package/node/third_party/c-ares/src/lib/dsa/ares_htable.c +451 -0
  119. package/node/third_party/c-ares/src/lib/dsa/ares_htable.h +174 -0
  120. package/node/third_party/c-ares/src/lib/dsa/ares_htable_asvp.c +224 -0
  121. package/node/third_party/c-ares/src/lib/dsa/ares_htable_dict.c +230 -0
  122. package/node/third_party/c-ares/src/lib/dsa/ares_htable_strvp.c +210 -0
  123. package/node/third_party/c-ares/src/lib/dsa/ares_htable_szvp.c +188 -0
  124. package/node/third_party/c-ares/src/lib/dsa/ares_htable_vpstr.c +186 -0
  125. package/node/third_party/c-ares/src/lib/dsa/ares_htable_vpvp.c +194 -0
  126. package/node/third_party/c-ares/src/lib/dsa/ares_llist.c +385 -0
  127. package/node/third_party/c-ares/src/lib/dsa/ares_slist.c +479 -0
  128. package/node/third_party/c-ares/src/lib/dsa/ares_slist.h +207 -0
  129. package/node/third_party/c-ares/src/lib/event/ares_event.h +191 -0
  130. package/node/third_party/c-ares/src/lib/event/ares_event_configchg.c +746 -0
  131. package/node/third_party/c-ares/src/lib/event/ares_event_epoll.c +192 -0
  132. package/node/third_party/c-ares/src/lib/event/ares_event_kqueue.c +248 -0
  133. package/node/third_party/c-ares/src/lib/event/ares_event_poll.c +140 -0
  134. package/node/third_party/c-ares/src/lib/event/ares_event_select.c +159 -0
  135. package/node/third_party/c-ares/src/lib/event/ares_event_thread.c +575 -0
  136. package/node/third_party/c-ares/src/lib/event/ares_event_wake_pipe.c +172 -0
  137. package/node/third_party/c-ares/src/lib/event/ares_event_win32.c +981 -0
  138. package/node/third_party/c-ares/src/lib/event/ares_event_win32.h +161 -0
  139. package/node/third_party/c-ares/src/lib/include/ares_array.h +276 -0
  140. package/node/third_party/c-ares/src/lib/include/ares_buf.h +732 -0
  141. package/node/third_party/c-ares/src/lib/include/ares_htable_asvp.h +130 -0
  142. package/node/third_party/c-ares/src/lib/include/ares_htable_dict.h +123 -0
  143. package/node/third_party/c-ares/src/lib/include/ares_htable_strvp.h +130 -0
  144. package/node/third_party/c-ares/src/lib/include/ares_htable_szvp.h +118 -0
  145. package/node/third_party/c-ares/src/lib/include/ares_htable_vpstr.h +111 -0
  146. package/node/third_party/c-ares/src/lib/include/ares_htable_vpvp.h +128 -0
  147. package/node/third_party/c-ares/src/lib/include/ares_llist.h +239 -0
  148. package/node/third_party/c-ares/src/lib/include/ares_mem.h +41 -0
  149. package/node/third_party/c-ares/src/lib/include/ares_str.h +261 -0
  150. package/node/third_party/c-ares/src/lib/inet_net_pton.c +451 -0
  151. package/node/third_party/c-ares/src/lib/inet_ntop.c +219 -0
  152. package/node/third_party/c-ares/src/lib/legacy/ares_create_query.c +78 -0
  153. package/node/third_party/c-ares/src/lib/legacy/ares_expand_name.c +99 -0
  154. package/node/third_party/c-ares/src/lib/legacy/ares_expand_string.c +107 -0
  155. package/node/third_party/c-ares/src/lib/legacy/ares_fds.c +80 -0
  156. package/node/third_party/c-ares/src/lib/legacy/ares_getsock.c +85 -0
  157. package/node/third_party/c-ares/src/lib/legacy/ares_parse_a_reply.c +107 -0
  158. package/node/third_party/c-ares/src/lib/legacy/ares_parse_aaaa_reply.c +109 -0
  159. package/node/third_party/c-ares/src/lib/legacy/ares_parse_caa_reply.c +137 -0
  160. package/node/third_party/c-ares/src/lib/legacy/ares_parse_mx_reply.c +110 -0
  161. package/node/third_party/c-ares/src/lib/legacy/ares_parse_naptr_reply.c +132 -0
  162. package/node/third_party/c-ares/src/lib/legacy/ares_parse_ns_reply.c +154 -0
  163. package/node/third_party/c-ares/src/lib/legacy/ares_parse_ptr_reply.c +213 -0
  164. package/node/third_party/c-ares/src/lib/legacy/ares_parse_soa_reply.c +115 -0
  165. package/node/third_party/c-ares/src/lib/legacy/ares_parse_srv_reply.c +114 -0
  166. package/node/third_party/c-ares/src/lib/legacy/ares_parse_txt_reply.c +144 -0
  167. package/node/third_party/c-ares/src/lib/legacy/ares_parse_uri_reply.c +113 -0
  168. package/node/third_party/c-ares/src/lib/record/ares_dns_mapping.c +982 -0
  169. package/node/third_party/c-ares/src/lib/record/ares_dns_multistring.c +309 -0
  170. package/node/third_party/c-ares/src/lib/record/ares_dns_multistring.h +72 -0
  171. package/node/third_party/c-ares/src/lib/record/ares_dns_name.c +736 -0
  172. package/node/third_party/c-ares/src/lib/record/ares_dns_parse.c +1360 -0
  173. package/node/third_party/c-ares/src/lib/record/ares_dns_private.h +273 -0
  174. package/node/third_party/c-ares/src/lib/record/ares_dns_record.c +1701 -0
  175. package/node/third_party/c-ares/src/lib/record/ares_dns_write.c +1229 -0
  176. package/node/third_party/c-ares/src/lib/str/ares_buf.c +1510 -0
  177. package/node/third_party/c-ares/src/lib/str/ares_str.c +562 -0
  178. package/node/third_party/c-ares/src/lib/str/ares_strsplit.c +90 -0
  179. package/node/third_party/c-ares/src/lib/str/ares_strsplit.h +51 -0
  180. package/node/third_party/c-ares/src/lib/thirdparty/apple/dnsinfo.h +122 -0
  181. package/node/third_party/c-ares/src/lib/util/ares_iface_ips.c +640 -0
  182. package/node/third_party/c-ares/src/lib/util/ares_iface_ips.h +140 -0
  183. package/node/third_party/c-ares/src/lib/util/ares_math.c +167 -0
  184. package/node/third_party/c-ares/src/lib/util/ares_math.h +58 -0
  185. package/node/third_party/c-ares/src/lib/util/ares_rand.c +389 -0
  186. package/node/third_party/c-ares/src/lib/util/ares_rand.h +36 -0
  187. package/node/third_party/c-ares/src/lib/util/ares_threads.c +613 -0
  188. package/node/third_party/c-ares/src/lib/util/ares_threads.h +60 -0
  189. package/node/third_party/c-ares/src/lib/util/ares_time.h +49 -0
  190. package/node/third_party/c-ares/src/lib/util/ares_timeval.c +95 -0
  191. package/node/third_party/c-ares/src/lib/util/ares_uri.c +1627 -0
  192. package/node/third_party/c-ares/src/lib/util/ares_uri.h +252 -0
  193. package/node/third_party/c-ares/src/lib/windows_port.c +29 -0
  194. package/package.json +1 -1
  195. package/prebuilt/darwin-arm64/cares-a6286c65ca9374a3.a +0 -0
  196. package/prebuilt/darwin-x64/cares-a6286c65ca9374a3.a +0 -0
  197. package/prebuilt/linux-arm64/cares-4ba05ac8e948af8e.a +0 -0
  198. package/prebuilt/linux-arm64-musl/cares-4ba05ac8e948af8e.a +0 -0
  199. package/prebuilt/linux-x64/cares-4ba05ac8e948af8e.a +0 -0
  200. package/prebuilt/linux-x64-musl/cares-4ba05ac8e948af8e.a +0 -0
  201. package/prebuilt/win32-x64/boringssl-ce573858472b7fc9.a +0 -0
  202. package/prebuilt/win32-x64/brotli-20d86a61f30baeb3.a +0 -0
  203. package/prebuilt/win32-x64/cares-49c9ec95162b912d.a +0 -0
  204. package/prebuilt/win32-x64/uv-b11bae65d7fced82.a +0 -0
  205. package/prebuilt/win32-x64/zlib-4ce2317a4c3f5fb5.a +0 -0
  206. package/prebuilt/win32-x64/zstd-015315ac7c784c73.a +0 -0
  207. package/runtime/include/js_buffer.h +185 -0
  208. package/runtime/include/js_builtins.h +58 -0
  209. package/runtime/include/js_crypto.h +11 -0
  210. package/runtime/include/js_exception.h +3 -0
  211. package/runtime/include/js_function.h +1 -1
  212. package/runtime/include/js_gc.h +13 -10
  213. package/runtime/include/js_jobs.h +2 -0
  214. package/runtime/include/js_object.h +79 -3
  215. package/runtime/include/js_ops.h +27 -0
  216. package/runtime/include/js_runtime.h +1 -0
  217. package/runtime/include/js_web.h +0 -3
  218. package/runtime/include/jscontext.h +8 -0
  219. package/runtime/src/builtins.c +17 -4
  220. package/runtime/src/crypto_internal.h +1 -0
  221. package/runtime/src/crypto_subtle.c +66 -7
  222. package/runtime/src/crypto_tls.c +265 -19
  223. package/runtime/src/function.c +37 -10
  224. package/runtime/src/gc.c +76 -93
  225. package/runtime/src/inspect.c +18 -1
  226. package/runtime/src/iterator.c +5 -1
  227. package/runtime/src/jobs.c +37 -22
  228. package/runtime/src/object.c +91 -8
  229. package/runtime/src/os.c +16 -6
  230. package/runtime/src/os.h +2 -0
  231. package/runtime/src/process.c +97 -10
  232. package/runtime/src/typedarray.c +23 -0
@@ -0,0 +1,202 @@
1
+ /* MIT License
2
+ *
3
+ * Copyright (c) 2024 Brad House
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice (including the next
13
+ * paragraph) shall be included in all copies or substantial portions of the
14
+ * Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ *
24
+ * SPDX-License-Identifier: MIT
25
+ */
26
+ #ifndef __ARES_CONN_H
27
+ #define __ARES_CONN_H
28
+
29
+ #include "ares_socket.h"
30
+
31
+ struct ares_conn;
32
+ typedef struct ares_conn ares_conn_t;
33
+
34
+ struct ares_server;
35
+ typedef struct ares_server ares_server_t;
36
+
37
+ typedef enum {
38
+ /*! No flags */
39
+ ARES_CONN_FLAG_NONE = 0,
40
+ /*! TCP connection, not UDP */
41
+ ARES_CONN_FLAG_TCP = 1 << 0,
42
+ /*! TCP Fast Open is enabled and being used if supported by the OS */
43
+ ARES_CONN_FLAG_TFO = 1 << 1,
44
+ /*! TCP Fast Open has not yet sent its first packet. Gets unset on first
45
+ * write to a connection */
46
+ ARES_CONN_FLAG_TFO_INITIAL = 1 << 2,
47
+ /*! Connection has been retired: it must not be handed any NEW queries (e.g.
48
+ * it experienced a query timeout, suggesting packets are being dropped on
49
+ * it). Its in-flight queries continue to drain and it is cleaned up once
50
+ * idle. This is a per-connection signal so that a transient failure does
51
+ * not evict otherwise-healthy connections to the same server. */
52
+ ARES_CONN_FLAG_NONEW = 1 << 3
53
+ } ares_conn_flags_t;
54
+
55
+ typedef enum {
56
+ ARES_CONN_STATE_NONE = 0,
57
+ ARES_CONN_STATE_READ = 1 << 0,
58
+ ARES_CONN_STATE_WRITE = 1 << 1,
59
+ ARES_CONN_STATE_CONNECTED = 1 << 2, /* This doesn't get a callback */
60
+ ARES_CONN_STATE_CBFLAGS = ARES_CONN_STATE_READ | ARES_CONN_STATE_WRITE
61
+ } ares_conn_state_flags_t;
62
+
63
+ struct ares_conn {
64
+ ares_server_t *server;
65
+ ares_socket_t fd;
66
+ struct ares_addr self_ip;
67
+ ares_conn_flags_t flags;
68
+ ares_conn_state_flags_t state_flags;
69
+
70
+ /*! Outbound buffered data that is not yet sent. Exists as one contiguous
71
+ * stream in TCP format (big endian 16bit length prefix followed by DNS
72
+ * wire-format message). For TCP this can be sent as-is, UDP this must
73
+ * be sent per-packet (stripping the length prefix) */
74
+ ares_buf_t *out_buf;
75
+
76
+ /*! Inbound buffered data that is not yet parsed. Exists as one contiguous
77
+ * stream in TCP format (big endian 16bit length prefix followed by DNS
78
+ * wire-format message). TCP may have partial data and this needs to be
79
+ * handled gracefully, but UDP will always have a full message */
80
+ ares_buf_t *in_buf;
81
+
82
+ /* total number of queries run on this connection since it was established */
83
+ size_t total_queries;
84
+
85
+ /* list of outstanding queries to this connection */
86
+ ares_llist_t *queries_to_conn;
87
+ };
88
+
89
+ /*! Various buckets for grouping history */
90
+ typedef enum {
91
+ ARES_METRIC_1MINUTE = 0, /*!< Bucket for tracking over the last minute */
92
+ ARES_METRIC_15MINUTES, /*!< Bucket for tracking over the last 15 minutes */
93
+ ARES_METRIC_1HOUR, /*!< Bucket for tracking over the last hour */
94
+ ARES_METRIC_1DAY, /*!< Bucket for tracking over the last day */
95
+ ARES_METRIC_INCEPTION, /*!< Bucket for tracking since inception */
96
+ ARES_METRIC_COUNT /*!< Count of buckets, not a real bucket */
97
+ } ares_server_bucket_t;
98
+
99
+ /*! Data metrics collected for each bucket */
100
+ typedef struct {
101
+ time_t ts; /*!< Timestamp divided by bucket divisor */
102
+ unsigned int latency_min_ms; /*!< Minimum latency for queries */
103
+ unsigned int latency_max_ms; /*!< Maximum latency for queries */
104
+ ares_uint64_t total_ms; /*!< Cumulative query time for bucket */
105
+ ares_uint64_t total_count; /*!< Number of queries for bucket */
106
+
107
+ time_t prev_ts; /*!< Previous period bucket timestamp */
108
+ ares_uint64_t
109
+ prev_total_ms; /*!< Previous period bucket cumulative query time */
110
+ ares_uint64_t prev_total_count; /*!< Previous period bucket query count */
111
+ } ares_server_metrics_t;
112
+
113
+ typedef enum {
114
+ ARES_COOKIE_INITIAL = 0,
115
+ ARES_COOKIE_GENERATED = 1,
116
+ ARES_COOKIE_SUPPORTED = 2,
117
+ ARES_COOKIE_UNSUPPORTED = 3
118
+ } ares_cookie_state_t;
119
+
120
+ /*! Structure holding tracking data for RFC 7873/9018 DNS cookies.
121
+ * Implementation plan for this feature is here:
122
+ * https://github.com/c-ares/c-ares/issues/620
123
+ */
124
+ typedef struct {
125
+ /*! starts at INITIAL, transitions as needed. */
126
+ ares_cookie_state_t state;
127
+ /*! randomly-generate client cookie */
128
+ unsigned char client[8];
129
+ /*! timestamp client cookie was generated, used for rotation purposes */
130
+ ares_timeval_t client_ts;
131
+ /*! IP address last used for client to connect to server. If this changes
132
+ * The client cookie gets invalidated */
133
+ struct ares_addr client_ip;
134
+ /*! Server Cookie last received, 8-32 bytes in length */
135
+ unsigned char server[32];
136
+ /*! Length of server cookie on file. */
137
+ size_t server_len;
138
+ /*! Timestamp of last attempt to use cookies, but it was determined that the
139
+ * server didn't support them */
140
+ ares_timeval_t unsupported_ts;
141
+ } ares_cookie_t;
142
+
143
+ struct ares_server {
144
+ /* Configuration */
145
+ size_t idx; /* index for server in system configuration */
146
+ struct ares_addr addr;
147
+ unsigned short udp_port; /* host byte order */
148
+ unsigned short tcp_port; /* host byte order */
149
+ char ll_iface[64]; /* IPv6 Link Local Interface */
150
+ unsigned int ll_scope; /* IPv6 Link Local Scope */
151
+
152
+ size_t consec_failures; /* Consecutive query failure count
153
+ * can be hard errors or timeouts
154
+ */
155
+ ares_bool_t probe_pending; /* Whether a probe is pending for this
156
+ * server due to prior failures */
157
+ ares_llist_t *connections;
158
+ ares_conn_t *tcp_conn;
159
+
160
+ /* The next time when we will retry this server if it has hit failures */
161
+ ares_timeval_t next_retry_time;
162
+
163
+ /*! Buckets for collecting metrics about the server */
164
+ ares_server_metrics_t metrics[ARES_METRIC_COUNT];
165
+
166
+ /*! RFC 7873/9018 DNS Cookies */
167
+ ares_cookie_t cookie;
168
+
169
+ /* Link back to owning channel */
170
+ ares_channel_t *channel;
171
+ };
172
+
173
+ void ares_close_connection(ares_conn_t *conn, ares_status_t requeue_status);
174
+ void ares_close_sockets(ares_server_t *server);
175
+ void ares_check_cleanup_conns(const ares_channel_t *channel);
176
+
177
+ void ares_destroy_servers_state(ares_channel_t *channel);
178
+ ares_status_t ares_open_connection(ares_conn_t **conn_out,
179
+ ares_channel_t *channel,
180
+ ares_server_t *server, ares_bool_t is_tcp);
181
+
182
+ ares_conn_err_t ares_conn_write(ares_conn_t *conn, const void *data, size_t len,
183
+ size_t *written);
184
+ ares_status_t ares_conn_flush(ares_conn_t *conn);
185
+ ares_conn_err_t ares_conn_read(ares_conn_t *conn, void *data, size_t len,
186
+ size_t *read_bytes);
187
+ ares_conn_t *ares_conn_from_fd(const ares_channel_t *channel, ares_socket_t fd);
188
+ void ares_conn_sock_state_cb_update(ares_conn_t *conn,
189
+ ares_conn_state_flags_t flags);
190
+ ares_conn_err_t ares_socket_recv(ares_channel_t *channel, ares_socket_t s,
191
+ ares_bool_t is_tcp, void *data,
192
+ size_t data_len, size_t *read_bytes);
193
+ ares_conn_err_t ares_socket_recvfrom(ares_channel_t *channel, ares_socket_t s,
194
+ ares_bool_t is_tcp, void *data,
195
+ size_t data_len, int flags,
196
+ struct sockaddr *from,
197
+ ares_socklen_t *from_len,
198
+ size_t *read_bytes);
199
+
200
+ void ares_destroy_server(ares_server_t *server);
201
+
202
+ #endif
@@ -0,0 +1,461 @@
1
+ /* MIT License
2
+ *
3
+ * Copyright (c) 2024 Brad House
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice (including the next
13
+ * paragraph) shall be included in all copies or substantial portions of the
14
+ * Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ *
24
+ * SPDX-License-Identifier: MIT
25
+ */
26
+
27
+ /* DNS cookies are a simple form of learned mutual authentication supported by
28
+ * most DNS server implementations these days and can help prevent DNS Cache
29
+ * Poisoning attacks for clients and DNS amplification attacks for servers.
30
+ *
31
+ * A good overview is here:
32
+ * https://www.dotmagazine.online/issues/digital-responsibility-and-sustainability/dns-cookies-transaction-mechanism
33
+ *
34
+ * RFCs used for implementation are
35
+ * [RFC7873](https://datatracker.ietf.org/doc/html/rfc7873) which is extended by
36
+ * [RFC9018](https://datatracker.ietf.org/doc/html/rfc9018).
37
+ *
38
+ * Though this could be used on TCP, the likelihood of it being useful is small
39
+ * and could cause some issues. TCP is better used as a fallback in case there
40
+ * are issues with DNS Cookie support in the upstream servers (e.g. AnyCast
41
+ * cluster issues).
42
+ *
43
+ * While most recursive DNS servers support DNS Cookies, public DNS servers like
44
+ * Google (8.8.8.8, 8.8.4.4) and CloudFlare (1.1.1.1, 1.0.0.1) don't seem to
45
+ * have this enabled yet for unknown reasons.
46
+ *
47
+ * The risk to having DNS Cookie support always enabled is nearly zero as there
48
+ * is built-in detection support and it will simply bypass using cookies if the
49
+ * remote server doesn't support it. The problem arises if a remote server
50
+ * supports DNS cookies, then stops supporting them (such as if an administrator
51
+ * reconfigured the server, or maybe there are different servers in a cluster
52
+ * with different configurations). We need to detect this behavior by tracking
53
+ * how much time has gone by since we received our last valid cookie reply, and
54
+ * if we exceed the threshold, reset all cookie parameters like we haven't
55
+ * attempted a request yet.
56
+ *
57
+ * ## Implementation Plan
58
+ *
59
+ * ### Constants:
60
+ * - `COOKIE_CLIENT_TIMEOUT`: 86400s (1 day)
61
+ * - How often to regenerate the per-server client cookie, even if our
62
+ * source ip address hasn't changed.
63
+ * - `COOKIE_UNSUPPORTED_TIMEOUT`: 300s (5 minutes)
64
+ * - If a server responds without a cookie in the reply, this is how long to
65
+ * wait before attempting to send a client cookie again.
66
+ * - `COOKIE_REGRESSION_TIMEOUT`: 120s (2 minutes)
67
+ * - If a server was once known to return cookies, and all of a sudden stops
68
+ * returning cookies (but the reply is otherwise valid), this is how long
69
+ * to continue to attempt to use cookies before giving up and resetting.
70
+ * Such an event would cause an outage for this duration, but since a
71
+ * cache poisoning attack should be dropping invalid replies we should be
72
+ * able to still get the valid reply and not assume it is a server
73
+ * regression just because we received replies without cookies.
74
+ * - `COOKIE_RESEND_MAX`: 3
75
+ * - Maximum times to resend a query to a server due to the server responding
76
+ * with `BAD_COOKIE`, after this, we switch to TCP.
77
+ *
78
+ * ### Per-server variables:
79
+ * - `cookie.state`: Known state of cookie support, enumeration.
80
+ * - `INITIAL` (0): Initial state, not yet determined. Used during startup.
81
+ * - `GENERATED` (1): Cookie has been generated and sent to a server, but no
82
+ * validated response yet.
83
+ * - `SUPPORTED` (2): Server has been determined to properly support cookies
84
+ * - `UNSUPPORTED` (3): Server has been determined to not support cookies
85
+ * - `cookie.client` : 8 byte randomly generated client cookie
86
+ * - `cookie.client_ts`: Timestamp client cookie was generated
87
+ * - `cookie.client_ip`: IP address client used to connect to server
88
+ * - `cookie.server`: 8 to 32 byte server cookie
89
+ * - `cookie.server_len`: length of server cookie
90
+ * - `cookie.unsupported_ts`: Timestamp of last attempt to use a cookies, but
91
+ * it was determined that the server didn't support them.
92
+ *
93
+ * ### Per-query variables:
94
+ * - `query.client_cookie`: Duplicate of `cookie.client` at the point in time
95
+ * the query is put on the wire. This should be available in the
96
+ * `ares_dns_record_t` for the request for verification purposes so we don't
97
+ * actually need to duplicate this, just naming it here for the ease of
98
+ * documentation below.
99
+ * - `query.cookie_try_count`: Number of tries to send a cookie but receive
100
+ * `BAD_COOKIE` responses. Used to know when we need to switch to TCP.
101
+ *
102
+ * ### Procedure:
103
+ * **NOTE**: These steps will all be done after obtaining a connection handle as
104
+ * some of these steps depend on determining the source ip address for the
105
+ * connection.
106
+ *
107
+ * 1. If the query is not using EDNS, then **skip any remaining processing**.
108
+ * 2. If using TCP, ensure there is no EDNS cookie opt (10) set (there may have
109
+ * been if this is a resend after upgrade to TCP), then **skip any remaining
110
+ * processing**.
111
+ * 3. If `cookie.state == SUPPORTED`, `cookie.unsupported_ts` is non-zero, and
112
+ * evaluates greater than `COOKIE_REGRESSION_TIMEOUT`, then clear all cookie
113
+ * settings, set `cookie.state = INITIAL`. Continue to next step (4)
114
+ * 4. If `cookie.state == UNSUPPORTED`
115
+ * - If `cookie.unsupported_ts` evaluates less than
116
+ * `COOKIE_UNSUPPORTED_TIMEOUT`
117
+ * - Ensure there is no EDNS cookie opt (10) set (shouldn't be unless
118
+ * requester had put this themselves), then **skip any remaining
119
+ * processing** as we don't want to try to send cookies.
120
+ * - Otherwise:
121
+ * - clear all cookie settings, set `cookie.state = INITIAL`.
122
+ * - Continue to next step (5) which will send a new cookie.
123
+ * 5. If `cookie.state == INITIAL`:
124
+ * - randomly generate new `cookie.client`
125
+ * - set `cookie.client_ts` to the current timestamp.
126
+ * - set `cookie.state = GENERATED`.
127
+ * - set `cookie.client_ip` to the current source ip address.
128
+ * 6. If `cookie.state == GENERATED || cookie.state == SUPPORTED` and
129
+ * `cookie.client_ip` does not match the current source ip address:
130
+ * - clear `cookie.server`
131
+ * - randomly generate new `cookie.client`
132
+ * - set `cookie.client_ts` to the current timestamp.
133
+ * - set `cookie.client_ip` to the current source ip address.
134
+ * - do not change the `cookie.state`
135
+ * 7. If `cookie.state == SUPPORTED` and `cookie.client_ts` evaluation exceeds
136
+ * `COOKIE_CLIENT_TIMEOUT`:
137
+ * - clear `cookie.server`
138
+ * - randomly generate new `cookie.client`
139
+ * - set `cookie.client_ts` to the current timestamp.
140
+ * - set `cookie.client_ip` to the current source ip address.
141
+ * - do not change the `cookie.state`
142
+ * 8. Generate EDNS OPT record (10) for client cookie. The option value will be
143
+ * the `cookie.client` concatenated with the `cookie.server`. If there is no
144
+ * known server cookie, it will not be appended. Copy `cookie.client` to
145
+ * `query.client_cookie` to handle possible client cookie changes by other
146
+ * queries before a reply is received (technically this is in the cached
147
+ * `ares_dns_record_t` so no need to manually do this). Send request to
148
+ * server.
149
+ * 9. Evaluate response:
150
+ * 1. If invalid EDNS OPT cookie (10) length sent back in response (valid
151
+ * length is 16-40), or bad client cookie value (validate first 8 bytes
152
+ * against `query.client_cookie` not `cookie.client`), **drop response**
153
+ * as if it hadn't been received. This is likely a spoofing attack.
154
+ * Wait for valid response up to normal response timeout.
155
+ * 2. If a EDNS OPT cookie (10) server cookie is returned:
156
+ * - set `cookie.unsupported_ts` to zero and `cookie.state = SUPPORTED`.
157
+ * We can confirm this server supports cookies based on the existence
158
+ * of this record.
159
+ * - If a new EDNS OPT cookie (10) server cookie is in the response, and
160
+ * the `client.cookie` matches the `query.client_cookie` still (hasn't
161
+ * been rotated by some other parallel query), save it as
162
+ * `cookie.server`.
163
+ * 3. If dns response `rcode` is `BAD_COOKIE`:
164
+ * - Ensure a EDNS OPT cookie (10) is returned, otherwise **drop
165
+ * response**, this is completely invalid and likely an spoof of some
166
+ * sort.
167
+ * - Otherwise
168
+ * - Increment `query.cookie_try_count`
169
+ * - If `query.cookie_try_count >= COOKIE_RESEND_MAX`, set
170
+ * `query.using_tcp` to force the next attempt to use TCP.
171
+ * - **Requeue the query**, but do not increment the normal
172
+ * `try_count` as a `BAD_COOKIE` reply isn't a normal try failure.
173
+ * This should end up going all the way back to step 1 on the next
174
+ * attempt.
175
+ * 4. If EDNS OPT cookie (10) is **NOT** returned in the response:
176
+ * - If `cookie.state == SUPPORTED`
177
+ * - if `cookie.unsupported_ts` is zero, set to the current timestamp.
178
+ * - Drop the response, wait for a valid response to be returned
179
+ * - if `cookie.state == GENERATED`
180
+ * - clear all cookie settings
181
+ * - set `cookie.state = UNSUPPORTED`
182
+ * - set `cookie.unsupported_ts` to the current time
183
+ * - Accept response (state should be `UNSUPPORTED` if we're here)
184
+ */
185
+
186
+ #include "ares_private.h"
187
+
188
+ /* 1 day */
189
+ #define COOKIE_CLIENT_TIMEOUT_MS (86400 * 1000)
190
+
191
+ /* 5 minutes */
192
+ #define COOKIE_UNSUPPORTED_TIMEOUT_MS (300 * 1000)
193
+
194
+ /* 2 minutes */
195
+ #define COOKIE_REGRESSION_TIMEOUT_MS (120 * 1000)
196
+
197
+ #define COOKIE_RESEND_MAX 3
198
+
199
+ static const unsigned char *
200
+ ares_dns_cookie_fetch(const ares_dns_record_t *dnsrec, size_t *len)
201
+ {
202
+ const ares_dns_rr_t *rr = ares_dns_get_opt_rr_const(dnsrec);
203
+ const unsigned char *val = NULL;
204
+ *len = 0;
205
+
206
+ if (rr == NULL) {
207
+ return NULL;
208
+ }
209
+
210
+ if (!ares_dns_rr_get_opt_byid(rr, ARES_RR_OPT_OPTIONS, ARES_OPT_PARAM_COOKIE,
211
+ &val, len)) {
212
+ return NULL;
213
+ }
214
+
215
+ return val;
216
+ }
217
+
218
+ static ares_bool_t timeval_is_set(const ares_timeval_t *tv)
219
+ {
220
+ if (tv->sec != 0 || tv->usec != 0) {
221
+ return ARES_TRUE;
222
+ }
223
+ return ARES_FALSE;
224
+ }
225
+
226
+ static ares_bool_t timeval_expired(const ares_timeval_t *tv,
227
+ const ares_timeval_t *now,
228
+ unsigned long millsecs)
229
+ {
230
+ ares_int64_t tvdiff_ms;
231
+ ares_timeval_t tvdiff;
232
+ ares_timeval_diff(&tvdiff, tv, now);
233
+
234
+ tvdiff_ms = tvdiff.sec * 1000 + tvdiff.usec / 1000;
235
+ if (tvdiff_ms >= (ares_int64_t)millsecs) {
236
+ return ARES_TRUE;
237
+ }
238
+ return ARES_FALSE;
239
+ }
240
+
241
+ static void ares_cookie_clear(ares_cookie_t *cookie)
242
+ {
243
+ memset(cookie, 0, sizeof(*cookie));
244
+ cookie->state = ARES_COOKIE_INITIAL;
245
+ }
246
+
247
+ static void ares_cookie_generate(ares_cookie_t *cookie, ares_conn_t *conn,
248
+ const ares_timeval_t *now)
249
+ {
250
+ ares_channel_t *channel = conn->server->channel;
251
+
252
+ ares_rand_bytes(channel->rand_state, cookie->client, sizeof(cookie->client));
253
+ memcpy(&cookie->client_ts, now, sizeof(cookie->client_ts));
254
+ memcpy(&cookie->client_ip, &conn->self_ip, sizeof(cookie->client_ip));
255
+ }
256
+
257
+ static void ares_cookie_clear_server(ares_cookie_t *cookie)
258
+ {
259
+ memset(cookie->server, 0, sizeof(cookie->server));
260
+ cookie->server_len = 0;
261
+ }
262
+
263
+ static ares_bool_t ares_addr_equal(const struct ares_addr *addr1,
264
+ const struct ares_addr *addr2)
265
+ {
266
+ if (addr1->family != addr2->family) {
267
+ return ARES_FALSE;
268
+ }
269
+
270
+ switch (addr1->family) {
271
+ case AF_INET:
272
+ if (memcmp(&addr1->addr.addr4, &addr2->addr.addr4,
273
+ sizeof(addr1->addr.addr4)) == 0) {
274
+ return ARES_TRUE;
275
+ }
276
+ break;
277
+ case AF_INET6:
278
+ /* This structure is weird, and due to padding SonarCloud complains if
279
+ * you don't punch all the way down. At some point we should rework
280
+ * this structure */
281
+ if (memcmp(&addr1->addr.addr6._S6_un._S6_u8,
282
+ &addr2->addr.addr6._S6_un._S6_u8,
283
+ sizeof(addr1->addr.addr6._S6_un._S6_u8)) == 0) {
284
+ return ARES_TRUE;
285
+ }
286
+ break;
287
+ default:
288
+ break; /* LCOV_EXCL_LINE */
289
+ }
290
+
291
+ return ARES_FALSE;
292
+ }
293
+
294
+ ares_status_t ares_cookie_apply(ares_dns_record_t *dnsrec, ares_conn_t *conn,
295
+ const ares_timeval_t *now)
296
+ {
297
+ ares_server_t *server = conn->server;
298
+ ares_cookie_t *cookie = &server->cookie;
299
+ ares_dns_rr_t *rr = ares_dns_get_opt_rr(dnsrec);
300
+ unsigned char c[40];
301
+ size_t c_len;
302
+
303
+ /* If there is no OPT record, then EDNS isn't supported, and therefore
304
+ * cookies can't be supported */
305
+ if (rr == NULL) {
306
+ return ARES_SUCCESS;
307
+ }
308
+
309
+ /* No cookies on TCP, make sure we remove one if one is present */
310
+ if (conn->flags & ARES_CONN_FLAG_TCP) {
311
+ ares_dns_rr_del_opt_byid(rr, ARES_RR_OPT_OPTIONS, ARES_OPT_PARAM_COOKIE);
312
+ return ARES_SUCCESS;
313
+ }
314
+
315
+ /* Look for regression */
316
+ if (cookie->state == ARES_COOKIE_SUPPORTED &&
317
+ timeval_is_set(&cookie->unsupported_ts) &&
318
+ timeval_expired(&cookie->unsupported_ts, now,
319
+ COOKIE_REGRESSION_TIMEOUT_MS)) {
320
+ ares_cookie_clear(cookie);
321
+ }
322
+
323
+ /* Handle unsupported state */
324
+ if (cookie->state == ARES_COOKIE_UNSUPPORTED) {
325
+ /* If timer hasn't expired, just delete any possible cookie and return */
326
+ if (!timeval_expired(&cookie->unsupported_ts, now,
327
+ COOKIE_UNSUPPORTED_TIMEOUT_MS)) {
328
+ ares_dns_rr_del_opt_byid(rr, ARES_RR_OPT_OPTIONS, ARES_OPT_PARAM_COOKIE);
329
+ return ARES_SUCCESS;
330
+ }
331
+
332
+ /* We want to try to "learn" again */
333
+ ares_cookie_clear(cookie);
334
+ }
335
+
336
+ /* Generate a new cookie */
337
+ if (cookie->state == ARES_COOKIE_INITIAL) {
338
+ ares_cookie_generate(cookie, conn, now);
339
+ cookie->state = ARES_COOKIE_GENERATED;
340
+ }
341
+
342
+ /* Regenerate the cookie and clear the server cookie if the client ip has
343
+ * changed */
344
+ if ((cookie->state == ARES_COOKIE_GENERATED ||
345
+ cookie->state == ARES_COOKIE_SUPPORTED) &&
346
+ !ares_addr_equal(&conn->self_ip, &cookie->client_ip)) {
347
+ ares_cookie_clear_server(cookie);
348
+ ares_cookie_generate(cookie, conn, now);
349
+ }
350
+
351
+ /* If the client cookie has reached its maximum time, refresh it */
352
+ if (cookie->state == ARES_COOKIE_SUPPORTED &&
353
+ timeval_expired(&cookie->client_ts, now, COOKIE_CLIENT_TIMEOUT_MS)) {
354
+ ares_cookie_clear_server(cookie);
355
+ ares_cookie_generate(cookie, conn, now);
356
+ }
357
+
358
+ /* Generate the full cookie which is the client cookie concatenated with the
359
+ * server cookie (if there is one) and apply it. */
360
+ memcpy(c, cookie->client, sizeof(cookie->client));
361
+ if (cookie->server_len) {
362
+ memcpy(c + sizeof(cookie->client), cookie->server, cookie->server_len);
363
+ }
364
+ c_len = sizeof(cookie->client) + cookie->server_len;
365
+
366
+ return ares_dns_rr_set_opt(rr, ARES_RR_OPT_OPTIONS, ARES_OPT_PARAM_COOKIE, c,
367
+ c_len);
368
+ }
369
+
370
+ ares_status_t ares_cookie_validate(ares_query_t *query,
371
+ const ares_dns_record_t *dnsresp,
372
+ ares_conn_t *conn, const ares_timeval_t *now,
373
+ ares_array_t **requeue)
374
+ {
375
+ ares_server_t *server = conn->server;
376
+ ares_cookie_t *cookie = &server->cookie;
377
+ const ares_dns_record_t *dnsreq = query->query;
378
+ const unsigned char *resp_cookie;
379
+ size_t resp_cookie_len;
380
+ const unsigned char *req_cookie;
381
+ size_t req_cookie_len;
382
+
383
+ resp_cookie = ares_dns_cookie_fetch(dnsresp, &resp_cookie_len);
384
+
385
+ /* Invalid cookie length, drop */
386
+ if (resp_cookie && (resp_cookie_len < 8 || resp_cookie_len > 40)) {
387
+ return ARES_EBADRESP;
388
+ }
389
+
390
+ req_cookie = ares_dns_cookie_fetch(dnsreq, &req_cookie_len);
391
+
392
+ /* Didn't request cookies, so we can stop evaluating */
393
+ if (req_cookie == NULL) {
394
+ return ARES_SUCCESS;
395
+ }
396
+
397
+ /* If 8-byte prefix for returned cookie doesn't match the requested cookie,
398
+ * drop for spoofing */
399
+ if (resp_cookie && memcmp(req_cookie, resp_cookie, 8) != 0) {
400
+ return ARES_EBADRESP;
401
+ }
402
+
403
+ if (resp_cookie && resp_cookie_len > 8) {
404
+ /* Make sure we record that we successfully received a cookie response */
405
+ cookie->state = ARES_COOKIE_SUPPORTED;
406
+ memset(&cookie->unsupported_ts, 0, sizeof(cookie->unsupported_ts));
407
+
408
+ /* If client cookie hasn't been rotated, save the returned server cookie */
409
+ if (memcmp(cookie->client, req_cookie, sizeof(cookie->client)) == 0) {
410
+ cookie->server_len = resp_cookie_len - 8;
411
+ memcpy(cookie->server, resp_cookie + 8, cookie->server_len);
412
+ }
413
+ }
414
+
415
+ if (ares_dns_record_get_rcode(dnsresp) == ARES_RCODE_BADCOOKIE) {
416
+ /* Illegal to return BADCOOKIE but no cookie, drop */
417
+ if (resp_cookie == NULL) {
418
+ return ARES_EBADRESP;
419
+ }
420
+
421
+ /* If we have too many attempts to send a cookie, we need to requeue as
422
+ * tcp */
423
+ query->cookie_try_count++;
424
+ if (query->cookie_try_count >= COOKIE_RESEND_MAX) {
425
+ query->using_tcp = ARES_TRUE;
426
+ }
427
+
428
+ /* Resend the request, hopefully it will work the next time as we should
429
+ * have recorded a server cookie */
430
+ ares_requeue_query(query, now, ARES_SUCCESS,
431
+ ARES_FALSE /* Don't increment try count */, NULL,
432
+ requeue);
433
+
434
+ /* Parent needs to drop this response */
435
+ return ARES_EBADRESP;
436
+ }
437
+
438
+ /* We've got a response with a server cookie, and we've done all the
439
+ * evaluation we can, return success */
440
+ if (resp_cookie_len > 8) {
441
+ return ARES_SUCCESS;
442
+ }
443
+
444
+ if (cookie->state == ARES_COOKIE_SUPPORTED) {
445
+ /* If we're not currently tracking an error time yet, start */
446
+ if (!timeval_is_set(&cookie->unsupported_ts)) {
447
+ memcpy(&cookie->unsupported_ts, now, sizeof(cookie->unsupported_ts));
448
+ }
449
+ /* Drop it since we expected a cookie */
450
+ return ARES_EBADRESP;
451
+ }
452
+
453
+ if (cookie->state == ARES_COOKIE_GENERATED) {
454
+ ares_cookie_clear(cookie);
455
+ cookie->state = ARES_COOKIE_UNSUPPORTED;
456
+ memcpy(&cookie->unsupported_ts, now, sizeof(cookie->unsupported_ts));
457
+ }
458
+
459
+ /* Cookie state should be UNSUPPORTED if we're here */
460
+ return ARES_SUCCESS;
461
+ }