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);
59 d
->name
= TAKE_PTR(normalized
);
63 case DNS_SEARCH_DOMAIN_LINK
:
65 LIST_APPEND(domains
, l
->search_domains
, d
);
66 l
->n_search_domains
++;
69 case DNS_SERVER_SYSTEM
:
70 LIST_APPEND(domains
, m
->search_domains
, d
);
71 m
->n_search_domains
++;
75 assert_not_reached("Unknown search domain type");
86 DnsSearchDomain
* dns_search_domain_ref(DnsSearchDomain
*d
) {
96 DnsSearchDomain
* dns_search_domain_unref(DnsSearchDomain
*d
) {
100 assert(d
->n_ref
> 0);
110 void dns_search_domain_unlink(DnsSearchDomain
*d
) {
119 case DNS_SEARCH_DOMAIN_LINK
:
121 assert(d
->link
->n_search_domains
> 0);
122 LIST_REMOVE(domains
, d
->link
->search_domains
, d
);
123 d
->link
->n_search_domains
--;
126 case DNS_SEARCH_DOMAIN_SYSTEM
:
127 assert(d
->manager
->n_search_domains
> 0);
128 LIST_REMOVE(domains
, d
->manager
->search_domains
, d
);
129 d
->manager
->n_search_domains
--;
135 dns_search_domain_unref(d
);
138 void dns_search_domain_move_back_and_unmark(DnsSearchDomain
*d
) {
139 DnsSearchDomain
*tail
;
148 if (!d
->linked
|| !d
->domains_next
)
153 case DNS_SEARCH_DOMAIN_LINK
:
155 LIST_FIND_TAIL(domains
, d
, tail
);
156 LIST_REMOVE(domains
, d
->link
->search_domains
, d
);
157 LIST_INSERT_AFTER(domains
, d
->link
->search_domains
, tail
, d
);
160 case DNS_SEARCH_DOMAIN_SYSTEM
:
161 LIST_FIND_TAIL(domains
, d
, tail
);
162 LIST_REMOVE(domains
, d
->manager
->search_domains
, d
);
163 LIST_INSERT_AFTER(domains
, d
->manager
->search_domains
, tail
, d
);
167 assert_not_reached("Unknown search domain type");
171 void dns_search_domain_unlink_all(DnsSearchDomain
*first
) {
172 DnsSearchDomain
*next
;
177 next
= first
->domains_next
;
178 dns_search_domain_unlink(first
);
180 dns_search_domain_unlink_all(next
);
183 void dns_search_domain_unlink_marked(DnsSearchDomain
*first
) {
184 DnsSearchDomain
*next
;
189 next
= first
->domains_next
;
192 dns_search_domain_unlink(first
);
194 dns_search_domain_unlink_marked(next
);
197 void dns_search_domain_mark_all(DnsSearchDomain
*first
) {
201 first
->marked
= true;
202 dns_search_domain_mark_all(first
->domains_next
);
205 int dns_search_domain_find(DnsSearchDomain
*first
, const char *name
, DnsSearchDomain
**ret
) {
212 LIST_FOREACH(domains
, d
, first
) {
214 r
= dns_name_equal(name
, d
->name
);