1
0

dhcp_server.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /*
  2. * File : dhcp_server.c
  3. * A simple DHCP server implementation
  4. *
  5. * COPYRIGHT (C) 2011-2018, Shanghai Real-Thread Technology Co., Ltd
  6. * http://www.rt-thread.com
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * 3. The name of the author may not be used to endorse or promote products
  18. * derived from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  21. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  22. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  23. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  25. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  28. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  29. * OF SUCH DAMAGE.
  30. *
  31. * Change Logs:
  32. * Date Author Notes
  33. * 2013-01-30 aozima the first version
  34. * 2013-08-08 aozima support different network segments.
  35. * 2015-01-30 bernard release to RT-Thread RTOS.
  36. * 2017-12-27 aozima add [mac-ip] table support.
  37. */
  38. #include <stdio.h>
  39. #include <stdint.h>
  40. #include <rtthread.h>
  41. #include <lwip/opt.h>
  42. #include <lwip/sockets.h>
  43. #include <lwip/inet_chksum.h>
  44. #include <netif/etharp.h>
  45. #include <netif/ethernetif.h>
  46. #include <lwip/ip.h>
  47. #include <lwip/init.h>
  48. #if (LWIP_VERSION) >= 0x02000000U
  49. #include <lwip/prot/dhcp.h>
  50. #endif
  51. /* DHCP server option */
  52. /* allocated client ip range */
  53. #ifndef DHCPD_CLIENT_IP_MIN
  54. #define DHCPD_CLIENT_IP_MIN 2
  55. #endif
  56. #ifndef DHCPD_CLIENT_IP_MAX
  57. #define DHCPD_CLIENT_IP_MAX 254
  58. #endif
  59. /* the DHCP server address */
  60. #ifndef DHCPD_SERVER_IP
  61. #define DHCPD_SERVER_IP "192.168.169.1"
  62. #endif
  63. //#define DHCP_DEBUG_PRINTF
  64. #ifdef DHCP_DEBUG_PRINTF
  65. #define DEBUG_PRINTF rt_kprintf("[DHCP] "); rt_kprintf
  66. #else
  67. #define DEBUG_PRINTF(...)
  68. #endif /* DHCP_DEBUG_PRINTF */
  69. /* we need some routines in the DHCP of lwIP */
  70. #undef LWIP_DHCP
  71. #define LWIP_DHCP 1
  72. #include <lwip/dhcp.h>
  73. #ifndef DHCP_CLIENT_PORT
  74. #define DHCP_CLIENT_PORT 68
  75. #endif
  76. #ifndef DHCP_SERVER_PORT
  77. #define DHCP_SERVER_PORT 67
  78. #endif
  79. #ifndef ETHADDR32_COPY
  80. #define ETHADDR32_COPY(dst, src) SMEMCPY(dst, src, ETH_HWADDR_LEN)
  81. #endif
  82. #ifndef ETHADDR16_COPY
  83. #define ETHADDR16_COPY(dst, src) SMEMCPY(dst, src, ETH_HWADDR_LEN)
  84. #endif
  85. /* buffer size for receive DHCP packet */
  86. #define BUFSZ 1024
  87. #ifndef MAC_ADDR_LEN
  88. #define MAC_ADDR_LEN 6
  89. #endif
  90. #ifndef MAC_TABLE_LEN
  91. #define MAC_TABLE_LEN 4
  92. #endif
  93. struct mac_addr_t
  94. {
  95. uint8_t add[MAC_ADDR_LEN];
  96. };
  97. struct mac_ip_item_t
  98. {
  99. struct mac_addr_t mac_addr;
  100. uint8_t ip_addr_3;
  101. };
  102. static rt_err_t _low_level_dhcp_send(struct netif *netif,
  103. const void *buffer,
  104. rt_size_t size)
  105. {
  106. struct pbuf *p;
  107. struct eth_hdr *ethhdr;
  108. struct ip_hdr *iphdr;
  109. struct udp_hdr *udphdr;
  110. p = pbuf_alloc(PBUF_LINK,
  111. SIZEOF_ETH_HDR + sizeof(struct ip_hdr)
  112. + sizeof(struct udp_hdr) + size,
  113. PBUF_RAM);
  114. if (p == RT_NULL) return -RT_ENOMEM;
  115. ethhdr = (struct eth_hdr *)p->payload;
  116. iphdr = (struct ip_hdr *)((char *)ethhdr + SIZEOF_ETH_HDR);
  117. udphdr = (struct udp_hdr *)((char *)iphdr + sizeof(struct ip_hdr));
  118. ETHADDR32_COPY(&ethhdr->dest, (struct eth_addr *)&ethbroadcast);
  119. ETHADDR16_COPY(&ethhdr->src, netif->hwaddr);
  120. ethhdr->type = PP_HTONS(ETHTYPE_IP);
  121. iphdr->src.addr = 0x00000000; /* src: 0.0.0.0 */
  122. iphdr->dest.addr = 0xFFFFFFFF; /* src: 255.255.255.255 */
  123. IPH_VHL_SET(iphdr, 4, IP_HLEN / 4);
  124. IPH_TOS_SET(iphdr, 0x00);
  125. IPH_LEN_SET(iphdr, htons(IP_HLEN + sizeof(struct udp_hdr) + size));
  126. IPH_ID_SET(iphdr, htons(2));
  127. IPH_OFFSET_SET(iphdr, 0);
  128. IPH_TTL_SET(iphdr, 255);
  129. IPH_PROTO_SET(iphdr, IP_PROTO_UDP);
  130. IPH_CHKSUM_SET(iphdr, 0);
  131. IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
  132. udphdr->src = htons(DHCP_SERVER_PORT);
  133. udphdr->dest = htons(DHCP_CLIENT_PORT);
  134. udphdr->len = htons(sizeof(struct udp_hdr) + size);
  135. udphdr->chksum = 0;
  136. memcpy((char *)udphdr + sizeof(struct udp_hdr),
  137. buffer, size);
  138. netif->linkoutput(netif, p);
  139. pbuf_free(p);
  140. return RT_EOK;
  141. }
  142. static uint8_t get_ip(struct mac_addr_t *p_mac_addr)
  143. {
  144. static uint8_t next_client_ip = DHCPD_CLIENT_IP_MIN;
  145. static struct mac_ip_item_t mac_table[MAC_TABLE_LEN];
  146. static int offset = 0;
  147. struct mac_addr_t bad_mac;
  148. int i;
  149. uint8_t ip_addr_3;
  150. rt_memset(&bad_mac, 0, sizeof(bad_mac));
  151. if (!rt_memcmp(&bad_mac, p_mac_addr, sizeof(bad_mac)))
  152. {
  153. DEBUG_PRINTF("mac address all zero");
  154. ip_addr_3 = DHCPD_CLIENT_IP_MAX;
  155. goto _return;
  156. }
  157. rt_memset(&bad_mac, 0xFF, sizeof(bad_mac));
  158. if (!rt_memcmp(&bad_mac, p_mac_addr, sizeof(bad_mac)))
  159. {
  160. DEBUG_PRINTF("mac address all one");
  161. ip_addr_3 = DHCPD_CLIENT_IP_MAX;
  162. goto _return;
  163. }
  164. for (i = 0; i < MAC_TABLE_LEN; i++)
  165. {
  166. if (!rt_memcmp(&mac_table[i].mac_addr, p_mac_addr, sizeof(bad_mac)))
  167. {
  168. //use old ip
  169. ip_addr_3 = mac_table[i].ip_addr_3;
  170. DEBUG_PRINTF("return old ip: %d\n", (int)ip_addr_3);
  171. goto _return;
  172. }
  173. }
  174. /* add new ip */
  175. mac_table[offset].mac_addr = *p_mac_addr;
  176. mac_table[offset].ip_addr_3 = next_client_ip;
  177. ip_addr_3 = mac_table[offset].ip_addr_3 ;
  178. offset++;
  179. if (offset >= MAC_TABLE_LEN)
  180. offset = 0;
  181. next_client_ip++;
  182. if (next_client_ip > DHCPD_CLIENT_IP_MAX)
  183. next_client_ip = DHCPD_CLIENT_IP_MIN;
  184. DEBUG_PRINTF("create new ip: %d\n", (int)ip_addr_3);
  185. DEBUG_PRINTF("next_client_ip %d\n", next_client_ip);
  186. _return:
  187. return ip_addr_3;
  188. }
  189. static void dhcpd_thread_entry(void *parameter)
  190. {
  191. struct netif *netif = RT_NULL;
  192. int sock;
  193. int bytes_read;
  194. char *recv_data;
  195. rt_uint32_t addr_len;
  196. struct sockaddr_in server_addr, client_addr;
  197. struct dhcp_msg *msg;
  198. int optval = 1;
  199. struct mac_addr_t mac_addr;
  200. uint8_t DHCPD_SERVER_IPADDR0, DHCPD_SERVER_IPADDR1, DHCPD_SERVER_IPADDR2, DHCPD_SERVER_IPADDR3;
  201. /* get ethernet interface. */
  202. netif = (struct netif *) parameter;
  203. RT_ASSERT(netif != RT_NULL);
  204. /* our DHCP server information */
  205. {
  206. #if (LWIP_VERSION) >= 0x02000000U
  207. ip4_addr_t addr;
  208. ip4addr_aton(DHCPD_SERVER_IP, &addr);
  209. #else
  210. struct ip_addr addr;
  211. ipaddr_aton(DHCPD_SERVER_IP, &addr);
  212. #endif /* LWIP_VERSION */
  213. DHCPD_SERVER_IPADDR0 = (ntohl(addr.addr) >> 24) & 0xFF;
  214. DHCPD_SERVER_IPADDR1 = (ntohl(addr.addr) >> 16) & 0xFF;
  215. DHCPD_SERVER_IPADDR2 = (ntohl(addr.addr) >> 8) & 0xFF;
  216. DHCPD_SERVER_IPADDR3 = (ntohl(addr.addr) >> 0) & 0xFF;
  217. }
  218. DEBUG_PRINTF("DHCP server IP: %d.%d.%d.%d client IP: %d.%d.%d.%d-%d\n",
  219. DHCPD_SERVER_IPADDR0, DHCPD_SERVER_IPADDR1,
  220. DHCPD_SERVER_IPADDR2, DHCPD_SERVER_IPADDR3,
  221. DHCPD_SERVER_IPADDR0, DHCPD_SERVER_IPADDR1,
  222. DHCPD_SERVER_IPADDR2, DHCPD_CLIENT_IP_MIN, DHCPD_CLIENT_IP_MAX);
  223. /* allocate buffer for receive */
  224. recv_data = rt_malloc(BUFSZ);
  225. if (recv_data == RT_NULL)
  226. {
  227. /* No memory */
  228. DEBUG_PRINTF("Out of memory\n");
  229. return;
  230. }
  231. /* create a socket with UDP */
  232. if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
  233. {
  234. DEBUG_PRINTF("create socket failed, errno = %d\n", errno);
  235. rt_free(recv_data);
  236. return;
  237. }
  238. /* set to receive broadcast packet */
  239. setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(optval));
  240. /* initialize server address */
  241. server_addr.sin_family = AF_INET;
  242. server_addr.sin_port = htons(DHCP_SERVER_PORT);
  243. server_addr.sin_addr.s_addr = INADDR_ANY;
  244. rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));
  245. /* bind socket to the server address */
  246. if (bind(sock, (struct sockaddr *)&server_addr,
  247. sizeof(struct sockaddr)) == -1)
  248. {
  249. /* bind failed. */
  250. DEBUG_PRINTF("bind server address failed, errno=%d\n", errno);
  251. closesocket(sock);
  252. rt_free(recv_data);
  253. return;
  254. }
  255. addr_len = sizeof(struct sockaddr);
  256. DEBUG_PRINTF("DHCP server listen on port %d...\n", DHCP_SERVER_PORT);
  257. while (1)
  258. {
  259. bytes_read = recvfrom(sock, recv_data, BUFSZ - 1, 0,
  260. (struct sockaddr *)&client_addr, (socklen_t *)&addr_len);
  261. if (bytes_read <= 0)
  262. {
  263. closesocket(sock);
  264. rt_free(recv_data);
  265. return;
  266. }
  267. else if (bytes_read < DHCP_MSG_LEN)
  268. {
  269. DEBUG_PRINTF("packet too short, wait for next!\n");
  270. continue;
  271. }
  272. msg = (struct dhcp_msg *)recv_data;
  273. /* check message type to make sure we can handle it */
  274. if ((msg->op != DHCP_BOOTREQUEST) || (msg->cookie != PP_HTONL(DHCP_MAGIC_COOKIE)))
  275. {
  276. continue;
  277. }
  278. memcpy(mac_addr.add, msg->chaddr, MAC_ADDR_LEN);
  279. /* handler. */
  280. {
  281. uint8_t *dhcp_opt;
  282. uint8_t option;
  283. uint8_t length;
  284. uint8_t message_type = 0;
  285. uint8_t finished = 0;
  286. uint32_t request_ip = 0;
  287. uint8_t client_ip_3;
  288. client_ip_3 = get_ip(&mac_addr);
  289. dhcp_opt = (uint8_t *)msg + DHCP_OPTIONS_OFS;
  290. while (finished == 0)
  291. {
  292. option = *dhcp_opt;
  293. length = *(dhcp_opt + 1);
  294. switch (option)
  295. {
  296. case DHCP_OPTION_REQUESTED_IP:
  297. request_ip = *(dhcp_opt + 2) << 24 | *(dhcp_opt + 3) << 16
  298. | *(dhcp_opt + 4) << 8 | *(dhcp_opt + 5);
  299. break;
  300. case DHCP_OPTION_END:
  301. finished = 1;
  302. break;
  303. case DHCP_OPTION_MESSAGE_TYPE:
  304. message_type = *(dhcp_opt + 2);
  305. break;
  306. default:
  307. break;
  308. } /* switch(option) */
  309. dhcp_opt += (2 + length);
  310. }
  311. /* reply. */
  312. dhcp_opt = (uint8_t *)msg + DHCP_OPTIONS_OFS;
  313. /* check. */
  314. if (request_ip)
  315. {
  316. uint32_t client_ip = DHCPD_SERVER_IPADDR0 << 24 | DHCPD_SERVER_IPADDR1 << 16
  317. | DHCPD_SERVER_IPADDR2 << 8 | client_ip_3;
  318. DEBUG_PRINTF("message_type: %d, request_ip: %08X, client_ip: %08X.\n", message_type, request_ip, client_ip);
  319. if (request_ip != client_ip)
  320. {
  321. *dhcp_opt++ = DHCP_OPTION_MESSAGE_TYPE;
  322. *dhcp_opt++ = DHCP_OPTION_MESSAGE_TYPE_LEN;
  323. *dhcp_opt++ = DHCP_NAK;
  324. *dhcp_opt++ = DHCP_OPTION_END;
  325. DEBUG_PRINTF("requested IP invalid, reply DHCP_NAK\n");
  326. if (netif != RT_NULL)
  327. {
  328. int send_byte = (dhcp_opt - (uint8_t *)msg);
  329. _low_level_dhcp_send(netif, msg, send_byte);
  330. DEBUG_PRINTF("DHCP server send %d byte\n", send_byte);
  331. }
  332. continue;
  333. }
  334. }
  335. if (message_type == DHCP_DISCOVER)
  336. {
  337. DEBUG_PRINTF("request DHCP_DISCOVER\n");
  338. DEBUG_PRINTF("reply DHCP_OFFER\n");
  339. // DHCP_OPTION_MESSAGE_TYPE
  340. *dhcp_opt++ = DHCP_OPTION_MESSAGE_TYPE;
  341. *dhcp_opt++ = DHCP_OPTION_MESSAGE_TYPE_LEN;
  342. *dhcp_opt++ = DHCP_OFFER;
  343. // DHCP_OPTION_SERVER_ID
  344. *dhcp_opt++ = DHCP_OPTION_SERVER_ID;
  345. *dhcp_opt++ = 4;
  346. *dhcp_opt++ = DHCPD_SERVER_IPADDR0;
  347. *dhcp_opt++ = DHCPD_SERVER_IPADDR1;
  348. *dhcp_opt++ = DHCPD_SERVER_IPADDR2;
  349. *dhcp_opt++ = DHCPD_SERVER_IPADDR3;
  350. // DHCP_OPTION_LEASE_TIME
  351. *dhcp_opt++ = DHCP_OPTION_LEASE_TIME;
  352. *dhcp_opt++ = 4;
  353. *dhcp_opt++ = 0x00;
  354. *dhcp_opt++ = 0x01;
  355. *dhcp_opt++ = 0x51;
  356. *dhcp_opt++ = 0x80;
  357. }
  358. else if (message_type == DHCP_REQUEST)
  359. {
  360. DEBUG_PRINTF("request DHCP_REQUEST\n");
  361. DEBUG_PRINTF("reply DHCP_ACK\n");
  362. // DHCP_OPTION_MESSAGE_TYPE
  363. *dhcp_opt++ = DHCP_OPTION_MESSAGE_TYPE;
  364. *dhcp_opt++ = DHCP_OPTION_MESSAGE_TYPE_LEN;
  365. *dhcp_opt++ = DHCP_ACK;
  366. // DHCP_OPTION_SERVER_ID
  367. *dhcp_opt++ = DHCP_OPTION_SERVER_ID;
  368. *dhcp_opt++ = 4;
  369. *dhcp_opt++ = DHCPD_SERVER_IPADDR0;
  370. *dhcp_opt++ = DHCPD_SERVER_IPADDR1;
  371. *dhcp_opt++ = DHCPD_SERVER_IPADDR2;
  372. *dhcp_opt++ = DHCPD_SERVER_IPADDR3;
  373. // DHCP_OPTION_SUBNET_MASK
  374. *dhcp_opt++ = DHCP_OPTION_SUBNET_MASK;
  375. *dhcp_opt++ = 4;
  376. *dhcp_opt++ = 0xFF;
  377. *dhcp_opt++ = 0xFF;
  378. *dhcp_opt++ = 0xFF;
  379. *dhcp_opt++ = 0x00;
  380. #ifdef DHCPD_USING_ROUTER
  381. // DHCP_OPTION_ROUTER
  382. *dhcp_opt++ = DHCP_OPTION_ROUTER;
  383. *dhcp_opt++ = 4;
  384. *dhcp_opt++ = DHCPD_SERVER_IPADDR0;
  385. *dhcp_opt++ = DHCPD_SERVER_IPADDR1;
  386. *dhcp_opt++ = DHCPD_SERVER_IPADDR2;
  387. *dhcp_opt++ = 1;
  388. #endif
  389. // DHCP_OPTION_DNS_SERVER, use the default DNS server address in lwIP
  390. *dhcp_opt++ = DHCP_OPTION_DNS_SERVER;
  391. *dhcp_opt++ = 4;
  392. #ifndef DHCP_DNS_SERVER_IP
  393. *dhcp_opt++ = DHCPD_SERVER_IPADDR0;
  394. *dhcp_opt++ = DHCPD_SERVER_IPADDR1;
  395. *dhcp_opt++ = DHCPD_SERVER_IPADDR2;
  396. *dhcp_opt++ = 1;
  397. #else
  398. {
  399. #if (LWIP_VERSION) >= 0x02000000U
  400. ip4_addr_t dns_addr;
  401. #else
  402. struct ip_addr dns_addr;
  403. #endif /* LWIP_VERSION */
  404. ip4addr_aton(DHCP_DNS_SERVER_IP, &dns_addr);
  405. *dhcp_opt++ = (ntohl(dns_addr.addr) >> 24) & 0xFF;
  406. *dhcp_opt++ = (ntohl(dns_addr.addr) >> 16) & 0xFF;
  407. *dhcp_opt++ = (ntohl(dns_addr.addr) >> 8) & 0xFF;
  408. *dhcp_opt++ = (ntohl(dns_addr.addr) >> 0) & 0xFF;
  409. }
  410. #endif
  411. // DHCP_OPTION_LEASE_TIME
  412. *dhcp_opt++ = DHCP_OPTION_LEASE_TIME;
  413. *dhcp_opt++ = 4;
  414. *dhcp_opt++ = 0x00;
  415. *dhcp_opt++ = 0x01;
  416. *dhcp_opt++ = 0x51;
  417. *dhcp_opt++ = 0x80;
  418. }
  419. else
  420. {
  421. DEBUG_PRINTF("un handle message:%d\n", message_type);
  422. }
  423. // append DHCP_OPTION_END
  424. *dhcp_opt++ = DHCP_OPTION_END;
  425. /* send reply. */
  426. if ((message_type == DHCP_DISCOVER) || (message_type == DHCP_REQUEST))
  427. {
  428. msg->op = DHCP_BOOTREPLY;
  429. IP4_ADDR(&msg->yiaddr,
  430. DHCPD_SERVER_IPADDR0, DHCPD_SERVER_IPADDR1,
  431. DHCPD_SERVER_IPADDR2, client_ip_3);
  432. client_addr.sin_addr.s_addr = INADDR_BROADCAST;
  433. if (netif != RT_NULL)
  434. {
  435. int send_byte = (dhcp_opt - (uint8_t *)msg);
  436. _low_level_dhcp_send(netif, msg, send_byte);
  437. DEBUG_PRINTF("DHCP server send %d byte\n", send_byte);
  438. }
  439. }
  440. } /* handler. */
  441. }
  442. }
  443. void dhcpd_start(const char *netif_name)
  444. {
  445. rt_thread_t thread;
  446. struct netif *netif = netif_list;
  447. if (strlen(netif_name) > sizeof(netif->name))
  448. {
  449. rt_kprintf("network interface name too long!\r\n");
  450. return;
  451. }
  452. while (netif != RT_NULL)
  453. {
  454. if (strncmp(netif_name, netif->name, sizeof(netif->name)) == 0)
  455. break;
  456. netif = netif->next;
  457. if (netif == RT_NULL)
  458. {
  459. rt_kprintf("network interface: %s not found!\r\n", netif_name);
  460. return;
  461. }
  462. }
  463. if (1)
  464. {
  465. extern void set_if(const char *netif_name, const char *ip_addr, const char *gw_addr, const char *nm_addr);
  466. dhcp_stop(netif);
  467. set_if(netif_name, DHCPD_SERVER_IP, "0.0.0.0", "255.255.255.0");
  468. netif_set_up(netif);
  469. }
  470. thread = rt_thread_create("dhcpd",
  471. dhcpd_thread_entry, netif,
  472. 1024,
  473. RT_THREAD_PRIORITY_MAX - 3,
  474. 2);
  475. if (thread != RT_NULL)
  476. {
  477. rt_thread_startup(thread);
  478. }
  479. }