From 1503bb24e67aa8e6e1c420a7fa6a64c4e9e0c5bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Baylac=20Jacqu=C3=A9?= Date: Mon, 9 Oct 2023 11:22:36 +0200 Subject: [PATCH] Chore/refactor: get rid of Host struct We don't really need this Host struct anymore, it's pretty much a duplicate of the Rust Hostent version at this point. Getting rid of it, moving the serialization procedure to the Hostent structure. --- src/ffi.rs | 9 ++++++++ src/handlers.rs | 57 ++++++++++++++++++------------------------------- 2 files changed, 30 insertions(+), 36 deletions(-) diff --git a/src/ffi.rs b/src/ffi.rs index dd78c38..9f28a46 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -76,6 +76,15 @@ mod glibcffi { } } +/// This structure is the Rust counterpart of the `libc::hostent` C +/// function the Libc hostent struct. +/// +/// It's mostly used to perform the gethostbyaddr and gethostbyname +/// operations. +/// +/// This struct can be serialized to the wire through the +/// `serialize` function or retrieved from the C boundary using the +/// TryFrom `libc:hostent` trait. #[derive(Clone, Debug)] pub struct Hostent { pub name: String, diff --git a/src/handlers.rs b/src/handlers.rs index c83ef8d..98a6270 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -27,7 +27,7 @@ use nix::unistd::{getgrouplist, Gid, Group, Uid, User}; use slog::{debug, error, Logger}; use std::mem::size_of; -use crate::ffi::{gethostbyname2_r, gethostbyaddr_r, LibcIp}; +use crate::ffi::{gethostbyname2_r, gethostbyaddr_r, LibcIp, Hostent}; use crate::protocol::{AiResponse, AiResponseHeader}; use super::config::Config; @@ -180,11 +180,7 @@ pub fn handle_request( Ok(hostent) => hostent, Err(e) => bail!("error during lookup: {:?}", e) }; - let host = Host { - addresses: hostent.addr_list, - hostname: hostent.name - }; - host.serialize() + hostent.serialize() } RequestType::GETHOSTBYADDRv6 => { let key = request.key; @@ -197,11 +193,7 @@ pub fn handle_request( Ok(hostent) => hostent, Err(e) => bail!("error during lookup: {:?}", e) }; - let host = Host { - addresses: hostent.addr_list, - hostname: hostent.name - }; - host.serialize() + hostent.serialize() } RequestType::GETHOSTBYNAME => { @@ -210,11 +202,7 @@ pub fn handle_request( Ok(hostent) => hostent, Err(e) => bail!("error during lookup: {:?}", e) }; - let host = Host { - addresses: hostent.addr_list, - hostname: hostent.name - }; - host.serialize() + hostent.serialize() } RequestType::GETHOSTBYNAMEv6 => { @@ -224,11 +212,7 @@ pub fn handle_request( Ok(hostent) => hostent, Err(e) => bail!("error during lookup: {:?}", e) }; - let host = Host { - addresses: hostent.addr_list, - hostname: hostent.name - }; - host.serialize() + hostent.serialize() } // These will normally send an FD pointing to the internal cache structure, @@ -354,13 +338,7 @@ fn serialize_initgroups(groups: Vec) -> Result> { Ok(result) } -pub struct Host { - pub addresses: Vec, - // aliases is unused so far - pub hostname: String, -} - -impl Host { +impl Hostent { fn serialize(&self) -> Result> { // Loop over all addresses. // Serialize them into a slice, which is used later in the payload. @@ -369,7 +347,7 @@ impl Host { let mut num_v6 = 0; let mut buf_addrs = vec![]; - for address in self.addresses.iter() { + for address in self.addr_list.iter() { match address { IpAddr::V4(ip4) => { num_v4 += 1; @@ -394,7 +372,7 @@ impl Host { let num_addrs = num_v4 + num_v6; let has_addrs = num_addrs > 0; - let hostname_c_string_bytes = CString::new(self.hostname.clone())?.into_bytes_with_nul(); + let hostname_c_string_bytes = CString::new(self.name.clone())?.into_bytes_with_nul(); let hostname_c_string_len = if has_addrs { hostname_c_string_bytes.len() as i32 } else { @@ -522,6 +500,8 @@ fn serialize_address_info(resp: &AiResponse) -> Result> { #[cfg(test)] mod test { + use nix::libc::{AF_INET, AF_INET6}; + use super::super::config::Config; use std::net::{Ipv4Addr, Ipv6Addr}; @@ -623,9 +603,11 @@ mod test { key: &[127, 0, 0, 1], }; - let expected = (Host { - addresses: vec![IpAddr::from(Ipv4Addr::new(127, 0, 0, 1))], - hostname: "localhost".to_string(), + let expected = (Hostent { + addr_list: vec![IpAddr::from(Ipv4Addr::new(127, 0, 0, 1))], + name: "localhost".to_string(), + addr_type: AF_INET, + aliases: Vec::new() }).serialize().expect("must succeed"); let output = handle_request(&test_logger(), &Config::default(), &request) @@ -653,9 +635,12 @@ mod test { key: &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], }; - let expected = (Host { - addresses: vec![IpAddr::from(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1))], - hostname: "localhost".to_string(), + let expected = (Hostent { + addr_list: vec![IpAddr::from(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1))], + name: "localhost".to_string(), + addr_type: AF_INET6, + aliases: Vec::new() + }).serialize().expect("must succeed"); let output = handle_request(&test_logger(), &Config::default(), &request)