From 1e6d6ed142e84c5532b193fd038c12526522702d Mon Sep 17 00:00:00 2001 From: Emil Hernvall Date: Thu, 15 Mar 2018 14:20:10 +0100 Subject: [PATCH] Chapter 2 tweaks --- chapter2.md | 35 ++++++++++------------------------- 1 file changed, 10 insertions(+), 25 deletions(-) diff --git a/chapter2.md b/chapter2.md index 6cc6346..f782896 100644 --- a/chapter2.md +++ b/chapter2.md @@ -229,46 +229,31 @@ fn main() { // Bind a UDP socket to an arbitrary port let socket = UdpSocket::bind(("0.0.0.0", 43210)).unwrap(); -``` -Next we'll build our query packet. It's important that we remember to set the -`recursion_desired` flag. As noted earlier, the packet id is arbitrary. - -```rust + // Build our query packet. It's important that we remember to set the + // `recursion_desired` flag. As noted earlier, the packet id is arbitrary. let mut packet = DnsPacket::new(); packet.header.id = 6666; packet.header.questions = 1; packet.header.recursion_desired = true; packet.questions.push(DnsQuestion::new(qname.to_string(), qtype)); -``` -We can use our new write method to write the packet to a buffer... + // Use our new write method to write the packet to a buffer... + let mut req_buffer = BytePacketBuffer::new(); + packet.write(&mut req_buffer).unwrap(); -```rust - let mut req_buffer = BytePacketBuffer::new(); - packet.write(&mut req_buffer).unwrap(); -``` - -...and send it off to the server using our socket: - -```rust + // ...and send it off to the server using our socket: socket.send_to(&req_buffer.buf[0..req_buffer.pos], server).unwrap(); -``` -To prepare for receiving the response, we'll create a new `BytePacketBuffer`. -We'll then ask the socket to write the response directly into our buffer. - -```rust + // To prepare for receiving the response, we'll create a new `BytePacketBuffer`, + // and ask the socket to write the response directly into our buffer. let mut res_buffer = BytePacketBuffer::new(); socket.recv_from(&mut res_buffer.buf).unwrap(); -``` -As per the previous section, `DnsPacket::from_buffer()` is then used to -actually parse the packet after which we can print the response. - -```rust + // As per the previous section, `DnsPacket::from_buffer()` is then used to + // actually parse the packet after which we can print the response. let res_packet = DnsPacket::from_buffer(&mut res_buffer).unwrap(); println!("{:?}", res_packet.header);