]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
resolved-dns-scope: use hash_ops with destructor for conflict_queue
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 12 Apr 2025 14:19:51 +0000 (23:19 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 13 Apr 2025 00:58:49 +0000 (09:58 +0900)
src/resolve/resolved-dns-rr.c
src/resolve/resolved-dns-rr.h
src/resolve/resolved-dns-scope.c

index 2ad7deab78314dd79dd47e317174b8a1a91ca87d..7f0bd35d3c7701cd6f3b0806a07d9944c8614c1d 100644 (file)
@@ -1688,6 +1688,15 @@ int dns_resource_record_compare_func(const DnsResourceRecord *x, const DnsResour
 
 DEFINE_HASH_OPS(dns_resource_record_hash_ops, DnsResourceRecord, dns_resource_record_hash_func, dns_resource_record_compare_func);
 
+DEFINE_HASH_OPS_FULL(
+                dns_resource_record_hash_ops_by_key,
+                DnsResourceKey,
+                dns_resource_key_hash_func,
+                dns_resource_key_compare_func,
+                dns_resource_key_unref,
+                DnsResourceRecord,
+                dns_resource_record_unref);
+
 DnsResourceRecord *dns_resource_record_copy(DnsResourceRecord *rr) {
         _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *copy = NULL;
         DnsResourceRecord *t;
index 23bf2f66d4498f050a3a61bcbb817f4b5eec0d2a..e039e43cec7030230b58ca06d0b8ef26d3616fac 100644 (file)
@@ -414,6 +414,7 @@ int dns_resource_record_compare_func(const DnsResourceRecord *x, const DnsResour
 
 extern const struct hash_ops dns_resource_key_hash_ops;
 extern const struct hash_ops dns_resource_record_hash_ops;
+extern const struct hash_ops dns_resource_record_hash_ops_by_key;
 
 int dnssec_algorithm_to_string_alloc(int i, char **ret);
 int dnssec_algorithm_from_string(const char *s) _pure_;
index 3f137468cce838e829f18edc51dbe2154eaaa8b3..33e17321832e664aa742847b194019a2ee157c56 100644 (file)
@@ -112,7 +112,7 @@ DnsScope* dns_scope_free(DnsScope *s) {
 
         hashmap_free(s->transactions_by_key);
 
-        ordered_hashmap_free_with_destructor(s->conflict_queue, dns_resource_record_unref);
+        ordered_hashmap_free(s->conflict_queue);
         sd_event_source_disable_unref(s->conflict_event_source);
 
         sd_event_source_disable_unref(s->announce_event_source);
@@ -1238,13 +1238,10 @@ static int on_conflict_dispatch(sd_event_source *es, usec_t usec, void *userdata
                 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
                 _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
 
-                key = ordered_hashmap_first_key(scope->conflict_queue);
-                if (!key)
+                rr = ordered_hashmap_steal_first_key_and_value(scope->conflict_queue, (void**) &key);
+                if (!rr)
                         break;
 
-                rr = ordered_hashmap_remove(scope->conflict_queue, key);
-                assert(rr);
-
                 r = dns_scope_make_conflict_packet(scope, rr, &p);
                 if (r < 0) {
                         log_error_errno(r, "Failed to make conflict packet: %m");
@@ -1265,18 +1262,10 @@ int dns_scope_notify_conflict(DnsScope *scope, DnsResourceRecord *rr) {
         assert(scope);
         assert(rr);
 
-        /* We don't send these queries immediately. Instead, we queue
-         * them, and send them after some jitter delay. */
-        r = ordered_hashmap_ensure_allocated(&scope->conflict_queue, &dns_resource_key_hash_ops);
-        if (r < 0) {
-                log_oom();
-                return r;
-        }
-
-        /* We only place one RR per key in the conflict
-         * messages, not all of them. That should be enough to
-         * indicate where there might be a conflict */
-        r = ordered_hashmap_put(scope->conflict_queue, rr->key, rr);
+        /* We don't send these queries immediately. Instead, we queue them, and send them after some jitter
+         * delay.  We only place one RR per key in the conflict messages, not all of them. That should be
+         * enough to indicate where there might be a conflict */
+        r = ordered_hashmap_ensure_put(&scope->conflict_queue, &dns_resource_record_hash_ops_by_key, rr->key, rr);
         if (IN_SET(r, 0, -EEXIST))
                 return 0;
         if (r < 0)