mirror of
https://github.com/EmilHernvall/dnsguide.git
synced 2024-12-22 21:34:30 +07:00
Chapter 2 tweaks
This commit is contained in:
parent
cf153a7847
commit
1e6d6ed142
35
chapter2.md
35
chapter2.md
@ -229,46 +229,31 @@ fn main() {
|
|||||||
|
|
||||||
// Bind a UDP socket to an arbitrary port
|
// Bind a UDP socket to an arbitrary port
|
||||||
let socket = UdpSocket::bind(("0.0.0.0", 43210)).unwrap();
|
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
|
// Build our query packet. It's important that we remember to set the
|
||||||
`recursion_desired` flag. As noted earlier, the packet id is arbitrary.
|
// `recursion_desired` flag. As noted earlier, the packet id is arbitrary.
|
||||||
|
|
||||||
```rust
|
|
||||||
let mut packet = DnsPacket::new();
|
let mut packet = DnsPacket::new();
|
||||||
|
|
||||||
packet.header.id = 6666;
|
packet.header.id = 6666;
|
||||||
packet.header.questions = 1;
|
packet.header.questions = 1;
|
||||||
packet.header.recursion_desired = true;
|
packet.header.recursion_desired = true;
|
||||||
packet.questions.push(DnsQuestion::new(qname.to_string(), qtype));
|
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
|
// ...and send it off to the server using our socket:
|
||||||
let mut req_buffer = BytePacketBuffer::new();
|
|
||||||
packet.write(&mut req_buffer).unwrap();
|
|
||||||
```
|
|
||||||
|
|
||||||
...and send it off to the server using our socket:
|
|
||||||
|
|
||||||
```rust
|
|
||||||
socket.send_to(&req_buffer.buf[0..req_buffer.pos], server).unwrap();
|
socket.send_to(&req_buffer.buf[0..req_buffer.pos], server).unwrap();
|
||||||
```
|
|
||||||
|
|
||||||
To prepare for receiving the response, we'll create a new `BytePacketBuffer`.
|
// 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.
|
// and ask the socket to write the response directly into our buffer.
|
||||||
|
|
||||||
```rust
|
|
||||||
let mut res_buffer = BytePacketBuffer::new();
|
let mut res_buffer = BytePacketBuffer::new();
|
||||||
socket.recv_from(&mut res_buffer.buf).unwrap();
|
socket.recv_from(&mut res_buffer.buf).unwrap();
|
||||||
```
|
|
||||||
|
|
||||||
As per the previous section, `DnsPacket::from_buffer()` is then used to
|
// As per the previous section, `DnsPacket::from_buffer()` is then used to
|
||||||
actually parse the packet after which we can print the response.
|
// actually parse the packet after which we can print the response.
|
||||||
|
|
||||||
```rust
|
|
||||||
let res_packet = DnsPacket::from_buffer(&mut res_buffer).unwrap();
|
let res_packet = DnsPacket::from_buffer(&mut res_buffer).unwrap();
|
||||||
println!("{:?}", res_packet.header);
|
println!("{:?}", res_packet.header);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user