Skip to content
This repository has been archived by the owner on Jan 25, 2024. It is now read-only.

Commit

Permalink
Chore/refactor: get rid of Host struct
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
picnoir committed Oct 9, 2023
1 parent e03ecc3 commit 1503bb2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 36 deletions.
9 changes: 9 additions & 0 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
57 changes: 21 additions & 36 deletions src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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 => {
Expand All @@ -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 => {
Expand All @@ -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,
Expand Down Expand Up @@ -354,13 +338,7 @@ fn serialize_initgroups(groups: Vec<Gid>) -> Result<Vec<u8>> {
Ok(result)
}

pub struct Host {
pub addresses: Vec<std::net::IpAddr>,
// aliases is unused so far
pub hostname: String,
}

impl Host {
impl Hostent {
fn serialize(&self) -> Result<Vec<u8>> {
// Loop over all addresses.
// Serialize them into a slice, which is used later in the payload.
Expand All @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -522,6 +500,8 @@ fn serialize_address_info(resp: &AiResponse) -> Result<Vec<u8>> {

#[cfg(test)]
mod test {
use nix::libc::{AF_INET, AF_INET6};

use super::super::config::Config;
use std::net::{Ipv4Addr, Ipv6Addr};

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 1503bb2

Please sign in to comment.