Blame view

RIOT/sys/net/application_layer/dns/dns.c 4.85 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
  /*
   * Copyright (C) 2017 Kaspar Schleiser <kaspar@schleiser.de>
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @ingroup net_sock_dns
   * @{
   * @file
   * @brief   sock DNS client implementation
   * @author  Kaspar Schleiser <kaspar@schleiser.de>
   * @}
   */
  
  #include <string.h>
  #include <stdio.h>
  
  #include "net/sock/udp.h"
  #include "net/sock/dns.h"
  
  #ifdef RIOT_VERSION
  #include "byteorder.h"
  #endif
  
  /* min domain name length is 1, so minimum record length is 7 */
  #define DNS_MIN_REPLY_LEN   (unsigned)(sizeof(sock_dns_hdr_t ) + 7)
  
  static ssize_t _enc_domain_name(uint8_t *out, const char *domain_name)
  {
      /*
       * DNS encodes domain names with "<len><part><len><part>", e.g.,
       * "example.org" ends up as "\7example\3org" in the packet.
       */
      uint8_t *part_start = out;
      uint8_t *out_pos = ++out;
  
      char c;
  
      while ((c = *domain_name)) {
          if (c == '.') {
              /* replace dot with length of name part as byte */
              *part_start = (out_pos - part_start - 1);
              part_start = out_pos++;
          }
          else {
              *out_pos++ = c;
          }
          domain_name++;
      }
  
      *part_start = (out_pos - part_start - 1);
      *out_pos++ = 0;
  
      return out_pos - out + 1;
  }
  
  static unsigned _put_short(uint8_t *out, uint16_t val)
  {
      memcpy(out, &val, 2);
      return 2;
  }
  
  static unsigned _get_short(uint8_t *buf)
  {
      uint16_t _tmp;
      memcpy(&_tmp, buf, 2);
      return _tmp;
  }
  
  static size_t _skip_hostname(uint8_t *buf)
  {
      uint8_t *bufpos = buf;
  
      /* handle DNS Message Compression */
      if (*bufpos >= 192) {
          return 2;
      }
  
      while(*bufpos) {
          bufpos += *bufpos + 1;
      }
      return (bufpos - buf + 1);
  }
  
  static int _parse_dns_reply(uint8_t *buf, size_t len, void* addr_out, int family)
  {
      sock_dns_hdr_t *hdr = (sock_dns_hdr_t*) buf;
      uint8_t *bufpos = buf + sizeof(*hdr);
  
      /* skip all queries that are part of the reply */
      for (unsigned n = 0; n < ntohs(hdr->qdcount); n++) {
          bufpos += _skip_hostname(bufpos);
          bufpos += 4;    /* skip type and class of query */
      }
  
      for (unsigned n = 0; n < ntohs(hdr->ancount); n++) {
          bufpos += _skip_hostname(bufpos);
          uint16_t _type = ntohs(_get_short(bufpos));
          bufpos += 2;
          uint16_t class = ntohs(_get_short(bufpos));
          bufpos += 2;
          bufpos += 4; /* skip ttl */
  
          unsigned addrlen = ntohs(_get_short(bufpos));
          bufpos += 2;
          if ((bufpos + addrlen) > (buf + len)) {
              return -EBADMSG;
          }
  
          /* skip unwanted answers */
          if ((class != DNS_CLASS_IN) ||
                  ((_type == DNS_TYPE_A) && (family == AF_INET6)) ||
                  ((_type == DNS_TYPE_AAAA) && (family == AF_INET)) ||
                  ! ((_type == DNS_TYPE_A) || ((_type == DNS_TYPE_AAAA))
                      )) {
              bufpos += addrlen;
              continue;
          }
  
          memcpy(addr_out, bufpos, addrlen);
          return addrlen;
      }
  
      return -1;
  }
  
  int sock_dns_query(const char *domain_name, void *addr_out, int family)
  {
      uint8_t buf[SOCK_DNS_QUERYBUF_LEN];
      uint8_t reply_buf[512];
  
      if (strlen(domain_name) > SOCK_DNS_MAX_NAME_LEN) {
          return -ENOSPC;
      }
  
      sock_dns_hdr_t *hdr = (sock_dns_hdr_t*) buf;
      memset(hdr, 0, sizeof(*hdr));
      hdr->id = 0; /* random? */
      hdr->flags = htons(0x0120);
      hdr->qdcount = htons(1 + (family == AF_UNSPEC));
  
      uint8_t *bufpos = buf + sizeof(*hdr);
  
      unsigned _name_ptr;
      if ((family == AF_INET6) || (family == AF_UNSPEC)) {
          _name_ptr = (bufpos - buf);
          bufpos += _enc_domain_name(bufpos, domain_name);
          bufpos += _put_short(bufpos, htons(DNS_TYPE_AAAA));
          bufpos += _put_short(bufpos, htons(DNS_CLASS_IN));
      }
  
      if ((family == AF_INET) || (family == AF_UNSPEC)) {
          if (family == AF_UNSPEC) {
              bufpos += _put_short(bufpos, htons((0xc000) | (_name_ptr)));
          }
          else {
              bufpos += _enc_domain_name(bufpos, domain_name);
          }
          bufpos += _put_short(bufpos, htons(DNS_TYPE_A));
          bufpos += _put_short(bufpos, htons(DNS_CLASS_IN));
      }
  
      sock_udp_t sock_dns;
  
      ssize_t res = sock_udp_create(&sock_dns, NULL, &sock_dns_server, 0);
      if (res) {
              puts("a");
          goto out;
      }
  
      for (int i = 0; i < SOCK_DNS_RETRIES; i++) {
          res = sock_udp_send(&sock_dns, buf, (bufpos-buf), NULL);
          if (res <= 0) {
              continue;
          }
          res = sock_udp_recv(&sock_dns, reply_buf, sizeof(reply_buf), 1000000LU, NULL);
          if ((res > 0) && (res > (int)DNS_MIN_REPLY_LEN)) {
              if ((res = _parse_dns_reply(reply_buf, res, addr_out, family)) > 0) {
                  goto out;
              }
          }
      }
  
  out:
      sock_udp_close(&sock_dns);
      return res;
  }