grpc 1.74.1 → 1.75.0.pre1

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 (368) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +83 -41
  3. data/include/grpc/credentials.h +7 -1
  4. data/src/core/call/client_call.cc +4 -4
  5. data/src/core/call/filter_fusion.h +1230 -0
  6. data/src/core/call/metadata.cc +22 -0
  7. data/src/core/call/metadata.h +24 -2
  8. data/src/core/channelz/channelz.cc +10 -17
  9. data/src/core/channelz/channelz.h +58 -19
  10. data/src/core/channelz/channelz_registry.cc +0 -162
  11. data/src/core/channelz/channelz_registry.h +14 -7
  12. data/src/core/channelz/property_list.cc +19 -23
  13. data/src/core/channelz/property_list.h +3 -1
  14. data/src/core/channelz/v2tov1/convert.cc +683 -0
  15. data/src/core/channelz/v2tov1/convert.h +58 -0
  16. data/src/core/channelz/v2tov1/legacy_api.cc +425 -0
  17. data/src/core/channelz/v2tov1/legacy_api.h +32 -0
  18. data/src/core/channelz/v2tov1/property_list.cc +118 -0
  19. data/src/core/channelz/v2tov1/property_list.h +52 -0
  20. data/src/core/client_channel/client_channel_filter.cc +5 -4
  21. data/src/core/client_channel/client_channel_filter.h +2 -2
  22. data/src/core/client_channel/client_channel_internal.h +2 -1
  23. data/src/core/client_channel/load_balanced_call_destination.cc +6 -5
  24. data/src/core/client_channel/subchannel.cc +14 -6
  25. data/src/core/client_channel/subchannel.h +2 -0
  26. data/src/core/config/core_configuration.cc +3 -1
  27. data/src/core/config/core_configuration.h +12 -0
  28. data/src/core/credentials/transport/alts/alts_credentials.cc +5 -0
  29. data/src/core/credentials/transport/alts/check_gcp_environment_windows.cc +2 -0
  30. data/src/core/credentials/transport/channel_creds_registry_init.cc +3 -1
  31. data/src/core/credentials/transport/ssl/ssl_credentials.cc +1 -1
  32. data/src/core/credentials/transport/ssl/ssl_security_connector.cc +8 -3
  33. data/src/core/credentials/transport/tls/grpc_tls_certificate_distributor.cc +29 -24
  34. data/src/core/credentials/transport/tls/grpc_tls_certificate_distributor.h +19 -8
  35. data/src/core/credentials/transport/tls/grpc_tls_certificate_provider.cc +96 -54
  36. data/src/core/credentials/transport/tls/grpc_tls_certificate_provider.h +15 -2
  37. data/src/core/credentials/transport/tls/spiffe_utils.cc +371 -0
  38. data/src/core/credentials/transport/tls/spiffe_utils.h +171 -0
  39. data/src/core/credentials/transport/tls/ssl_utils.cc +11 -10
  40. data/src/core/credentials/transport/tls/ssl_utils.h +4 -2
  41. data/src/core/credentials/transport/tls/tls_credentials.cc +2 -0
  42. data/src/core/credentials/transport/tls/tls_security_connector.cc +11 -26
  43. data/src/core/credentials/transport/tls/tls_security_connector.h +12 -12
  44. data/src/core/ext/filters/backend_metrics/backend_metric_filter.cc +1 -2
  45. data/src/core/ext/filters/http/client/http_client_filter.cc +3 -6
  46. data/src/core/ext/filters/http/client_authority_filter.cc +1 -2
  47. data/src/core/ext/filters/http/message_compress/compression_filter.cc +8 -8
  48. data/src/core/ext/filters/http/server/http_server_filter.cc +3 -6
  49. data/src/core/ext/filters/message_size/message_size_filter.cc +4 -4
  50. data/src/core/ext/filters/rbac/rbac_filter.cc +1 -1
  51. data/src/core/ext/filters/stateful_session/stateful_session_filter.cc +3 -5
  52. data/src/core/ext/transport/chttp2/transport/chttp2_transport.cc +3 -2
  53. data/src/core/ext/transport/chttp2/transport/flow_control.cc +1 -0
  54. data/src/core/ext/transport/chttp2/transport/flow_control.h +1 -0
  55. data/src/core/ext/transport/chttp2/transport/frame.cc +89 -6
  56. data/src/core/ext/transport/chttp2/transport/frame.h +38 -0
  57. data/src/core/ext/transport/chttp2/transport/header_assembler.h +5 -14
  58. data/src/core/ext/transport/chttp2/transport/hpack_parser.h +4 -1
  59. data/src/core/ext/transport/chttp2/transport/http2_client_transport.cc +294 -78
  60. data/src/core/ext/transport/chttp2/transport/http2_client_transport.h +128 -9
  61. data/src/core/ext/transport/chttp2/transport/http2_settings.cc +11 -38
  62. data/src/core/ext/transport/chttp2/transport/http2_settings.h +52 -35
  63. data/src/core/ext/transport/chttp2/transport/http2_settings_manager.cc +61 -0
  64. data/src/core/ext/transport/chttp2/transport/http2_settings_manager.h +142 -0
  65. data/src/core/ext/transport/chttp2/transport/http2_transport.cc +81 -3
  66. data/src/core/ext/transport/chttp2/transport/http2_transport.h +12 -1
  67. data/src/core/ext/transport/chttp2/transport/message_assembler.h +2 -2
  68. data/src/core/ext/transport/chttp2/transport/parsing.cc +2 -1
  69. data/src/core/ext/transport/chttp2/transport/ping_promise.cc +2 -1
  70. data/src/core/ext/transport/chttp2/transport/ping_promise.h +22 -5
  71. data/src/core/ext/transport/chttp2/transport/stream_data_queue.h +607 -0
  72. data/src/core/ext/transport/chttp2/transport/writable_streams.h +254 -0
  73. data/src/core/ext/transport/chttp2/transport/writing.cc +6 -4
  74. data/src/core/ext/upb-gen/src/proto/grpc/channelz/channelz.upb.h +4959 -0
  75. data/src/core/ext/upb-gen/src/proto/grpc/channelz/channelz.upb_minitable.c +1111 -0
  76. data/src/core/ext/upb-gen/src/proto/grpc/channelz/channelz.upb_minitable.h +108 -0
  77. data/src/core/ext/upb-gen/src/proto/grpc/channelz/v2/property_list.upb.h +142 -54
  78. data/src/core/ext/upb-gen/src/proto/grpc/channelz/v2/property_list.upb_minitable.c +18 -14
  79. data/src/core/ext/upb-gen/src/proto/grpc/channelz/v2/property_list.upb_minitable.h +2 -2
  80. data/src/core/ext/upbdefs-gen/src/proto/grpc/channelz/channelz.upbdefs.c +716 -0
  81. data/src/core/ext/upbdefs-gen/src/proto/grpc/channelz/channelz.upbdefs.h +227 -0
  82. data/src/core/ext/upbdefs-gen/src/proto/grpc/channelz/v2/property_list.upbdefs.c +86 -88
  83. data/src/core/ext/upbdefs-gen/src/proto/grpc/channelz/v2/property_list.upbdefs.h +2 -2
  84. data/src/core/filter/auth/auth_filters.h +2 -2
  85. data/src/core/filter/fused_filters.cc +154 -0
  86. data/src/core/handshaker/security/legacy_secure_endpoint.cc +1 -1
  87. data/src/core/handshaker/security/pipelined_secure_endpoint.cc +965 -0
  88. data/src/core/handshaker/security/secure_endpoint.cc +28 -13
  89. data/src/core/handshaker/security/secure_endpoint.h +8 -0
  90. data/src/core/lib/channel/promise_based_filter.cc +15 -25
  91. data/src/core/lib/channel/promise_based_filter.h +6 -5
  92. data/src/core/lib/event_engine/ares_resolver.h +3 -1
  93. data/src/core/lib/event_engine/cf_engine/cf_engine.cc +9 -5
  94. data/src/core/lib/event_engine/cf_engine/cf_engine.h +2 -1
  95. data/src/core/lib/event_engine/cf_engine/cfsocket_listener.cc +263 -0
  96. data/src/core/lib/event_engine/cf_engine/cfsocket_listener.h +107 -0
  97. data/src/core/lib/event_engine/cf_engine/cfstream_endpoint.cc +31 -3
  98. data/src/core/lib/event_engine/cf_engine/cfstream_endpoint.h +12 -0
  99. data/src/core/lib/event_engine/posix_engine/ev_epoll1_linux.cc +12 -10
  100. data/src/core/lib/event_engine/posix_engine/ev_epoll1_linux.h +6 -4
  101. data/src/core/lib/event_engine/posix_engine/ev_poll_posix.cc +15 -14
  102. data/src/core/lib/event_engine/posix_engine/ev_poll_posix.h +7 -5
  103. data/src/core/lib/event_engine/posix_engine/event_poller.h +0 -8
  104. data/src/core/lib/event_engine/posix_engine/event_poller_posix_default.cc +11 -5
  105. data/src/core/lib/event_engine/posix_engine/event_poller_posix_default.h +3 -2
  106. data/src/core/lib/event_engine/posix_engine/grpc_polled_fd_posix.h +1 -0
  107. data/src/core/lib/event_engine/posix_engine/lockfree_event.cc +4 -4
  108. data/src/core/lib/event_engine/posix_engine/lockfree_event.h +3 -4
  109. data/src/core/lib/event_engine/posix_engine/posix_endpoint.cc +2 -2
  110. data/src/core/lib/event_engine/posix_engine/posix_engine.cc +188 -199
  111. data/src/core/lib/event_engine/posix_engine/posix_engine.h +30 -45
  112. data/src/core/lib/event_engine/posix_engine/posix_engine_listener.cc +1 -1
  113. data/src/core/lib/event_engine/posix_engine/tcp_socket_utils.h +1 -1
  114. data/src/core/lib/event_engine/windows/grpc_polled_fd_windows.cc +2 -1
  115. data/src/core/lib/experiments/experiments.cc +120 -6
  116. data/src/core/lib/experiments/experiments.h +46 -3
  117. data/src/core/lib/iomgr/combiner.cc +1 -1
  118. data/src/core/lib/iomgr/exec_ctx.h +3 -9
  119. data/src/core/lib/iomgr/socket_mutator.cc +1 -1
  120. data/src/core/lib/iomgr/socket_utils_posix.cc +1 -1
  121. data/src/core/lib/iomgr/socket_utils_posix.h +1 -1
  122. data/src/core/lib/iomgr/tcp_client_posix.cc +1 -1
  123. data/src/core/lib/iomgr/tcp_posix.cc +3 -3
  124. data/src/core/lib/promise/activity.h +2 -2
  125. data/src/core/lib/promise/mpsc.cc +8 -8
  126. data/src/core/lib/promise/party.cc +7 -7
  127. data/src/core/lib/promise/party.h +4 -4
  128. data/src/core/lib/promise/poll.h +10 -0
  129. data/src/core/lib/resource_quota/memory_quota.cc +90 -3
  130. data/src/core/lib/resource_quota/memory_quota.h +20 -9
  131. data/src/core/lib/resource_quota/periodic_update.cc +14 -0
  132. data/src/core/lib/resource_quota/periodic_update.h +8 -0
  133. data/src/core/lib/resource_quota/resource_quota.cc +15 -4
  134. data/src/core/lib/resource_quota/resource_quota.h +3 -0
  135. data/src/core/lib/security/authorization/grpc_server_authz_filter.cc +1 -2
  136. data/src/core/lib/surface/call.cc +5 -5
  137. data/src/core/lib/surface/call.h +6 -5
  138. data/src/core/lib/surface/completion_queue.cc +2 -4
  139. data/src/core/lib/surface/filter_stack_call.cc +1 -1
  140. data/src/core/lib/surface/version.cc +2 -2
  141. data/src/core/lib/transport/promise_endpoint.cc +2 -2
  142. data/src/core/lib/transport/promise_endpoint.h +3 -3
  143. data/src/core/load_balancing/endpoint_list.cc +29 -2
  144. data/src/core/load_balancing/grpclb/client_load_reporting_filter.cc +3 -3
  145. data/src/core/load_balancing/grpclb/client_load_reporting_filter.h +1 -1
  146. data/src/core/load_balancing/pick_first/pick_first.cc +12 -5
  147. data/src/core/load_balancing/xds/xds_cluster_impl.cc +5 -3
  148. data/src/core/net/socket_mutator.cc +19 -0
  149. data/src/core/net/socket_mutator.h +25 -0
  150. data/src/core/plugin_registry/grpc_plugin_registry.cc +6 -0
  151. data/src/core/resolver/dns/c_ares/grpc_ares_ev_driver.h +6 -1
  152. data/src/core/resolver/dns/c_ares/grpc_ares_ev_driver_windows.cc +2 -1
  153. data/src/core/resolver/dns/c_ares/grpc_ares_wrapper.cc +8 -5
  154. data/src/core/resolver/dns/c_ares/grpc_ares_wrapper.h +2 -1
  155. data/src/core/resolver/xds/xds_dependency_manager.cc +1 -1
  156. data/src/core/server/server.cc +1 -1
  157. data/src/core/server/server_call_tracer_filter.cc +0 -66
  158. data/src/core/server/server_call_tracer_filter.h +64 -0
  159. data/src/core/server/server_config_selector_filter.cc +1 -1
  160. data/src/core/service_config/service_config_channel_arg_filter.cc +3 -60
  161. data/src/core/service_config/service_config_channel_arg_filter.h +82 -0
  162. data/src/core/telemetry/call_tracer.cc +20 -14
  163. data/src/core/telemetry/call_tracer.h +22 -17
  164. data/src/core/telemetry/metrics.h +8 -8
  165. data/src/core/telemetry/stats_data.cc +151 -151
  166. data/src/core/telemetry/stats_data.h +87 -87
  167. data/src/core/transport/auth_context.cc +20 -0
  168. data/src/core/transport/auth_context.h +4 -0
  169. data/src/core/transport/auth_context_comparator_registry.h +69 -0
  170. data/src/core/tsi/alts/handshaker/alts_handshaker_client.cc +2 -3
  171. data/src/core/tsi/ssl_transport_security.cc +202 -32
  172. data/src/core/tsi/ssl_transport_security.h +19 -10
  173. data/src/core/tsi/ssl_transport_security_utils.cc +21 -0
  174. data/src/core/tsi/ssl_transport_security_utils.h +4 -0
  175. data/src/core/util/http_client/httpcli_security_connector.cc +3 -1
  176. data/src/core/util/latent_see.cc +178 -146
  177. data/src/core/util/latent_see.h +245 -188
  178. data/src/core/util/single_set_ptr.h +5 -2
  179. data/src/core/util/useful.h +91 -0
  180. data/src/core/util/windows/directory_reader.cc +1 -0
  181. data/src/core/util/windows/thd.cc +1 -3
  182. data/src/core/util/work_serializer.cc +1 -1
  183. data/src/core/xds/grpc/file_watcher_certificate_provider_factory.cc +32 -5
  184. data/src/core/xds/grpc/file_watcher_certificate_provider_factory.h +5 -0
  185. data/src/core/xds/grpc/xds_certificate_provider.cc +5 -6
  186. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +1 -1
  187. data/src/ruby/lib/grpc/version.rb +1 -1
  188. data/third_party/cares/cares/include/ares.h +925 -460
  189. data/third_party/cares/cares/include/ares_dns.h +86 -71
  190. data/third_party/cares/cares/include/ares_dns_record.h +1118 -0
  191. data/third_party/cares/cares/include/ares_nameser.h +215 -189
  192. data/third_party/cares/cares/include/ares_version.h +37 -14
  193. data/third_party/cares/cares/src/lib/ares_addrinfo2hostent.c +305 -0
  194. data/third_party/cares/cares/src/lib/ares_addrinfo_localhost.c +245 -0
  195. data/third_party/cares/cares/src/lib/ares_android.c +216 -164
  196. data/third_party/cares/cares/src/lib/ares_android.h +25 -14
  197. data/third_party/cares/cares/src/lib/ares_cancel.c +68 -44
  198. data/third_party/cares/cares/src/lib/ares_close_sockets.c +137 -0
  199. data/third_party/cares/cares/src/lib/ares_conn.c +511 -0
  200. data/third_party/cares/cares/src/lib/ares_conn.h +196 -0
  201. data/third_party/cares/cares/src/lib/ares_cookie.c +461 -0
  202. data/third_party/cares/cares/src/lib/ares_data.c +93 -181
  203. data/third_party/cares/cares/src/lib/ares_data.h +50 -39
  204. data/third_party/cares/cares/src/lib/ares_destroy.c +127 -89
  205. data/third_party/cares/cares/src/lib/ares_free_hostent.c +35 -24
  206. data/third_party/cares/cares/src/lib/ares_free_string.c +24 -16
  207. data/third_party/cares/cares/src/lib/ares_freeaddrinfo.c +45 -38
  208. data/third_party/cares/cares/src/lib/ares_getaddrinfo.c +549 -663
  209. data/third_party/cares/cares/src/lib/ares_getenv.c +25 -15
  210. data/third_party/cares/cares/src/lib/ares_getenv.h +26 -18
  211. data/third_party/cares/cares/src/lib/ares_gethostbyaddr.c +163 -221
  212. data/third_party/cares/cares/src/lib/ares_gethostbyname.c +222 -223
  213. data/third_party/cares/cares/src/lib/ares_getnameinfo.c +328 -338
  214. data/third_party/cares/cares/src/lib/ares_hosts_file.c +952 -0
  215. data/third_party/cares/cares/src/lib/ares_inet_net_pton.h +25 -19
  216. data/third_party/cares/cares/src/lib/ares_init.c +425 -2091
  217. data/third_party/cares/cares/src/lib/ares_ipv6.h +63 -33
  218. data/third_party/cares/cares/src/lib/ares_library_init.c +110 -54
  219. data/third_party/cares/cares/src/lib/ares_metrics.c +261 -0
  220. data/third_party/cares/cares/src/lib/ares_options.c +418 -332
  221. data/third_party/cares/cares/src/lib/ares_parse_into_addrinfo.c +179 -0
  222. data/third_party/cares/cares/src/lib/ares_private.h +558 -356
  223. data/third_party/cares/cares/src/lib/ares_process.c +1224 -1369
  224. data/third_party/cares/cares/src/lib/ares_qcache.c +430 -0
  225. data/third_party/cares/cares/src/lib/ares_query.c +126 -121
  226. data/third_party/cares/cares/src/lib/ares_search.c +564 -262
  227. data/third_party/cares/cares/src/lib/ares_send.c +264 -93
  228. data/third_party/cares/cares/src/lib/ares_set_socket_functions.c +588 -0
  229. data/third_party/cares/cares/src/lib/ares_setup.h +115 -111
  230. data/third_party/cares/cares/src/lib/ares_socket.c +425 -0
  231. data/third_party/cares/cares/src/lib/ares_socket.h +163 -0
  232. data/third_party/cares/cares/src/lib/ares_sortaddrinfo.c +447 -0
  233. data/third_party/cares/cares/src/lib/ares_strerror.c +83 -48
  234. data/third_party/cares/cares/src/lib/ares_sysconfig.c +639 -0
  235. data/third_party/cares/cares/src/lib/ares_sysconfig_files.c +839 -0
  236. data/third_party/cares/cares/src/lib/ares_sysconfig_mac.c +373 -0
  237. data/third_party/cares/cares/src/lib/ares_sysconfig_win.c +621 -0
  238. data/third_party/cares/cares/src/lib/ares_timeout.c +136 -73
  239. data/third_party/cares/cares/src/lib/ares_update_servers.c +1362 -0
  240. data/third_party/cares/cares/src/lib/ares_version.c +29 -4
  241. data/third_party/cares/cares/src/lib/config-dos.h +88 -89
  242. data/third_party/cares/cares/src/lib/config-win32.h +122 -77
  243. data/third_party/cares/cares/src/lib/dsa/ares_array.c +394 -0
  244. data/third_party/cares/cares/src/lib/dsa/ares_htable.c +447 -0
  245. data/third_party/cares/cares/src/lib/dsa/ares_htable.h +174 -0
  246. data/third_party/cares/cares/src/lib/dsa/ares_htable_asvp.c +224 -0
  247. data/third_party/cares/cares/src/lib/dsa/ares_htable_dict.c +228 -0
  248. data/third_party/cares/cares/src/lib/dsa/ares_htable_strvp.c +210 -0
  249. data/third_party/cares/cares/src/lib/dsa/ares_htable_szvp.c +188 -0
  250. data/third_party/cares/cares/src/lib/dsa/ares_htable_vpstr.c +186 -0
  251. data/third_party/cares/cares/src/lib/dsa/ares_htable_vpvp.c +194 -0
  252. data/third_party/cares/cares/src/lib/dsa/ares_llist.c +382 -0
  253. data/third_party/cares/cares/src/lib/dsa/ares_slist.c +479 -0
  254. data/third_party/cares/cares/src/lib/dsa/ares_slist.h +207 -0
  255. data/third_party/cares/cares/src/lib/event/ares_event.h +191 -0
  256. data/third_party/cares/cares/src/lib/event/ares_event_configchg.c +743 -0
  257. data/third_party/cares/cares/src/lib/event/ares_event_epoll.c +192 -0
  258. data/third_party/cares/cares/src/lib/event/ares_event_kqueue.c +248 -0
  259. data/third_party/cares/cares/src/lib/event/ares_event_poll.c +140 -0
  260. data/third_party/cares/cares/src/lib/event/ares_event_select.c +159 -0
  261. data/third_party/cares/cares/src/lib/event/ares_event_thread.c +567 -0
  262. data/third_party/cares/cares/src/lib/event/ares_event_wake_pipe.c +166 -0
  263. data/third_party/cares/cares/src/lib/event/ares_event_win32.c +978 -0
  264. data/third_party/cares/cares/src/lib/event/ares_event_win32.h +161 -0
  265. data/third_party/cares/cares/src/lib/include/ares_array.h +276 -0
  266. data/third_party/cares/cares/src/lib/include/ares_buf.h +732 -0
  267. data/third_party/cares/cares/src/lib/include/ares_htable_asvp.h +130 -0
  268. data/third_party/cares/cares/src/lib/include/ares_htable_dict.h +123 -0
  269. data/third_party/cares/cares/src/lib/include/ares_htable_strvp.h +130 -0
  270. data/third_party/cares/cares/src/lib/include/ares_htable_szvp.h +118 -0
  271. data/third_party/cares/cares/src/lib/include/ares_htable_vpstr.h +111 -0
  272. data/third_party/cares/cares/src/lib/include/ares_htable_vpvp.h +128 -0
  273. data/third_party/cares/cares/src/lib/include/ares_llist.h +239 -0
  274. data/third_party/cares/cares/src/lib/include/ares_mem.h +38 -0
  275. data/third_party/cares/cares/src/lib/include/ares_str.h +244 -0
  276. data/third_party/cares/cares/src/lib/inet_net_pton.c +202 -157
  277. data/third_party/cares/cares/src/lib/inet_ntop.c +87 -69
  278. data/third_party/cares/cares/src/lib/legacy/ares_create_query.c +78 -0
  279. data/third_party/cares/cares/src/lib/legacy/ares_expand_name.c +99 -0
  280. data/third_party/cares/cares/src/lib/legacy/ares_expand_string.c +107 -0
  281. data/third_party/cares/cares/src/lib/legacy/ares_fds.c +80 -0
  282. data/third_party/cares/cares/src/lib/legacy/ares_getsock.c +85 -0
  283. data/third_party/cares/cares/src/lib/legacy/ares_parse_a_reply.c +107 -0
  284. data/third_party/cares/cares/src/lib/legacy/ares_parse_aaaa_reply.c +109 -0
  285. data/third_party/cares/cares/src/lib/legacy/ares_parse_caa_reply.c +137 -0
  286. data/third_party/cares/cares/src/lib/legacy/ares_parse_mx_reply.c +110 -0
  287. data/third_party/cares/cares/src/lib/legacy/ares_parse_naptr_reply.c +132 -0
  288. data/third_party/cares/cares/src/lib/legacy/ares_parse_ns_reply.c +154 -0
  289. data/third_party/cares/cares/src/lib/legacy/ares_parse_ptr_reply.c +213 -0
  290. data/third_party/cares/cares/src/lib/legacy/ares_parse_soa_reply.c +115 -0
  291. data/third_party/cares/cares/src/lib/legacy/ares_parse_srv_reply.c +114 -0
  292. data/third_party/cares/cares/src/lib/legacy/ares_parse_txt_reply.c +144 -0
  293. data/third_party/cares/cares/src/lib/legacy/ares_parse_uri_reply.c +113 -0
  294. data/third_party/cares/cares/src/lib/record/ares_dns_mapping.c +982 -0
  295. data/third_party/cares/cares/src/lib/record/ares_dns_multistring.c +307 -0
  296. data/third_party/cares/cares/src/lib/record/ares_dns_multistring.h +72 -0
  297. data/third_party/cares/cares/src/lib/record/ares_dns_name.c +673 -0
  298. data/third_party/cares/cares/src/lib/record/ares_dns_parse.c +1329 -0
  299. data/third_party/cares/cares/src/lib/record/ares_dns_private.h +273 -0
  300. data/third_party/cares/cares/src/lib/record/ares_dns_record.c +1661 -0
  301. data/third_party/cares/cares/src/lib/record/ares_dns_write.c +1229 -0
  302. data/third_party/cares/cares/src/lib/str/ares_buf.c +1498 -0
  303. data/third_party/cares/cares/src/lib/str/ares_str.c +508 -0
  304. data/third_party/cares/cares/src/lib/str/ares_strsplit.c +90 -0
  305. data/third_party/cares/cares/src/lib/str/ares_strsplit.h +51 -0
  306. data/third_party/cares/cares/src/lib/thirdparty/apple/dnsinfo.h +122 -0
  307. data/third_party/cares/cares/src/lib/util/ares_iface_ips.c +628 -0
  308. data/third_party/cares/cares/src/lib/util/ares_iface_ips.h +139 -0
  309. data/third_party/cares/cares/src/lib/util/ares_math.c +158 -0
  310. data/third_party/cares/cares/src/lib/util/ares_math.h +45 -0
  311. data/third_party/cares/cares/src/lib/util/ares_rand.c +389 -0
  312. data/third_party/cares/cares/src/lib/util/ares_rand.h +36 -0
  313. data/third_party/cares/cares/src/lib/util/ares_threads.c +614 -0
  314. data/third_party/cares/cares/src/lib/util/ares_threads.h +60 -0
  315. data/third_party/cares/cares/src/lib/util/ares_time.h +48 -0
  316. data/third_party/cares/cares/src/lib/util/ares_timeval.c +95 -0
  317. data/third_party/cares/cares/src/lib/util/ares_uri.c +1626 -0
  318. data/third_party/cares/cares/src/lib/util/ares_uri.h +252 -0
  319. data/third_party/cares/cares/src/lib/windows_port.c +16 -9
  320. metadata +121 -49
  321. data/src/core/util/ring_buffer.h +0 -122
  322. data/third_party/cares/cares/include/ares_rules.h +0 -125
  323. data/third_party/cares/cares/src/lib/ares__addrinfo2hostent.c +0 -266
  324. data/third_party/cares/cares/src/lib/ares__addrinfo_localhost.c +0 -240
  325. data/third_party/cares/cares/src/lib/ares__close_sockets.c +0 -61
  326. data/third_party/cares/cares/src/lib/ares__get_hostent.c +0 -260
  327. data/third_party/cares/cares/src/lib/ares__parse_into_addrinfo.c +0 -229
  328. data/third_party/cares/cares/src/lib/ares__read_line.c +0 -73
  329. data/third_party/cares/cares/src/lib/ares__readaddrinfo.c +0 -258
  330. data/third_party/cares/cares/src/lib/ares__sortaddrinfo.c +0 -507
  331. data/third_party/cares/cares/src/lib/ares__timeval.c +0 -111
  332. data/third_party/cares/cares/src/lib/ares_create_query.c +0 -197
  333. data/third_party/cares/cares/src/lib/ares_expand_name.c +0 -311
  334. data/third_party/cares/cares/src/lib/ares_expand_string.c +0 -67
  335. data/third_party/cares/cares/src/lib/ares_fds.c +0 -59
  336. data/third_party/cares/cares/src/lib/ares_getsock.c +0 -66
  337. data/third_party/cares/cares/src/lib/ares_iphlpapi.h +0 -221
  338. data/third_party/cares/cares/src/lib/ares_llist.c +0 -63
  339. data/third_party/cares/cares/src/lib/ares_llist.h +0 -39
  340. data/third_party/cares/cares/src/lib/ares_mkquery.c +0 -24
  341. data/third_party/cares/cares/src/lib/ares_nowarn.c +0 -260
  342. data/third_party/cares/cares/src/lib/ares_nowarn.h +0 -61
  343. data/third_party/cares/cares/src/lib/ares_parse_a_reply.c +0 -90
  344. data/third_party/cares/cares/src/lib/ares_parse_aaaa_reply.c +0 -92
  345. data/third_party/cares/cares/src/lib/ares_parse_caa_reply.c +0 -199
  346. data/third_party/cares/cares/src/lib/ares_parse_mx_reply.c +0 -164
  347. data/third_party/cares/cares/src/lib/ares_parse_naptr_reply.c +0 -183
  348. data/third_party/cares/cares/src/lib/ares_parse_ns_reply.c +0 -177
  349. data/third_party/cares/cares/src/lib/ares_parse_ptr_reply.c +0 -228
  350. data/third_party/cares/cares/src/lib/ares_parse_soa_reply.c +0 -179
  351. data/third_party/cares/cares/src/lib/ares_parse_srv_reply.c +0 -168
  352. data/third_party/cares/cares/src/lib/ares_parse_txt_reply.c +0 -214
  353. data/third_party/cares/cares/src/lib/ares_parse_uri_reply.c +0 -184
  354. data/third_party/cares/cares/src/lib/ares_platform.c +0 -11042
  355. data/third_party/cares/cares/src/lib/ares_platform.h +0 -43
  356. data/third_party/cares/cares/src/lib/ares_rand.c +0 -279
  357. data/third_party/cares/cares/src/lib/ares_strcasecmp.c +0 -66
  358. data/third_party/cares/cares/src/lib/ares_strcasecmp.h +0 -30
  359. data/third_party/cares/cares/src/lib/ares_strdup.c +0 -42
  360. data/third_party/cares/cares/src/lib/ares_strdup.h +0 -24
  361. data/third_party/cares/cares/src/lib/ares_strsplit.c +0 -94
  362. data/third_party/cares/cares/src/lib/ares_strsplit.h +0 -42
  363. data/third_party/cares/cares/src/lib/ares_writev.c +0 -79
  364. data/third_party/cares/cares/src/lib/ares_writev.h +0 -36
  365. data/third_party/cares/cares/src/lib/bitncmp.c +0 -59
  366. data/third_party/cares/cares/src/lib/bitncmp.h +0 -26
  367. data/third_party/cares/cares/src/lib/setup_once.h +0 -554
  368. data/third_party/cares/cares/src/tools/ares_getopt.h +0 -53
@@ -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_REGRESSION_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
+ }