Skip to content

Commit be3eb06

Browse files
Merge branch 'master' into discv5.2
2 parents 2c52cd0 + 19342b3 commit be3eb06

File tree

8 files changed

+58
-19
lines changed

8 files changed

+58
-19
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "discv5"
33
authors = ["Age Manning <[email protected]>"]
44
edition = "2018"
5-
version = "0.2.2"
5+
version = "0.3.0"
66
description = "Implementation of the p2p discv5 discovery protocol"
77
license = "Apache-2.0"
88
repository = "https://github.com/sigp/discv5"

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/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
//! A set of configuration parameters to tune the discovery protocol.
12
use crate::{
23
kbucket::MAX_NODES_PER_BUCKET, socket::ListenConfig, Enr, Executor, PermitBanList, RateLimiter,
34
RateLimiterBuilder,
45
};
5-
///! A set of configuration parameters to tune the discovery protocol.
66
use std::time::Duration;
77

88
/// Configuration parameters that define the performance of the discovery network.

src/executor.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
///! A simple trait to allow generic executors or wrappers for spawning the discv5 tasks.
2-
use std::future::Future;
3-
use std::pin::Pin;
1+
//! A simple trait to allow generic executors or wrappers for spawning the discv5 tasks.
2+
use std::{future::Future, pin::Pin};
43

54
pub trait Executor: ExecutorClone {
65
/// Run the given future in the background until it ends.

src/handler/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ impl Handler {
595595
Some((node_address, request_call)) => {
596596
// Verify that the src_addresses match
597597
if node_address.socket_addr != src_address {
598-
trace!("Received a WHOAREYOU packet for a message with a non-expected source. Source {}, expected_source: {} message_nonce {}", src_address, node_address.socket_addr, hex::encode(request_nonce));
598+
debug!("Received a WHOAREYOU packet for a message with a non-expected source. Source {}, expected_source: {} message_nonce {}", src_address, node_address.socket_addr, hex::encode(request_nonce));
599599
// Add the request back if src_address doesn't match
600600
self.active_requests.insert(node_address, request_call);
601601
return;

src/ipmode.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1+
//! A set of configuration parameters to tune the discovery protocol.
12
use crate::{
23
socket::ListenConfig,
34
Enr,
45
IpMode::{DualStack, Ip4, Ip6},
56
};
67
use std::net::SocketAddr;
78

8-
///! A set of configuration parameters to tune the discovery protocol.
9-
109
/// Sets the socket type to be established and also determines the type of ENRs that we will store
1110
/// in our routing table.
1211
/// We store ENR's that have a `get_contractable_addr()` based on the `IpMode` set.

src/service.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1133,7 +1133,8 @@ impl Service {
11331133
return;
11341134
}
11351135
Err(NonContactable { enr }) => {
1136-
error!("Query {} has a non contactable enr: {}", *query_id, enr);
1136+
// This can happen quite often in ipv6 only nodes
1137+
debug!("Query {} has a non contactable enr: {}", *query_id, enr);
11371138
}
11381139
}
11391140
} else {

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)