Pure Rust implementation to work with DNS packets
You can parse or write a DNS packet by using Packet or PacketBuf structs
Packet holds references for the original data and it is more suitable for situations where you need to manipulate the packet before generating the final bytes buffer
```rust use simpledns::*; use simpledns::rdata::*; let question = Question::new(Name::newunchecked("srv.udp.local"), TYPE::TXT.into(), CLASS::IN.into(), false); let resource = ResourceRecord::new(Name::newunchecked("srv.udp.local"), CLASS::IN, 10, RData::A(A { address: 10 }));
let mut packet = Packet::newquery(1, false); packet.questions.push(question); packet.additionalrecords.push(resource);
let bytes = packet.buildbytesvec(); assert!(bytes.is_ok());
// Same as above, but Names are compressed
let bytes = packet.buildbytesveccompressed();
assert!(bytes.isok());
``
It doesn't matter what order the resources are added, the packet will be built only when
buildbytesvec` is called
To parse the contents of a buffer into a packet, you need call call [Packet::parse] ```rust use simple_dns::Packet;
let bytes = b"\x00\x03\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x06\x67\x6f\x6f\x67\x6c\x65\x03\x63\x6f\x6d\x00\x00\x01\x00\x01"; let packet = Packet::parse(&bytes[..]); assert!(packet.is_ok()); ```
PacketBuf holds an internal buffer that is populated right when a resource is added.
It DOES matter the order in which the resources are added
```rust use simpledns::*; use simpledns::rdata::*; let question = Question::new(Name::newunchecked("srv.udp.local"), TYPE::TXT.into(), CLASS::IN.into(), false); let resource = ResourceRecord::new(Name::newunchecked("srv.udp.local"), CLASS::IN, 10, RData::A(A { address: 10 }));
let mut packet = PacketBuf::new(PacketHeader::newquery(1, false), true); assert!(packet.addanswer(&resource).isok()); assert!(packet.addquestion(&question).is_err()); //This will fail, since an answer is already added ```
It is possible to create a PacketBuf from a buffer by calling PacketBuf::from, but be aware that this will clone the contents from the buffer
The Packet structure provides parsing e building of a DNS packet, it aims to be fully compliant with the RFCs bellow: - RFC 1034 - RFC 1035 - RFC 1138 - RFC 1183 - RFC 1706 - RFC 1876 - RFC 1996 - RFC 2136 - RFC 6762 - RFC 2782 - RFC 3596
Other Resource Records defined by other RFCs that are not in this list will be implemented over time
This library can parse update packets, however, it does not validate update rules and the update fields are overloaded in the packet fields, as defined in the RFC 2136.