blob: e4e7c34927d9c037be9d0946fa4eede759ce8a58 [file] [log] [blame]
Austin Schuh41baf202022-01-01 14:33:40 -08001/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2015 by Sergey Fetisov <fsenok@gmail.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25/*
26 * version: 1.0 demo (7.02.2015)
27 * brief: tiny dns ipv4 server using lwip (pcb)
28 */
29
30#include "dnserver.h"
31
32#define DNS_MAX_HOST_NAME_LEN 128
33
34static struct udp_pcb *pcb = NULL;
35dns_query_proc_t query_proc = NULL;
36
37#pragma pack(push, 1)
38typedef struct
39{
40#if BYTE_ORDER == LITTLE_ENDIAN
41 uint8_t rd: 1, /* Recursion Desired */
42 tc: 1, /* Truncation Flag */
43 aa: 1, /* Authoritative Answer Flag */
44 opcode: 4, /* Operation code */
45 qr: 1; /* Query/Response Flag */
46 uint8_t rcode: 4, /* Response Code */
47 z: 3, /* Zero */
48 ra: 1; /* Recursion Available */
49#else
50 uint8_t qr: 1, /* Query/Response Flag */
51 opcode: 4, /* Operation code */
52 aa: 1, /* Authoritative Answer Flag */
53 tc: 1, /* Truncation Flag */
54 rd: 1; /* Recursion Desired */
55 uint8_t ra: 1, /* Recursion Available */
56 z: 3, /* Zero */
57 rcode: 4; /* Response Code */
58#endif
59} dns_header_flags_t;
60
61typedef struct
62{
63 uint16_t id;
64 dns_header_flags_t flags;
65 uint16_t n_record[4];
66} dns_header_t;
67
68typedef struct dns_answer
69{
70 uint16_t name;
71 uint16_t type;
72 uint16_t Class;
73 uint32_t ttl;
74 uint16_t len;
75 uint32_t addr;
76} dns_answer_t;
77#pragma pack(pop)
78
79typedef struct dns_query
80{
81 char name[DNS_MAX_HOST_NAME_LEN];
82 uint16_t type;
83 uint16_t Class;
84} dns_query_t;
85
86static uint16_t get_uint16(const uint8_t *pnt)
87{
88 uint16_t result;
89 memcpy(&result, pnt, sizeof(result));
90 return result;
91}
92
93static int parse_next_query(void *data, int size, dns_query_t *query)
94{
95 int len;
96 int lables;
97 uint8_t *ptr;
98
99 len = 0;
100 lables = 0;
101 ptr = (uint8_t *)data;
102
103 while (true)
104 {
105 uint8_t lable_len;
106 if (size <= 0) return -1;
107 lable_len = *ptr++;
108 size--;
109 if (lable_len == 0) break;
110 if (lables > 0)
111 {
112 if (len == DNS_MAX_HOST_NAME_LEN) return -2;
113 query->name[len++] = '.';
114 }
115 if (lable_len > size) return -1;
116 if (len + lable_len >= DNS_MAX_HOST_NAME_LEN) return -2;
117 memcpy(&query->name[len], ptr, lable_len);
118 len += lable_len;
119 ptr += lable_len;
120 size -= lable_len;
121 lables++;
122 }
123
124 if (size < 4) return -1;
125 query->name[len] = 0;
126 query->type = get_uint16(ptr);
127 ptr += 2;
128 query->Class = get_uint16(ptr);
129 ptr += 2;
130 return ptr - (uint8_t *)data;
131}
132
133static void udp_recv_proc(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
134{
135 int len;
136 dns_header_t *header;
137 static dns_query_t query;
138 struct pbuf *out;
139 ip4_addr_t host_addr;
140 dns_answer_t *answer;
141
142 (void)arg;
143
144 if (p->len <= sizeof(dns_header_t)) goto error;
145 header = (dns_header_t *)p->payload;
146 if (header->flags.qr != 0) goto error;
147 if (ntohs(header->n_record[0]) != 1) goto error;
148
149 len = parse_next_query(header + 1, p->len - sizeof(dns_header_t), &query);
150 if (len < 0) goto error;
151 if (!query_proc(query.name, &host_addr)) goto error;
152
153 len += sizeof(dns_header_t);
154 out = pbuf_alloc(PBUF_TRANSPORT, len + 16, PBUF_POOL);
155 if (out == NULL) goto error;
156
157 memcpy(out->payload, p->payload, len);
158 header = (dns_header_t *)out->payload;
159 header->flags.qr = 1;
160 header->n_record[1] = htons(1);
161 answer = (struct dns_answer *)((uint8_t *)out->payload + len);
162 answer->name = htons(0xC00C);
163 answer->type = htons(1);
164 answer->Class = htons(1);
165 answer->ttl = htonl(32);
166 answer->len = htons(4);
167 answer->addr = host_addr.addr;
168
169 udp_sendto(upcb, out, addr, port);
170 pbuf_free(out);
171
172error:
173 pbuf_free(p);
174}
175
176err_t dnserv_init(const ip_addr_t *bind, uint16_t port, dns_query_proc_t qp)
177{
178 err_t err;
179 udp_init();
180 dnserv_free();
181 pcb = udp_new();
182 if (pcb == NULL)
183 return ERR_MEM;
184 err = udp_bind(pcb, bind, port);
185 if (err != ERR_OK)
186 {
187 dnserv_free();
188 return err;
189 }
190 udp_recv(pcb, udp_recv_proc, NULL);
191 query_proc = qp;
192 return ERR_OK;
193}
194
195void dnserv_free()
196{
197 if (pcb == NULL) return;
198 udp_remove(pcb);
199 pcb = NULL;
200}