moving server socket to new thread, adding dns structs
This commit is contained in:
parent
81907a3fa8
commit
d3aefea3cd
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
||||
/target
|
||||
**/*.log
|
4
Cargo.lock
generated
4
Cargo.lock
generated
@ -122,11 +122,15 @@ dependencies = [
|
||||
"clap",
|
||||
"dnstplib",
|
||||
"log",
|
||||
"simplelog",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dnstplib"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"log",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "heck"
|
||||
|
@ -8,4 +8,5 @@ edition = "2021"
|
||||
[dependencies]
|
||||
dnstplib = { path = "../dnstp" }
|
||||
clap = { version = "4.4.18", features = ["derive"] }
|
||||
log = "0.4.20"
|
||||
log = "0.4.20"
|
||||
simplelog = "0.12.1"
|
@ -1,6 +1,10 @@
|
||||
use std::fs::File;
|
||||
use std::net::{SocketAddr, UdpSocket};
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
use clap::Parser;
|
||||
use log::error;
|
||||
use log::{error, info, LevelFilter};
|
||||
use simplelog::*;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(author, version, about, long_about = None)]
|
||||
@ -9,6 +13,13 @@ struct Args {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
CombinedLogger::init(
|
||||
vec![
|
||||
TermLogger::new(LevelFilter::Info, Config::default(), TerminalMode::Mixed, ColorChoice::Auto),
|
||||
WriteLogger::new(LevelFilter::Info, Config::default(), File::create("dnstp.log").unwrap()),
|
||||
]
|
||||
).unwrap();
|
||||
|
||||
let args = Args::parse();
|
||||
|
||||
let address = SocketAddr::from(([127, 0, 0, 1], 0));
|
||||
@ -17,9 +28,12 @@ fn main() {
|
||||
Ok(s) => {
|
||||
loop {
|
||||
|
||||
info!("sending...");
|
||||
|
||||
let send_buf = "hello world";
|
||||
let send_res = s.send_to(send_buf.as_bytes(), "127.0.0.1:5000");
|
||||
|
||||
thread::sleep(Duration::from_secs(1));
|
||||
}
|
||||
}
|
||||
Err(e) => error!("couldn't bind to address {}", e)
|
||||
|
Binary file not shown.
@ -1,11 +1,13 @@
|
||||
use clap::Parser;
|
||||
use std::str;
|
||||
use std::{thread};
|
||||
|
||||
use log::{error, info, warn};
|
||||
use simplelog::*;
|
||||
use std::fs::File;
|
||||
use std::net::{SocketAddr, UdpSocket};
|
||||
|
||||
use dnstplib::dns_socket::DNSSocket;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(author, version, about, long_about = None)]
|
||||
struct Args {
|
||||
@ -37,22 +39,8 @@ fn main() {
|
||||
.map(|x| x.parse().expect("Couldn't parse address"))
|
||||
.collect();
|
||||
|
||||
match UdpSocket::bind(&addresses[..]) {
|
||||
Ok(s) => {
|
||||
loop {
|
||||
let mut buf = [0; 512];
|
||||
let res = s.recv_from(&mut buf);
|
||||
|
||||
match res {
|
||||
Ok(r) => {
|
||||
let res_str = str::from_utf8(&buf).unwrap();
|
||||
info!("{}", res_str);
|
||||
}
|
||||
Err(_) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => error!("couldn't bind to address {}", e)
|
||||
}
|
||||
let mut socket = DNSSocket::new(addresses);
|
||||
socket.run();
|
||||
|
||||
thread::park();
|
||||
}
|
||||
|
@ -5,4 +5,5 @@ edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
[dependencies]
|
||||
log = "0.4.20"
|
35
dnstp/src/dns_header.rs
Normal file
35
dnstp/src/dns_header.rs
Normal file
@ -0,0 +1,35 @@
|
||||
|
||||
pub enum Direction {
|
||||
Request, Response
|
||||
}
|
||||
|
||||
pub enum Opcode {
|
||||
Query = 0,
|
||||
RQuery = 1,
|
||||
Status = 2,
|
||||
Reserved = 3
|
||||
}
|
||||
|
||||
pub enum ResponseCode {
|
||||
NoError = 0,
|
||||
FormatSpecError = 1,
|
||||
ServerFailure = 2,
|
||||
NameError = 3,
|
||||
RequestTypeUnsupported = 4,
|
||||
NotExecuted = 5
|
||||
}
|
||||
|
||||
pub struct DNSHeader {
|
||||
pub id: u16,
|
||||
pub direction: Direction,
|
||||
pub opcode: Opcode,
|
||||
pub authoritative: bool,
|
||||
pub truncation: bool,
|
||||
pub recursion_desired: bool,
|
||||
pub recursion_available: bool,
|
||||
pub response: ResponseCode,
|
||||
pub question_count: u16,
|
||||
pub answer_record_count: u16,
|
||||
pub authority_record_count: u16,
|
||||
pub additional_record_count: u16,
|
||||
}
|
92
dnstp/src/dns_socket.rs
Normal file
92
dnstp/src/dns_socket.rs
Normal file
@ -0,0 +1,92 @@
|
||||
use std::net::{SocketAddr, UdpSocket};
|
||||
use std::thread;
|
||||
use std::thread::{JoinHandle};
|
||||
use log::{error, info};
|
||||
|
||||
use std::str;
|
||||
use std::sync::mpsc;
|
||||
use std::sync::mpsc::{Receiver, Sender, TryRecvError};
|
||||
|
||||
pub struct DNSSocket {
|
||||
addresses: Vec<SocketAddr>,
|
||||
thread: Option<JoinHandle<()>>,
|
||||
thread_killer: Option<Sender<()>>
|
||||
}
|
||||
|
||||
impl DNSSocket {
|
||||
pub fn new(addresses: Vec<SocketAddr>) -> DNSSocket
|
||||
{
|
||||
DNSSocket {
|
||||
addresses,
|
||||
thread: None,
|
||||
thread_killer: None
|
||||
}
|
||||
}
|
||||
|
||||
// pub fn new<T, U: ToSocketAddrs>(addr: T) -> DNSSocket
|
||||
// where
|
||||
// T: Iterator<Item = U>,
|
||||
// {
|
||||
// DNSSocket {
|
||||
// addresses: addr
|
||||
// .map(|x| x.parse().expect("Couldn't parse address"))
|
||||
// .collect()
|
||||
// }
|
||||
// }
|
||||
|
||||
fn bind(&mut self) -> Option<UdpSocket>
|
||||
{
|
||||
match UdpSocket::bind(&self.addresses[..]) {
|
||||
Ok(s) => Option::from(s),
|
||||
Err(e) => None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run(&mut self)
|
||||
{
|
||||
let (tx, rx): (Sender<()>, Receiver<()>) = mpsc::channel();
|
||||
self.thread_killer = Some(tx);
|
||||
let socket = self.bind();
|
||||
|
||||
self.thread = Some(thread::spawn(move || {
|
||||
|
||||
match socket {
|
||||
None => {
|
||||
error!("no socket created, failed to bind to address")
|
||||
}
|
||||
Some(s) => {
|
||||
let mut cancelled = false;
|
||||
while !cancelled {
|
||||
let mut buf = [0; 512];
|
||||
let res = s.recv_from(&mut buf);
|
||||
|
||||
match res {
|
||||
Ok(r) => {
|
||||
let res_str = str::from_utf8(&buf).unwrap();
|
||||
info!("received: {}", res_str);
|
||||
}
|
||||
Err(_) => {}
|
||||
}
|
||||
|
||||
cancelled = match rx.try_recv() {
|
||||
Ok(_) | Err(TryRecvError::Disconnected) => true,
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
info!("socket thread finishing")
|
||||
}));
|
||||
}
|
||||
|
||||
pub fn stop(&mut self)
|
||||
{
|
||||
// if let Some(t) = &mut self.thread {
|
||||
if let Some(k) = &self.thread_killer {
|
||||
k.send(());
|
||||
// t.join();
|
||||
}
|
||||
// }
|
||||
}
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
pub mod dns_socket;
|
||||
pub mod request_parser;
|
||||
mod dns_header;
|
0
dnstp/src/request_parser.rs
Normal file
0
dnstp/src/request_parser.rs
Normal file
Loading…
Reference in New Issue
Block a user