]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
dd0bc0f1 | 2 | |
ca78ad1d ZJS |
3 | #include <sys/stat.h> |
4 | #include <sys/types.h> | |
5 | #include <unistd.h> | |
6 | ||
dd0bc0f1 LP |
7 | #include "fd-util.h" |
8 | #include "fileio.h" | |
9 | #include "hostname-util.h" | |
dd0bc0f1 | 10 | #include "resolved-dns-synthesize.h" |
7c777a77 | 11 | #include "resolved-etc-hosts.h" |
5c3fa98d | 12 | #include "socket-netlink.h" |
36d892b7 | 13 | #include "stat-util.h" |
dd0bc0f1 LP |
14 | #include "string-util.h" |
15 | #include "strv.h" | |
16 | #include "time-util.h" | |
17 | ||
18 | /* Recheck /etc/hosts at most once every 2s */ | |
19 | #define ETC_HOSTS_RECHECK_USEC (2*USEC_PER_SEC) | |
20 | ||
bb5c7730 YW |
21 | static EtcHostsItemByAddress *etc_hosts_item_by_address_free(EtcHostsItemByAddress *item) { |
22 | if (!item) | |
23 | return NULL; | |
24 | ||
16a6bc5a | 25 | set_free(item->names); |
bb5c7730 | 26 | return mfree(item); |
37b7cc8d | 27 | } |
dd0bc0f1 | 28 | |
e4e34099 YW |
29 | DEFINE_TRIVIAL_CLEANUP_FUNC(EtcHostsItemByAddress*, etc_hosts_item_by_address_free); |
30 | ||
59dad407 YW |
31 | DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR( |
32 | by_address_hash_ops, | |
33 | struct in_addr_data, | |
34 | in_addr_data_hash_func, | |
35 | in_addr_data_compare_func, | |
36 | EtcHostsItemByAddress, | |
37 | etc_hosts_item_by_address_free); | |
38 | ||
bb5c7730 YW |
39 | static EtcHostsItemByName *etc_hosts_item_by_name_free(EtcHostsItemByName *item) { |
40 | if (!item) | |
41 | return NULL; | |
42 | ||
37b7cc8d | 43 | free(item->name); |
16a6bc5a | 44 | set_free(item->addresses); |
bb5c7730 | 45 | return mfree(item); |
37b7cc8d | 46 | } |
dd0bc0f1 | 47 | |
e4e34099 YW |
48 | DEFINE_TRIVIAL_CLEANUP_FUNC(EtcHostsItemByName*, etc_hosts_item_by_name_free); |
49 | ||
59dad407 YW |
50 | DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR( |
51 | by_name_hash_ops, | |
52 | char, | |
53 | dns_name_hash_func, | |
54 | dns_name_compare_func, | |
55 | EtcHostsItemByName, | |
56 | etc_hosts_item_by_name_free); | |
57 | ||
0f7bdf35 YW |
58 | void etc_hosts_clear(EtcHosts *hosts) { |
59 | assert(hosts); | |
60 | ||
59dad407 YW |
61 | hosts->by_address = hashmap_free(hosts->by_address); |
62 | hosts->by_name = hashmap_free(hosts->by_name); | |
232481a0 | 63 | hosts->no_address = set_free(hosts->no_address); |
37b7cc8d | 64 | } |
dd0bc0f1 | 65 | |
37b7cc8d | 66 | void manager_etc_hosts_flush(Manager *m) { |
0f7bdf35 | 67 | etc_hosts_clear(&m->etc_hosts); |
36d892b7 | 68 | m->etc_hosts_stat = (struct stat) {}; |
dd0bc0f1 LP |
69 | } |
70 | ||
37b7cc8d | 71 | static int parse_line(EtcHosts *hosts, unsigned nr, const char *line) { |
0351eb11 YW |
72 | _cleanup_free_ char *address_str = NULL; |
73 | struct in_addr_data address = {}; | |
fd373593 | 74 | bool found = false; |
133eedad | 75 | EtcHostsItemByAddress *item; |
dd0bc0f1 LP |
76 | int r; |
77 | ||
37b7cc8d | 78 | assert(hosts); |
0351eb11 | 79 | assert(line); |
dd0bc0f1 | 80 | |
0351eb11 YW |
81 | r = extract_first_word(&line, &address_str, NULL, EXTRACT_RELAX); |
82 | if (r < 0) | |
bd005277 ZJS |
83 | return log_error_errno(r, "/etc/hosts:%u: failed to extract address: %m", nr); |
84 | assert(r > 0); /* We already checked that the line is not empty, so it should contain *something* */ | |
0351eb11 | 85 | |
7bf8c3de | 86 | r = in_addr_ifindex_from_string_auto(address_str, &address.family, &address.address, NULL); |
bd005277 ZJS |
87 | if (r < 0) { |
88 | log_warning_errno(r, "/etc/hosts:%u: address '%s' is invalid, ignoring: %m", nr, address_str); | |
89 | return 0; | |
90 | } | |
0351eb11 | 91 | |
94876904 | 92 | r = in_addr_data_is_null(&address); |
bd005277 ZJS |
93 | if (r < 0) { |
94 | log_warning_errno(r, "/etc/hosts:%u: address '%s' is invalid, ignoring: %m", nr, address_str); | |
95 | return 0; | |
96 | } | |
dd0bc0f1 LP |
97 | if (r > 0) |
98 | /* This is an 0.0.0.0 or :: item, which we assume means that we shall map the specified hostname to | |
99 | * nothing. */ | |
100 | item = NULL; | |
101 | else { | |
78fc21a1 | 102 | /* If this is a normal address, then simply add entry mapping it to the specified names */ |
dd0bc0f1 | 103 | |
37b7cc8d | 104 | item = hashmap_get(hosts->by_address, &address); |
0351eb11 | 105 | if (!item) { |
e4e34099 | 106 | _cleanup_(etc_hosts_item_by_address_freep) EtcHostsItemByAddress *new_item = NULL; |
dd0bc0f1 | 107 | |
e4e34099 YW |
108 | new_item = new(EtcHostsItemByAddress, 1); |
109 | if (!new_item) | |
dd0bc0f1 LP |
110 | return log_oom(); |
111 | ||
e4e34099 | 112 | *new_item = (EtcHostsItemByAddress) { |
1ed31408 LP |
113 | .address = address, |
114 | }; | |
dd0bc0f1 | 115 | |
59dad407 | 116 | r = hashmap_ensure_put(&hosts->by_address, &by_address_hash_ops, &new_item->address, new_item); |
e4e34099 | 117 | if (r < 0) |
dd0bc0f1 | 118 | return log_oom(); |
e4e34099 YW |
119 | |
120 | item = TAKE_PTR(new_item); | |
dd0bc0f1 LP |
121 | } |
122 | } | |
123 | ||
0351eb11 YW |
124 | for (;;) { |
125 | _cleanup_free_ char *name = NULL; | |
dd0bc0f1 LP |
126 | EtcHostsItemByName *bn; |
127 | ||
0351eb11 YW |
128 | r = extract_first_word(&line, &name, NULL, EXTRACT_RELAX); |
129 | if (r < 0) | |
38b38500 | 130 | return log_error_errno(r, "/etc/hosts:%u: couldn't extract hostname: %m", nr); |
0351eb11 YW |
131 | if (r == 0) |
132 | break; | |
133 | ||
7470cc4c | 134 | r = dns_name_is_valid_ldh(name); |
bd005277 | 135 | if (r <= 0) { |
98b1eb71 YW |
136 | if (r < 0) |
137 | log_warning_errno(r, "/etc/hosts:%u: Failed to check the validity of hostname \"%s\", ignoring: %m", nr, name); | |
138 | else | |
139 | log_warning("/etc/hosts:%u: hostname \"%s\" is not valid, ignoring.", nr, name); | |
bd005277 ZJS |
140 | continue; |
141 | } | |
142 | ||
10b0c818 YW |
143 | found = true; |
144 | ||
fd373593 ZJS |
145 | if (!item) { |
146 | /* Optimize the case where we don't need to store any addresses, by storing | |
147 | * only the name in a dedicated Set instead of the hashmap */ | |
148 | ||
232481a0 | 149 | r = set_ensure_consume(&hosts->no_address, &dns_name_hash_ops_free, TAKE_PTR(name)); |
fd373593 | 150 | if (r < 0) |
10b0c818 | 151 | return log_oom(); |
fd373593 | 152 | |
fd373593 | 153 | continue; |
0351eb11 YW |
154 | } |
155 | ||
37b7cc8d | 156 | bn = hashmap_get(hosts->by_name, name); |
dd0bc0f1 | 157 | if (!bn) { |
e4e34099 | 158 | _cleanup_(etc_hosts_item_by_name_freep) EtcHostsItemByName *new_item = NULL; |
16a6bc5a YW |
159 | _cleanup_free_ char *name_copy = NULL; |
160 | ||
161 | name_copy = strdup(name); | |
162 | if (!name_copy) | |
163 | return log_oom(); | |
dd0bc0f1 | 164 | |
e4e34099 YW |
165 | new_item = new(EtcHostsItemByName, 1); |
166 | if (!new_item) | |
dd0bc0f1 LP |
167 | return log_oom(); |
168 | ||
e4e34099 | 169 | *new_item = (EtcHostsItemByName) { |
16a6bc5a | 170 | .name = TAKE_PTR(name_copy), |
e4e34099 YW |
171 | }; |
172 | ||
59dad407 | 173 | r = hashmap_ensure_put(&hosts->by_name, &by_name_hash_ops, new_item->name, new_item); |
e4e34099 | 174 | if (r < 0) |
dd0bc0f1 | 175 | return log_oom(); |
0351eb11 | 176 | |
e4e34099 | 177 | bn = TAKE_PTR(new_item); |
dd0bc0f1 LP |
178 | } |
179 | ||
16a6bc5a YW |
180 | if (!set_contains(bn->addresses, &address)) { |
181 | _cleanup_free_ struct in_addr_data *address_copy = NULL; | |
182 | ||
183 | address_copy = newdup(struct in_addr_data, &address, 1); | |
184 | if (!address_copy) | |
185 | return log_oom(); | |
dd0bc0f1 | 186 | |
16a6bc5a YW |
187 | r = set_ensure_consume(&bn->addresses, &in_addr_data_hash_ops_free, TAKE_PTR(address_copy)); |
188 | if (r < 0) | |
189 | return log_oom(); | |
190 | } | |
191 | ||
192 | r = set_ensure_consume(&item->names, &dns_name_hash_ops_free, TAKE_PTR(name)); | |
193 | if (r < 0) | |
194 | return log_oom(); | |
dd0bc0f1 LP |
195 | } |
196 | ||
baaa35ad | 197 | if (!found) |
10b0c818 | 198 | log_warning("/etc/hosts:%u: line is missing any valid hostnames", nr); |
dd0bc0f1 | 199 | |
0351eb11 | 200 | return 0; |
dd0bc0f1 LP |
201 | } |
202 | ||
9ca875e8 LP |
203 | static void strip_localhost(EtcHosts *hosts) { |
204 | static const struct in_addr_data local_in_addrs[] = { | |
205 | { | |
206 | .family = AF_INET, | |
207 | #if __BYTE_ORDER == __LITTLE_ENDIAN | |
208 | /* We want constant expressions here, that's why we don't use htole32() here */ | |
209 | .address.in.s_addr = UINT32_C(0x0100007F), | |
210 | #else | |
211 | .address.in.s_addr = UINT32_C(0x7F000001), | |
212 | #endif | |
213 | }, | |
214 | { | |
215 | .family = AF_INET6, | |
216 | .address.in6 = IN6ADDR_LOOPBACK_INIT, | |
217 | }, | |
218 | }; | |
219 | ||
9ca875e8 LP |
220 | assert(hosts); |
221 | ||
222 | /* Removes the 'localhost' entry from what we loaded. But only if the mapping is exclusively between | |
223 | * 127.0.0.1 and localhost (or aliases to that we recognize). If there's any other name assigned to | |
224 | * it, we leave the entry in. | |
225 | * | |
226 | * This way our regular synthesizing can take over, but only if it would result in the exact same | |
227 | * mappings. */ | |
228 | ||
229 | for (size_t j = 0; j < ELEMENTSOF(local_in_addrs); j++) { | |
9f1a1f20 YW |
230 | bool all_localhost, all_local_address; |
231 | EtcHostsItemByAddress *item; | |
16a6bc5a | 232 | const char *name; |
9ca875e8 LP |
233 | |
234 | item = hashmap_get(hosts->by_address, local_in_addrs + j); | |
235 | if (!item) | |
236 | continue; | |
237 | ||
238 | /* Check whether all hostnames the loopback address points to are localhost ones */ | |
239 | all_localhost = true; | |
16a6bc5a YW |
240 | SET_FOREACH(name, item->names) |
241 | if (!is_localhost(name)) { | |
9ca875e8 LP |
242 | all_localhost = false; |
243 | break; | |
244 | } | |
245 | ||
246 | if (!all_localhost) /* Not all names are localhost, hence keep the entries for this address. */ | |
247 | continue; | |
248 | ||
249 | /* Now check if the names listed for this address actually all point back just to this | |
250 | * address (or the other loopback address). If not, let's stay away from this too. */ | |
9f1a1f20 | 251 | all_local_address = true; |
16a6bc5a | 252 | SET_FOREACH(name, item->names) { |
9ca875e8 | 253 | EtcHostsItemByName *n; |
16a6bc5a | 254 | struct in_addr_data *a; |
9ca875e8 | 255 | |
16a6bc5a | 256 | n = hashmap_get(hosts->by_name, name); |
9ca875e8 LP |
257 | if (!n) /* No reverse entry? Then almost certainly the entry already got deleted from |
258 | * the previous iteration of this loop, i.e. via the other protocol */ | |
259 | break; | |
260 | ||
261 | /* Now check if the addresses of this item are all localhost addresses */ | |
16a6bc5a YW |
262 | SET_FOREACH(a, n->addresses) |
263 | if (!in_addr_is_localhost(a->family, &a->address)) { | |
9ca875e8 LP |
264 | all_local_address = false; |
265 | break; | |
266 | } | |
267 | ||
9f1a1f20 | 268 | if (!all_local_address) |
9ca875e8 | 269 | break; |
9ca875e8 LP |
270 | } |
271 | ||
9f1a1f20 | 272 | if (!all_local_address) |
9ca875e8 LP |
273 | continue; |
274 | ||
16a6bc5a YW |
275 | SET_FOREACH(name, item->names) |
276 | etc_hosts_item_by_name_free(hashmap_remove(hosts->by_name, name)); | |
9ca875e8 LP |
277 | |
278 | assert_se(hashmap_remove(hosts->by_address, local_in_addrs + j) == item); | |
133eedad | 279 | etc_hosts_item_by_address_free(item); |
9ca875e8 LP |
280 | } |
281 | } | |
282 | ||
78fc21a1 | 283 | int etc_hosts_parse(EtcHosts *hosts, FILE *f) { |
0f7bdf35 | 284 | _cleanup_(etc_hosts_clear) EtcHosts t = {}; |
37b7cc8d ZJS |
285 | unsigned nr = 0; |
286 | int r; | |
287 | ||
7c777a77 LP |
288 | for (;;) { |
289 | _cleanup_free_ char *line = NULL; | |
37b7cc8d ZJS |
290 | char *l; |
291 | ||
7c777a77 LP |
292 | r = read_line(f, LONG_LINE_MAX, &line); |
293 | if (r < 0) | |
294 | return log_error_errno(r, "Failed to read /etc/hosts: %m"); | |
295 | if (r == 0) | |
296 | break; | |
297 | ||
37b7cc8d ZJS |
298 | nr++; |
299 | ||
bd005277 ZJS |
300 | l = strchr(line, '#'); |
301 | if (l) | |
302 | *l = '\0'; | |
303 | ||
37b7cc8d ZJS |
304 | l = strstrip(line); |
305 | if (isempty(l)) | |
306 | continue; | |
37b7cc8d ZJS |
307 | |
308 | r = parse_line(&t, nr, l); | |
309 | if (r < 0) | |
310 | return r; | |
311 | } | |
312 | ||
9ca875e8 LP |
313 | strip_localhost(&t); |
314 | ||
0f7bdf35 | 315 | etc_hosts_clear(hosts); |
088d71f8 | 316 | *hosts = TAKE_STRUCT(t); |
37b7cc8d ZJS |
317 | return 0; |
318 | } | |
319 | ||
452ca091 | 320 | static int manager_etc_hosts_read(Manager *m) { |
dd0bc0f1 | 321 | _cleanup_fclose_ FILE *f = NULL; |
dd0bc0f1 LP |
322 | struct stat st; |
323 | usec_t ts; | |
dd0bc0f1 LP |
324 | int r; |
325 | ||
ba4e0427 | 326 | assert_se(sd_event_now(m->event, CLOCK_BOOTTIME, &ts) >= 0); |
dd0bc0f1 LP |
327 | |
328 | /* See if we checked /etc/hosts recently already */ | |
329 | if (m->etc_hosts_last != USEC_INFINITY && m->etc_hosts_last + ETC_HOSTS_RECHECK_USEC > ts) | |
330 | return 0; | |
331 | ||
332 | m->etc_hosts_last = ts; | |
333 | ||
36d892b7 | 334 | if (m->etc_hosts_stat.st_mode != 0) { |
dd0bc0f1 | 335 | if (stat("/etc/hosts", &st) < 0) { |
37b7cc8d ZJS |
336 | if (errno != ENOENT) |
337 | return log_error_errno(errno, "Failed to stat /etc/hosts: %m"); | |
dd0bc0f1 | 338 | |
37b7cc8d ZJS |
339 | manager_etc_hosts_flush(m); |
340 | return 0; | |
dd0bc0f1 LP |
341 | } |
342 | ||
aa5408e2 | 343 | /* Did the mtime or ino/dev change? If not, there's no point in re-reading the file. */ |
36d892b7 | 344 | if (stat_inode_unmodified(&m->etc_hosts_stat, &st)) |
dd0bc0f1 LP |
345 | return 0; |
346 | } | |
347 | ||
348 | f = fopen("/etc/hosts", "re"); | |
349 | if (!f) { | |
37b7cc8d ZJS |
350 | if (errno != ENOENT) |
351 | return log_error_errno(errno, "Failed to open /etc/hosts: %m"); | |
dd0bc0f1 | 352 | |
37b7cc8d ZJS |
353 | manager_etc_hosts_flush(m); |
354 | return 0; | |
dd0bc0f1 LP |
355 | } |
356 | ||
357 | /* Take the timestamp at the beginning of processing, so that any changes made later are read on the next | |
358 | * invocation */ | |
359 | r = fstat(fileno(f), &st); | |
360 | if (r < 0) | |
361 | return log_error_errno(errno, "Failed to fstat() /etc/hosts: %m"); | |
362 | ||
37b7cc8d | 363 | r = etc_hosts_parse(&m->etc_hosts, f); |
be19cdf3 | 364 | if (r < 0) |
37b7cc8d | 365 | return r; |
dd0bc0f1 | 366 | |
36d892b7 | 367 | m->etc_hosts_stat = st; |
dd0bc0f1 LP |
368 | m->etc_hosts_last = ts; |
369 | ||
370 | return 1; | |
dd0bc0f1 LP |
371 | } |
372 | ||
0f178863 YW |
373 | static int etc_hosts_lookup_by_address( |
374 | EtcHosts *hosts, | |
375 | DnsQuestion *q, | |
376 | const char *name, | |
377 | const struct in_addr_data *address, | |
378 | DnsAnswer **answer) { | |
379 | ||
380 | DnsResourceKey *t, *found_ptr = NULL; | |
381 | EtcHostsItemByAddress *item; | |
dd0bc0f1 LP |
382 | int r; |
383 | ||
0f178863 | 384 | assert(hosts); |
dd0bc0f1 | 385 | assert(q); |
0f178863 YW |
386 | assert(name); |
387 | assert(address); | |
dd0bc0f1 LP |
388 | assert(answer); |
389 | ||
0f178863 YW |
390 | item = hashmap_get(hosts->by_address, address); |
391 | if (!item) | |
86317087 YW |
392 | return 0; |
393 | ||
0f178863 YW |
394 | /* We have an address in /etc/hosts that matches the queried name. Let's return successful. Actual data |
395 | * we'll only return if the request was for PTR. */ | |
dd0bc0f1 | 396 | |
0f178863 YW |
397 | DNS_QUESTION_FOREACH(t, q) { |
398 | if (!IN_SET(t->type, DNS_TYPE_PTR, DNS_TYPE_ANY)) | |
399 | continue; | |
400 | if (!IN_SET(t->class, DNS_CLASS_IN, DNS_CLASS_ANY)) | |
401 | continue; | |
dd0bc0f1 | 402 | |
0f178863 YW |
403 | r = dns_name_equal(dns_resource_key_name(t), name); |
404 | if (r < 0) | |
405 | return r; | |
406 | if (r > 0) { | |
407 | found_ptr = t; | |
408 | break; | |
409 | } | |
410 | } | |
dd0bc0f1 | 411 | |
0f178863 | 412 | if (found_ptr) { |
16a6bc5a YW |
413 | const char *n; |
414 | ||
415 | r = dns_answer_reserve(answer, set_size(item->names)); | |
0f178863 YW |
416 | if (r < 0) |
417 | return r; | |
dd0bc0f1 | 418 | |
16a6bc5a | 419 | SET_FOREACH(n, item->names) { |
0f178863 | 420 | _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; |
dd0bc0f1 | 421 | |
0f178863 YW |
422 | rr = dns_resource_record_new(found_ptr); |
423 | if (!rr) | |
424 | return -ENOMEM; | |
dd0bc0f1 | 425 | |
16a6bc5a | 426 | rr->ptr.name = strdup(n); |
0f178863 YW |
427 | if (!rr->ptr.name) |
428 | return -ENOMEM; | |
dd0bc0f1 | 429 | |
0f178863 | 430 | r = dns_answer_add(*answer, rr, 0, DNS_ANSWER_AUTHENTICATED, NULL); |
dd0bc0f1 LP |
431 | if (r < 0) |
432 | return r; | |
0f178863 YW |
433 | } |
434 | } | |
dd0bc0f1 | 435 | |
0f178863 YW |
436 | return 1; |
437 | } | |
dd0bc0f1 | 438 | |
0f178863 YW |
439 | static int etc_hosts_lookup_by_name( |
440 | EtcHosts *hosts, | |
441 | DnsQuestion *q, | |
442 | const char *name, | |
443 | DnsAnswer **answer) { | |
dd0bc0f1 | 444 | |
0f178863 | 445 | bool found_a = false, found_aaaa = false; |
16a6bc5a | 446 | const struct in_addr_data *a; |
0f178863 YW |
447 | EtcHostsItemByName *item; |
448 | DnsResourceKey *t; | |
449 | int r; | |
dd0bc0f1 | 450 | |
0f178863 YW |
451 | assert(hosts); |
452 | assert(q); | |
453 | assert(name); | |
454 | assert(answer); | |
dd0bc0f1 | 455 | |
0f178863 YW |
456 | item = hashmap_get(hosts->by_name, name); |
457 | if (item) { | |
16a6bc5a | 458 | r = dns_answer_reserve(answer, set_size(item->addresses)); |
fd373593 ZJS |
459 | if (r < 0) |
460 | return r; | |
461 | } else { | |
462 | /* Check if name was listed with no address. If yes, continue to return an answer. */ | |
0f178863 | 463 | if (!set_contains(hosts->no_address, name)) |
fd373593 ZJS |
464 | return 0; |
465 | } | |
dd0bc0f1 LP |
466 | |
467 | DNS_QUESTION_FOREACH(t, q) { | |
468 | if (!IN_SET(t->type, DNS_TYPE_A, DNS_TYPE_AAAA, DNS_TYPE_ANY)) | |
469 | continue; | |
470 | if (!IN_SET(t->class, DNS_CLASS_IN, DNS_CLASS_ANY)) | |
471 | continue; | |
472 | ||
1c02e7ba | 473 | r = dns_name_equal(dns_resource_key_name(t), name); |
dd0bc0f1 LP |
474 | if (r < 0) |
475 | return r; | |
476 | if (r == 0) | |
477 | continue; | |
478 | ||
479 | if (IN_SET(t->type, DNS_TYPE_A, DNS_TYPE_ANY)) | |
480 | found_a = true; | |
481 | if (IN_SET(t->type, DNS_TYPE_AAAA, DNS_TYPE_ANY)) | |
482 | found_aaaa = true; | |
483 | ||
484 | if (found_a && found_aaaa) | |
485 | break; | |
486 | } | |
487 | ||
863b620a | 488 | SET_FOREACH(a, item ? item->addresses : NULL) { |
dd0bc0f1 LP |
489 | _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; |
490 | ||
16a6bc5a YW |
491 | if ((!found_a && a->family == AF_INET) || |
492 | (!found_aaaa && a->family == AF_INET6)) | |
dd0bc0f1 LP |
493 | continue; |
494 | ||
16a6bc5a | 495 | r = dns_resource_record_new_address(&rr, a->family, &a->address, item->name); |
dd0bc0f1 LP |
496 | if (r < 0) |
497 | return r; | |
498 | ||
04617bf8 | 499 | r = dns_answer_add(*answer, rr, 0, DNS_ANSWER_AUTHENTICATED, NULL); |
dd0bc0f1 LP |
500 | if (r < 0) |
501 | return r; | |
502 | } | |
503 | ||
4050e04b | 504 | return found_a || found_aaaa; |
dd0bc0f1 | 505 | } |
0f178863 YW |
506 | |
507 | int manager_etc_hosts_lookup(Manager *m, DnsQuestion *q, DnsAnswer **answer) { | |
508 | struct in_addr_data k; | |
509 | const char *name; | |
510 | ||
511 | assert(m); | |
512 | assert(q); | |
513 | assert(answer); | |
514 | ||
515 | if (!m->read_etc_hosts) | |
516 | return 0; | |
517 | ||
518 | (void) manager_etc_hosts_read(m); | |
519 | ||
520 | name = dns_question_first_name(q); | |
521 | if (!name) | |
522 | return 0; | |
523 | ||
524 | if (dns_name_address(name, &k.family, &k.address) > 0) | |
525 | return etc_hosts_lookup_by_address(&m->etc_hosts, q, name, &k, answer); | |
526 | ||
527 | return etc_hosts_lookup_by_name(&m->etc_hosts, q, name, answer); | |
528 | } |