Skip to content

Commit d92dabc

Browse files
authored
fix(relay): expose Deny reason for circuits and reservations
Fixes: #6066 Pull-Request: #6067.
1 parent f4e5a8d commit d92dabc

File tree

4 files changed

+65
-14
lines changed

4 files changed

+65
-14
lines changed

protocols/relay/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
- Remove duplicated forwarding of pending events to connection handler.
44
- Emit `relay::Event::ReservationClosed` when an active reservation is dropped due to the connection closing.
55
See [PR 5869](https://github.com/libp2p/rust-libp2p/pull/5869).
6+
- Include denial reason in `relay::Event::CircuitReqDenied` and `relay::Event::ReservationReqDenied`
7+
See [PR 6067](https://github.com/libp2p/rust-libp2p/pull/6067/files).
68

79
## 0.19.0
810

protocols/relay/src/behaviour.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,10 @@ pub enum Event {
184184
error: inbound_hop::Error,
185185
},
186186
/// An inbound reservation request has been denied.
187-
ReservationReqDenied { src_peer_id: PeerId },
187+
ReservationReqDenied {
188+
src_peer_id: PeerId,
189+
status: StatusCode,
190+
},
188191
/// Denying an inbound reservation request has failed.
189192
#[deprecated(
190193
note = "Will be removed in favor of logging them internally, see <https://github.com/libp2p/rust-libp2p/issues/4757> for details."
@@ -201,6 +204,7 @@ pub enum Event {
201204
CircuitReqDenied {
202205
src_peer_id: PeerId,
203206
dst_peer_id: PeerId,
207+
status: StatusCode,
204208
},
205209
/// Denying an inbound circuit request failed.
206210
#[deprecated(
@@ -481,10 +485,11 @@ impl NetworkBehaviour for Behaviour {
481485
},
482486
));
483487
}
484-
handler::Event::ReservationReqDenied {} => {
488+
handler::Event::ReservationReqDenied { status } => {
485489
self.queued_actions.push_back(ToSwarm::GenerateEvent(
486490
Event::ReservationReqDenied {
487491
src_peer_id: event_source,
492+
status: status.into(),
488493
},
489494
));
490495
}
@@ -592,6 +597,7 @@ impl NetworkBehaviour for Behaviour {
592597
handler::Event::CircuitReqDenied {
593598
circuit_id,
594599
dst_peer_id,
600+
status,
595601
} => {
596602
if let Some(circuit_id) = circuit_id {
597603
self.circuits.remove(circuit_id);
@@ -601,6 +607,7 @@ impl NetworkBehaviour for Behaviour {
601607
.push_back(ToSwarm::GenerateEvent(Event::CircuitReqDenied {
602608
src_peer_id: event_source,
603609
dst_peer_id,
610+
status: status.into(),
604611
}));
605612
}
606613
handler::Event::CircuitReqDenyFailed {
@@ -809,3 +816,31 @@ impl Add<u64> for CircuitId {
809816
CircuitId(self.0 + rhs)
810817
}
811818
}
819+
820+
/// Status code for a relay reservation request that was denied.
821+
#[derive(Debug)]
822+
pub enum StatusCode {
823+
OK,
824+
ReservationRefused,
825+
ResourceLimitExceeded,
826+
PermissionDenied,
827+
ConnectionFailed,
828+
NoReservation,
829+
MalformedMessage,
830+
UnexpectedMessage,
831+
}
832+
833+
impl From<proto::Status> for StatusCode {
834+
fn from(other: proto::Status) -> Self {
835+
match other {
836+
proto::Status::OK => Self::OK,
837+
proto::Status::RESERVATION_REFUSED => Self::ReservationRefused,
838+
proto::Status::RESOURCE_LIMIT_EXCEEDED => Self::ResourceLimitExceeded,
839+
proto::Status::PERMISSION_DENIED => Self::PermissionDenied,
840+
proto::Status::CONNECTION_FAILED => Self::ConnectionFailed,
841+
proto::Status::NO_RESERVATION => Self::NoReservation,
842+
proto::Status::MALFORMED_MESSAGE => Self::MalformedMessage,
843+
proto::Status::UNEXPECTED_MESSAGE => Self::UnexpectedMessage,
844+
}
845+
}
846+
}

protocols/relay/src/behaviour/handler.rs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ pub enum Event {
159159
/// Accepting an inbound reservation request failed.
160160
ReservationReqAcceptFailed { error: inbound_hop::Error },
161161
/// An inbound reservation request has been denied.
162-
ReservationReqDenied {},
162+
ReservationReqDenied { status: proto::Status },
163163
/// Denying an inbound reservation request has failed.
164164
ReservationReqDenyFailed { error: inbound_hop::Error },
165165
/// An inbound reservation has timed out.
@@ -173,6 +173,7 @@ pub enum Event {
173173
CircuitReqDenied {
174174
circuit_id: Option<CircuitId>,
175175
dst_peer_id: PeerId,
176+
status: proto::Status,
176177
},
177178
/// Denying an inbound circuit request failed.
178179
CircuitReqDenyFailed {
@@ -238,9 +239,10 @@ impl fmt::Debug for Event {
238239
.debug_struct("Event::ReservationReqAcceptFailed")
239240
.field("error", error)
240241
.finish(),
241-
Event::ReservationReqDenied {} => {
242-
f.debug_struct("Event::ReservationReqDenied").finish()
243-
}
242+
Event::ReservationReqDenied { status } => f
243+
.debug_struct("Event::ReservationReqDenied")
244+
.field("status", status)
245+
.finish(),
244246
Event::ReservationReqDenyFailed { error } => f
245247
.debug_struct("Event::ReservationReqDenyFailed")
246248
.field("error", error)
@@ -256,10 +258,12 @@ impl fmt::Debug for Event {
256258
Event::CircuitReqDenied {
257259
circuit_id,
258260
dst_peer_id,
261+
status,
259262
} => f
260263
.debug_struct("Event::CircuitReqDenied")
261264
.field("circuit_id", circuit_id)
262265
.field("dst_peer_id", dst_peer_id)
266+
.field("status", status)
263267
.finish(),
264268
Event::CircuitReqDenyFailed {
265269
circuit_id,
@@ -359,7 +363,12 @@ pub struct Handler {
359363
/// Futures accepting an inbound circuit request.
360364
circuit_accept_futures: Futures<Result<CircuitParts, (CircuitId, PeerId, inbound_hop::Error)>>,
361365
/// Futures denying an inbound circuit request.
362-
circuit_deny_futures: Futures<(Option<CircuitId>, PeerId, Result<(), inbound_hop::Error>)>,
366+
circuit_deny_futures: Futures<(
367+
Option<CircuitId>,
368+
PeerId,
369+
proto::Status,
370+
Result<(), inbound_hop::Error>,
371+
)>,
363372
/// Futures relaying data for circuit between two peers.
364373
circuits: Futures<(CircuitId, PeerId, Result<(), std::io::Error>)>,
365374

@@ -479,7 +488,7 @@ impl Handler {
479488

480489
enum ReservationRequestFuture {
481490
Accepting(BoxFuture<'static, Result<(), inbound_hop::Error>>),
482-
Denying(BoxFuture<'static, Result<(), inbound_hop::Error>>),
491+
Denying(BoxFuture<'static, (proto::Status, Result<(), inbound_hop::Error>)>),
483492
}
484493

485494
type Futures<T> = FuturesUnordered<BoxFuture<'static, T>>;
@@ -519,7 +528,11 @@ impl ConnectionHandler for Handler {
519528
if self
520529
.reservation_request_future
521530
.replace(ReservationRequestFuture::Denying(
522-
inbound_reservation_req.deny(status).err_into().boxed(),
531+
inbound_reservation_req
532+
.deny(status)
533+
.err_into()
534+
.map(move |result| (status, result))
535+
.boxed(),
523536
))
524537
.is_some()
525538
{
@@ -554,7 +567,7 @@ impl ConnectionHandler for Handler {
554567
inbound_circuit_req
555568
.deny(status)
556569
.err_into()
557-
.map(move |result| (circuit_id, dst_peer_id, result))
570+
.map(move |result| (circuit_id, dst_peer_id, status, result))
558571
.boxed(),
559572
);
560573
}
@@ -719,7 +732,7 @@ impl ConnectionHandler for Handler {
719732
}
720733

721734
// Deny new circuits.
722-
if let Poll::Ready(Some((circuit_id, dst_peer_id, result))) =
735+
if let Poll::Ready(Some((circuit_id, dst_peer_id, status, result))) =
723736
self.circuit_deny_futures.poll_next_unpin(cx)
724737
{
725738
match result {
@@ -728,6 +741,7 @@ impl ConnectionHandler for Handler {
728741
Event::CircuitReqDenied {
729742
circuit_id,
730743
dst_peer_id,
744+
status,
731745
},
732746
));
733747
}
@@ -838,13 +852,13 @@ impl ConnectionHandler for Handler {
838852
}
839853
}
840854
Some(ReservationRequestFuture::Denying(fut)) => {
841-
if let Poll::Ready(result) = fut.poll_unpin(cx) {
855+
if let Poll::Ready((status, result)) = fut.poll_unpin(cx) {
842856
self.reservation_request_future = None;
843857

844858
match result {
845859
Ok(()) => {
846860
return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(
847-
Event::ReservationReqDenied {},
861+
Event::ReservationReqDenied { status },
848862
))
849863
}
850864
Err(error) => {

protocols/relay/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ mod proto {
3939
};
4040
}
4141

42-
pub use behaviour::{rate_limiter::RateLimiter, Behaviour, CircuitId, Config, Event};
42+
pub use behaviour::{rate_limiter::RateLimiter, Behaviour, CircuitId, Config, Event, StatusCode};
4343
pub use protocol::{HOP_PROTOCOL_NAME, STOP_PROTOCOL_NAME};
4444

4545
/// Types related to the relay protocol inbound.

0 commit comments

Comments
 (0)