Skip to content

Commit cb0d174

Browse files
committed
Give copy semantics to Hdr
Hdr<T> is only 32bits, we should not be passing references around, it makes things uglier.
1 parent d6f815f commit cb0d174

6 files changed

Lines changed: 62 additions & 54 deletions

File tree

ezpkt/src/dhcp.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl Dhcp {
2121
let mut pkt = Packet::with_capacity(MIN_CAPACITY + capacity);
2222
let dhcp: Hdr<dhcp_hdr> = pkt.push_hdr();
2323

24-
pkt.get_mut_hdr(&dhcp).magic = MAGIC.to_be();
24+
pkt.get_mut_hdr(dhcp).magic = MAGIC.to_be();
2525

2626
Self {
2727
pkt,
@@ -35,73 +35,73 @@ impl Dhcp {
3535

3636
#[must_use]
3737
pub fn op(mut self, op: u8) -> Self {
38-
self.pkt.get_mut_hdr(&self.dhcp).op = op;
38+
self.pkt.get_mut_hdr(self.dhcp).op = op;
3939
self
4040
}
4141

4242
#[must_use]
4343
pub fn htype(mut self, htype: u8) -> Self {
44-
self.pkt.get_mut_hdr(&self.dhcp).htype = htype;
44+
self.pkt.get_mut_hdr(self.dhcp).htype = htype;
4545
self
4646
}
4747

4848
#[must_use]
4949
pub fn hlen(mut self, hlen: u8) -> Self {
50-
self.pkt.get_mut_hdr(&self.dhcp).hlen = hlen;
50+
self.pkt.get_mut_hdr(self.dhcp).hlen = hlen;
5151
self
5252
}
5353

5454
#[must_use]
5555
pub fn hops(mut self, hops: u8) -> Self {
56-
self.pkt.get_mut_hdr(&self.dhcp).hops = hops;
56+
self.pkt.get_mut_hdr(self.dhcp).hops = hops;
5757
self
5858
}
5959

6060
#[must_use]
6161
pub fn xid(mut self, xid: u32) -> Self {
62-
self.pkt.get_mut_hdr(&self.dhcp).xid = xid.to_be();
62+
self.pkt.get_mut_hdr(self.dhcp).xid = xid.to_be();
6363
self
6464
}
6565

6666
#[must_use]
6767
pub fn secs(mut self, secs: u16) -> Self {
68-
self.pkt.get_mut_hdr(&self.dhcp).secs = secs.to_be();
68+
self.pkt.get_mut_hdr(self.dhcp).secs = secs.to_be();
6969
self
7070
}
7171

7272
#[must_use]
7373
pub fn flags(mut self, flags: u16) -> Self {
74-
self.pkt.get_mut_hdr(&self.dhcp).flags = flags.to_be();
74+
self.pkt.get_mut_hdr(self.dhcp).flags = flags.to_be();
7575
self
7676
}
7777

7878
#[must_use]
7979
pub fn ciaddr(mut self, ciaddr: u32) -> Self {
80-
self.pkt.get_mut_hdr(&self.dhcp).ciaddr = ciaddr.to_be();
80+
self.pkt.get_mut_hdr(self.dhcp).ciaddr = ciaddr.to_be();
8181
self
8282
}
8383

8484
#[must_use]
8585
pub fn yiaddr(mut self, yiaddr: u32) -> Self {
86-
self.pkt.get_mut_hdr(&self.dhcp).yiaddr = yiaddr.to_be();
86+
self.pkt.get_mut_hdr(self.dhcp).yiaddr = yiaddr.to_be();
8787
self
8888
}
8989

9090
#[must_use]
9191
pub fn siaddr(mut self, siaddr: u32) -> Self {
92-
self.pkt.get_mut_hdr(&self.dhcp).siaddr = siaddr.to_be();
92+
self.pkt.get_mut_hdr(self.dhcp).siaddr = siaddr.to_be();
9393
self
9494
}
9595

9696
#[must_use]
9797
pub fn giaddr(mut self, giaddr: u32) -> Self {
98-
self.pkt.get_mut_hdr(&self.dhcp).giaddr = giaddr.to_be();
98+
self.pkt.get_mut_hdr(self.dhcp).giaddr = giaddr.to_be();
9999
self
100100
}
101101

102102
#[must_use]
103103
pub fn chaddr<T: AsRef<[u8]>>(mut self, chaddr: T) -> Self {
104-
let dhcp = self.pkt.get_mut_hdr(&self.dhcp);
104+
let dhcp = self.pkt.get_mut_hdr(self.dhcp);
105105
let buf = chaddr.as_ref();
106106
let cplen = std::cmp::min(buf.len(), dhcp.chaddr.len());
107107
dhcp.chaddr[..cplen].copy_from_slice(&buf[..cplen]);
@@ -110,7 +110,7 @@ impl Dhcp {
110110

111111
#[must_use]
112112
pub fn sname<T: AsRef<[u8]>>(mut self, sname: T) -> Self {
113-
let dhcp = self.pkt.get_mut_hdr(&self.dhcp);
113+
let dhcp = self.pkt.get_mut_hdr(self.dhcp);
114114
let buf = sname.as_ref();
115115
let cplen = std::cmp::min(buf.len(), dhcp.sname.len());
116116
dhcp.sname[..cplen].copy_from_slice(&buf[..cplen]);
@@ -119,7 +119,7 @@ impl Dhcp {
119119

120120
#[must_use]
121121
pub fn file<T: AsRef<[u8]>>(mut self, file: T) -> Self {
122-
let dhcp = self.pkt.get_mut_hdr(&self.dhcp);
122+
let dhcp = self.pkt.get_mut_hdr(self.dhcp);
123123
let buf = file.as_ref();
124124
let cplen = std::cmp::min(buf.len(), dhcp.file.len());
125125
dhcp.file[..cplen].copy_from_slice(&buf[..cplen]);
@@ -128,7 +128,7 @@ impl Dhcp {
128128

129129
#[must_use]
130130
pub fn magic(mut self, magic: u32) -> Self {
131-
self.pkt.get_mut_hdr(&self.dhcp).magic = magic.to_be();
131+
self.pkt.get_mut_hdr(self.dhcp).magic = magic.to_be();
132132
self
133133
}
134134

ezpkt/src/icmp4.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ impl IcmpDgram {
3333
let mut pkt = Packet::with_capacity(ICMP_DGRAM_OVERHEAD);
3434

3535
let eth: Hdr<eth_hdr> = pkt.push_hdr();
36-
pkt.get_mut_hdr(&eth)
36+
pkt.get_mut_hdr(eth)
3737
.dst_from_ip(dst)
3838
.src_from_ip(src)
3939
.proto(0x0800);
4040

4141
let ip: Hdr<ip_hdr> = pkt.push_hdr();
42-
pkt.get_mut_hdr(&ip)
42+
pkt.get_mut_hdr(ip)
4343
.init()
4444
.protocol(1)
4545
.saddr(src)
@@ -69,37 +69,37 @@ impl IcmpDgram {
6969
}
7070

7171
fn update_tot_len(mut self) -> Self {
72-
self.pkt.get_mut_hdr(&self.ip)
72+
self.pkt.get_mut_hdr(self.ip)
7373
.tot_len(self.tot_len as u16);
7474
self
7575
}
7676

7777
fn ping(mut self, id: u16, seq: u16, bytes: &[u8]) -> Self {
7878
self = self.push(bytes);
79-
self.pkt.get_mut_hdr(&self.icmp)
79+
self.pkt.get_mut_hdr(self.icmp)
8080
.typ(ICMP_ECHO);
81-
self.pkt.get_mut_hdr(&self.echo)
81+
self.pkt.get_mut_hdr(self.echo)
8282
.id(id)
8383
.seq(seq);
8484

85-
let bytes = self.pkt.bytes_after(&self.ip, self.tot_len);
85+
let bytes = self.pkt.bytes_after(self.ip, self.tot_len);
8686
let csum = ip_csum(bytes);
87-
self.pkt.get_mut_hdr(&self.icmp).csum(csum);
87+
self.pkt.get_mut_hdr(self.icmp).csum(csum);
8888

8989
self
9090
}
9191

9292
fn pong(mut self, id: u16, seq: u16, bytes: &[u8]) -> Self {
9393
self = self.push(bytes);
94-
self.pkt.get_mut_hdr(&self.icmp)
94+
self.pkt.get_mut_hdr(self.icmp)
9595
.typ(ICMP_ECHOREPLY);
96-
self.pkt.get_mut_hdr(&self.echo)
96+
self.pkt.get_mut_hdr(self.echo)
9797
.id(id)
9898
.seq(seq);
9999

100-
let bytes = self.pkt.bytes_after(&self.ip, self.tot_len);
100+
let bytes = self.pkt.bytes_after(self.ip, self.tot_len);
101101
let csum = ip_csum(bytes);
102-
self.pkt.get_mut_hdr(&self.icmp).csum(csum);
102+
self.pkt.get_mut_hdr(self.icmp).csum(csum);
103103

104104
self
105105
}

ezpkt/src/tcp4.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,20 @@ impl TcpSeg {
2727
let mut pkt = Packet::with_capacity(TCPSEG_OVERHEAD);
2828

2929
let eth: Hdr<eth_hdr> = pkt.push_hdr();
30-
pkt.get_mut_hdr(&eth)
30+
pkt.get_mut_hdr(eth)
3131
.dst_from_ip(*dst.ip())
3232
.src_from_ip(*src.ip())
3333
.proto(0x0800);
3434

3535
let ip: Hdr<ip_hdr> = pkt.push_hdr();
36-
pkt.get_mut_hdr(&ip)
36+
pkt.get_mut_hdr(ip)
3737
.init()
3838
.protocol(6)
3939
.saddr(*src.ip())
4040
.daddr(*dst.ip());
4141

4242
let tcp: Hdr<tcp_hdr> = pkt.push_hdr();
43-
pkt.get_mut_hdr(&tcp)
43+
pkt.get_mut_hdr(tcp)
4444
.init()
4545
.seq(st.snd_nxt)
4646
.sport(src.port())
@@ -61,28 +61,28 @@ impl TcpSeg {
6161
}
6262

6363
fn syn(mut self) -> Self {
64-
self.pkt.get_mut_hdr(&self.tcp)
64+
self.pkt.get_mut_hdr(self.tcp)
6565
.syn();
6666
self.seq += 1;
6767
self
6868
}
6969

7070
fn syn_ack(mut self) -> Self {
71-
self.pkt.get_mut_hdr(&self.tcp)
71+
self.pkt.get_mut_hdr(self.tcp)
7272
.syn()
7373
.ack(self.st.rcv_nxt);
7474
self.seq += 1;
7575
self
7676
}
7777

7878
fn ack(mut self) -> Self {
79-
self.pkt.get_mut_hdr(&self.tcp)
79+
self.pkt.get_mut_hdr(self.tcp)
8080
.ack(self.st.rcv_nxt);
8181
self
8282
}
8383

8484
fn push(mut self, bytes: &[u8]) -> Self {
85-
self.pkt.get_mut_hdr(&self.tcp)
85+
self.pkt.get_mut_hdr(self.tcp)
8686
.ack(self.st.rcv_nxt)
8787
.push();
8888
self.seq += bytes.len() as u32;
@@ -96,7 +96,7 @@ impl TcpSeg {
9696
}
9797

9898
fn fin(mut self) -> Self {
99-
self.pkt.get_mut_hdr(&self.tcp)
99+
self.pkt.get_mut_hdr(self.tcp)
100100
.fin();
101101
self.seq += 1;
102102
self
@@ -109,7 +109,7 @@ impl TcpSeg {
109109
}
110110

111111
fn update_tot_len(mut self) -> Self {
112-
self.pkt.get_mut_hdr(&self.ip)
112+
self.pkt.get_mut_hdr(self.ip)
113113
.tot_len(self.tot_len as u16);
114114
self
115115
}

ezpkt/src/udp4.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ impl UdpDgram {
3131
let mut pkt = Packet::with_capacity(UDP_DGRAM_OVERHEAD + payload_sz);
3232

3333
let eth: Hdr<eth_hdr> = pkt.push_hdr();
34-
pkt.get_mut_hdr(&eth)
34+
pkt.get_mut_hdr(eth)
3535
.proto(0x0800);
3636

3737
let ip: Hdr<ip_hdr> = pkt.push_hdr();
38-
pkt.get_mut_hdr(&ip)
38+
pkt.get_mut_hdr(ip)
3939
.init()
4040
.protocol(17);
4141

@@ -62,23 +62,23 @@ impl UdpDgram {
6262

6363
#[must_use]
6464
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());
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());
6868
self
6969
}
7070

7171
#[must_use]
7272
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());
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());
7676
self
7777
}
7878

7979
#[must_use]
8080
pub fn broadcast(mut self) -> Self {
81-
self.pkt.get_mut_hdr(&self.eth).broadcast();
81+
self.pkt.get_mut_hdr(self.eth).broadcast();
8282
self
8383
}
8484

@@ -93,14 +93,14 @@ impl UdpDgram {
9393

9494
#[must_use]
9595
fn update_tot_len(mut self) -> Self {
96-
self.pkt.get_mut_hdr(&self.ip)
96+
self.pkt.get_mut_hdr(self.ip)
9797
.tot_len(self.tot_len as u16);
9898
self
9999
}
100100

101101
#[must_use]
102102
fn update_dgram_len(mut self) -> Self {
103-
self.pkt.get_mut_hdr(&self.udp)
103+
self.pkt.get_mut_hdr(self.udp)
104104
.len(self.dgram_len as u16);
105105
self
106106
}

pkt/src/lib.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ pub struct Hdr<T> {
2323
phantom: std::marker::PhantomData<T>,
2424
}
2525

26+
impl<T> Copy for Hdr<T> { }
27+
28+
impl<T> Clone for Hdr<T> {
29+
fn clone(&self) -> Self {
30+
*self
31+
}
32+
}
33+
2634
impl<T> Hdr<T> {
2735
fn new(off: usize) -> Self {
2836
Self {
@@ -156,7 +164,7 @@ impl Packet {
156164
}
157165

158166
/// Get a reference to the part of the buffer corresponding to this header
159-
pub fn get_hdr<T>(&self, hdr: &Hdr<T>) -> &T {
167+
pub fn get_hdr<T>(&self, hdr: Hdr<T>) -> &T {
160168
let sz = std::mem::size_of::<T>();
161169
let off = hdr.off as usize;
162170
let bytes = &self.buf[off..off + sz];
@@ -167,7 +175,7 @@ impl Packet {
167175
}
168176

169177
/// Get a mutable reference to the part of the buffer corresponding to this header
170-
pub fn get_mut_hdr<T>(&mut self, hdr: &Hdr<T>) -> &mut T {
178+
pub fn get_mut_hdr<T>(&mut self, hdr: Hdr<T>) -> &mut T {
171179
let sz = std::mem::size_of::<T>();
172180
let off = hdr.off as usize;
173181
let bytes = &mut self.buf[off..off + sz];
@@ -193,12 +201,12 @@ impl Packet {
193201
&mut self.buf[off..off + len]
194202
}
195203

196-
pub fn bytes_from<T>(&mut self, hdr: &Hdr<T>, len: usize) -> &mut [u8] {
204+
pub fn bytes_from<T>(&mut self, hdr: Hdr<T>, len: usize) -> &mut [u8] {
197205
let off = hdr.off();
198206
&mut self.buf[off..off + len]
199207
}
200208

201-
pub fn bytes_after<T>(&mut self, hdr: &Hdr<T>, len: usize) -> &mut [u8] {
209+
pub fn bytes_after<T>(&mut self, hdr: Hdr<T>, len: usize) -> &mut [u8] {
202210
let off = hdr.off();
203211
let start = off + hdr.len();
204212
let end = off + len;

pkt/src/pcap.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub enum LinkType {
1111
Ethernet = 1,
1212
}
1313

14-
#[derive(Debug)]
14+
#[derive(Debug, Copy, Clone)]
1515
#[repr(C)]
1616
struct pcap_hdr {
1717
magic: u32,
@@ -26,7 +26,7 @@ struct pcap_hdr {
2626
impl Serialize for pcap_hdr {
2727
}
2828

29-
#[derive(Debug)]
29+
#[derive(Debug, Copy, Clone)]
3030
#[repr(C)]
3131
struct pcap_pkt {
3232
sec: u32,
@@ -94,7 +94,7 @@ impl PcapWriter {
9494
}
9595

9696
let hdr: Hdr<pcap_pkt> = pkt.lower_headroom();
97-
let mut pcap_hdr = pkt.get_mut_hdr(&hdr);
97+
let mut pcap_hdr = pkt.get_mut_hdr(hdr);
9898

9999
pcap_hdr.sec = self.cnt as u32;
100100
pcap_hdr.len = len;

0 commit comments

Comments
 (0)