ethernetif.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. /*
  2. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  3. * COPYRIGHT (C) 2006-2018, RT-Thread Development Team
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * 3. The name of the author may not be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  20. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  21. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  22. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  25. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  26. * OF SUCH DAMAGE.
  27. *
  28. * This file is part of the lwIP TCP/IP stack.
  29. *
  30. * Author: Adam Dunkels <adam@sics.se>
  31. *
  32. * Change Logs:
  33. * Date Author Notes
  34. * 2010-07-07 Bernard fix send mail to mailbox issue.
  35. * 2011-07-30 mbbill port lwIP 1.4.0 to RT-Thread
  36. * 2012-04-10 Bernard add more compatible with RT-Thread.
  37. * 2012-11-12 Bernard The network interface can be initialized
  38. * after lwIP initialization.
  39. * 2013-02-28 aozima fixed list_tcps bug: ipaddr_ntoa isn't reentrant.
  40. * 2016-08-18 Bernard port to lwIP 2.0.0
  41. * 2018-11-02 MurphyZhao port to lwIP 2.1.0
  42. */
  43. #include "lwip/opt.h"
  44. #include "lwip/opt.h"
  45. #include "lwip/debug.h"
  46. #include "lwip/def.h"
  47. #include "lwip/mem.h"
  48. #include "lwip/pbuf.h"
  49. #include "lwip/sys.h"
  50. #include "lwip/netif.h"
  51. #include "lwip/stats.h"
  52. #include "lwip/tcpip.h"
  53. #include "lwip/dhcp.h"
  54. #include "lwip/netifapi.h"
  55. #include "netif/etharp.h"
  56. #include "netif/ethernetif.h"
  57. #include "lwip/inet.h"
  58. #if LWIP_IPV6
  59. #include "lwip/ethip6.h"
  60. #endif /* LWIP_IPV6 */
  61. #define netifapi_netif_set_link_up(n) netifapi_netif_common(n, netif_set_link_up, NULL)
  62. #define netifapi_netif_set_link_down(n) netifapi_netif_common(n, netif_set_link_down, NULL)
  63. #ifndef RT_LWIP_ETHTHREAD_PRIORITY
  64. #define RT_ETHERNETIF_THREAD_PREORITY 0x90
  65. #else
  66. #define RT_ETHERNETIF_THREAD_PREORITY RT_LWIP_ETHTHREAD_PRIORITY
  67. #endif
  68. #ifndef LWIP_NO_TX_THREAD
  69. /**
  70. * Tx message structure for Ethernet interface
  71. */
  72. struct eth_tx_msg
  73. {
  74. struct netif *netif;
  75. struct pbuf *buf;
  76. };
  77. static struct rt_mailbox eth_tx_thread_mb;
  78. static struct rt_thread eth_tx_thread;
  79. #ifndef RT_LWIP_ETHTHREAD_MBOX_SIZE
  80. static char eth_tx_thread_mb_pool[32 * 4];
  81. static char eth_tx_thread_stack[512];
  82. #else
  83. static char eth_tx_thread_mb_pool[RT_LWIP_ETHTHREAD_MBOX_SIZE * 4];
  84. static char eth_tx_thread_stack[RT_LWIP_ETHTHREAD_STACKSIZE];
  85. #endif
  86. #endif
  87. #ifndef LWIP_NO_RX_THREAD
  88. static struct rt_mailbox eth_rx_thread_mb;
  89. static struct rt_thread eth_rx_thread;
  90. #ifndef RT_LWIP_ETHTHREAD_MBOX_SIZE
  91. static char eth_rx_thread_mb_pool[48 * 4];
  92. static char eth_rx_thread_stack[1024];
  93. #else
  94. static char eth_rx_thread_mb_pool[RT_LWIP_ETHTHREAD_MBOX_SIZE * 4];
  95. static char eth_rx_thread_stack[RT_LWIP_ETHTHREAD_STACKSIZE];
  96. #endif
  97. #endif
  98. static err_t ethernetif_linkoutput(struct netif *netif, struct pbuf *p)
  99. {
  100. #ifndef LWIP_NO_TX_THREAD
  101. struct eth_tx_msg msg;
  102. struct eth_device* enetif;
  103. RT_ASSERT(netif != RT_NULL);
  104. enetif = (struct eth_device*)netif->state;
  105. /* send a message to eth tx thread */
  106. msg.netif = netif;
  107. msg.buf = p;
  108. if (rt_mb_send(&eth_tx_thread_mb, (rt_uint32_t) &msg) == RT_EOK)
  109. {
  110. /* waiting for ack */
  111. rt_sem_take(&(enetif->tx_ack), RT_WAITING_FOREVER);
  112. }
  113. #else
  114. struct eth_device* enetif;
  115. RT_ASSERT(netif != RT_NULL);
  116. enetif = (struct eth_device*)netif->state;
  117. if (enetif->eth_tx(&(enetif->parent), p) != RT_EOK)
  118. {
  119. return ERR_IF;
  120. }
  121. #endif
  122. return ERR_OK;
  123. }
  124. static err_t eth_netif_device_init(struct netif *netif)
  125. {
  126. struct eth_device *ethif;
  127. ethif = (struct eth_device*)netif->state;
  128. if (ethif != RT_NULL)
  129. {
  130. rt_device_t device;
  131. /* get device object */
  132. device = (rt_device_t) ethif;
  133. if (rt_device_init(device) != RT_EOK)
  134. {
  135. return ERR_IF;
  136. }
  137. /* copy device flags to netif flags */
  138. netif->flags = (ethif->flags & 0xff);
  139. netif->mtu = ETHERNET_MTU;
  140. /* set output */
  141. netif->output = etharp_output;
  142. #if LWIP_IPV6
  143. netif->output_ip6 = ethip6_output;
  144. netif->ip6_autoconfig_enabled = 1;
  145. netif_create_ip6_linklocal_address(netif, 1);
  146. #if LWIP_IPV6_MLD
  147. netif->flags |= NETIF_FLAG_MLD6;
  148. /*
  149. * For hardware/netifs that implement MAC filtering.
  150. * All-nodes link-local is handled by default, so we must let the hardware know
  151. * to allow multicast packets in.
  152. * Should set mld_mac_filter previously. */
  153. if (netif->mld_mac_filter != NULL)
  154. {
  155. ip6_addr_t ip6_allnodes_ll;
  156. ip6_addr_set_allnodes_linklocal(&ip6_allnodes_ll);
  157. netif->mld_mac_filter(netif, &ip6_allnodes_ll, NETIF_ADD_MAC_FILTER);
  158. }
  159. #endif /* LWIP_IPV6_MLD */
  160. #endif /* LWIP_IPV6 */
  161. /* set default netif */
  162. if (netif_default == RT_NULL)
  163. netif_set_default(ethif->netif);
  164. #if LWIP_DHCP
  165. /* set interface up */
  166. netif_set_up(ethif->netif);
  167. /* if this interface uses DHCP, start the DHCP client */
  168. dhcp_start(ethif->netif);
  169. #else
  170. /* set interface up */
  171. netif_set_up(ethif->netif);
  172. #endif
  173. if (ethif->flags & ETHIF_LINK_PHYUP)
  174. {
  175. /* set link_up for this netif */
  176. netif_set_link_up(ethif->netif);
  177. }
  178. return ERR_OK;
  179. }
  180. return ERR_IF;
  181. }
  182. /* Keep old drivers compatible in RT-Thread */
  183. rt_err_t eth_device_init_with_flag(struct eth_device *dev, const char *name, rt_uint16_t flags)
  184. {
  185. struct netif* netif;
  186. netif = (struct netif*) rt_malloc (sizeof(struct netif));
  187. if (netif == RT_NULL)
  188. {
  189. rt_kprintf("malloc netif failed\n");
  190. return -RT_ERROR;
  191. }
  192. rt_memset(netif, 0, sizeof(struct netif));
  193. /* set netif */
  194. dev->netif = netif;
  195. /* device flags, which will be set to netif flags when initializing */
  196. dev->flags = flags;
  197. /* link changed status of device */
  198. dev->link_changed = 0x00;
  199. dev->parent.type = RT_Device_Class_NetIf;
  200. /* register to RT-Thread device manager */
  201. rt_device_register(&(dev->parent), name, RT_DEVICE_FLAG_RDWR);
  202. rt_sem_init(&(dev->tx_ack), name, 0, RT_IPC_FLAG_FIFO);
  203. /* set name */
  204. netif->name[0] = name[0];
  205. netif->name[1] = name[1];
  206. /* set hw address to 6 */
  207. netif->hwaddr_len = 6;
  208. /* maximum transfer unit */
  209. netif->mtu = ETHERNET_MTU;
  210. /* set linkoutput */
  211. netif->linkoutput = ethernetif_linkoutput;
  212. /* get hardware MAC address */
  213. rt_device_control(&(dev->parent), NIOCTL_GADDR, netif->hwaddr);
  214. #if LWIP_NETIF_HOSTNAME
  215. /* Initialize interface hostname */
  216. netif->hostname = "rtthread";
  217. #endif /* LWIP_NETIF_HOSTNAME */
  218. /* if tcp thread has been started up, we add this netif to the system */
  219. if (rt_thread_find("tcpip") != RT_NULL)
  220. {
  221. ip4_addr_t ipaddr, netmask, gw;
  222. #if !LWIP_DHCP
  223. ipaddr.addr = inet_addr(RT_LWIP_IPADDR);
  224. gw.addr = inet_addr(RT_LWIP_GWADDR);
  225. netmask.addr = inet_addr(RT_LWIP_MSKADDR);
  226. #else
  227. IP4_ADDR(&ipaddr, 0, 0, 0, 0);
  228. IP4_ADDR(&gw, 0, 0, 0, 0);
  229. IP4_ADDR(&netmask, 0, 0, 0, 0);
  230. #endif
  231. netifapi_netif_add(netif, &ipaddr, &netmask, &gw, dev, eth_netif_device_init, tcpip_input);
  232. }
  233. return RT_EOK;
  234. }
  235. rt_err_t eth_device_init(struct eth_device * dev, const char *name)
  236. {
  237. rt_uint16_t flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;
  238. #if LWIP_IGMP
  239. /* IGMP support */
  240. flags |= NETIF_FLAG_IGMP;
  241. #endif
  242. return eth_device_init_with_flag(dev, name, flags);
  243. }
  244. #ifndef LWIP_NO_RX_THREAD
  245. rt_err_t eth_device_ready(struct eth_device* dev)
  246. {
  247. if (dev->netif)
  248. /* post message to Ethernet thread */
  249. return rt_mb_send(&eth_rx_thread_mb, (rt_uint32_t)dev);
  250. else
  251. return ERR_OK; /* netif is not initialized yet, just return. */
  252. }
  253. rt_err_t eth_device_linkchange(struct eth_device* dev, rt_bool_t up)
  254. {
  255. rt_uint32_t level;
  256. RT_ASSERT(dev != RT_NULL);
  257. level = rt_hw_interrupt_disable();
  258. dev->link_changed = 0x01;
  259. if (up == RT_TRUE)
  260. dev->link_status = 0x01;
  261. else
  262. dev->link_status = 0x00;
  263. rt_hw_interrupt_enable(level);
  264. /* post message to ethernet thread */
  265. return rt_mb_send(&eth_rx_thread_mb, (rt_uint32_t)dev);
  266. }
  267. #else
  268. /* NOTE: please not use it in interrupt when no RxThread exist */
  269. rt_err_t eth_device_linkchange(struct eth_device* dev, rt_bool_t up)
  270. {
  271. if (up == RT_TRUE)
  272. netifapi_netif_set_link_up(dev->netif);
  273. else
  274. netifapi_netif_set_link_down(dev->netif);
  275. return RT_EOK;
  276. }
  277. #endif
  278. #ifndef LWIP_NO_TX_THREAD
  279. /* Ethernet Tx Thread */
  280. static void eth_tx_thread_entry(void* parameter)
  281. {
  282. struct eth_tx_msg* msg;
  283. while (1)
  284. {
  285. if (rt_mb_recv(&eth_tx_thread_mb, (rt_ubase_t *)&msg, RT_WAITING_FOREVER) == RT_EOK)
  286. {
  287. struct eth_device* enetif;
  288. RT_ASSERT(msg->netif != RT_NULL);
  289. RT_ASSERT(msg->buf != RT_NULL);
  290. enetif = (struct eth_device*)msg->netif->state;
  291. if (enetif != RT_NULL)
  292. {
  293. /* call driver's interface */
  294. if (enetif->eth_tx(&(enetif->parent), msg->buf) != RT_EOK)
  295. {
  296. /* transmit eth packet failed */
  297. }
  298. }
  299. /* send ACK */
  300. rt_sem_release(&(enetif->tx_ack));
  301. }
  302. }
  303. }
  304. #endif
  305. #ifndef LWIP_NO_RX_THREAD
  306. /* Ethernet Rx Thread */
  307. static void eth_rx_thread_entry(void* parameter)
  308. {
  309. struct eth_device* device;
  310. while (1)
  311. {
  312. if (rt_mb_recv(&eth_rx_thread_mb, (rt_ubase_t *)&device, RT_WAITING_FOREVER) == RT_EOK)
  313. {
  314. struct pbuf *p;
  315. /* check link status */
  316. if (device->link_changed)
  317. {
  318. int status;
  319. rt_uint32_t level;
  320. level = rt_hw_interrupt_disable();
  321. status = device->link_status;
  322. device->link_changed = 0x00;
  323. rt_hw_interrupt_enable(level);
  324. if (status)
  325. netifapi_netif_set_link_up(device->netif);
  326. else
  327. netifapi_netif_set_link_down(device->netif);
  328. }
  329. /* receive all of buffer */
  330. while (1)
  331. {
  332. if(device->eth_rx == RT_NULL) break;
  333. p = device->eth_rx(&(device->parent));
  334. if (p != RT_NULL)
  335. {
  336. /* notify to upper layer */
  337. if( device->netif->input(p, device->netif) != ERR_OK )
  338. {
  339. LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: Input error\n"));
  340. pbuf_free(p);
  341. p = NULL;
  342. }
  343. }
  344. else break;
  345. }
  346. }
  347. else
  348. {
  349. LWIP_ASSERT("Should not happen!\n",0);
  350. }
  351. }
  352. }
  353. #endif
  354. /* this function does not need,
  355. * use eth_system_device_init_private()
  356. * call by lwip_system_init().
  357. */
  358. int eth_system_device_init(void)
  359. {
  360. return 0;
  361. }
  362. int eth_system_device_init_private(void)
  363. {
  364. rt_err_t result = RT_EOK;
  365. /* initialize Rx thread. */
  366. #ifndef LWIP_NO_RX_THREAD
  367. /* initialize mailbox and create Ethernet Rx thread */
  368. result = rt_mb_init(&eth_rx_thread_mb, "erxmb",
  369. &eth_rx_thread_mb_pool[0], sizeof(eth_rx_thread_mb_pool)/4,
  370. RT_IPC_FLAG_FIFO);
  371. RT_ASSERT(result == RT_EOK);
  372. result = rt_thread_init(&eth_rx_thread, "erx", eth_rx_thread_entry, RT_NULL,
  373. &eth_rx_thread_stack[0], sizeof(eth_rx_thread_stack),
  374. RT_ETHERNETIF_THREAD_PREORITY, 16);
  375. RT_ASSERT(result == RT_EOK);
  376. result = rt_thread_startup(&eth_rx_thread);
  377. RT_ASSERT(result == RT_EOK);
  378. #endif
  379. /* initialize Tx thread */
  380. #ifndef LWIP_NO_TX_THREAD
  381. /* initialize mailbox and create Ethernet Tx thread */
  382. result = rt_mb_init(&eth_tx_thread_mb, "etxmb",
  383. &eth_tx_thread_mb_pool[0], sizeof(eth_tx_thread_mb_pool)/4,
  384. RT_IPC_FLAG_FIFO);
  385. RT_ASSERT(result == RT_EOK);
  386. result = rt_thread_init(&eth_tx_thread, "etx", eth_tx_thread_entry, RT_NULL,
  387. &eth_tx_thread_stack[0], sizeof(eth_tx_thread_stack),
  388. RT_ETHERNETIF_THREAD_PREORITY, 16);
  389. RT_ASSERT(result == RT_EOK);
  390. result = rt_thread_startup(&eth_tx_thread);
  391. RT_ASSERT(result == RT_EOK);
  392. #endif
  393. return (int)result;
  394. }
  395. #ifdef RT_USING_FINSH
  396. #include <finsh.h>
  397. void set_if(char* netif_name, char* ip_addr, char* gw_addr, char* nm_addr)
  398. {
  399. ip4_addr_t *ip;
  400. ip4_addr_t addr;
  401. struct netif * netif = netif_list;
  402. if(strlen(netif_name) > sizeof(netif->name))
  403. {
  404. rt_kprintf("network interface name too long!\r\n");
  405. return;
  406. }
  407. while(netif != RT_NULL)
  408. {
  409. if(strncmp(netif_name, netif->name, sizeof(netif->name)) == 0)
  410. break;
  411. netif = netif->next;
  412. if( netif == RT_NULL )
  413. {
  414. rt_kprintf("network interface: %s not found!\r\n", netif_name);
  415. return;
  416. }
  417. }
  418. ip = (ip4_addr_t *)&addr;
  419. /* set ip address */
  420. if ((ip_addr != RT_NULL) && ip4addr_aton(ip_addr, &addr))
  421. {
  422. netif_set_ipaddr(netif, ip);
  423. }
  424. /* set gateway address */
  425. if ((gw_addr != RT_NULL) && ip4addr_aton(gw_addr, &addr))
  426. {
  427. netif_set_gw(netif, ip);
  428. }
  429. /* set netmask address */
  430. if ((nm_addr != RT_NULL) && ip4addr_aton(nm_addr, &addr))
  431. {
  432. netif_set_netmask(netif, ip);
  433. }
  434. }
  435. FINSH_FUNCTION_EXPORT(set_if, set network interface address);
  436. #if LWIP_DNS
  437. #include <lwip/dns.h>
  438. void set_dns(char* dns_server)
  439. {
  440. ip_addr_t addr;
  441. if ((dns_server != RT_NULL) && ipaddr_aton(dns_server, &addr))
  442. {
  443. dns_setserver(0, &addr);
  444. }
  445. }
  446. FINSH_FUNCTION_EXPORT(set_dns, set DNS server address);
  447. #endif
  448. void list_if(void)
  449. {
  450. rt_ubase_t index;
  451. struct netif * netif;
  452. rt_enter_critical();
  453. netif = netif_list;
  454. while( netif != RT_NULL )
  455. {
  456. rt_kprintf("network interface: %c%c%s\n",
  457. netif->name[0],
  458. netif->name[1],
  459. (netif == netif_default)?" (Default)":"");
  460. rt_kprintf("MTU: %d\n", netif->mtu);
  461. rt_kprintf("MAC: ");
  462. for (index = 0; index < netif->hwaddr_len; index ++)
  463. rt_kprintf("%02x ", netif->hwaddr[index]);
  464. rt_kprintf("\nFLAGS:");
  465. if (netif->flags & NETIF_FLAG_UP) rt_kprintf(" UP");
  466. else rt_kprintf(" DOWN");
  467. if (netif->flags & NETIF_FLAG_LINK_UP) rt_kprintf(" LINK_UP");
  468. else rt_kprintf(" LINK_DOWN");
  469. if (netif->flags & NETIF_FLAG_ETHARP) rt_kprintf(" ETHARP");
  470. if (netif->flags & NETIF_FLAG_BROADCAST) rt_kprintf(" BROADCAST");
  471. if (netif->flags & NETIF_FLAG_IGMP) rt_kprintf(" IGMP");
  472. rt_kprintf("\n");
  473. rt_kprintf("ip address: %s\n", ipaddr_ntoa(&(netif->ip_addr)));
  474. rt_kprintf("gw address: %s\n", ipaddr_ntoa(&(netif->gw)));
  475. rt_kprintf("net mask : %s\n", ipaddr_ntoa(&(netif->netmask)));
  476. #if LWIP_IPV6
  477. {
  478. ip6_addr_t *addr;
  479. int addr_state;
  480. int i;
  481. addr = (ip6_addr_t *)&netif->ip6_addr[0];
  482. addr_state = netif->ip6_addr_state[0];
  483. rt_kprintf("\nipv6 link-local: %s state:%02X %s\n", ip6addr_ntoa(addr),
  484. addr_state, ip6_addr_isvalid(addr_state)?"VALID":"INVALID");
  485. for(i=1; i<LWIP_IPV6_NUM_ADDRESSES; i++)
  486. {
  487. addr = (ip6_addr_t *)&netif->ip6_addr[i];
  488. addr_state = netif->ip6_addr_state[i];
  489. rt_kprintf("ipv6[%d] address: %s state:%02X %s\n", i, ip6addr_ntoa(addr),
  490. addr_state, ip6_addr_isvalid(addr_state)?"VALID":"INVALID");
  491. }
  492. }
  493. rt_kprintf("\r\n");
  494. #endif /* LWIP_IPV6 */
  495. netif = netif->next;
  496. }
  497. #if LWIP_DNS
  498. {
  499. const ip_addr_t *ip_addr;
  500. for(index=0; index<DNS_MAX_SERVERS; index++)
  501. {
  502. ip_addr = dns_getserver(index);
  503. rt_kprintf("dns server #%d: %s\n", index, ipaddr_ntoa(ip_addr));
  504. }
  505. }
  506. #endif /**< #if LWIP_DNS */
  507. rt_exit_critical();
  508. }
  509. FINSH_FUNCTION_EXPORT(list_if, list network interface information);
  510. #if LWIP_TCP
  511. #include <lwip/tcp.h>
  512. #include <lwip/priv/tcp_priv.h>
  513. void list_tcps(void)
  514. {
  515. rt_uint32_t num = 0;
  516. struct tcp_pcb *pcb;
  517. char local_ip_str[16];
  518. char remote_ip_str[16];
  519. extern struct tcp_pcb *tcp_active_pcbs;
  520. extern union tcp_listen_pcbs_t tcp_listen_pcbs;
  521. extern struct tcp_pcb *tcp_tw_pcbs;
  522. rt_enter_critical();
  523. rt_kprintf("Active PCB states:\n");
  524. for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next)
  525. {
  526. strcpy(local_ip_str, ipaddr_ntoa(&(pcb->local_ip)));
  527. strcpy(remote_ip_str, ipaddr_ntoa(&(pcb->remote_ip)));
  528. rt_kprintf("#%d %s:%d <==> %s:%d snd_nxt 0x%08X rcv_nxt 0x%08X ",
  529. num++,
  530. local_ip_str,
  531. pcb->local_port,
  532. remote_ip_str,
  533. pcb->remote_port,
  534. pcb->snd_nxt,
  535. pcb->rcv_nxt);
  536. rt_kprintf("state: %s\n", tcp_debug_state_str(pcb->state));
  537. }
  538. rt_kprintf("Listen PCB states:\n");
  539. num = 0;
  540. for(pcb = (struct tcp_pcb *)tcp_listen_pcbs.pcbs; pcb != NULL; pcb = pcb->next)
  541. {
  542. rt_kprintf("#%d local port %d ", num++, pcb->local_port);
  543. rt_kprintf("state: %s\n", tcp_debug_state_str(pcb->state));
  544. }
  545. rt_kprintf("TIME-WAIT PCB states:\n");
  546. num = 0;
  547. for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next)
  548. {
  549. strcpy(local_ip_str, ipaddr_ntoa(&(pcb->local_ip)));
  550. strcpy(remote_ip_str, ipaddr_ntoa(&(pcb->remote_ip)));
  551. rt_kprintf("#%d %s:%d <==> %s:%d snd_nxt 0x%08X rcv_nxt 0x%08X ",
  552. num++,
  553. local_ip_str,
  554. pcb->local_port,
  555. remote_ip_str,
  556. pcb->remote_port,
  557. pcb->snd_nxt,
  558. pcb->rcv_nxt);
  559. rt_kprintf("state: %s\n", tcp_debug_state_str(pcb->state));
  560. }
  561. rt_exit_critical();
  562. }
  563. FINSH_FUNCTION_EXPORT(list_tcps, list all of tcp connections);
  564. #endif /* LWIP_TCP */
  565. #if LWIP_UDP
  566. #include "lwip/udp.h"
  567. void list_udps(void)
  568. {
  569. struct udp_pcb *pcb;
  570. rt_uint32_t num = 0;
  571. char local_ip_str[16];
  572. char remote_ip_str[16];
  573. rt_enter_critical();
  574. rt_kprintf("Active UDP PCB states:\n");
  575. for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next)
  576. {
  577. strcpy(local_ip_str, ipaddr_ntoa(&(pcb->local_ip)));
  578. strcpy(remote_ip_str, ipaddr_ntoa(&(pcb->remote_ip)));
  579. rt_kprintf("#%d %d %s:%d <==> %s:%d \n",
  580. num, (int)pcb->flags,
  581. local_ip_str,
  582. pcb->local_port,
  583. remote_ip_str,
  584. pcb->remote_port);
  585. num++;
  586. }
  587. rt_exit_critical();
  588. }
  589. FINSH_FUNCTION_EXPORT(list_udps, list all of udp connections);
  590. #endif /* LWIP_UDP */
  591. #endif