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

Allow forwarding queries via IPv6 and to any port #264

Merged
merged 1 commit into from
Sep 24, 2023
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
7 changes: 4 additions & 3 deletions bin-dnsq/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clap::Parser;
use std::net::Ipv4Addr;
use std::net::SocketAddr;
use std::path::PathBuf;
use std::process;

Expand Down Expand Up @@ -52,9 +52,10 @@ struct Args {
authoritative_only: bool,

/// Act as a forwarding resolver, not a recursive resolver: forward queries
/// which can't be answered from local state to this nameserver
/// which can't be answered from local state to this nameserver (in
/// `ip:port` form)
#[clap(short, long, value_parser)]
forward_address: Option<Ipv4Addr>,
forward_address: Option<SocketAddr>,

/// Path to a hosts file, can be specified more than once
#[clap(short = 'a', long, value_parser)]
Expand Down
8 changes: 4 additions & 4 deletions bin-resolved/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use bytes::BytesMut;
use clap::Parser;
use std::collections::HashSet;
use std::env;
use std::net::Ipv4Addr;
use std::net::{Ipv4Addr, SocketAddr};
use std::path::PathBuf;
use std::process;
use std::sync::Arc;
Expand Down Expand Up @@ -307,7 +307,7 @@ async fn listen_udp_task(args: ListenArgs, socket: UdpSocket) {
#[derive(Debug, Clone)]
struct ListenArgs {
authoritative_only: bool,
forward_address: Option<Ipv4Addr>,
forward_address: Option<SocketAddr>,
zones_lock: Arc<RwLock<Zones>>,
cache: SharedCache,
}
Expand Down Expand Up @@ -453,9 +453,9 @@ struct Args {

/// Act as a forwarding resolver, not a recursive resolver:
/// forward queries which can't be answered from local state to
/// this nameserver and cache the result
/// this nameserver (in `ip:port` form) and cache the result
#[clap(short, long, value_parser, env = "RESOLVED_FORWARD_ADDRESS")]
forward_address: Option<Ipv4Addr>,
forward_address: Option<SocketAddr>,

/// How many records to hold in the cache
#[clap(
Expand Down
6 changes: 3 additions & 3 deletions lib-dns-resolver/src/forwarding.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use async_recursion::async_recursion;
use std::net::Ipv4Addr;
use std::net::SocketAddr;
use std::time::Duration;
use tokio::time::timeout;
use tracing::Instrument;
Expand Down Expand Up @@ -28,7 +28,7 @@ use crate::util::types::*;
pub async fn resolve_forwarding(
question_stack: &mut Vec<Question>,
metrics: &mut Metrics,
forward_address: Ipv4Addr,
forward_address: SocketAddr,
zones: &Zones,
cache: &SharedCache,
question: &Question,
Expand Down Expand Up @@ -58,7 +58,7 @@ pub async fn resolve_forwarding(
async fn resolve_forwarding_notimeout(
question_stack: &mut Vec<Question>,
metrics: &mut Metrics,
forward_address: Ipv4Addr,
forward_address: SocketAddr,
zones: &Zones,
cache: &SharedCache,
question: &Question,
Expand Down
4 changes: 2 additions & 2 deletions lib-dns-resolver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub mod metrics;
pub mod recursive;
pub mod util;

use std::net::Ipv4Addr;
use std::net::SocketAddr;
use tracing::Instrument;

use dns_types::protocol::types::Question;
Expand All @@ -42,7 +42,7 @@ pub const RECURSION_LIMIT: usize = 32;
/// Resolve a question using the standard DNS algorithms.
pub async fn resolve(
is_recursive: bool,
forward_address: Option<Ipv4Addr>,
forward_address: Option<SocketAddr>,
zones: &Zones,
cache: &SharedCache,
question: &Question,
Expand Down
15 changes: 9 additions & 6 deletions lib-dns-resolver/src/recursive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use crate::metrics::Metrics;
use crate::util::nameserver::*;
use crate::util::types::*;

pub const UPSTREAM_DNS_PORT: u16 = 53;

/// Recursive DNS resolution.
///
/// This corresponds to the standard resolver algorithm. If
Expand Down Expand Up @@ -121,12 +123,13 @@ async fn resolve_recursive_notimeout(
)
.await
{
if let Some(nameserver_response) = query_nameserver(ip, question, false)
.instrument(
tracing::error_span!("query_nameserver", address = %ip, %match_count),
)
.await
.and_then(|res| validate_nameserver_response(question, &res, match_count))
if let Some(nameserver_response) =
query_nameserver((ip, UPSTREAM_DNS_PORT).into(), question, false)
.instrument(
tracing::error_span!("query_nameserver", address = %ip, %match_count),
)
.await
.and_then(|res| validate_nameserver_response(question, &res, match_count))
{
if resolve_candidates_locally {
tracing::trace!(?candidate, "resolved fast candidate");
Expand Down
16 changes: 8 additions & 8 deletions lib-dns-resolver/src/util/nameserver.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rand::Rng;
use std::cmp::Ordering;
use std::net::Ipv4Addr;
use std::net::SocketAddr;
use std::time::Duration;
use tokio::net::{TcpStream, UdpSocket};
use tokio::time::timeout;
Expand All @@ -18,7 +18,7 @@ use crate::util::net::{read_tcp_bytes, send_tcp_bytes, send_udp_bytes};
///
/// This has a 5s timeout for each request, so 10s in total.
pub async fn query_nameserver(
address: Ipv4Addr,
address: SocketAddr,
question: &Question,
recursion_desired: bool,
) -> Option<Message> {
Expand Down Expand Up @@ -58,7 +58,7 @@ pub async fn query_nameserver(
///
/// This has a 5s timeout.
pub async fn query_nameserver_udp(
address: Ipv4Addr,
address: SocketAddr,
serialised_request: &mut [u8],
) -> Option<Message> {
match timeout(
Expand All @@ -74,7 +74,7 @@ pub async fn query_nameserver_udp(

/// Timeout-less version of `query_nameserver_udp`.
async fn query_nameserver_udp_notimeout(
address: Ipv4Addr,
address: SocketAddr,
serialised_request: &mut [u8],
) -> Option<Message> {
if serialised_request.len() > 512 {
Expand All @@ -83,7 +83,7 @@ async fn query_nameserver_udp_notimeout(

let mut buf = vec![0u8; 512];
let sock = UdpSocket::bind("0.0.0.0:0").await.ok()?;
sock.connect((address, 53)).await.ok()?;
sock.connect(address).await.ok()?;
send_udp_bytes(&sock, serialised_request).await.ok()?;
sock.recv(&mut buf).await.ok()?;

Expand All @@ -96,7 +96,7 @@ async fn query_nameserver_udp_notimeout(
///
/// This has a 5s timeout.
pub async fn query_nameserver_tcp(
address: Ipv4Addr,
address: SocketAddr,
serialised_request: &mut [u8],
) -> Option<Message> {
match timeout(
Expand All @@ -112,10 +112,10 @@ pub async fn query_nameserver_tcp(

/// Timeout-less version of `query_nameserver_tcp`.
async fn query_nameserver_tcp_notimeout(
address: Ipv4Addr,
address: SocketAddr,
serialised_request: &mut [u8],
) -> Option<Message> {
let mut stream = TcpStream::connect((address, 53)).await.ok()?;
let mut stream = TcpStream::connect(address).await.ok()?;
send_tcp_bytes(&mut stream, serialised_request).await.ok()?;
let bytes = read_tcp_bytes(&mut stream).await.ok()?;

Expand Down
Loading