| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <stdio.h> |
| 4 |
|
| 5 |
#include "sd-dhcp-lease.h" |
| 6 |
|
| 7 |
#include "alloc-util.h" |
| 8 |
#include "dns-resolver-internal.h" |
| 9 |
#include "extract-word.h" |
| 10 |
#include "in-addr-util.h" |
| 11 |
#include "network-internal.h" |
| 12 |
#include "string-util.h" |
| 13 |
#include "strv.h" |
| 14 |
|
| 15 |
size_t serialize_in_addrs(FILE *f, |
| 16 |
const struct in_addr *addresses, |
| 17 |
size_t size, |
| 18 |
bool *with_leading_space, |
| 19 |
bool (*predicate)(const struct in_addr *addr)) { |
| 20 |
assert(f); |
| 21 |
assert(addresses); |
| 22 |
|
| 23 |
size_t count = 0; |
| 24 |
bool _space = false; |
| 25 |
if (!with_leading_space) |
| 26 |
with_leading_space = &_space; |
| 27 |
|
| 28 |
for (size_t i = 0; i < size; i++) { |
| 29 |
if (predicate && !predicate(&addresses[i])) |
| 30 |
continue; |
| 31 |
|
| 32 |
if (*with_leading_space) |
| 33 |
fputc(' ', f); |
| 34 |
fputs(IN4_ADDR_TO_STRING(&addresses[i]), f); |
| 35 |
count++; |
| 36 |
*with_leading_space = true; |
| 37 |
} |
| 38 |
|
| 39 |
return count; |
| 40 |
} |
| 41 |
|
| 42 |
int deserialize_in_addrs(struct in_addr **ret, const char *string) { |
| 43 |
_cleanup_free_ struct in_addr *addresses = NULL; |
| 44 |
int size = 0; |
| 45 |
|
| 46 |
assert(ret); |
| 47 |
assert(string); |
| 48 |
|
| 49 |
for (;;) { |
| 50 |
_cleanup_free_ char *word = NULL; |
| 51 |
union in_addr_union a; |
| 52 |
int r; |
| 53 |
|
| 54 |
r = extract_first_word(&string, &word, NULL, 0); |
| 55 |
if (r < 0) |
| 56 |
return r; |
| 57 |
if (r == 0) |
| 58 |
break; |
| 59 |
|
| 60 |
if (in_addr_from_string(AF_INET, word, &a) < 0) |
| 61 |
continue; |
| 62 |
|
| 63 |
if (!GREEDY_REALLOC(addresses, size + 1)) |
| 64 |
return -ENOMEM; |
| 65 |
|
| 66 |
addresses[size++] = a.in; |
| 67 |
} |
| 68 |
|
| 69 |
*ret = size > 0 ? TAKE_PTR(addresses) : NULL; |
| 70 |
|
| 71 |
return size; |
| 72 |
} |
| 73 |
|
| 74 |
void serialize_in6_addrs(FILE *f, const struct in6_addr *addresses, size_t size, bool *with_leading_space) { |
| 75 |
assert(f); |
| 76 |
assert(addresses); |
| 77 |
assert(size); |
| 78 |
|
| 79 |
bool _space = false; |
| 80 |
if (!with_leading_space) |
| 81 |
with_leading_space = &_space; |
| 82 |
|
| 83 |
for (size_t i = 0; i < size; i++) { |
| 84 |
if (*with_leading_space) |
| 85 |
fputc(' ', f); |
| 86 |
fputs(IN6_ADDR_TO_STRING(&addresses[i]), f); |
| 87 |
*with_leading_space = true; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
int deserialize_in6_addrs(struct in6_addr **ret, const char *string) { |
| 92 |
_cleanup_free_ struct in6_addr *addresses = NULL; |
| 93 |
int size = 0; |
| 94 |
|
| 95 |
assert(ret); |
| 96 |
assert(string); |
| 97 |
|
| 98 |
for (;;) { |
| 99 |
_cleanup_free_ char *word = NULL; |
| 100 |
union in_addr_union a; |
| 101 |
int r; |
| 102 |
|
| 103 |
r = extract_first_word(&string, &word, NULL, 0); |
| 104 |
if (r < 0) |
| 105 |
return r; |
| 106 |
if (r == 0) |
| 107 |
break; |
| 108 |
|
| 109 |
if (in_addr_from_string(AF_INET6, word, &a) < 0) |
| 110 |
continue; |
| 111 |
|
| 112 |
if (!GREEDY_REALLOC(addresses, size + 1)) |
| 113 |
return -ENOMEM; |
| 114 |
|
| 115 |
addresses[size++] = a.in6; |
| 116 |
} |
| 117 |
|
| 118 |
*ret = TAKE_PTR(addresses); |
| 119 |
|
| 120 |
return size; |
| 121 |
} |
| 122 |
|
| 123 |
int serialize_dnr(FILE *f, const sd_dns_resolver *dnr, size_t n_dnr, bool *with_leading_space) { |
| 124 |
int r; |
| 125 |
|
| 126 |
bool _space = false; |
| 127 |
if (!with_leading_space) |
| 128 |
with_leading_space = &_space; |
| 129 |
|
| 130 |
int n = 0; |
| 131 |
_cleanup_strv_free_ char **names = NULL; |
| 132 |
r = dns_resolvers_to_dot_strv(dnr, n_dnr, &names); |
| 133 |
if (r < 0) |
| 134 |
return r; |
| 135 |
if (r > 0) |
| 136 |
fputstrv(f, names, NULL, with_leading_space); |
| 137 |
n += r; |
| 138 |
return n; |
| 139 |
} |
| 140 |
|
| 141 |
static int coalesce_dnr(sd_dns_resolver *dnr, size_t n_dnr, int family, const char *auth_name, |
| 142 |
union in_addr_union *addr) { |
| 143 |
assert(dnr || n_dnr == 0); |
| 144 |
assert(auth_name); |
| 145 |
assert(addr); |
| 146 |
|
| 147 |
/* Look through list of DNR for matching resolvers to add our addr to. Since DoT is assumed, no need |
| 148 |
* to compare transports/dohpath/etc. */ |
| 149 |
FOREACH_ARRAY(res, dnr, n_dnr) { |
| 150 |
if (family == res->family && streq(auth_name, res->auth_name)) { |
| 151 |
if (!GREEDY_REALLOC(res->addrs, res->n_addrs + 1)) |
| 152 |
return -ENOMEM; |
| 153 |
res->addrs[res->n_addrs++] = *addr; |
| 154 |
return true; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
return false; |
| 159 |
} |
| 160 |
|
| 161 |
/* Deserialized resolvers are assumed to offer DoT service. */ |
| 162 |
int deserialize_dnr(sd_dns_resolver **ret, const char *string) { |
| 163 |
int r; |
| 164 |
|
| 165 |
assert(ret); |
| 166 |
assert(string); |
| 167 |
|
| 168 |
sd_dns_resolver *dnr = NULL; |
| 169 |
size_t n = 0; |
| 170 |
CLEANUP_ARRAY(dnr, n, dns_resolver_free_array); |
| 171 |
int priority = 0; |
| 172 |
|
| 173 |
for (;;) { |
| 174 |
_cleanup_free_ char *word = NULL; |
| 175 |
|
| 176 |
r = extract_first_word(&string, &word, NULL, 0); |
| 177 |
if (r < 0) |
| 178 |
return r; |
| 179 |
if (r == 0) |
| 180 |
break; |
| 181 |
|
| 182 |
uint16_t port; |
| 183 |
int family; |
| 184 |
_cleanup_free_ union in_addr_union *addr = new(union in_addr_union, 1); |
| 185 |
_cleanup_free_ char *auth_name = NULL; |
| 186 |
|
| 187 |
r = in_addr_port_ifindex_name_from_string_auto(word, &family, addr, &port, NULL, &auth_name); |
| 188 |
if (r < 0) |
| 189 |
return r; |
| 190 |
|
| 191 |
r = coalesce_dnr(dnr, n, family, auth_name, addr); |
| 192 |
if (r < 0) |
| 193 |
return r; |
| 194 |
if (r > 0) |
| 195 |
continue; |
| 196 |
|
| 197 |
if (!GREEDY_REALLOC(dnr, n+1)) |
| 198 |
return -ENOMEM; |
| 199 |
|
| 200 |
priority = n+1; |
| 201 |
dnr[n++] = (sd_dns_resolver) { |
| 202 |
.priority = priority, /* not serialized, but this will preserve the order */ |
| 203 |
.auth_name = TAKE_PTR(auth_name), |
| 204 |
.family = family, |
| 205 |
.addrs = TAKE_PTR(addr), |
| 206 |
.n_addrs = 1, |
| 207 |
.transports = SD_DNS_ALPN_DOT, |
| 208 |
.port = port, |
| 209 |
}; |
| 210 |
} |
| 211 |
|
| 212 |
*ret = TAKE_PTR(dnr); |
| 213 |
return n; |
| 214 |
} |