dns/src/main.rs
ddidderr 812264e2ab
(chore) replace flume dependency with std::sync::mpsc (#12)
Co-authored-by: ddidderr <ddidderr@paul.network>
Reviewed-on: #12
2022-04-06 20:17:48 +02:00

41 lines
915 B
Rust

use std::sync::mpsc;
use std::net::UdpSocket;
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 main() {
let (thread_udp, _thread_udp_tx, thread_udp_rx) = listen();
for msg in thread_udp_rx.iter() {
let hdr_struct = DNSHeader::from_udp_datagram(&msg).unwrap();
dbg!(hdr_struct);
}
let _ = thread_udp.join();
}