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. * 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. #if LWIP_DNS
  913. #include <lwip/dns.h>
  914. void set_dns(uint8_t dns_num, char* dns_server)
  915. {
  916. ip_addr_t addr;
  917. if ((dns_server != RT_NULL) && ipaddr_aton(dns_server, &addr))
  918. {
  919. dns_setserver(dns_num, &addr);
  920. }
  921. }
  922. #endif
  923. void list_if(void)
  924. {
  925. rt_uint8_t index;
  926. struct netif * netif;
  927. rt_enter_critical();
  928. netif = netif_list;
  929. while( netif != RT_NULL )
  930. {
  931. rt_kprintf("network interface: %c%c%s\n",
  932. netif->name[0],
  933. netif->name[1],
  934. (netif == netif_default)?" (Default)":"");
  935. rt_kprintf("MTU: %d\n", netif->mtu);
  936. rt_kprintf("MAC: ");
  937. for (index = 0; index < netif->hwaddr_len; index ++)
  938. rt_kprintf("%02x ", netif->hwaddr[index]);
  939. rt_kprintf("\nFLAGS:");
  940. if (netif->flags & NETIF_FLAG_UP) rt_kprintf(" UP");
  941. else rt_kprintf(" DOWN");
  942. if (netif->flags & NETIF_FLAG_LINK_UP) rt_kprintf(" LINK_UP");
  943. else rt_kprintf(" LINK_DOWN");
  944. if (netif->flags & NETIF_FLAG_ETHARP) rt_kprintf(" ETHARP");
  945. if (netif->flags & NETIF_FLAG_BROADCAST) rt_kprintf(" BROADCAST");
  946. if (netif->flags & NETIF_FLAG_IGMP) rt_kprintf(" IGMP");
  947. rt_kprintf("\n");
  948. rt_kprintf("ip address: %s\n", ipaddr_ntoa(&(netif->ip_addr)));
  949. rt_kprintf("gw address: %s\n", ipaddr_ntoa(&(netif->gw)));
  950. rt_kprintf("net mask : %s\n", ipaddr_ntoa(&(netif->netmask)));
  951. #if LWIP_IPV6
  952. {
  953. ip6_addr_t *addr;
  954. int addr_state;
  955. int i;
  956. addr = (ip6_addr_t *)&netif->ip6_addr[0];
  957. addr_state = netif->ip6_addr_state[0];
  958. rt_kprintf("\nipv6 link-local: %s state:%02X %s\n", ip6addr_ntoa(addr),
  959. addr_state, ip6_addr_isvalid(addr_state)?"VALID":"INVALID");
  960. for(i=1; i<LWIP_IPV6_NUM_ADDRESSES; i++)
  961. {
  962. addr = (ip6_addr_t *)&netif->ip6_addr[i];
  963. addr_state = netif->ip6_addr_state[i];
  964. rt_kprintf("ipv6[%d] address: %s state:%02X %s\n", i, ip6addr_ntoa(addr),
  965. addr_state, ip6_addr_isvalid(addr_state)?"VALID":"INVALID");
  966. }
  967. }
  968. rt_kprintf("\r\n");
  969. #endif /* LWIP_IPV6 */
  970. netif = netif->next;
  971. }
  972. #if LWIP_DNS
  973. {
  974. #if LWIP_VERSION_MAJOR == 1U /* v1.x */
  975. struct ip_addr ip_addr;
  976. for(index=0; index<DNS_MAX_SERVERS; index++)
  977. {
  978. ip_addr = dns_getserver(index);
  979. rt_kprintf("dns server #%d: %s\n", index, ipaddr_ntoa(&(ip_addr)));
  980. }
  981. #else /* >= v2.x */
  982. const ip_addr_t *ip_addr;
  983. for(index=0; index<DNS_MAX_SERVERS; index++)
  984. {
  985. ip_addr = dns_getserver(index);
  986. rt_kprintf("dns server #%d: %s\n", index, inet_ntoa(ip_addr));
  987. }
  988. #endif /* LWIP_VERSION_MAJOR == 1U */
  989. }
  990. #endif /**< #if LWIP_DNS */
  991. rt_exit_critical();
  992. }
  993. #if LWIP_TCP
  994. #include <lwip/tcp.h>
  995. #if LWIP_VERSION_MAJOR == 1U /* v1.x */
  996. #include <lwip/tcp_impl.h>
  997. #else /* >= v2.x */
  998. #include <lwip/priv/tcp_priv.h>
  999. #endif /* LWIP_VERSION_MAJOR == 1U */
  1000. void list_tcps(void)
  1001. {
  1002. rt_uint32_t num = 0;
  1003. struct tcp_pcb *pcb;
  1004. char local_ip_str[16];
  1005. char remote_ip_str[16];
  1006. extern struct tcp_pcb *tcp_active_pcbs;
  1007. extern union tcp_listen_pcbs_t tcp_listen_pcbs;
  1008. extern struct tcp_pcb *tcp_tw_pcbs;
  1009. rt_enter_critical();
  1010. rt_kprintf("Active PCB states:\n");
  1011. for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next)
  1012. {
  1013. strcpy(local_ip_str, ipaddr_ntoa(&(pcb->local_ip)));
  1014. strcpy(remote_ip_str, ipaddr_ntoa(&(pcb->remote_ip)));
  1015. rt_kprintf("#%d %s:%d <==> %s:%d snd_nxt 0x%08X rcv_nxt 0x%08X ",
  1016. num++,
  1017. local_ip_str,
  1018. pcb->local_port,
  1019. remote_ip_str,
  1020. pcb->remote_port,
  1021. pcb->snd_nxt,
  1022. pcb->rcv_nxt);
  1023. rt_kprintf("state: %s\n", tcp_debug_state_str(pcb->state));
  1024. }
  1025. rt_kprintf("Listen PCB states:\n");
  1026. num = 0;
  1027. for(pcb = (struct tcp_pcb *)tcp_listen_pcbs.pcbs; pcb != NULL; pcb = pcb->next)
  1028. {
  1029. rt_kprintf("#%d local port %d ", num++, pcb->local_port);
  1030. rt_kprintf("state: %s\n", tcp_debug_state_str(pcb->state));
  1031. }
  1032. rt_kprintf("TIME-WAIT PCB states:\n");
  1033. num = 0;
  1034. for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next)
  1035. {
  1036. strcpy(local_ip_str, ipaddr_ntoa(&(pcb->local_ip)));
  1037. strcpy(remote_ip_str, ipaddr_ntoa(&(pcb->remote_ip)));
  1038. rt_kprintf("#%d %s:%d <==> %s:%d snd_nxt 0x%08X rcv_nxt 0x%08X ",
  1039. num++,
  1040. local_ip_str,
  1041. pcb->local_port,
  1042. remote_ip_str,
  1043. pcb->remote_port,
  1044. pcb->snd_nxt,
  1045. pcb->rcv_nxt);
  1046. rt_kprintf("state: %s\n", tcp_debug_state_str(pcb->state));
  1047. }
  1048. rt_exit_critical();
  1049. }
  1050. #endif /* LWIP_TCP */
  1051. #if LWIP_UDP
  1052. #include "lwip/udp.h"
  1053. void list_udps(void)
  1054. {
  1055. struct udp_pcb *pcb;
  1056. rt_uint32_t num = 0;
  1057. char local_ip_str[16];
  1058. char remote_ip_str[16];
  1059. rt_enter_critical();
  1060. rt_kprintf("Active UDP PCB states:\n");
  1061. for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next)
  1062. {
  1063. strcpy(local_ip_str, ipaddr_ntoa(&(pcb->local_ip)));
  1064. strcpy(remote_ip_str, ipaddr_ntoa(&(pcb->remote_ip)));
  1065. rt_kprintf("#%d %d %s:%d <==> %s:%d \n",
  1066. num, (int)pcb->flags,
  1067. local_ip_str,
  1068. pcb->local_port,
  1069. remote_ip_str,
  1070. pcb->remote_port);
  1071. num++;
  1072. }
  1073. rt_exit_critical();
  1074. }
  1075. #endif /* LWIP_UDP */
  1076. #endif