~ Parsing the child's output: strictly, and fail-closed on anything * unexpected. */
| 218 | /*~ Parsing the child's output: strictly, and fail-closed on anything |
| 219 | * unexpected. */ |
| 220 | static void dns_parse_results(struct dns_lookup *lookup) |
| 221 | { |
| 222 | const u8 *p = lookup->in; |
| 223 | size_t n = lookup->inode; |
| 224 | |
| 225 | if (n == 0) { |
| 226 | lookup->err = tal_fmt(lookup, "dns lookup of %s failed", |
| 227 | lookup->hostname); |
| 228 | return; |
| 229 | } |
| 230 | if (n % DNS_RECORD_LEN != 0) { |
| 231 | lookup->err = tal_fmt(lookup, |
| 232 | "dns resolver returned malformed data"); |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | for (size_t off = 0; off < n; off += DNS_RECORD_LEN) { |
| 237 | struct wireaddr wa; |
| 238 | /* The records are packed at arbitrary byte offsets, so we |
| 239 | * must copy the address into an aligned struct, not cast |
| 240 | * the buffer pointer. */ |
| 241 | struct in_addr ip4; |
| 242 | struct in6_addr ip6; |
| 243 | u16 port = ((u16)p[off + 17] << 8) | p[off + 18]; |
| 244 | |
| 245 | if (p[off] == 1) { |
| 246 | memcpy(&ip4, p + off + 1, sizeof(ip4)); |
| 247 | wireaddr_from_ipv4(&wa, &ip4, port); |
| 248 | } else if (p[off] == 2) { |
| 249 | memcpy(&ip6, p + off + 1, sizeof(ip6)); |
| 250 | wireaddr_from_ipv6(&wa, &ip6, port); |
| 251 | } else { |
| 252 | lookup->err = tal_fmt(lookup, "dns resolver returned" |
| 253 | " unexpected address type %u", |
| 254 | p[off]); |
| 255 | tal_free(lookup->addrs); |
| 256 | lookup->addrs = tal_arr(lookup, struct wireaddr, 0); |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | /* getaddrinfo can hand us repeats: dedup. */ |
| 261 | if (wireaddr_arr_contains(lookup->addrs, &wa)) |
| 262 | continue; |
| 263 | tal_arr_expand(&lookup->addrs, wa); |
| 264 | } |
| 265 | |
| 266 | if (tal_count(lookup->addrs) == 0) |
| 267 | lookup->err = tal_fmt(lookup, "dns lookup of %s returned no" |
| 268 | " addresses", lookup->hostname); |
| 269 | } |
| 270 | |
| 271 | /* The pipe closed: normally that means the child exited (EOF fails the |
| 272 | * conn rather than calling our read callback), and we parse the bytes it |
no test coverage detected