Skip to content

Commit 61fca79

Browse files
gtedesco-r7rmcconnell-r7
authored andcommitted
Allow frag to create raw packets
1 parent ca5270c commit 61fca79

File tree

3 files changed

+36
-15
lines changed

3 files changed

+36
-15
lines changed

docs/ipv4/IpFrag.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,22 @@
1313
## datagram
1414
```resynth
1515
resynth fn datagram (
16+
raw: bool = false,
1617
=>
1718
*collect_args: bytes,
1819
) -> Pkt;
1920
```
2021
Return the entire datagram without fragmenting it
2122

23+
### Arguments
24+
* 'raw' If true, then omit ethernet header
25+
2226
## fragment
2327
```resynth
2428
resynth fn fragment (
2529
frag_off: u16,
2630
len: u16,
31+
raw: bool = false,
2732
=>
2833
*collect_args: bytes,
2934
) -> Pkt;
@@ -33,11 +38,13 @@ resynth fn fragment (
3338
### Arguments
3439
* `frag_off` Offset in 8-byte blocks
3540
* `len` Length in bytes
41+
* 'raw' If true, then omit ethernet header
3642

3743
## tail
3844
```resynth
3945
resynth fn tail (
4046
frag_off: u16,
47+
raw: bool = false,
4148
=>
4249
*collect_args: bytes,
4350
) -> Pkt;
@@ -47,3 +54,4 @@ resynth fn tail (
4754

4855
### Arguments
4956
* `frag_off` Offset in 8-byte blocks
57+
* 'raw' If true, then omit ethernet header

ezpkt/src/ip4.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@ pub struct IpDgram {
1515

1616
impl IpDgram {
1717
#[must_use]
18-
pub fn new(mut iph: ip_hdr, payload: &[u8]) -> Self {
18+
pub fn new(mut iph: ip_hdr, payload: &[u8], raw: bool) -> Self {
1919
let pkt = Packet::with_capacity(IP_DGRAM_OVERHEAD + payload.len());
2020

21-
pkt.push(eth_hdr::new(
22-
iph.get_saddr().into(),
23-
iph.get_saddr().into(),
24-
ethertype::IPV4,
25-
));
21+
if !raw {
22+
pkt.push(eth_hdr::new(
23+
iph.get_saddr().into(),
24+
iph.get_saddr().into(),
25+
ethertype::IPV4,
26+
));
27+
}
2628

2729
iph.set_tot_len(payload.len() as u16 + IPH_LEN as u16);
2830

@@ -64,26 +66,26 @@ impl IpFrag {
6466
}
6567

6668
/// Offset is in 8-byte blocks, len is in bytes
67-
pub fn fragment(&self, off: u16, len: u16) -> Packet {
69+
pub fn fragment(&self, off: u16, len: u16, raw: bool) -> Packet {
6870
let byte_off = (off as usize) << 3;
6971
let byte_len = (len as usize) << 3;
7072
let byte_end = byte_off + byte_len;
7173
let end = min(byte_end, self.payload.len());
7274
let content = &self.payload[byte_off..end];
7375

74-
IpDgram::new(self.hdr, content)
76+
IpDgram::new(self.hdr, content, raw)
7577
.frag(off, end != self.payload.len())
7678
.into()
7779
}
7880

7981
/// Offset is in 8-byte blocks, include all bytes until the end
80-
pub fn tail(&self, off: u16) -> Packet {
81-
self.fragment(off, self.payload.len() as u16)
82+
pub fn tail(&self, off: u16, raw: bool) -> Packet {
83+
self.fragment(off, self.payload.len() as u16, raw)
8284
}
8385

8486
/// The whole payload in one unfragmented datagram
85-
pub fn datagram(&self) -> Packet {
86-
IpDgram::new(self.hdr, &self.payload[..])
87+
pub fn datagram(&self, raw: bool) -> Packet {
88+
IpDgram::new(self.hdr, &self.payload[..], raw)
8789
.frag(0, false)
8890
.into()
8991
}

src/stdlib/ipv4/mod.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,12 @@ const FRAG_FRAGMENT: FuncDef = func!(
9999
/// ### Arguments
100100
/// * `frag_off` Offset in 8-byte blocks
101101
/// * `len` Length in bytes
102+
/// * 'raw' If true, then omit ethernet header
102103
resynth fn fragment(
103104
frag_off: U16,
104105
len: U16,
105106
=>
107+
raw: Bool = false,
106108
=>
107109
Str
108110
) -> Pkt
@@ -113,8 +115,9 @@ const FRAG_FRAGMENT: FuncDef = func!(
113115

114116
let frag_off: u16 = args.next().into();
115117
let len: u16 = args.next().into();
118+
let raw: bool = args.next().into();
116119

117-
Ok(this.fragment(frag_off, len).into())
120+
Ok(this.fragment(frag_off, len, raw).into())
118121
}
119122
);
120123

@@ -124,9 +127,11 @@ const FRAG_TAIL: FuncDef = func!(
124127
///
125128
/// ### Arguments
126129
/// * `frag_off` Offset in 8-byte blocks
130+
/// * 'raw' If true, then omit ethernet header
127131
resynth fn tail(
128132
frag_off: U16,
129133
=>
134+
raw: Bool = false,
130135
=>
131136
Str
132137
) -> Pkt
@@ -136,24 +141,30 @@ const FRAG_TAIL: FuncDef = func!(
136141
let this: &mut IpFrag = r.as_mut_any().downcast_mut().unwrap();
137142

138143
let frag_off: u16 = args.next().into();
144+
let raw: bool = args.next().into();
139145

140-
Ok(this.tail(frag_off).into())
146+
Ok(this.tail(frag_off, raw).into())
141147
}
142148
);
143149

144150
const FRAG_DATAGRAM: FuncDef = func!(
145151
/// Return the entire datagram without fragmenting it
152+
///
153+
/// ### Arguments
154+
/// * 'raw' If true, then omit ethernet header
146155
resynth fn datagram(
147156
=>
157+
raw: Bool = false,
148158
=>
149159
Str
150160
) -> Pkt
151161
|mut args| {
152162
let obj = args.take_this();
153163
let mut r = obj.borrow_mut();
154164
let this: &mut IpFrag = r.as_mut_any().downcast_mut().unwrap();
165+
let raw: bool = args.next().into();
155166

156-
Ok(this.datagram().into())
167+
Ok(this.datagram(raw).into())
157168
}
158169
);
159170

0 commit comments

Comments
 (0)