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,746 @@
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
+ #include "ares_private.h"
27
+ #include "ares_event.h"
28
+
29
+ #if defined(__ANDROID__) && defined(CARES_THREADS)
30
+
31
+ ares_status_t ares_event_configchg_init(ares_event_configchg_t **configchg,
32
+ ares_event_thread_t *e)
33
+ {
34
+ (void)configchg;
35
+ (void)e;
36
+ /* No ability */
37
+ return ARES_ENOTIMP;
38
+ }
39
+
40
+ void ares_event_configchg_destroy(ares_event_configchg_t *configchg)
41
+ {
42
+ /* No-op */
43
+ (void)configchg;
44
+ }
45
+
46
+ #elif defined(__linux__) && defined(CARES_THREADS)
47
+
48
+ # include <sys/inotify.h>
49
+
50
+ struct ares_event_configchg {
51
+ int inotify_fd;
52
+ ares_event_thread_t *e;
53
+ };
54
+
55
+ void ares_event_configchg_destroy(ares_event_configchg_t *configchg)
56
+ {
57
+ if (configchg == NULL) {
58
+ return; /* LCOV_EXCL_LINE: DefensiveCoding */
59
+ }
60
+
61
+ /* Tell event system to stop monitoring for changes. This will cause the
62
+ * cleanup to be called */
63
+ ares_event_update(NULL, configchg->e, ARES_EVENT_FLAG_NONE, NULL,
64
+ configchg->inotify_fd, NULL, NULL, NULL);
65
+ }
66
+
67
+ static void ares_event_configchg_free(void *data)
68
+ {
69
+ ares_event_configchg_t *configchg = data;
70
+ if (configchg == NULL) {
71
+ return; /* LCOV_EXCL_LINE: DefensiveCoding */
72
+ }
73
+
74
+ if (configchg->inotify_fd >= 0) {
75
+ close(configchg->inotify_fd);
76
+ configchg->inotify_fd = -1;
77
+ }
78
+
79
+ ares_free(configchg);
80
+ }
81
+
82
+ static void ares_event_configchg_cb(ares_event_thread_t *e, ares_socket_t fd,
83
+ void *data, ares_event_flags_t flags)
84
+ {
85
+ const ares_event_configchg_t *configchg = data;
86
+
87
+ /* Some systems cannot read integer variables if they are not
88
+ * properly aligned. On other systems, incorrect alignment may
89
+ * decrease performance. Hence, the buffer used for reading from
90
+ * the inotify file descriptor should have the same alignment as
91
+ * struct inotify_event. */
92
+ unsigned char buf[4096]
93
+ __attribute__((aligned(__alignof__(struct inotify_event))));
94
+ const struct inotify_event *event;
95
+ ssize_t len;
96
+ ares_bool_t triggered = ARES_FALSE;
97
+
98
+ (void)fd;
99
+ (void)flags;
100
+
101
+ while (1) {
102
+ const unsigned char *ptr;
103
+
104
+ len = read(configchg->inotify_fd, buf, sizeof(buf));
105
+ if (len <= 0) {
106
+ break;
107
+ }
108
+
109
+ /* Loop over all events in the buffer. Says kernel will check the buffer
110
+ * size provided, so I assume it won't ever return partial events. */
111
+ for (ptr = buf; ptr < buf + len;
112
+ ptr += sizeof(struct inotify_event) + event->len) {
113
+ event = (const struct inotify_event *)((const void *)ptr);
114
+
115
+ if (event->len == 0 || ares_strlen(event->name) == 0) {
116
+ continue;
117
+ }
118
+
119
+ if (ares_strcaseeq(event->name, "resolv.conf") ||
120
+ ares_strcaseeq(event->name, "nsswitch.conf")) {
121
+ triggered = ARES_TRUE;
122
+ }
123
+ }
124
+ }
125
+
126
+ /* Only process after all events are read. No need to process more often as
127
+ * we don't want to reload the config back to back */
128
+ if (triggered) {
129
+ ares_reinit(e->channel);
130
+ }
131
+ }
132
+
133
+ ares_status_t ares_event_configchg_init(ares_event_configchg_t **configchg,
134
+ ares_event_thread_t *e)
135
+ {
136
+ ares_status_t status = ARES_SUCCESS;
137
+ ares_event_configchg_t *c;
138
+
139
+ (void)e;
140
+
141
+ /* Not used by this implementation */
142
+ *configchg = NULL;
143
+
144
+ c = ares_malloc_zero(sizeof(*c));
145
+ if (c == NULL) {
146
+ return ARES_ENOMEM; /* LCOV_EXCL_LINE: OutOfMemory */
147
+ }
148
+
149
+ c->e = e;
150
+ c->inotify_fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
151
+ if (c->inotify_fd == -1) {
152
+ status = ARES_ESERVFAIL; /* LCOV_EXCL_LINE: UntestablePath */
153
+ goto done; /* LCOV_EXCL_LINE: UntestablePath */
154
+ }
155
+
156
+ /* We need to monitor /etc/resolv.conf, /etc/nsswitch.conf */
157
+ if (inotify_add_watch(c->inotify_fd, "/etc",
158
+ IN_CREATE | IN_MODIFY | IN_MOVED_TO | IN_ONLYDIR) ==
159
+ -1) {
160
+ status = ARES_ESERVFAIL; /* LCOV_EXCL_LINE: UntestablePath */
161
+ goto done; /* LCOV_EXCL_LINE: UntestablePath */
162
+ }
163
+
164
+ status =
165
+ ares_event_update(NULL, e, ARES_EVENT_FLAG_READ, ares_event_configchg_cb,
166
+ c->inotify_fd, c, ares_event_configchg_free, NULL);
167
+
168
+ done:
169
+ if (status != ARES_SUCCESS) {
170
+ ares_event_configchg_free(c);
171
+ } else {
172
+ *configchg = c;
173
+ }
174
+ return status;
175
+ }
176
+
177
+ #elif defined(USE_WINSOCK) && defined(CARES_THREADS)
178
+
179
+ # include <winsock2.h>
180
+ # include <iphlpapi.h>
181
+ # include <stdio.h>
182
+ # include <windows.h>
183
+
184
+ struct ares_event_configchg {
185
+ HANDLE ifchg_hnd;
186
+ HKEY regip4;
187
+ HANDLE regip4_event;
188
+ HANDLE regip4_wait;
189
+ HKEY regip6;
190
+ HANDLE regip6_event;
191
+ HANDLE regip6_wait;
192
+ ares_event_thread_t *e;
193
+ };
194
+
195
+ void ares_event_configchg_destroy(ares_event_configchg_t *configchg)
196
+ {
197
+ if (configchg == NULL) {
198
+ return;
199
+ }
200
+
201
+ # ifdef HAVE_NOTIFYIPINTERFACECHANGE
202
+ if (configchg->ifchg_hnd != NULL) {
203
+ CancelMibChangeNotify2(configchg->ifchg_hnd);
204
+ configchg->ifchg_hnd = NULL;
205
+ }
206
+ # endif
207
+
208
+ # ifdef HAVE_REGISTERWAITFORSINGLEOBJECT
209
+ if (configchg->regip4_wait != NULL) {
210
+ /* Use INVALID_HANDLE_VALUE to wait for any running callback to complete
211
+ * before returning, preventing use-after-free of configchg */
212
+ UnregisterWaitEx(configchg->regip4_wait, INVALID_HANDLE_VALUE);
213
+ configchg->regip4_wait = NULL;
214
+ }
215
+
216
+ if (configchg->regip6_wait != NULL) {
217
+ UnregisterWaitEx(configchg->regip6_wait, INVALID_HANDLE_VALUE);
218
+ configchg->regip6_wait = NULL;
219
+ }
220
+
221
+ if (configchg->regip4 != NULL) {
222
+ RegCloseKey(configchg->regip4);
223
+ configchg->regip4 = NULL;
224
+ }
225
+
226
+ if (configchg->regip6 != NULL) {
227
+ RegCloseKey(configchg->regip6);
228
+ configchg->regip6 = NULL;
229
+ }
230
+
231
+ if (configchg->regip4_event != NULL) {
232
+ CloseHandle(configchg->regip4_event);
233
+ configchg->regip4_event = NULL;
234
+ }
235
+
236
+ if (configchg->regip6_event != NULL) {
237
+ CloseHandle(configchg->regip6_event);
238
+ configchg->regip6_event = NULL;
239
+ }
240
+ # endif
241
+
242
+ ares_free(configchg);
243
+ }
244
+
245
+
246
+ # ifdef HAVE_NOTIFYIPINTERFACECHANGE
247
+ static void NETIOAPI_API_
248
+ ares_event_configchg_ip_cb(PVOID CallerContext, PMIB_IPINTERFACE_ROW Row,
249
+ MIB_NOTIFICATION_TYPE NotificationType)
250
+ {
251
+ ares_event_configchg_t *configchg = CallerContext;
252
+ (void)Row;
253
+ (void)NotificationType;
254
+ ares_reinit(configchg->e->channel);
255
+ }
256
+ # endif
257
+
258
+ static ares_bool_t
259
+ ares_event_configchg_regnotify(ares_event_configchg_t *configchg)
260
+ {
261
+ # ifdef HAVE_REGISTERWAITFORSINGLEOBJECT
262
+ # if defined(__WATCOMC__) && !defined(REG_NOTIFY_THREAD_AGNOSTIC)
263
+ # define REG_NOTIFY_THREAD_AGNOSTIC 0x10000000L
264
+ # endif
265
+ DWORD flags = REG_NOTIFY_CHANGE_NAME | REG_NOTIFY_CHANGE_LAST_SET |
266
+ REG_NOTIFY_THREAD_AGNOSTIC;
267
+
268
+ if (RegNotifyChangeKeyValue(configchg->regip4, TRUE, flags,
269
+ configchg->regip4_event, TRUE) != ERROR_SUCCESS) {
270
+ return ARES_FALSE;
271
+ }
272
+
273
+ if (RegNotifyChangeKeyValue(configchg->regip6, TRUE, flags,
274
+ configchg->regip6_event, TRUE) != ERROR_SUCCESS) {
275
+ return ARES_FALSE;
276
+ }
277
+ # else
278
+ (void)configchg;
279
+ # endif
280
+ return ARES_TRUE;
281
+ }
282
+
283
+ static VOID CALLBACK ares_event_configchg_reg_cb(PVOID lpParameter,
284
+ BOOLEAN TimerOrWaitFired)
285
+ {
286
+ ares_event_configchg_t *configchg = lpParameter;
287
+ (void)TimerOrWaitFired;
288
+
289
+ ares_reinit(configchg->e->channel);
290
+
291
+ /* Re-arm, as its single-shot. However, we don't know which one needs to
292
+ * be re-armed, so we just do both */
293
+ ares_event_configchg_regnotify(configchg);
294
+ }
295
+
296
+ ares_status_t ares_event_configchg_init(ares_event_configchg_t **configchg,
297
+ ares_event_thread_t *e)
298
+ {
299
+ ares_status_t status = ARES_SUCCESS;
300
+ ares_event_configchg_t *c = NULL;
301
+
302
+ c = ares_malloc_zero(sizeof(**configchg));
303
+ if (c == NULL) {
304
+ return ARES_ENOMEM;
305
+ }
306
+
307
+ c->e = e;
308
+
309
+ # ifdef HAVE_NOTIFYIPINTERFACECHANGE
310
+ /* NOTE: If a user goes into the control panel and changes the network
311
+ * adapter DNS addresses manually, this will NOT trigger a notification.
312
+ * We've also tried listening on NotifyUnicastIpAddressChange(), but
313
+ * that didn't get triggered either.
314
+ */
315
+ if (NotifyIpInterfaceChange(AF_UNSPEC, ares_event_configchg_ip_cb, c, FALSE,
316
+ &c->ifchg_hnd) != NO_ERROR) {
317
+ status = ARES_ESERVFAIL;
318
+ goto done;
319
+ }
320
+ # endif
321
+
322
+ # ifdef HAVE_REGISTERWAITFORSINGLEOBJECT
323
+ /* Monitor HKLM\SYSTEM\CurrentControlSet\Services\Tcpip6\Parameters\Interfaces
324
+ * and HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces
325
+ * for changes via RegNotifyChangeKeyValue() */
326
+ if (RegOpenKeyExW(
327
+ HKEY_LOCAL_MACHINE,
328
+ L"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces",
329
+ 0, KEY_NOTIFY, &c->regip4) != ERROR_SUCCESS) {
330
+ status = ARES_ESERVFAIL;
331
+ goto done;
332
+ }
333
+
334
+ if (RegOpenKeyExW(
335
+ HKEY_LOCAL_MACHINE,
336
+ L"SYSTEM\\CurrentControlSet\\Services\\Tcpip6\\Parameters\\Interfaces",
337
+ 0, KEY_NOTIFY, &c->regip6) != ERROR_SUCCESS) {
338
+ status = ARES_ESERVFAIL;
339
+ goto done;
340
+ }
341
+
342
+ c->regip4_event = CreateEvent(NULL, TRUE, FALSE, NULL);
343
+ if (c->regip4_event == NULL) {
344
+ status = ARES_ESERVFAIL;
345
+ goto done;
346
+ }
347
+
348
+ c->regip6_event = CreateEvent(NULL, TRUE, FALSE, NULL);
349
+ if (c->regip6_event == NULL) {
350
+ status = ARES_ESERVFAIL;
351
+ goto done;
352
+ }
353
+
354
+ if (!RegisterWaitForSingleObject(&c->regip4_wait, c->regip4_event,
355
+ ares_event_configchg_reg_cb, c, INFINITE,
356
+ WT_EXECUTEDEFAULT)) {
357
+ status = ARES_ESERVFAIL;
358
+ goto done;
359
+ }
360
+
361
+ if (!RegisterWaitForSingleObject(&c->regip6_wait, c->regip6_event,
362
+ ares_event_configchg_reg_cb, c, INFINITE,
363
+ WT_EXECUTEDEFAULT)) {
364
+ status = ARES_ESERVFAIL;
365
+ goto done;
366
+ }
367
+ # endif
368
+
369
+ if (!ares_event_configchg_regnotify(c)) {
370
+ status = ARES_ESERVFAIL;
371
+ goto done;
372
+ }
373
+
374
+ done:
375
+ if (status != ARES_SUCCESS) {
376
+ ares_event_configchg_destroy(c);
377
+ } else {
378
+ *configchg = c;
379
+ }
380
+
381
+ return status;
382
+ }
383
+
384
+ #elif defined(__APPLE__) && defined(CARES_THREADS)
385
+
386
+ # include <sys/types.h>
387
+ # include <unistd.h>
388
+ # include <stdbool.h>
389
+ # include <notify.h>
390
+ # include <dlfcn.h>
391
+ # include <fcntl.h>
392
+
393
+ struct ares_event_configchg {
394
+ int fd;
395
+ int token;
396
+ };
397
+
398
+ void ares_event_configchg_destroy(ares_event_configchg_t *configchg)
399
+ {
400
+ (void)configchg;
401
+
402
+ /* Cleanup happens automatically */
403
+ }
404
+
405
+ static void ares_event_configchg_free(void *data)
406
+ {
407
+ ares_event_configchg_t *configchg = data;
408
+ if (configchg == NULL) {
409
+ return;
410
+ }
411
+
412
+ if (configchg->fd >= 0) {
413
+ notify_cancel(configchg->token);
414
+ /* automatically closes fd */
415
+ configchg->fd = -1;
416
+ }
417
+
418
+ ares_free(configchg);
419
+ }
420
+
421
+ static void ares_event_configchg_cb(ares_event_thread_t *e, ares_socket_t fd,
422
+ void *data, ares_event_flags_t flags)
423
+ {
424
+ ares_event_configchg_t *configchg = data;
425
+ ares_bool_t triggered = ARES_FALSE;
426
+
427
+ (void)fd;
428
+ (void)flags;
429
+
430
+ while (1) {
431
+ int t = 0;
432
+ ssize_t len;
433
+
434
+ len = read(configchg->fd, &t, sizeof(t));
435
+
436
+ if (len < (ssize_t)sizeof(t)) {
437
+ break;
438
+ }
439
+
440
+ /* Token is read in network byte order (yeah, docs don't mention this) */
441
+ t = (int)ntohl(t);
442
+
443
+ if (t != configchg->token) {
444
+ continue;
445
+ }
446
+
447
+ triggered = ARES_TRUE;
448
+ }
449
+
450
+ /* Only process after all events are read. No need to process more often as
451
+ * we don't want to reload the config back to back */
452
+ if (triggered) {
453
+ ares_reinit(e->channel);
454
+ }
455
+ }
456
+
457
+ ares_status_t ares_event_configchg_init(ares_event_configchg_t **configchg,
458
+ ares_event_thread_t *e)
459
+ {
460
+ ares_status_t status = ARES_SUCCESS;
461
+ void *handle = NULL;
462
+ const char *(*pdns_configuration_notify_key)(void) = NULL;
463
+ const char *notify_key = NULL;
464
+ int flags;
465
+ size_t i;
466
+ const char *searchlibs[] = {
467
+ "/usr/lib/libSystem.dylib",
468
+ "/System/Library/Frameworks/SystemConfiguration.framework/"
469
+ "SystemConfiguration",
470
+ NULL
471
+ };
472
+
473
+ *configchg = ares_malloc_zero(sizeof(**configchg));
474
+ if (*configchg == NULL) {
475
+ return ARES_ENOMEM;
476
+ }
477
+
478
+ /* Load symbol as it isn't normally public */
479
+ for (i = 0; searchlibs[i] != NULL; i++) {
480
+ handle = dlopen(searchlibs[i], RTLD_LAZY);
481
+ if (handle == NULL) {
482
+ /* Fail, loop! */
483
+ continue;
484
+ }
485
+
486
+ pdns_configuration_notify_key =
487
+ (const char *(*)(void))dlsym(handle, "dns_configuration_notify_key");
488
+ if (pdns_configuration_notify_key != NULL) {
489
+ break;
490
+ }
491
+
492
+ /* Fail, loop! */
493
+ dlclose(handle);
494
+ handle = NULL;
495
+ }
496
+
497
+ if (pdns_configuration_notify_key == NULL) {
498
+ status = ARES_ESERVFAIL;
499
+ goto done;
500
+ }
501
+
502
+ notify_key = pdns_configuration_notify_key();
503
+ if (notify_key == NULL) {
504
+ status = ARES_ESERVFAIL;
505
+ goto done;
506
+ }
507
+
508
+ if (notify_register_file_descriptor(notify_key, &(*configchg)->fd, 0,
509
+ &(*configchg)->token) !=
510
+ NOTIFY_STATUS_OK) {
511
+ status = ARES_ESERVFAIL;
512
+ goto done;
513
+ }
514
+
515
+ /* Set file descriptor to non-blocking */
516
+ flags = fcntl((*configchg)->fd, F_GETFL, 0);
517
+ fcntl((*configchg)->fd, F_SETFL, flags | O_NONBLOCK);
518
+
519
+ /* Register file descriptor with event subsystem */
520
+ status = ares_event_update(NULL, e, ARES_EVENT_FLAG_READ,
521
+ ares_event_configchg_cb, (*configchg)->fd,
522
+ *configchg, ares_event_configchg_free, NULL);
523
+
524
+ done:
525
+ if (status != ARES_SUCCESS) {
526
+ ares_event_configchg_free(*configchg);
527
+ *configchg = NULL;
528
+ }
529
+
530
+ if (handle) {
531
+ dlclose(handle);
532
+ }
533
+
534
+ return status;
535
+ }
536
+
537
+ #elif defined(HAVE_STAT) && !defined(_WIN32) && defined(CARES_THREADS)
538
+ # ifdef HAVE_SYS_TYPES_H
539
+ # include <sys/types.h>
540
+ # endif
541
+ # ifdef HAVE_SYS_STAT_H
542
+ # include <sys/stat.h>
543
+ # endif
544
+
545
+ typedef struct {
546
+ size_t size;
547
+ time_t mtime;
548
+ } fileinfo_t;
549
+
550
+ struct ares_event_configchg {
551
+ ares_bool_t isup;
552
+ ares_thread_t *thread;
553
+ ares_htable_strvp_t *filestat;
554
+ ares_thread_mutex_t *lock;
555
+ ares_thread_cond_t *wake;
556
+ const char *resolvconf_path;
557
+ ares_event_thread_t *e;
558
+ };
559
+
560
+ static ares_status_t config_change_check(ares_htable_strvp_t *filestat,
561
+ const char *resolvconf_path)
562
+ {
563
+ size_t i;
564
+ const char *configfiles[16];
565
+ ares_bool_t changed = ARES_FALSE;
566
+ size_t cnt = 0;
567
+
568
+ memset(configfiles, 0, sizeof(configfiles));
569
+
570
+ configfiles[cnt++] = resolvconf_path;
571
+ configfiles[cnt++] = "/etc/nsswitch.conf";
572
+ #ifdef _AIX
573
+ configfiles[cnt++] = "/etc/netsvc.conf";
574
+ #endif
575
+ #ifdef __osf /* Tru64 */
576
+ configfiles[cnt++] = "/etc/svc.conf";
577
+ #endif
578
+ #ifdef __QNX__
579
+ configfiles[cnt++] = "/etc/net.cfg";
580
+ #endif
581
+ configfiles[cnt++] = NULL;
582
+
583
+ for (i = 0; configfiles[i] != NULL; i++) {
584
+ fileinfo_t *fi = ares_htable_strvp_get_direct(filestat, configfiles[i]);
585
+ struct stat st;
586
+
587
+ if (stat(configfiles[i], &st) == 0) {
588
+ if (fi == NULL) {
589
+ fi = ares_malloc_zero(sizeof(*fi));
590
+ if (fi == NULL) {
591
+ return ARES_ENOMEM;
592
+ }
593
+ if (!ares_htable_strvp_insert(filestat, configfiles[i], fi)) {
594
+ ares_free(fi);
595
+ return ARES_ENOMEM;
596
+ }
597
+ }
598
+ if (fi->size != (size_t)st.st_size || fi->mtime != (time_t)st.st_mtime) {
599
+ changed = ARES_TRUE;
600
+ }
601
+ fi->size = (size_t)st.st_size;
602
+ fi->mtime = (time_t)st.st_mtime;
603
+ } else if (fi != NULL) {
604
+ /* File no longer exists, remove */
605
+ ares_htable_strvp_remove(filestat, configfiles[i]);
606
+ changed = ARES_TRUE;
607
+ }
608
+ }
609
+
610
+ if (changed) {
611
+ return ARES_SUCCESS;
612
+ }
613
+ return ARES_ENOTFOUND;
614
+ }
615
+
616
+ static void *ares_event_configchg_thread(void *arg)
617
+ {
618
+ ares_event_configchg_t *c = arg;
619
+
620
+ ares_thread_mutex_lock(c->lock);
621
+ while (c->isup) {
622
+ ares_status_t status;
623
+
624
+ if (ares_thread_cond_timedwait(c->wake, c->lock, 30000) != ARES_ETIMEOUT) {
625
+ continue;
626
+ }
627
+
628
+ /* make sure status didn't change even though we got a timeout */
629
+ if (!c->isup) {
630
+ break;
631
+ }
632
+
633
+ status = config_change_check(c->filestat, c->resolvconf_path);
634
+ if (status == ARES_SUCCESS) {
635
+ ares_reinit(c->e->channel);
636
+ }
637
+ }
638
+
639
+ ares_thread_mutex_unlock(c->lock);
640
+ return NULL;
641
+ }
642
+
643
+ ares_status_t ares_event_configchg_init(ares_event_configchg_t **configchg,
644
+ ares_event_thread_t *e)
645
+ {
646
+ ares_status_t status = ARES_SUCCESS;
647
+ ares_event_configchg_t *c = NULL;
648
+
649
+ *configchg = NULL;
650
+
651
+ c = ares_malloc_zero(sizeof(*c));
652
+ if (c == NULL) {
653
+ status = ARES_ENOMEM;
654
+ goto done;
655
+ }
656
+
657
+ c->e = e;
658
+
659
+ c->filestat = ares_htable_strvp_create(ares_free);
660
+ if (c->filestat == NULL) {
661
+ status = ARES_ENOMEM;
662
+ goto done;
663
+ }
664
+
665
+ c->wake = ares_thread_cond_create();
666
+ if (c->wake == NULL) {
667
+ status = ARES_ENOMEM;
668
+ goto done;
669
+ }
670
+
671
+ c->lock = ares_thread_mutex_create();
672
+ if (c->lock == NULL) {
673
+ status = ARES_ENOMEM;
674
+ goto done;
675
+ }
676
+
677
+ c->resolvconf_path = c->e->channel->resolvconf_path;
678
+ if (c->resolvconf_path == NULL) {
679
+ c->resolvconf_path = PATH_RESOLV_CONF;
680
+ }
681
+
682
+ status = config_change_check(c->filestat, c->resolvconf_path);
683
+ if (status == ARES_ENOMEM) {
684
+ goto done;
685
+ }
686
+
687
+ c->isup = ARES_TRUE;
688
+ status = ares_thread_create(&c->thread, ares_event_configchg_thread, c);
689
+
690
+ done:
691
+ if (status != ARES_SUCCESS) {
692
+ ares_event_configchg_destroy(c);
693
+ } else {
694
+ *configchg = c;
695
+ }
696
+ return status;
697
+ }
698
+
699
+ void ares_event_configchg_destroy(ares_event_configchg_t *configchg)
700
+ {
701
+ if (configchg == NULL) {
702
+ return;
703
+ }
704
+
705
+ if (configchg->lock) {
706
+ ares_thread_mutex_lock(configchg->lock);
707
+ }
708
+
709
+ configchg->isup = ARES_FALSE;
710
+ if (configchg->wake) {
711
+ ares_thread_cond_signal(configchg->wake);
712
+ }
713
+
714
+ if (configchg->lock) {
715
+ ares_thread_mutex_unlock(configchg->lock);
716
+ }
717
+
718
+ if (configchg->thread) {
719
+ void *rv = NULL;
720
+ ares_thread_join(configchg->thread, &rv);
721
+ }
722
+
723
+ ares_thread_mutex_destroy(configchg->lock);
724
+ ares_thread_cond_destroy(configchg->wake);
725
+ ares_htable_strvp_destroy(configchg->filestat);
726
+ ares_free(configchg);
727
+ }
728
+
729
+ #else
730
+
731
+ ares_status_t ares_event_configchg_init(ares_event_configchg_t **configchg,
732
+ ares_event_thread_t *e)
733
+ {
734
+ (void)configchg;
735
+ (void)e;
736
+ /* No ability */
737
+ return ARES_ENOTIMP;
738
+ }
739
+
740
+ void ares_event_configchg_destroy(ares_event_configchg_t *configchg)
741
+ {
742
+ /* No-op */
743
+ (void)configchg;
744
+ }
745
+
746
+ #endif