dnserver.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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;
  31. dns_query_proc_t query_proc;
  32. #pragma pack(push, 1)
  33. typedef struct {
  34. #if BYTE_ORDER == LITTLE_ENDIAN
  35. uint8_t rd : 1, /* Recursion Desired */
  36. tc : 1, /* Truncation Flag */
  37. aa : 1, /* Authoritative Answer Flag */
  38. opcode : 4, /* Operation code */
  39. qr : 1; /* Query/Response Flag */
  40. uint8_t rcode : 4, /* Response Code */
  41. z : 3, /* Zero */
  42. ra : 1; /* Recursion Available */
  43. #else
  44. uint8_t qr : 1, /* Query/Response Flag */
  45. opcode : 4, /* Operation code */
  46. aa : 1, /* Authoritative Answer Flag */
  47. tc : 1, /* Truncation Flag */
  48. rd : 1; /* Recursion Desired */
  49. uint8_t ra : 1, /* Recursion Available */
  50. z : 3, /* Zero */
  51. rcode : 4; /* Response Code */
  52. #endif
  53. } dns_header_flags_t;
  54. typedef struct {
  55. uint16_t id;
  56. dns_header_flags_t flags;
  57. uint16_t n_record[4];
  58. } dns_header_t;
  59. typedef struct dns_answer {
  60. uint16_t name;
  61. uint16_t type;
  62. uint16_t Class;
  63. uint32_t ttl;
  64. uint16_t len;
  65. uint32_t addr;
  66. } dns_answer_t;
  67. #pragma pack(pop)
  68. typedef struct dns_query {
  69. char name[DNS_MAX_HOST_NAME_LEN];
  70. uint16_t type;
  71. uint16_t Class;
  72. } dns_query_t;
  73. static int parse_next_query(void *data, int size, dns_query_t *query)
  74. {
  75. int len;
  76. int lables;
  77. uint8_t *ptr;
  78. len = 0;
  79. lables = 0;
  80. ptr = (uint8_t *)data;
  81. while (true) {
  82. uint8_t lable_len;
  83. if (size <= 0)
  84. return -1;
  85. lable_len = *ptr++;
  86. size--;
  87. if (lable_len == 0)
  88. break;
  89. if (lables > 0) {
  90. if (len == DNS_MAX_HOST_NAME_LEN)
  91. return -2;
  92. query->name[len++] = '.';
  93. }
  94. if (lable_len > size)
  95. return -1;
  96. if (len + lable_len >= DNS_MAX_HOST_NAME_LEN)
  97. return -2;
  98. memcpy(&query->name[len], ptr, lable_len);
  99. len += lable_len;
  100. ptr += lable_len;
  101. size -= lable_len;
  102. lables++;
  103. }
  104. if (size < 4)
  105. return -1;
  106. query->name[len] = 0;
  107. query->type = *(uint16_t *)ptr;
  108. ptr += 2;
  109. query->Class = *(uint16_t *)ptr;
  110. ptr += 2;
  111. return ptr - (uint8_t *)data;
  112. }
  113. static void udp_recv_proc(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
  114. {
  115. (void)arg;
  116. int len;
  117. dns_header_t *header;
  118. static dns_query_t query;
  119. struct pbuf *out;
  120. ip_addr_t host_addr;
  121. dns_answer_t *answer;
  122. if (p->len <= sizeof(dns_header_t))
  123. goto error;
  124. header = (dns_header_t *)p->payload;
  125. if (header->flags.qr != 0)
  126. goto error;
  127. if (ntohs(header->n_record[0]) != 1)
  128. goto error;
  129. len = parse_next_query(header + 1, p->len - sizeof(dns_header_t), &query);
  130. if (len < 0)
  131. goto error;
  132. if (!query_proc(query.name, &host_addr))
  133. goto error;
  134. len += sizeof(dns_header_t);
  135. out = pbuf_alloc(PBUF_TRANSPORT, len + 16, PBUF_POOL);
  136. if (out == NULL)
  137. 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. dnserv_free();
  165. return err;
  166. }
  167. udp_recv(pcb, udp_recv_proc, NULL);
  168. query_proc = qp;
  169. return ERR_OK;
  170. }
  171. void dnserv_free(void)
  172. {
  173. if (pcb == NULL)
  174. return;
  175. udp_remove(pcb);
  176. pcb = NULL;
  177. }