-
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.
Merge pull request #2 from bitsexplained/prep-for-publish
refactor to have lib crate
- Loading branch information
Showing
14 changed files
with
127 additions
and
115 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,25 @@ | ||
[package] | ||
name = "dns" | ||
version = "0.1.0" | ||
name = "micro-dns" | ||
version = "0.1.11" | ||
edition = "2021" | ||
description = "a recursive dns resolver" | ||
homepage = "https://github.com/bitsexplained/dns" | ||
documentation = "https://docs.rs/micro-dns" | ||
repository = "https://github.com/bitsexplained/dns" | ||
readme = "README.md" | ||
license = "MIT" | ||
keywords = ["dns", "resolver", "recursive"] | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
|
||
|
||
[[bin]] | ||
name = "micro-dns" | ||
path = "src/main.rs" | ||
|
||
|
||
[lib] | ||
name = "micro_dns" | ||
path = "src/lib/lib.rs" |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,4 +1,6 @@ | ||
use crate::utils::types::Result; | ||
|
||
use crate::types::Result; | ||
|
||
|
||
pub struct BytePacketBuffer{ | ||
pub buf: [u8; 512], | ||
|
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
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
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 |
---|---|---|
@@ -0,0 +1,89 @@ | ||
|
||
use std::net::UdpSocket; | ||
|
||
|
||
pub mod buffer; | ||
pub mod dns_header; | ||
pub mod dns_packet; | ||
pub mod dns_lookup; | ||
pub mod types; | ||
|
||
|
||
use crate::types::Result; | ||
use crate::buffer::BytePacketBuffer; | ||
use crate::dns_header::ResultCode; | ||
use crate::dns_packet::DnsPacket; | ||
use crate::dns_lookup::recursive_lookup; | ||
|
||
|
||
|
||
/// Handle a single incoming packet | ||
pub fn handle_query(socket: &UdpSocket) -> Result<()> { | ||
// With a socket ready, we can go ahead and read a packet. This will | ||
// block until one is received. | ||
let mut req_buffer = BytePacketBuffer::new(); | ||
|
||
// The `recv_from` function will write the data into the provided buffer, | ||
// and return the length of the data read as well as the source address. | ||
// We're not interested in the length, but we need to keep track of the | ||
// source in order to send our reply later on. | ||
let (_, src) = socket.recv_from(&mut req_buffer.buf)?; | ||
|
||
// Next, `DnsPacket::from_buffer` is used to parse the raw bytes into | ||
// a `DnsPacket`. | ||
let mut request = DnsPacket::from_buffer(&mut req_buffer)?; | ||
|
||
// Create and initialize the response packet | ||
let mut packet = DnsPacket::new(); | ||
packet.header.id = request.header.id; | ||
packet.header.recursion_desired = true; | ||
packet.header.recursion_available = true; | ||
packet.header.response = true; | ||
|
||
// In the normal case, exactly one question is present | ||
if let Some(question) = request.questions.pop() { | ||
println!("Received query: {:?}", question); | ||
|
||
// Since all is set up and as expected, the query can be forwarded to the | ||
// target server. There's always the possibility that the query will | ||
// fail, in which case the `SERVFAIL` response code is set to indicate | ||
// as much to the client. If rather everything goes as planned, the | ||
// question and response records as copied into our response packet. | ||
if let Ok(result) = recursive_lookup(&question.name, question.question_type) { | ||
packet.questions.push(question); | ||
packet.header.rescode = result.header.rescode; | ||
|
||
for rec in result.answers { | ||
println!("Answer: {:?}", rec); | ||
packet.answers.push(rec); | ||
} | ||
for rec in result.authorities { | ||
println!("Authority: {:?}", rec); | ||
packet.authorities.push(rec); | ||
} | ||
for rec in result.resources { | ||
println!("Resource: {:?}", rec); | ||
packet.resources.push(rec); | ||
} | ||
} else { | ||
packet.header.rescode = ResultCode::SERVFAIL; | ||
} | ||
} | ||
// Being mindful of how unreliable input data from arbitrary senders can be, we | ||
// need make sure that a question is actually present. If not, we return `FORMERR` | ||
// to indicate that the sender made something wrong. | ||
else { | ||
packet.header.rescode = ResultCode::FORMERR; | ||
} | ||
|
||
// encode our response and send it back | ||
let mut res_buffer = BytePacketBuffer::new(); | ||
packet.write(&mut res_buffer)?; | ||
|
||
let len = res_buffer.pos(); | ||
let data = res_buffer.get_range(0, len)?; | ||
|
||
socket.send_to(data, src)?; | ||
|
||
Ok(()) | ||
} |
File renamed without changes.
File renamed without changes.
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 was deleted.
Oops, something went wrong.