reth_network_types/peers/
addr.rs

1//! `RLPx` (TCP) and `Discovery` (UDP) sockets of a peer.
2
3use std::net::{IpAddr, SocketAddr};
4
5/// Represents a peer's address information.
6///
7/// # Fields
8///
9/// - `tcp`: A `SocketAddr` representing the peer's data transfer address.
10/// - `udp`: An optional `SocketAddr` representing the peer's discover address. `None` if the peer
11///   is directly connecting to us or the port is the same to `tcp`'s
12#[derive(Debug, Copy, Clone, PartialEq, Eq)]
13pub struct PeerAddr {
14    tcp: SocketAddr,
15    udp: Option<SocketAddr>,
16}
17
18impl PeerAddr {
19    /// Returns the peer's TCP address.
20    pub const fn tcp(&self) -> SocketAddr {
21        self.tcp
22    }
23
24    /// Returns the peer's UDP address.
25    pub const fn udp(&self) -> Option<SocketAddr> {
26        self.udp
27    }
28
29    /// Returns a new `PeerAddr` with the given `tcp` and `udp` addresses.
30    pub const fn new(tcp: SocketAddr, udp: Option<SocketAddr>) -> Self {
31        Self { tcp, udp }
32    }
33
34    /// Returns a new `PeerAddr` with a `tcp` address only.
35    pub const fn from_tcp(tcp: SocketAddr) -> Self {
36        Self { tcp, udp: None }
37    }
38
39    /// Returns a new `PeerAddr` with the given `tcp` and `udp` ports.
40    pub fn new_with_ports(ip: IpAddr, tcp_port: u16, udp_port: Option<u16>) -> Self {
41        let tcp = SocketAddr::new(ip, tcp_port);
42        let udp = udp_port.map(|port| SocketAddr::new(ip, port));
43        Self::new(tcp, udp)
44    }
45}