Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 12 additions & 46 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions portmapper/src/nat_pmp/protocol/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ impl Response {
pub const RESPONSE_INDICATOR: u8 = 1u8 << 7;

/// Decode a map response.
fn decode_map(buf: &[u8], proto: MapProtocol) -> Response {
fn decode_map(buf: &[u8], proto: MapProtocol) -> Result<Self, Error> {
if buf.len() != Self::MAX_SIZE {
return Err(MalformedSnafu.build());
}

let epoch_bytes = buf[4..8].try_into().expect("slice has the right len");
let epoch_time = u32::from_be_bytes(epoch_bytes);

Expand All @@ -128,13 +132,13 @@ impl Response {
let lifetime_bytes = buf[12..16].try_into().expect("slice has the right len");
let lifetime_seconds = u32::from_be_bytes(lifetime_bytes);

Response::PortMap {
Ok(Response::PortMap {
proto,
epoch_time,
private_port,
external_port,
lifetime_seconds,
}
})
}

/// Decode a response.
Expand Down Expand Up @@ -176,8 +180,8 @@ impl Response {
public_ip: ip_bytes.into(),
}
}
Opcode::MapUdp => Self::decode_map(buf, MapProtocol::Udp),
Opcode::MapTcp => Self::decode_map(buf, MapProtocol::Tcp),
Opcode::MapUdp => Self::decode_map(buf, MapProtocol::Udp)?,
Opcode::MapTcp => Self::decode_map(buf, MapProtocol::Tcp)?,
};

Ok(response)
Expand Down
Loading