Skip to content

Commit

Permalink
Always cause for denied connection
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaseizinger committed Dec 7, 2022
1 parent a269e9d commit eed846b
Show file tree
Hide file tree
Showing 24 changed files with 100 additions and 59 deletions.
17 changes: 17 additions & 0 deletions misc/metrics/src/swarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ pub struct Metrics {
connections_established: Family<ConnectionEstablishedLabels, Counter>,
connections_closed: Family<ConnectionClosedLabels, Counter>,

connections_denied: Family<AddressLabels, Counter>,

new_listen_addr: Family<AddressLabels, Counter>,
expired_listen_addr: Family<AddressLabels, Counter>,

Expand Down Expand Up @@ -60,6 +62,13 @@ impl Metrics {
Box::new(connections_incoming_error.clone()),
);

let connections_denied = Family::default();
sub_registry.register(
"connections_denied",
"Number of denied connections",
Box::new(connections_denied.clone()),
);

let new_listen_addr = Family::default();
sub_registry.register(
"new_listen_addr",
Expand Down Expand Up @@ -128,6 +137,7 @@ impl Metrics {
connections_incoming_error,
connections_established,
connections_closed,
connections_denied,
new_listen_addr,
expired_listen_addr,
listener_closed,
Expand Down Expand Up @@ -269,6 +279,13 @@ impl<TBvEv, THandleErr> super::Recorder<libp2p_swarm::SwarmEvent<TBvEv, THandleE
libp2p_swarm::SwarmEvent::Dialing(_) => {
self.dial_attempt.inc();
}
libp2p_swarm::SwarmEvent::ConnectionDenied { endpoint, .. } => {
self.connections_denied
.get_or_create(&AddressLabels {
protocols: protocol_stack::as_string(endpoint.get_remote_address()),
})
.inc();
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions protocols/autonat/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use libp2p_request_response::{
ProtocolSupport, RequestId, RequestResponse, RequestResponseConfig, RequestResponseEvent,
RequestResponseMessage, ResponseChannel,
};
use libp2p_swarm::behaviour::{ConnectionDenied, THandlerInEvent};
use libp2p_swarm::behaviour::THandlerInEvent;
use libp2p_swarm::{
behaviour::{
AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, ExpiredExternalAddr,
Expand Down Expand Up @@ -461,7 +461,7 @@ impl NetworkBehaviour for Behaviour {
&mut self,
peer: &PeerId,
connected_point: &ConnectedPoint,
) -> Result<Self::ConnectionHandler, ConnectionDenied> {
) -> Result<Self::ConnectionHandler, Box<dyn std::error::Error + Send + 'static>> {
self.inner.new_handler(peer, connected_point)
}

Expand Down
5 changes: 2 additions & 3 deletions protocols/dcutr/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ use libp2p_core::connection::{ConnectedPoint, ConnectionId};
use libp2p_core::multiaddr::Protocol;
use libp2p_core::{Multiaddr, PeerId};
use libp2p_swarm::behaviour::{
ConnectionClosed, ConnectionDenied, ConnectionEstablished, DialFailure, FromSwarm,
THandlerInEvent,
ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm, THandlerInEvent,
};
use libp2p_swarm::dial_opts::{self, DialOpts};
use libp2p_swarm::{
Expand Down Expand Up @@ -216,7 +215,7 @@ impl NetworkBehaviour for Behaviour {
&mut self,
peer: &PeerId,
connected_point: &ConnectedPoint,
) -> Result<Self::ConnectionHandler, ConnectionDenied> {
) -> Result<Self::ConnectionHandler, Box<dyn std::error::Error + Send + 'static>> {
match (
self.awaiting_direct_inbound_connections.entry(*peer),
self.awaiting_direct_outbound_connections.entry(*peer),
Expand Down
4 changes: 2 additions & 2 deletions protocols/floodsub/src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ use crate::FloodsubConfig;
use cuckoofilter::{CuckooError, CuckooFilter};
use fnv::FnvHashSet;
use libp2p_core::{connection::ConnectionId, ConnectedPoint, PeerId};
use libp2p_swarm::behaviour::THandlerInEvent;
use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, FromSwarm};
use libp2p_swarm::behaviour::{ConnectionDenied, THandlerInEvent};
use libp2p_swarm::ConnectionHandler;
use libp2p_swarm::{
dial_opts::DialOpts, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, OneShotHandler,
Expand Down Expand Up @@ -340,7 +340,7 @@ impl NetworkBehaviour for Floodsub {
&mut self,
_: &PeerId,
_: &ConnectedPoint,
) -> Result<Self::ConnectionHandler, ConnectionDenied> {
) -> Result<Self::ConnectionHandler, Box<dyn std::error::Error + Send + 'static>> {
Ok(Default::default())
}

Expand Down
3 changes: 1 addition & 2 deletions protocols/gossipsub/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ use crate::types::{
};
use crate::types::{GossipsubRpc, PeerConnections, PeerKind};
use crate::{rpc_proto, TopicScoreParams};
use libp2p_swarm::behaviour::ConnectionDenied;
use std::{cmp::Ordering::Equal, fmt::Debug};
use wasm_timer::Interval;

Expand Down Expand Up @@ -3302,7 +3301,7 @@ where
&mut self,
_: &PeerId,
_: &ConnectedPoint,
) -> Result<Self::ConnectionHandler, ConnectionDenied> {
) -> Result<Self::ConnectionHandler, Box<dyn std::error::Error + Send + 'static>> {
let protocol_config = ProtocolConfig::new(
self.config.protocol_id().clone(),
self.config.custom_id_version().clone(),
Expand Down
5 changes: 2 additions & 3 deletions protocols/identify/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ use libp2p_core::{
connection::ConnectionId, multiaddr::Protocol, ConnectedPoint, Multiaddr, PeerId, PublicKey,
};
use libp2p_swarm::behaviour::{
ConnectionClosed, ConnectionDenied, ConnectionEstablished, DialFailure, FromSwarm,
THandlerInEvent,
ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm, THandlerInEvent,
};
use libp2p_swarm::{
dial_opts::DialOpts, AddressScore, ConnectionHandler, ConnectionHandlerUpgrErr, DialError,
Expand Down Expand Up @@ -243,7 +242,7 @@ impl NetworkBehaviour for Behaviour {
&mut self,
peer: &PeerId,
_: &ConnectedPoint,
) -> Result<Self::ConnectionHandler, ConnectionDenied> {
) -> Result<Self::ConnectionHandler, Box<dyn std::error::Error + Send + 'static>> {
Ok(Handler::new(
self.config.initial_delay,
self.config.interval,
Expand Down
6 changes: 3 additions & 3 deletions protocols/kad/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ use fnv::{FnvHashMap, FnvHashSet};
use instant::Instant;
use libp2p_core::{connection::ConnectionId, ConnectedPoint, Multiaddr, PeerId};
use libp2p_swarm::behaviour::{
AddressChange, ConnectionClosed, ConnectionDenied, ConnectionEstablished, DialFailure,
ExpiredListenAddr, FromSwarm, NewExternalAddr, NewListenAddr, THandlerInEvent,
AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, ExpiredListenAddr,
FromSwarm, NewExternalAddr, NewListenAddr, THandlerInEvent,
};
use libp2p_swarm::{
dial_opts::{self, DialOpts},
Expand Down Expand Up @@ -1979,7 +1979,7 @@ where
&mut self,
remote_peer_id: &PeerId,
endpoint: &ConnectedPoint,
) -> Result<Self::ConnectionHandler, ConnectionDenied> {
) -> Result<Self::ConnectionHandler, Box<dyn std::error::Error + Send + 'static>> {
Ok(KademliaHandler::new(
KademliaHandlerConfig {
protocol_config: self.protocol_config.clone(),
Expand Down
4 changes: 2 additions & 2 deletions protocols/mdns/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::Config;
use futures::Stream;
use if_watch::IfEvent;
use libp2p_core::{Multiaddr, PeerId};
use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionDenied, FromSwarm};
use libp2p_swarm::behaviour::{ConnectionClosed, FromSwarm};
use libp2p_swarm::{
dummy, ConnectionHandler, NetworkBehaviour, NetworkBehaviourAction, PollParameters,
};
Expand Down Expand Up @@ -172,7 +172,7 @@ where
&mut self,
_: &PeerId,
_: &libp2p_core::ConnectedPoint,
) -> Result<Self::ConnectionHandler, ConnectionDenied> {
) -> Result<Self::ConnectionHandler, Box<dyn std::error::Error + Send + 'static>> {
Ok(dummy::ConnectionHandler)
}

Expand Down
5 changes: 3 additions & 2 deletions protocols/ping/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ mod protocol;
use handler::Handler;
pub use handler::{Config, Failure, Success};
use libp2p_core::{connection::ConnectionId, ConnectedPoint, PeerId};
use libp2p_swarm::behaviour::{ConnectionDenied, THandlerInEvent};
use libp2p_swarm::behaviour::THandlerInEvent;
use libp2p_swarm::{
behaviour::FromSwarm, NetworkBehaviour, NetworkBehaviourAction, PollParameters,
};
Expand Down Expand Up @@ -124,7 +124,8 @@ impl NetworkBehaviour for Behaviour {
&mut self,
_: &PeerId,
_: &ConnectedPoint,
) -> std::result::Result<Self::ConnectionHandler, ConnectionDenied> {
) -> std::result::Result<Self::ConnectionHandler, Box<dyn std::error::Error + Send + 'static>>
{
Ok(Handler::new(self.config.clone()))
}

Expand Down
4 changes: 2 additions & 2 deletions protocols/relay/src/v2/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ use futures::ready;
use futures::stream::StreamExt;
use libp2p_core::connection::ConnectionId;
use libp2p_core::{ConnectedPoint, PeerId};
use libp2p_swarm::behaviour::THandlerInEvent;
use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, FromSwarm};
use libp2p_swarm::behaviour::{ConnectionDenied, THandlerInEvent};
use libp2p_swarm::dial_opts::DialOpts;
use libp2p_swarm::{dummy, ConnectionHandler};
use libp2p_swarm::{
Expand Down Expand Up @@ -159,7 +159,7 @@ impl NetworkBehaviour for Client {
&mut self,
peer: &PeerId,
connected_point: &ConnectedPoint,
) -> Result<Self::ConnectionHandler, ConnectionDenied> {
) -> Result<Self::ConnectionHandler, Box<dyn std::error::Error + Send + 'static>> {
if connected_point.is_relayed() {
if let Some(event) = self.initial_events.remove(peer) {
log::debug!(
Expand Down
4 changes: 2 additions & 2 deletions protocols/relay/src/v2/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ use instant::Instant;
use libp2p_core::connection::ConnectionId;
use libp2p_core::multiaddr::Protocol;
use libp2p_core::{ConnectedPoint, PeerId};
use libp2p_swarm::behaviour::THandlerInEvent;
use libp2p_swarm::behaviour::{ConnectionClosed, FromSwarm};
use libp2p_swarm::behaviour::{ConnectionDenied, THandlerInEvent};
use libp2p_swarm::{
dummy, ConnectionHandlerUpgrErr, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler,
PollParameters,
Expand Down Expand Up @@ -258,7 +258,7 @@ impl NetworkBehaviour for Relay {
&mut self,
_: &PeerId,
endpoint: &ConnectedPoint,
) -> Result<Self::ConnectionHandler, ConnectionDenied> {
) -> Result<Self::ConnectionHandler, Box<dyn std::error::Error + Send + 'static>> {
if endpoint.is_relayed() {
// Deny all substreams on relayed connection.
Ok(Either::Right(dummy::ConnectionHandler))
Expand Down
4 changes: 2 additions & 2 deletions protocols/rendezvous/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ use libp2p_core::connection::ConnectionId;
use libp2p_core::identity::error::SigningError;
use libp2p_core::identity::Keypair;
use libp2p_core::{ConnectedPoint, Multiaddr, PeerId, PeerRecord};
use libp2p_swarm::behaviour::FromSwarm;
use libp2p_swarm::behaviour::THandlerInEvent;
use libp2p_swarm::behaviour::{ConnectionDenied, FromSwarm};
use libp2p_swarm::{
CloseConnection, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters,
};
Expand Down Expand Up @@ -170,7 +170,7 @@ impl NetworkBehaviour for Behaviour {
&mut self,
_: &PeerId,
_: &ConnectedPoint,
) -> Result<Self::ConnectionHandler, ConnectionDenied> {
) -> Result<Self::ConnectionHandler, Box<dyn std::error::Error + Send + 'static>> {
let initial_keep_alive = Duration::from_secs(30);

Ok(SubstreamConnectionHandler::new_outbound_only(
Expand Down
4 changes: 2 additions & 2 deletions protocols/rendezvous/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ use futures::stream::FuturesUnordered;
use futures::{FutureExt, StreamExt};
use libp2p_core::connection::ConnectionId;
use libp2p_core::{ConnectedPoint, PeerId};
use libp2p_swarm::behaviour::FromSwarm;
use libp2p_swarm::behaviour::THandlerInEvent;
use libp2p_swarm::behaviour::{ConnectionDenied, FromSwarm};
use libp2p_swarm::{
CloseConnection, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters,
};
Expand Down Expand Up @@ -116,7 +116,7 @@ impl NetworkBehaviour for Behaviour {
&mut self,
_: &PeerId,
_: &ConnectedPoint,
) -> Result<Self::ConnectionHandler, ConnectionDenied> {
) -> Result<Self::ConnectionHandler, Box<dyn std::error::Error + Send + 'static>> {
let initial_keep_alive = Duration::from_secs(30);

Ok(SubstreamConnectionHandler::new_inbound_only(
Expand Down
4 changes: 2 additions & 2 deletions protocols/request-response/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub use handler::ProtocolSupport;
use futures::channel::oneshot;
use handler::{RequestProtocol, RequestResponseHandler, RequestResponseHandlerEvent};
use libp2p_core::{connection::ConnectionId, ConnectedPoint, Multiaddr, PeerId};
use libp2p_swarm::behaviour::{ConnectionDenied, THandlerInEvent};
use libp2p_swarm::behaviour::THandlerInEvent;
use libp2p_swarm::{
behaviour::{AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm},
dial_opts::DialOpts,
Expand Down Expand Up @@ -698,7 +698,7 @@ where
&mut self,
_: &PeerId,
_: &ConnectedPoint,
) -> Result<Self::ConnectionHandler, ConnectionDenied> {
) -> Result<Self::ConnectionHandler, Box<dyn std::error::Error + Send + 'static>> {
Ok(RequestResponseHandler::new(
self.inbound_protocols.clone(),
self.codec.clone(),
Expand Down
3 changes: 1 addition & 2 deletions swarm-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream {
.unwrap_or_else(|| syn::parse_quote! { ::libp2p::swarm::derive_prelude });

let multiaddr = quote! { #prelude_path::Multiaddr };
let connection_denied = quote! { #prelude_path::ConnectionDenied };
let trait_to_impl = quote! { #prelude_path::NetworkBehaviour };
let either_ident = quote! { #prelude_path::EitherOutput };
let network_behaviour_action = quote! { #prelude_path::NetworkBehaviourAction };
Expand Down Expand Up @@ -560,7 +559,7 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream {
type OutEvent = #out_event_reference;

#[allow(clippy::needless_question_mark)]
fn new_handler(&mut self, peer: &#peer_id, connected_point: &#connected_point) -> Result<Self::ConnectionHandler, #connection_denied> {
fn new_handler(&mut self, peer: &#peer_id, connected_point: &#connected_point) -> Result<Self::ConnectionHandler, Box<dyn std::error::Error + Send + 'static>> {
use #connection_handler;

Ok(#new_handler)
Expand Down
6 changes: 1 addition & 5 deletions swarm/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ pub trait NetworkBehaviour: 'static {
&mut self,
peer: &PeerId,
connected_point: &ConnectedPoint,
) -> Result<Self::ConnectionHandler, ConnectionDenied>;
) -> Result<Self::ConnectionHandler, Box<dyn std::error::Error + Send + 'static>>;

/// Addresses that this behaviour is aware of for this specific peer, and that may allow
/// reaching the peer.
Expand Down Expand Up @@ -370,10 +370,6 @@ pub trait NetworkBehaviour: 'static {
) -> Poll<NetworkBehaviourAction<Self::OutEvent, THandlerInEvent<Self::ConnectionHandler>>>;
}

#[derive(Debug)]
#[non_exhaustive]
pub enum ConnectionDenied {}

/// Parameters passed to `poll()`, that the `NetworkBehaviour` has access to.
pub trait PollParameters {
/// Iterator returned by [`supported_protocols`](PollParameters::supported_protocols).
Expand Down
4 changes: 2 additions & 2 deletions swarm/src/behaviour/either.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

use crate::behaviour::THandlerInEvent;
use crate::behaviour::{
self, inject_from_swarm, NetworkBehaviour, NetworkBehaviourAction, PollParameters,
};
use crate::behaviour::{ConnectionDenied, THandlerInEvent};
use either::Either;
use libp2p_core::{ConnectedPoint, Multiaddr, PeerId};
use std::{task::Context, task::Poll};
Expand All @@ -39,7 +39,7 @@ where
&mut self,
peer: &PeerId,
connected_point: &ConnectedPoint,
) -> Result<Self::ConnectionHandler, ConnectionDenied> {
) -> Result<Self::ConnectionHandler, Box<dyn std::error::Error + Send + 'static>> {
Ok(match self {
Either::Left(a) => Either::Left(a.new_handler(peer, connected_point)?),
Either::Right(b) => Either::Right(b.new_handler(peer, connected_point)?),
Expand Down
4 changes: 2 additions & 2 deletions swarm/src/behaviour/toggle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

use crate::behaviour::THandlerInEvent;
use crate::behaviour::{inject_from_swarm, FromSwarm};
use crate::behaviour::{ConnectionDenied, THandlerInEvent};
use crate::handler::{
ConnectionEvent, ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr,
DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound, KeepAlive,
Expand Down Expand Up @@ -76,7 +76,7 @@ where
&mut self,
peer: &PeerId,
connected_point: &ConnectedPoint,
) -> Result<Self::ConnectionHandler, ConnectionDenied> {
) -> Result<Self::ConnectionHandler, Box<dyn std::error::Error + Send + 'static>> {
Ok(ToggleConnectionHandler {
inner: match self.inner.as_mut() {
None => None,
Expand Down
Loading

0 comments on commit eed846b

Please sign in to comment.