-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7034bf0
commit 88a4622
Showing
3 changed files
with
74 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)?; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |