Skip to main content

amaru_network/
socket_addr.rs

1// Copyright 2025 PRAGMA
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::net::SocketAddr;
16
17use amaru_ouroboros::ToSocketAddrs;
18use tokio::net::lookup_host;
19
20pub async fn resolve(addr: ToSocketAddrs) -> std::io::Result<Vec<SocketAddr>> {
21    use ToSocketAddrs::*;
22    let result = match addr {
23        SocketAddrs(addr) => Ok(addr),
24        SocketAddrV4(addr) => Ok(vec![addr.into()]),
25        SocketAddrV6(addr) => Ok(vec![addr.into()]),
26        IpAddrs(addr) => Ok(addr.into_iter().map(|(addr, port)| (addr, port).into()).collect()),
27        IpAddrV4(addr, port) => Ok(vec![(addr, port).into()]),
28        IpAddrV6(addr, port) => Ok(vec![(addr, port).into()]),
29        String(addr) => Ok(lookup_host(&addr).await?.take(100).collect()),
30    };
31    tracing::info!(?result, "resolved addresses");
32    result
33}