LWIP库

1.以太网框图

TXFIFO和RXFIFO是独立的一个硬件缓冲区,CPU不能通过寻址直接访问(不在CPU的地址映射区);CPU通过控制DMA寄存器来访问FIFO。

数据具体流程如下:

  1. 数据接收: 当以太网 DMA 接收到数据帧时,它会将数据写入接收描述符链表中指向的缓冲区(Buffer1Addr 所指向的地址)。

  2. 数据转移: 以太网驱动程序检查接收描述符的状态。当有数据接收完成时,驱动程序从缓冲区读取数据。

  3. 封装到 pbuf 以太网驱动程序会将读取到的数据封装到一个新的 pbuf 结构中。通常,驱动程序会调用一个函数来为接收到的数据创建 pbuf,并将数据指针指向接收描述符链表中缓冲区的地址。

  4. 传递给 lwIP: 完成 pbuf 的封装后,驱动程序会将 pbuf 传递给 lwIP 协议栈进行进一步处理,如 IP 层和应用层的处理。

2.网络数据层级结构

3.LWIP启动流程(带操作系统)

4.LWIP内存管理

5.LWIP网络接口

LWIP的虚拟网卡使用netif结构体封装。

6.数据包管理

LWIP各层之间通过对pbuf的地址偏添加首部,而标志tcp/ip协议栈通过对各层数据进行拷贝。

7.IP协议

8.SOCKET接口TCP通信

9.两个局域网间通信例子

从一个私人 IP 地址发送数据到另一个私人 IP 地址的整个过程可以概括为以下几个步骤。为了便于说明,我们假设两个设备分别位于两个不同的局域网(LAN)中,通过各自的路由器连接到互联网。以下是详细的过程描述:

场景描述

  • 设备 A:位于 LAN A,使用私有 IP 地址 192.168.1.2。该局域网的路由器 R1 有公共 IP 地址 203.0.113.1
  • 设备 B:位于 LAN B,使用私有 IP 地址 192.168.2.2。该局域网的路由器 R2 有公共 IP 地址 198.51.100.1

1. 应用层数据生成

设备 A 上的应用程序生成需要发送的数据。例如,A 上的一个应用程序可能是一个网页浏览器,它需要向 B 上运行的一个 HTTP 服务器发送请求。

2. 数据封装在传输层

在传输层,数据被封装在 TCP 或 UDP 数据包中:

  • 传输层头部包括源端口号和目标端口号,用于标识发送和接收的应用程序。
  • 例如,浏览器可能使用源端口 12345,目标端口 80(HTTP 服务器端口)。

3. IP 层封装

传输层数据被封装在 IP 数据包中:

  • IP 包头包含源 IP 地址 192.168.1.2(设备 A 的 IP 地址)和目标 IP 地址 192.168.2.2(设备 B 的 IP 地址)。
  • IP 包会包含一个协议字段,标明该数据是 TCP 或 UDP 协议的数据。

4. 以太网帧封装和 MAC 地址解析

IP 数据包被进一步封装在以太网帧中:

  • 设备 A 使用 ARP(地址解析协议)解析局域网中路由器 R1 的 MAC 地址,因为 R1 是 A 发出 LAN 的默认网关。
  • 以太网帧包含源 MAC 地址(A 的 MAC 地址)和目标 MAC 地址(R1 的 MAC 地址)。

5. 数据通过 LAN A 发送到路由器 R1

设备 A 通过局域网将以太网帧发送到 R1:

  • R1 读取以太网帧,识别出目标 IP 地址 192.168.2.2 不在本地网络中,因此它需要通过互联网传输。

6. NAT(网络地址转换)处理

路由器 R1 对 IP 数据包进行 NAT 处理:

  • R1 修改 IP 包的源地址,将其从 192.168.1.2 改为 203.0.113.1(R1 的公共 IP)。
  • R1 还会修改源端口号,并在 NAT 表中记录私有 IP 地址和端口号的映射关系,以便响应时能够正确转发。

7. 数据通过互联网传输到 R2

路由器 R1 将数据包通过互联网发送到 R2 的公共 IP 地址 198.51.100.1

  • 互联网的路由设备根据目标 IP 地址将数据包转发到目标网络。

8. R2 接收数据包并进行 NAT 转换

路由器 R2 收到数据包后,通过 NAT 过程将目标 IP 地址 198.51.100.1 和目标端口号转换为私有网络内设备 B 的地址和端口号:

  • R2 通过 NAT 表将目标 IP 地址和端口号改为 192.168.2.2 和 B 的相应端口号。

9. 数据通过 LAN B 发送到设备 B

R2 将修改后的 IP 数据包封装在以太网帧中,目标 MAC 地址为 B 的 MAC 地址,将其通过 LAN B 发送给设备 B。

10. 设备 B 接收和处理数据包

设备 B 解封装接收到的以太网帧,提取出 IP 数据包和传输层数据,最终交付给目标应用程序(例如,HTTP 服务器)。

11. 设备 B 发送响应数据

如果设备 B 需要回应,例如返回一个 HTTP 响应,则该过程逆向进行:

  • B 生成响应数据,封装在传输层数据包中,接着是 IP 层封装、以太网帧封装。
  • 通过 R2 发送到互联网,R2 执行 NAT 过程,将响应发送到 R1 的公共 IP 地址。
  • R1 再次执行 NAT 过程,将响应转发回设备 A。

总结

在私有 IP 地址之间传输数据时,网络地址转换(NAT)在路由器上起到了至关重要的作用。它允许多个私有 IP 地址共享一个公共 IP 地址与互联网通信,并通过端口号和 NAT 表的映射,确保通信的正确性和安全性。整个过程中,数据包会经过多个层次的封装和解封装操作,每一层次都起着不同的功能作用,以保证数据可靠且有效地传输。

INTRODUCTION lwIP is a small independent implementation of the TCP/IP protocol suite. The focus of the lwIP TCP/IP implementation is to reduce the RAM usage while still having a full scale TCP. This making lwIP suitable for use in embedded systems with tens of kilobytes of free RAM and room for around 40 kilobytes of code ROM. lwIP was originally developed by Adam Dunkels at the Computer and Networks Architectures (CNA) lab at the Swedish Institute of Computer Science (SICS) and is now developed and maintained by a worldwide network of developers. FEATURES * IP (Internet Protocol, IPv4 and IPv6) including packet forwarding over multiple network interfaces * ICMP (Internet Control Message Protocol) for network maintenance and debugging * IGMP (Internet Group Management Protocol) for multicast traffic management * MLD (Multicast listener discovery for IPv6). Aims to be compliant with RFC 2710. No support for MLDv2 * ND (Neighbor discovery and stateless address autoconfiguration for IPv6). Aims to be compliant with RFC 4861 (Neighbor discovery) and RFC 4862 (Address autoconfiguration) * DHCP, AutoIP/APIPA (Zeroconf), ACD (Address Conflict Detection) and (stateless) DHCPv6 * UDP (User Datagram Protocol) including experimental UDP-lite extensions * TCP (Transmission Control Protocol) with congestion control, RTT estimation fast recovery/fast retransmit and sending SACKs * raw/native API for enhanced performance * Optional Berkeley-like socket API * TLS: optional layered TCP ("altcp") for nearly transparent TLS for any TCP-based protocol (ported to mbedTLS) (see changelog for more info) * PPPoS and PPPoE (Point-to-point protocol over Serial/Ethernet) * DNS (Domain name resolver incl. mDNS) * 6LoWPAN (via IEEE 802.15.4, BLE or ZEP) APPLICATIONS * HTTP server with SSI and CGI (HTTPS via altcp) * SNMPv2c agent with MIB compiler (Simple Network Management Protocol), v3 via altcp * SNTP (Simple network time protocol) * NetBIOS name service responder * MDNS (Multicast DNS) responder * iPerf server implementation * MQTT client (TLS support via altcp) LICENSE lwIP is freely available under a BSD license. DEVELOPMENT lwIP has grown into an excellent TCP/IP stack for embedded devices, and developers using the stack often submit bug fixes, improvements, and additions to the stack to further increase its usefulness. Development of lwIP is hosted on Savannah, a central point for software development, maintenance and distribution. Everyone can help improve lwIP by use of Savannah's interface, Git and the mailing list. A core team of developers will commit changes to the Git source tree. The lwIP TCP/IP stack is maintained in the 'lwip' Git module and contributions (such as platform ports) are in the 'contrib' Git module. See doc/savannah.txt for details on Git server access for users and developers. The current Git trees are web-browsable: http://git.savannah.gnu.org/cgit/lwip.git http://git.savannah.gnu.org/cgit/lwip/lwip-contrib.git Submit patches and bugs via the lwIP project page: http://savannah.nongnu.org/projects/lwip/ Continuous integration builds (GCC, clang): https://travis-ci.org/lwip-tcpip/lwip DOCUMENTATION Self documentation of the source code is regularly extracted from the current Git sources and is available from this web page: http://www.nongnu.org/lwip/ Also, there are mailing lists you can subscribe at http://savannah.nongnu.org/mail/?group=lwip plus searchable archives: http://lists.nongnu.org/archive/html/lwip-users/ http://lists.nongnu.org/archive/html/lwip-devel/ There is a wiki about lwIP at http://lwip.wikia.com/wiki/LwIP_Wiki You might get questions answered there, but unfortunately, it is not as well maintained as it should be. lwIP was originally written by Adam Dunkels: http://dunkels.com/adam/ Reading Adam's papers, the files in docs/, browsing the source code documentation and browsing the mailing list archives is a good way to become familiar with the design of lwIP. Adam Dunkels Leon Woestenberg
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值