dns/src/main.rs
ddidderr 0dc41ef845
(tests) Implement tests for DNS header parsing
* implemented 3 tests
  * valid opcodes
  * invalid opcodes
  * header too short
* fixed byteorder and bit shifting for flags
* added hexdump for main function for testing purposes
2022-04-06 20:19:41 +02:00

53 lines
1.2 KiB
Rust

use std::net::UdpSocket;
use std::sync::mpsc;
use std::thread::{self, JoinHandle};
mod proto;
use proto::DNSHeader;
fn listen() -> (
JoinHandle<()>,
mpsc::Sender<Vec<u8>>,
mpsc::Receiver<Vec<u8>>,
) {
let (tx, rx) = mpsc::channel();
let tx_clone = tx.clone();
(
thread::spawn(move || {
let socket = UdpSocket::bind("127.0.0.1:13337").unwrap();
let mut buf = [0; 512];
loop {
let (len, _src) = socket.recv_from(&mut buf).unwrap();
let buf = &mut buf[..len];
tx.send(Vec::from(buf)).unwrap();
}
}),
tx_clone,
rx,
)
}
fn print_hex(bytes: &[u8]) {
let stdout = std::io::stdout();
let mut stdout = stdout.lock();
let mut hxp = hexyl::Printer::new(&mut stdout, true, hexyl::BorderStyle::None, false);
let _ = hxp.print_all(bytes);
}
fn main() {
let (thread_udp, _thread_udp_tx, thread_udp_rx) = listen();
for msg in thread_udp_rx.iter() {
print_hex(&msg);
let hdr_struct = DNSHeader::from_udp_datagram(&msg).unwrap();
dbg!(hdr_struct);
}
let _ = thread_udp.join();
}