Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
DNS: minimal query parse and A-record response

Macros

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

Functions

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.
 
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.
 

Detailed Description

Macro Definition Documentation

◆ N_DNS_QNAME_MAX

#define N_DNS_QNAME_MAX   256

Maximum decoded QNAME length, including the terminating NUL.

Examples
ex_dns.c.

Definition at line 43 of file n_dns.h.

◆ N_DNS_TYPE_A

#define N_DNS_TYPE_A   1

DNS resource-record type A (IPv4 address).

Examples
ex_dns.c.

Definition at line 45 of file n_dns.h.

Function Documentation

◆ 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.

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.

Parameters
queryThe received query bytes. NULL returns -1.
qlenThe query length in bytes.
ipv4_beThe answer IPv4 address, in network byte order.
ttlThe record time-to-live in seconds.
outOutput buffer for the response. NULL returns -1.
outszSize of out.
Returns
The response length in bytes, or -1 on a malformed query or too-small buffer.

Definition at line 94 of file n_dns.c.

References N_DNS_HEADER_LEN, and n_dns_u16().

Referenced by test_build_response(), and test_errors().

+ Here is the call graph for this function:
+ Here is the caller graph for this 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.

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.

Parameters
msgThe DNS message bytes. NULL returns -1.
lenThe message length in bytes.
id_outOut: the 16-bit transaction id, or NULL.
qname_outOut: the decoded question name, or NULL.
qname_szSize of qname_out.
qtype_outOut: the question type (QTYPE), or NULL.
Returns
0 on success, -1 on a malformed or truncated message.

Definition at line 39 of file n_dns.c.

References N_DNS_HEADER_LEN, and n_dns_u16().

Referenced by test_build_response(), test_errors(), and test_parse().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: