ethernetif.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2010-07-07 Bernard fix send mail to mailbox issue.
  9. * 2011-07-30 mbbill port lwIP 1.4.0 to RT-Thread
  10. * 2012-04-10 Bernard add more compatible with RT-Thread.
  11. * 2012-11-12 Bernard The network interface can be initialized
  12. * after lwIP initialization.
  13. * 2013-02-28 aozima fixed list_tcps bug: ipaddr_ntoa isn't reentrant.
  14. * 2016-08-18 Bernard port to lwIP 2.0.0
  15. * 2018-11-02 MurphyZhao port to lwIP 2.1.0
  16. * 2021-09-07 Grissiom fix eth_tx_msg ack bug
  17. * 2022-02-22 xiangxistu integrate v1.4.1 v2.0.3 and v2.1.2 porting layer
  18. */
  19. /*
  20. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  21. * All rights reserved.
  22. *
  23. * Redistribution and use in source and binary forms, with or without modification,
  24. * are permitted provided that the following conditions are met:
  25. *
  26. * 1. Redistributions of source code must retain the above copyright notice,
  27. * this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright notice,
  29. * this list of conditions and the following disclaimer in the documentation
  30. * and/or other materials provided with the distribution.
  31. * 3. The name of the author may not be used to endorse or promote products
  32. * derived from this software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  35. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  36. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  37. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  38. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  39. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  40. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  41. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  42. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  43. * OF SUCH DAMAGE.
  44. *
  45. * This file is part of the lwIP TCP/IP stack.
  46. *
  47. * Author: Adam Dunkels <adam@sics.se>
  48. */
  49. #include <string.h>
  50. #include <lwip/init.h>
  51. #include <lwip/opt.h>
  52. #include <lwip/debug.h>
  53. #include <lwip/def.h>
  54. #include <lwip/mem.h>
  55. #include <lwip/pbuf.h>
  56. #include <lwip/sys.h>
  57. #include <lwip/netif.h>
  58. #include <lwip/stats.h>
  59. #include <lwip/tcpip.h>
  60. #include <lwip/dhcp.h>
  61. #include <lwip/netifapi.h>
  62. #include <lwip/inet.h>
  63. #include <netif/etharp.h>
  64. #include <netif/ethernetif.h>
  65. #include <ipc/completion.h>
  66. #if LWIP_IPV6
  67. #include "lwip/ethip6.h"
  68. #endif /* LWIP_IPV6 */
  69. #define netifapi_netif_set_link_up(n) netifapi_netif_common(n, netif_set_link_up, NULL)
  70. #define netifapi_netif_set_link_down(n) netifapi_netif_common(n, netif_set_link_down, NULL)
  71. #ifndef RT_LWIP_ETHTHREAD_PRIORITY
  72. #define RT_ETHERNETIF_THREAD_PREORITY 0x90
  73. #else
  74. #define RT_ETHERNETIF_THREAD_PREORITY RT_LWIP_ETHTHREAD_PRIORITY
  75. #endif
  76. #ifndef LWIP_NO_TX_THREAD
  77. /**
  78. * Tx message structure for Ethernet interface
  79. */
  80. struct eth_tx_msg
  81. {
  82. struct netif *netif;
  83. struct pbuf *buf;
  84. struct rt_completion ack;
  85. };
  86. static struct rt_mailbox eth_tx_thread_mb;
  87. static struct rt_thread eth_tx_thread;
  88. #ifndef RT_LWIP_ETHTHREAD_MBOX_SIZE
  89. static char eth_tx_thread_mb_pool[32 * sizeof(rt_ubase_t)];
  90. static char eth_tx_thread_stack[512];
  91. #else
  92. static char eth_tx_thread_mb_pool[RT_LWIP_ETHTHREAD_MBOX_SIZE * sizeof(rt_ubase_t)];
  93. static char eth_tx_thread_stack[RT_LWIP_ETHTHREAD_STACKSIZE];
  94. #endif
  95. #endif
  96. #ifndef LWIP_NO_RX_THREAD
  97. static struct rt_mailbox eth_rx_thread_mb;
  98. static struct rt_thread eth_rx_thread;
  99. #ifndef RT_LWIP_ETHTHREAD_MBOX_SIZE
  100. static char eth_rx_thread_mb_pool[48 * sizeof(rt_ubase_t)];
  101. static char eth_rx_thread_stack[1024];
  102. #else
  103. static char eth_rx_thread_mb_pool[RT_LWIP_ETHTHREAD_MBOX_SIZE * sizeof(rt_ubase_t)];
  104. static char eth_rx_thread_stack[RT_LWIP_ETHTHREAD_STACKSIZE];
  105. #endif
  106. #endif
  107. #ifdef RT_USING_NETDEV
  108. #include "lwip/ip.h"
  109. #include "lwip/init.h"
  110. #include "lwip/netdb.h"
  111. #include <netdev.h>
  112. static int lwip_netdev_set_up(struct netdev *netif)
  113. {
  114. netif_set_up((struct netif *)netif->user_data);
  115. return ERR_OK;
  116. }
  117. static int lwip_netdev_set_down(struct netdev *netif)
  118. {
  119. netif_set_down((struct netif *)netif->user_data);
  120. return ERR_OK;
  121. }
  122. #ifndef ip_2_ip4
  123. #define ip_2_ip4(ipaddr) (ipaddr)
  124. #endif
  125. static int lwip_netdev_set_addr_info(struct netdev *netif, ip_addr_t *ip_addr, ip_addr_t *netmask, ip_addr_t *gw)
  126. {
  127. if (ip_addr && netmask && gw)
  128. {
  129. netif_set_addr((struct netif *)netif->user_data, ip_2_ip4(ip_addr), ip_2_ip4(netmask), ip_2_ip4(gw));
  130. }
  131. else
  132. {
  133. if (ip_addr)
  134. {
  135. netif_set_ipaddr((struct netif *)netif->user_data, ip_2_ip4(ip_addr));
  136. }
  137. if (netmask)
  138. {
  139. netif_set_netmask((struct netif *)netif->user_data, ip_2_ip4(netmask));
  140. }
  141. if (gw)
  142. {
  143. netif_set_gw((struct netif *)netif->user_data, ip_2_ip4(gw));
  144. }
  145. }
  146. return ERR_OK;
  147. }
  148. #ifdef RT_LWIP_DNS
  149. static int lwip_netdev_set_dns_server(struct netdev *netif, uint8_t dns_num, ip_addr_t *dns_server)
  150. {
  151. #if LWIP_VERSION_MAJOR == 1U /* v1.x */
  152. extern void dns_setserver(u8_t numdns, ip_addr_t *dnsserver);
  153. #else /* >=2.x */
  154. extern void dns_setserver(uint8_t dns_num, const ip_addr_t *dns_server);
  155. #endif /* LWIP_VERSION_MAJOR == 1U */
  156. dns_setserver(dns_num, dns_server);
  157. return ERR_OK;
  158. }
  159. #endif /* RT_LWIP_DNS */
  160. #ifdef RT_LWIP_DHCP
  161. static int lwip_netdev_set_dhcp(struct netdev *netif, rt_bool_t is_enabled)
  162. {
  163. netdev_low_level_set_dhcp_status(netif, is_enabled);
  164. if(RT_TRUE == is_enabled)
  165. {
  166. dhcp_start((struct netif *)netif->user_data);
  167. }
  168. else
  169. {
  170. dhcp_stop((struct netif *)netif->user_data);
  171. }
  172. return ERR_OK;
  173. }
  174. #endif /* RT_LWIP_DHCP */
  175. #ifdef RT_USING_FINSH
  176. #ifdef RT_LWIP_USING_PING
  177. extern int lwip_ping_recv(int s, int *ttl);
  178. extern err_t lwip_ping_send(int s, ip_addr_t *addr, int size);
  179. int lwip_netdev_ping(struct netdev *netif, const char *host, size_t data_len,
  180. uint32_t timeout, struct netdev_ping_resp *ping_resp)
  181. {
  182. int s, ttl, recv_len, result = 0;
  183. int elapsed_time;
  184. rt_tick_t recv_start_tick;
  185. #if LWIP_VERSION_MAJOR == 1U /* v1.x */
  186. int recv_timeout = timeout * 1000UL / RT_TICK_PER_SECOND;
  187. #else /* >= v2.x */
  188. struct timeval recv_timeout = { timeout / RT_TICK_PER_SECOND, timeout % RT_TICK_PER_SECOND };
  189. #endif
  190. ip_addr_t target_addr;
  191. struct addrinfo hint, *res = RT_NULL;
  192. struct sockaddr_in *h = RT_NULL;
  193. struct in_addr ina;
  194. struct sockaddr_in local;
  195. RT_ASSERT(netif);
  196. RT_ASSERT(host);
  197. RT_ASSERT(ping_resp);
  198. rt_memset(&hint, 0x00, sizeof(hint));
  199. /* convert URL to IP */
  200. if (lwip_getaddrinfo(host, RT_NULL, &hint, &res) != 0)
  201. {
  202. return -RT_ERROR;
  203. }
  204. SMEMCPY(&h, &res->ai_addr, sizeof(struct sockaddr_in *));
  205. SMEMCPY(&ina, &h->sin_addr, sizeof(ina));
  206. lwip_freeaddrinfo(res);
  207. if (inet_aton(inet_ntoa(ina), &target_addr) == 0)
  208. {
  209. return -RT_ERROR;
  210. }
  211. SMEMCPY(&(ping_resp->ip_addr), &target_addr, sizeof(ip_addr_t));
  212. /* new a socket */
  213. if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0)
  214. {
  215. return -RT_ERROR;
  216. }
  217. local.sin_len = sizeof(local);
  218. local.sin_family = AF_INET;
  219. local.sin_port = 0;
  220. local.sin_addr.s_addr = (netif->ip_addr.addr);
  221. lwip_bind(s, (struct sockaddr *)&local, sizeof(struct sockaddr_in));
  222. lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &recv_timeout, sizeof(recv_timeout));
  223. if (lwip_ping_send(s, &target_addr, data_len) == ERR_OK)
  224. {
  225. recv_start_tick = rt_tick_get();
  226. if ((recv_len = lwip_ping_recv(s, &ttl)) >= 0)
  227. {
  228. elapsed_time = (rt_tick_get() - recv_start_tick) * 1000UL / RT_TICK_PER_SECOND;
  229. ping_resp->data_len = recv_len;
  230. ping_resp->ttl = ttl;
  231. ping_resp->ticks = elapsed_time;
  232. }
  233. else
  234. {
  235. result = -RT_ETIMEOUT;
  236. goto __exit;
  237. }
  238. }
  239. else
  240. {
  241. result = -RT_ETIMEOUT;
  242. goto __exit;
  243. }
  244. __exit:
  245. lwip_close(s);
  246. return result;
  247. }
  248. #endif /* RT_LWIP_USING_PING */
  249. #if defined (RT_LWIP_TCP) || defined (RT_LWIP_UDP)
  250. void lwip_netdev_netstat(struct netdev *netif)
  251. {
  252. extern void list_tcps(void);
  253. extern void list_udps(void);
  254. #ifdef RT_LWIP_TCP
  255. list_tcps();
  256. #endif
  257. #ifdef RT_LWIP_UDP
  258. list_udps();
  259. #endif
  260. }
  261. #endif /* RT_LWIP_TCP || RT_LWIP_UDP */
  262. #endif /* RT_USING_FINSH */
  263. static int lwip_netdev_set_default(struct netdev *netif)
  264. {
  265. netif_set_default((struct netif *)netif->user_data);
  266. return ERR_OK;
  267. }
  268. const struct netdev_ops lwip_netdev_ops =
  269. {
  270. lwip_netdev_set_up,
  271. lwip_netdev_set_down,
  272. lwip_netdev_set_addr_info,
  273. #ifdef RT_LWIP_DNS
  274. lwip_netdev_set_dns_server,
  275. #else
  276. NULL,
  277. #endif /* RT_LWIP_DNS */
  278. #ifdef RT_LWIP_DHCP
  279. lwip_netdev_set_dhcp,
  280. #else
  281. NULL,
  282. #endif /* RT_LWIP_DHCP */
  283. #ifdef RT_USING_FINSH
  284. #ifdef RT_LWIP_USING_PING
  285. lwip_netdev_ping,
  286. #else
  287. NULL,
  288. #endif /* RT_LWIP_USING_PING */
  289. #if defined (RT_LWIP_TCP) || defined (RT_LWIP_UDP)
  290. lwip_netdev_netstat,
  291. #endif /* RT_LWIP_TCP || RT_LWIP_UDP */
  292. #endif /* RT_USING_FINSH */
  293. lwip_netdev_set_default,
  294. };
  295. /* synchronize lwIP network interface device and network interface device flags */
  296. static int netdev_flags_sync(struct netif *lwip_netif)
  297. {
  298. struct netdev *netdev = NULL;
  299. RT_ASSERT(lwip_netif);
  300. netdev = netdev_get_by_name(lwip_netif->name);
  301. if (netdev == RT_NULL)
  302. {
  303. return -ERR_IF;
  304. }
  305. netdev->mtu = lwip_netif->mtu;
  306. /* the macro definition is different from lwip-1.4.1 and lwip-2.x.x about 'flags'. */
  307. if(lwip_netif->flags & NETIF_FLAG_BROADCAST)
  308. {
  309. netdev->flags |= NETDEV_FLAG_BROADCAST;
  310. }
  311. if(lwip_netif->flags & NETIF_FLAG_ETHARP)
  312. {
  313. netdev->flags |= NETDEV_FLAG_ETHARP;
  314. }
  315. if(lwip_netif->flags & NETIF_FLAG_IGMP)
  316. {
  317. netdev->flags |= NETDEV_FLAG_IGMP;
  318. }
  319. #if LWIP_VERSION_MAJOR >= 2U /* >= v2.x */
  320. if(lwip_netif->flags & NETIF_FLAG_MLD6)
  321. {
  322. netdev->flags |= NETDEV_FLAG_MLD6;
  323. }
  324. #endif /* LWIP_VERSION_MAJOR >= 2U */
  325. #if LWIP_DHCP
  326. netdev_low_level_set_dhcp_status(netdev, RT_TRUE);
  327. #else
  328. netdev_low_level_set_dhcp_status(netdev, RT_FALSE);
  329. #endif
  330. return ERR_OK;
  331. }
  332. static int netdev_add(struct netif *lwip_netif)
  333. {
  334. #define LWIP_NETIF_NAME_LEN 2
  335. int result = 0;
  336. struct netdev *netdev = RT_NULL;
  337. char name[LWIP_NETIF_NAME_LEN + 1] = {0};
  338. RT_ASSERT(lwip_netif);
  339. netdev = (struct netdev *)rt_calloc(1, sizeof(struct netdev));
  340. if (netdev == RT_NULL)
  341. {
  342. return -ERR_IF;
  343. }
  344. #ifdef SAL_USING_LWIP
  345. extern int sal_lwip_netdev_set_pf_info(struct netdev *netdev);
  346. /* set the lwIP network interface device protocol family information */
  347. sal_lwip_netdev_set_pf_info(netdev);
  348. #endif /* SAL_USING_LWIP */
  349. rt_strncpy(name, lwip_netif->name, LWIP_NETIF_NAME_LEN);
  350. result = netdev_register(netdev, name, (void *)lwip_netif);
  351. /* Update netdev info after registered */
  352. netdev_flags_sync(lwip_netif);
  353. netdev->ops = &lwip_netdev_ops;
  354. netdev->hwaddr_len = lwip_netif->hwaddr_len;
  355. SMEMCPY(netdev->hwaddr, lwip_netif->hwaddr, lwip_netif->hwaddr_len);
  356. netdev->ip_addr = lwip_netif->ip_addr;
  357. netdev->gw = lwip_netif->gw;
  358. netdev->netmask = lwip_netif->netmask;
  359. return result;
  360. }
  361. static void netdev_del(struct netif *lwip_netif)
  362. {
  363. char name[LWIP_NETIF_NAME_LEN + 1];
  364. struct netdev *netdev;
  365. RT_ASSERT(lwip_netif);
  366. rt_strncpy(name, lwip_netif->name, LWIP_NETIF_NAME_LEN);
  367. netdev = netdev_get_by_name(name);
  368. netdev_unregister(netdev);
  369. rt_free(netdev);
  370. }
  371. #endif /* RT_USING_NETDEV */
  372. static err_t ethernetif_linkoutput(struct netif *netif, struct pbuf *p)
  373. {
  374. #ifndef LWIP_NO_TX_THREAD
  375. struct eth_tx_msg msg;
  376. RT_ASSERT(netif != RT_NULL);
  377. /* send a message to eth tx thread */
  378. msg.netif = netif;
  379. msg.buf = p;
  380. rt_completion_init(&msg.ack);
  381. if (rt_mb_send(&eth_tx_thread_mb, (rt_ubase_t) &msg) == RT_EOK)
  382. {
  383. /* waiting for ack */
  384. rt_completion_wait(&msg.ack, RT_WAITING_FOREVER);
  385. }
  386. #else
  387. struct eth_device* enetif;
  388. RT_ASSERT(netif != RT_NULL);
  389. enetif = (struct eth_device*)netif->state;
  390. if (enetif->eth_tx(&(enetif->parent), p) != RT_EOK)
  391. {
  392. return ERR_IF;
  393. }
  394. #endif
  395. return ERR_OK;
  396. }
  397. static err_t eth_netif_device_init(struct netif *netif)
  398. {
  399. struct eth_device *ethif;
  400. ethif = (struct eth_device*)netif->state;
  401. if (ethif != RT_NULL)
  402. {
  403. rt_device_t device;
  404. #ifdef RT_USING_NETDEV
  405. /* network interface device register */
  406. netdev_add(netif);
  407. #endif /* RT_USING_NETDEV */
  408. /* get device object */
  409. device = (rt_device_t) ethif;
  410. if (rt_device_init(device) != RT_EOK)
  411. {
  412. return ERR_IF;
  413. }
  414. if (rt_device_open(device, RT_DEVICE_FLAG_RDWR) != RT_EOK)
  415. {
  416. return ERR_IF;
  417. }
  418. /* copy device flags to netif flags */
  419. netif->flags = (ethif->flags & 0xff);
  420. netif->mtu = ETHERNET_MTU;
  421. /* set output */
  422. netif->output = etharp_output;
  423. #if LWIP_IPV6
  424. netif->output_ip6 = ethip6_output;
  425. netif->ip6_autoconfig_enabled = 1;
  426. netif_create_ip6_linklocal_address(netif, 1);
  427. #if LWIP_IPV6_MLD
  428. netif->flags |= NETIF_FLAG_MLD6;
  429. /*
  430. * For hardware/netifs that implement MAC filtering.
  431. * All-nodes link-local is handled by default, so we must let the hardware know
  432. * to allow multicast packets in.
  433. * Should set mld_mac_filter previously. */
  434. if (netif->mld_mac_filter != NULL)
  435. {
  436. ip6_addr_t ip6_allnodes_ll;
  437. ip6_addr_set_allnodes_linklocal(&ip6_allnodes_ll);
  438. netif->mld_mac_filter(netif, &ip6_allnodes_ll, NETIF_ADD_MAC_FILTER);
  439. }
  440. #endif /* LWIP_IPV6_MLD */
  441. #endif /* LWIP_IPV6 */
  442. /* set default netif */
  443. if (netif_default == RT_NULL)
  444. netif_set_default(ethif->netif);
  445. /* set interface up */
  446. netif_set_up(ethif->netif);
  447. #if LWIP_DHCP
  448. /* if this interface uses DHCP, start the DHCP client */
  449. dhcp_start(ethif->netif);
  450. #endif
  451. if (ethif->flags & ETHIF_LINK_PHYUP)
  452. {
  453. /* set link_up for this netif */
  454. netif_set_link_up(ethif->netif);
  455. }
  456. #ifdef RT_USING_NETDEV
  457. /* network interface device flags synchronize */
  458. netdev_flags_sync(netif);
  459. #endif /* RT_USING_NETDEV */
  460. return ERR_OK;
  461. }
  462. return ERR_IF;
  463. }
  464. /* Keep old drivers compatible in RT-Thread */
  465. rt_err_t eth_device_init_with_flag(struct eth_device *dev, const char *name, rt_uint16_t flags)
  466. {
  467. struct netif* netif;
  468. #if LWIP_NETIF_HOSTNAME
  469. #define LWIP_HOSTNAME_LEN 16
  470. char *hostname = RT_NULL;
  471. netif = (struct netif*) rt_calloc (1, sizeof(struct netif) + LWIP_HOSTNAME_LEN);
  472. #else
  473. netif = (struct netif*) rt_calloc (1, sizeof(struct netif));
  474. #endif
  475. if (netif == RT_NULL)
  476. {
  477. rt_kprintf("malloc netif failed\n");
  478. return -RT_ERROR;
  479. }
  480. /* set netif */
  481. dev->netif = netif;
  482. dev->flags = flags;
  483. /* link changed status of device */
  484. dev->link_changed = 0x00;
  485. /* avoid send the same mail to mailbox */
  486. dev->rx_notice = 0x00;
  487. dev->parent.type = RT_Device_Class_NetIf;
  488. /* register to RT-Thread device manager */
  489. rt_device_register(&(dev->parent), name, RT_DEVICE_FLAG_RDWR);
  490. /* set name */
  491. netif->name[0] = name[0];
  492. netif->name[1] = name[1];
  493. /* set hw address to 6 */
  494. netif->hwaddr_len = 6;
  495. /* maximum transfer unit */
  496. netif->mtu = ETHERNET_MTU;
  497. /* set linkoutput */
  498. netif->linkoutput = ethernetif_linkoutput;
  499. /* get hardware MAC address */
  500. rt_device_control(&(dev->parent), NIOCTL_GADDR, netif->hwaddr);
  501. #if LWIP_NETIF_HOSTNAME
  502. /* Initialize interface hostname */
  503. hostname = (char *)netif + sizeof(struct netif);
  504. rt_sprintf(hostname, "rtthread_%02x%02x", name[0], name[1]);
  505. netif->hostname = hostname;
  506. #endif /* LWIP_NETIF_HOSTNAME */
  507. /* if tcp thread has been started up, we add this netif to the system */
  508. if (rt_thread_find("tcpip") != RT_NULL)
  509. {
  510. #if LWIP_VERSION_MAJOR == 1U /* v1.x */
  511. struct ip_addr ipaddr, netmask, gw;
  512. #else /* >= v2.x */
  513. ip4_addr_t ipaddr, netmask, gw;
  514. #endif /* LWIP_VERSION_MAJOR == 1U */
  515. #if !LWIP_DHCP
  516. ipaddr.addr = inet_addr(RT_LWIP_IPADDR);
  517. gw.addr = inet_addr(RT_LWIP_GWADDR);
  518. netmask.addr = inet_addr(RT_LWIP_MSKADDR);
  519. #else
  520. IP4_ADDR(&ipaddr, 0, 0, 0, 0);
  521. IP4_ADDR(&gw, 0, 0, 0, 0);
  522. IP4_ADDR(&netmask, 0, 0, 0, 0);
  523. #endif
  524. netifapi_netif_add(netif, &ipaddr, &netmask, &gw, dev, eth_netif_device_init, tcpip_input);
  525. }
  526. return RT_EOK;
  527. }
  528. rt_err_t eth_device_init(struct eth_device * dev, const char *name)
  529. {
  530. rt_uint16_t flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;
  531. #if LWIP_IGMP
  532. /* IGMP support */
  533. flags |= NETIF_FLAG_IGMP;
  534. #endif
  535. return eth_device_init_with_flag(dev, name, flags);
  536. }
  537. void eth_device_deinit(struct eth_device *dev)
  538. {
  539. struct netif* netif = dev->netif;
  540. #if LWIP_DHCP
  541. dhcp_stop(netif);
  542. dhcp_cleanup(netif);
  543. #endif
  544. netif_set_down(netif);
  545. netif_remove(netif);
  546. #ifdef RT_USING_NETDEV
  547. netdev_del(netif);
  548. #endif
  549. rt_device_close(&(dev->parent));
  550. rt_device_unregister(&(dev->parent));
  551. rt_free(netif);
  552. }
  553. #ifdef SAL_USING_AF_UNIX /* create loopback netdev */
  554. static err_t af_unix_eth_netif_device_init(struct netif *netif)
  555. {
  556. struct eth_device *ethif;
  557. ethif = (struct eth_device*)netif->state;
  558. if (ethif != RT_NULL)
  559. {
  560. rt_device_t device;
  561. #ifdef RT_USING_NETDEV
  562. /* network interface device register */
  563. netdev_add(netif);
  564. #endif /* RT_USING_NETDEV */
  565. /* get device object */
  566. device = (rt_device_t) ethif;
  567. if (rt_device_init(device) != RT_EOK)
  568. {
  569. return ERR_IF;
  570. }
  571. if (rt_device_open(device, RT_DEVICE_FLAG_RDWR) != RT_EOK)
  572. {
  573. return ERR_IF;
  574. }
  575. /* copy device flags to netif flags */
  576. netif->flags = (ethif->flags & 0xff);
  577. netif->mtu = ETHERNET_MTU;
  578. /* set output */
  579. netif->output = etharp_output;
  580. #if LWIP_IPV6
  581. netif->output_ip6 = ethip6_output;
  582. netif->ip6_autoconfig_enabled = 1;
  583. netif_create_ip6_linklocal_address(netif, 1);
  584. #if LWIP_IPV6_MLD
  585. netif->flags |= NETIF_FLAG_MLD6;
  586. /*
  587. * For hardware/netifs that implement MAC filtering.
  588. * All-nodes link-local is handled by default, so we must let the hardware know
  589. * to allow multicast packets in.
  590. * Should set mld_mac_filter previously. */
  591. if (netif->mld_mac_filter != NULL)
  592. {
  593. ip6_addr_t ip6_allnodes_ll;
  594. ip6_addr_set_allnodes_linklocal(&ip6_allnodes_ll);
  595. netif->mld_mac_filter(netif, &ip6_allnodes_ll, NETIF_ADD_MAC_FILTER);
  596. }
  597. #endif /* LWIP_IPV6_MLD */
  598. #endif /* LWIP_IPV6 */
  599. /* set default netif */
  600. if (netif_default == RT_NULL)
  601. netif_set_default(ethif->netif);
  602. /* set interface up */
  603. netif_set_up(ethif->netif);
  604. if (ethif->flags & ETHIF_LINK_PHYUP)
  605. {
  606. /* set link_up for this netif */
  607. netif_set_link_up(ethif->netif);
  608. }
  609. #ifdef RT_USING_NETDEV
  610. /* network interface device flags synchronize */
  611. netdev_flags_sync(netif);
  612. #endif /* RT_USING_NETDEV */
  613. return ERR_OK;
  614. }
  615. return ERR_IF;
  616. }
  617. /* Keep old drivers compatible in RT-Thread */
  618. rt_err_t af_unix_eth_device_init_with_flag(struct eth_device *dev, const char *name, rt_uint16_t flags)
  619. {
  620. struct netif* netif;
  621. #if LWIP_NETIF_HOSTNAME
  622. #define LWIP_HOSTNAME_LEN 16
  623. char *hostname = RT_NULL;
  624. netif = (struct netif*) rt_calloc (1, sizeof(struct netif) + LWIP_HOSTNAME_LEN);
  625. #else
  626. netif = (struct netif*) rt_calloc (1, sizeof(struct netif));
  627. #endif
  628. if (netif == RT_NULL)
  629. {
  630. rt_kprintf("malloc netif failed\n");
  631. return -RT_ERROR;
  632. }
  633. /* set netif */
  634. dev->netif = netif;
  635. dev->flags = flags;
  636. /* link changed status of device */
  637. dev->link_changed = 0x00;
  638. /* avoid send the same mail to mailbox */
  639. dev->rx_notice = 0x00;
  640. dev->parent.type = RT_Device_Class_NetIf;
  641. /* register to RT-Thread device manager */
  642. rt_device_register(&(dev->parent), name, RT_DEVICE_FLAG_RDWR);
  643. /* set name */
  644. netif->name[0] = name[0];
  645. netif->name[1] = name[1];
  646. /* set hw address to 6 */
  647. netif->hwaddr_len = 6;
  648. /* maximum transfer unit */
  649. netif->mtu = ETHERNET_MTU;
  650. /* set linkoutput */
  651. netif->linkoutput = ethernetif_linkoutput;
  652. /* get hardware MAC address */
  653. rt_device_control(&(dev->parent), NIOCTL_GADDR, netif->hwaddr);
  654. #if LWIP_NETIF_HOSTNAME
  655. /* Initialize interface hostname */
  656. hostname = (char *)netif + sizeof(struct netif);
  657. rt_sprintf(hostname, "rtthread_%02x%02x", name[0], name[1]);
  658. netif->hostname = hostname;
  659. #endif /* LWIP_NETIF_HOSTNAME */
  660. /* if tcp thread has been started up, we add this netif to the system */
  661. if (rt_thread_find("tcpip") != RT_NULL)
  662. {
  663. #if LWIP_VERSION_MAJOR == 1U /* v1.x */
  664. struct ip_addr ipaddr, netmask, gw;
  665. #else /* >= v2.x */
  666. ip4_addr_t ipaddr, netmask, gw;
  667. #endif /* LWIP_VERSION_MAJOR == 1U */
  668. ipaddr.addr = inet_addr("127.0.0.1");
  669. gw.addr = inet_addr("255.0.0.0");
  670. netmask.addr = inet_addr("127.0.0.1");
  671. netifapi_netif_add(netif, &ipaddr, &netmask, &gw, dev, af_unix_eth_netif_device_init, tcpip_input);
  672. }
  673. return RT_EOK;
  674. }
  675. rt_err_t af_unix_eth_device_init(struct eth_device * dev, const char *name)
  676. {
  677. rt_uint16_t flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;
  678. #if LWIP_IGMP
  679. /* IGMP support */
  680. flags |= NETIF_FLAG_IGMP;
  681. #endif
  682. return af_unix_eth_device_init_with_flag(dev, name, flags);
  683. }
  684. #endif /* SAL_USING_AF_UNIX */
  685. #ifndef LWIP_NO_RX_THREAD
  686. rt_err_t eth_device_ready(struct eth_device* dev)
  687. {
  688. if (dev->netif)
  689. {
  690. if(dev->rx_notice == RT_FALSE)
  691. {
  692. dev->rx_notice = RT_TRUE;
  693. return rt_mb_send(&eth_rx_thread_mb, (rt_ubase_t)dev);
  694. }
  695. else
  696. return RT_EOK;
  697. /* post message to Ethernet thread */
  698. }
  699. else
  700. return -RT_ERROR; /* netif is not initialized yet, just return. */
  701. }
  702. rt_err_t eth_device_linkchange(struct eth_device* dev, rt_bool_t up)
  703. {
  704. rt_base_t level;
  705. RT_ASSERT(dev != RT_NULL);
  706. level = rt_hw_interrupt_disable();
  707. dev->link_changed = 0x01;
  708. if (up == RT_TRUE)
  709. dev->link_status = 0x01;
  710. else
  711. dev->link_status = 0x00;
  712. rt_hw_interrupt_enable(level);
  713. /* post message to ethernet thread */
  714. return rt_mb_send(&eth_rx_thread_mb, (rt_ubase_t)dev);
  715. }
  716. #else
  717. /* NOTE: please not use it in interrupt when no RxThread exist */
  718. rt_err_t eth_device_linkchange(struct eth_device* dev, rt_bool_t up)
  719. {
  720. if (up == RT_TRUE)
  721. netifapi_netif_set_link_up(dev->netif);
  722. else
  723. netifapi_netif_set_link_down(dev->netif);
  724. return RT_EOK;
  725. }
  726. #endif
  727. #ifndef LWIP_NO_TX_THREAD
  728. /* Ethernet Tx Thread */
  729. static void eth_tx_thread_entry(void* parameter)
  730. {
  731. struct eth_tx_msg* msg;
  732. while (1)
  733. {
  734. if (rt_mb_recv(&eth_tx_thread_mb, (rt_ubase_t *)&msg, RT_WAITING_FOREVER) == RT_EOK)
  735. {
  736. struct eth_device* enetif;
  737. RT_ASSERT(msg->netif != RT_NULL);
  738. RT_ASSERT(msg->buf != RT_NULL);
  739. enetif = (struct eth_device*)msg->netif->state;
  740. if (enetif != RT_NULL)
  741. {
  742. /* call driver's interface */
  743. if (enetif->eth_tx(&(enetif->parent), msg->buf) != RT_EOK)
  744. {
  745. /* transmit eth packet failed */
  746. }
  747. }
  748. /* send ACK */
  749. rt_completion_done(&msg->ack);
  750. }
  751. }
  752. }
  753. #endif
  754. #ifndef LWIP_NO_RX_THREAD
  755. /* Ethernet Rx Thread */
  756. static void eth_rx_thread_entry(void* parameter)
  757. {
  758. struct eth_device* device;
  759. while (1)
  760. {
  761. if (rt_mb_recv(&eth_rx_thread_mb, (rt_ubase_t *)&device, RT_WAITING_FOREVER) == RT_EOK)
  762. {
  763. rt_base_t level;
  764. struct pbuf *p;
  765. /* check link status */
  766. if (device->link_changed)
  767. {
  768. int status;
  769. level = rt_hw_interrupt_disable();
  770. status = device->link_status;
  771. device->link_changed = 0x00;
  772. rt_hw_interrupt_enable(level);
  773. if (status)
  774. netifapi_netif_set_link_up(device->netif);
  775. else
  776. netifapi_netif_set_link_down(device->netif);
  777. }
  778. level = rt_hw_interrupt_disable();
  779. /* 'rx_notice' will be modify in the interrupt or here */
  780. device->rx_notice = RT_FALSE;
  781. rt_hw_interrupt_enable(level);
  782. /* receive all of buffer */
  783. while (1)
  784. {
  785. if(device->eth_rx == RT_NULL) break;
  786. p = device->eth_rx(&(device->parent));
  787. if (p != RT_NULL)
  788. {
  789. /* notify to upper layer */
  790. if( device->netif->input(p, device->netif) != ERR_OK )
  791. {
  792. LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: Input error\n"));
  793. pbuf_free(p);
  794. p = NULL;
  795. }
  796. }
  797. else break;
  798. }
  799. }
  800. else
  801. {
  802. LWIP_ASSERT("Should not happen!\n",0);
  803. }
  804. }
  805. }
  806. #endif
  807. /* this function does not need,
  808. * use eth_system_device_init_private()
  809. * call by lwip_system_init().
  810. */
  811. int eth_system_device_init(void)
  812. {
  813. return 0;
  814. }
  815. int eth_system_device_init_private(void)
  816. {
  817. rt_err_t result = RT_EOK;
  818. /* initialize Rx thread. */
  819. #ifndef LWIP_NO_RX_THREAD
  820. /* initialize mailbox and create Ethernet Rx thread */
  821. result = rt_mb_init(&eth_rx_thread_mb, "erxmb",
  822. &eth_rx_thread_mb_pool[0], sizeof(eth_rx_thread_mb_pool)/sizeof(rt_ubase_t),
  823. RT_IPC_FLAG_FIFO);
  824. RT_ASSERT(result == RT_EOK);
  825. result = rt_thread_init(&eth_rx_thread, "erx", eth_rx_thread_entry, RT_NULL,
  826. &eth_rx_thread_stack[0], sizeof(eth_rx_thread_stack),
  827. RT_ETHERNETIF_THREAD_PREORITY, 16);
  828. RT_ASSERT(result == RT_EOK);
  829. result = rt_thread_startup(&eth_rx_thread);
  830. RT_ASSERT(result == RT_EOK);
  831. #endif
  832. /* initialize Tx thread */
  833. #ifndef LWIP_NO_TX_THREAD
  834. /* initialize mailbox and create Ethernet Tx thread */
  835. result = rt_mb_init(&eth_tx_thread_mb, "etxmb",
  836. &eth_tx_thread_mb_pool[0], sizeof(eth_tx_thread_mb_pool)/sizeof(rt_ubase_t),
  837. RT_IPC_FLAG_FIFO);
  838. RT_ASSERT(result == RT_EOK);
  839. result = rt_thread_init(&eth_tx_thread, "etx", eth_tx_thread_entry, RT_NULL,
  840. &eth_tx_thread_stack[0], sizeof(eth_tx_thread_stack),
  841. RT_ETHERNETIF_THREAD_PREORITY, 16);
  842. RT_ASSERT(result == RT_EOK);
  843. result = rt_thread_startup(&eth_tx_thread);
  844. RT_ASSERT(result == RT_EOK);
  845. #endif
  846. return (int)result;
  847. }
  848. void set_if(char* netif_name, char* ip_addr, char* gw_addr, char* nm_addr)
  849. {
  850. #if LWIP_VERSION_MAJOR == 1U /* v1.x */
  851. struct ip_addr *ip;
  852. struct ip_addr addr;
  853. #else /* >= v2.x */
  854. ip4_addr_t *ip;
  855. ip4_addr_t addr;
  856. #endif /* LWIP_VERSION_MAJOR == 1U */
  857. struct netif * netif = netif_list;
  858. if(strlen(netif_name) > sizeof(netif->name))
  859. {
  860. rt_kprintf("network interface name too long!\r\n");
  861. return;
  862. }
  863. while(netif != RT_NULL)
  864. {
  865. if(strncmp(netif_name, netif->name, sizeof(netif->name)) == 0)
  866. break;
  867. netif = netif->next;
  868. if( netif == RT_NULL )
  869. {
  870. rt_kprintf("network interface: %s not found!\r\n", netif_name);
  871. return;
  872. }
  873. }
  874. #if LWIP_VERSION_MAJOR == 1U /* v1.x */
  875. ip = (struct ip_addr *)&addr;
  876. #else /* >= v2.x */
  877. ip = (ip4_addr_t *)&addr;
  878. #endif /* LWIP_VERSION_MAJOR == 1U */
  879. /* set ip address */
  880. if ((ip_addr != RT_NULL) && inet_aton(ip_addr, &addr))
  881. {
  882. netif_set_ipaddr(netif, ip);
  883. }
  884. /* set gateway address */
  885. if ((gw_addr != RT_NULL) && inet_aton(gw_addr, &addr))
  886. {
  887. netif_set_gw(netif, ip);
  888. }
  889. /* set netmask address */
  890. if ((nm_addr != RT_NULL) && inet_aton(nm_addr, &addr))
  891. {
  892. netif_set_netmask(netif, ip);
  893. }
  894. }
  895. #ifdef RT_USING_FINSH
  896. #include <finsh.h>
  897. FINSH_FUNCTION_EXPORT(set_if, set network interface address);
  898. #if LWIP_DNS
  899. #include <lwip/dns.h>
  900. void set_dns(uint8_t dns_num, char* dns_server)
  901. {
  902. ip_addr_t addr;
  903. if ((dns_server != RT_NULL) && ipaddr_aton(dns_server, &addr))
  904. {
  905. dns_setserver(dns_num, &addr);
  906. }
  907. }
  908. FINSH_FUNCTION_EXPORT(set_dns, set DNS server address);
  909. #endif
  910. void list_if(void)
  911. {
  912. rt_uint8_t index;
  913. struct netif * netif;
  914. rt_enter_critical();
  915. netif = netif_list;
  916. while( netif != RT_NULL )
  917. {
  918. rt_kprintf("network interface: %c%c%s\n",
  919. netif->name[0],
  920. netif->name[1],
  921. (netif == netif_default)?" (Default)":"");
  922. rt_kprintf("MTU: %d\n", netif->mtu);
  923. rt_kprintf("MAC: ");
  924. for (index = 0; index < netif->hwaddr_len; index ++)
  925. rt_kprintf("%02x ", netif->hwaddr[index]);
  926. rt_kprintf("\nFLAGS:");
  927. if (netif->flags & NETIF_FLAG_UP) rt_kprintf(" UP");
  928. else rt_kprintf(" DOWN");
  929. if (netif->flags & NETIF_FLAG_LINK_UP) rt_kprintf(" LINK_UP");
  930. else rt_kprintf(" LINK_DOWN");
  931. if (netif->flags & NETIF_FLAG_ETHARP) rt_kprintf(" ETHARP");
  932. if (netif->flags & NETIF_FLAG_BROADCAST) rt_kprintf(" BROADCAST");
  933. if (netif->flags & NETIF_FLAG_IGMP) rt_kprintf(" IGMP");
  934. rt_kprintf("\n");
  935. rt_kprintf("ip address: %s\n", ipaddr_ntoa(&(netif->ip_addr)));
  936. rt_kprintf("gw address: %s\n", ipaddr_ntoa(&(netif->gw)));
  937. rt_kprintf("net mask : %s\n", ipaddr_ntoa(&(netif->netmask)));
  938. #if LWIP_IPV6
  939. {
  940. ip6_addr_t *addr;
  941. int addr_state;
  942. int i;
  943. addr = (ip6_addr_t *)&netif->ip6_addr[0];
  944. addr_state = netif->ip6_addr_state[0];
  945. rt_kprintf("\nipv6 link-local: %s state:%02X %s\n", ip6addr_ntoa(addr),
  946. addr_state, ip6_addr_isvalid(addr_state)?"VALID":"INVALID");
  947. for(i=1; i<LWIP_IPV6_NUM_ADDRESSES; i++)
  948. {
  949. addr = (ip6_addr_t *)&netif->ip6_addr[i];
  950. addr_state = netif->ip6_addr_state[i];
  951. rt_kprintf("ipv6[%d] address: %s state:%02X %s\n", i, ip6addr_ntoa(addr),
  952. addr_state, ip6_addr_isvalid(addr_state)?"VALID":"INVALID");
  953. }
  954. }
  955. rt_kprintf("\r\n");
  956. #endif /* LWIP_IPV6 */
  957. netif = netif->next;
  958. }
  959. #if LWIP_DNS
  960. {
  961. #if LWIP_VERSION_MAJOR == 1U /* v1.x */
  962. struct ip_addr ip_addr;
  963. for(index=0; index<DNS_MAX_SERVERS; index++)
  964. {
  965. ip_addr = dns_getserver(index);
  966. rt_kprintf("dns server #%d: %s\n", index, ipaddr_ntoa(&(ip_addr)));
  967. }
  968. #else /* >= v2.x */
  969. const ip_addr_t *ip_addr;
  970. for(index=0; index<DNS_MAX_SERVERS; index++)
  971. {
  972. ip_addr = dns_getserver(index);
  973. rt_kprintf("dns server #%d: %s\n", index, inet_ntoa(ip_addr));
  974. }
  975. #endif /* LWIP_VERSION_MAJOR == 1U */
  976. }
  977. #endif /**< #if LWIP_DNS */
  978. rt_exit_critical();
  979. }
  980. FINSH_FUNCTION_EXPORT(list_if, list network interface information);
  981. #if LWIP_TCP
  982. #include <lwip/tcp.h>
  983. #if LWIP_VERSION_MAJOR == 1U /* v1.x */
  984. #include <lwip/tcp_impl.h>
  985. #else /* >= v2.x */
  986. #include <lwip/priv/tcp_priv.h>
  987. #endif /* LWIP_VERSION_MAJOR == 1U */
  988. void list_tcps(void)
  989. {
  990. rt_uint32_t num = 0;
  991. struct tcp_pcb *pcb;
  992. char local_ip_str[16];
  993. char remote_ip_str[16];
  994. extern struct tcp_pcb *tcp_active_pcbs;
  995. extern union tcp_listen_pcbs_t tcp_listen_pcbs;
  996. extern struct tcp_pcb *tcp_tw_pcbs;
  997. rt_enter_critical();
  998. rt_kprintf("Active PCB states:\n");
  999. for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next)
  1000. {
  1001. strcpy(local_ip_str, ipaddr_ntoa(&(pcb->local_ip)));
  1002. strcpy(remote_ip_str, ipaddr_ntoa(&(pcb->remote_ip)));
  1003. rt_kprintf("#%d %s:%d <==> %s:%d snd_nxt 0x%08X rcv_nxt 0x%08X ",
  1004. num++,
  1005. local_ip_str,
  1006. pcb->local_port,
  1007. remote_ip_str,
  1008. pcb->remote_port,
  1009. pcb->snd_nxt,
  1010. pcb->rcv_nxt);
  1011. rt_kprintf("state: %s\n", tcp_debug_state_str(pcb->state));
  1012. }
  1013. rt_kprintf("Listen PCB states:\n");
  1014. num = 0;
  1015. for(pcb = (struct tcp_pcb *)tcp_listen_pcbs.pcbs; pcb != NULL; pcb = pcb->next)
  1016. {
  1017. rt_kprintf("#%d local port %d ", num++, pcb->local_port);
  1018. rt_kprintf("state: %s\n", tcp_debug_state_str(pcb->state));
  1019. }
  1020. rt_kprintf("TIME-WAIT PCB states:\n");
  1021. num = 0;
  1022. for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next)
  1023. {
  1024. strcpy(local_ip_str, ipaddr_ntoa(&(pcb->local_ip)));
  1025. strcpy(remote_ip_str, ipaddr_ntoa(&(pcb->remote_ip)));
  1026. rt_kprintf("#%d %s:%d <==> %s:%d snd_nxt 0x%08X rcv_nxt 0x%08X ",
  1027. num++,
  1028. local_ip_str,
  1029. pcb->local_port,
  1030. remote_ip_str,
  1031. pcb->remote_port,
  1032. pcb->snd_nxt,
  1033. pcb->rcv_nxt);
  1034. rt_kprintf("state: %s\n", tcp_debug_state_str(pcb->state));
  1035. }
  1036. rt_exit_critical();
  1037. }
  1038. FINSH_FUNCTION_EXPORT(list_tcps, list all of tcp connections);
  1039. #endif /* LWIP_TCP */
  1040. #if LWIP_UDP
  1041. #include "lwip/udp.h"
  1042. void list_udps(void)
  1043. {
  1044. struct udp_pcb *pcb;
  1045. rt_uint32_t num = 0;
  1046. char local_ip_str[16];
  1047. char remote_ip_str[16];
  1048. rt_enter_critical();
  1049. rt_kprintf("Active UDP PCB states:\n");
  1050. for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next)
  1051. {
  1052. strcpy(local_ip_str, ipaddr_ntoa(&(pcb->local_ip)));
  1053. strcpy(remote_ip_str, ipaddr_ntoa(&(pcb->remote_ip)));
  1054. rt_kprintf("#%d %d %s:%d <==> %s:%d \n",
  1055. num, (int)pcb->flags,
  1056. local_ip_str,
  1057. pcb->local_port,
  1058. remote_ip_str,
  1059. pcb->remote_port);
  1060. num++;
  1061. }
  1062. rt_exit_critical();
  1063. }
  1064. FINSH_FUNCTION_EXPORT(list_udps, list all of udp connections);
  1065. #endif /* LWIP_UDP */
  1066. #endif