dns/src/main.rs
mice_on_drugs 6f9e4e9d59 Create module structure (#29)
Co-authored-by: Tobias Ottenweller <tobi@ottenweller.net>
Reviewed-on: #29
Co-authored-by: mice_on_drugs <tobi@ottenweller.net>
Co-committed-by: mice_on_drugs <tobi@ottenweller.net>
2022-06-05 19:05:49 +02:00

48 lines
983 B
Rust

use std::net::UdpSocket;
use std::sync::mpsc;
use std::thread::{self, JoinHandle};
use hexhex::print_hex;
use crate::proto::{UdpCoder, Coder};
mod proto;
mod models;
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 main() {
let (thread_udp, _thread_udp_tx, thread_udp_rx) = listen();
for msg in thread_udp_rx.iter() {
print_hex(&msg);
let query = UdpCoder::decode(&msg).unwrap();
dbg!(query);
}
let _ = thread_udp.join();
}