dns_server.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /**************************************************************************
  2. * File Name : dns_server.c
  3. * Author :
  4. * Version :
  5. * Date :
  6. * Description :
  7. *
  8. * Copyright (c) 2014 Winner Microelectronics Co., Ltd.
  9. * All rights reserved.
  10. *
  11. ***************************************************************************/
  12. #include <assert.h>
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include "tls_common.h"
  17. #include "wm_mem.h"
  18. #include "wm_debug.h"
  19. #include "lwip/stats.h"
  20. #include "lwip/sys.h"
  21. #include "lwip/api.h"
  22. #include "lwip/tcpip.h"
  23. #include "lwip/memp.h"
  24. #include "lwip/udp.h"
  25. #include "netif/ethernetif.h"
  26. #include "dns_server.h"
  27. #include "wm_wifi_oneshot.h"
  28. #if 0
  29. static DNS_SERVER DnsServer;
  30. static INT32S _DnsCompareName(INT8U * MyDns, INT8U * Query)
  31. {
  32. INT8U n;
  33. if((strlen((const char *)Query) - 1) > 32)
  34. {
  35. return 1;
  36. }
  37. do
  38. {
  39. n = *Query++;
  40. /* @see RFC 1035 - 4.1.4. Message compression. */
  41. if((n & 0xc0) == 0xc0)
  42. {
  43. /* Compressed name. */
  44. break;
  45. }
  46. else
  47. {
  48. /* Not compressed name. */
  49. while(n > 0)
  50. {
  51. if((*MyDns) != (*Query))
  52. {
  53. return 1;
  54. }
  55. ++Query;
  56. ++MyDns;
  57. --n;
  58. };
  59. ++MyDns;
  60. }
  61. } while (*Query != 0);
  62. return ((*(--MyDns) == 0) ? 0 : 1);
  63. }
  64. static void _DNSNameErrGenAndSend(ip_addr_t *Addr, INT16U Port, PDNS_QUERY pDnsQuery, INT8U * QueryName, INT16U TansactionId)
  65. {
  66. INT32U Len;
  67. INT8U * Body;
  68. PDNS_HEADER pDnsHeader;
  69. struct pbuf * pDnsBuf;
  70. INT8U * pDnsReply;
  71. Len = ((sizeof(DNS_HEADER) + (strlen((const char *)QueryName) + 1) + sizeof(DNS_QUERY) + 3) >> 2) << 2;
  72. pDnsReply = tls_mem_alloc(Len);
  73. if(pDnsReply == NULL)
  74. {
  75. return;
  76. }
  77. pDnsHeader = (PDNS_HEADER)pDnsReply;
  78. Body = (INT8U *)(pDnsHeader + 1);
  79. Len = 0;
  80. /* Header. */
  81. pDnsHeader->TansactionId = TansactionId;
  82. pDnsHeader->DnsFlag1 = DNS_FLAG1_RESPONSE;
  83. pDnsHeader->DnsFlag2 = DNS_FLAG2_ERR_NAME;
  84. pDnsHeader->Quentions = htons(1);
  85. pDnsHeader->AnswerRR = 0;
  86. pDnsHeader->AuthorityRR = 0;
  87. pDnsHeader->AdditionalRR = 0;
  88. Len += sizeof(DNS_HEADER);
  89. /* Querry. */
  90. MEMCPY(Body, QueryName, strlen((const char *)QueryName) + 1);
  91. Body += strlen((const char *)QueryName) + 1;
  92. Len += strlen((const char *)QueryName) + 1;
  93. MEMCPY(Body, pDnsQuery, sizeof(DNS_QUERY));
  94. Len += sizeof(DNS_QUERY);
  95. pDnsBuf = pbuf_alloc(PBUF_TRANSPORT, Len, PBUF_RAM);
  96. if(pDnsBuf == NULL)
  97. {
  98. tls_mem_free(pDnsReply);
  99. return;
  100. }
  101. pbuf_take(pDnsBuf, (const void *) pDnsReply, Len);
  102. /* Send to the client. */
  103. udp_sendto(DnsServer.Socket, pDnsBuf, Addr, Port);
  104. pbuf_free(pDnsBuf);
  105. tls_mem_free(pDnsReply);
  106. }
  107. static void _DNSAnswerGenAndSend(ip_addr_t *Addr, INT16U Port, PDNS_QUERY pDnsQuery, INT8U * QueryName, INT16U TansactionId)
  108. {
  109. INT32U Len;
  110. INT8U * Body;
  111. INT32U ServerIpAddr;
  112. PDNS_HEADER pDnsHeader;
  113. DNS_ANSWER DnsAnswer;
  114. struct pbuf * pDnsBuf;
  115. INT8U * pDnsReply;
  116. INT16U Tmp;
  117. Len = ((sizeof(DNS_HEADER) + (strlen((const char *)QueryName) + 1) + sizeof(DNS_QUERY) + 2 + sizeof(DNS_ANSWER) + 2 + 4 + 3) >> 2) << 2;
  118. pDnsReply = tls_mem_alloc(Len);
  119. if(pDnsReply == NULL)
  120. {
  121. return;
  122. }
  123. pDnsHeader = (PDNS_HEADER)pDnsReply;
  124. Body = (INT8U *)(pDnsHeader + 1);
  125. Len = 0;
  126. /* Header. */
  127. pDnsHeader->TansactionId = TansactionId;
  128. pDnsHeader->DnsFlag1 = DNS_FLAG1_RESPONSE;
  129. pDnsHeader->DnsFlag2 = DNS_FLAG2_ERR_NONE;
  130. pDnsHeader->Quentions = htons(1);
  131. pDnsHeader->AnswerRR = htons(1);
  132. pDnsHeader->AuthorityRR = 0;
  133. pDnsHeader->AdditionalRR = 0;
  134. Len += sizeof(DNS_HEADER);
  135. /* Querry. */
  136. MEMCPY(Body, QueryName, strlen((const char *)QueryName) + 1);
  137. Body += strlen((const char *)QueryName) + 1;
  138. Len += strlen((const char *)QueryName) + 1;
  139. MEMCPY(Body, pDnsQuery, sizeof(DNS_QUERY));
  140. Body += sizeof(DNS_QUERY);
  141. Len += sizeof(DNS_QUERY);
  142. /* NAME: provided as offset to first occurence in response. */
  143. Tmp = DNS_NAME_OFFSET | sizeof(DNS_HEADER);
  144. Tmp = htons(Tmp);
  145. MEMCPY(Body, &Tmp, sizeof(INT16U));
  146. Body += sizeof(INT16U);
  147. Len += sizeof(INT16U);
  148. /* Answer. */
  149. DnsAnswer.Type = htons(DNS_RRTYPE_A);
  150. DnsAnswer.Class = htons(DNS_RRCLASS_IN);
  151. DnsAnswer.Ttl = htonl(DNS_DEFAULT_TTL);
  152. MEMCPY(Body, &DnsAnswer, sizeof(DNS_ANSWER));
  153. Body += sizeof(DNS_ANSWER);
  154. Len += sizeof(DNS_ANSWER);
  155. /* Length. */
  156. Tmp = htons(4);
  157. MEMCPY(Body, &Tmp, sizeof(INT16U));
  158. Body += sizeof(INT16U);
  159. Len += sizeof(INT16U);
  160. /* IP Address. */
  161. ServerIpAddr = ip_addr_get_ip4_u32(&DnsServer.HostIp);
  162. MEMCPY(Body, &ServerIpAddr, 4);
  163. Body += 4;
  164. Len += 4;
  165. pDnsBuf = pbuf_alloc(PBUF_TRANSPORT, Len, PBUF_RAM);
  166. if(pDnsBuf == NULL)
  167. {
  168. tls_mem_free(pDnsReply);
  169. return;
  170. }
  171. pbuf_take(pDnsBuf, (const void *) pDnsReply, Len);
  172. /* Send to the client. */
  173. udp_sendto(DnsServer.Socket, pDnsBuf, Addr, Port);
  174. pbuf_free(pDnsBuf);
  175. tls_mem_free(pDnsReply);
  176. }
  177. /* DNSS_RecvCb */
  178. /*-------------------------------------------------------------------------
  179. Description:
  180. When an incoming DNS message is to me, this function process it and trigger the state machine.
  181. Arguments:
  182. Arg: Pointer to the user supplied argument.
  183. Pcb: Pointer to the udp_pcb which received data.
  184. P: Pointer to the packet buffer that was received.
  185. Addr: The remote IP address from which the packet was received.
  186. Port: The remote port from which the packet was received .
  187. Return Value:
  188. None.
  189. Note:
  190. -------------------------------------------------------------------------*/
  191. void DNSS_RecvCb(void *Arg, struct udp_pcb *Pcb, struct pbuf *P, ip_addr_t *Addr, INT16U Port)
  192. {
  193. PDNS_HEADER pDnsHeader;
  194. DNS_QUERY DnsQuery;
  195. //INT16U nQuestions, nAnswers;
  196. INT8U * pDnsName;
  197. INT8U * pDnsMsg;
  198. do
  199. {
  200. pDnsMsg = tls_mem_alloc(P->tot_len);
  201. if(pDnsMsg == NULL)
  202. {
  203. break;
  204. }
  205. pbuf_copy_partial(P, pDnsMsg, P->tot_len, 0);
  206. pDnsHeader = (PDNS_HEADER)pDnsMsg;
  207. /* Get the quention number and answer number. */
  208. //nQuestions = ntohs(pDnsHeader->Quentions);
  209. //nAnswers = ntohs(pDnsHeader->AnswerRR);
  210. /* Filter out the response frame and the unstandard query frame. */
  211. if((pDnsHeader->DnsFlag1 & DNS_FLAG1_RESPONSE) || ((pDnsHeader->DnsFlag1 & (0xf << 3)) != DNS_FLAG1_OPCODE_STANDARD))
  212. {
  213. break;
  214. }
  215. /* Locate the dns name. */
  216. pDnsName = (INT8U *)(pDnsHeader + 1);
  217. /* Get the query class and type. */
  218. MEMCPY(&DnsQuery, (pDnsName + strlen((const char *)pDnsName) + 1), sizeof(DnsQuery));
  219. /* Check the query class and type. */
  220. if((DnsQuery.Class != htons(DNS_RRCLASS_IN)) && (DnsQuery.Type != htons(DNS_RRTYPE_A)))
  221. {
  222. break;
  223. }
  224. if ((_DnsCompareName(DnsServer.DnsName, pDnsName) != 0) &&
  225. (3 != tls_wifi_get_oneshot_flag()))
  226. {
  227. /* Not my dns name, so notify the client name error. */
  228. #if TLS_CONFIG_AP
  229. struct netif *netif = tls_get_netif();
  230. if (!netif_is_up(netif))
  231. #endif
  232. _DNSNameErrGenAndSend(Addr, Port, &DnsQuery, pDnsName, pDnsHeader->TansactionId);
  233. }
  234. else
  235. {
  236. /* My dns name, so send the answer to the client. */
  237. _DNSAnswerGenAndSend(Addr, Port, &DnsQuery, pDnsName, pDnsHeader->TansactionId);
  238. }
  239. }while(0);
  240. if(pDnsMsg)
  241. {
  242. tls_mem_free(pDnsMsg);
  243. }
  244. pbuf_free(P);
  245. }
  246. /* DNSS_Config */
  247. /*-------------------------------------------------------------------------
  248. Description:
  249. This function is used to updata the server's dns name.
  250. Arguments:
  251. DnsName : Pointer the server's dns name.
  252. Return Value:
  253. The DNS Server error code:
  254. DNSS_ERR_SUCCESS - No error
  255. DNSS_ERR_PARAM - Input parameter error
  256. Note:
  257. The length of the DNS name must be less than 32.
  258. -------------------------------------------------------------------------*/
  259. INT8S DNSS_Config(INT8U * DnsName)
  260. {
  261. if((DnsName == NULL) || (strlen((const char *)DnsName) > 32))
  262. {
  263. /* The length of the DNS name must be less than 32. */
  264. return DNSS_ERR_PARAM;
  265. }
  266. memset(DnsServer.DnsName, 0, 32);
  267. MEMCPY(DnsServer.DnsName, DnsName, strlen((const char *)DnsName));
  268. return DNSS_ERR_SUCCESS;
  269. }
  270. /* DNSS_Start */
  271. /*-------------------------------------------------------------------------
  272. Description:
  273. This function is used to start the dns server's service.
  274. Arguments:
  275. DnsName : Specify the server's dns name
  276. Netif: Pointer to the Lwip network interface.
  277. Return Value:
  278. The DNS Server error code:
  279. DNSS_ERR_SUCCESS - No error
  280. DNSS_ERR_PARAM - Input parameter error
  281. DNSS_ERR_MEM - Out of memory
  282. DNSS_ERR_LINKDOWN - The NI is inactive
  283. Note:
  284. The dns server must be started after the network interface was actived.
  285. -------------------------------------------------------------------------*/
  286. INT8S DNSS_Start(struct netif *Netif, INT8U * DnsName)
  287. {
  288. if((Netif == NULL) || (strlen((const char *)DnsName) > 32))
  289. {
  290. return DNSS_ERR_PARAM;
  291. }
  292. if(netif_is_up(Netif) == 0)
  293. {
  294. return DNSS_ERR_LINKDOWN;
  295. }
  296. memset(&DnsServer, 0, sizeof(DnsServer));
  297. MEMCPY(DnsServer.DnsName, DnsName, strlen((const char *)DnsName));
  298. ip_addr_set(&DnsServer.HostIp, &Netif->ip_addr);
  299. DnsServer.Socket = udp_new();
  300. if(DnsServer.Socket == NULL)
  301. {
  302. return DNSS_ERR_MEM;
  303. }
  304. /* Set up local and remote port for the pcb. */
  305. udp_bind(DnsServer.Socket, IP_ADDR_ANY, DNS_SERVER_PORT);
  306. /* Set up the recv callback and argument. */
  307. udp_recv(DnsServer.Socket, (udp_recv_fn)DNSS_RecvCb, Netif);
  308. return DNSS_ERR_SUCCESS;
  309. }
  310. /* DNSS_Stop */
  311. /*-------------------------------------------------------------------------
  312. Description:
  313. This function is used to stop the dns server's service.
  314. Arguments:
  315. None.
  316. Return Value:
  317. None.
  318. Note:
  319. -------------------------------------------------------------------------*/
  320. void DNSS_Stop(void)
  321. {
  322. if(DnsServer.Socket) udp_remove(DnsServer.Socket);
  323. }
  324. #endif