doing re-imports, adding a_from_request

This commit is contained in:
Andy Pack 2024-02-07 17:23:34 +00:00
parent 308dc38ae0
commit 325e825b1d
Signed by: sarsoo
GPG Key ID: A55BA3536A5E0ED7
8 changed files with 54 additions and 47 deletions

View File

@ -1,6 +1,7 @@
use std::convert::TryFrom;
use crate::byte::apply_split_bytes;
use crate::message::header::Direction::Response;
use crate::message::Direction::Response;
/// Size in bytes for a DNS message
pub const HEADER_SIZE: usize = 12;

View File

@ -4,3 +4,9 @@ pub mod question;
pub mod request;
pub mod answer;
pub mod response;
pub use question::{DNSQuestion, QClass, QType, QuestionParseError, questions_to_bytes, questions_from_bytes};
pub use answer::{DNSAnswer, RawRData, RData, ARdata, AnswerParseError, answers_to_bytes, answers_from_bytes};
pub use header::{DNSHeader, Direction, Opcode, ResponseCode, HEADER_SIZE};
pub use request::DNSRequest;
pub use response::DNSResponse;

View File

@ -1,6 +1,5 @@
use std::net::SocketAddr;
use crate::message::header::DNSHeader;
use crate::message::question::{DNSQuestion, questions_to_bytes};
use crate::message::{DNSQuestion, DNSHeader, questions_to_bytes};
#[derive(Ord, PartialOrd, Eq, PartialEq, Debug, Clone)]
pub struct DNSRequest {

View File

@ -1,7 +1,5 @@
use std::net::SocketAddr;
use crate::message::answer::{answers_to_bytes, DNSAnswer};
use crate::message::header::DNSHeader;
use crate::message::question::{DNSQuestion, questions_to_bytes};
use std::net::{Ipv4Addr, SocketAddr};
use crate::message::{Direction, DNSHeader, DNSRequest, ResponseCode, answers_to_bytes, ARdata, DNSAnswer, DNSQuestion, questions_to_bytes};
#[derive(Debug)]
pub struct DNSResponse {
@ -24,4 +22,35 @@ impl DNSResponse {
return header_bytes
}
pub fn a_from_request(request: &DNSRequest, ip: impl Fn(&DNSQuestion) -> Ipv4Addr) -> DNSResponse
{
let mut response = DNSResponse{
header: request.header.clone(),
questions: request.questions.clone(),
answers: vec![],
peer: request.peer
};
response.answers = request.questions
.iter()
.map(|x|
DNSAnswer::from_query(x,
12,
Box::from(ARdata::from(ip(x))),
None))
.collect();
response.header.direction = Direction::Response;
response.header.response = ResponseCode::NoError;
response.header.answer_record_count = response.answers.len() as u16;
response.header.authority_record_count = 0;
response.header.additional_record_count = 0;
if response.header.recursion_desired {
response.header.recursion_available = true;
}
response
}
}

View File

@ -1,6 +1,5 @@
pub mod socket;
// pub mod processor;
//
// pub mod processor::request_processor;
// pub mod processor::response_processor;
pub mod raw_request;
pub use raw_request::{NetworkMessage, NetworkMessagePtr};
pub use socket::DNSSocket;

View File

@ -4,8 +4,9 @@ use std::thread::{JoinHandle};
use log::{debug, error, info};
use std::sync::mpsc;
use std::sync::mpsc::{Receiver, Sender, TryRecvError};
use crate::message::header::HEADER_SIZE;
use crate::net::raw_request::{NetworkMessage, NetworkMessagePtr};
use crate::message::HEADER_SIZE;
use crate::net::{NetworkMessage, NetworkMessagePtr};
pub struct DNSSocket {
addresses: Vec<SocketAddr>,

View File

@ -3,11 +3,9 @@ use std::sync::mpsc;
use std::sync::mpsc::{Receiver, Sender};
use std::thread;
use log::{error, info};
use crate::message::answer::{DNSAnswer, ARdata};
use crate::message::header::{Direction, ResponseCode};
use crate::message::question::QuestionParseError;
use crate::message::response::DNSResponse;
use crate::net::raw_request::{NetworkMessage, NetworkMessagePtr};
use crate::message::{QuestionParseError, DNSResponse};
use crate::net::{NetworkMessage, NetworkMessagePtr};
use crate::request_parser::{HeaderParseError, parse_request, RequestParseError};
pub struct RequestProcesor {
@ -36,31 +34,7 @@ impl RequestProcesor {
Ok(r) => {
info!("received dns message: {:?}", r);
let mut response = DNSResponse{
header: r.header.clone(),
questions: r.questions.clone(),
answers: vec![],
peer: r.peer
};
response.answers = r.questions
.iter()
.map(|x|
DNSAnswer::from_query(x,
12,
Box::from(ARdata::from(Ipv4Addr::from([127, 0, 0, 1]))),
None))
.collect();
response.header.direction = Direction::Response;
response.header.response = ResponseCode::NoError;
response.header.answer_record_count = 1;
response.header.authority_record_count = 0;
response.header.additional_record_count = 0;
if response.header.recursion_desired {
response.header.recursion_available = true;
}
let mut response = DNSResponse::a_from_request(&r, |q| Ipv4Addr::from([127, 0, 0, 1]));
sending_channel.send(Box::from(
NetworkMessage {

View File

@ -1,8 +1,6 @@
use crate::byte;
use crate::message::header::{Direction, DNSHeader, Opcode, ResponseCode};
use crate::message::question::{QuestionParseError, questions_from_bytes};
use crate::message::request::DNSRequest;
use crate::net::raw_request::NetworkMessage;
use crate::message::{DNSRequest, Direction, DNSHeader, Opcode, ResponseCode, QuestionParseError, questions_from_bytes};
use crate::net::NetworkMessage;
use crate::request_parser::RequestParseError::{HeaderParse, QuesionsParse};
pub const ID_START: usize = 0;