Struct dns_parser::Builder
[−]
[src]
pub struct Builder { buf: Vec<u8>, labels: HashMap<String, u16>, }
Allows to build a DNS packet
Both query and answer packets may be built with this interface, although, much of functionality is not implemented yet.
Fields
buf | |
labels |
Methods
impl Builder
fn new_query(id: u16, recursion: bool) -> Builder
Creates a new query
Initially all sections are empty. You're expected to fill
the questions section with add_question
fn new_response(id: u16, rc: ResponseCode, tc: bool, rd: bool, ra: bool) -> Builder
Creates a new response
Similar to new_query
, all sections are empty. You
will need to add all your questions first, then add
your answers.
fn add_question(&mut self, qname: &str, qtype: QueryType, qclass: QueryClass) -> &mut Builder
Adds a question to the packet
Panics
- Answers, nameservers or additional section has already been written
- There are already 65535 questions in the buffer.
- When name is invalid
fn add_answer(&mut self, aname: &str, atype: Type, aclass: Class, ttl: u32, data: Vec<u8>) -> &mut Builder
Adds an answer to the packet
NOTE: You need to add all you questions first before adding answers.
Panics
- There are too many answers in the buffer.
- When name is invalid
fn write_name(&mut self, name: &str)
fn build(self) -> Result<Vec<u8>, Vec<u8>>
Returns the final packet
When packet is not truncated method returns Ok(packet)
. If
packet is truncated the method returns Err(packet)
. In both
cases the packet is fully valid.
In the server implementation you may use
x.build().unwrap_or_else(|x| x)
.
In the client implementation it's probably unwise to send truncated packet, as it doesn't make sense. Even panicking may be more appropriate.