| 143 | } // namespace |
| 144 | |
| 145 | std::vector<ip_address> resolve(std::string_view host) { |
| 146 | addrinfo hint; |
| 147 | memset(&hint, 0, sizeof(hint)); |
| 148 | hint.ai_socktype = SOCK_STREAM; |
| 149 | hint.ai_family = AF_UNSPEC; |
| 150 | if (host.empty()) |
| 151 | hint.ai_flags = AI_PASSIVE; |
| 152 | addrinfo* tmp = nullptr; |
| 153 | std::string host_str{host.begin(), host.end()}; |
| 154 | if (getaddrinfo(host.empty() ? nullptr : host_str.c_str(), |
| 155 | host.empty() ? dummy_port.data() : nullptr, &hint, &tmp) |
| 156 | != 0) |
| 157 | return {}; |
| 158 | std::unique_ptr<addrinfo, decltype(freeaddrinfo)*> addrs{tmp, freeaddrinfo}; |
| 159 | char buffer[INET6_ADDRSTRLEN]; |
| 160 | std::vector<ip_address> results; |
| 161 | for (auto i = addrs.get(); i != nullptr; i = i->ai_next) { |
| 162 | auto family = fetch_addr_str(buffer, i->ai_addr); |
| 163 | if (family != AF_UNSPEC) { |
| 164 | ip_address ip; |
| 165 | if (auto err = parse(buffer, ip)) |
| 166 | log::net::error("could not parse IP address: {}", buffer); |
| 167 | else |
| 168 | results.emplace_back(ip); |
| 169 | } |
| 170 | } |
| 171 | // TODO: Should we just prefer ipv6 or use a config option? |
| 172 | // std::stable_sort(std::begin(results), std::end(results), |
| 173 | // [](const ip_address& lhs, const ip_address& rhs) { |
| 174 | // return !lhs.embeds_v4() && rhs.embeds_v4(); |
| 175 | // }); |
| 176 | return results; |
| 177 | } |
| 178 | |
| 179 | std::vector<ip_address> resolve(ip_address host) { |
| 180 | return resolve(to_string(host)); |