resolved: unify DnsServer handling code between Link and Manager
authorLennart Poettering <lennart@poettering.net>
Tue, 24 Nov 2015 20:27:29 +0000 (21:27 +0100)
committerLennart Poettering <lennart@poettering.net>
Wed, 25 Nov 2015 20:58:38 +0000 (21:58 +0100)
This copies concepts we introduced for the DnsSearchDomain stuff, and
reworks the operations on lists of dns servers to be reusable and
generic for use both with the Link and the Manager object.

src/resolve/resolved-conf.c
src/resolve/resolved-dns-server.c
src/resolve/resolved-dns-server.h
src/resolve/resolved-link.c
src/resolve/resolved-link.h
src/resolve/resolved-manager.c
src/resolve/resolved-resolv-conf.c

index dbac451..3fc7d9a 100644 (file)
@@ -40,7 +40,7 @@ int manager_add_dns_server_by_string(Manager *m, DnsServerType type, const char
                 return r;
 
         /* Filter out duplicates */
-        s = manager_find_dns_server(m, type, family, &address);
+        s = dns_server_find(manager_get_first_dns_server(m, type), family, &address);
         if (s) {
                 /*
                  * Drop the marker. This is used to find the servers
@@ -141,7 +141,7 @@ int config_parse_dns_servers(
 
         if (isempty(rvalue))
                 /* Empty assignment means clear the list */
-                manager_flush_dns_servers(m, ltype);
+                dns_server_unlink_all(manager_get_first_dns_server(m, ltype));
         else {
                 /* Otherwise, add to the list */
                 r = manager_parse_dns_server_string_and_warn(m, ltype, rvalue);
index 81301ab..93b9541 100644 (file)
@@ -240,74 +240,64 @@ const struct hash_ops dns_server_hash_ops = {
         .compare = dns_server_compare_func
 };
 
-DnsServer *manager_get_first_dns_server(Manager *m, DnsServerType t) {
-        assert(m);
-
-        switch (t) {
+void dns_server_unlink_all(DnsServer *first) {
+        DnsServer *next;
 
-        case DNS_SERVER_SYSTEM:
-                return m->dns_servers;
+        if (!first)
+                return;
 
-        case DNS_SERVER_FALLBACK:
-                return m->fallback_dns_servers;
+        next = first->servers_next;
+        dns_server_unlink(first);
 
-        default:
-                return NULL;
-        }
+        dns_server_unlink_all(next);
 }
 
-void manager_flush_dns_servers(Manager *m, DnsServerType type) {
-        assert(m);
+void dns_server_unlink_marked(DnsServer *first) {
+        DnsServer *next;
 
-        for (;;) {
-                DnsServer *first;
+        if (!first)
+                return;
 
-                first = manager_get_first_dns_server(m, type);
-                if (!first)
-                        break;
+        next = first->servers_next;
 
+        if (first->marked)
                 dns_server_unlink(first);
-        }
-}
-
-void manager_flush_marked_dns_servers(Manager *m, DnsServerType type) {
-        DnsServer *first, *s, *next;
 
-        assert(m);
-
-        first = manager_get_first_dns_server(m, type);
+        dns_server_unlink_marked(next);
+}
 
-        LIST_FOREACH_SAFE(servers, s, next, first) {
-                if (!s->marked)
-                        continue;
+void dns_server_mark_all(DnsServer *first) {
+        if (!first)
+                return;
 
-                dns_server_unlink(s);
-        }
+        first->marked = true;
+        dns_server_mark_all(first->servers_next);
 }
 
-void manager_mark_dns_servers(Manager *m, DnsServerType type) {
-        DnsServer *first, *s;
+DnsServer *dns_server_find(DnsServer *first, int family, const union in_addr_union *in_addr) {
+        DnsServer *s;
 
-        assert(m);
-
-        first = manager_get_first_dns_server(m, type);
         LIST_FOREACH(servers, s, first)
-                s->marked = true;
-}
+                if (s->family == family && in_addr_equal(family, &s->address, in_addr) > 0)
+                        return s;
 
-DnsServer* manager_find_dns_server(Manager *m, DnsServerType type, int family, const union in_addr_union *in_addr) {
-        DnsServer *first, *s;
+        return NULL;
+}
 
+DnsServer *manager_get_first_dns_server(Manager *m, DnsServerType t) {
         assert(m);
-        assert(in_addr);
 
-        first = manager_get_first_dns_server(m, type);
+        switch (t) {
 
-        LIST_FOREACH(servers, s, first)
-                if (s->family == family && in_addr_equal(family, &s->address, in_addr) > 0)
-                        return s;
+        case DNS_SERVER_SYSTEM:
+                return m->dns_servers;
 
-        return NULL;
+        case DNS_SERVER_FALLBACK:
+                return m->fallback_dns_servers;
+
+        default:
+                return NULL;
+        }
 }
 
 DnsServer *manager_set_dns_server(Manager *m, DnsServer *s) {
index 0077456..3a78d4a 100644 (file)
@@ -72,14 +72,15 @@ void dns_server_move_back_and_unmark(DnsServer *s);
 void dns_server_packet_received(DnsServer *s, usec_t rtt);
 void dns_server_packet_lost(DnsServer *s, usec_t usec);
 
-DnsServer *manager_get_first_dns_server(Manager *m, DnsServerType t);
+DnsServer *dns_server_find(DnsServer *first, int family, const union in_addr_union *in_addr);
+
+void dns_server_unlink_all(DnsServer *first);
+void dns_server_unlink_marked(DnsServer *first);
+void dns_server_mark_all(DnsServer *first);
 
-void manager_flush_dns_servers(Manager *m, DnsServerType t);
-void manager_flush_marked_dns_servers(Manager *m, DnsServerType type);
-void manager_mark_dns_servers(Manager *m, DnsServerType type);
+DnsServer *manager_get_first_dns_server(Manager *m, DnsServerType t);
 
 DnsServer *manager_set_dns_server(Manager *m, DnsServer *s);
-DnsServer *manager_find_dns_server(Manager *m, DnsServerType t, int family, const union in_addr_union *in_addr);
 DnsServer *manager_get_dns_server(Manager *m);
 void manager_next_dns_server(Manager *m);
 
index c367160..ddd9427 100644 (file)
@@ -65,7 +65,7 @@ Link *link_free(Link *l) {
         if (!l)
                 return NULL;
 
-        link_flush_dns_servers(l);
+        dns_server_unlink_marked(l->dns_servers);
         dns_search_domain_unlink_all(l->search_domains);
 
         while (l->addresses)
@@ -162,7 +162,7 @@ static int link_update_dns_servers(Link *l) {
         if (r < 0)
                 goto clear;
 
-        link_mark_dns_servers(l);
+        dns_server_mark_all(l->dns_servers);
 
         STRV_FOREACH(nameserver, nameservers) {
                 union in_addr_union a;
@@ -173,7 +173,7 @@ static int link_update_dns_servers(Link *l) {
                 if (r < 0)
                         goto clear;
 
-                s = link_find_dns_server(l, family, &a);
+                s = dns_server_find(l->dns_servers, family, &a);
                 if (s)
                         dns_server_move_back_and_unmark(s);
                 else {
@@ -183,11 +183,11 @@ static int link_update_dns_servers(Link *l) {
                 }
         }
 
-        link_flush_marked_dns_servers(l);
+        dns_server_unlink_marked(l->dns_servers);
         return 0;
 
 clear:
-        link_flush_dns_servers(l);
+        dns_server_unlink_all(l->dns_servers);
         return r;
 }
 
@@ -314,47 +314,6 @@ LinkAddress *link_find_address(Link *l, int family, const union in_addr_union *i
         return NULL;
 }
 
-void link_flush_dns_servers(Link *l) {
-        assert(l);
-
-        while (l->dns_servers)
-                dns_server_unlink(l->dns_servers);
-}
-
-void link_flush_marked_dns_servers(Link *l) {
-        DnsServer *s, *next;
-
-        assert(l);
-
-        LIST_FOREACH_SAFE(servers, s, next, l->dns_servers) {
-                if (!s->marked)
-                        continue;
-
-                dns_server_unlink(s);
-        }
-}
-
-void link_mark_dns_servers(Link *l) {
-        DnsServer *s;
-
-        assert(l);
-
-        LIST_FOREACH(servers, s, l->dns_servers)
-                s->marked = true;
-}
-
-DnsServer* link_find_dns_server(Link *l, int family, const union in_addr_union *in_addr) {
-        DnsServer *s;
-
-        assert(l);
-        assert(in_addr);
-
-        LIST_FOREACH(servers, s, l->dns_servers)
-                if (s->family == family && in_addr_equal(family, &s->address, in_addr))
-                        return s;
-        return NULL;
-}
-
 DnsServer* link_set_dns_server(Link *l, DnsServer *s) {
         assert(l);
 
index d72461d..a25715d 100644 (file)
@@ -79,17 +79,10 @@ bool link_relevant(Link *l, int family);
 LinkAddress* link_find_address(Link *l, int family, const union in_addr_union *in_addr);
 void link_add_rrs(Link *l, bool force_remove);
 
-void link_flush_dns_servers(Link *l);
-void link_flush_marked_dns_servers(Link *l);
-void link_mark_dns_servers(Link *l);
-
 DnsServer* link_set_dns_server(Link *l, DnsServer *s);
-DnsServer* link_find_dns_server(Link *l, int family, const union in_addr_union *in_addr);
 DnsServer* link_get_dns_server(Link *l);
 void link_next_dns_server(Link *l);
 
-void link_flush_search_domains(Link *l);
-
 int link_address_new(Link *l, LinkAddress **ret, int family, const union in_addr_union *in_addr);
 LinkAddress *link_address_free(LinkAddress *a);
 int link_address_update_rtnl(LinkAddress *a, sd_netlink_message *m);
index 0771ac8..31f042e 100644 (file)
@@ -533,9 +533,8 @@ Manager *manager_free(Manager *m) {
         if (!m)
                 return NULL;
 
-        manager_flush_dns_servers(m, DNS_SERVER_SYSTEM);
-        manager_flush_dns_servers(m, DNS_SERVER_FALLBACK);
-
+        dns_server_unlink_all(m->dns_servers);
+        dns_server_unlink_all(m->fallback_dns_servers);
         dns_search_domain_unlink_all(m->search_domains);
 
         while ((l = hashmap_first(m->links)))
index 2ab0008..f5cce67 100644 (file)
@@ -85,7 +85,7 @@ int manager_read_resolv_conf(Manager *m) {
                 goto clear;
         }
 
-        manager_mark_dns_servers(m, DNS_SERVER_SYSTEM);
+        dns_server_mark_all(m->dns_servers);
         dns_search_domain_mark_all(m->search_domains);
 
         FOREACH_LINE(line, f, r = -errno; goto clear) {
@@ -118,7 +118,7 @@ int manager_read_resolv_conf(Manager *m) {
         /* Flush out all servers and search domains that are still
          * marked. Those are then ones that didn't appear in the new
          * /etc/resolv.conf */
-        manager_flush_marked_dns_servers(m, DNS_SERVER_SYSTEM);
+        dns_server_unlink_marked(m->dns_servers);
         dns_search_domain_unlink_marked(m->search_domains);
 
         /* Whenever /etc/resolv.conf changes, start using the first
@@ -134,7 +134,7 @@ int manager_read_resolv_conf(Manager *m) {
         return 0;
 
 clear:
-        manager_flush_dns_servers(m, DNS_SERVER_SYSTEM);
+        dns_server_unlink_all(m->dns_servers);
         dns_search_domain_unlink_all(m->search_domains);
         return r;
 }