feat: working on multiple request types

This commit is contained in:
2026-07-06 01:58:05 +02:00
parent dee178ebc2
commit 9c0f296aa8
6 changed files with 46 additions and 32 deletions
+28 -23
View File
@@ -11,7 +11,7 @@ use dotenv::dotenv;
use tokio::net::UdpSocket;
use tracing::{Instrument, Level};
use crate::dns::*;
use crate::{config::ConfigData, dns::*};
#[tokio::main]
async fn main() -> Result<()> {
@@ -61,13 +61,6 @@ async fn handle_datagram(
let mut response = message.prepare_response();
for question in message.questions {
if question.question_type != RECORD_TYPE_A {
tracing::error!(
"only A records are supported yet, requested: {}",
question.question_type
);
return Ok(());
}
if question.question_class != CLASS_IN {
tracing::error!(
"only IN class records are supported yet, requested: {}",
@@ -76,22 +69,13 @@ async fn handle_datagram(
return Ok(());
}
let address = config.get_address_by_domain(&question.domain);
match address {
Some(address) => {
let answer = Answer {
domain: question.domain,
record_type: RECORD_TYPE_A,
class: CLASS_IN,
ttl: 1,
data: address.to_vec(),
};
response.answers.push(answer);
response.header.number_of_answers += 1;
match question.question_type {
RECORD_TYPE_A => {
handle_type_a_question(&config, &question, &mut response);
}
None => {
response.header.response_code = RESPONSE_CODE_NXDOMAIN;
qt => {
tracing::error!("unsupported question type, requested: {qt}");
return Ok(());
}
}
}
@@ -106,3 +90,24 @@ async fn handle_datagram(
result
}
fn handle_type_a_question(config: &ConfigData, question: &Question, response: &mut Message) {
let address = config.get_ipv4_address_by_domain(&question.domain);
match address {
Some(address) => {
let answer = Answer {
domain: question.domain.clone(),
record_type: RECORD_TYPE_A,
class: CLASS_IN,
ttl: 1,
data: address.to_vec(),
};
response.answers.push(answer);
response.header.number_of_answers += 1;
}
None => {
response.header.response_code = RESPONSE_CODE_NXDOMAIN;
}
}
}