| 445 | } |
| 446 | |
| 447 | struct wireaddr * |
| 448 | wireaddr_from_hostname(const tal_t *ctx, |
| 449 | const char *hostname, |
| 450 | const u16 port, bool *no_dns, |
| 451 | struct sockaddr *broken_reply, |
| 452 | const char **err_msg) |
| 453 | { |
| 454 | struct wireaddr *addrs; |
| 455 | struct sockaddr_in6 *sa6; |
| 456 | struct sockaddr_in *sa4; |
| 457 | struct addrinfo *addrinfo, *addrinfos; |
| 458 | struct addrinfo hints; |
| 459 | int gai_err; |
| 460 | |
| 461 | addrs = tal_arr(ctx, struct wireaddr, 0); |
| 462 | if (no_dns) |
| 463 | *no_dns = false; |
| 464 | |
| 465 | /* Don't do lookup on onion addresses. */ |
| 466 | if (strends(hostname, ".onion")) { |
| 467 | u8 *dec = b32_decode(tmpctx, hostname, |
| 468 | strlen(hostname) - strlen(".onion")); |
| 469 | tal_resize(&addrs, 1); |
| 470 | if (tal_count(dec) == TOR_V3_ADDRLEN) { |
| 471 | addrs[0].type = ADDR_TYPE_TOR_V3; |
| 472 | } else { |
| 473 | if (err_msg) |
| 474 | *err_msg = "Invalid Tor address"; |
| 475 | return tal_free(addrs); |
| 476 | } |
| 477 | |
| 478 | addrs[0].addrlen = tal_count(dec); |
| 479 | addrs[0].port = port; |
| 480 | memcpy(addrs[0].addr, dec, tal_count(dec)); |
| 481 | return addrs; |
| 482 | } |
| 483 | |
| 484 | /* Tell them we wanted DNS and fail. */ |
| 485 | if (no_dns) { |
| 486 | if (err_msg) |
| 487 | *err_msg = "Needed DNS, but lookups suppressed"; |
| 488 | *no_dns = true; |
| 489 | return tal_free(addrs); |
| 490 | } |
| 491 | |
| 492 | memset(&hints, 0, sizeof(hints)); |
| 493 | hints.ai_family = AF_UNSPEC; |
| 494 | hints.ai_socktype = SOCK_STREAM; |
| 495 | hints.ai_protocol = 0; |
| 496 | hints.ai_flags = AI_ADDRCONFIG; |
| 497 | gai_err = getaddrinfo(hostname, tal_fmt(tmpctx, "%d", port), |
| 498 | &hints, &addrinfos); |
| 499 | if (gai_err != 0) { |
| 500 | if (err_msg) |
| 501 | *err_msg = gai_strerror(gai_err); |
| 502 | return tal_free(addrs); |
| 503 | } |
| 504 | |