Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgraded trust-dns-resolver (#92) #105

Merged
merged 5 commits into from
Jan 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ To set up your development environment:

To run tests: `cargo test`

After tests, check the formatting: `cargo fmt -- --check`
ebroto marked this conversation as resolved.
Show resolved Hide resolved

Note that at the moment the tests do not test the os layer (anything in the `os` folder).

If you are stuck, unsure about how to approach an issue or would like some guidance, you are welcome to contact: aram@poor.dev
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ chrono = "0.4"
regex = "1.3.1"
lazy_static = "1.4.0"
tokio = { version = "0.2", features = ["rt-core", "sync"] }
trust-dns-resolver = "=0.18.0-alpha.2"
trust-dns-resolver = "0.18.1"
async-trait = "0.1.21"

[target.'cfg(target_os="linux")'.dependencies]
Expand Down
6 changes: 1 addition & 5 deletions src/network/dns/client.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::network::dns::{resolver::Lookup, IpTable};
use std::{
collections::HashSet,
future::Future,
net::Ipv4Addr,
sync::{Arc, Mutex},
thread::{Builder, JoinHandle},
Expand All @@ -23,14 +22,12 @@ pub struct Client {
}

impl Client {
pub fn new<R, B>(resolver: R, background: B) -> Result<Self, failure::Error>
pub fn new<R>(resolver: R, mut runtime: Runtime) -> Result<Self, failure::Error>
where
R: Lookup + Send + Sync + 'static,
B: Future<Output = ()> + Send + 'static,
{
let cache = Arc::new(Mutex::new(IpTable::new()));
let pending = Arc::new(Mutex::new(PendingAddrs::new()));
let mut runtime = Runtime::new()?;
let (tx, mut rx) = mpsc::channel::<Vec<Ipv4Addr>>(CHANNEL_SIZE);

let handle = Builder::new().name("resolver".into()).spawn({
Expand All @@ -39,7 +36,6 @@ impl Client {
move || {
runtime.block_on(async {
let resolver = Arc::new(resolver);
tokio::spawn(background);

while let Some(ips) = rx.recv().await {
for ip in ips {
Expand Down
13 changes: 7 additions & 6 deletions src/network/dns/resolver.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
use async_trait::async_trait;
use std::{future::Future, net::Ipv4Addr};
use trust_dns_resolver::{error::ResolveErrorKind, AsyncResolver};
use std::net::Ipv4Addr;
use tokio::runtime::Handle;
use trust_dns_resolver::{error::ResolveErrorKind, TokioAsyncResolver};

#[async_trait]
pub trait Lookup {
async fn lookup(&self, ip: Ipv4Addr) -> Option<String>;
}

pub struct Resolver(AsyncResolver);
pub struct Resolver(TokioAsyncResolver);

impl Resolver {
pub fn new() -> Result<(Self, impl Future<Output = ()>), failure::Error> {
let (resolver, background) = AsyncResolver::from_system_conf()?;
Ok((Self(resolver), background))
pub async fn new(runtime: Handle) -> Result<Self, failure::Error> {
let resolver = TokioAsyncResolver::from_system_conf(runtime).await?;
Ok(Self(resolver))
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/os/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use ::pnet_bandwhich_fork::datalink::{self, Config, NetworkInterface};
use ::std::io::{self, stdin, Write};
use ::termion::event::Event;
use ::termion::input::TermRead;
use ::tokio::runtime::Runtime;

use ::std::io::ErrorKind;
use ::std::time;
Expand Down Expand Up @@ -123,8 +124,9 @@ pub fn get_input(
let write_to_stdout = create_write_to_stdout();
let (on_winch, cleanup) = sigwinch();
let dns_client = if resolve {
let (resolver, background) = dns::Resolver::new()?;
let dns_client = dns::Client::new(resolver, background)?;
let mut runtime = Runtime::new()?;
let resolver = runtime.block_on(dns::Resolver::new(runtime.handle().clone()))?;
let dns_client = dns::Client::new(resolver, runtime)?;
Some(dns_client)
} else {
None
Expand Down
17 changes: 3 additions & 14 deletions src/tests/fakes/fake_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ use ::ipnetwork::IpNetwork;
use ::pnet_bandwhich_fork::datalink::DataLinkReceiver;
use ::pnet_bandwhich_fork::datalink::NetworkInterface;
use ::std::collections::HashMap;
use ::std::future::Future;
use ::std::net::{IpAddr, Ipv4Addr, SocketAddr};
use ::std::pin::Pin;
use ::std::task::{Context, Poll};
use ::std::{thread, time};
use ::termion::event::Event;
use ::tokio::runtime::Runtime;

use crate::{
network::{
Expand Down Expand Up @@ -172,7 +170,8 @@ pub fn create_fake_on_winch(should_send_winch_event: bool) -> Box<OnSigWinch> {
}

pub fn create_fake_dns_client(ips_to_hosts: HashMap<IpAddr, String>) -> Option<dns::Client> {
let dns_client = dns::Client::new(FakeResolver(ips_to_hosts), FakeBackground {}).unwrap();
let runtime = Runtime::new().unwrap();
let dns_client = dns::Client::new(FakeResolver(ips_to_hosts), runtime).unwrap();
Some(dns_client)
}

ebroto marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -185,13 +184,3 @@ impl Lookup for FakeResolver {
self.0.get(&ip).cloned()
}
}

struct FakeBackground {}

impl Future for FakeBackground {
type Output = ();

fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
Poll::Ready(())
}
}