Skip to content

Commit

Permalink
update multiaddr_to_socketaddr_protocol,
Browse files Browse the repository at this point in the history
start from the beginning instead of the end to be able to abort early.
  • Loading branch information
jxs committed Jul 29, 2023
1 parent e106ad9 commit ac78e67
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 31 deletions.
1 change: 0 additions & 1 deletion protocols/upnp/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

## 0.1.0

- Initial version
48 changes: 18 additions & 30 deletions protocols/upnp/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,39 +543,27 @@ where
/// Fails if the given [`Multiaddr`] does not begin with an IP
/// protocol encapsulating a TCP or UDP port.
fn multiaddr_to_socketaddr_protocol(
mut addr: Multiaddr,
addr: Multiaddr,
) -> Result<(SocketAddr, PortMappingProtocol), ()> {
let mut port = None;
let mut protocol = None;
while let Some(proto) = addr.pop() {
match proto {
multiaddr::Protocol::Ip6(_) => {
// Idg only supports Ipv4.
return Err(());
let mut iter = addr.into_iter();
match iter.next() {
// Idg only supports Ipv4.
Some(multiaddr::Protocol::Ip4(ipv4)) if ipv4.is_private() => match iter.next() {
Some(multiaddr::Protocol::Tcp(port)) => {
return Ok((
SocketAddr::V4(SocketAddrV4::new(ipv4, port)),
PortMappingProtocol::TCP,
));
}
Some(multiaddr::Protocol::Udp(port)) => {
return Ok((
SocketAddr::V4(SocketAddrV4::new(ipv4, port)),
PortMappingProtocol::TCP,
));
}
multiaddr::Protocol::Ip4(ipv4) if ipv4.is_private() => match (port, protocol) {
(Some(port), Some(protocol)) => {
return Ok((SocketAddr::V4(SocketAddrV4::new(ipv4, port)), protocol));
}
_ => return Err(()),
},
multiaddr::Protocol::Tcp(portnum) => match (port, protocol) {
(None, None) => {
port = Some(portnum);
protocol = Some(PortMappingProtocol::TCP);
}
_ => return Err(()),
},
multiaddr::Protocol::Udp(portnum) => match (port, protocol) {
(None, None) => {
port = Some(portnum);
protocol = Some(PortMappingProtocol::UDP);
}
_ => return Err(()),
},

_ => {}
}
},
_ => {}
}
Err(())
}

0 comments on commit ac78e67

Please sign in to comment.