dhcpserver.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. //#include "esp_common.h"
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include "lwip/inet.h"
  17. #include "lwip/err.h"
  18. #include "lwip/pbuf.h"
  19. #include "lwip/udp.h"
  20. #include "lwip/mem.h"
  21. #include "lwip/ip_addr.h"
  22. #include "esp_netif.h"
  23. #include "dhcpserver/dhcpserver.h"
  24. #include "dhcpserver/dhcpserver_options.h"
  25. #if ESP_DHCP
  26. #define BOOTP_BROADCAST 0x8000
  27. #define DHCP_REQUEST 1
  28. #define DHCP_REPLY 2
  29. #define DHCP_HTYPE_ETHERNET 1
  30. #define DHCP_HLEN_ETHERNET 6
  31. #define DHCP_MSG_LEN 236
  32. #define DHCPS_SERVER_PORT 67
  33. #define DHCPS_CLIENT_PORT 68
  34. #define DHCPDISCOVER 1
  35. #define DHCPOFFER 2
  36. #define DHCPREQUEST 3
  37. #define DHCPDECLINE 4
  38. #define DHCPACK 5
  39. #define DHCPNAK 6
  40. #define DHCPRELEASE 7
  41. #define DHCP_OPTION_SUBNET_MASK 1
  42. #define DHCP_OPTION_ROUTER 3
  43. #define DHCP_OPTION_DNS_SERVER 6
  44. #define DHCP_OPTION_REQ_IPADDR 50
  45. #define DHCP_OPTION_LEASE_TIME 51
  46. #define DHCP_OPTION_MSG_TYPE 53
  47. #define DHCP_OPTION_SERVER_ID 54
  48. #define DHCP_OPTION_INTERFACE_MTU 26
  49. #define DHCP_OPTION_PERFORM_ROUTER_DISCOVERY 31
  50. #define DHCP_OPTION_BROADCAST_ADDRESS 28
  51. #define DHCP_OPTION_REQ_LIST 55
  52. #define DHCP_OPTION_END 255
  53. //#define USE_CLASS_B_NET 1
  54. #define DHCPS_DEBUG 0
  55. #define DHCPS_LOG printf
  56. #define MAX_STATION_NUM CONFIG_LWIP_DHCPS_MAX_STATION_NUM
  57. #define DHCPS_STATE_OFFER 1
  58. #define DHCPS_STATE_DECLINE 2
  59. #define DHCPS_STATE_ACK 3
  60. #define DHCPS_STATE_NAK 4
  61. #define DHCPS_STATE_IDLE 5
  62. #define DHCPS_STATE_RELEASE 6
  63. typedef struct _list_node {
  64. void *pnode;
  65. struct _list_node *pnext;
  66. } list_node;
  67. ////////////////////////////////////////////////////////////////////////////////////
  68. static const u32_t magic_cookie = 0x63538263;
  69. static struct netif *dhcps_netif = NULL;
  70. static ip4_addr_t broadcast_dhcps;
  71. static ip4_addr_t server_address;
  72. static ip4_addr_t dns_server = {0};
  73. static ip4_addr_t client_address; //added
  74. static ip4_addr_t client_address_plus;
  75. static ip4_addr_t s_dhcps_mask = {
  76. #ifdef USE_CLASS_B_NET
  77. .addr = PP_HTONL(LWIP_MAKEU32(255, 240, 0, 0))
  78. #else
  79. .addr = PP_HTONL(LWIP_MAKEU32(255, 255, 255, 0))
  80. #endif
  81. };
  82. static list_node *plist = NULL;
  83. static bool renew = false;
  84. static dhcps_lease_t dhcps_poll;
  85. static dhcps_time_t dhcps_lease_time = DHCPS_LEASE_TIME_DEF; //minute
  86. static dhcps_offer_t dhcps_offer = 0xFF;
  87. static dhcps_offer_t dhcps_dns = 0x00;
  88. static dhcps_cb_t dhcps_cb;
  89. /******************************************************************************
  90. * FunctionName : dhcps_option_info
  91. * Description : get the DHCP message option info
  92. * Parameters : op_id -- DHCP message option id
  93. * opt_len -- DHCP message option length
  94. * Returns : DHCP message option addr
  95. *******************************************************************************/
  96. void *dhcps_option_info(u8_t op_id, u32_t opt_len)
  97. {
  98. void *option_arg = NULL;
  99. switch (op_id) {
  100. case IP_ADDRESS_LEASE_TIME:
  101. if (opt_len == sizeof(dhcps_time_t)) {
  102. option_arg = &dhcps_lease_time;
  103. }
  104. break;
  105. case REQUESTED_IP_ADDRESS:
  106. if (opt_len == sizeof(dhcps_lease_t)) {
  107. option_arg = &dhcps_poll;
  108. }
  109. break;
  110. case ROUTER_SOLICITATION_ADDRESS:
  111. if (opt_len == sizeof(dhcps_offer_t)) {
  112. option_arg = &dhcps_offer;
  113. }
  114. break;
  115. case DOMAIN_NAME_SERVER:
  116. if (opt_len == sizeof(dhcps_offer_t)) {
  117. option_arg = &dhcps_dns;
  118. }
  119. break;
  120. case SUBNET_MASK:
  121. if (opt_len == sizeof(s_dhcps_mask)) {
  122. option_arg = &s_dhcps_mask;
  123. }
  124. break;
  125. default:
  126. break;
  127. }
  128. return option_arg;
  129. }
  130. /******************************************************************************
  131. * FunctionName : dhcps_set_option_info
  132. * Description : set the DHCP message option info
  133. * Parameters : op_id -- DHCP message option id
  134. * opt_info -- DHCP message option info
  135. * opt_len -- DHCP message option length
  136. * Returns : none
  137. *******************************************************************************/
  138. void dhcps_set_option_info(u8_t op_id, void *opt_info, u32_t opt_len)
  139. {
  140. if (opt_info == NULL) {
  141. return;
  142. }
  143. switch (op_id) {
  144. case IP_ADDRESS_LEASE_TIME:
  145. if (opt_len == sizeof(dhcps_time_t)) {
  146. dhcps_lease_time = *(dhcps_time_t *)opt_info;
  147. }
  148. break;
  149. case REQUESTED_IP_ADDRESS:
  150. if (opt_len == sizeof(dhcps_lease_t)) {
  151. dhcps_poll = *(dhcps_lease_t *)opt_info;
  152. }
  153. break;
  154. case ROUTER_SOLICITATION_ADDRESS:
  155. if (opt_len == sizeof(dhcps_offer_t)) {
  156. dhcps_offer = *(dhcps_offer_t *)opt_info;
  157. }
  158. break;
  159. case DOMAIN_NAME_SERVER:
  160. if (opt_len == sizeof(dhcps_offer_t)) {
  161. dhcps_dns = *(dhcps_offer_t *)opt_info;
  162. }
  163. break;
  164. case SUBNET_MASK:
  165. if (opt_len == sizeof(s_dhcps_mask)) {
  166. s_dhcps_mask = *(ip4_addr_t *)opt_info;
  167. }
  168. default:
  169. break;
  170. }
  171. return;
  172. }
  173. /******************************************************************************
  174. * FunctionName : node_insert_to_list
  175. * Description : insert the node to the list
  176. * Parameters : phead -- the head node of the list
  177. * pinsert -- the insert node of the list
  178. * Returns : none
  179. *******************************************************************************/
  180. static void node_insert_to_list(list_node **phead, list_node *pinsert)
  181. {
  182. list_node *plist = NULL;
  183. struct dhcps_pool *pdhcps_pool = NULL;
  184. struct dhcps_pool *pdhcps_node = NULL;
  185. if (*phead == NULL) {
  186. *phead = pinsert;
  187. } else {
  188. plist = *phead;
  189. pdhcps_node = pinsert->pnode;
  190. pdhcps_pool = plist->pnode;
  191. if (pdhcps_node->ip.addr < pdhcps_pool->ip.addr) {
  192. pinsert->pnext = plist;
  193. *phead = pinsert;
  194. } else {
  195. while (plist->pnext != NULL) {
  196. pdhcps_pool = plist->pnext->pnode;
  197. if (pdhcps_node->ip.addr < pdhcps_pool->ip.addr) {
  198. pinsert->pnext = plist->pnext;
  199. plist->pnext = pinsert;
  200. break;
  201. }
  202. plist = plist->pnext;
  203. }
  204. if (plist->pnext == NULL) {
  205. plist->pnext = pinsert;
  206. }
  207. }
  208. }
  209. // pinsert->pnext = NULL;
  210. }
  211. /******************************************************************************
  212. * FunctionName : node_delete_from_list
  213. * Description : remove the node from list
  214. * Parameters : phead -- the head node of the list
  215. * pdelete -- the remove node of the list
  216. * Returns : none
  217. *******************************************************************************/
  218. void node_remove_from_list(list_node **phead, list_node *pdelete)
  219. {
  220. list_node *plist = NULL;
  221. plist = *phead;
  222. if (plist == NULL) {
  223. *phead = NULL;
  224. } else {
  225. if (plist == pdelete) {
  226. // Note: Ignoring the "use after free" warnings, as it could only happen
  227. // if the linked list contains loops
  228. *phead = plist->pnext; // NOLINT(clang-analyzer-unix.Malloc)
  229. pdelete->pnext = NULL;
  230. } else {
  231. while (plist != NULL) {
  232. if (plist->pnext == pdelete) { // NOLINT(clang-analyzer-unix.Malloc)
  233. plist->pnext = pdelete->pnext;
  234. pdelete->pnext = NULL;
  235. }
  236. plist = plist->pnext;
  237. }
  238. }
  239. }
  240. }
  241. /******************************************************************************
  242. * FunctionName : add_msg_type
  243. * Description : add TYPE option of DHCP message
  244. * Parameters : optptr -- the addr of DHCP message option
  245. * Returns : the addr of DHCP message option
  246. *******************************************************************************/
  247. static u8_t *add_msg_type(u8_t *optptr, u8_t type)
  248. {
  249. *optptr++ = DHCP_OPTION_MSG_TYPE;
  250. *optptr++ = 1;
  251. *optptr++ = type;
  252. return optptr;
  253. }
  254. /******************************************************************************
  255. * FunctionName : add_offer_options
  256. * Description : add OFFER option of DHCP message
  257. * Parameters : optptr -- the addr of DHCP message option
  258. * Returns : the addr of DHCP message option
  259. *******************************************************************************/
  260. static u8_t *add_offer_options(u8_t *optptr)
  261. {
  262. ip4_addr_t ipadd;
  263. ipadd.addr = *((u32_t *) &server_address);
  264. *optptr++ = DHCP_OPTION_SUBNET_MASK;
  265. *optptr++ = 4;
  266. *optptr++ = ip4_addr1(&s_dhcps_mask);
  267. *optptr++ = ip4_addr2(&s_dhcps_mask);
  268. *optptr++ = ip4_addr3(&s_dhcps_mask);
  269. *optptr++ = ip4_addr4(&s_dhcps_mask);
  270. *optptr++ = DHCP_OPTION_LEASE_TIME;
  271. *optptr++ = 4;
  272. *optptr++ = ((dhcps_lease_time * DHCPS_LEASE_UNIT) >> 24) & 0xFF;
  273. *optptr++ = ((dhcps_lease_time * DHCPS_LEASE_UNIT) >> 16) & 0xFF;
  274. *optptr++ = ((dhcps_lease_time * DHCPS_LEASE_UNIT) >> 8) & 0xFF;
  275. *optptr++ = ((dhcps_lease_time * DHCPS_LEASE_UNIT) >> 0) & 0xFF;
  276. *optptr++ = DHCP_OPTION_SERVER_ID;
  277. *optptr++ = 4;
  278. *optptr++ = ip4_addr1(&ipadd);
  279. *optptr++ = ip4_addr2(&ipadd);
  280. *optptr++ = ip4_addr3(&ipadd);
  281. *optptr++ = ip4_addr4(&ipadd);
  282. if (dhcps_router_enabled(dhcps_offer)) {
  283. esp_netif_ip_info_t if_ip;
  284. memset(&if_ip , 0x00, sizeof(esp_netif_ip_info_t));
  285. esp_netif_get_ip_info(dhcps_netif->state, &if_ip);
  286. ip4_addr_t* gw_ip = (ip4_addr_t*)&if_ip.gw;
  287. if (!ip4_addr_isany_val(*gw_ip)) {
  288. *optptr++ = DHCP_OPTION_ROUTER;
  289. *optptr++ = 4;
  290. *optptr++ = ip4_addr1(gw_ip);
  291. *optptr++ = ip4_addr2(gw_ip);
  292. *optptr++ = ip4_addr3(gw_ip);
  293. *optptr++ = ip4_addr4(gw_ip);
  294. }
  295. }
  296. *optptr++ = DHCP_OPTION_DNS_SERVER;
  297. *optptr++ = 4;
  298. if (dhcps_dns_enabled(dhcps_dns)) {
  299. *optptr++ = ip4_addr1(&dns_server);
  300. *optptr++ = ip4_addr2(&dns_server);
  301. *optptr++ = ip4_addr3(&dns_server);
  302. *optptr++ = ip4_addr4(&dns_server);
  303. }else {
  304. *optptr++ = ip4_addr1(&ipadd);
  305. *optptr++ = ip4_addr2(&ipadd);
  306. *optptr++ = ip4_addr3(&ipadd);
  307. *optptr++ = ip4_addr4(&ipadd);
  308. }
  309. ip4_addr_t broadcast_addr = { .addr = (ipadd.addr & s_dhcps_mask.addr) | ~s_dhcps_mask.addr };
  310. *optptr++ = DHCP_OPTION_BROADCAST_ADDRESS;
  311. *optptr++ = 4;
  312. *optptr++ = ip4_addr1(&broadcast_addr);
  313. *optptr++ = ip4_addr2(&broadcast_addr);
  314. *optptr++ = ip4_addr3(&broadcast_addr);
  315. *optptr++ = ip4_addr4(&broadcast_addr);
  316. *optptr++ = DHCP_OPTION_INTERFACE_MTU;
  317. *optptr++ = 2;
  318. *optptr++ = 0x05;
  319. *optptr++ = 0xdc;
  320. *optptr++ = DHCP_OPTION_PERFORM_ROUTER_DISCOVERY;
  321. *optptr++ = 1;
  322. *optptr++ = 0x00;
  323. *optptr++ = 43;
  324. *optptr++ = 6;
  325. *optptr++ = 0x01;
  326. *optptr++ = 4;
  327. *optptr++ = 0x00;
  328. *optptr++ = 0x00;
  329. *optptr++ = 0x00;
  330. *optptr++ = 0x02;
  331. return optptr;
  332. }
  333. /******************************************************************************
  334. * FunctionName : add_end
  335. * Description : add end option of DHCP message
  336. * Parameters : optptr -- the addr of DHCP message option
  337. * Returns : the addr of DHCP message option
  338. *******************************************************************************/
  339. static u8_t *add_end(u8_t *optptr)
  340. {
  341. *optptr++ = DHCP_OPTION_END;
  342. return optptr;
  343. }
  344. /******************************************************************************
  345. * FunctionName : create_msg
  346. * Description : create response message
  347. * Parameters : m -- DHCP message info
  348. * Returns : none
  349. *******************************************************************************/
  350. static void create_msg(struct dhcps_msg *m)
  351. {
  352. ip4_addr_t client;
  353. client.addr = *((uint32_t *) &client_address);
  354. m->op = DHCP_REPLY;
  355. m->htype = DHCP_HTYPE_ETHERNET;
  356. m->hlen = 6;
  357. m->hops = 0;
  358. // os_memcpy((char *) xid, (char *) m->xid, sizeof(m->xid));
  359. m->secs = 0;
  360. m->flags = htons(BOOTP_BROADCAST);
  361. memcpy((char *) m->yiaddr, (char *) &client.addr, sizeof(m->yiaddr));
  362. memset((char *) m->ciaddr, 0, sizeof(m->ciaddr));
  363. memset((char *) m->siaddr, 0, sizeof(m->siaddr));
  364. memset((char *) m->giaddr, 0, sizeof(m->giaddr));
  365. memset((char *) m->sname, 0, sizeof(m->sname));
  366. memset((char *) m->file, 0, sizeof(m->file));
  367. memset((char *) m->options, 0, sizeof(m->options));
  368. u32_t magic_cookie_temp = magic_cookie;
  369. memcpy((char *) m->options, &magic_cookie_temp, sizeof(magic_cookie_temp));
  370. }
  371. struct pbuf * dhcps_pbuf_alloc(u16_t len)
  372. {
  373. u16_t mlen = sizeof(struct dhcps_msg);
  374. if (len > mlen) {
  375. #if DHCPS_DEBUG
  376. DHCPS_LOG("dhcps: len=%d mlen=%d", len, mlen);
  377. #endif
  378. mlen = len;
  379. }
  380. return pbuf_alloc(PBUF_TRANSPORT, mlen, PBUF_RAM);
  381. }
  382. /******************************************************************************
  383. * FunctionName : send_offer
  384. * Description : DHCP message OFFER Response
  385. * Parameters : m -- DHCP message info
  386. * Returns : none
  387. *******************************************************************************/
  388. static void send_offer(struct dhcps_msg *m, u16_t len)
  389. {
  390. u8_t *end;
  391. struct pbuf *p, *q;
  392. u8_t *data;
  393. u16_t cnt = 0;
  394. u16_t i;
  395. #if DHCPS_DEBUG
  396. err_t SendOffer_err_t;
  397. #endif
  398. create_msg(m);
  399. end = add_msg_type(&m->options[4], DHCPOFFER);
  400. end = add_offer_options(end);
  401. end = add_end(end);
  402. p = dhcps_pbuf_alloc(len);
  403. #if DHCPS_DEBUG
  404. DHCPS_LOG("udhcp: send_offer>>p->ref = %d\n", p->ref);
  405. #endif
  406. if (p != NULL) {
  407. #if DHCPS_DEBUG
  408. DHCPS_LOG("dhcps: send_offer>>pbuf_alloc succeed\n");
  409. DHCPS_LOG("dhcps: send_offer>>p->tot_len = %d\n", p->tot_len);
  410. DHCPS_LOG("dhcps: send_offer>>p->len = %d\n", p->len);
  411. #endif
  412. q = p;
  413. while (q != NULL) {
  414. data = (u8_t *)q->payload;
  415. for (i = 0; i < q->len; i++) {
  416. data[i] = ((u8_t *) m)[cnt++];
  417. #if DHCPS_DEBUG
  418. DHCPS_LOG("%02x ", data[i]);
  419. if ((i + 1) % 16 == 0) {
  420. DHCPS_LOG("\n");
  421. }
  422. #endif
  423. }
  424. q = q->next;
  425. }
  426. } else {
  427. #if DHCPS_DEBUG
  428. DHCPS_LOG("dhcps: send_offer>>pbuf_alloc failed\n");
  429. #endif
  430. return;
  431. }
  432. ip_addr_t ip_temp = IPADDR4_INIT(0x0);
  433. ip4_addr_set(ip_2_ip4(&ip_temp), &broadcast_dhcps);
  434. struct udp_pcb *pcb_dhcps = dhcps_netif->dhcps_pcb;
  435. #if DHCPS_DEBUG
  436. SendOffer_err_t = udp_sendto(pcb_dhcps, p, &ip_temp, DHCPS_CLIENT_PORT);
  437. DHCPS_LOG("dhcps: send_offer>>udp_sendto result %x\n", SendOffer_err_t);
  438. #else
  439. udp_sendto(pcb_dhcps, p, &ip_temp, DHCPS_CLIENT_PORT);
  440. #endif
  441. if (p->ref != 0) {
  442. #if DHCPS_DEBUG
  443. DHCPS_LOG("udhcp: send_offer>>free pbuf\n");
  444. #endif
  445. pbuf_free(p);
  446. }
  447. }
  448. /******************************************************************************
  449. * FunctionName : send_nak
  450. * Description : DHCP message NACK Response
  451. * Parameters : m -- DHCP message info
  452. * Returns : none
  453. *******************************************************************************/
  454. static void send_nak(struct dhcps_msg *m, u16_t len)
  455. {
  456. u8_t *end;
  457. struct pbuf *p, *q;
  458. u8_t *data;
  459. u16_t cnt = 0;
  460. u16_t i;
  461. #if DHCPS_DEBUG
  462. err_t SendNak_err_t;
  463. #endif
  464. create_msg(m);
  465. end = add_msg_type(&m->options[4], DHCPNAK);
  466. end = add_end(end);
  467. p = dhcps_pbuf_alloc(len);
  468. #if DHCPS_DEBUG
  469. DHCPS_LOG("udhcp: send_nak>>p->ref = %d\n", p->ref);
  470. #endif
  471. if (p != NULL) {
  472. #if DHCPS_DEBUG
  473. DHCPS_LOG("dhcps: send_nak>>pbuf_alloc succeed\n");
  474. DHCPS_LOG("dhcps: send_nak>>p->tot_len = %d\n", p->tot_len);
  475. DHCPS_LOG("dhcps: send_nak>>p->len = %d\n", p->len);
  476. #endif
  477. q = p;
  478. while (q != NULL) {
  479. data = (u8_t *)q->payload;
  480. for (i = 0; i < q->len; i++) {
  481. data[i] = ((u8_t *) m)[cnt++];
  482. #if DHCPS_DEBUG
  483. DHCPS_LOG("%02x ", data[i]);
  484. if ((i + 1) % 16 == 0) {
  485. DHCPS_LOG("\n");
  486. }
  487. #endif
  488. }
  489. q = q->next;
  490. }
  491. } else {
  492. #if DHCPS_DEBUG
  493. DHCPS_LOG("dhcps: send_nak>>pbuf_alloc failed\n");
  494. #endif
  495. return;
  496. }
  497. ip_addr_t ip_temp = IPADDR4_INIT(0x0);
  498. ip4_addr_set(ip_2_ip4(&ip_temp), &broadcast_dhcps);
  499. struct udp_pcb *pcb_dhcps = dhcps_netif->dhcps_pcb;
  500. #if DHCPS_DEBUG
  501. SendNak_err_t = udp_sendto(pcb_dhcps, p, &ip_temp, DHCPS_CLIENT_PORT);
  502. DHCPS_LOG("dhcps: send_nak>>udp_sendto result %x\n", SendNak_err_t);
  503. #else
  504. udp_sendto(pcb_dhcps, p, &ip_temp, DHCPS_CLIENT_PORT);
  505. #endif
  506. if (p->ref != 0) {
  507. #if DHCPS_DEBUG
  508. DHCPS_LOG("udhcp: send_nak>>free pbuf\n");
  509. #endif
  510. pbuf_free(p);
  511. }
  512. }
  513. /******************************************************************************
  514. * FunctionName : send_ack
  515. * Description : DHCP message ACK Response
  516. * Parameters : m -- DHCP message info
  517. * Returns : none
  518. *******************************************************************************/
  519. static void send_ack(struct dhcps_msg *m, u16_t len)
  520. {
  521. u8_t *end;
  522. struct pbuf *p, *q;
  523. u8_t *data;
  524. u16_t cnt = 0;
  525. u16_t i;
  526. err_t SendAck_err_t;
  527. create_msg(m);
  528. end = add_msg_type(&m->options[4], DHCPACK);
  529. end = add_offer_options(end);
  530. end = add_end(end);
  531. p = dhcps_pbuf_alloc(len);
  532. #if DHCPS_DEBUG
  533. DHCPS_LOG("udhcp: send_ack>>p->ref = %d\n", p->ref);
  534. #endif
  535. if (p != NULL) {
  536. #if DHCPS_DEBUG
  537. DHCPS_LOG("dhcps: send_ack>>pbuf_alloc succeed\n");
  538. DHCPS_LOG("dhcps: send_ack>>p->tot_len = %d\n", p->tot_len);
  539. DHCPS_LOG("dhcps: send_ack>>p->len = %d\n", p->len);
  540. #endif
  541. q = p;
  542. while (q != NULL) {
  543. data = (u8_t *)q->payload;
  544. for (i = 0; i < q->len; i++) {
  545. data[i] = ((u8_t *) m)[cnt++];
  546. #if DHCPS_DEBUG
  547. DHCPS_LOG("%02x ", data[i]);
  548. if ((i + 1) % 16 == 0) {
  549. DHCPS_LOG("\n");
  550. }
  551. #endif
  552. }
  553. q = q->next;
  554. }
  555. } else {
  556. #if DHCPS_DEBUG
  557. DHCPS_LOG("dhcps: send_ack>>pbuf_alloc failed\n");
  558. #endif
  559. return;
  560. }
  561. ip_addr_t ip_temp = IPADDR4_INIT(0x0);
  562. ip4_addr_set(ip_2_ip4(&ip_temp), &broadcast_dhcps);
  563. struct udp_pcb *pcb_dhcps = dhcps_netif->dhcps_pcb;
  564. SendAck_err_t = udp_sendto(pcb_dhcps, p, &ip_temp, DHCPS_CLIENT_PORT);
  565. #if DHCPS_DEBUG
  566. DHCPS_LOG("dhcps: send_ack>>udp_sendto result %x\n", SendAck_err_t);
  567. #endif
  568. if (SendAck_err_t == ERR_OK) {
  569. dhcps_cb(m->yiaddr);
  570. }
  571. if (p->ref != 0) {
  572. #if DHCPS_DEBUG
  573. DHCPS_LOG("udhcp: send_ack>>free pbuf\n");
  574. #endif
  575. pbuf_free(p);
  576. }
  577. }
  578. /******************************************************************************
  579. * FunctionName : parse_options
  580. * Description : parse DHCP message options
  581. * Parameters : optptr -- DHCP message option info
  582. * len -- DHCP message option length
  583. * Returns : none
  584. *******************************************************************************/
  585. static u8_t parse_options(u8_t *optptr, s16_t len)
  586. {
  587. ip4_addr_t client;
  588. bool is_dhcp_parse_end = false;
  589. struct dhcps_state s;
  590. client.addr = *((uint32_t *) &client_address);
  591. u8_t *end = optptr + len;
  592. u16_t type = 0;
  593. s.state = DHCPS_STATE_IDLE;
  594. while (optptr < end) {
  595. #if DHCPS_DEBUG
  596. DHCPS_LOG("dhcps: (s16_t)*optptr = %d\n", (s16_t)*optptr);
  597. #endif
  598. switch ((s16_t) *optptr) {
  599. case DHCP_OPTION_MSG_TYPE: //53
  600. type = *(optptr + 2);
  601. break;
  602. case DHCP_OPTION_REQ_IPADDR://50
  603. if (memcmp((char *) &client.addr, (char *) optptr + 2, 4) == 0) {
  604. #if DHCPS_DEBUG
  605. DHCPS_LOG("dhcps: DHCP_OPTION_REQ_IPADDR = 0 ok\n");
  606. #endif
  607. s.state = DHCPS_STATE_ACK;
  608. } else {
  609. #if DHCPS_DEBUG
  610. DHCPS_LOG("dhcps: DHCP_OPTION_REQ_IPADDR != 0 err\n");
  611. #endif
  612. s.state = DHCPS_STATE_NAK;
  613. }
  614. break;
  615. case DHCP_OPTION_END: {
  616. is_dhcp_parse_end = true;
  617. }
  618. break;
  619. }
  620. if (is_dhcp_parse_end) {
  621. break;
  622. }
  623. optptr += optptr[1] + 2;
  624. }
  625. switch (type) {
  626. case DHCPDISCOVER://1
  627. s.state = DHCPS_STATE_OFFER;
  628. #if DHCPS_DEBUG
  629. DHCPS_LOG("dhcps: DHCPD_STATE_OFFER\n");
  630. #endif
  631. break;
  632. case DHCPREQUEST://3
  633. if (!(s.state == DHCPS_STATE_ACK || s.state == DHCPS_STATE_NAK)) {
  634. if (renew == true) {
  635. s.state = DHCPS_STATE_ACK;
  636. } else {
  637. s.state = DHCPS_STATE_NAK;
  638. }
  639. #if DHCPS_DEBUG
  640. DHCPS_LOG("dhcps: DHCPD_STATE_NAK\n");
  641. #endif
  642. }
  643. break;
  644. case DHCPDECLINE://4
  645. s.state = DHCPS_STATE_IDLE;
  646. #if DHCPS_DEBUG
  647. DHCPS_LOG("dhcps: DHCPD_STATE_IDLE\n");
  648. #endif
  649. break;
  650. case DHCPRELEASE://7
  651. s.state = DHCPS_STATE_RELEASE;
  652. #if DHCPS_DEBUG
  653. DHCPS_LOG("dhcps: DHCPD_STATE_IDLE\n");
  654. #endif
  655. break;
  656. }
  657. #if DHCPS_DEBUG
  658. DHCPS_LOG("dhcps: return s.state = %d\n", s.state);
  659. #endif
  660. return s.state;
  661. }
  662. /******************************************************************************
  663. * FunctionName : parse_msg
  664. * Description : parse DHCP message from netif
  665. * Parameters : m -- DHCP message info
  666. * len -- DHCP message length
  667. * Returns : DHCP message type
  668. *******************************************************************************/
  669. static s16_t parse_msg(struct dhcps_msg *m, u16_t len)
  670. {
  671. u32_t lease_timer = (dhcps_lease_time * DHCPS_LEASE_UNIT)/DHCPS_COARSE_TIMER_SECS;
  672. if (memcmp((char *)m->options, &magic_cookie, sizeof(magic_cookie)) == 0) {
  673. #if DHCPS_DEBUG
  674. DHCPS_LOG("dhcps: len = %d\n", len);
  675. #endif
  676. ip4_addr_t addr_tmp;
  677. struct dhcps_pool *pdhcps_pool = NULL;
  678. list_node *pnode = NULL;
  679. list_node *pback_node = NULL;
  680. ip4_addr_t first_address;
  681. bool flag = false;
  682. first_address.addr = dhcps_poll.start_ip.addr;
  683. client_address.addr = client_address_plus.addr;
  684. renew = false;
  685. if (plist != NULL) {
  686. for (pback_node = plist; pback_node != NULL; pback_node = pback_node->pnext) {
  687. pdhcps_pool = pback_node->pnode;
  688. if (memcmp(pdhcps_pool->mac, m->chaddr, sizeof(pdhcps_pool->mac)) == 0) {
  689. if (memcmp(&pdhcps_pool->ip.addr, m->ciaddr, sizeof(pdhcps_pool->ip.addr)) == 0) {
  690. renew = true;
  691. }
  692. client_address.addr = pdhcps_pool->ip.addr;
  693. pdhcps_pool->lease_timer = lease_timer;
  694. pnode = pback_node;
  695. goto POOL_CHECK;
  696. } else if (pdhcps_pool->ip.addr == client_address_plus.addr) {
  697. addr_tmp.addr = htonl(client_address_plus.addr);
  698. addr_tmp.addr++;
  699. client_address_plus.addr = htonl(addr_tmp.addr);
  700. client_address.addr = client_address_plus.addr;
  701. }
  702. if (flag == false) { // search the fisrt unused ip
  703. if (first_address.addr < pdhcps_pool->ip.addr) {
  704. flag = true;
  705. } else {
  706. addr_tmp.addr = htonl(first_address.addr);
  707. addr_tmp.addr++;
  708. first_address.addr = htonl(addr_tmp.addr);
  709. }
  710. }
  711. }
  712. } else {
  713. client_address.addr = dhcps_poll.start_ip.addr;
  714. }
  715. if (client_address_plus.addr > dhcps_poll.end_ip.addr) {
  716. client_address.addr = first_address.addr;
  717. }
  718. if (client_address.addr > dhcps_poll.end_ip.addr) {
  719. client_address_plus.addr = dhcps_poll.start_ip.addr;
  720. pdhcps_pool = NULL;
  721. pnode = NULL;
  722. } else {
  723. pdhcps_pool = (struct dhcps_pool *)mem_malloc(sizeof(struct dhcps_pool));
  724. memset(pdhcps_pool , 0x00 , sizeof(struct dhcps_pool));
  725. pdhcps_pool->ip.addr = client_address.addr;
  726. memcpy(pdhcps_pool->mac, m->chaddr, sizeof(pdhcps_pool->mac));
  727. pdhcps_pool->lease_timer = lease_timer;
  728. pnode = (list_node *)mem_malloc(sizeof(list_node));
  729. memset(pnode , 0x00 , sizeof(list_node));
  730. pnode->pnode = pdhcps_pool;
  731. pnode->pnext = NULL;
  732. node_insert_to_list(&plist, pnode);
  733. if (client_address.addr == dhcps_poll.end_ip.addr) {
  734. client_address_plus.addr = dhcps_poll.start_ip.addr;
  735. } else {
  736. addr_tmp.addr = htonl(client_address.addr);
  737. addr_tmp.addr++;
  738. client_address_plus.addr = htonl(addr_tmp.addr);
  739. }
  740. }
  741. POOL_CHECK:
  742. if ((client_address.addr > dhcps_poll.end_ip.addr) || (ip4_addr_isany(&client_address))) {
  743. if (pnode != NULL) {
  744. node_remove_from_list(&plist, pnode);
  745. free(pnode);
  746. pnode = NULL;
  747. }
  748. if (pdhcps_pool != NULL) {
  749. free(pdhcps_pool);
  750. pdhcps_pool = NULL;
  751. }
  752. return 4;
  753. }
  754. s16_t ret = parse_options(&m->options[4], len);;
  755. if (ret == DHCPS_STATE_RELEASE || ret == DHCPS_STATE_NAK) {
  756. if (pnode != NULL) {
  757. node_remove_from_list(&plist, pnode);
  758. free(pnode);
  759. pnode = NULL;
  760. }
  761. if (pdhcps_pool != NULL) {
  762. free(pdhcps_pool);
  763. pdhcps_pool = NULL;
  764. }
  765. memset(&client_address, 0x0, sizeof(client_address));
  766. }
  767. #if DHCPS_DEBUG
  768. DHCPS_LOG("dhcps: xid changed\n");
  769. DHCPS_LOG("dhcps: client_address.addr = %x\n", client_address.addr);
  770. #endif
  771. return ret;
  772. }
  773. return 0;
  774. }
  775. /******************************************************************************
  776. * FunctionName : handle_dhcp
  777. * Description : If an incoming DHCP message is in response to us, then trigger the state machine
  778. * Parameters : arg -- arg user supplied argument (udp_pcb.recv_arg)
  779. * pcb -- the udp_pcb which received data
  780. * p -- the packet buffer that was received
  781. * addr -- the remote IP address from which the packet was received
  782. * port -- the remote port from which the packet was received
  783. * Returns : none
  784. *******************************************************************************/
  785. static void handle_dhcp(void *arg,
  786. struct udp_pcb *pcb,
  787. struct pbuf *p,
  788. const ip_addr_t *addr,
  789. u16_t port)
  790. {
  791. struct dhcps_msg *pmsg_dhcps = NULL;
  792. s16_t tlen, malloc_len;
  793. u16_t i;
  794. u16_t dhcps_msg_cnt = 0;
  795. u8_t *p_dhcps_msg = NULL;
  796. u8_t *data;
  797. #if DHCPS_DEBUG
  798. DHCPS_LOG("dhcps: handle_dhcp-> receive a packet\n");
  799. #endif
  800. if (p == NULL) {
  801. return;
  802. }
  803. malloc_len = sizeof(struct dhcps_msg);
  804. #if DHCPS_DEBUG
  805. DHCPS_LOG("dhcps: handle_dhcp malloc_len=%d rx_len=%d", malloc_len, p->tot_len);
  806. #endif
  807. if (malloc_len < p->tot_len) {
  808. malloc_len = p->tot_len;
  809. }
  810. pmsg_dhcps = (struct dhcps_msg *)mem_malloc(malloc_len);
  811. if (NULL == pmsg_dhcps) {
  812. pbuf_free(p);
  813. return;
  814. }
  815. memset(pmsg_dhcps , 0x00 , malloc_len);
  816. p_dhcps_msg = (u8_t *)pmsg_dhcps;
  817. tlen = p->tot_len;
  818. data = p->payload;
  819. #if DHCPS_DEBUG
  820. DHCPS_LOG("dhcps: handle_dhcp-> p->tot_len = %d\n", tlen);
  821. DHCPS_LOG("dhcps: handle_dhcp-> p->len = %d\n", p->len);
  822. #endif
  823. for (i = 0; i < p->len; i++) {
  824. p_dhcps_msg[dhcps_msg_cnt++] = data[i];
  825. #if DHCPS_DEBUG
  826. DHCPS_LOG("%02x ", data[i]);
  827. if ((i + 1) % 16 == 0) {
  828. DHCPS_LOG("\n");
  829. }
  830. #endif
  831. }
  832. if (p->next != NULL) {
  833. #if DHCPS_DEBUG
  834. DHCPS_LOG("dhcps: handle_dhcp-> p->next != NULL\n");
  835. DHCPS_LOG("dhcps: handle_dhcp-> p->next->tot_len = %d\n", p->next->tot_len);
  836. DHCPS_LOG("dhcps: handle_dhcp-> p->next->len = %d\n", p->next->len);
  837. #endif
  838. data = p->next->payload;
  839. for (i = 0; i < p->next->len; i++) {
  840. p_dhcps_msg[dhcps_msg_cnt++] = data[i];
  841. #if DHCPS_DEBUG
  842. DHCPS_LOG("%02x ", data[i]);
  843. if ((i + 1) % 16 == 0) {
  844. DHCPS_LOG("\n");
  845. }
  846. #endif
  847. }
  848. }
  849. #if DHCPS_DEBUG
  850. DHCPS_LOG("dhcps: handle_dhcp-> parse_msg(p)\n");
  851. #endif
  852. switch (parse_msg(pmsg_dhcps, tlen - 240)) {
  853. case DHCPS_STATE_OFFER://1
  854. #if DHCPS_DEBUG
  855. DHCPS_LOG("dhcps: handle_dhcp-> DHCPD_STATE_OFFER\n");
  856. #endif
  857. send_offer(pmsg_dhcps, malloc_len);
  858. break;
  859. case DHCPS_STATE_ACK://3
  860. #if DHCPS_DEBUG
  861. DHCPS_LOG("dhcps: handle_dhcp-> DHCPD_STATE_ACK\n");
  862. #endif
  863. send_ack(pmsg_dhcps, malloc_len);
  864. break;
  865. case DHCPS_STATE_NAK://4
  866. #if DHCPS_DEBUG
  867. DHCPS_LOG("dhcps: handle_dhcp-> DHCPD_STATE_NAK\n");
  868. #endif
  869. send_nak(pmsg_dhcps, malloc_len);
  870. break;
  871. default :
  872. break;
  873. }
  874. #if DHCPS_DEBUG
  875. DHCPS_LOG("dhcps: handle_dhcp-> pbuf_free(p)\n");
  876. #endif
  877. pbuf_free(p);
  878. free(pmsg_dhcps);
  879. pmsg_dhcps = NULL;
  880. }
  881. /******************************************************************************
  882. * FunctionName : dhcps_poll_set
  883. * Description : set ip poll from start to end for station
  884. * Parameters : ip -- The current ip addr
  885. * Returns : none
  886. *******************************************************************************/
  887. static void dhcps_poll_set(u32_t ip)
  888. {
  889. u32_t softap_ip = 0, local_ip = 0;
  890. u32_t start_ip = 0;
  891. u32_t end_ip = 0;
  892. if (dhcps_poll.enable == true) {
  893. softap_ip = htonl(ip);
  894. start_ip = htonl(dhcps_poll.start_ip.addr);
  895. end_ip = htonl(dhcps_poll.end_ip.addr);
  896. /*config ip information can't contain local ip*/
  897. if ((start_ip <= softap_ip) && (softap_ip <= end_ip)) {
  898. dhcps_poll.enable = false;
  899. } else {
  900. /*config ip information must be in the same segment as the local ip*/
  901. softap_ip >>= 8;
  902. if (((start_ip >> 8 != softap_ip) || (end_ip >> 8 != softap_ip))
  903. || (end_ip - start_ip > DHCPS_MAX_LEASE)) {
  904. dhcps_poll.enable = false;
  905. }
  906. }
  907. }
  908. if (dhcps_poll.enable == false) {
  909. local_ip = softap_ip = htonl(ip);
  910. softap_ip &= 0xFFFFFF00;
  911. local_ip &= 0xFF;
  912. if (local_ip >= 0x80) {
  913. local_ip -= DHCPS_MAX_LEASE;
  914. } else {
  915. local_ip ++;
  916. }
  917. bzero(&dhcps_poll, sizeof(dhcps_poll));
  918. dhcps_poll.start_ip.addr = softap_ip | local_ip;
  919. dhcps_poll.end_ip.addr = softap_ip | (local_ip + DHCPS_MAX_LEASE - 1);
  920. dhcps_poll.start_ip.addr = htonl(dhcps_poll.start_ip.addr);
  921. dhcps_poll.end_ip.addr = htonl(dhcps_poll.end_ip.addr);
  922. }
  923. }
  924. /******************************************************************************
  925. * FunctionName : dhcps_set_new_lease_cb
  926. * Description : set callback for dhcp server when it assign an IP
  927. * to the connected dhcp client
  928. * Parameters : cb -- callback for dhcp server
  929. * Returns : none
  930. *******************************************************************************/
  931. void dhcps_set_new_lease_cb(dhcps_cb_t cb)
  932. {
  933. dhcps_cb = cb;
  934. }
  935. /******************************************************************************
  936. * FunctionName : dhcps_start
  937. * Description : start dhcp server function
  938. * Parameters : netif -- The current netif addr
  939. * : info -- The current ip info
  940. * Returns : none
  941. *******************************************************************************/
  942. void dhcps_start(struct netif *netif, ip4_addr_t ip)
  943. {
  944. dhcps_netif = netif;
  945. if (dhcps_netif->dhcps_pcb != NULL) {
  946. udp_remove(dhcps_netif->dhcps_pcb);
  947. }
  948. dhcps_netif->dhcps_pcb = udp_new();
  949. struct udp_pcb *pcb_dhcps = dhcps_netif->dhcps_pcb;
  950. if (pcb_dhcps == NULL || ip4_addr_isany_val(ip)) {
  951. printf("dhcps_start(): could not obtain pcb\n");
  952. }
  953. dhcps_netif->dhcps_pcb = pcb_dhcps;
  954. IP4_ADDR(&broadcast_dhcps, 255, 255, 255, 255);
  955. server_address.addr = ip.addr;
  956. dhcps_poll_set(server_address.addr);
  957. client_address_plus.addr = dhcps_poll.start_ip.addr;
  958. udp_bind(pcb_dhcps, &netif->ip_addr, DHCPS_SERVER_PORT);
  959. udp_recv(pcb_dhcps, handle_dhcp, NULL);
  960. #if DHCPS_DEBUG
  961. DHCPS_LOG("dhcps:dhcps_start->udp_recv function Set a receive callback handle_dhcp for UDP_PCB pcb_dhcps\n");
  962. #endif
  963. }
  964. /******************************************************************************
  965. * FunctionName : dhcps_stop
  966. * Description : stop dhcp server function
  967. * Parameters : netif -- The current netif addr
  968. * Returns : none
  969. *******************************************************************************/
  970. void dhcps_stop(struct netif *netif)
  971. {
  972. struct netif *apnetif = netif;
  973. if (apnetif == NULL) {
  974. printf("dhcps_stop: apnetif == NULL\n");
  975. return;
  976. }
  977. if (apnetif->dhcps_pcb != NULL) {
  978. udp_disconnect(apnetif->dhcps_pcb);
  979. udp_remove(apnetif->dhcps_pcb);
  980. apnetif->dhcps_pcb = NULL;
  981. }
  982. list_node *pnode = NULL;
  983. list_node *pback_node = NULL;
  984. pnode = plist;
  985. while (pnode != NULL) {
  986. pback_node = pnode;
  987. pnode = pback_node->pnext;
  988. node_remove_from_list(&plist, pback_node);
  989. free(pback_node->pnode);
  990. pback_node->pnode = NULL;
  991. free(pback_node);
  992. pback_node = NULL;
  993. }
  994. }
  995. /******************************************************************************
  996. * FunctionName : kill_oldest_dhcps_pool
  997. * Description : remove the oldest node from list
  998. * Parameters : none
  999. * Returns : none
  1000. *******************************************************************************/
  1001. static void kill_oldest_dhcps_pool(void)
  1002. {
  1003. list_node *pre = NULL, *p = NULL;
  1004. list_node *minpre = NULL, *minp = NULL;
  1005. struct dhcps_pool *pdhcps_pool = NULL, *pmin_pool = NULL;
  1006. pre = plist;
  1007. assert(pre != NULL && pre->pnext != NULL); // Expect the list to have at least 2 nodes
  1008. p = pre->pnext;
  1009. minpre = pre;
  1010. minp = p;
  1011. while (p != NULL) {
  1012. pdhcps_pool = p->pnode;
  1013. pmin_pool = minp->pnode;
  1014. if (pdhcps_pool->lease_timer < pmin_pool->lease_timer) {
  1015. minp = p;
  1016. minpre = pre;
  1017. }
  1018. pre = p;
  1019. p = p->pnext;
  1020. }
  1021. minpre->pnext = minp->pnext;
  1022. free(minp->pnode);
  1023. minp->pnode = NULL;
  1024. free(minp);
  1025. minp = NULL;
  1026. }
  1027. /******************************************************************************
  1028. * FunctionName : dhcps_coarse_tmr
  1029. * Description : the lease time count
  1030. * Parameters : none
  1031. * Returns : none
  1032. *******************************************************************************/
  1033. void dhcps_coarse_tmr(void)
  1034. {
  1035. u8_t num_dhcps_pool = 0;
  1036. list_node *pback_node = NULL;
  1037. list_node *pnode = NULL;
  1038. struct dhcps_pool *pdhcps_pool = NULL;
  1039. pnode = plist;
  1040. while (pnode != NULL) {
  1041. pdhcps_pool = pnode->pnode;
  1042. pdhcps_pool->lease_timer --;
  1043. if (pdhcps_pool->lease_timer == 0) {
  1044. pback_node = pnode;
  1045. pnode = pback_node->pnext;
  1046. node_remove_from_list(&plist, pback_node);
  1047. free(pback_node->pnode);
  1048. pback_node->pnode = NULL;
  1049. free(pback_node);
  1050. pback_node = NULL;
  1051. } else {
  1052. pnode = pnode ->pnext;
  1053. num_dhcps_pool ++;
  1054. }
  1055. }
  1056. if (num_dhcps_pool > MAX_STATION_NUM) {
  1057. kill_oldest_dhcps_pool();
  1058. }
  1059. }
  1060. /******************************************************************************
  1061. * FunctionName : dhcp_search_ip_on_mac
  1062. * Description : Search ip address based on mac address
  1063. * Parameters : mac -- The MAC addr
  1064. * ip -- The IP info
  1065. * Returns : true or false
  1066. *******************************************************************************/
  1067. bool dhcp_search_ip_on_mac(u8_t *mac, ip4_addr_t *ip)
  1068. {
  1069. struct dhcps_pool *pdhcps_pool = NULL;
  1070. list_node *pback_node = NULL;
  1071. bool ret = false;
  1072. for (pback_node = plist; pback_node != NULL; pback_node = pback_node->pnext) {
  1073. pdhcps_pool = pback_node->pnode;
  1074. if (memcmp(pdhcps_pool->mac, mac, sizeof(pdhcps_pool->mac)) == 0) {
  1075. memcpy(&ip->addr, &pdhcps_pool->ip.addr, sizeof(pdhcps_pool->ip.addr));
  1076. ret = true;
  1077. break;
  1078. }
  1079. }
  1080. return ret;
  1081. }
  1082. /******************************************************************************
  1083. * FunctionName : dhcps_dns_setserver
  1084. * Description : set DNS server address for dhcpserver
  1085. * Parameters : dnsserver -- The DNS server address
  1086. * Returns : none
  1087. *******************************************************************************/
  1088. void
  1089. dhcps_dns_setserver(const ip_addr_t *dnsserver)
  1090. {
  1091. if (dnsserver != NULL) {
  1092. dns_server = *(ip_2_ip4(dnsserver));
  1093. } else {
  1094. dns_server = *(ip_2_ip4(IP_ADDR_ANY));
  1095. }
  1096. }
  1097. /******************************************************************************
  1098. * FunctionName : dhcps_dns_getserver
  1099. * Description : get DNS server address for dhcpserver
  1100. * Parameters : none
  1101. * Returns : ip4_addr_t
  1102. *******************************************************************************/
  1103. ip4_addr_t
  1104. dhcps_dns_getserver(void)
  1105. {
  1106. return dns_server;
  1107. }
  1108. #endif // ESP_DHCP