pupdns/include/dns_packets.h

158 lines
3.9 KiB
C
Raw Permalink Normal View History

2025-05-13 13:17:10 -07:00
#pragma once
#include <stddef.h>
2025-07-18 16:25:26 -07:00
#define DNS_HEADER_BYTES 12
2025-05-13 13:17:10 -07:00
struct DNSPacketHeader {
2025-05-13 13:17:10 -07:00
/*
* ID (Packet Identifier)
* a random ID is assigned to query packets.
* Response packets must contain the same ID as the query.
*/
2025-07-18 16:25:26 -07:00
unsigned int packet_id : 16;
2025-05-13 13:17:10 -07:00
/*
2025-07-18 16:25:26 -07:00
* RD (Recursion Desired)
* if set by the client, the server should
* attempt to resolve the query recursively
2025-05-13 13:17:10 -07:00
*
2025-07-18 16:25:26 -07:00
* the server may not allow recursive resolution.
* (see RA / Recursion Available)
2025-05-13 13:17:10 -07:00
*/
2025-07-18 16:25:26 -07:00
unsigned int recursion_desired : 1;
2025-05-13 13:17:10 -07:00
/*
* TC (Truncated)
* if true, this message was truncated.
* usually happens when the response payload
* is larger than the max UDP packet size
*
* if set, the query may be re-issued over TCP for the full payload.
*/
2025-07-18 16:25:26 -07:00
unsigned int is_truncated : 1;
2025-05-13 13:17:10 -07:00
/*
2025-07-18 16:25:26 -07:00
* AA (Authoritative Answer)
2025-05-13 13:17:10 -07:00
*
2025-07-18 16:25:26 -07:00
* 1 if the responding server is authoritative for the queried domain
2025-05-13 13:17:10 -07:00
*/
2025-07-18 16:25:26 -07:00
unsigned int is_authoritative : 1;
2025-05-13 13:17:10 -07:00
/*
2025-07-18 16:25:26 -07:00
* OPCODE (Operation Code)
*
* 0 - standard query (QUERY)
* 1 - inverse query (IQUERY)
* 2 - server status request (STATUS)
* 3-15 - reserved
*
* almost always will be 0
2025-05-13 13:17:10 -07:00
*/
2025-07-18 16:25:26 -07:00
unsigned int opcode : 4;
2025-05-13 13:17:10 -07:00
/*
2025-07-18 16:25:26 -07:00
* QR (Query/Response)
* 0 if the packet is a query
* 1 if the packet is a response
2025-05-13 13:17:10 -07:00
*/
2025-07-18 16:25:26 -07:00
unsigned int is_response : 1;
2025-05-13 13:17:10 -07:00
/*
* RCODE (Response Code)
* Set by the server to indicate success or failure of the query.
* Provides details about the cause of any failure.
*
* 0 - No error
* 1 - Format error - the nameserver was unable to interpret the query
* 2 - Server failure - the nameserver was unable to process the query due to an internal problem
* 3 - Name error - Meaningful only from authoritative nameserver -- the domain name specified in the query does not exist.
* 4 - Not implemented - the nameserver does not support the requested query type
* 5 - Refused - the nameserver refuses to perform the requested operation. for example, it may refuse a zone transfer.
* 6-15 - Reserved for future use
*/
2025-07-18 16:25:26 -07:00
unsigned int response_code : 4;
/*
* Z (Reserved)
* in the original spec, this is reserved for future use.
*
* in later RFCs, it is used for DNSSEC queries.
* (to be implemented)
*/
unsigned int z : 3;
/*
* RA (Recursion Available)
* if set by the server, incicates that the server allows
* recursive queries.
*/
unsigned int recursion_available : 1;
2025-05-13 13:17:10 -07:00
/*
* QDCOUNT (Question Count)
* the number of entries in the question section
*/
2025-07-18 16:25:26 -07:00
unsigned int question_count : 16;
2025-05-13 13:17:10 -07:00
/*
* ANCOUNT (Answer Count)
* the number of entries in the answer section
*/
2025-07-18 16:25:26 -07:00
unsigned int answer_count : 16;
2025-05-13 13:17:10 -07:00
/*
* NSCOUNT (Nameserver Count)
* the number of entries in the authority records section
*/
2025-07-18 16:25:26 -07:00
unsigned int ns_count : 16;
2025-05-13 13:17:10 -07:00
/*
* ARCOUNT (Additional Records Count)
* the number of entries in the additional records section
*/
2025-07-18 16:25:26 -07:00
unsigned int ar_count : 16;
2025-05-13 13:17:10 -07:00
};
/*
* RecordPreamble
* shared fields that all DNS record types use
*/
struct RecordPreamble {
// TODO: add label sequence
/*
* TYPE
* the record type
*/
2025-07-18 16:25:26 -07:00
unsigned int type : 16;
2025-05-13 13:17:10 -07:00
/*
* CLASS
* specifies the class of the data in the RDATA field.
* in practice, always set to 1
*/
2025-07-18 16:25:26 -07:00
unsigned int class : 16;
2025-05-13 13:17:10 -07:00
/*
* TTL (Time-To-Live)
* how long a record can be cached before it should be requeried.
*/
2025-07-18 16:25:26 -07:00
unsigned int ttl : 32;
2025-05-13 13:17:10 -07:00
/*
* RDLENGTH (Record Data Length)
* length of the record-type-specific data.
*/
2025-07-18 16:25:26 -07:00
unsigned int rd_data_length: 16;
2025-05-13 13:17:10 -07:00
};
/*
* A Record
* represents the mapping of a domain name to an IPv4 address
*/
struct ARecord {
struct RecordPreamble preamble;
2025-07-18 16:25:26 -07:00
unsigned int ip : 32;
2025-05-13 13:17:10 -07:00
};