Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_dns.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
27#include "nilorea/n_dns.h"
28
29#include <string.h>
30
32#define N_DNS_HEADER_LEN 12
33
34/* Read a big-endian 16-bit value at msg[off]. */
35static uint16_t n_dns_u16(const unsigned char* msg, size_t off) {
36 return (uint16_t)(((unsigned)msg[off] << 8) | (unsigned)msg[off + 1]);
37}
38
39int 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) {
40 size_t off = N_DNS_HEADER_LEN;
41 size_t out = 0;
42 if (!msg || len < N_DNS_HEADER_LEN)
43 return -1;
44 if (n_dns_u16(msg, 4) < 1) /* QDCOUNT */
45 return -1;
46 if (id_out)
47 *id_out = n_dns_u16(msg, 0);
48
49 /* decode the first QNAME (a sequence of length-prefixed labels ending in 0) */
50 for (;;) {
51 unsigned label_len;
52 if (off >= len)
53 return -1;
54 label_len = msg[off];
55 if (label_len == 0) {
56 off++;
57 break;
58 }
59 if ((label_len & 0xC0u) != 0) /* compression is not valid in a question */
60 return -1;
61 off++;
62 if (off + label_len > len)
63 return -1;
64 if (qname_out) {
65 if (out != 0) {
66 if (out + 1 >= qname_sz)
67 return -1;
68 qname_out[out++] = '.';
69 }
70 for (unsigned i = 0; i < label_len; i++) {
71 unsigned char c = msg[off + i];
72 if (out + 1 >= qname_sz)
73 return -1;
74 if (c >= 'A' && c <= 'Z')
75 c = (unsigned char)(c + 32); /* lowercase for case-insensitive matching */
76 qname_out[out++] = (char)c;
77 }
78 }
79 off += label_len;
80 }
81 if (qname_out) {
82 if (out >= qname_sz)
83 return -1;
84 qname_out[out] = '\0';
85 }
86
87 if (off + 4 > len) /* QTYPE + QCLASS */
88 return -1;
89 if (qtype_out)
90 *qtype_out = n_dns_u16(msg, off);
91 return 0;
92}
93
94int 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) {
95 size_t qend = N_DNS_HEADER_LEN;
96 size_t pos;
97 uint16_t qflags;
98 uint16_t rflags;
99 if (!query || qlen < N_DNS_HEADER_LEN || !out)
100 return -1;
101
102 /* walk the first question's QNAME to find where the question section ends */
103 for (;;) {
104 unsigned label_len;
105 if (qend >= qlen)
106 return -1;
107 label_len = query[qend];
108 if (label_len == 0) {
109 qend++;
110 break;
111 }
112 if ((label_len & 0xC0u) != 0)
113 return -1;
114 qend += (size_t)label_len + 1;
115 }
116 qend += 4; /* QTYPE + QCLASS */
117 if (qend > qlen)
118 return -1;
119 if (outsz < qend + 16) /* the question plus a 16-byte A answer */
120 return -1;
121
122 /* copy the header and question verbatim, then rewrite the header */
123 memcpy(out, query, qend);
124 qflags = n_dns_u16(query, 2);
125 /* QR=1, echo opcode + RD, set AA=1 and RA=1, RCODE=0 */
126 rflags = (uint16_t)(0x8000u | (qflags & 0x7800u) | 0x0400u | (qflags & 0x0100u) | 0x0080u);
127 out[2] = (unsigned char)(rflags >> 8);
128 out[3] = (unsigned char)(rflags & 0xFFu);
129 out[4] = 0;
130 out[5] = 1; /* QDCOUNT = 1 */
131 out[6] = 0;
132 out[7] = 1; /* ANCOUNT = 1 */
133 out[8] = 0;
134 out[9] = 0; /* NSCOUNT = 0 */
135 out[10] = 0;
136 out[11] = 0; /* ARCOUNT = 0 */
137
138 /* answer resource record: name -> pointer to the question at offset 12 */
139 pos = qend;
140 out[pos++] = 0xC0;
141 out[pos++] = 0x0C;
142 out[pos++] = 0x00;
143 out[pos++] = 0x01; /* TYPE = A */
144 out[pos++] = 0x00;
145 out[pos++] = 0x01; /* CLASS = IN */
146 out[pos++] = (unsigned char)((ttl >> 24) & 0xFFu);
147 out[pos++] = (unsigned char)((ttl >> 16) & 0xFFu);
148 out[pos++] = (unsigned char)((ttl >> 8) & 0xFFu);
149 out[pos++] = (unsigned char)(ttl & 0xFFu);
150 out[pos++] = 0x00;
151 out[pos++] = 0x04; /* RDLENGTH = 4 */
152 memcpy(out + pos, &ipv4_be, 4); /* RDATA: the address in network byte order */
153 pos += 4;
154
155 return (int)pos;
156}
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.
Definition n_dns.c:94
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.
Definition n_dns.c:39
#define N_DNS_HEADER_LEN
DNS header size in bytes (id, flags, and the four section counts).
Definition n_dns.c:32
static uint16_t n_dns_u16(const unsigned char *msg, size_t off)
Definition n_dns.c:35
Minimal DNS message helpers: parse a query question and build an A-record response.