Skip to content

Commit 5a8ce98

Browse files
committed
UDP: Store local and use local address in metadata
Closes: #903
1 parent 5eced2a commit 5a8ce98

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

src/socket/udp.rs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,27 @@ use crate::socket::PollAt;
88
#[cfg(feature = "async")]
99
use crate::socket::WakerRegistration;
1010
use crate::storage::Empty;
11-
use crate::wire::{IpEndpoint, IpListenEndpoint, IpProtocol, IpRepr, UdpRepr};
11+
use crate::wire::{IpEndpoint, IpAddress, IpListenEndpoint, IpProtocol, IpRepr, UdpRepr};
1212

1313
/// Metadata for a sent or received UDP packet.
1414
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
1515
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
1616
pub struct UdpMetadata {
1717
pub endpoint: IpEndpoint,
18+
/// The IP address to which an incoming datagram was sent, or to which an outgoing datagram
19+
/// will be sent. Incoming datagrams always have this set. On outgoing datagrams, if it is not
20+
/// set, and the socket is not bound to a single address anyway, a suitable address will be
21+
/// determined using the algorithms of RFC 6724 (candidate source address selection) or some
22+
/// heuristic (for IPv4).
23+
pub local_address: Option<IpAddress>,
1824
pub meta: PacketMeta,
1925
}
2026

2127
impl<T: Into<IpEndpoint>> From<T> for UdpMetadata {
2228
fn from(value: T) -> Self {
2329
Self {
2430
endpoint: value.into(),
31+
local_address: None,
2532
meta: PacketMeta::default(),
2633
}
2734
}
@@ -493,6 +500,7 @@ impl<'a> Socket<'a> {
493500

494501
let metadata = UdpMetadata {
495502
endpoint: remote_endpoint,
503+
local_address: Some(ip_repr.dst_addr()),
496504
meta,
497505
};
498506

@@ -517,19 +525,23 @@ impl<'a> Socket<'a> {
517525
let hop_limit = self.hop_limit.unwrap_or(64);
518526

519527
let res = self.tx_buffer.dequeue_with(|packet_meta, payload_buf| {
520-
let src_addr = match endpoint.addr {
521-
Some(addr) => addr,
522-
None => match cx.get_source_address(&packet_meta.endpoint.addr) {
528+
let src_addr = if let Some(s) = packet_meta.local_address {
529+
s
530+
} else {
531+
match endpoint.addr {
523532
Some(addr) => addr,
524-
None => {
525-
net_trace!(
526-
"udp:{}:{}: cannot find suitable source address, dropping.",
527-
endpoint,
528-
packet_meta.endpoint
529-
);
530-
return Ok(());
531-
}
532-
},
533+
None => match cx.get_source_address(&packet_meta.endpoint.addr) {
534+
Some(addr) => addr,
535+
None => {
536+
net_trace!(
537+
"udp:{}:{}: cannot find suitable source address, dropping.",
538+
endpoint,
539+
packet_meta.endpoint
540+
);
541+
return Ok(());
542+
}
543+
},
544+
}
533545
};
534546

535547
net_trace!(

0 commit comments

Comments
 (0)