1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*!
Nata is a network packet manipulation toolkit.
This library takes inspiration from Python's [Scapy](https://scapy.net/).
Nata enables extensible parsing and crafting of network packets.
# Layer
A Layer represents the layout structure of a specific protocol (such as [Tcp](crate::layer::tcp::Tcp)).
Nata has [layer implementations](./layer/trait.LayerExt.html#implementors) for many core network protocols.
For custom protocols or those implemented in nata already, see [layer](crate::layer) for examples on adding a new layer.
If you think a protocol should be included by default in nata, consider contributing! See [here](https://github.com/sharksforarms/nata) for more information.
## Example
```rust
use nata::layer::LayerExt;
use nata::layer::ether::{Ether, EtherType, MacAddress};
# use hexlit::hex;
let data: &[u8] = &hex!("feff200001000000010000000800");
let (_rest, ether) = Ether::parse(data).unwrap();
assert_eq!(Ether {
src: MacAddress([0x00, 0x00, 0x01, 0x00, 0x00, 0x00]),
dst: MacAddress([0xfe, 0xff, 0x20, 0x00, 0x01, 0x00]),
ether_type: EtherType::IPv4,
}, ether);
let ether_bytes = ether.to_bytes().unwrap();
assert_eq!(data, ether_bytes);
```
# Packet
Data sent over a network such as the Internet, are split up into packets.
A [Packet](crate::packet::Packet) is defined as a collection of
[Layer](crate::layer::Layer).
## Example
```rust
use nata::packet::Packet;
use nata::layer::{
LayerExt,
LayerOwned,
ether::Ether,
ip::ipv4::Ipv4,
tcp::Tcp,
raw::Raw,
};
let layers: Vec<LayerOwned> = vec![
Box::new(Ether::default()),
Box::new(Ipv4::default()),
Box::new(Tcp::default()),
Box::new(Raw::parse(b"hello world").unwrap().1),
];
let mut packet = Packet::from_layers(layers);
// Update length fields, checksums, etc.
packet.finalize().unwrap();
```
# Packet Parser
The packet parser defines the heuristics on which layer to parse next, given the current layer and
the remaining bytes.
Nata provides default layer bindings for layers it implements. These can be found [here](crate::packet::bindings).
```rust
use nata::packet::PacketParser;
use nata::layer::{
Layer,
LayerExt,
ether::Ether,
ip::ipv4::Ipv4,
tcp::Tcp,
};
use nata::is_layer;
# use hexlit::hex;
# use nata::layer::{LayerOwned, LayerError};
// My custom Http layer
#[derive(Debug, Clone)]
struct Http {}
impl Layer for Http {}
impl LayerExt for Http {
// ...
# fn finalize(&mut self, prev: &[LayerOwned], _next: &[LayerOwned]) -> Result<(), LayerError> {
# Ok(())
# }
#
# fn parse(input: &[u8]) -> Result<(&[u8], Self), LayerError>
# where
# Self: Sized,
# {
# let http = Http {};
# Ok(([].as_ref(), http))
# }
#
# fn to_bytes(&self) -> Result<Vec<u8>, LayerError> {
# unimplemented!()
# }
}
let mut pb = PacketParser::new();
// Add a layer binding to `Tcp`
// if the current layer is Tcp and the destination port is 80,
// return `Http` as a the next layer to parse
pb.bind_layer(|tcp: &Tcp, _rest| {
if tcp.dport == 80 {
Some(Http::parse_layer)
} else {
None
}
});
// Ether / IP / TCP / "GET /example HTTP/1.1"
let test_data = hex!("ffffffffffff0000000000000800450000330001000040067cc27f0000017f00000100140050000000000000000050022000ffa20000474554202f6578616d706c6520485454502f312e31");
let (_rest, packet) = pb.parse_packet::<Ether>(&test_data).unwrap();
let layers = packet.layers();
assert!(is_layer!(layers[0], Ether));
assert!(is_layer!(layers[1], Ipv4));
assert!(is_layer!(layers[2], Tcp));
assert!(is_layer!(layers[3], Http));
```
# Interface
An [Interface](crate::datalink::Interface) provides the circuitry necessary to perform I/O with packets.
This could be reading/writing from/to a network interface, a pcap file, or other.
See [here](crate::datalink) for more information.
## Example
```rust,no_run
use nata::{
datalink::{pcap::Pcap, Interface, PacketWrite},
layer::{ether::Ether, ip::Ipv4, raw::Raw, tcp::Tcp, LayerExt, LayerOwned},
packet::Packet,
};
// Read from interface using libpcap
let int = Interface::init::<Pcap>("lo").unwrap();
let (mut rx, mut _tx) = int.into_split();
for (_i, pkt) in (&mut rx).enumerate() {
println!("Packet: {:?}", pkt);
}
```
*/
extern crate alloc;
extern crate std;