save
This commit is contained in:
parent
c8c3d37220
commit
71502147da
49
src/dhcp.rs
49
src/dhcp.rs
@ -1,7 +1,7 @@
|
|||||||
use core::net::Ipv4Addr;
|
use core::net::Ipv4Addr;
|
||||||
|
|
||||||
use dhcparse::{
|
use dhcparse::{
|
||||||
dhcpv4::{self, MessageType as DhcpMsgType},
|
dhcpv4::{self, DhcpOption, MessageType as DhcpMsgType},
|
||||||
v4_options,
|
v4_options,
|
||||||
};
|
};
|
||||||
use embassy_net::{
|
use embassy_net::{
|
||||||
@ -21,6 +21,7 @@ pub async fn dhcp_server(stack: Stack<'static>) {
|
|||||||
let mut tx_meta = [PacketMetadata::EMPTY; 16];
|
let mut tx_meta = [PacketMetadata::EMPTY; 16];
|
||||||
let mut buf = [0; 4096];
|
let mut buf = [0; 4096];
|
||||||
let mut res_buf = Vec::<u8, 4096>::new();
|
let mut res_buf = Vec::<u8, 4096>::new();
|
||||||
|
let mut opts = Vec::<u8, 255>::new();
|
||||||
loop {
|
loop {
|
||||||
let mut socket = UdpSocket::new(
|
let mut socket = UdpSocket::new(
|
||||||
stack,
|
stack,
|
||||||
@ -40,14 +41,20 @@ pub async fn dhcp_server(stack: Stack<'static>) {
|
|||||||
let msg = unwrap(dhcpv4::Message::new(&buf[..n])).await;
|
let msg = unwrap(dhcpv4::Message::new(&buf[..n])).await;
|
||||||
|
|
||||||
let msg_type = unwrap(v4_options!(msg; MessageType required)).await;
|
let msg_type = unwrap(v4_options!(msg; MessageType required)).await;
|
||||||
|
let mut rapid_commit = false;
|
||||||
for o in unwrap(msg.options()).await {
|
for o in unwrap(msg.options()).await {
|
||||||
info!(
|
match o {
|
||||||
"{:?}",
|
Err(_) => break,
|
||||||
match o {
|
Ok(o) => {
|
||||||
Err(_) => break,
|
if let DhcpOption::Unknown(80, _) = o.0 {
|
||||||
Ok(o) => o,
|
if msg_type != DhcpMsgType::DISCOVER {
|
||||||
|
info!("ERROR : rapid commit option on {:?} message", msg_type);
|
||||||
|
}
|
||||||
|
rapid_commit = true;
|
||||||
|
}
|
||||||
|
info!("{:?}", o);
|
||||||
}
|
}
|
||||||
);
|
}
|
||||||
}
|
}
|
||||||
info!("{:?}", msg_type);
|
info!("{:?}", msg_type);
|
||||||
Timer::after_secs(0).await;
|
Timer::after_secs(0).await;
|
||||||
@ -74,23 +81,34 @@ pub async fn dhcp_server(stack: Stack<'static>) {
|
|||||||
.extend_from_slice(&[
|
.extend_from_slice(&[
|
||||||
53,
|
53,
|
||||||
1,
|
1,
|
||||||
match msg_type {
|
match (msg_type, rapid_commit) {
|
||||||
DhcpMsgType::DISCOVER => 2, // DHCP_OFFER
|
(DhcpMsgType::DISCOVER, false) => 2, // DHCP_OFFER
|
||||||
DhcpMsgType::REQUEST => 5, // DHCP_ACK
|
_ => 5, // DHCP_ACK
|
||||||
_ => unreachable!(),
|
|
||||||
},
|
},
|
||||||
])
|
])
|
||||||
.unwrap(); // opt message type
|
.unwrap(); // opt message type
|
||||||
|
|
||||||
unwrap(
|
unwrap(
|
||||||
write_dhcp_opts(
|
write_dhcp_opts(
|
||||||
&mut res_buf,
|
&mut res_buf,
|
||||||
match msg_type {
|
match (msg_type, rapid_commit) {
|
||||||
DhcpMsgType::DISCOVER => {
|
(DhcpMsgType::DISCOVER, false) => {
|
||||||
unwrap(v4_options!(msg; ParameterRequestList))
|
unwrap(v4_options!(msg; ParameterRequestList))
|
||||||
.await
|
.await
|
||||||
.unwrap_or(&[])
|
.unwrap_or(&[])
|
||||||
}
|
}
|
||||||
DhcpMsgType::REQUEST => &[1, 3, 51, 6, 54],
|
(DhcpMsgType::DISCOVER, true) => {
|
||||||
|
opts.clear();
|
||||||
|
opts.extend_from_slice(
|
||||||
|
unwrap(v4_options!(msg; ParameterRequestList))
|
||||||
|
.await
|
||||||
|
.unwrap_or(&[]),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
opts.push(80).unwrap();
|
||||||
|
&opts
|
||||||
|
}
|
||||||
|
(DhcpMsgType::REQUEST, false) => &[1, 3, 51, 6, 54],
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
@ -126,7 +144,7 @@ pub async fn dhcp_server(stack: Stack<'static>) {
|
|||||||
.await,
|
.await,
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
info!("offer sent");
|
info!("offer/ack sent");
|
||||||
Timer::after_secs(0).await;
|
Timer::after_secs(0).await;
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
@ -152,6 +170,7 @@ async fn write_dhcp_opts<const N: usize>(buf: &mut Vec<u8, N>, op_codes: &[u8])
|
|||||||
54 => (4, &[192, 254, 0, 2]), // DhcpOption::ServerIdentifier(&dhcpv4::Addr([192, 254, 0, 2])),
|
54 => (4, &[192, 254, 0, 2]), // DhcpOption::ServerIdentifier(&dhcpv4::Addr([192, 254, 0, 2])),
|
||||||
58 => (4, &500_u32.to_be_bytes()), // DhcpOption::Unknown(58, &[0, 0, 0x1, 0xF4]), // renewal time = 500s
|
58 => (4, &500_u32.to_be_bytes()), // DhcpOption::Unknown(58, &[0, 0, 0x1, 0xF4]), // renewal time = 500s
|
||||||
59 => (4, &600_u32.to_be_bytes()), // DhcpOption::Unknown(59, &[0, 0, 0x2, 0x58]), // rebinding time = 600s
|
59 => (4, &600_u32.to_be_bytes()), // DhcpOption::Unknown(59, &[0, 0, 0x2, 0x58]), // rebinding time = 600s
|
||||||
|
80 => (0, &[]),
|
||||||
_ => {
|
_ => {
|
||||||
info!("Unhandled requested option : {}", o);
|
info!("Unhandled requested option : {}", o);
|
||||||
Timer::after_secs(0).await;
|
Timer::after_secs(0).await;
|
||||||
|
Loading…
Reference in New Issue
Block a user