Pure Rust implementation to work with DNS packets
You can parse or write a DNS packet by using Packet
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 mut packet = Packet::new_query(1);
let question = Question::new(Name::newunchecked("srv._udp.local"), TYPE::TXT.into(), CLASS::IN.into(), false); packet.questions.push(question);
let resource = ResourceRecord::new(Name::newunchecked("srv.udp.local"), CLASS::IN, 10, RData::A(A { address: 10 })); 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()); ```
It is possible to check some information about a packet withouth parsing the packet, by using the header_buffer
module functions.
Be cautious when checking RCODE and packet flags, see the module documentation for more information.
```rust use simpledns::{headerbuffer, PacketFlag}; let buffer = 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";
asserteq!(Ok(3), headerbuffer::id(&buffer[..])); assert!(!headerbuffer::hasflags(&buffer[..], PacketFlag::RESPONSE).unwrap()); ```
EDNS is supported by Packet opt and opt_mut functions, when working with ENDS packets, you SHOULD NOT add OPT Resource Records directly to the Additional Records sections unless you know exactly what you are doing.
EDNS extends the DNS packet header by adding an OPT resource record and moving part of the header information to the additional records section. RCODE went from 4 bits to 12 bits, where the first 4 bits are stored in the header section and the last 8 bits are stored somewhere else inside the packet.
This has some implications on how a packet can be parsed or build ``` use simpledns::{headerbuffer, RCODE, Packet};
let buffer = b"\x00\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x01\x01\x2e\x00\x00\x29\x01\xf4\x00\x00\x03\x01\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; let packet = Packet::parse(&buffer[..]).unwrap();
// Without parsing the full packet, it is impossible to know the true RCODE of the packet asserteq!(RCODE::NoError, headerbuffer::rcode(&buffer[..]).unwrap()); assert_eq!(RCODE::BADVERS, packet.rcode()); ```
Please, refer to RFC 6891 for more information
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 - RFC 6891
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.