ethernetif.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265
  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;
  187. #else /* >= v2.x */
  188. struct timeval recv_timeout = { timeout / 1000UL, timeout % 1000UL * 1000 };
  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. #ifndef NETDEV_USING_IPV6
  221. local.sin_addr.s_addr = (netif->ip_addr.addr);
  222. #else
  223. local.sin_addr.s_addr = (netif->ip_addr.u_addr.ip4.addr);
  224. #endif
  225. lwip_bind(s, (struct sockaddr *)&local, sizeof(struct sockaddr_in));
  226. lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &recv_timeout, sizeof(recv_timeout));
  227. if (lwip_ping_send(s, &target_addr, data_len) == ERR_OK)
  228. {
  229. recv_start_tick = rt_tick_get();
  230. if ((recv_len = lwip_ping_recv(s, &ttl)) >= 0)
  231. {
  232. elapsed_time = (rt_tick_get() - recv_start_tick) * 1000UL / RT_TICK_PER_SECOND;
  233. ping_resp->data_len = recv_len;
  234. ping_resp->ttl = ttl;
  235. ping_resp->ticks = elapsed_time;
  236. }
  237. else
  238. {
  239. result = -RT_ETIMEOUT;
  240. goto __exit;
  241. }
  242. }
  243. else
  244. {
  245. result = -RT_ETIMEOUT;
  246. goto __exit;
  247. }
  248. __exit:
  249. lwip_close(s);
  250. return result;
  251. }
  252. #endif /* RT_LWIP_USING_PING */
  253. #if defined (RT_LWIP_TCP) || defined (RT_LWIP_UDP)
  254. void lwip_netdev_netstat(struct netdev *netif)
  255. {
  256. extern void list_tcps(void);
  257. extern void list_udps(void);
  258. #ifdef RT_LWIP_TCP
  259. list_tcps();
  260. #endif
  261. #ifdef RT_LWIP_UDP
  262. list_udps();
  263. #endif
  264. }
  265. #endif /* RT_LWIP_TCP || RT_LWIP_UDP */
  266. #endif /* RT_USING_FINSH */
  267. static int lwip_netdev_set_default(struct netdev *netif)
  268. {
  269. netif_set_default((struct netif *)netif->user_data);
  270. return ERR_OK;
  271. }
  272. const struct netdev_ops lwip_netdev_ops =
  273. {
  274. lwip_netdev_set_up,
  275. lwip_netdev_set_down,
  276. lwip_netdev_set_addr_info,
  277. #ifdef RT_LWIP_DNS
  278. lwip_netdev_set_dns_server,
  279. #else
  280. NULL,
  281. #endif /* RT_LWIP_DNS */
  282. #ifdef RT_LWIP_DHCP
  283. lwip_netdev_set_dhcp,
  284. #else
  285. NULL,
  286. #endif /* RT_LWIP_DHCP */
  287. #ifdef RT_USING_FINSH
  288. #ifdef RT_LWIP_USING_PING
  289. lwip_netdev_ping,
  290. #else
  291. NULL,
  292. #endif /* RT_LWIP_USING_PING */
  293. #if defined (RT_LWIP_TCP) || defined (RT_LWIP_UDP)
  294. lwip_netdev_netstat,
  295. #endif /* RT_LWIP_TCP || RT_LWIP_UDP */
  296. #endif /* RT_USING_FINSH */
  297. lwip_netdev_set_default,
  298. };
  299. /* synchronize lwIP network interface device and network interface device flags */
  300. static int netdev_flags_sync(struct netif *lwip_netif)
  301. {
  302. struct netdev *netdev = NULL;
  303. RT_ASSERT(lwip_netif);
  304. netdev = netdev_get_by_name(lwip_netif->name);
  305. if (netdev == RT_NULL)
  306. {
  307. return -ERR_IF;
  308. }
  309. netdev->mtu = lwip_netif->mtu;
  310. /* the macro definition is different from lwip-1.4.1 and lwip-2.x.x about 'flags'. */
  311. if(lwip_netif->flags & NETIF_FLAG_BROADCAST)
  312. {
  313. netdev->flags |= NETDEV_FLAG_BROADCAST;
  314. }
  315. if(lwip_netif->flags & NETIF_FLAG_ETHARP)
  316. {
  317. netdev->flags |= NETDEV_FLAG_ETHARP;
  318. }
  319. if(lwip_netif->flags & NETIF_FLAG_IGMP)
  320. {
  321. netdev->flags |= NETDEV_FLAG_IGMP;
  322. }
  323. #if LWIP_VERSION_MAJOR >= 2U /* >= v2.x */
  324. if(lwip_netif->flags & NETIF_FLAG_MLD6)
  325. {
  326. netdev->flags |= NETDEV_FLAG_MLD6;
  327. }
  328. #endif /* LWIP_VERSION_MAJOR >= 2U */
  329. #if LWIP_DHCP
  330. netdev_low_level_set_dhcp_status(netdev, RT_TRUE);
  331. #else
  332. netdev_low_level_set_dhcp_status(netdev, RT_FALSE);
  333. #endif
  334. return ERR_OK;
  335. }
  336. static int netdev_add(struct netif *lwip_netif)
  337. {
  338. #define LWIP_NETIF_NAME_LEN 2
  339. int result = 0;
  340. struct netdev *netdev = RT_NULL;
  341. char name[LWIP_NETIF_NAME_LEN + 1] = {0};
  342. RT_ASSERT(lwip_netif);
  343. netdev = (struct netdev *)rt_calloc(1, sizeof(struct netdev));
  344. if (netdev == RT_NULL)
  345. {
  346. return -ERR_IF;
  347. }
  348. #ifdef SAL_USING_LWIP
  349. extern int sal_lwip_netdev_set_pf_info(struct netdev *netdev);
  350. /* set the lwIP network interface device protocol family information */
  351. sal_lwip_netdev_set_pf_info(netdev);
  352. #endif /* SAL_USING_LWIP */
  353. rt_strncpy(name, lwip_netif->name, LWIP_NETIF_NAME_LEN);
  354. result = netdev_register(netdev, name, (void *)lwip_netif);
  355. /* Update netdev info after registered */
  356. netdev_flags_sync(lwip_netif);
  357. netdev->ops = &lwip_netdev_ops;
  358. netdev->hwaddr_len = lwip_netif->hwaddr_len;
  359. SMEMCPY(netdev->hwaddr, lwip_netif->hwaddr, lwip_netif->hwaddr_len);
  360. netdev->ip_addr = lwip_netif->ip_addr;
  361. netdev->gw = lwip_netif->gw;
  362. netdev->netmask = lwip_netif->netmask;
  363. return result;
  364. }
  365. static void netdev_del(struct netif *lwip_netif)
  366. {
  367. char name[LWIP_NETIF_NAME_LEN + 1];
  368. struct netdev *netdev;
  369. RT_ASSERT(lwip_netif);
  370. rt_strncpy(name, lwip_netif->name, LWIP_NETIF_NAME_LEN);
  371. netdev = netdev_get_by_name(name);
  372. netdev_unregister(netdev);
  373. rt_free(netdev);
  374. }
  375. #endif /* RT_USING_NETDEV */
  376. static err_t ethernetif_linkoutput(struct netif *netif, struct pbuf *p)
  377. {
  378. #ifndef LWIP_NO_TX_THREAD
  379. struct eth_tx_msg msg;
  380. RT_ASSERT(netif != RT_NULL);
  381. /* send a message to eth tx thread */
  382. msg.netif = netif;
  383. msg.buf = p;
  384. rt_completion_init(&msg.ack);
  385. if (rt_mb_send(&eth_tx_thread_mb, (rt_ubase_t) &msg) == RT_EOK)
  386. {
  387. /* waiting for ack */
  388. rt_completion_wait(&msg.ack, RT_WAITING_FOREVER);
  389. }
  390. #else
  391. struct eth_device* enetif;
  392. RT_ASSERT(netif != RT_NULL);
  393. enetif = (struct eth_device*)netif->state;
  394. if (enetif->eth_tx(&(enetif->parent), p) != RT_EOK)
  395. {
  396. return ERR_IF;
  397. }
  398. #endif
  399. return ERR_OK;
  400. }
  401. static err_t eth_netif_device_init(struct netif *netif)
  402. {
  403. struct eth_device *ethif;
  404. ethif = (struct eth_device*)netif->state;
  405. if (ethif != RT_NULL)
  406. {
  407. rt_device_t device;
  408. #ifdef RT_USING_NETDEV
  409. /* network interface device register */
  410. netdev_add(netif);
  411. #endif /* RT_USING_NETDEV */
  412. /* get device object */
  413. device = (rt_device_t) ethif;
  414. if (rt_device_init(device) != RT_EOK)
  415. {
  416. return ERR_IF;
  417. }
  418. if (rt_device_open(device, RT_DEVICE_FLAG_RDWR) != RT_EOK)
  419. {
  420. return ERR_IF;
  421. }
  422. /* copy device flags to netif flags */
  423. netif->flags = (ethif->flags & 0xff);
  424. netif->mtu = ETHERNET_MTU;
  425. /* set output */
  426. netif->output = etharp_output;
  427. #if LWIP_IPV6
  428. netif->output_ip6 = ethip6_output;
  429. netif->ip6_autoconfig_enabled = 1;
  430. netif_create_ip6_linklocal_address(netif, 1);
  431. #if LWIP_IPV6_MLD
  432. netif->flags |= NETIF_FLAG_MLD6;
  433. /*
  434. * For hardware/netifs that implement MAC filtering.
  435. * All-nodes link-local is handled by default, so we must let the hardware know
  436. * to allow multicast packets in.
  437. * Should set mld_mac_filter previously. */
  438. if (netif->mld_mac_filter != NULL)
  439. {
  440. ip6_addr_t ip6_allnodes_ll;
  441. ip6_addr_set_allnodes_linklocal(&ip6_allnodes_ll);
  442. netif->mld_mac_filter(netif, &ip6_allnodes_ll, NETIF_ADD_MAC_FILTER);
  443. }
  444. #endif /* LWIP_IPV6_MLD */
  445. #endif /* LWIP_IPV6 */
  446. /* set default netif */
  447. if (netif_default == RT_NULL)
  448. netif_set_default(ethif->netif);
  449. /* set interface up */
  450. netif_set_up(ethif->netif);
  451. #if LWIP_DHCP
  452. /* if this interface uses DHCP, start the DHCP client */
  453. dhcp_start(ethif->netif);
  454. #endif
  455. if (ethif->flags & ETHIF_LINK_PHYUP)
  456. {
  457. /* set link_up for this netif */
  458. netif_set_link_up(ethif->netif);
  459. }
  460. #ifdef RT_USING_NETDEV
  461. /* network interface device flags synchronize */
  462. netdev_flags_sync(netif);
  463. #endif /* RT_USING_NETDEV */
  464. return ERR_OK;
  465. }
  466. return ERR_IF;
  467. }
  468. /* Keep old drivers compatible in RT-Thread */
  469. rt_err_t eth_device_init_with_flag(struct eth_device *dev, const char *name, rt_uint16_t flags)
  470. {
  471. struct netif* netif;
  472. #if LWIP_NETIF_HOSTNAME
  473. #define LWIP_HOSTNAME_LEN 16
  474. char *hostname = RT_NULL;
  475. netif = (struct netif*) rt_calloc (1, sizeof(struct netif) + LWIP_HOSTNAME_LEN);
  476. #else
  477. netif = (struct netif*) rt_calloc (1, sizeof(struct netif));
  478. #endif
  479. if (netif == RT_NULL)
  480. {
  481. rt_kprintf("malloc netif failed\n");
  482. return -RT_ERROR;
  483. }
  484. rt_spin_lock_init(&(dev->spinlock));
  485. /* set netif */
  486. dev->netif = netif;
  487. dev->flags = flags;
  488. /* link changed status of device */
  489. dev->link_changed = 0x00;
  490. /* avoid send the same mail to mailbox */
  491. dev->rx_notice = 0x00;
  492. dev->parent.type = RT_Device_Class_NetIf;
  493. /* register to RT-Thread device manager */
  494. rt_device_register(&(dev->parent), name, RT_DEVICE_FLAG_RDWR);
  495. /* set name */
  496. netif->name[0] = name[0];
  497. netif->name[1] = name[1];
  498. /* set hw address to 6 */
  499. netif->hwaddr_len = 6;
  500. /* maximum transfer unit */
  501. netif->mtu = ETHERNET_MTU;
  502. /* set linkoutput */
  503. netif->linkoutput = ethernetif_linkoutput;
  504. /* get hardware MAC address */
  505. rt_device_control(&(dev->parent), NIOCTL_GADDR, netif->hwaddr);
  506. #if LWIP_NETIF_HOSTNAME
  507. /* Initialize interface hostname */
  508. hostname = (char *)netif + sizeof(struct netif);
  509. rt_sprintf(hostname, "rtthread_%02x%02x", name[0], name[1]);
  510. netif->hostname = hostname;
  511. #endif /* LWIP_NETIF_HOSTNAME */
  512. /* if tcp thread has been started up, we add this netif to the system */
  513. if (rt_thread_find("tcpip") != RT_NULL)
  514. {
  515. #if LWIP_VERSION_MAJOR == 1U /* v1.x */
  516. struct ip_addr ipaddr, netmask, gw;
  517. #else /* >= v2.x */
  518. ip4_addr_t ipaddr, netmask, gw;
  519. #endif /* LWIP_VERSION_MAJOR == 1U */
  520. #if !LWIP_DHCP
  521. ipaddr.addr = inet_addr(RT_LWIP_IPADDR);
  522. gw.addr = inet_addr(RT_LWIP_GWADDR);
  523. netmask.addr = inet_addr(RT_LWIP_MSKADDR);
  524. #else
  525. IP4_ADDR(&ipaddr, 0, 0, 0, 0);
  526. IP4_ADDR(&gw, 0, 0, 0, 0);
  527. IP4_ADDR(&netmask, 0, 0, 0, 0);
  528. #endif
  529. netifapi_netif_add(netif, &ipaddr, &netmask, &gw, dev, eth_netif_device_init, tcpip_input);
  530. }
  531. return RT_EOK;
  532. }
  533. rt_err_t eth_device_init(struct eth_device * dev, const char *name)
  534. {
  535. rt_uint16_t flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;
  536. #if LWIP_IGMP
  537. /* IGMP support */
  538. flags |= NETIF_FLAG_IGMP;
  539. #endif
  540. return eth_device_init_with_flag(dev, name, flags);
  541. }
  542. void eth_device_deinit(struct eth_device *dev)
  543. {
  544. struct netif* netif = dev->netif;
  545. #if LWIP_DHCP
  546. dhcp_stop(netif);
  547. dhcp_cleanup(netif);
  548. #endif
  549. netif_set_down(netif);
  550. netif_remove(netif);
  551. #ifdef RT_USING_NETDEV
  552. netdev_del(netif);
  553. #endif
  554. rt_device_close(&(dev->parent));
  555. rt_device_unregister(&(dev->parent));
  556. rt_free(netif);
  557. }
  558. #ifdef SAL_USING_AF_UNIX /* create loopback netdev */
  559. static err_t af_unix_eth_netif_device_init(struct netif *netif)
  560. {
  561. struct eth_device *ethif;
  562. ethif = (struct eth_device*)netif->state;
  563. if (ethif != RT_NULL)
  564. {
  565. rt_device_t device;
  566. #ifdef RT_USING_NETDEV
  567. /* network interface device register */
  568. netdev_add(netif);
  569. #endif /* RT_USING_NETDEV */
  570. /* get device object */
  571. device = (rt_device_t) ethif;
  572. if (rt_device_init(device) != RT_EOK)
  573. {
  574. return ERR_IF;
  575. }
  576. if (rt_device_open(device, RT_DEVICE_FLAG_RDWR) != RT_EOK)
  577. {
  578. return ERR_IF;
  579. }
  580. /* copy device flags to netif flags */
  581. netif->flags = (ethif->flags & 0xff);
  582. netif->mtu = ETHERNET_MTU;
  583. /* set output */
  584. netif->output = etharp_output;
  585. #if LWIP_IPV6
  586. netif->output_ip6 = ethip6_output;
  587. netif->ip6_autoconfig_enabled = 1;
  588. netif_create_ip6_linklocal_address(netif, 1);
  589. #if LWIP_IPV6_MLD
  590. netif->flags |= NETIF_FLAG_MLD6;
  591. /*
  592. * For hardware/netifs that implement MAC filtering.
  593. * All-nodes link-local is handled by default, so we must let the hardware know
  594. * to allow multicast packets in.
  595. * Should set mld_mac_filter previously. */
  596. if (netif->mld_mac_filter != NULL)
  597. {
  598. ip6_addr_t ip6_allnodes_ll;
  599. ip6_addr_set_allnodes_linklocal(&ip6_allnodes_ll);
  600. netif->mld_mac_filter(netif, &ip6_allnodes_ll, NETIF_ADD_MAC_FILTER);
  601. }
  602. #endif /* LWIP_IPV6_MLD */
  603. #endif /* LWIP_IPV6 */
  604. /* set default netif */
  605. if (netif_default == RT_NULL)
  606. netif_set_default(ethif->netif);
  607. /* set interface up */
  608. netif_set_up(ethif->netif);
  609. if (ethif->flags & ETHIF_LINK_PHYUP)
  610. {
  611. /* set link_up for this netif */
  612. netif_set_link_up(ethif->netif);
  613. }
  614. #ifdef RT_USING_NETDEV
  615. /* network interface device flags synchronize */
  616. netdev_flags_sync(netif);
  617. #endif /* RT_USING_NETDEV */
  618. return ERR_OK;
  619. }
  620. return ERR_IF;
  621. }
  622. /* Keep old drivers compatible in RT-Thread */
  623. rt_err_t af_unix_eth_device_init_with_flag(struct eth_device *dev, const char *name, rt_uint16_t flags)
  624. {
  625. struct netif* netif;
  626. #if LWIP_NETIF_HOSTNAME
  627. #define LWIP_HOSTNAME_LEN 16
  628. char *hostname = RT_NULL;
  629. netif = (struct netif*) rt_calloc (1, sizeof(struct netif) + LWIP_HOSTNAME_LEN);
  630. #else
  631. netif = (struct netif*) rt_calloc (1, sizeof(struct netif));
  632. #endif
  633. if (netif == RT_NULL)
  634. {
  635. rt_kprintf("malloc netif failed\n");
  636. return -RT_ERROR;
  637. }
  638. /* set netif */
  639. dev->netif = netif;
  640. dev->flags = flags;
  641. /* link changed status of device */
  642. dev->link_changed = 0x00;
  643. /* avoid send the same mail to mailbox */
  644. dev->rx_notice = 0x00;
  645. dev->parent.type = RT_Device_Class_NetIf;
  646. /* register to RT-Thread device manager */
  647. rt_device_register(&(dev->parent), name, RT_DEVICE_FLAG_RDWR);
  648. /* set name */
  649. netif->name[0] = name[0];
  650. netif->name[1] = name[1];
  651. /* set hw address to 6 */
  652. netif->hwaddr_len = 6;
  653. /* maximum transfer unit */
  654. netif->mtu = ETHERNET_MTU;
  655. /* set linkoutput */
  656. netif->linkoutput = ethernetif_linkoutput;
  657. /* get hardware MAC address */
  658. rt_device_control(&(dev->parent), NIOCTL_GADDR, netif->hwaddr);
  659. #if LWIP_NETIF_HOSTNAME
  660. /* Initialize interface hostname */
  661. hostname = (char *)netif + sizeof(struct netif);
  662. rt_sprintf(hostname, "rtthread_%02x%02x", name[0], name[1]);
  663. netif->hostname = hostname;
  664. #endif /* LWIP_NETIF_HOSTNAME */
  665. /* if tcp thread has been started up, we add this netif to the system */
  666. if (rt_thread_find("tcpip") != RT_NULL)
  667. {
  668. #if LWIP_VERSION_MAJOR == 1U /* v1.x */
  669. struct ip_addr ipaddr, netmask, gw;
  670. #else /* >= v2.x */
  671. ip4_addr_t ipaddr, netmask, gw;
  672. #endif /* LWIP_VERSION_MAJOR == 1U */
  673. ipaddr.addr = inet_addr("127.0.0.1");
  674. gw.addr = inet_addr("255.0.0.0");
  675. netmask.addr = inet_addr("127.0.0.1");
  676. netifapi_netif_add(netif, &ipaddr, &netmask, &gw, dev, af_unix_eth_netif_device_init, tcpip_input);
  677. }
  678. return RT_EOK;
  679. }
  680. rt_err_t af_unix_eth_device_init(struct eth_device * dev, const char *name)
  681. {
  682. rt_uint16_t flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;
  683. #if LWIP_IGMP
  684. /* IGMP support */
  685. flags |= NETIF_FLAG_IGMP;
  686. #endif
  687. return af_unix_eth_device_init_with_flag(dev, name, flags);
  688. }
  689. #endif /* SAL_USING_AF_UNIX */
  690. #ifndef LWIP_NO_RX_THREAD
  691. rt_err_t eth_device_ready(struct eth_device* dev)
  692. {
  693. if (dev->netif)
  694. {
  695. if(dev->rx_notice == RT_FALSE)
  696. {
  697. dev->rx_notice = RT_TRUE;
  698. return rt_mb_send(&eth_rx_thread_mb, (rt_ubase_t)dev);
  699. }
  700. else
  701. return RT_EOK;
  702. /* post message to Ethernet thread */
  703. }
  704. else
  705. return -RT_ERROR; /* netif is not initialized yet, just return. */
  706. }
  707. rt_err_t eth_device_linkchange(struct eth_device* dev, rt_bool_t up)
  708. {
  709. rt_base_t level;
  710. RT_ASSERT(dev != RT_NULL);
  711. level = rt_spin_lock_irqsave(&(dev->spinlock));
  712. dev->link_changed = 0x01;
  713. if (up == RT_TRUE)
  714. dev->link_status = 0x01;
  715. else
  716. dev->link_status = 0x00;
  717. rt_spin_unlock_irqrestore(&(dev->spinlock), level);
  718. /* post message to ethernet thread */
  719. return rt_mb_send(&eth_rx_thread_mb, (rt_ubase_t)dev);
  720. }
  721. #else
  722. /* NOTE: please not use it in interrupt when no RxThread exist */
  723. rt_err_t eth_device_linkchange(struct eth_device* dev, rt_bool_t up)
  724. {
  725. if (up == RT_TRUE)
  726. netifapi_netif_set_link_up(dev->netif);
  727. else
  728. netifapi_netif_set_link_down(dev->netif);
  729. return RT_EOK;
  730. }
  731. #endif
  732. #ifndef LWIP_NO_TX_THREAD
  733. /* Ethernet Tx Thread */
  734. static void eth_tx_thread_entry(void* parameter)
  735. {
  736. struct eth_tx_msg* msg;
  737. while (1)
  738. {
  739. if (rt_mb_recv(&eth_tx_thread_mb, (rt_ubase_t *)&msg, RT_WAITING_FOREVER) == RT_EOK)
  740. {
  741. struct eth_device* enetif;
  742. RT_ASSERT(msg->netif != RT_NULL);
  743. RT_ASSERT(msg->buf != RT_NULL);
  744. enetif = (struct eth_device*)msg->netif->state;
  745. if (enetif != RT_NULL)
  746. {
  747. /* call driver's interface */
  748. if (enetif->eth_tx(&(enetif->parent), msg->buf) != RT_EOK)
  749. {
  750. /* transmit eth packet failed */
  751. }
  752. }
  753. /* send ACK */
  754. rt_completion_done(&msg->ack);
  755. }
  756. }
  757. }
  758. #endif
  759. #ifndef LWIP_NO_RX_THREAD
  760. /* Ethernet Rx Thread */
  761. static void eth_rx_thread_entry(void* parameter)
  762. {
  763. struct eth_device* device;
  764. while (1)
  765. {
  766. if (rt_mb_recv(&eth_rx_thread_mb, (rt_ubase_t *)&device, RT_WAITING_FOREVER) == RT_EOK)
  767. {
  768. rt_base_t level;
  769. struct pbuf *p;
  770. /* check link status */
  771. if (device->link_changed)
  772. {
  773. int status;
  774. level = rt_spin_lock_irqsave(&(device->spinlock));
  775. status = device->link_status;
  776. device->link_changed = 0x00;
  777. rt_spin_unlock_irqrestore(&(device->spinlock), level);
  778. if (status)
  779. netifapi_netif_set_link_up(device->netif);
  780. else
  781. netifapi_netif_set_link_down(device->netif);
  782. }
  783. level = rt_spin_lock_irqsave(&(device->spinlock));
  784. /* 'rx_notice' will be modify in the interrupt or here */
  785. device->rx_notice = RT_FALSE;
  786. rt_spin_unlock_irqrestore(&(device->spinlock), level);
  787. /* receive all of buffer */
  788. while (1)
  789. {
  790. if(device->eth_rx == RT_NULL) break;
  791. p = device->eth_rx(&(device->parent));
  792. if (p != RT_NULL)
  793. {
  794. /* notify to upper layer */
  795. if( device->netif->input(p, device->netif) != ERR_OK )
  796. {
  797. LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: Input error\n"));
  798. pbuf_free(p);
  799. p = NULL;
  800. }
  801. }
  802. else break;
  803. }
  804. }
  805. else
  806. {
  807. LWIP_ASSERT("Should not happen!\n",0);
  808. }
  809. }
  810. }
  811. #endif
  812. /* this function does not need,
  813. * use eth_system_device_init_private()
  814. * call by lwip_system_init().
  815. */
  816. int eth_system_device_init(void)
  817. {
  818. return 0;
  819. }
  820. int eth_system_device_init_private(void)
  821. {
  822. rt_err_t result = RT_EOK;
  823. /* initialize Rx thread. */
  824. #ifndef LWIP_NO_RX_THREAD
  825. /* initialize mailbox and create Ethernet Rx thread */
  826. result = rt_mb_init(&eth_rx_thread_mb, "erxmb",
  827. &eth_rx_thread_mb_pool[0], sizeof(eth_rx_thread_mb_pool)/sizeof(rt_ubase_t),
  828. RT_IPC_FLAG_FIFO);
  829. RT_ASSERT(result == RT_EOK);
  830. result = rt_thread_init(&eth_rx_thread, "erx", eth_rx_thread_entry, RT_NULL,
  831. &eth_rx_thread_stack[0], sizeof(eth_rx_thread_stack),
  832. RT_ETHERNETIF_THREAD_PREORITY, 16);
  833. RT_ASSERT(result == RT_EOK);
  834. result = rt_thread_startup(&eth_rx_thread);
  835. RT_ASSERT(result == RT_EOK);
  836. #endif
  837. /* initialize Tx thread */
  838. #ifndef LWIP_NO_TX_THREAD
  839. /* initialize mailbox and create Ethernet Tx thread */
  840. result = rt_mb_init(&eth_tx_thread_mb, "etxmb",
  841. &eth_tx_thread_mb_pool[0], sizeof(eth_tx_thread_mb_pool)/sizeof(rt_ubase_t),
  842. RT_IPC_FLAG_FIFO);
  843. RT_ASSERT(result == RT_EOK);
  844. result = rt_thread_init(&eth_tx_thread, "etx", eth_tx_thread_entry, RT_NULL,
  845. &eth_tx_thread_stack[0], sizeof(eth_tx_thread_stack),
  846. RT_ETHERNETIF_THREAD_PREORITY, 16);
  847. RT_ASSERT(result == RT_EOK);
  848. result = rt_thread_startup(&eth_tx_thread);
  849. RT_ASSERT(result == RT_EOK);
  850. #endif
  851. return (int)result;
  852. }
  853. void set_if(char* netif_name, char* ip_addr, char* gw_addr, char* nm_addr)
  854. {
  855. #if LWIP_VERSION_MAJOR == 1U /* v1.x */
  856. struct ip_addr *ip;
  857. struct ip_addr addr;
  858. #else /* >= v2.x */
  859. ip4_addr_t *ip;
  860. ip4_addr_t addr;
  861. #endif /* LWIP_VERSION_MAJOR == 1U */
  862. struct netif * netif = netif_list;
  863. if(strlen(netif_name) > sizeof(netif->name))
  864. {
  865. rt_kprintf("network interface name too long!\r\n");
  866. return;
  867. }
  868. while(netif != RT_NULL)
  869. {
  870. if(strncmp(netif_name, netif->name, sizeof(netif->name)) == 0)
  871. break;
  872. netif = netif->next;
  873. if( netif == RT_NULL )
  874. {
  875. rt_kprintf("network interface: %s not found!\r\n", netif_name);
  876. return;
  877. }
  878. }
  879. #if LWIP_VERSION_MAJOR == 1U /* v1.x */
  880. ip = (struct ip_addr *)&addr;
  881. #else /* >= v2.x */
  882. ip = (ip4_addr_t *)&addr;
  883. #endif /* LWIP_VERSION_MAJOR == 1U */
  884. /* set ip address */
  885. if ((ip_addr != RT_NULL) && inet_aton(ip_addr, &addr))
  886. {
  887. netif_set_ipaddr(netif, ip);
  888. }
  889. /* set gateway address */
  890. if ((gw_addr != RT_NULL) && inet_aton(gw_addr, &addr))
  891. {
  892. netif_set_gw(netif, ip);
  893. }
  894. /* set netmask address */
  895. if ((nm_addr != RT_NULL) && inet_aton(nm_addr, &addr))
  896. {
  897. netif_set_netmask(netif, ip);
  898. }
  899. }
  900. #ifdef RT_USING_FINSH
  901. #include <finsh.h>
  902. FINSH_FUNCTION_EXPORT(set_if, set network interface address);
  903. #if LWIP_DNS
  904. #include <lwip/dns.h>
  905. void set_dns(uint8_t dns_num, char* dns_server)
  906. {
  907. ip_addr_t addr;
  908. if ((dns_server != RT_NULL) && ipaddr_aton(dns_server, &addr))
  909. {
  910. dns_setserver(dns_num, &addr);
  911. }
  912. }
  913. FINSH_FUNCTION_EXPORT(set_dns, set DNS server address);
  914. #endif
  915. void list_if(void)
  916. {
  917. rt_uint8_t index;
  918. struct netif * netif;
  919. rt_enter_critical();
  920. netif = netif_list;
  921. while( netif != RT_NULL )
  922. {
  923. rt_kprintf("network interface: %c%c%s\n",
  924. netif->name[0],
  925. netif->name[1],
  926. (netif == netif_default)?" (Default)":"");
  927. rt_kprintf("MTU: %d\n", netif->mtu);
  928. rt_kprintf("MAC: ");
  929. for (index = 0; index < netif->hwaddr_len; index ++)
  930. rt_kprintf("%02x ", netif->hwaddr[index]);
  931. rt_kprintf("\nFLAGS:");
  932. if (netif->flags & NETIF_FLAG_UP) rt_kprintf(" UP");
  933. else rt_kprintf(" DOWN");
  934. if (netif->flags & NETIF_FLAG_LINK_UP) rt_kprintf(" LINK_UP");
  935. else rt_kprintf(" LINK_DOWN");
  936. if (netif->flags & NETIF_FLAG_ETHARP) rt_kprintf(" ETHARP");
  937. if (netif->flags & NETIF_FLAG_BROADCAST) rt_kprintf(" BROADCAST");
  938. if (netif->flags & NETIF_FLAG_IGMP) rt_kprintf(" IGMP");
  939. rt_kprintf("\n");
  940. rt_kprintf("ip address: %s\n", ipaddr_ntoa(&(netif->ip_addr)));
  941. rt_kprintf("gw address: %s\n", ipaddr_ntoa(&(netif->gw)));
  942. rt_kprintf("net mask : %s\n", ipaddr_ntoa(&(netif->netmask)));
  943. #if LWIP_IPV6
  944. {
  945. ip6_addr_t *addr;
  946. int addr_state;
  947. int i;
  948. addr = (ip6_addr_t *)&netif->ip6_addr[0];
  949. addr_state = netif->ip6_addr_state[0];
  950. rt_kprintf("\nipv6 link-local: %s state:%02X %s\n", ip6addr_ntoa(addr),
  951. addr_state, ip6_addr_isvalid(addr_state)?"VALID":"INVALID");
  952. for(i=1; i<LWIP_IPV6_NUM_ADDRESSES; i++)
  953. {
  954. addr = (ip6_addr_t *)&netif->ip6_addr[i];
  955. addr_state = netif->ip6_addr_state[i];
  956. rt_kprintf("ipv6[%d] address: %s state:%02X %s\n", i, ip6addr_ntoa(addr),
  957. addr_state, ip6_addr_isvalid(addr_state)?"VALID":"INVALID");
  958. }
  959. }
  960. rt_kprintf("\r\n");
  961. #endif /* LWIP_IPV6 */
  962. netif = netif->next;
  963. }
  964. #if LWIP_DNS
  965. {
  966. #if LWIP_VERSION_MAJOR == 1U /* v1.x */
  967. struct ip_addr ip_addr;
  968. for(index=0; index<DNS_MAX_SERVERS; index++)
  969. {
  970. ip_addr = dns_getserver(index);
  971. rt_kprintf("dns server #%d: %s\n", index, ipaddr_ntoa(&(ip_addr)));
  972. }
  973. #else /* >= v2.x */
  974. const ip_addr_t *ip_addr;
  975. for(index=0; index<DNS_MAX_SERVERS; index++)
  976. {
  977. ip_addr = dns_getserver(index);
  978. rt_kprintf("dns server #%d: %s\n", index, inet_ntoa(ip_addr));
  979. }
  980. #endif /* LWIP_VERSION_MAJOR == 1U */
  981. }
  982. #endif /**< #if LWIP_DNS */
  983. rt_exit_critical();
  984. }
  985. FINSH_FUNCTION_EXPORT(list_if, list network interface information);
  986. #if LWIP_TCP
  987. #include <lwip/tcp.h>
  988. #if LWIP_VERSION_MAJOR == 1U /* v1.x */
  989. #include <lwip/tcp_impl.h>
  990. #else /* >= v2.x */
  991. #include <lwip/priv/tcp_priv.h>
  992. #endif /* LWIP_VERSION_MAJOR == 1U */
  993. void list_tcps(void)
  994. {
  995. rt_uint32_t num = 0;
  996. struct tcp_pcb *pcb;
  997. char local_ip_str[16];
  998. char remote_ip_str[16];
  999. extern struct tcp_pcb *tcp_active_pcbs;
  1000. extern union tcp_listen_pcbs_t tcp_listen_pcbs;
  1001. extern struct tcp_pcb *tcp_tw_pcbs;
  1002. rt_enter_critical();
  1003. rt_kprintf("Active PCB states:\n");
  1004. for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next)
  1005. {
  1006. strcpy(local_ip_str, ipaddr_ntoa(&(pcb->local_ip)));
  1007. strcpy(remote_ip_str, ipaddr_ntoa(&(pcb->remote_ip)));
  1008. rt_kprintf("#%d %s:%d <==> %s:%d snd_nxt 0x%08X rcv_nxt 0x%08X ",
  1009. num++,
  1010. local_ip_str,
  1011. pcb->local_port,
  1012. remote_ip_str,
  1013. pcb->remote_port,
  1014. pcb->snd_nxt,
  1015. pcb->rcv_nxt);
  1016. rt_kprintf("state: %s\n", tcp_debug_state_str(pcb->state));
  1017. }
  1018. rt_kprintf("Listen PCB states:\n");
  1019. num = 0;
  1020. for(pcb = (struct tcp_pcb *)tcp_listen_pcbs.pcbs; pcb != NULL; pcb = pcb->next)
  1021. {
  1022. rt_kprintf("#%d local port %d ", num++, pcb->local_port);
  1023. rt_kprintf("state: %s\n", tcp_debug_state_str(pcb->state));
  1024. }
  1025. rt_kprintf("TIME-WAIT PCB states:\n");
  1026. num = 0;
  1027. for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next)
  1028. {
  1029. strcpy(local_ip_str, ipaddr_ntoa(&(pcb->local_ip)));
  1030. strcpy(remote_ip_str, ipaddr_ntoa(&(pcb->remote_ip)));
  1031. rt_kprintf("#%d %s:%d <==> %s:%d snd_nxt 0x%08X rcv_nxt 0x%08X ",
  1032. num++,
  1033. local_ip_str,
  1034. pcb->local_port,
  1035. remote_ip_str,
  1036. pcb->remote_port,
  1037. pcb->snd_nxt,
  1038. pcb->rcv_nxt);
  1039. rt_kprintf("state: %s\n", tcp_debug_state_str(pcb->state));
  1040. }
  1041. rt_exit_critical();
  1042. }
  1043. FINSH_FUNCTION_EXPORT(list_tcps, list all of tcp connections);
  1044. #endif /* LWIP_TCP */
  1045. #if LWIP_UDP
  1046. #include "lwip/udp.h"
  1047. void list_udps(void)
  1048. {
  1049. struct udp_pcb *pcb;
  1050. rt_uint32_t num = 0;
  1051. char local_ip_str[16];
  1052. char remote_ip_str[16];
  1053. rt_enter_critical();
  1054. rt_kprintf("Active UDP PCB states:\n");
  1055. for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next)
  1056. {
  1057. strcpy(local_ip_str, ipaddr_ntoa(&(pcb->local_ip)));
  1058. strcpy(remote_ip_str, ipaddr_ntoa(&(pcb->remote_ip)));
  1059. rt_kprintf("#%d %d %s:%d <==> %s:%d \n",
  1060. num, (int)pcb->flags,
  1061. local_ip_str,
  1062. pcb->local_port,
  1063. remote_ip_str,
  1064. pcb->remote_port);
  1065. num++;
  1066. }
  1067. rt_exit_critical();
  1068. }
  1069. FINSH_FUNCTION_EXPORT(list_udps, list all of udp connections);
  1070. #endif /* LWIP_UDP */
  1071. #endif