Eliminate all useless allocation
This commit is contained in:
21
chapter4.md
21
chapter4.md
@@ -190,7 +190,7 @@ fn handle_query(socket: &UdpSocket) -> Result<()> {
|
||||
|
||||
// Next, `DnsPacket::from_buffer` is used to parse the raw bytes into
|
||||
// a `DnsPacket`.
|
||||
let request = DnsPacket::from_buffer(&mut req_buffer)?;
|
||||
let mut request = DnsPacket::from_buffer(&mut req_buffer)?;
|
||||
|
||||
// Create and initialize the response packet
|
||||
let mut packet = DnsPacket::new();
|
||||
@@ -199,15 +199,8 @@ fn handle_query(socket: &UdpSocket) -> Result<()> {
|
||||
packet.header.recursion_available = true;
|
||||
packet.header.response = true;
|
||||
|
||||
// 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.
|
||||
if request.questions.is_empty() {
|
||||
packet.header.rescode = ResultCode::FORMERR;
|
||||
}
|
||||
// Usually a question will be present, though.
|
||||
else {
|
||||
let question = &request.questions[0];
|
||||
// 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
|
||||
@@ -216,7 +209,7 @@ fn handle_query(socket: &UdpSocket) -> Result<()> {
|
||||
// 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) = lookup(&question.name, question.qtype) {
|
||||
packet.questions.push(question.clone());
|
||||
packet.questions.push(question);
|
||||
packet.header.rescode = result.header.rescode;
|
||||
|
||||
for rec in result.answers {
|
||||
@@ -235,6 +228,12 @@ fn handle_query(socket: &UdpSocket) -> Result<()> {
|
||||
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;
|
||||
}
|
||||
|
||||
// The only thing remaining is to encode our response and send it off!
|
||||
let mut res_buffer = BytePacketBuffer::new();
|
||||
|
||||
Reference in New Issue
Block a user