dnserver.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. * version: 1.0 demo (7.02.2015)
  26. * brief: tiny dns ipv4 server using lwip (pcb)
  27. */
  28. #include "dnserver.h"
  29. #define DNS_MAX_HOST_NAME_LEN 128
  30. static struct udp_pcb *pcb = NULL;
  31. dns_query_proc_t query_proc = NULL;
  32. #pragma pack(push, 1)
  33. typedef struct
  34. {
  35. #if BYTE_ORDER == LITTLE_ENDIAN
  36. uint8_t rd: 1, /* Recursion Desired */
  37. tc: 1, /* Truncation Flag */
  38. aa: 1, /* Authoritative Answer Flag */
  39. opcode: 4, /* Operation code */
  40. qr: 1; /* Query/Response Flag */
  41. uint8_t rcode: 4, /* Response Code */
  42. z: 3, /* Zero */
  43. ra: 1; /* Recursion Available */
  44. #else
  45. uint8_t qr: 1, /* Query/Response Flag */
  46. opcode: 4, /* Operation code */
  47. aa: 1, /* Authoritative Answer Flag */
  48. tc: 1, /* Truncation Flag */
  49. rd: 1; /* Recursion Desired */
  50. uint8_t ra: 1, /* Recursion Available */
  51. z: 3, /* Zero */
  52. rcode: 4; /* Response Code */
  53. #endif
  54. } dns_header_flags_t;
  55. typedef struct
  56. {
  57. uint16_t id;
  58. dns_header_flags_t flags;
  59. uint16_t n_record[4];
  60. } dns_header_t;
  61. typedef struct dns_answer
  62. {
  63. uint16_t name;
  64. uint16_t type;
  65. uint16_t Class;
  66. uint32_t ttl;
  67. uint16_t len;
  68. uint32_t addr;
  69. } dns_answer_t;
  70. #pragma pack(pop)
  71. typedef struct dns_query
  72. {
  73. char name[DNS_MAX_HOST_NAME_LEN];
  74. uint16_t type;
  75. uint16_t Class;
  76. } dns_query_t;
  77. static uint16_t get_uint16(const uint8_t *pnt)
  78. {
  79. uint16_t result;
  80. memcpy(&result, pnt, sizeof(result));
  81. return result;
  82. }
  83. static int parse_next_query(void *data, int size, dns_query_t *query)
  84. {
  85. int len;
  86. int lables;
  87. uint8_t *ptr;
  88. len = 0;
  89. lables = 0;
  90. ptr = (uint8_t *)data;
  91. while (true)
  92. {
  93. uint8_t lable_len;
  94. if (size <= 0) return -1;
  95. lable_len = *ptr++;
  96. size--;
  97. if (lable_len == 0) break;
  98. if (lables > 0)
  99. {
  100. if (len == DNS_MAX_HOST_NAME_LEN) return -2;
  101. query->name[len++] = '.';
  102. }
  103. if (lable_len > size) return -1;
  104. if (len + lable_len >= DNS_MAX_HOST_NAME_LEN) return -2;
  105. memcpy(&query->name[len], ptr, lable_len);
  106. len += lable_len;
  107. ptr += lable_len;
  108. size -= lable_len;
  109. lables++;
  110. }
  111. if (size < 4) return -1;
  112. query->name[len] = 0;
  113. query->type = get_uint16(ptr);
  114. ptr += 2;
  115. query->Class = get_uint16(ptr);
  116. ptr += 2;
  117. return ptr - (uint8_t *)data;
  118. }
  119. static void udp_recv_proc(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
  120. {
  121. int len;
  122. dns_header_t *header;
  123. static dns_query_t query;
  124. struct pbuf *out;
  125. ip4_addr_t host_addr;
  126. dns_answer_t *answer;
  127. (void)arg;
  128. if (p->len <= sizeof(dns_header_t)) goto error;
  129. header = (dns_header_t *)p->payload;
  130. if (header->flags.qr != 0) goto error;
  131. if (ntohs(header->n_record[0]) != 1) goto error;
  132. len = parse_next_query(header + 1, p->len - sizeof(dns_header_t), &query);
  133. if (len < 0) goto error;
  134. if (!query_proc(query.name, &host_addr)) goto error;
  135. len += sizeof(dns_header_t);
  136. out = pbuf_alloc(PBUF_TRANSPORT, len + 16, PBUF_POOL);
  137. if (out == NULL) goto error;
  138. memcpy(out->payload, p->payload, len);
  139. header = (dns_header_t *)out->payload;
  140. header->flags.qr = 1;
  141. header->n_record[1] = htons(1);
  142. answer = (struct dns_answer *)((uint8_t *)out->payload + len);
  143. answer->name = htons(0xC00C);
  144. answer->type = htons(1);
  145. answer->Class = htons(1);
  146. answer->ttl = htonl(32);
  147. answer->len = htons(4);
  148. answer->addr = host_addr.addr;
  149. udp_sendto(upcb, out, addr, port);
  150. pbuf_free(out);
  151. error:
  152. pbuf_free(p);
  153. }
  154. err_t dnserv_init(const ip_addr_t *bind, uint16_t port, dns_query_proc_t qp)
  155. {
  156. err_t err;
  157. udp_init();
  158. dnserv_free();
  159. pcb = udp_new();
  160. if (pcb == NULL)
  161. return ERR_MEM;
  162. err = udp_bind(pcb, bind, port);
  163. if (err != ERR_OK)
  164. {
  165. dnserv_free();
  166. return err;
  167. }
  168. udp_recv(pcb, udp_recv_proc, NULL);
  169. query_proc = qp;
  170. return ERR_OK;
  171. }
  172. void dnserv_free()
  173. {
  174. if (pcb == NULL) return;
  175. udp_remove(pcb);
  176. pcb = NULL;
  177. }