From ec0b93e7c0a6e732ad9b1910b3ead8e92dd2e94f Mon Sep 17 00:00:00 2001 From: Xander Bil Date: Sun, 3 Mar 2024 22:52:36 +0100 Subject: [PATCH] fixed loop --- src/main.rs | 40 ++++++++++++++++++++-------------------- src/structs.rs | 2 +- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/main.rs b/src/main.rs index 915f3e2..1242bb0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use std::{error::Error, net::SocketAddr}; +use std::{error::Error, net::SocketAddr, sync::Arc}; use parser::FromBytes; use structs::Message; @@ -17,12 +17,12 @@ async fn create_query(message: Message) -> Message { let mut response = message.clone(); let ip = String::from("93.184.216.34"); let rr = RR { - name: vec![String::from("example"),String::from("org")], + name: vec![String::from("example"), String::from("org")], _type: Type::A, class: Class::IN, ttl: 4096, rdlength: ip.len() as u16, - rdata: vec![1,2,3,4], + rdata: vec![1, 2, 3, 4], }; response.header.flags |= 0b1000010110000000; @@ -37,22 +37,22 @@ async fn create_query(message: Message) -> Message { async fn main() -> Result<(), Box> { let local_addr: SocketAddr = "127.0.0.1:8080".parse()?; - let socket = UdpSocket::bind(local_addr).await?; + let socket_shared = Arc::new(UdpSocket::bind(local_addr).await?); - let mut data = vec![0u8; MAX_DATAGRAM_SIZE]; - let (len,addr) = socket.recv_from(&mut data).await?; - match Message::from_bytes(&data[..len]) { - Ok(message) => { - tokio::spawn(async move { - let response = create_query(message).await; - println!("{:?}",response); - let vec = Message::to_bytes(response); - let decoded = Message::from_bytes(vec.as_slice()); - println!("{:?}",decoded); - let _ = socket.send_to(vec.as_slice(),addr).await; - }); - } - Err(err) => println!("{}", err), - }; - loop {} + loop { + let mut data = vec![0u8; MAX_DATAGRAM_SIZE]; + let (len, addr) = socket_shared.recv_from(&mut data).await?; + match Message::from_bytes(&data[..len]) { + Ok(message) => { + let socket = socket_shared.clone(); + tokio::spawn(async move { + let response = create_query(message).await; + let _ = socket + .send_to(Message::to_bytes(response).as_slice(), addr) + .await; + }); + } + Err(err) => println!("{}", err), + }; + } } diff --git a/src/structs.rs b/src/structs.rs index 7eeaacd..06350f2 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -12,7 +12,7 @@ pub enum Class { #[derive(Debug, Clone)] pub struct Question { - pub qname: Vec, // TODO: not padded + pub qname: Vec, pub qtype: Type, // NOTE: should be QTYPE, right now not really needed pub qclass: Class, //NOTE: should be QCLASS, right now not really needed }