adding authority and additional resource records, renaming to resource records

This commit is contained in:
Andy Pack 2024-02-07 19:26:42 +00:00
parent 6dc0060980
commit bda56b4084
Signed by: sarsoo
GPG Key ID: A55BA3536A5E0ED7
6 changed files with 61 additions and 18 deletions

View File

@ -6,7 +6,8 @@ use clap::Parser;
use log::{info, LevelFilter};
use rand::RngCore;
use simplelog::*;
use dnstplib::message::{DNSQuestion, DNSRequest, DNSHeader, QClass, QType, Direction, Opcode, ResponseCode};
use dnstplib::message::DNSRequest;
use dnstplib::net::{DNSSocket, NetworkMessage};
use dnstplib::processor::ResponseProcesor;

View File

@ -0,0 +1,30 @@
use std::fmt::{Debug, Formatter};
use std::net::{Ipv4Addr, Ipv6Addr};
use crate::message::answer::RData;
pub struct AAAARdata {
pub rdata: Ipv6Addr
}
impl Debug for AAAARdata {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("IP")
.field("data", &self.rdata)
.finish()
}
}
impl RData for AAAARdata {
fn to_bytes(&self) -> Vec<u8> {
return self.rdata.octets().to_vec();
}
}
impl AAAARdata {
pub fn from(rdata: Ipv6Addr) -> AAAARdata
{
AAAARdata {
rdata
}
}
}

View File

@ -4,6 +4,10 @@ pub use raw_rdata::RawRData;
mod a_rdata;
pub use a_rdata::ARdata;
mod aaaa_rdata;
pub use aaaa_rdata::AAAARdata;
#[cfg(test)]
mod tests;
@ -17,7 +21,7 @@ pub trait RData: Debug {
}
#[derive(Debug)]
pub struct DNSAnswer {
pub struct ResourceRecord {
pub name_offset: u16,
pub answer_type: QType,
pub class: QClass,
@ -26,7 +30,7 @@ pub struct DNSAnswer {
pub r_data: Box<dyn RData>
}
impl DNSAnswer {
impl ResourceRecord {
pub fn to_bytes(&self) -> Vec<u8>
{
@ -56,9 +60,9 @@ impl DNSAnswer {
return ret
}
pub fn from_query(query: &DNSQuestion, name_offset: u16, data: Box<dyn RData>, ttl: Option<u32>) -> DNSAnswer
pub fn from_query(query: &DNSQuestion, name_offset: u16, data: Box<dyn RData>, ttl: Option<u32>) -> ResourceRecord
{
DNSAnswer {
ResourceRecord {
name_offset,
answer_type: query.qtype,
class: query.qclass,
@ -69,7 +73,7 @@ impl DNSAnswer {
}
}
pub fn answers_to_bytes(answers: &Vec<DNSAnswer>) -> Vec<u8>
pub fn records_to_bytes(answers: &Vec<ResourceRecord>) -> Vec<u8>
{
let mut ret = Vec::with_capacity(20);
@ -82,13 +86,13 @@ pub fn answers_to_bytes(answers: &Vec<DNSAnswer>) -> Vec<u8>
}
#[derive(Ord, PartialOrd, Eq, PartialEq, Debug)]
pub enum AnswerParseError {
pub enum RecordParseError {
ShortLength(usize),
QTypeParse(u8),
QClassParse(u8)
}
pub fn answers_from_bytes(bytes: Vec<u8>, total_answers: u16) -> Result<(i32, Vec<DNSAnswer>), AnswerParseError>
pub fn answers_from_bytes(bytes: Vec<u8>, total_answers: u16) -> Result<(i32, Vec<ResourceRecord>), RecordParseError>
{
Ok((0, vec![]))
}

View File

@ -5,7 +5,7 @@ use super::*;
#[test]
#[ignore]
fn one_answer_back_and_forth() {
let q = DNSAnswer {
let q = ResourceRecord {
// name_offset: "google.com".to_string(),
name_offset: 12,
answer_type: QType::A,

View File

@ -1,4 +1,4 @@
//! Structures making up the DNS workflow including requests, responses and headers
pub mod header;
pub mod question;
pub mod request;
@ -6,7 +6,7 @@ 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 answer::{ResourceRecord, RawRData, RData, ARdata, AAAARdata, RecordParseError, records_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,11 +1,13 @@
use std::net::{Ipv4Addr, SocketAddr};
use crate::message::{Direction, DNSHeader, DNSRequest, ResponseCode, answers_to_bytes, ARdata, DNSAnswer, DNSQuestion, questions_to_bytes};
use crate::message::{Direction, DNSHeader, DNSRequest, ResponseCode, records_to_bytes, ARdata, ResourceRecord, DNSQuestion, questions_to_bytes};
#[derive(Debug)]
pub struct DNSResponse {
pub header: DNSHeader,
pub questions: Vec<DNSQuestion>,
pub answers: Vec<DNSAnswer>,
pub answers: Vec<ResourceRecord>,
pub authorities: Vec<ResourceRecord>,
pub additionals: Vec<ResourceRecord>,
pub peer: SocketAddr
}
@ -15,10 +17,14 @@ impl DNSResponse {
{
let mut header_bytes = self.header.to_bytes().to_vec();
let mut body_bytes = questions_to_bytes(&self.questions);
let mut answer_bytes = answers_to_bytes(&self.answers);
let mut answer_bytes = records_to_bytes(&self.answers);
let mut authority_bytes = records_to_bytes(&self.authorities);
let mut additional_bytes = records_to_bytes(&self.additionals);
header_bytes.append(&mut body_bytes);
header_bytes.append(&mut answer_bytes);
header_bytes.append(&mut authority_bytes);
header_bytes.append(&mut additional_bytes);
return header_bytes
}
@ -29,13 +35,15 @@ impl DNSResponse {
header: request.header.clone(),
questions: request.questions.clone(),
answers: vec![],
authorities: vec![],
additionals: vec![],
peer: request.peer
};
response.answers = request.questions
.iter()
.map(|x|
DNSAnswer::from_query(x,
ResourceRecord::from_query(x,
12,
Box::from(ARdata::from(ip(x))),
None))