mirror of
https://github.com/ZeusWPI/ZNS.git
synced 2024-10-30 05:24:26 +01:00
Handle parser errors
This commit is contained in:
parent
9f8ecac4f0
commit
79b040f5f9
1 changed files with 30 additions and 11 deletions
|
@ -5,8 +5,9 @@ use std::sync::Arc;
|
||||||
use tokio::net::UdpSocket;
|
use tokio::net::UdpSocket;
|
||||||
|
|
||||||
use crate::db::models::{get_from_database, insert_into_database};
|
use crate::db::models::{get_from_database, insert_into_database};
|
||||||
|
use crate::errors::ParseError;
|
||||||
use crate::parser::FromBytes;
|
use crate::parser::FromBytes;
|
||||||
use crate::structs::{Class, Message, Type, RCODE, RR, Opcode};
|
use crate::structs::{Class, Header, Message, Opcode, Type, RCODE};
|
||||||
use crate::utils::vec_equal;
|
use crate::utils::vec_equal;
|
||||||
|
|
||||||
const MAX_DATAGRAM_SIZE: usize = 4096;
|
const MAX_DATAGRAM_SIZE: usize = 4096;
|
||||||
|
@ -15,7 +16,6 @@ fn set_response_flags(flags: u16, rcode: RCODE) -> u16 {
|
||||||
(flags | 0b1_0000_1_0_0_0_000_0000 | rcode as u16) & 0b1_1111_1_0_1_0_111_1111
|
(flags | 0b1_0000_1_0_0_0_000_0000 | rcode as u16) & 0b1_1111_1_0_1_0_111_1111
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn get_opcode(flags: &u16) -> Result<Opcode, String> {
|
fn get_opcode(flags: &u16) -> Result<Opcode, String> {
|
||||||
Opcode::try_from((flags & 0b0111100000000000) >> 11)
|
Opcode::try_from((flags & 0b0111100000000000) >> 11)
|
||||||
}
|
}
|
||||||
|
@ -55,10 +55,7 @@ async fn handle_update(message: Message) -> Message {
|
||||||
// Check Zone authority
|
// Check Zone authority
|
||||||
let zone = &message.question[0];
|
let zone = &message.question[0];
|
||||||
let zlen = zone.qname.len();
|
let zlen = zone.qname.len();
|
||||||
if !(zlen >= 2
|
if !(zlen >= 2 && zone.qname[zlen - 1] == "gent" && zone.qname[zlen - 2] == "zeus") {
|
||||||
&& zone.qname[zlen - 1] == "gent"
|
|
||||||
&& zone.qname[zlen - 2] == "zeus")
|
|
||||||
{
|
|
||||||
response.header.flags = set_response_flags(response.header.flags, RCODE::NOTAUTH);
|
response.header.flags = set_response_flags(response.header.flags, RCODE::NOTAUTH);
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
@ -89,7 +86,6 @@ async fn handle_update(message: Message) -> Message {
|
||||||
response.header.flags = set_response_flags(response.header.flags, RCODE::FORMERR);
|
response.header.flags = set_response_flags(response.header.flags, RCODE::FORMERR);
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for rr in message.authority {
|
for rr in message.authority {
|
||||||
|
@ -108,6 +104,32 @@ async fn handle_update(message: Message) -> Message {
|
||||||
response
|
response
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn handle_parse_error(bytes: &[u8], err: ParseError) -> Message {
|
||||||
|
eprintln!("{}", err);
|
||||||
|
let mut header = Header::from_bytes(bytes, &mut 0).unwrap_or(Header {
|
||||||
|
id: 0,
|
||||||
|
flags: 0,
|
||||||
|
qdcount: 0,
|
||||||
|
ancount: 0,
|
||||||
|
nscount: 0,
|
||||||
|
arcount: 0,
|
||||||
|
});
|
||||||
|
|
||||||
|
header.qdcount = 0;
|
||||||
|
header.ancount = 0;
|
||||||
|
header.nscount = 0;
|
||||||
|
header.arcount = 0;
|
||||||
|
header.flags = set_response_flags(header.flags, RCODE::FORMERR);
|
||||||
|
|
||||||
|
Message {
|
||||||
|
header,
|
||||||
|
question: vec![],
|
||||||
|
answer: vec![],
|
||||||
|
authority: vec![],
|
||||||
|
additional: vec![],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async fn get_response(bytes: &[u8]) -> Message {
|
async fn get_response(bytes: &[u8]) -> Message {
|
||||||
let mut i: usize = 0;
|
let mut i: usize = 0;
|
||||||
match Message::from_bytes(bytes, &mut i) {
|
match Message::from_bytes(bytes, &mut i) {
|
||||||
|
@ -118,10 +140,7 @@ async fn get_response(bytes: &[u8]) -> Message {
|
||||||
},
|
},
|
||||||
Err(_) => todo!(),
|
Err(_) => todo!(),
|
||||||
},
|
},
|
||||||
Err(err) => {
|
Err(err) => handle_parse_error(bytes, err),
|
||||||
println!("{}", err);
|
|
||||||
unimplemented!() //TODO: implement this
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue