1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2015 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 #include "alloc-util.h"
22 #include "dns-domain.h"
23 #include "resolved-dns-search-domain.h"
25 int dns_search_domain_new(
27 DnsSearchDomain
**ret
,
28 DnsSearchDomainType type
,
32 _cleanup_free_
char *normalized
= NULL
;
37 assert((type
== DNS_SEARCH_DOMAIN_LINK
) == !!l
);
40 r
= dns_name_normalize(name
, &normalized
);
45 if (l
->n_search_domains
>= LINK_SEARCH_DOMAINS_MAX
)
48 if (m
->n_search_domains
>= MANAGER_SEARCH_DOMAINS_MAX
)
52 d
= new0(DnsSearchDomain
, 1);
64 case DNS_SEARCH_DOMAIN_LINK
:
66 LIST_APPEND(domains
, l
->search_domains
, d
);
67 l
->n_search_domains
++;
70 case DNS_SERVER_SYSTEM
:
71 LIST_APPEND(domains
, m
->search_domains
, d
);
72 m
->n_search_domains
++;
76 assert_not_reached("Unknown search domain type");
87 DnsSearchDomain
* dns_search_domain_ref(DnsSearchDomain
*d
) {
97 DnsSearchDomain
* dns_search_domain_unref(DnsSearchDomain
*d
) {
101 assert(d
->n_ref
> 0);
111 void dns_search_domain_unlink(DnsSearchDomain
*d
) {
120 case DNS_SEARCH_DOMAIN_LINK
:
122 assert(d
->link
->n_search_domains
> 0);
123 LIST_REMOVE(domains
, d
->link
->search_domains
, d
);
124 d
->link
->n_search_domains
--;
127 case DNS_SEARCH_DOMAIN_SYSTEM
:
128 assert(d
->manager
->n_search_domains
> 0);
129 LIST_REMOVE(domains
, d
->manager
->search_domains
, d
);
130 d
->manager
->n_search_domains
--;
136 dns_search_domain_unref(d
);
139 void dns_search_domain_move_back_and_unmark(DnsSearchDomain
*d
) {
140 DnsSearchDomain
*tail
;
149 if (!d
->linked
|| !d
->domains_next
)
154 case DNS_SEARCH_DOMAIN_LINK
:
156 LIST_FIND_TAIL(domains
, d
, tail
);
157 LIST_REMOVE(domains
, d
->link
->search_domains
, d
);
158 LIST_INSERT_AFTER(domains
, d
->link
->search_domains
, tail
, d
);
161 case DNS_SEARCH_DOMAIN_SYSTEM
:
162 LIST_FIND_TAIL(domains
, d
, tail
);
163 LIST_REMOVE(domains
, d
->manager
->search_domains
, d
);
164 LIST_INSERT_AFTER(domains
, d
->manager
->search_domains
, tail
, d
);
168 assert_not_reached("Unknown search domain type");
172 void dns_search_domain_unlink_all(DnsSearchDomain
*first
) {
173 DnsSearchDomain
*next
;
178 next
= first
->domains_next
;
179 dns_search_domain_unlink(first
);
181 dns_search_domain_unlink_all(next
);
184 void dns_search_domain_unlink_marked(DnsSearchDomain
*first
) {
185 DnsSearchDomain
*next
;
190 next
= first
->domains_next
;
193 dns_search_domain_unlink(first
);
195 dns_search_domain_unlink_marked(next
);
198 void dns_search_domain_mark_all(DnsSearchDomain
*first
) {
202 first
->marked
= true;
203 dns_search_domain_mark_all(first
->domains_next
);
206 int dns_search_domain_find(DnsSearchDomain
*first
, const char *name
, DnsSearchDomain
**ret
) {
213 LIST_FOREACH(domains
, d
, first
) {
215 r
= dns_name_equal(name
, d
->name
);