@@ -18,6 +18,7 @@ const UDP_DGRAM_OVERHEAD: usize =
1818/// Helper for creating UDP datagrams
1919pub struct UdpDgram {
2020 pkt : Packet ,
21+ eth : Hdr < eth_hdr > ,
2122 ip : Hdr < ip_hdr > ,
2223 udp : Hdr < udp_hdr > ,
2324 tot_len : usize ,
@@ -26,31 +27,25 @@ pub struct UdpDgram {
2627
2728impl UdpDgram {
2829 #[ must_use]
29- pub fn with_capacity ( src : SocketAddrV4 , dst : SocketAddrV4 , payload_sz : usize ) -> Self {
30+ pub fn with_capacity ( payload_sz : usize ) -> Self {
3031 let mut pkt = Packet :: with_capacity ( UDP_DGRAM_OVERHEAD + payload_sz) ;
3132
3233 let eth: Hdr < eth_hdr > = pkt. push_hdr ( ) ;
3334 pkt. get_mut_hdr ( & eth)
34- . dst_from_ip ( * dst. ip ( ) )
35- . src_from_ip ( * src. ip ( ) )
3635 . proto ( 0x0800 ) ;
3736
3837 let ip: Hdr < ip_hdr > = pkt. push_hdr ( ) ;
3938 pkt. get_mut_hdr ( & ip)
4039 . init ( )
41- . protocol ( 17 )
42- . saddr ( * src. ip ( ) )
43- . daddr ( * dst. ip ( ) ) ;
40+ . protocol ( 17 ) ;
4441
4542 let udp: Hdr < udp_hdr > = pkt. push_hdr ( ) ;
46- pkt. get_mut_hdr ( & udp)
47- . sport ( src. port ( ) )
48- . dport ( dst. port ( ) ) ;
4943
5044 let tot_len = ip. len ( ) + udp. len ( ) ;
5145
5246 let ret = Self {
5347 pkt,
48+ eth,
5449 ip,
5550 udp,
5651 tot_len,
@@ -61,15 +56,38 @@ impl UdpDgram {
6156 }
6257
6358 #[ must_use]
64- pub fn new ( src : SocketAddrV4 , dst : SocketAddrV4 ) -> Self {
65- Self :: with_capacity ( src , dst , 0 )
59+ pub fn new ( ) -> Self {
60+ Self :: with_capacity ( 0 )
6661 }
6762
6863 #[ must_use]
69- pub fn push ( mut self , bytes : & [ u8 ] ) -> Self {
70- self . pkt . push_bytes ( bytes) ;
71- self . tot_len += bytes. len ( ) ;
72- self . dgram_len += bytes. len ( ) ;
64+ pub fn src ( mut self , src : SocketAddrV4 ) -> Self {
65+ self . pkt . get_mut_hdr ( & self . eth ) . src_from_ip ( * src. ip ( ) ) ;
66+ self . pkt . get_mut_hdr ( & self . ip ) . saddr ( * src. ip ( ) ) ;
67+ self . pkt . get_mut_hdr ( & self . udp ) . sport ( src. port ( ) ) ;
68+ self
69+ }
70+
71+ #[ must_use]
72+ pub fn dst ( mut self , dst : SocketAddrV4 ) -> Self {
73+ self . pkt . get_mut_hdr ( & self . eth ) . dst_from_ip ( * dst. ip ( ) ) ;
74+ self . pkt . get_mut_hdr ( & self . ip ) . daddr ( * dst. ip ( ) ) ;
75+ self . pkt . get_mut_hdr ( & self . udp ) . dport ( dst. port ( ) ) ;
76+ self
77+ }
78+
79+ #[ must_use]
80+ pub fn broadcast ( mut self ) -> Self {
81+ self . pkt . get_mut_hdr ( & self . eth ) . broadcast ( ) ;
82+ self
83+ }
84+
85+ #[ must_use]
86+ pub fn push < T : AsRef < [ u8 ] > > ( mut self , bytes : T ) -> Self {
87+ let buf = bytes. as_ref ( ) ;
88+ self . pkt . push_bytes ( buf) ;
89+ self . tot_len += buf. len ( ) ;
90+ self . dgram_len += buf. len ( ) ;
7391 self . update_tot_len ( ) . update_dgram_len ( )
7492 }
7593
@@ -88,8 +106,14 @@ impl UdpDgram {
88106 }
89107}
90108
109+ impl Default for UdpDgram {
110+ fn default ( ) -> Self {
111+ Self :: new ( )
112+ }
113+ }
114+
91115impl From < UdpDgram > for Packet {
92- fn from ( seg : UdpDgram ) -> Self {
116+ fn from ( seg : UdpDgram ) -> Self {
93117 seg. pkt
94118 }
95119}
@@ -104,11 +128,11 @@ impl UdpFlow {
104128 }
105129
106130 fn clnt ( & self ) -> UdpDgram {
107- UdpDgram :: new ( self . cl , self . sv )
131+ UdpDgram :: new ( ) . src ( self . cl ) . dst ( self . sv )
108132 }
109133
110134 fn srvr ( & self ) -> UdpDgram {
111- UdpDgram :: new ( self . sv , self . cl )
135+ UdpDgram :: new ( ) . src ( self . sv ) . dst ( self . cl )
112136 }
113137
114138 pub fn client_dgram ( & mut self , bytes : & [ u8 ] ) -> Packet {
0 commit comments