ethernetif.c 35 KB

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