2022-04-03 17:34:52 +02:00
|
|
|
use std::sync::mpsc;
|
2022-03-13 21:56:17 +01:00
|
|
|
use std::net::UdpSocket;
|
|
|
|
use std::thread::{self, JoinHandle};
|
|
|
|
|
|
|
|
mod proto;
|
2022-03-15 17:01:17 +01:00
|
|
|
use proto::DNSHeader;
|
2022-03-13 21:56:17 +01:00
|
|
|
|
2022-04-03 17:34:52 +02:00
|
|
|
fn listen() -> (JoinHandle<()>, mpsc::Sender<Vec<u8>>, mpsc::Receiver<Vec<u8>>) {
|
|
|
|
let (tx, rx) = mpsc::channel();
|
2022-03-13 21:56:17 +01:00
|
|
|
|
|
|
|
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,
|
2022-04-03 17:34:52 +02:00
|
|
|
rx,
|
2022-03-13 21:56:17 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let (thread_udp, _thread_udp_tx, thread_udp_rx) = listen();
|
|
|
|
|
|
|
|
for msg in thread_udp_rx.iter() {
|
2022-03-15 17:01:17 +01:00
|
|
|
let hdr_struct = DNSHeader::from_udp_datagram(&msg).unwrap();
|
2022-03-13 21:56:17 +01:00
|
|
|
dbg!(hdr_struct);
|
|
|
|
}
|
|
|
|
|
|
|
|
let _ = thread_udp.join();
|
|
|
|
}
|