nzrdhcp: broadcast if needed

This commit is contained in:
snow flurry 2024-08-15 01:01:43 -07:00
parent 24a0c1cc68
commit b0646410b9

View file

@ -14,7 +14,12 @@ use tracing::instrument;
const EMPTY_V4: Ipv4Addr = Ipv4Addr::new(0, 0, 0, 0);
const DEFAULT_LEASE: u32 = 86400;
fn make_reply(msg: &Message, msg_type: MessageType, lease_addr: Option<Ipv4Addr>) -> Message {
fn make_reply(
msg: &Message,
msg_type: MessageType,
lease_addr: Option<Ipv4Addr>,
broadcast: bool,
) -> Message {
let mut resp = Message::new(
EMPTY_V4,
lease_addr.unwrap_or(EMPTY_V4),
@ -25,7 +30,11 @@ fn make_reply(msg: &Message, msg_type: MessageType, lease_addr: Option<Ipv4Addr>
resp.set_opcode(Opcode::BootReply)
.set_xid(msg.xid())
.set_htype(msg.htype())
.set_flags(msg.flags());
.set_flags(if broadcast {
msg.flags().set_broadcast()
} else {
msg.flags()
});
resp.opts_mut().insert(DhcpOption::MessageType(msg_type));
resp
}
@ -71,21 +80,26 @@ async fn handle_message(ctx: &Context, from: SocketAddr, msg: &Message) {
let mut response = match msg_type {
MessageType::Discover => {
lease_time = Some(DEFAULT_LEASE);
make_reply(msg, MessageType::Offer, Some(instance.lease.addr.addr))
make_reply(
msg,
MessageType::Offer,
Some(instance.lease.addr.addr),
true,
)
}
MessageType::Request => {
if let Some(DhcpOption::RequestedIpAddress(addr)) =
msg.opts().get(OptionCode::RequestedIpAddress)
{
if *addr == instance.lease.addr.addr {
make_reply(msg, MessageType::Ack, Some(instance.lease.addr.addr))
make_reply(msg, MessageType::Ack, Some(instance.lease.addr.addr), true)
} else {
nak = true;
make_reply(msg, MessageType::Nak, None)
make_reply(msg, MessageType::Nak, None, true)
}
} else {
nak = true;
make_reply(msg, MessageType::Nak, None)
make_reply(msg, MessageType::Nak, None, true)
}
}
MessageType::Decline => {
@ -101,7 +115,7 @@ async fn handle_message(ctx: &Context, from: SocketAddr, msg: &Message) {
tracing::debug!("Ignoring DHCPRELEASE");
return;
}
MessageType::Inform => make_reply(msg, MessageType::Ack, None),
MessageType::Inform => make_reply(msg, MessageType::Ack, None, false),
other => {
tracing::info!("Received unhandled message {other:?}");
return;