Skip to content

Commit

Permalink
Attempt to read ipv4-mapped ipv6 to ipv4 if possible (#3221)
Browse files Browse the repository at this point in the history
  • Loading branch information
quentinlesceller authored Feb 14, 2020
1 parent 65e4f7e commit b400a4b
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions p2p/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,15 @@ impl Readable for PeerAddr {
))))
} else {
let ip = try_iter_map_vec!(0..8, |_| reader.read_u16());
let ipv6 = Ipv6Addr::new(ip[0], ip[1], ip[2], ip[3], ip[4], ip[5], ip[6], ip[7]);
let port = reader.read_u16()?;
Ok(PeerAddr(SocketAddr::V6(SocketAddrV6::new(
Ipv6Addr::new(ip[0], ip[1], ip[2], ip[3], ip[4], ip[5], ip[6], ip[7]),
port,
0,
0,
))))
if let Some(ipv4) = ipv6.to_ipv4() {
Ok(PeerAddr(SocketAddr::V4(SocketAddrV4::new(ipv4, port))))
} else {
Ok(PeerAddr(SocketAddr::V6(SocketAddrV6::new(
ipv6, port, 0, 0,
))))
}
}
}
}
Expand Down

0 comments on commit b400a4b

Please sign in to comment.