Skip to content

Commit

Permalink
Add Message type
Browse files Browse the repository at this point in the history
  • Loading branch information
hernangonzalez committed May 31, 2024
1 parent 7034bf0 commit 88a4622
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 22 deletions.
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ edition = "2021"
#
# DON'T EDIT THIS!
[dependencies]
anyhow = "1.0.68" # error handling
bytes = "1.3.0" # helps manage buffers
thiserror = "1.0.38" # error handling
nom = "7.1.3" # parsing
rand = "0.8.5" # randomness
anyhow = "1.0.68" # error handling
bytes = "1.3.0" # helps manage buffers
thiserror = "1.0.38" # error handling
nom = "7.1.3" # parsing
rand = "0.8.5" # randomness
29 changes: 12 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
// Uncomment this block to pass the first stage
#![warn(clippy::pedantic)]

mod message;
use anyhow::Result;
use std::net::UdpSocket;

fn main() {
// You can use print statements as follows for debugging, they'll be visible when running tests.
fn main() -> Result<()> {
println!("Logs from your program will appear here!");

// Uncomment this block to pass the first stage
let udp_socket = UdpSocket::bind("127.0.0.1:2053").expect("Failed to bind to address");
let mut buf = [0; 512];

loop {
match udp_socket.recv_from(&mut buf) {
Ok((size, source)) => {
println!("Received {} bytes from {}", size, source);
let response = [];
udp_socket
.send_to(&response, source)
.expect("Failed to send response");
}
Err(e) => {
eprintln!("Error receiving data: {}", e);
break;
}
}
let (size, source) = udp_socket.recv_from(&mut buf)?;
anyhow::ensure!(size > 12, "Packet is not long enough: {size}");

dbg!(size);

let response = [];
udp_socket.send_to(&response, source)?;
}
}
57 changes: 57 additions & 0 deletions src/message.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#[allow(dead_code)]
#[derive(Clone, Copy, Debug)]
struct PacketId(u16);

#[allow(dead_code)]
#[derive(Clone, Copy, Debug)]
enum QueryMode {
Query,
Response,
}

#[allow(dead_code)]
#[derive(Clone, Copy, Debug)]
struct OpCode(u8); // 4 bits

#[allow(dead_code)]
#[derive(Clone, Copy, Debug)]
struct Authoritative(bool);

#[allow(dead_code)]
#[derive(Clone, Copy, Debug)]
struct Truncated(bool);

#[allow(dead_code)]
#[derive(Clone, Copy, Debug)]
struct Recursive(bool);

#[allow(dead_code)]
#[derive(Clone, Copy, Debug)]
struct Reserved; // 3 bits

#[allow(dead_code)]
#[derive(Clone, Copy, Debug)]
struct ResCode(u8); // 4 bits

#[allow(dead_code)]
#[derive(Clone, Copy, Debug)]
struct Header {
id: PacketId,
qr: QueryMode,
op_code: OpCode,
aa: Authoritative,
tc: Truncated,
red: Recursive,
z: Reserved,
r_code: ResCode,
qd_count: u16,
an_count: u16,
ns_count: u16,
ar_count: u16,
}

#[allow(dead_code)]
#[derive(Clone, Copy, Debug)]
struct Message {
header: Header,
}

0 comments on commit 88a4622

Please sign in to comment.