Skip to content

Commit 6807647

Browse files
authored
Add some convenience functions for ListenConfig (#189)
* Add some convenient functions for ListenConfig * Another helper function * Rename function * Doc comments and fmt * Remove builder-like functions * Fix docs and tests
1 parent 70bba1e commit 6807647

File tree

2 files changed

+50
-10
lines changed

2 files changed

+50
-10
lines changed

examples/find_nodes.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use discv5::{
2222
Discv5, Discv5ConfigBuilder, Discv5Event, ListenConfig,
2323
};
2424
use std::{
25-
net::{Ipv4Addr, Ipv6Addr},
25+
net::{IpAddr, Ipv4Addr, Ipv6Addr},
2626
time::Duration,
2727
};
2828
use tracing::{info, warn};
@@ -112,8 +112,8 @@ async fn main() {
112112

113113
// the address to listen on.
114114
let listen_config = match args.socket_kind {
115-
SocketKind::Ip4 => ListenConfig::new_ipv4(Ipv4Addr::UNSPECIFIED, port),
116-
SocketKind::Ip6 => ListenConfig::new_ipv6(Ipv6Addr::UNSPECIFIED, port6),
115+
SocketKind::Ip4 => ListenConfig::from_ip(IpAddr::V4(Ipv4Addr::UNSPECIFIED), port),
116+
SocketKind::Ip6 => ListenConfig::from_ip(IpAddr::V6(Ipv6Addr::UNSPECIFIED), port6),
117117
SocketKind::Ds => ListenConfig::default()
118118
.with_ipv4(Ipv4Addr::UNSPECIFIED, port)
119119
.with_ipv6(Ipv6Addr::UNSPECIFIED, port6),

src/socket/mod.rs

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use socket2::{Domain, Protocol, Socket as Socket2, Type};
66
use std::{
77
collections::HashMap,
88
io::Error,
9-
net::{Ipv4Addr, Ipv6Addr, SocketAddr},
9+
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6},
1010
sync::Arc,
1111
time::Duration,
1212
};
@@ -157,14 +157,39 @@ impl Socket {
157157
}
158158

159159
impl ListenConfig {
160-
/// Creates a [`ListenConfig`] with an ipv4-only socket.
161-
pub fn new_ipv4(ip: Ipv4Addr, port: u16) -> ListenConfig {
162-
Self::Ipv4 { ip, port }
160+
/// If an [`IpAddr`] is known, a ListenConfig can be created based on the version. This will
161+
/// not create a dual stack configuration.
162+
pub fn from_ip(ip: IpAddr, port: u16) -> ListenConfig {
163+
match ip {
164+
IpAddr::V4(ip) => ListenConfig::Ipv4 { ip, port },
165+
IpAddr::V6(ip) => ListenConfig::Ipv6 { ip, port },
166+
}
163167
}
164168

165-
/// Creates a [`ListenConfig`] with an ipv6-only socket.
166-
pub fn new_ipv6(ip: Ipv6Addr, port: u16) -> ListenConfig {
167-
Self::Ipv6 { ip, port }
169+
/// Allows optional ipv4 and ipv6 addresses to be entered to create a [`ListenConfig`]. If both
170+
/// are specified a dual-stack configuration will result. This will panic if both parameters
171+
/// are None.
172+
pub fn from_two_sockets(
173+
ipv4: Option<SocketAddrV4>,
174+
ipv6: Option<SocketAddrV6>,
175+
) -> ListenConfig {
176+
match (ipv4, ipv6) {
177+
(Some(ipv4), None) => ListenConfig::Ipv4 {
178+
ip: *ipv4.ip(),
179+
port: ipv4.port(),
180+
},
181+
(None, Some(ipv6)) => ListenConfig::Ipv6 {
182+
ip: *ipv6.ip(),
183+
port: ipv6.port(),
184+
},
185+
(Some(ipv4), Some(ipv6)) => ListenConfig::DualStack {
186+
ipv4: *ipv4.ip(),
187+
ipv4_port: ipv4.port(),
188+
ipv6: *ipv6.ip(),
189+
ipv6_port: ipv6.port(),
190+
},
191+
(None, None) => panic!("At least one IP address must be entered."),
192+
}
168193
}
169194

170195
// Overrides the ipv4 address and port of ipv4 and dual stack configurations. Ipv6
@@ -229,6 +254,21 @@ impl Default for ListenConfig {
229254
}
230255
}
231256

257+
impl From<SocketAddr> for ListenConfig {
258+
fn from(socket_addr: SocketAddr) -> Self {
259+
match socket_addr {
260+
SocketAddr::V4(socket) => ListenConfig::Ipv4 {
261+
ip: *socket.ip(),
262+
port: socket.port(),
263+
},
264+
SocketAddr::V6(socket) => ListenConfig::Ipv6 {
265+
ip: *socket.ip(),
266+
port: socket.port(),
267+
},
268+
}
269+
}
270+
}
271+
232272
impl Drop for Socket {
233273
// close the send/recv handlers
234274
fn drop(&mut self) {

0 commit comments

Comments
 (0)