ethernetif.c 35 KB

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