5 Module N_DNS
gullradriel edited this page 2026-10-09 00:55:18 +02:00

DNS: minimal query parse and A-record response

Functions

Name
int n_dns_parse_query(const unsigned char * msg, size_t len, uint16_t * id_out, char * qname_out, size_t qname_sz, uint16_t * qtype_out)
Parse the header id and the first question (QNAME + QTYPE) of a DNS message.
int n_dns_build_a_response(const unsigned char * query, size_t qlen, uint32_t ipv4_be, uint32_t ttl, unsigned char * out, size_t outsz)
Build a DNS response answering a query's first question with one A record.

Defines

Name
N_DNS_TYPE_A
DNS resource-record type A (IPv4 address).
N_DNS_QNAME_MAX
Maximum decoded QNAME length, including the terminating NUL.

Functions Documentation

function n_dns_parse_query

int n_dns_parse_query(
    const unsigned char * msg,
    size_t len,
    uint16_t * id_out,
    char * qname_out,
    size_t qname_sz,
    uint16_t * qtype_out
)

Parse the header id and the first question (QNAME + QTYPE) of a DNS message.

Parameters:

  • msg The DNS message bytes. NULL returns -1.
  • len The message length in bytes.
  • id_out Out: the 16-bit transaction id, or NULL.
  • qname_out Out: the decoded question name, or NULL.
  • qname_sz Size of qname_out.
  • qtype_out Out: the question type (QTYPE), or NULL.

Return: 0 on success, -1 on a malformed or truncated message.

The QNAME is written as a lowercase dot-separated string (no trailing dot). Name compression is rejected in the question (a query has none). Any out pointer may be NULL to skip it.

function n_dns_build_a_response

int n_dns_build_a_response(
    const unsigned char * query,
    size_t qlen,
    uint32_t ipv4_be,
    uint32_t ttl,
    unsigned char * out,
    size_t outsz
)

Build a DNS response answering a query's first question with one A record.

Parameters:

  • query The received query bytes. NULL returns -1.
  • qlen The query length in bytes.
  • ipv4_be The answer IPv4 address, in network byte order.
  • ttl The record time-to-live in seconds.
  • out Output buffer for the response. NULL returns -1.
  • outsz Size of out.

Return: The response length in bytes, or -1 on a malformed query or too-small buffer.

Echoes the query header (with QR/AA/RA set and RCODE 0) and question, then appends a single A resource record pointing at ipv4_be. The address is taken in network byte order (as returned by inet_addr) and copied verbatim into the RDATA, so the helper is endianness-agnostic.

Macros Documentation

define N_DNS_TYPE_A

#define N_DNS_TYPE_A 1

DNS resource-record type A (IPv4 address).

define N_DNS_QNAME_MAX

#define N_DNS_QNAME_MAX 256

Maximum decoded QNAME length, including the terminating NUL.