Skip to content

Commit 3ccb164

Browse files
committed
Ensure received messages are sent from the proper ports in
statime-netptp.
1 parent b04d486 commit 3ccb164

3 files changed

Lines changed: 72 additions & 23 deletions

File tree

statime-netptp/src/addresses/ipv4.rs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -125,18 +125,29 @@ impl BoundInterface for Ipv4BoundInterface {
125125
}
126126

127127
fn poll_recv(&self, buf: &mut [u8], cx: &mut Context) -> Poll<Result<RecvResult<Self::Addr>>> {
128-
if let Poll::Ready(result) = self.event_socket.poll_recv(buf, cx) {
129-
Poll::Ready(result)
130-
} else {
131-
self.general_socket.poll_recv(buf, cx)
128+
loop {
129+
return if let Poll::Ready(result) = self.event_socket.poll_recv(buf, cx) {
130+
if let Ok(recv_result) = &result
131+
&& recv_result.remote_addr.port() != GENERAL_PORT
132+
&& recv_result.remote_addr.port() != EVENT_PORT
133+
{
134+
// Ignore messages not sent from the PORTS we can send messages to.
135+
// This avoids the potentially confusing situation of a message send
136+
// from a random port triggering a response being send to port 319 or 320.
137+
continue;
138+
}
139+
Poll::Ready(result)
140+
} else {
141+
self.general_socket.poll_recv(buf, cx)
142+
}
143+
.map_ok(|result| RecvResult {
144+
bytes_read: result.bytes_read,
145+
remote_addr: *result.remote_addr.ip(),
146+
local_addr: *result.local_addr.ip(),
147+
timestamp: result.timestamp,
148+
full_timestamp_data: result.full_timestamp_data,
149+
});
132150
}
133-
.map_ok(|result| RecvResult {
134-
bytes_read: result.bytes_read,
135-
remote_addr: *result.remote_addr.ip(),
136-
local_addr: *result.local_addr.ip(),
137-
timestamp: result.timestamp,
138-
full_timestamp_data: result.full_timestamp_data,
139-
})
140151
}
141152

142153
fn poll_recv_timestamp(

statime-netptp/src/addresses/ipv6.rs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -125,18 +125,29 @@ impl BoundInterface for Ipv6BoundInterface {
125125
}
126126

127127
fn poll_recv(&self, buf: &mut [u8], cx: &mut Context) -> Poll<Result<RecvResult<Self::Addr>>> {
128-
if let Poll::Ready(result) = self.event_socket.poll_recv(buf, cx) {
129-
Poll::Ready(result)
130-
} else {
131-
self.general_socket.poll_recv(buf, cx)
128+
loop {
129+
return if let Poll::Ready(result) = self.event_socket.poll_recv(buf, cx) {
130+
if let Ok(recv_result) = &result
131+
&& recv_result.remote_addr.port() != GENERAL_PORT
132+
&& recv_result.remote_addr.port() != EVENT_PORT
133+
{
134+
// Ignore messages not sent from the PORTS we can send messages to.
135+
// This avoids the potentially confusing situation of a message send
136+
// from a random port triggering a response being send to port 319 or 320.
137+
continue;
138+
}
139+
Poll::Ready(result)
140+
} else {
141+
self.general_socket.poll_recv(buf, cx)
142+
}
143+
.map_ok(|result| RecvResult {
144+
bytes_read: result.bytes_read,
145+
remote_addr: *result.remote_addr.ip(),
146+
local_addr: *result.local_addr.ip(),
147+
timestamp: result.timestamp,
148+
full_timestamp_data: result.full_timestamp_data,
149+
});
132150
}
133-
.map_ok(|result| RecvResult {
134-
bytes_read: result.bytes_read,
135-
remote_addr: *result.remote_addr.ip(),
136-
local_addr: *result.local_addr.ip(),
137-
timestamp: result.timestamp,
138-
full_timestamp_data: result.full_timestamp_data,
139-
})
140151
}
141152

142153
fn poll_recv_timestamp(

statime-netptp/src/test.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
use std::net::{Ipv4Addr, Ipv6Addr};
1+
use std::{
2+
net::{Ipv4Addr, Ipv6Addr},
3+
time::Duration,
4+
};
5+
6+
use tokio::net::UdpSocket;
27

38
use crate::NetworkManager;
49

@@ -23,6 +28,17 @@ async fn test_ipv4() {
2328
let result = socket.recv().await.unwrap();
2429
assert_eq!(&*result.bytes_read, [5, 6, 7, 8].as_slice());
2530
assert!(result.timestamp.is_some());
31+
32+
let external = UdpSocket::bind("0.0.0.0:0").await.unwrap();
33+
external
34+
.send_to(&[9, 10, 11, 12], "127.0.0.1:4319")
35+
.await
36+
.unwrap();
37+
assert!(
38+
tokio::time::timeout(Duration::from_millis(100), socket.recv())
39+
.await
40+
.is_err()
41+
);
2642
}
2743

2844
#[tokio::test]
@@ -46,4 +62,15 @@ async fn test_ipv6() {
4662
let result = socket.recv().await.unwrap();
4763
assert_eq!(&*result.bytes_read, [5, 6, 7, 8].as_slice());
4864
assert!(result.timestamp.is_some());
65+
66+
let external = UdpSocket::bind("[::]:0").await.unwrap();
67+
external
68+
.send_to(&[9, 10, 11, 12], "[::1]:4319")
69+
.await
70+
.unwrap();
71+
assert!(
72+
tokio::time::timeout(Duration::from_millis(100), socket.recv())
73+
.await
74+
.is_err()
75+
);
4976
}

0 commit comments

Comments
 (0)