Skip to content

Commit

Permalink
backend: return simple Vector in lookup()
Browse files Browse the repository at this point in the history
There is no use of the enum, the caller just checks if it gets ips or
not so we can make this much simpler.

Now there are a lot of unit test using this so I had to fix them. While
fixing I decided it woul dbe a godd idea to also change the format they
are written as this all seems super complicated for no reason there.
We can direclty check the vector in assert_eq!() and do not need the
complicated destructuring. And just use expect() directly over matching
error just to panic anyway. Also parsing the same ips over and over
doesn't look nice either.

Signed-off-by: Paul Holzinger <[email protected]>
  • Loading branch information
Luap99 committed Sep 19, 2024
1 parent f544277 commit fc08648
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 201 deletions.
27 changes: 5 additions & 22 deletions src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,6 @@ pub struct DNSBackend {
pub search_domain: String,
}

pub enum DNSResult {
// We know the IP address of the requester and what networks they are in.
// Here's a vector of IPs corresponding to your query.
Success(Vec<IpAddr>),
// We know the IP address of the requester and what networks they are in.
// However, there were no results for the requested name to look up.
NXDomain,
// We do not know the IP address of the requester.
NoSuchIP,
// Other, unspecified error occurred.
Error(String),
}

impl DNSBackend {
// Create a new backend from the given set of network mappings.
pub fn new(
Expand Down Expand Up @@ -71,12 +58,8 @@ impl DNSBackend {
}

// Handle a single DNS lookup made by a given IP.
// The name being looked up *must* have the TLD used by the DNS server
// stripped.
// TODO: right now this returns v4 and v6 addresses intermixed and relies on
// the caller to sort through them; we could add a v6 bool as an argument
// and do it here instead.
pub fn lookup(&self, requester: &IpAddr, entry: &str) -> DNSResult {
// Returns all the ips for the given entry name
pub fn lookup(&self, requester: &IpAddr, entry: &str) -> Option<Vec<IpAddr>> {
// Normalize lookup entry to lowercase.
let mut name = entry.to_lowercase();

Expand All @@ -89,7 +72,7 @@ impl DNSBackend {

let nets = match self.ip_mappings.get(requester) {
Some(n) => n,
None => return DNSResult::NoSuchIP,
None => return None,
};

let mut results: Vec<IpAddr> = Vec::new();
Expand All @@ -116,10 +99,10 @@ impl DNSBackend {
}

if results.is_empty() {
return DNSResult::NXDomain;
return None;
}

DNSResult::Success(results)
Some(results)
}

// Returns list of network resolvers for a particular container
Expand Down
8 changes: 3 additions & 5 deletions src/dns/coredns.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::backend::DNSBackend;
use crate::backend::DNSResult;
use crate::error::AardvarkResult;
use arc_swap::ArcSwap;
use arc_swap::Guard;
Expand Down Expand Up @@ -461,12 +460,11 @@ fn reply_ip<'a>(
// attempt intra network resolution
match backend.lookup(&src_address.ip(), name) {
// If we go success from backend lookup
DNSResult::Success(_ip_vec) => {
debug!("Found backend lookup");
resolved_ip_list = _ip_vec;
Some(ips) => {
resolved_ip_list = ips;
}
// For everything else assume the src_address was not in ip_mappings
_ => {
None => {
debug!("No backend lookup found, try resolving in current resolvers entry");
if let Some(container_mappings) = backend.name_mappings.get(network_name) {
if let Some(ips) = container_mappings.get(name) {
Expand Down
Loading

0 comments on commit fc08648

Please sign in to comment.