visaljs 0.0.10 → 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 (221) hide show
  1. package/README.md +12 -4
  2. package/THIRD_PARTY_LICENSES +31 -1
  3. package/cli.js +962 -68
  4. package/node/include/js_node.h +11 -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 +395 -86
  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/perf_histogram.js +1 -1
  31. package/node/prelude/internal/process_more.js +32 -17
  32. package/node/prelude/internal/process_signals.js +8 -2
  33. package/node/prelude/internal/punycode.ts +163 -0
  34. package/node/prelude/internal/readline.js +648 -274
  35. package/node/prelude/internal/readline_editor.js +1240 -0
  36. package/node/prelude/internal/string_width.js +43 -0
  37. package/node/prelude/internal/tls/common.js +145 -33
  38. package/node/prelude/internal/tls/wrap.js +182 -49
  39. package/node/prelude/internal/tls/x509.js +4 -1
  40. package/node/prelude/internal/udp.js +177 -0
  41. package/node/prelude/net.js +13 -1
  42. package/node/prelude/os.js +24 -6
  43. package/node/prelude/perf_hooks.js +37 -2
  44. package/node/prelude/punycode.js +2 -130
  45. package/node/prelude/readline.js +113 -4
  46. package/node/prelude/readline_promises.js +56 -32
  47. package/node/prelude/tls.js +21 -4
  48. package/node/prelude/util.js +3 -16
  49. package/node/src/host_dns.c +623 -0
  50. package/node/src/host_http.c +12 -0
  51. package/node/src/host_net.c +46 -0
  52. package/node/src/host_os.c +1 -6
  53. package/node/src/host_sys.c +70 -0
  54. package/node/src/host_udp.c +418 -0
  55. package/node/third_party/c-ares/LICENSE.md +24 -0
  56. package/node/third_party/c-ares/config/darwin/ares_config.h +573 -0
  57. package/node/third_party/c-ares/config/freebsd/ares_config.h +573 -0
  58. package/node/third_party/c-ares/config/linux/ares_config.h +573 -0
  59. package/node/third_party/c-ares/config/openbsd/ares_config.h +583 -0
  60. package/node/third_party/c-ares/include/ares.h +1237 -0
  61. package/node/third_party/c-ares/include/ares_build.h +202 -0
  62. package/node/third_party/c-ares/include/ares_dns.h +127 -0
  63. package/node/third_party/c-ares/include/ares_dns_record.h +1119 -0
  64. package/node/third_party/c-ares/include/ares_nameser.h +510 -0
  65. package/node/third_party/c-ares/include/ares_version.h +47 -0
  66. package/node/third_party/c-ares/src/lib/ares_addrinfo2hostent.c +305 -0
  67. package/node/third_party/c-ares/src/lib/ares_addrinfo_localhost.c +245 -0
  68. package/node/third_party/c-ares/src/lib/ares_android.c +496 -0
  69. package/node/third_party/c-ares/src/lib/ares_android.h +38 -0
  70. package/node/third_party/c-ares/src/lib/ares_cancel.c +87 -0
  71. package/node/third_party/c-ares/src/lib/ares_close_sockets.c +137 -0
  72. package/node/third_party/c-ares/src/lib/ares_conn.c +511 -0
  73. package/node/third_party/c-ares/src/lib/ares_conn.h +202 -0
  74. package/node/third_party/c-ares/src/lib/ares_cookie.c +461 -0
  75. package/node/third_party/c-ares/src/lib/ares_data.c +168 -0
  76. package/node/third_party/c-ares/src/lib/ares_data.h +92 -0
  77. package/node/third_party/c-ares/src/lib/ares_destroy.c +159 -0
  78. package/node/third_party/c-ares/src/lib/ares_free_hostent.c +54 -0
  79. package/node/third_party/c-ares/src/lib/ares_free_string.c +33 -0
  80. package/node/third_party/c-ares/src/lib/ares_freeaddrinfo.c +67 -0
  81. package/node/third_party/c-ares/src/lib/ares_getaddrinfo.c +806 -0
  82. package/node/third_party/c-ares/src/lib/ares_getenv.c +38 -0
  83. package/node/third_party/c-ares/src/lib/ares_getenv.h +34 -0
  84. package/node/third_party/c-ares/src/lib/ares_gethostbyaddr.c +229 -0
  85. package/node/third_party/c-ares/src/lib/ares_gethostbyname.c +341 -0
  86. package/node/third_party/c-ares/src/lib/ares_getnameinfo.c +437 -0
  87. package/node/third_party/c-ares/src/lib/ares_hosts_file.c +960 -0
  88. package/node/third_party/c-ares/src/lib/ares_inet_net_pton.h +31 -0
  89. package/node/third_party/c-ares/src/lib/ares_init.c +631 -0
  90. package/node/third_party/c-ares/src/lib/ares_ipv6.h +115 -0
  91. package/node/third_party/c-ares/src/lib/ares_library_init.c +190 -0
  92. package/node/third_party/c-ares/src/lib/ares_metrics.c +261 -0
  93. package/node/third_party/c-ares/src/lib/ares_options.c +492 -0
  94. package/node/third_party/c-ares/src/lib/ares_parse_into_addrinfo.c +179 -0
  95. package/node/third_party/c-ares/src/lib/ares_private.h +648 -0
  96. package/node/third_party/c-ares/src/lib/ares_process.c +1649 -0
  97. package/node/third_party/c-ares/src/lib/ares_qcache.c +442 -0
  98. package/node/third_party/c-ares/src/lib/ares_query.c +151 -0
  99. package/node/third_party/c-ares/src/lib/ares_search.c +623 -0
  100. package/node/third_party/c-ares/src/lib/ares_send.c +300 -0
  101. package/node/third_party/c-ares/src/lib/ares_set_socket_functions.c +590 -0
  102. package/node/third_party/c-ares/src/lib/ares_setup.h +224 -0
  103. package/node/third_party/c-ares/src/lib/ares_socket.c +427 -0
  104. package/node/third_party/c-ares/src/lib/ares_socket.h +163 -0
  105. package/node/third_party/c-ares/src/lib/ares_sortaddrinfo.c +447 -0
  106. package/node/third_party/c-ares/src/lib/ares_strerror.c +91 -0
  107. package/node/third_party/c-ares/src/lib/ares_sysconfig.c +642 -0
  108. package/node/third_party/c-ares/src/lib/ares_sysconfig_files.c +851 -0
  109. package/node/third_party/c-ares/src/lib/ares_sysconfig_mac.c +373 -0
  110. package/node/third_party/c-ares/src/lib/ares_sysconfig_win.c +673 -0
  111. package/node/third_party/c-ares/src/lib/ares_timeout.c +162 -0
  112. package/node/third_party/c-ares/src/lib/ares_update_servers.c +1376 -0
  113. package/node/third_party/c-ares/src/lib/ares_version.c +36 -0
  114. package/node/third_party/c-ares/src/lib/config-dos.h +114 -0
  115. package/node/third_party/c-ares/src/lib/config-win32.h +396 -0
  116. package/node/third_party/c-ares/src/lib/dsa/ares_array.c +409 -0
  117. package/node/third_party/c-ares/src/lib/dsa/ares_htable.c +451 -0
  118. package/node/third_party/c-ares/src/lib/dsa/ares_htable.h +174 -0
  119. package/node/third_party/c-ares/src/lib/dsa/ares_htable_asvp.c +224 -0
  120. package/node/third_party/c-ares/src/lib/dsa/ares_htable_dict.c +230 -0
  121. package/node/third_party/c-ares/src/lib/dsa/ares_htable_strvp.c +210 -0
  122. package/node/third_party/c-ares/src/lib/dsa/ares_htable_szvp.c +188 -0
  123. package/node/third_party/c-ares/src/lib/dsa/ares_htable_vpstr.c +186 -0
  124. package/node/third_party/c-ares/src/lib/dsa/ares_htable_vpvp.c +194 -0
  125. package/node/third_party/c-ares/src/lib/dsa/ares_llist.c +385 -0
  126. package/node/third_party/c-ares/src/lib/dsa/ares_slist.c +479 -0
  127. package/node/third_party/c-ares/src/lib/dsa/ares_slist.h +207 -0
  128. package/node/third_party/c-ares/src/lib/event/ares_event.h +191 -0
  129. package/node/third_party/c-ares/src/lib/event/ares_event_configchg.c +746 -0
  130. package/node/third_party/c-ares/src/lib/event/ares_event_epoll.c +192 -0
  131. package/node/third_party/c-ares/src/lib/event/ares_event_kqueue.c +248 -0
  132. package/node/third_party/c-ares/src/lib/event/ares_event_poll.c +140 -0
  133. package/node/third_party/c-ares/src/lib/event/ares_event_select.c +159 -0
  134. package/node/third_party/c-ares/src/lib/event/ares_event_thread.c +575 -0
  135. package/node/third_party/c-ares/src/lib/event/ares_event_wake_pipe.c +172 -0
  136. package/node/third_party/c-ares/src/lib/event/ares_event_win32.c +981 -0
  137. package/node/third_party/c-ares/src/lib/event/ares_event_win32.h +161 -0
  138. package/node/third_party/c-ares/src/lib/include/ares_array.h +276 -0
  139. package/node/third_party/c-ares/src/lib/include/ares_buf.h +732 -0
  140. package/node/third_party/c-ares/src/lib/include/ares_htable_asvp.h +130 -0
  141. package/node/third_party/c-ares/src/lib/include/ares_htable_dict.h +123 -0
  142. package/node/third_party/c-ares/src/lib/include/ares_htable_strvp.h +130 -0
  143. package/node/third_party/c-ares/src/lib/include/ares_htable_szvp.h +118 -0
  144. package/node/third_party/c-ares/src/lib/include/ares_htable_vpstr.h +111 -0
  145. package/node/third_party/c-ares/src/lib/include/ares_htable_vpvp.h +128 -0
  146. package/node/third_party/c-ares/src/lib/include/ares_llist.h +239 -0
  147. package/node/third_party/c-ares/src/lib/include/ares_mem.h +41 -0
  148. package/node/third_party/c-ares/src/lib/include/ares_str.h +261 -0
  149. package/node/third_party/c-ares/src/lib/inet_net_pton.c +451 -0
  150. package/node/third_party/c-ares/src/lib/inet_ntop.c +219 -0
  151. package/node/third_party/c-ares/src/lib/legacy/ares_create_query.c +78 -0
  152. package/node/third_party/c-ares/src/lib/legacy/ares_expand_name.c +99 -0
  153. package/node/third_party/c-ares/src/lib/legacy/ares_expand_string.c +107 -0
  154. package/node/third_party/c-ares/src/lib/legacy/ares_fds.c +80 -0
  155. package/node/third_party/c-ares/src/lib/legacy/ares_getsock.c +85 -0
  156. package/node/third_party/c-ares/src/lib/legacy/ares_parse_a_reply.c +107 -0
  157. package/node/third_party/c-ares/src/lib/legacy/ares_parse_aaaa_reply.c +109 -0
  158. package/node/third_party/c-ares/src/lib/legacy/ares_parse_caa_reply.c +137 -0
  159. package/node/third_party/c-ares/src/lib/legacy/ares_parse_mx_reply.c +110 -0
  160. package/node/third_party/c-ares/src/lib/legacy/ares_parse_naptr_reply.c +132 -0
  161. package/node/third_party/c-ares/src/lib/legacy/ares_parse_ns_reply.c +154 -0
  162. package/node/third_party/c-ares/src/lib/legacy/ares_parse_ptr_reply.c +213 -0
  163. package/node/third_party/c-ares/src/lib/legacy/ares_parse_soa_reply.c +115 -0
  164. package/node/third_party/c-ares/src/lib/legacy/ares_parse_srv_reply.c +114 -0
  165. package/node/third_party/c-ares/src/lib/legacy/ares_parse_txt_reply.c +144 -0
  166. package/node/third_party/c-ares/src/lib/legacy/ares_parse_uri_reply.c +113 -0
  167. package/node/third_party/c-ares/src/lib/record/ares_dns_mapping.c +982 -0
  168. package/node/third_party/c-ares/src/lib/record/ares_dns_multistring.c +309 -0
  169. package/node/third_party/c-ares/src/lib/record/ares_dns_multistring.h +72 -0
  170. package/node/third_party/c-ares/src/lib/record/ares_dns_name.c +736 -0
  171. package/node/third_party/c-ares/src/lib/record/ares_dns_parse.c +1360 -0
  172. package/node/third_party/c-ares/src/lib/record/ares_dns_private.h +273 -0
  173. package/node/third_party/c-ares/src/lib/record/ares_dns_record.c +1701 -0
  174. package/node/third_party/c-ares/src/lib/record/ares_dns_write.c +1229 -0
  175. package/node/third_party/c-ares/src/lib/str/ares_buf.c +1510 -0
  176. package/node/third_party/c-ares/src/lib/str/ares_str.c +562 -0
  177. package/node/third_party/c-ares/src/lib/str/ares_strsplit.c +90 -0
  178. package/node/third_party/c-ares/src/lib/str/ares_strsplit.h +51 -0
  179. package/node/third_party/c-ares/src/lib/thirdparty/apple/dnsinfo.h +122 -0
  180. package/node/third_party/c-ares/src/lib/util/ares_iface_ips.c +640 -0
  181. package/node/third_party/c-ares/src/lib/util/ares_iface_ips.h +140 -0
  182. package/node/third_party/c-ares/src/lib/util/ares_math.c +167 -0
  183. package/node/third_party/c-ares/src/lib/util/ares_math.h +58 -0
  184. package/node/third_party/c-ares/src/lib/util/ares_rand.c +389 -0
  185. package/node/third_party/c-ares/src/lib/util/ares_rand.h +36 -0
  186. package/node/third_party/c-ares/src/lib/util/ares_threads.c +613 -0
  187. package/node/third_party/c-ares/src/lib/util/ares_threads.h +60 -0
  188. package/node/third_party/c-ares/src/lib/util/ares_time.h +49 -0
  189. package/node/third_party/c-ares/src/lib/util/ares_timeval.c +95 -0
  190. package/node/third_party/c-ares/src/lib/util/ares_uri.c +1627 -0
  191. package/node/third_party/c-ares/src/lib/util/ares_uri.h +252 -0
  192. package/node/third_party/c-ares/src/lib/windows_port.c +29 -0
  193. package/package.json +1 -1
  194. package/prebuilt/darwin-arm64/cares-a6286c65ca9374a3.a +0 -0
  195. package/prebuilt/darwin-x64/cares-a6286c65ca9374a3.a +0 -0
  196. package/prebuilt/linux-arm64/cares-4ba05ac8e948af8e.a +0 -0
  197. package/prebuilt/linux-arm64-musl/cares-4ba05ac8e948af8e.a +0 -0
  198. package/prebuilt/linux-x64/cares-4ba05ac8e948af8e.a +0 -0
  199. package/prebuilt/linux-x64-musl/cares-4ba05ac8e948af8e.a +0 -0
  200. package/prebuilt/win32-x64/boringssl-ce573858472b7fc9.a +0 -0
  201. package/prebuilt/win32-x64/brotli-20d86a61f30baeb3.a +0 -0
  202. package/prebuilt/win32-x64/cares-49c9ec95162b912d.a +0 -0
  203. package/prebuilt/win32-x64/uv-b11bae65d7fced82.a +0 -0
  204. package/prebuilt/win32-x64/zlib-4ce2317a4c3f5fb5.a +0 -0
  205. package/prebuilt/win32-x64/zstd-015315ac7c784c73.a +0 -0
  206. package/runtime/include/js_builtins.h +5 -0
  207. package/runtime/include/js_crypto.h +11 -0
  208. package/runtime/include/js_gc.h +13 -10
  209. package/runtime/include/js_jobs.h +2 -0
  210. package/runtime/include/js_object.h +58 -0
  211. package/runtime/include/jscontext.h +1 -0
  212. package/runtime/src/crypto_internal.h +1 -0
  213. package/runtime/src/crypto_subtle.c +66 -7
  214. package/runtime/src/crypto_tls.c +265 -19
  215. package/runtime/src/gc.c +73 -92
  216. package/runtime/src/inspect.c +18 -1
  217. package/runtime/src/jobs.c +37 -22
  218. package/runtime/src/object.c +49 -2
  219. package/runtime/src/os.c +16 -6
  220. package/runtime/src/os.h +2 -0
  221. package/runtime/src/process.c +97 -10
@@ -0,0 +1,442 @@
1
+ /* MIT License
2
+ *
3
+ * Copyright (c) 2023 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
+ #include "ares_private.h"
27
+
28
+ struct ares_qcache {
29
+ ares_htable_strvp_t *cache;
30
+ ares_slist_t *expire;
31
+ unsigned int max_ttl;
32
+ };
33
+
34
+ typedef struct {
35
+ char *key;
36
+ ares_dns_record_t *dnsrec;
37
+ time_t expire_ts;
38
+ time_t insert_ts;
39
+ } ares_qcache_entry_t;
40
+
41
+ static char *ares_qcache_calc_key(const ares_dns_record_t *dnsrec)
42
+ {
43
+ ares_buf_t *buf = ares_buf_create();
44
+ size_t i;
45
+ ares_status_t status;
46
+ ares_dns_flags_t flags;
47
+
48
+ if (dnsrec == NULL || buf == NULL) {
49
+ return NULL; /* LCOV_EXCL_LINE: DefensiveCoding */
50
+ }
51
+
52
+ /* Format is OPCODE|FLAGS[|QTYPE1|QCLASS1|QNAME1]... */
53
+
54
+ status = ares_buf_append_str(
55
+ buf, ares_dns_opcode_tostr(ares_dns_record_get_opcode(dnsrec)));
56
+ if (status != ARES_SUCCESS) {
57
+ goto fail; /* LCOV_EXCL_LINE: OutOfMemory */
58
+ }
59
+
60
+ status = ares_buf_append_byte(buf, '|');
61
+ if (status != ARES_SUCCESS) {
62
+ goto fail; /* LCOV_EXCL_LINE: OutOfMemory */
63
+ }
64
+
65
+ flags = ares_dns_record_get_flags(dnsrec);
66
+ /* Only care about RD and CD */
67
+ if (flags & ARES_FLAG_RD) {
68
+ status = ares_buf_append_str(buf, "rd");
69
+ if (status != ARES_SUCCESS) {
70
+ goto fail; /* LCOV_EXCL_LINE: OutOfMemory */
71
+ }
72
+ }
73
+ if (flags & ARES_FLAG_CD) {
74
+ status = ares_buf_append_str(buf, "cd");
75
+ if (status != ARES_SUCCESS) {
76
+ goto fail; /* LCOV_EXCL_LINE: OutOfMemory */
77
+ }
78
+ }
79
+
80
+ for (i = 0; i < ares_dns_record_query_cnt(dnsrec); i++) {
81
+ const char *name;
82
+ size_t name_len;
83
+ ares_dns_rec_type_t qtype;
84
+ ares_dns_class_t qclass;
85
+
86
+ status = ares_dns_record_query_get(dnsrec, i, &name, &qtype, &qclass);
87
+ if (status != ARES_SUCCESS) {
88
+ goto fail; /* LCOV_EXCL_LINE: DefensiveCoding */
89
+ }
90
+
91
+ status = ares_buf_append_byte(buf, '|');
92
+ if (status != ARES_SUCCESS) {
93
+ goto fail; /* LCOV_EXCL_LINE: OutOfMemory */
94
+ }
95
+
96
+ status = ares_buf_append_str(buf, ares_dns_rec_type_tostr(qtype));
97
+ if (status != ARES_SUCCESS) {
98
+ goto fail; /* LCOV_EXCL_LINE: OutOfMemory */
99
+ }
100
+
101
+ status = ares_buf_append_byte(buf, '|');
102
+ if (status != ARES_SUCCESS) {
103
+ goto fail; /* LCOV_EXCL_LINE: OutOfMemory */
104
+ }
105
+
106
+ status = ares_buf_append_str(buf, ares_dns_class_tostr(qclass));
107
+ if (status != ARES_SUCCESS) {
108
+ goto fail; /* LCOV_EXCL_LINE: OutOfMemory */
109
+ }
110
+
111
+ status = ares_buf_append_byte(buf, '|');
112
+ if (status != ARES_SUCCESS) {
113
+ goto fail; /* LCOV_EXCL_LINE: OutOfMemory */
114
+ }
115
+
116
+ /* On queries, a '.' may be appended to the name to indicate an explicit
117
+ * name lookup without performing a search. Strip this since its not part
118
+ * of a cached response. */
119
+ name_len = ares_strlen(name);
120
+ if (name_len && name[name_len - 1] == '.') {
121
+ name_len--;
122
+ }
123
+
124
+ if (name_len > 0) {
125
+ status = ares_buf_append(buf, (const unsigned char *)name, name_len);
126
+ if (status != ARES_SUCCESS) {
127
+ goto fail; /* LCOV_EXCL_LINE: OutOfMemory */
128
+ }
129
+ }
130
+ }
131
+
132
+ return ares_buf_finish_str(buf, NULL);
133
+
134
+ /* LCOV_EXCL_START: OutOfMemory */
135
+ fail:
136
+ ares_buf_destroy(buf);
137
+ return NULL;
138
+ /* LCOV_EXCL_STOP */
139
+ }
140
+
141
+ static void ares_qcache_expire(ares_qcache_t *cache, const ares_timeval_t *now)
142
+ {
143
+ ares_slist_node_t *node;
144
+
145
+ if (cache == NULL) {
146
+ return;
147
+ }
148
+
149
+ while ((node = ares_slist_node_first(cache->expire)) != NULL) {
150
+ const ares_qcache_entry_t *entry = ares_slist_node_val(node);
151
+
152
+ /* If now is NULL, we're flushing everything, so don't break */
153
+ if (now != NULL && entry->expire_ts > now->sec) {
154
+ break;
155
+ }
156
+
157
+ ares_htable_strvp_remove(cache->cache, entry->key);
158
+ ares_slist_node_destroy(node);
159
+ }
160
+ }
161
+
162
+ void ares_qcache_flush(ares_qcache_t *cache)
163
+ {
164
+ ares_qcache_expire(cache, NULL /* flush all */);
165
+ }
166
+
167
+ void ares_qcache_destroy(ares_qcache_t *cache)
168
+ {
169
+ if (cache == NULL) {
170
+ return;
171
+ }
172
+
173
+ ares_htable_strvp_destroy(cache->cache);
174
+ ares_slist_destroy(cache->expire);
175
+ ares_free(cache);
176
+ }
177
+
178
+ static int ares_qcache_entry_sort_cb(const void *arg1, const void *arg2)
179
+ {
180
+ const ares_qcache_entry_t *entry1 = arg1;
181
+ const ares_qcache_entry_t *entry2 = arg2;
182
+
183
+ if (entry1->expire_ts > entry2->expire_ts) {
184
+ return 1;
185
+ }
186
+
187
+ if (entry1->expire_ts < entry2->expire_ts) {
188
+ return -1;
189
+ }
190
+
191
+ return 0;
192
+ }
193
+
194
+ static void ares_qcache_entry_destroy_cb(void *arg)
195
+ {
196
+ ares_qcache_entry_t *entry = arg;
197
+ if (entry == NULL) {
198
+ return; /* LCOV_EXCL_LINE: DefensiveCoding */
199
+ }
200
+
201
+ ares_free(entry->key);
202
+ ares_dns_record_destroy(entry->dnsrec);
203
+ ares_free(entry);
204
+ }
205
+
206
+ ares_status_t ares_qcache_create(ares_rand_state *rand_state,
207
+ unsigned int max_ttl,
208
+ ares_qcache_t **cache_out)
209
+ {
210
+ ares_status_t status = ARES_SUCCESS;
211
+ ares_qcache_t *cache;
212
+
213
+ cache = ares_malloc_zero(sizeof(*cache));
214
+ if (cache == NULL) {
215
+ status = ARES_ENOMEM; /* LCOV_EXCL_LINE: OutOfMemory */
216
+ goto done; /* LCOV_EXCL_LINE: OutOfMemory */
217
+ }
218
+
219
+ cache->cache = ares_htable_strvp_create(NULL);
220
+ if (cache->cache == NULL) {
221
+ status = ARES_ENOMEM; /* LCOV_EXCL_LINE: OutOfMemory */
222
+ goto done; /* LCOV_EXCL_LINE: OutOfMemory */
223
+ }
224
+
225
+ cache->expire = ares_slist_create(rand_state, ares_qcache_entry_sort_cb,
226
+ ares_qcache_entry_destroy_cb);
227
+ if (cache->expire == NULL) {
228
+ status = ARES_ENOMEM; /* LCOV_EXCL_LINE: OutOfMemory */
229
+ goto done; /* LCOV_EXCL_LINE: OutOfMemory */
230
+ }
231
+
232
+ cache->max_ttl = max_ttl;
233
+
234
+ done:
235
+ if (status != ARES_SUCCESS) {
236
+ *cache_out = NULL;
237
+ ares_qcache_destroy(cache);
238
+ return status;
239
+ }
240
+
241
+ *cache_out = cache;
242
+ return status;
243
+ }
244
+
245
+ static unsigned int ares_qcache_calc_minttl(ares_dns_record_t *dnsrec)
246
+ {
247
+ unsigned int minttl = 0xFFFFFFFF;
248
+ size_t sect;
249
+
250
+ for (sect = ARES_SECTION_ANSWER; sect <= ARES_SECTION_ADDITIONAL; sect++) {
251
+ size_t i;
252
+ for (i = 0; i < ares_dns_record_rr_cnt(dnsrec, (ares_dns_section_t)sect);
253
+ i++) {
254
+ const ares_dns_rr_t *rr =
255
+ ares_dns_record_rr_get(dnsrec, (ares_dns_section_t)sect, i);
256
+ ares_dns_rec_type_t type = ares_dns_rr_get_type(rr);
257
+ unsigned int ttl = ares_dns_rr_get_ttl(rr);
258
+
259
+ /* TTL is meaningless on these record types */
260
+ if (type == ARES_REC_TYPE_OPT || type == ARES_REC_TYPE_SOA ||
261
+ type == ARES_REC_TYPE_SIG) {
262
+ continue;
263
+ }
264
+
265
+ if (ttl < minttl) {
266
+ minttl = ttl;
267
+ }
268
+ }
269
+ }
270
+
271
+ return minttl;
272
+ }
273
+
274
+ static unsigned int ares_qcache_soa_minimum(ares_dns_record_t *dnsrec)
275
+ {
276
+ size_t i;
277
+
278
+ /* RFC 2308 Section 5 says its the minimum of MINIMUM and the TTL of the
279
+ * record. */
280
+ for (i = 0; i < ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_AUTHORITY); i++) {
281
+ const ares_dns_rr_t *rr =
282
+ ares_dns_record_rr_get(dnsrec, ARES_SECTION_AUTHORITY, i);
283
+ ares_dns_rec_type_t type = ares_dns_rr_get_type(rr);
284
+ unsigned int ttl;
285
+ unsigned int minimum;
286
+
287
+ if (type != ARES_REC_TYPE_SOA) {
288
+ continue;
289
+ }
290
+
291
+ minimum = ares_dns_rr_get_u32(rr, ARES_RR_SOA_MINIMUM);
292
+ ttl = ares_dns_rr_get_ttl(rr);
293
+
294
+ if (ttl > minimum) {
295
+ return minimum;
296
+ }
297
+ return ttl;
298
+ }
299
+
300
+ return 0;
301
+ }
302
+
303
+ /* On success, takes ownership of dnsrec */
304
+ static ares_status_t ares_qcache_insert_int(ares_qcache_t *qcache,
305
+ ares_dns_record_t *qresp,
306
+ const ares_dns_record_t *qreq,
307
+ const ares_timeval_t *now)
308
+ {
309
+ ares_qcache_entry_t *entry;
310
+ unsigned int ttl;
311
+ ares_dns_rcode_t rcode = ares_dns_record_get_rcode(qresp);
312
+ ares_dns_flags_t flags = ares_dns_record_get_flags(qresp);
313
+
314
+ if (qcache == NULL || qresp == NULL) {
315
+ return ARES_EFORMERR;
316
+ }
317
+
318
+ /* Only save NOERROR or NXDOMAIN */
319
+ if (rcode != ARES_RCODE_NOERROR && rcode != ARES_RCODE_NXDOMAIN) {
320
+ return ARES_ENOTIMP;
321
+ }
322
+
323
+ /* Don't save truncated queries */
324
+ if (flags & ARES_FLAG_TC) {
325
+ return ARES_ENOTIMP;
326
+ }
327
+
328
+ /* Look at SOA for NXDOMAIN for minimum */
329
+ if (rcode == ARES_RCODE_NXDOMAIN) {
330
+ ttl = ares_qcache_soa_minimum(qresp);
331
+ } else {
332
+ ttl = ares_qcache_calc_minttl(qresp);
333
+ }
334
+
335
+ if (ttl > qcache->max_ttl) {
336
+ ttl = qcache->max_ttl;
337
+ }
338
+
339
+ /* Don't cache something that is already expired */
340
+ if (ttl == 0) {
341
+ return ARES_EREFUSED;
342
+ }
343
+
344
+ entry = ares_malloc_zero(sizeof(*entry));
345
+ if (entry == NULL) {
346
+ goto fail; /* LCOV_EXCL_LINE: OutOfMemory */
347
+ }
348
+
349
+ entry->dnsrec = qresp;
350
+ entry->expire_ts = (time_t)now->sec + (time_t)ttl;
351
+ entry->insert_ts = (time_t)now->sec;
352
+
353
+ /* We can't guarantee the server responded with the same flags as the
354
+ * request had, so we have to re-parse the request in order to generate the
355
+ * key for caching, but we'll only do this once we know for sure we really
356
+ * want to cache it */
357
+ entry->key = ares_qcache_calc_key(qreq);
358
+ if (entry->key == NULL) {
359
+ goto fail; /* LCOV_EXCL_LINE: OutOfMemory */
360
+ }
361
+
362
+ if (!ares_htable_strvp_insert(qcache->cache, entry->key, entry)) {
363
+ goto fail; /* LCOV_EXCL_LINE: OutOfMemory */
364
+ }
365
+
366
+ if (ares_slist_insert(qcache->expire, entry) == NULL) {
367
+ goto fail; /* LCOV_EXCL_LINE: OutOfMemory */
368
+ }
369
+
370
+ return ARES_SUCCESS;
371
+
372
+ /* LCOV_EXCL_START: OutOfMemory */
373
+ fail:
374
+ if (entry != NULL) {
375
+ if (entry->key != NULL) {
376
+ ares_htable_strvp_remove(qcache->cache, entry->key);
377
+ ares_free(entry->key);
378
+ }
379
+ ares_free(entry);
380
+ }
381
+ return ARES_ENOMEM;
382
+ /* LCOV_EXCL_STOP */
383
+ }
384
+
385
+ ares_status_t ares_qcache_fetch(ares_channel_t *channel,
386
+ const ares_timeval_t *now,
387
+ const ares_dns_record_t *dnsrec,
388
+ const ares_dns_record_t **dnsrec_resp)
389
+ {
390
+ char *key = NULL;
391
+ ares_qcache_entry_t *entry;
392
+ ares_status_t status = ARES_SUCCESS;
393
+
394
+ if (channel == NULL || dnsrec == NULL || dnsrec_resp == NULL) {
395
+ return ARES_EFORMERR;
396
+ }
397
+
398
+ if (channel->qcache == NULL) {
399
+ return ARES_ENOTFOUND;
400
+ }
401
+
402
+ ares_qcache_expire(channel->qcache, now);
403
+
404
+ key = ares_qcache_calc_key(dnsrec);
405
+ if (key == NULL) {
406
+ status = ARES_ENOMEM; /* LCOV_EXCL_LINE: OutOfMemory */
407
+ goto done; /* LCOV_EXCL_LINE: OutOfMemory */
408
+ }
409
+
410
+ entry = ares_htable_strvp_get_direct(channel->qcache->cache, key);
411
+ if (entry == NULL) {
412
+ status = ARES_ENOTFOUND;
413
+ goto done;
414
+ }
415
+
416
+ ares_dns_record_ttl_decrement(entry->dnsrec,
417
+ (unsigned int)(now->sec - entry->insert_ts));
418
+
419
+ *dnsrec_resp = entry->dnsrec;
420
+
421
+ done:
422
+ ares_free(key);
423
+ return status;
424
+ }
425
+
426
+ ares_status_t ares_qcache_insert(ares_channel_t *channel,
427
+ const ares_timeval_t *now,
428
+ const ares_query_t *query,
429
+ const ares_dns_record_t *dnsrec)
430
+ {
431
+ ares_dns_record_t *dupdns = ares_dns_record_duplicate(dnsrec);
432
+ ares_status_t status;
433
+
434
+ if (dupdns == NULL) {
435
+ return ARES_ENOMEM;
436
+ }
437
+ status = ares_qcache_insert_int(channel->qcache, dupdns, query->query, now);
438
+ if (status != ARES_SUCCESS) {
439
+ ares_dns_record_destroy(dupdns);
440
+ }
441
+ return status;
442
+ }
@@ -0,0 +1,151 @@
1
+ /* MIT License
2
+ *
3
+ * Copyright (c) 1998 Massachusetts Institute of Technology
4
+ * Copyright (c) The c-ares project and its contributors
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice (including the next
14
+ * paragraph) shall be included in all copies or substantial portions of the
15
+ * Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ * SOFTWARE.
24
+ *
25
+ * SPDX-License-Identifier: MIT
26
+ */
27
+
28
+ #include "ares_private.h"
29
+
30
+ #ifdef HAVE_NETINET_IN_H
31
+ # include <netinet/in.h>
32
+ #endif
33
+
34
+ typedef struct {
35
+ ares_callback_dnsrec callback;
36
+ void *arg;
37
+ } ares_query_dnsrec_arg_t;
38
+
39
+ static void ares_query_dnsrec_cb(void *arg, ares_status_t status,
40
+ size_t timeouts,
41
+ const ares_dns_record_t *dnsrec)
42
+ {
43
+ ares_query_dnsrec_arg_t *qquery = arg;
44
+
45
+ if (status != ARES_SUCCESS) {
46
+ qquery->callback(qquery->arg, status, timeouts, dnsrec);
47
+ } else {
48
+ size_t ancount;
49
+ ares_dns_rcode_t rcode;
50
+ /* Pull the response code and answer count from the packet and convert any
51
+ * errors.
52
+ */
53
+ rcode = ares_dns_record_get_rcode(dnsrec);
54
+ ancount = ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ANSWER);
55
+ status = ares_dns_query_reply_tostatus(rcode, ancount);
56
+ qquery->callback(qquery->arg, status, timeouts, dnsrec);
57
+ }
58
+ ares_free(qquery);
59
+ }
60
+
61
+ ares_status_t ares_query_nolock(ares_channel_t *channel, const char *name,
62
+ ares_dns_class_t dnsclass,
63
+ ares_dns_rec_type_t type,
64
+ ares_callback_dnsrec callback, void *arg,
65
+ unsigned short *qid)
66
+ {
67
+ ares_status_t status;
68
+ ares_dns_record_t *dnsrec = NULL;
69
+ ares_dns_flags_t flags = 0;
70
+ ares_query_dnsrec_arg_t *qquery = NULL;
71
+
72
+ if (channel == NULL || name == NULL || callback == NULL) {
73
+ /* LCOV_EXCL_START: DefensiveCoding */
74
+ status = ARES_EFORMERR;
75
+ if (callback != NULL) {
76
+ callback(arg, status, 0, NULL);
77
+ }
78
+ return status;
79
+ /* LCOV_EXCL_STOP */
80
+ }
81
+
82
+ if (!(channel->flags & ARES_FLAG_NORECURSE)) {
83
+ flags |= ARES_FLAG_RD;
84
+ }
85
+
86
+ status = ares_dns_record_create_query(
87
+ &dnsrec, name, dnsclass, type, 0, flags,
88
+ (size_t)(channel->flags & ARES_FLAG_EDNS) ? channel->ednspsz : 0);
89
+ if (status != ARES_SUCCESS) {
90
+ callback(arg, status, 0, NULL); /* LCOV_EXCL_LINE: OutOfMemory */
91
+ return status; /* LCOV_EXCL_LINE: OutOfMemory */
92
+ }
93
+
94
+ qquery = ares_malloc(sizeof(*qquery));
95
+ if (qquery == NULL) {
96
+ /* LCOV_EXCL_START: OutOfMemory */
97
+ status = ARES_ENOMEM;
98
+ callback(arg, status, 0, NULL);
99
+ ares_dns_record_destroy(dnsrec);
100
+ return status;
101
+ /* LCOV_EXCL_STOP */
102
+ }
103
+
104
+ qquery->callback = callback;
105
+ qquery->arg = arg;
106
+
107
+ /* Send it off. qcallback will be called when we get an answer. */
108
+ status = ares_send_nolock(channel, NULL, 0, dnsrec, ares_query_dnsrec_cb,
109
+ qquery, qid);
110
+
111
+ ares_dns_record_destroy(dnsrec);
112
+ return status;
113
+ }
114
+
115
+ ares_status_t ares_query_dnsrec(ares_channel_t *channel, const char *name,
116
+ ares_dns_class_t dnsclass,
117
+ ares_dns_rec_type_t type,
118
+ ares_callback_dnsrec callback, void *arg,
119
+ unsigned short *qid)
120
+ {
121
+ ares_status_t status;
122
+
123
+ if (channel == NULL) {
124
+ return ARES_EFORMERR;
125
+ }
126
+
127
+ ares_channel_lock(channel);
128
+ status = ares_query_nolock(channel, name, dnsclass, type, callback, arg, qid);
129
+ ares_channel_unlock(channel);
130
+ return status;
131
+ }
132
+
133
+ void ares_query(ares_channel_t *channel, const char *name, int dnsclass,
134
+ int type, ares_callback callback, void *arg)
135
+ {
136
+ void *carg = NULL;
137
+
138
+ if (channel == NULL) {
139
+ return;
140
+ }
141
+
142
+ carg = ares_dnsrec_convert_arg(callback, arg);
143
+ if (carg == NULL) {
144
+ callback(arg, ARES_ENOMEM, 0, NULL, 0); /* LCOV_EXCL_LINE: OutOfMemory */
145
+ return; /* LCOV_EXCL_LINE: OutOfMemory */
146
+ }
147
+
148
+ ares_query_dnsrec(channel, name, (ares_dns_class_t)dnsclass,
149
+ (ares_dns_rec_type_t)type, ares_dnsrec_convert_cb, carg,
150
+ NULL);
151
+ }