dhcp working!!!!!!!!!

This commit is contained in:
Arkitu 2025-04-26 17:41:26 +02:00
parent 71502147da
commit 2bfca4ad1d

View File

@ -22,6 +22,7 @@ pub async fn dhcp_server(stack: Stack<'static>) {
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(); let mut opts = Vec::<u8, 255>::new();
let mut current_ip = 10_u8; // add one at each new connection, loop at 250, hope not to get on occupied ip
loop { loop {
let mut socket = UdpSocket::new( let mut socket = UdpSocket::new(
stack, stack,
@ -70,7 +71,9 @@ pub async fn dhcp_server(stack: Stack<'static>) {
res_buf.extend_from_slice(&[0; 2]).unwrap(); // secs res_buf.extend_from_slice(&[0; 2]).unwrap(); // secs
res_buf.extend_from_slice(&[0x80, 0x00]).unwrap(); // flags res_buf.extend_from_slice(&[0x80, 0x00]).unwrap(); // flags
res_buf.extend_from_slice(&buf[12..16]).unwrap(); // ciaddr res_buf.extend_from_slice(&buf[12..16]).unwrap(); // ciaddr
res_buf.extend_from_slice(&[192, 254, 0, 12]).unwrap(); // yiaddr res_buf
.extend_from_slice(&[192, 254, 0, current_ip])
.unwrap(); // yiaddr
res_buf.extend_from_slice(&[0; 4]).unwrap(); // siaddr res_buf.extend_from_slice(&[0; 4]).unwrap(); // siaddr
res_buf.extend_from_slice(&buf[24..28]).unwrap(); // giaddr res_buf.extend_from_slice(&buf[24..28]).unwrap(); // giaddr
res_buf.extend_from_slice(&buf[28..44]).unwrap(); // chaddr res_buf.extend_from_slice(&buf[28..44]).unwrap(); // chaddr
@ -88,33 +91,27 @@ pub async fn dhcp_server(stack: Stack<'static>) {
]) ])
.unwrap(); // opt message type .unwrap(); // opt message type
unwrap( opts.clear();
write_dhcp_opts( opts.extend_from_slice(
&mut res_buf, unwrap(v4_options!(msg; ParameterRequestList))
match (msg_type, rapid_commit) { .await
(DhcpMsgType::DISCOVER, false) => { .unwrap_or(&[]),
unwrap(v4_options!(msg; ParameterRequestList))
.await
.unwrap_or(&[])
}
(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!(),
},
)
.await,
) )
.await; .unwrap();
let default_opts: &[u8] = match (msg_type, rapid_commit) {
(DhcpMsgType::DISCOVER, false) => &[54],
(DhcpMsgType::DISCOVER, true) => &[54, 80],
(DhcpMsgType::REQUEST, false) => &[1, 3, 51, 6, 54],
_ => unreachable!(),
};
for o in default_opts {
if !opts.contains(o) {
opts.push(*o).unwrap();
}
}
unwrap(write_dhcp_opts(&mut res_buf, &opts).await).await;
res_buf.push(255).unwrap(); // end option res_buf.push(255).unwrap(); // end option
for o in unwrap(unwrap(dhcpv4::Message::new(&res_buf)).await.options()).await { for o in unwrap(unwrap(dhcpv4::Message::new(&res_buf)).await.options()).await {
@ -144,8 +141,15 @@ pub async fn dhcp_server(stack: Stack<'static>) {
.await, .await,
) )
.await; .await;
info!("offer/ack sent"); info!("offer/ack sent for ip {}", current_ip);
Timer::after_secs(0).await; Timer::after_secs(0).await;
if msg_type == DhcpMsgType::REQUEST || rapid_commit {
current_ip += 1;
if current_ip > 250 {
current_ip = 10;
}
}
} }
_ => {} _ => {}
} }