10
0
Fork 0
mirror of https://github.com/ZeusWPI/ZNS.git synced 2024-10-30 05:24:26 +01:00

fixed loop

This commit is contained in:
Xander Bil 2024-03-03 22:52:36 +01:00
parent 08b5947fbb
commit ec0b93e7c0
No known key found for this signature in database
GPG key ID: EC9706B54A278598
2 changed files with 21 additions and 21 deletions

View file

@ -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<dyn Error>> {
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),
};
}
}

View file

@ -12,7 +12,7 @@ pub enum Class {
#[derive(Debug, Clone)]
pub struct Question {
pub qname: Vec<String>, // TODO: not padded
pub qname: Vec<String>,
pub qtype: Type, // NOTE: should be QTYPE, right now not really needed
pub qclass: Class, //NOTE: should be QCLASS, right now not really needed
}