From 15a5bb445c3f599a110bfda0756baf18247ec05a Mon Sep 17 00:00:00 2001 From: diogo464 Date: Sat, 11 Oct 2025 11:40:17 +0100 Subject: fixed clippy warnings --- src/dhcp.rs | 27 +++++++++++---------------- src/main.rs | 42 +++++++++++++++++++----------------------- src/tftp.rs | 41 +++++++++++++++++++++++++---------------- 3 files changed, 55 insertions(+), 55 deletions(-) (limited to 'src') diff --git a/src/dhcp.rs b/src/dhcp.rs index 51680d1..47bae1e 100644 --- a/src/dhcp.rs +++ b/src/dhcp.rs @@ -9,10 +9,10 @@ use crate::wire; const MAGIC_COOKIE: [u8; 4] = [0x63, 0x82, 0x53, 0x63]; const FLAG_BROADCAST: u16 = 1 << 15; -pub const VENDOR_CLASS_PXE_CLIENT: &'static [u8] = b"PXEClient"; -pub const VENDOR_CLASS_PXE_SERVER: &'static [u8] = b"PXEServer"; +pub const VENDOR_CLASS_PXE_CLIENT: &[u8] = b"PXEClient"; +pub const VENDOR_CLASS_PXE_SERVER: &[u8] = b"PXEServer"; -pub const USER_CLASS_IPXE: &'static [u8] = b"iPXE"; +pub const USER_CLASS_IPXE: &[u8] = b"iPXE"; #[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)] pub enum BootOp { @@ -248,8 +248,8 @@ pub enum PxeClassIdentifierKind { } impl PxeClassIdentifierKind { - pub const KIND_CLIENT: &'static str = "PXEClient"; - pub const KIND_SERVER: &'static str = "PXEServer"; + pub const KIND_CLIENT: &str = "PXEClient"; + pub const KIND_SERVER: &str = "PXEServer"; } impl FromStr for PxeClassIdentifierKind { @@ -555,11 +555,6 @@ fn read_len8_prefixed_vec(cursor: &mut Cursor<&[u8]>) -> Result> { Ok(buf) } -fn read_len8_prefixed_string(cursor: &mut Cursor<&[u8]>) -> Result { - let buf = read_len8_prefixed_vec(cursor)?; - Ok(String::from_utf8(buf).unwrap()) -} - fn read_ipv4(cursor: &mut Cursor<&[u8]>) -> Result { Ok(Ipv4Addr::from_octets(read_arr(cursor)?)) } @@ -726,23 +721,23 @@ pub fn write_option(mut writer: W, option: &DhcpOption) -> Result<()> wire::write_ipv4(&mut writer, *ip)?; } DhcpOption::VendorClassIdentifier(vendor_class) => { - write_option_len_prefixed_buf(&mut writer, &vendor_class)? + write_option_len_prefixed_buf(&mut writer, vendor_class)? } - DhcpOption::TftpServerName(name) => write_option_len_prefixed_string(&mut writer, &name)?, - DhcpOption::TftpFileName(name) => write_option_len_prefixed_string(&mut writer, &name)?, + DhcpOption::TftpServerName(name) => write_option_len_prefixed_string(&mut writer, name)?, + DhcpOption::TftpFileName(name) => write_option_len_prefixed_string(&mut writer, name)?, DhcpOption::UserClassInformation(user_class) => { - write_option_len_prefixed_buf(&mut writer, &user_class)? + write_option_len_prefixed_buf(&mut writer, user_class)? } DhcpOption::ClientSystemArchitecture(arch) => { wire::write_u8(&mut writer, 2)?; wire::write_u16(&mut writer, u16::from(*arch))?; } DhcpOption::ClientMachineIdentifier(identifier) => { - write_option_len_prefixed_buf(&mut writer, &identifier)? + write_option_len_prefixed_buf(&mut writer, identifier)? } DhcpOption::Unknown { data, .. } => { wire::write_u8(&mut writer, u8::try_from(data.len()).unwrap())?; - wire::write(&mut writer, &data)?; + wire::write(&mut writer, data)?; } } Ok(()) diff --git a/src/main.rs b/src/main.rs index c179ac0..819d656 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,10 +14,10 @@ use ipnet::Ipv4Net; use crate::dhcp::{DhcpOption, DhcpPacket}; -const BOOT_FILE_X64_BIOS: &'static str = "netboot.xyz.kpxe"; -const BOOT_FILE_X64_EFI: &'static str = "netboot.xyz.efi"; -const BOOT_FILE_A64_EFI: &'static str = "netboot.xyz-arm64.efi"; -const MENU_FILE: &'static str = "menu.ipxe"; +const BOOT_FILE_X64_BIOS: &str = "netboot.xyz.kpxe"; +const BOOT_FILE_X64_EFI: &str = "netboot.xyz.efi"; +const BOOT_FILE_A64_EFI: &str = "netboot.xyz-arm64.efi"; +const MENU_FILE: &str = "menu.ipxe"; #[derive(Debug, Parser)] struct Cli { @@ -235,14 +235,13 @@ fn handle_packet_4011(context: &Context, buf: &[u8], socket: &UdpSocket, sender_ let mut client_class = None; for option in &packet.options { - if let DhcpOption::VendorClassIdentifier(vendor_class) = option { - if let Ok(dhcp::PxeClassIdentifier::Client(class)) = + if let DhcpOption::VendorClassIdentifier(vendor_class) = option + && let Ok(dhcp::PxeClassIdentifier::Client(class)) = dhcp::PxeClassIdentifier::try_from(vendor_class.as_slice()) { println!("{class}"); client_class = Some(class); } - } } let client_class = match client_class { Some(class) => class, @@ -303,22 +302,19 @@ fn list_network_interfaces() -> Result> { .to_string_lossy() .into_owned(); - match addr_family as i32 { - libc::AF_INET => { - let addr = ifa.ifa_addr as *const libc::sockaddr_in; - let mask = ifa.ifa_netmask as *const libc::sockaddr_in; - - let addr = Ipv4Addr::from((*addr).sin_addr.s_addr.to_ne_bytes()); - let mask = Ipv4Addr::from((*mask).sin_addr.s_addr.to_ne_bytes()); - let network = Ipv4Net::with_netmask(addr, mask).unwrap().trunc(); - - interfaces.push(InterfaceAddr { - interface: name, - address: addr, - network, - }); - } - _ => {} + if addr_family as i32 == libc::AF_INET { + let addr = ifa.ifa_addr as *const libc::sockaddr_in; + let mask = ifa.ifa_netmask as *const libc::sockaddr_in; + + let addr = Ipv4Addr::from((*addr).sin_addr.s_addr.to_ne_bytes()); + let mask = Ipv4Addr::from((*mask).sin_addr.s_addr.to_ne_bytes()); + let network = Ipv4Net::with_netmask(addr, mask).unwrap().trunc(); + + interfaces.push(InterfaceAddr { + interface: name, + address: addr, + network, + }); } } diff --git a/src/tftp.rs b/src/tftp.rs index 72bac22..e99dcf0 100644 --- a/src/tftp.rs +++ b/src/tftp.rs @@ -32,15 +32,24 @@ pub enum TftpOp { Oack, } -impl Into for TftpOp { - fn into(self) -> u16 { - match self { - TftpOp::ReadRequest => 1, - TftpOp::WriteRequest => 2, - TftpOp::Data => 3, - TftpOp::Ack => 4, - TftpOp::Error => 5, - TftpOp::Oack => 6, +impl TftpOp { + pub const CODE_READ_REQUEST: u16 = 1; + pub const CODE_WRITE_REQUEST: u16 = 2; + pub const CODE_DATA: u16 = 3; + pub const CODE_ACK: u16 = 4; + pub const CODE_ERROR: u16 = 5; + pub const CODE_OACK: u16 = 6; +} + +impl From for u16 { + fn from(value: TftpOp) -> Self { + match value { + TftpOp::ReadRequest => TftpOp::CODE_READ_REQUEST, + TftpOp::WriteRequest => TftpOp::CODE_WRITE_REQUEST, + TftpOp::Data => TftpOp::CODE_DATA, + TftpOp::Ack => TftpOp::CODE_ACK, + TftpOp::Error => TftpOp::CODE_ERROR, + TftpOp::Oack => TftpOp::CODE_OACK, } } } @@ -50,12 +59,12 @@ impl TryFrom for TftpOp { fn try_from(value: u16) -> std::result::Result { match value { - 1 => Ok(Self::ReadRequest), - 2 => Ok(Self::WriteRequest), - 3 => Ok(Self::Data), - 4 => Ok(Self::Ack), - 5 => Ok(Self::Error), - 6 => Ok(Self::Oack), + Self::CODE_READ_REQUEST => Ok(Self::ReadRequest), + Self::CODE_WRITE_REQUEST => Ok(Self::WriteRequest), + Self::CODE_DATA => Ok(Self::Data), + Self::CODE_ACK => Ok(Self::Ack), + Self::CODE_ERROR => Ok(Self::Error), + Self::CODE_OACK => Ok(Self::Oack), unknown => Err(InvalidTftpOp(unknown)), } } @@ -126,7 +135,7 @@ pub struct TftpReadRequestPacket { } impl TftpReadRequestPacket { - pub fn write(&self, mut writer: W) -> Result<()> { + pub fn write(&self, _writer: W) -> Result<()> { todo!() } } -- cgit