Compare commits

...

2 Commits

9 changed files with 95 additions and 20 deletions

View File

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

View File

@ -16,7 +16,7 @@ impl Debug for ARdata {
impl RData for ARdata { impl RData for ARdata {
fn to_bytes(&self) -> Vec<u8> { fn to_bytes(&self) -> Vec<u8> {
return self.rdata.octets().to_vec(); self.rdata.octets().to_vec()
} }
} }

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> {
self.rdata.octets().to_vec()
}
}
impl AAAARdata {
pub fn from(rdata: Ipv6Addr) -> AAAARdata
{
AAAARdata {
rdata
}
}
}

View File

@ -4,6 +4,13 @@ pub use raw_rdata::RawRData;
mod a_rdata; mod a_rdata;
pub use a_rdata::ARdata; pub use a_rdata::ARdata;
mod aaaa_rdata;
pub use aaaa_rdata::AAAARdata;
mod txt_rdata;
pub use txt_rdata::TXTRdata;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
@ -17,7 +24,7 @@ pub trait RData: Debug {
} }
#[derive(Debug)] #[derive(Debug)]
pub struct DNSAnswer { pub struct ResourceRecord {
pub name_offset: u16, pub name_offset: u16,
pub answer_type: QType, pub answer_type: QType,
pub class: QClass, pub class: QClass,
@ -26,7 +33,7 @@ pub struct DNSAnswer {
pub r_data: Box<dyn RData> pub r_data: Box<dyn RData>
} }
impl DNSAnswer { impl ResourceRecord {
pub fn to_bytes(&self) -> Vec<u8> pub fn to_bytes(&self) -> Vec<u8>
{ {
@ -56,9 +63,9 @@ impl DNSAnswer {
return ret 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, name_offset,
answer_type: query.qtype, answer_type: query.qtype,
class: query.qclass, class: query.qclass,
@ -69,7 +76,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); let mut ret = Vec::with_capacity(20);
@ -82,13 +89,13 @@ pub fn answers_to_bytes(answers: &Vec<DNSAnswer>) -> Vec<u8>
} }
#[derive(Ord, PartialOrd, Eq, PartialEq, Debug)] #[derive(Ord, PartialOrd, Eq, PartialEq, Debug)]
pub enum AnswerParseError { pub enum RecordParseError {
ShortLength(usize), ShortLength(usize),
QTypeParse(u8), QTypeParse(u8),
QClassParse(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![])) Ok((0, vec![]))
} }

View File

@ -15,7 +15,7 @@ impl Debug for RawRData {
impl RData for RawRData { impl RData for RawRData {
fn to_bytes(&self) -> Vec<u8> { fn to_bytes(&self) -> Vec<u8> {
return self.rdata.clone(); self.rdata.clone()
} }
} }

View File

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

View File

@ -0,0 +1,29 @@
use std::fmt::{Debug, Formatter};
use crate::message::answer::RData;
pub struct TXTRdata {
pub rdata: String
}
impl Debug for TXTRdata {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("TXT")
.field("data", &self.rdata)
.finish()
}
}
impl RData for TXTRdata {
fn to_bytes(&self) -> Vec<u8> {
self.rdata.into_bytes()
}
}
impl TXTRdata {
pub fn from(rdata: String) -> TXTRdata
{
TXTRdata {
rdata
}
}
}

View File

@ -1,4 +1,4 @@
//! Structures making up the DNS workflow including requests, responses and headers
pub mod header; pub mod header;
pub mod question; pub mod question;
pub mod request; pub mod request;
@ -6,7 +6,7 @@ pub mod answer;
pub mod response; pub mod response;
pub use question::{DNSQuestion, QClass, QType, QuestionParseError, questions_to_bytes, questions_from_bytes}; 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, TXTRdata, RecordParseError, records_to_bytes, answers_from_bytes};
pub use header::{DNSHeader, Direction, Opcode, ResponseCode, HEADER_SIZE}; pub use header::{DNSHeader, Direction, Opcode, ResponseCode, HEADER_SIZE};
pub use request::DNSRequest; pub use request::DNSRequest;
pub use response::DNSResponse; pub use response::DNSResponse;

View File

@ -1,11 +1,13 @@
use std::net::{Ipv4Addr, SocketAddr}; 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)] #[derive(Debug)]
pub struct DNSResponse { pub struct DNSResponse {
pub header: DNSHeader, pub header: DNSHeader,
pub questions: Vec<DNSQuestion>, pub questions: Vec<DNSQuestion>,
pub answers: Vec<DNSAnswer>, pub answers: Vec<ResourceRecord>,
pub authorities: Vec<ResourceRecord>,
pub additionals: Vec<ResourceRecord>,
pub peer: SocketAddr pub peer: SocketAddr
} }
@ -15,10 +17,14 @@ impl DNSResponse {
{ {
let mut header_bytes = self.header.to_bytes().to_vec(); let mut header_bytes = self.header.to_bytes().to_vec();
let mut body_bytes = questions_to_bytes(&self.questions); 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 body_bytes);
header_bytes.append(&mut answer_bytes); header_bytes.append(&mut answer_bytes);
header_bytes.append(&mut authority_bytes);
header_bytes.append(&mut additional_bytes);
return header_bytes return header_bytes
} }
@ -29,13 +35,15 @@ impl DNSResponse {
header: request.header.clone(), header: request.header.clone(),
questions: request.questions.clone(), questions: request.questions.clone(),
answers: vec![], answers: vec![],
authorities: vec![],
additionals: vec![],
peer: request.peer peer: request.peer
}; };
response.answers = request.questions response.answers = request.questions
.iter() .iter()
.map(|x| .map(|x|
DNSAnswer::from_query(x, ResourceRecord::from_query(x,
12, 12,
Box::from(ARdata::from(ip(x))), Box::from(ARdata::from(ip(x))),
None)) None))