基于LwIP实现在一个网口上同时支持IPv6+IPv4通信

本文基于恩智浦的i.MX RT1170 EVK及SDK,讨论如何在一个以太网口上同时实现IPv6和IPv4通信。


测试环境:
SDK:2.10.1
IDE: MCUXpresso 11.4.0
EVK: i.MX RT1170
PC: Windows10

1. 实现步骤
1、下载i.MX RT1170 SDK 2.10.1导入lwip_udpecho_bm_cm7工程到MCUXpresso。
2、在lwipopts.h文件中, 参考本文提供的代码链接, 添加以下代码:
  1. #define LWIP_IPV6 1
复制代码
3、在lwip_udpecho_bm.c文件中, 参考本文提供的代码链接,添加以下代码:
  1. ip6_addr_t ipaddr_v6;
  2. s8_t chosen_idx;
  3. // IPv6
  4. IP6_ADDR(&ipaddr_v6,PP_HTONL(0xFE800000),PP_HTONL(0x0),PP_HTONL(0x3DD79303),PP_HTONL(0x126c0df0));
  5. netif_add_ip6_address(&netif, &ipaddr_v6, &chosen_idx);
  6. netif.ip6_addr_state[chosen_idx] = IP6_ADDR_VALID;
  7. ip6_addr_assign_zone(ip_2_ip6(&netif.ip6_addr[0]), IP6_UNICAST, &netif);
  8. netif_ip6_addr_set_state(&netif, 0, IP6_ADDR_TENTATIVE);
复制代码
4、编译并下载到目标板运行。
5、参考下图用网线连接PC机和目标板。
<ignore_js_op>
6、ICMP测试
打开CMD窗口,参照下面的log分别进行IPv6和IPv4的测试。
  1. C:\Users\nxa16038>ping 192.168.0.102
  2. Pinging 192.168.0.102 with 32 bytes of data:
  3. Reply from 192.168.0.102: bytes=32 time=2ms TTL=255
  4. Reply from 192.168.0.102: bytes=32 time=1ms TTL=255
  5. Reply from 192.168.0.102: bytes=32 time=1ms TTL=255
  6. Reply from 192.168.0.102: bytes=32 time=1ms TTL=255
  7. Ping statistics for 192.168.0.102:
  8.     Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
  9. Approximate round trip times in milli-seconds:
  10.     Minimum = 1ms, Maximum = 2ms, Average = 1ms
  11. C:\Users\nxa16038>ping -6 FE80::3DD7:9303:126c:0df0
  12. Pinging fe80::3dd7:9303:126c:df0 with 32 bytes of data:
  13. Reply from fe80::3dd7:9303:126c:df0: time=1ms
  14. Reply from fe80::3dd7:9303:126c:df0: time=1ms
  15. Reply from fe80::3dd7:9303:126c:df0: time=1ms
  16. Reply from fe80::3dd7:9303:126c:df0: time=2ms
  17. Ping statistics for fe80::3dd7:9303:126c:df0:
  18.     Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
  19. Approximate round trip times in milli-seconds:
  20.     Minimum = 1ms, Maximum = 2ms, Average = 1ms
复制代码
7、UDP测试


UDP测试需要使用相关的工具,在本文提供的代码链接中附带了此工具,测试命令和log如下所示:
  1. C:\Users\nxa16038\Desktop\tt2\udp_test_tool\x64\Debug>udp_test.exe -6 FE80::3DD7:9303:126c:df0 7 5001 test_string_12345
  2. Build date and time: Oct 22 2021, 18:50:48
  3. arc = 6
  4. arg[0] = udp_test.exe
  5. arg[1] = -6
  6. arg[2] = FE80::3DD7:9303:126c:df0
  7. arg[3] = 7
  8. arg[4] = 5001
  9. arg[5] = test_string_12345
  10. Send ok.
  11. Receiving...
  12. Get connection.
  13. Get string: test_string_12345
  14. Remote addr: fe80:0000:0000:0000:3dd7:9303:126c:0df0
  15. C:\Users\nxa16038\Desktop\tt2\udp_test_tool\x64\Debug>udp_test.exe -4 192.168.0.102 7 5001 test_string_12345
  16. Build date and time: Oct 22 2021, 18:50:48
  17. arc = 6
  18. arg[0] = udp_test.exe
  19. arg[1] = -4
  20. arg[2] = 192.168.0.102
  21. arg[3] = 7
  22. arg[4] = 5001
  23. arg[5] = test_string_12345
  24. Send ok.
  25. Receiving...
  26. Get connection.
  27. Get string: test_string_12345, len = 17
  28. Remote addr: 192.168.0.102
复制代码
2. 获取源代码
本文的原代码可以从github.com进行下载,无需密码,下载链接为:
https://github.com/jiaguonxpcom/lwip_ipv6_ipv4
该代码编译后可以直接下载到i.MX RT1170 EVK运行。

posted on 2022-06-19 17:55  张凌001  阅读(1092)  评论(0编辑  收藏  举报

导航