Skip to content

Commit 297d90b

Browse files
thvdveldDirbaio
authored andcommitted
IPv4 fragmentation (outgoing)
1 parent 42f485a commit 297d90b

File tree

9 files changed

+554
-134
lines changed

9 files changed

+554
-134
lines changed

examples/client.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,24 @@ fn main() {
5151
let medium = device.capabilities().medium;
5252
let mut builder = InterfaceBuilder::new().ip_addrs(ip_addrs).routes(routes);
5353

54+
#[cfg(feature = "proto-ipv4-fragmentation")]
55+
let mut ipv4_out_packet_cache = [0u8; 1280];
5456
#[cfg(feature = "proto-ipv4-fragmentation")]
5557
{
5658
let ipv4_frag_cache = FragmentsCache::new(vec![], BTreeMap::new());
57-
builder = builder.ipv4_fragments_cache(ipv4_frag_cache);
59+
builder = builder
60+
.ipv4_fragments_cache(ipv4_frag_cache)
61+
.ipv4_out_packet_cache(&mut ipv4_out_packet_cache[..]);
5862
}
5963

6064
#[cfg(feature = "proto-sixlowpan-fragmentation")]
61-
let mut out_packet_buffer = [0u8; 1280];
65+
let mut sixlowpan_out_packet_cache = [0u8; 1280];
6266
#[cfg(feature = "proto-sixlowpan-fragmentation")]
6367
{
6468
let sixlowpan_frag_cache = FragmentsCache::new(vec![], BTreeMap::new());
6569
builder = builder
6670
.sixlowpan_fragments_cache(sixlowpan_frag_cache)
67-
.sixlowpan_out_packet_cache(&mut out_packet_buffer[..]);
71+
.sixlowpan_out_packet_cache(&mut sixlowpan_out_packet_cache[..]);
6872
}
6973

7074
if medium == Medium::Ethernet {

examples/server.rs

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use log::debug;
44
use std::collections::BTreeMap;
55
use std::fmt::Write;
66
use std::os::unix::io::AsRawFd;
7-
use std::str;
87

98
#[cfg(any(
109
feature = "proto-sixlowpan-fragmentation",
@@ -32,8 +31,14 @@ fn main() {
3231

3332
let neighbor_cache = NeighborCache::new(BTreeMap::new());
3433

35-
let udp_rx_buffer = udp::PacketBuffer::new(vec![udp::PacketMetadata::EMPTY], vec![0; 65535]);
36-
let udp_tx_buffer = udp::PacketBuffer::new(vec![udp::PacketMetadata::EMPTY], vec![0; 65535]);
34+
let udp_rx_buffer = udp::PacketBuffer::new(
35+
vec![udp::PacketMetadata::EMPTY, udp::PacketMetadata::EMPTY],
36+
vec![0; 65535],
37+
);
38+
let udp_tx_buffer = udp::PacketBuffer::new(
39+
vec![udp::PacketMetadata::EMPTY, udp::PacketMetadata::EMPTY],
40+
vec![0; 65535],
41+
);
3742
let udp_socket = udp::Socket::new(udp_rx_buffer, udp_tx_buffer);
3843

3944
let tcp1_rx_buffer = tcp::SocketBuffer::new(vec![0; 64]);
@@ -62,20 +67,31 @@ fn main() {
6267
let medium = device.capabilities().medium;
6368
let mut builder = InterfaceBuilder::new().ip_addrs(ip_addrs);
6469

70+
builder = builder.random_seed(
71+
std::time::SystemTime::now()
72+
.duration_since(std::time::UNIX_EPOCH)
73+
.unwrap()
74+
.as_secs(),
75+
);
76+
77+
#[cfg(feature = "proto-ipv4-fragmentation")]
78+
let mut ipv4_out_packet_cache = [0u8; 10_000];
6579
#[cfg(feature = "proto-ipv4-fragmentation")]
6680
{
6781
let ipv4_frag_cache = FragmentsCache::new(vec![], BTreeMap::new());
68-
builder = builder.ipv4_fragments_cache(ipv4_frag_cache);
82+
builder = builder
83+
.ipv4_fragments_cache(ipv4_frag_cache)
84+
.ipv4_out_packet_cache(&mut ipv4_out_packet_cache[..]);
6985
}
7086

7187
#[cfg(feature = "proto-sixlowpan-fragmentation")]
72-
let mut out_packet_buffer = [0u8; 1280];
88+
let mut sixlowpan_out_packet_cache = [0u8; 1280];
7389
#[cfg(feature = "proto-sixlowpan-fragmentation")]
7490
{
7591
let sixlowpan_frag_cache = FragmentsCache::new(vec![], BTreeMap::new());
7692
builder = builder
7793
.sixlowpan_fragments_cache(sixlowpan_frag_cache)
78-
.sixlowpan_out_packet_cache(&mut out_packet_buffer[..]);
94+
.sixlowpan_out_packet_cache(&mut sixlowpan_out_packet_cache[..]);
7995
}
8096

8197
if medium == Medium::Ethernet {
@@ -110,22 +126,16 @@ fn main() {
110126

111127
let client = match socket.recv() {
112128
Ok((data, endpoint)) => {
113-
debug!(
114-
"udp:6969 recv data: {:?} from {}",
115-
str::from_utf8(data).unwrap(),
116-
endpoint
117-
);
118-
Some(endpoint)
129+
debug!("udp:6969 recv data: {:?} from {}", data, endpoint);
130+
let mut data = data.to_vec();
131+
data.reverse();
132+
Some((endpoint, data))
119133
}
120134
Err(_) => None,
121135
};
122-
if let Some(endpoint) = client {
123-
let data = b"hello\n";
124-
debug!(
125-
"udp:6969 send data: {:?}",
126-
str::from_utf8(data.as_ref()).unwrap()
127-
);
128-
socket.send_slice(data, endpoint).unwrap();
136+
if let Some((endpoint, data)) = client {
137+
debug!("udp:6969 send data: {:?} to {}", data, endpoint,);
138+
socket.send_slice(&data, endpoint).unwrap();
129139
}
130140

131141
// tcp:6969: respond "hello"
@@ -160,10 +170,7 @@ fn main() {
160170
let recvd_len = buffer.len();
161171
let mut data = buffer.to_owned();
162172
if !data.is_empty() {
163-
debug!(
164-
"tcp:6970 recv data: {:?}",
165-
str::from_utf8(data.as_ref()).unwrap_or("(invalid utf8)")
166-
);
173+
debug!("tcp:6970 recv data: {:?}", data);
167174
data = data.split(|&b| b == b'\n').collect::<Vec<_>>().concat();
168175
data.reverse();
169176
data.extend(b"\n");
@@ -172,10 +179,7 @@ fn main() {
172179
})
173180
.unwrap();
174181
if socket.can_send() && !data.is_empty() {
175-
debug!(
176-
"tcp:6970 send data: {:?}",
177-
str::from_utf8(data.as_ref()).unwrap_or("(invalid utf8)")
178-
);
182+
debug!("tcp:6970 send data: {:?}", data);
179183
socket.send_slice(&data[..]).unwrap();
180184
}
181185
} else if socket.may_send() {

0 commit comments

Comments
 (0)