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