Coverage Report

Created: 2024-09-08 06:39

/src/tinyusb/lib/networking/dnserver.c
Line
Count
Source (jump to first uncovered line)
1
/*
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
0
#define DNS_MAX_HOST_NAME_LEN 128
33
34
static struct udp_pcb *pcb = NULL;
35
dns_query_proc_t query_proc = NULL;
36
37
#pragma pack(push, 1)
38
typedef 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
61
typedef struct
62
{
63
  uint16_t id;
64
  dns_header_flags_t flags;
65
  uint16_t n_record[4];
66
} dns_header_t;
67
68
typedef 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
79
typedef 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
86
static uint16_t get_uint16(const uint8_t *pnt)
87
0
{
88
0
  uint16_t result;
89
0
  memcpy(&result, pnt, sizeof(result));
90
0
  return result;
91
0
}
92
93
static int parse_next_query(void *data, int size, dns_query_t *query)
94
0
{
95
0
  int len;
96
0
  int labels;
97
0
  uint8_t *ptr;
98
99
0
  len = 0;
100
0
  labels = 0;
101
0
  ptr = (uint8_t *)data;
102
103
0
  while (true)
104
0
  {
105
0
    uint8_t lable_len;
106
0
    if (size <= 0) return -1;
107
0
    lable_len = *ptr++;
108
0
    size--;
109
0
    if (lable_len == 0) break;
110
0
    if (labels > 0)
111
0
    {
112
0
      if (len == DNS_MAX_HOST_NAME_LEN) return -2;
113
0
      query->name[len++] = '.';
114
0
    }
115
0
    if (lable_len > size) return -1;
116
0
    if (len + lable_len >= DNS_MAX_HOST_NAME_LEN) return -2;
117
0
    memcpy(&query->name[len], ptr, lable_len);
118
0
    len += lable_len;
119
0
    ptr += lable_len;
120
0
    size -= lable_len;
121
0
    labels++;
122
0
  }
123
124
0
  if (size < 4) return -1;
125
0
  query->name[len] = 0;
126
0
  query->type = get_uint16(ptr);
127
0
  ptr += 2;
128
0
  query->Class = get_uint16(ptr);
129
0
  ptr += 2;
130
0
  return ptr - (uint8_t *)data;
131
0
}
132
133
static void udp_recv_proc(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
134
0
{
135
0
  int len;
136
0
  dns_header_t *header;
137
0
  static dns_query_t query;
138
0
  struct pbuf *out;
139
0
  ip4_addr_t host_addr;
140
0
  dns_answer_t *answer;
141
142
0
  (void)arg;
143
144
0
  if (p->len <= sizeof(dns_header_t)) goto error;
145
0
  header = (dns_header_t *)p->payload;
146
0
  if (header->flags.qr != 0) goto error;
147
0
  if (ntohs(header->n_record[0]) != 1) goto error;
148
149
0
  len = parse_next_query(header + 1, p->len - sizeof(dns_header_t), &query);
150
0
  if (len < 0) goto error;
151
0
  if (!query_proc(query.name, &host_addr)) goto error;
152
153
0
  len += sizeof(dns_header_t);
154
0
  out = pbuf_alloc(PBUF_TRANSPORT, len + 16, PBUF_POOL);
155
0
  if (out == NULL) goto error;
156
157
0
  memcpy(out->payload, p->payload, len);
158
0
  header = (dns_header_t *)out->payload;
159
0
  header->flags.qr = 1;
160
0
  header->n_record[1] = htons(1);
161
0
  answer = (struct dns_answer *)((uint8_t *)out->payload + len);
162
0
  answer->name = htons(0xC00C);
163
0
  answer->type = htons(1);
164
0
  answer->Class = htons(1);
165
0
  answer->ttl = htonl(32);
166
0
  answer->len = htons(4);
167
0
  answer->addr = host_addr.addr;
168
169
0
  udp_sendto(upcb, out, addr, port);
170
0
  pbuf_free(out);
171
172
0
error:
173
0
  pbuf_free(p);
174
0
}
175
176
err_t dnserv_init(const ip_addr_t *bind, uint16_t port, dns_query_proc_t qp)
177
0
{
178
0
  err_t err;
179
0
  udp_init();
180
0
  dnserv_free();
181
0
  pcb = udp_new();
182
0
  if (pcb == NULL)
183
0
    return ERR_MEM;
184
0
  err = udp_bind(pcb, bind, port);
185
0
  if (err != ERR_OK)
186
0
  {
187
0
    dnserv_free();
188
0
    return err;
189
0
  }
190
0
  udp_recv(pcb, udp_recv_proc, NULL);
191
0
  query_proc = qp;
192
0
  return ERR_OK;
193
0
}
194
195
void dnserv_free(void)
196
0
{
197
0
  if (pcb == NULL) return;
198
0
  udp_remove(pcb);
199
0
  pcb = NULL;
200
0
}