@@ -8,20 +8,27 @@ use crate::socket::PollAt;
8
8
#[ cfg( feature = "async" ) ]
9
9
use crate :: socket:: WakerRegistration ;
10
10
use crate :: storage:: Empty ;
11
- use crate :: wire:: { IpEndpoint , IpListenEndpoint , IpProtocol , IpRepr , UdpRepr } ;
11
+ use crate :: wire:: { IpEndpoint , IpAddress , IpListenEndpoint , IpProtocol , IpRepr , UdpRepr } ;
12
12
13
13
/// Metadata for a sent or received UDP packet.
14
14
#[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
15
15
#[ derive( Debug , PartialEq , Eq , Clone , Copy ) ]
16
16
pub struct UdpMetadata {
17
17
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 > ,
18
24
pub meta : PacketMeta ,
19
25
}
20
26
21
27
impl < T : Into < IpEndpoint > > From < T > for UdpMetadata {
22
28
fn from ( value : T ) -> Self {
23
29
Self {
24
30
endpoint : value. into ( ) ,
31
+ local_address : None ,
25
32
meta : PacketMeta :: default ( ) ,
26
33
}
27
34
}
@@ -493,6 +500,7 @@ impl<'a> Socket<'a> {
493
500
494
501
let metadata = UdpMetadata {
495
502
endpoint : remote_endpoint,
503
+ local_address : Some ( ip_repr. dst_addr ( ) ) ,
496
504
meta,
497
505
} ;
498
506
@@ -517,19 +525,23 @@ impl<'a> Socket<'a> {
517
525
let hop_limit = self . hop_limit . unwrap_or ( 64 ) ;
518
526
519
527
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 {
523
532
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
+ }
533
545
} ;
534
546
535
547
net_trace ! (
0 commit comments