Skip to content
Open
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
1 change: 1 addition & 0 deletions src/iface/interface/tests/ipv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,7 @@ fn test_raw_socket_with_udp_socket(#[case] medium: Medium) {
Ok((
&UDP_PAYLOAD[..],
udp::UdpMetadata {
local_port: Some(68),
local_address: Some(dst_addr.into()),
..IpEndpoint::new(src_addr.into(), 67).into()
}
Expand Down
1 change: 1 addition & 0 deletions src/iface/interface/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ fn test_handle_udp_broadcast(#[case] medium: Medium) {
Ok((
&UDP_PAYLOAD[..],
udp::UdpMetadata {
local_port: Some(68),
local_address: Some(dst_addr),
..IpEndpoint::new(src_ip.into(), 67).into()
}
Expand Down
1 change: 1 addition & 0 deletions src/iface/interface/tests/sixlowpan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ In at rhoncus tortor. Cras blandit tellus diam, varius vestibulum nibh commodo n
Ok((
&udp_data[..],
udp::UdpMetadata {
local_port: Some(6969),
local_address: Some(
Ipv6Address::new(0xfe80, 0, 0, 0, 0x92fc, 0x48c2, 0xa441, 0xfc76).into()
),
Expand Down
12 changes: 6 additions & 6 deletions src/socket/icmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ impl Endpoint {
match *self {
Endpoint::Unspecified => false,
Endpoint::Ident(_) => true,
Endpoint::Tcp(endpoint) => endpoint.port != 0,
Endpoint::Udp(endpoint) => endpoint.port != 0,
Endpoint::Tcp(endpoint) => matches!(endpoint.port, Some(p) if p != 0),
Endpoint::Udp(endpoint) => matches!(endpoint.port, Some(p) if p != 0),
}
}
}
Expand Down Expand Up @@ -449,7 +449,7 @@ impl<'a> Socket<'a> {
&header.dst_addr.into(),
&cx.checksum_caps(),
) {
Ok(repr) => endpoint.port == repr.src_port,
Ok(repr) => endpoint.port == Some(repr.src_port),
Err(_) => false,
}
}
Expand All @@ -469,7 +469,7 @@ impl<'a> Socket<'a> {
&header.dst_addr.into(),
&cx.checksum_caps(),
) {
Ok(repr) => endpoint.port == repr.src_port,
Ok(repr) => endpoint.port == Some(repr.src_port),
Err(_) => false,
}
}
Expand Down Expand Up @@ -511,7 +511,7 @@ impl<'a> Socket<'a> {
&header.dst_addr.into(),
&cx.checksum_caps(),
) {
Ok(repr) => endpoint.port == repr.src_port,
Ok(repr) => endpoint.port == Some(repr.src_port),
Err(_) => false,
}
}
Expand All @@ -531,7 +531,7 @@ impl<'a> Socket<'a> {
&header.dst_addr.into(),
&cx.checksum_caps(),
) {
Ok(repr) => endpoint.port == repr.src_port,
Ok(repr) => endpoint.port == Some(repr.src_port),
Err(_) => false,
}
}
Expand Down
107 changes: 99 additions & 8 deletions src/socket/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -939,12 +939,15 @@ impl<'a> Socket<'a> {
/// This function returns `Err(Error::InvalidState)` if the socket was already open
/// (see [is_open](#method.is_open)), and `Err(Error::Unaddressable)`
/// if the port in the given endpoint is zero.
///
/// A `None` port ([`IpListenEndpoint::ANY_PORT`]) may be used to listen on
/// any port, which is useful for VPN/proxy-like applications.
pub fn listen<T>(&mut self, local_endpoint: T) -> Result<(), ListenError>
where
T: Into<IpListenEndpoint>,
{
let local_endpoint = local_endpoint.into();
if local_endpoint.port == 0 {
if local_endpoint.port == Some(0) {
return Err(ListenError::Unaddressable);
}

Expand Down Expand Up @@ -1029,9 +1032,13 @@ impl<'a> Socket<'a> {
if remote_endpoint.port == 0 || remote_endpoint.addr.is_unspecified() {
return Err(ConnectError::Unaddressable);
}
if local_endpoint.port == 0 {
return Err(ConnectError::Unaddressable);
}

// The local port must be specified (a `None` port means "any port" and is
// only valid for listening) and nonzero.
let local_port = match local_endpoint.port {
Some(port) if port != 0 => port,
_ => return Err(ConnectError::Unaddressable),
};

// If local address is not provided, choose it automatically.
let local_endpoint = IpEndpoint {
Expand All @@ -1046,7 +1053,7 @@ impl<'a> Socket<'a> {
.get_source_address(&remote_endpoint.addr)
.ok_or(ConnectError::Unaddressable)?,
},
port: local_endpoint.port,
port: local_port,
};

if local_endpoint.addr.version() != remote_endpoint.addr.version() {
Expand Down Expand Up @@ -1578,7 +1585,11 @@ impl<'a> Socket<'a> {
Some(addr) => ip_repr.dst_addr() == addr,
None => true,
};
addr_ok && repr.dst_port != 0 && repr.dst_port == self.listen_endpoint.port
let port_ok = match self.listen_endpoint.port {
Some(port) => repr.dst_port == port,
None => true,
};
addr_ok && port_ok && repr.dst_port != 0
}
}

Expand Down Expand Up @@ -1884,7 +1895,9 @@ impl<'a> Socket<'a> {
// Here we need to additionally check `listen_endpoint`, because we want to make sure
// that SYN-RECEIVED was actually converted from the LISTEN state (another possible
// reason is TCP simultaneous open).
(State::SynReceived, TcpControl::Rst) if self.listen_endpoint.port != 0 => {
(State::SynReceived, TcpControl::Rst)
if self.listen_endpoint.port.is_some_and(|p| p != 0) =>
{
tcp_trace!("received RST");
self.tuple = None;
self.set_state(State::Listen);
Expand Down Expand Up @@ -2931,7 +2944,7 @@ mod test {
const REMOTE_PORT: u16 = 49500;
const LISTEN_END: IpListenEndpoint = IpListenEndpoint {
addr: None,
port: LOCAL_PORT,
port: Some(LOCAL_PORT),
};
const TUPLE: Tuple = Tuple {
local: LOCAL_END,
Expand Down Expand Up @@ -3498,6 +3511,84 @@ mod test {
assert_eq!(s.listen(0), Err(ListenError::Unaddressable));
}

#[test]
fn test_listen_any_port() {
let mut s = socket();
assert_eq!(s.listen(IpListenEndpoint::ANY_PORT), Ok(()));
// A socket listening on any port accepts SYN packets to arbitrary ports.
let tcp_repr = TcpRepr {
dst_port: 12345,
control: TcpControl::Syn,
ack_number: None,
..SEND_TEMPL
};
assert!(s.socket.accepts(&mut s.cx, &SEND_IP_TEMPL, &tcp_repr));
}

#[test]
fn test_any_port_handshake_and_data() {
for port in [80, 443] {
let mut s = socket();
s.listen(IpListenEndpoint::ANY_PORT).unwrap();
send!(
s,
TcpRepr {
dst_port: port,
control: TcpControl::Syn,
seq_number: REMOTE_SEQ,
ack_number: None,
..SEND_TEMPL
}
);
assert_eq!(s.local_endpoint().unwrap().port, port);
recv!(
s,
[TcpRepr {
src_port: port,
control: TcpControl::Syn,
seq_number: LOCAL_SEQ,
ack_number: Some(REMOTE_SEQ + 1),
max_seg_size: Some(BASE_MSS),
..RECV_TEMPL
}]
);
send!(
s,
TcpRepr {
dst_port: port,
seq_number: REMOTE_SEQ + 1,
ack_number: Some(LOCAL_SEQ + 1),
..SEND_TEMPL
}
);
assert_eq!(s.state(), State::Established);
send!(
s,
TcpRepr {
dst_port: port,
seq_number: REMOTE_SEQ + 1,
ack_number: Some(LOCAL_SEQ + 1),
payload: b"hello",
..SEND_TEMPL
}
);
let mut data = [0; 5];
assert_eq!(s.recv_slice(&mut data), Ok(5));
assert_eq!(&data, b"hello");
assert_eq!(s.send_slice(b"world"), Ok(5));
recv!(
s,
[TcpRepr {
src_port: port,
seq_number: LOCAL_SEQ + 1,
ack_number: Some(REMOTE_SEQ + 6),
payload: b"world",
..RECV_TEMPL
}]
);
}
}

#[test]
fn test_listen_twice() {
let mut s = socket();
Expand Down
Loading
Loading