helper functions, re-imports

This commit is contained in:
Andy Pack 2024-02-07 17:44:28 +00:00
parent 325e825b1d
commit 8d92a97850
Signed by: sarsoo
GPG Key ID: A55BA3536A5E0ED7
5 changed files with 63 additions and 34 deletions

View File

@ -6,11 +6,8 @@ use clap::Parser;
use log::{info, LevelFilter};
use rand::RngCore;
use simplelog::*;
use dnstplib::message::header::{Direction, DNSHeader, Opcode, ResponseCode};
use dnstplib::message::question::{DNSQuestion, QClass, QType};
use dnstplib::message::request::DNSRequest;
use dnstplib::net::socket::DNSSocket;
use dnstplib::net::raw_request::NetworkMessage;
use dnstplib::message::{DNSQuestion, DNSRequest, DNSHeader, QClass, QType, Direction, Opcode, ResponseCode};
use dnstplib::net::{DNSSocket, NetworkMessage};
use dnstplib::processor::ResponseProcesor;
#[derive(Parser, Debug)]
@ -49,32 +46,7 @@ fn main() {
info!("sending...");
let message = DNSRequest {
header: DNSHeader {
id: rng.next_u32() as u16,
direction: Direction::Request,
opcode: Opcode::Query,
authoritative: false,
truncation: false,
recursion_desired: true,
recursion_available: false,
valid_zeroes: true,
response: ResponseCode::NoError,
question_count: 1,
answer_record_count: 0,
authority_record_count: 0,
additional_record_count: 0
},
questions: vec![
DNSQuestion {
qname: "duck.com".to_string(),
qtype: QType::A,
qclass: QClass::Internet
}
],
additional_records: vec![],
peer: address
};
let message = DNSRequest::from_hostname(address, rng.next_u32() as u16, "duck.com".to_string());
let bytes = message.to_bytes();

View File

@ -8,7 +8,7 @@ use simplelog::*;
use std::fs::File;
use std::net::SocketAddr;
use dnstplib::net::socket::DNSSocket;
use dnstplib::net::DNSSocket;
use dnstplib::processor::RequestProcesor;
/// Command-line arguments for configuring the server

View File

@ -128,4 +128,26 @@ impl DNSHeader {
header_bytes
}
pub fn new_request(id: u16, questions: Option<u16>) -> DNSHeader
{
DNSHeader {
id,
direction: Direction::Request,
opcode: Opcode::Query,
authoritative: false,
truncation: false,
recursion_desired: true,
recursion_available: false,
valid_zeroes: true,
response: ResponseCode::NoError,
question_count: match questions {
None => 1,
Some(v) => v
},
answer_record_count: 0,
authority_record_count: 0,
additional_record_count: 0
}
}
}

View File

@ -1,5 +1,5 @@
use std::net::SocketAddr;
use crate::message::{DNSQuestion, DNSHeader, questions_to_bytes};
use crate::message::{DNSQuestion, DNSHeader, questions_to_bytes, Direction, Opcode, ResponseCode, QType, QClass};
#[derive(Ord, PartialOrd, Eq, PartialEq, Debug, Clone)]
pub struct DNSRequest {
@ -20,4 +20,38 @@ impl DNSRequest {
return header_bytes
}
pub fn from_hostname(peer: SocketAddr, id: u16, hostname: String) -> DNSRequest
{
DNSRequest {
header: DNSHeader::new_request(id, None),
questions: vec![
DNSQuestion {
qname: hostname,
qtype: QType::A,
qclass: QClass::Internet
}
],
additional_records: vec![],
peer
}
}
pub fn from_hostnames(peer: SocketAddr, id: u16, hostnames: Vec<String>) -> DNSRequest
{
DNSRequest {
header: DNSHeader::new_request(id, Some(hostnames.len() as u16)),
questions: hostnames
.into_iter()
.map(|n|
DNSQuestion {
qname: n,
qclass: QClass::Internet,
qtype: QType::A
})
.collect(),
additional_records: vec![],
peer
}
}
}

View File

@ -3,6 +3,7 @@ use std::sync::mpsc::{Receiver, Sender};
use std::thread;
use log::info;
use std::str;
use crate::message::DNSResponse;
use crate::net::raw_request::NetworkMessagePtr;
pub struct ResponseProcesor {
@ -25,7 +26,7 @@ impl ResponseProcesor {
for mut m in rx
{
info!("processing: {}", str::from_utf8(&(*(*m).buffer)).unwrap());
info!("processing response");
// match sending_channel.send(m) {
// Ok(_) => {}