Skip to content

Commit 93c84d7

Browse files
committed
implement NewExternalAddr event handler
1 parent de41d95 commit 93c84d7

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

anchor/network/src/network.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,38 @@ impl<R: MessageReceiver> Network<R> {
216216
}
217217
AnchorBehaviourEvent::Upnp(upnp_event) => {
218218
match upnp_event {
219-
libp2p::upnp::Event::NewExternalAddr(_) => todo!(),
220-
libp2p::upnp::Event::ExpiredExternalAddr(_) => todo!(),
219+
libp2p::upnp::Event::NewExternalAddr(addr) => {
220+
info!(%addr, "UPnP route established");
221+
let mut iter = addr.iter();
222+
let is_ipv6 = {
223+
let addr = iter.next();
224+
matches!(addr, Some(Protocol::Ip6(_)))
225+
};
226+
match iter.next() {
227+
Some(Protocol::Udp(udp_port)) => match iter.next() {
228+
Some(Protocol::QuicV1) => {
229+
if let Err(e) =
230+
self.discovery().try_update_port(false, is_ipv6, udp_port)
231+
{
232+
warn!(error = e, "Failed to update ENR");
233+
}
234+
}
235+
_ => {
236+
trace!(%addr, "UPnP address mapped multiaddr from unknown transport");
237+
}
238+
},
239+
Some(Protocol::Tcp(tcp_port)) => {
240+
if let Err(e) = self.discovery().try_update_port(true, is_ipv6, tcp_port) {
241+
warn!(error = e, "Failed to update ENR");
242+
}
243+
}
244+
_ => {
245+
trace!(%addr, "UPnP address mapped multiaddr from unknown transport");
246+
}
247+
}
248+
249+
},
250+
libp2p::upnp::Event::ExpiredExternalAddr(_) => {},
221251
libp2p::upnp::Event::GatewayNotFound => info!("UPnP not available."),
222252
libp2p::upnp::Event::NonRoutableGateway => info!("UPnP is available but gateway is not exposed to public network"),
223253
}

0 commit comments

Comments
 (0)