lwip源码目录解析

lwip源码目录解析

原文链接:https://blog.csdn.net/liverpoolsun/article/details/19674319
学习lwip协议,首先得搞清楚源码目录下各个文件的含义,才能对lwip协议框架进行学习,本文转载于其它博客,源码之间会存在部分差异,但是不会影响到对整个代码框架的学习。

lwip根目录:
在这里插入图片描述

一.api接口

为方便用户编程,LwIP为用户提供两种简单的高级API接口:协议栈sequentialAPI和socket API。这两种API实现的原理都是通过引进邮箱和信号量等通信与同步机制,来实现对内核中raw/callback API函数的封装和调用。也就是说,要使用这两种API,必须基于底层操作系统提供的邮箱和信号量机制,必须要在开发板上移植好操作系统。
在这里插入图片描述

文件说明
api_lib.c包含sequential API函数的实现,主要包含预留给用户的编程接口
api_msg.c包含sequential API函数的实现,主要包含API消息的封装和处理函数
netbuf.c包含上层数据包管理函数的实现
netdb.c包含与主机名字转换相关的函数,主要在socket中被使用到
netifapi.c包含上层网络接口管理函数的实现
sockets.c包含socket API函数的实现
tcpip.c提供了上层API与协议栈内核交互的函数,它是整个上层API功能得以实现的一个枢纽,其实现的功能可以理解为:从API函数处接收消息,然后将消息递交给内核函数,内核函数根据消息做出相应的处理。

二.核心层代码

core文件夹是LwIP内核源代码,包含IP、ICMP、IGMP、TCP、UDP等核心协议以及建立在它们基础上的DNS、DHCP、SNMP等上层应用协议。内核源代码可以单独运行,且不需要操作系统的支持。我们先看下core根目录下的文件作用说明,然后再分析子目录中过的文件。
在这里插入图片描述

文件说明
dhcp.c包含DHCP(动态主机配置协议)客户端的实现代码
dns.c包含DNS(域名系统)客户端的实现代码
init.c包含了一个与LwIP协议栈初始化密切相关的函数,以及一些协议栈配置信息的检查与输出
mem.c议栈内存堆管理函数的实现代码
memp.c协议栈内存池管理函数的实现代码
netif.c包含协议栈网络接口管理的相关函数,协议栈内部对每个接口(比如以太网接口、回环接口等)用一个对应的数据结构进行描述,并通过使用netif.c中的函数进行统一管理。
pbuf.c包含协议栈内核使用的数据包管理函数,用于协议栈层次间的数据传递,避免数据拷贝
raw.c原始套接字的实现代码,可以通过该文件中的函数直接操纵IP层数据包
stats.c包含协议栈内部数据统计与显示的函数,比如内存使用状况、邮箱、信号量等信息
sys.c对操作系统模拟层的封装,为协议栈提供统一的邮箱、信号量操作函数。如果开发者需要使用协议栈的sequential API和socket API,则必须使用底层操作系统提供的邮箱与信号量机制,这时内核要求移植者提供一个称为sys_arch.c的操作系统模拟层文件,这个文件主要完成对操作系统中邮箱与信号量函数的封装。而sys.c文件的功能是将sys_arch.c中的函数再次封装,以得到具有协议栈特色的邮箱、信号量操作函数。所谓特色,就是在这些函数中加入一种机制,以实现协议栈中各个定时事件的正确处理。不使用sequential API和socket API时,开发者不需要再实现sys_arch.c中的函数,sys.c对内核来说也没用了,因为此时系统不需要任何邮箱和信号量机制。
tcp.c包含对TCP控制块操作的函数,也包含了TCP定时处理函数
tcp_in.c包含TCP协议数据接收、处理相关的函数,以及最重要的TCP状态机函数
tcp_out.c含TCP协议数据发送相关函数,例如数据包发送、超时重传函数等
udp.c包含实现UDP协议的相关函数,包括控制块管理、数据包发送函数、数据包接收函数等

core/ipv4文件夹包含了IPv4标准中与IP层数据包处理相关的所有代码

文件说明
autoip.c包含IP地址自动配置相关函数,若主机从DHCP服务器处获取IP地址失败,则此时主机可以选择启动AUTOIP功能来配置自身的IP地址。AUTOIP将主机配置为169.254.0.0/16中的某个地址,并提供一套完整的机制来避免IP地址冲突
icmp.c包含ICMP(网际控制报文协议)协议的实现代码。ICMP协议为IP数据包传递过程中的差错报告、差错纠正以及目的地址可达性测试提供了支持,常见的Ping命令就属于ICMP应用中的一种。
igmp.c包含IGMP(网络组管理协议)协议的实现代码。IGMP为网络中的多播数据传输提供了支持,主机加入某个多播组后,可以接收到改组的UDP多播数据。
inet.c包含IP层使用到的一些功能函数的定义,如IP地址的转换、网络字节序与主机字节序转换等
inet_chksum.c包含对IP数据包校验相关的函数
ip.c包含IPv4协议实现的相关函数,如数据包的接收、递交、发送等
ip_addr.c包含一个判断IP地址是否为广播地址的函数
ip_frag.c提供了IP层数据包分片与重组相关的函数

三.netif底层网络接口

netif文件夹主要包含了与底层网络接口相关的文件,本文中部分有效文件转移到port中。
在这里插入图片描述

文件说明
etharp.c包含ARP协议的实现代码。主要用来实现主机以太网物理地址到IP地址的映射。
ethernetif.c包含了与以太网网卡密切相关的初始化、发送、接收等函数的实现。这个文件夹中的函数并不能使用,它们都是一个框架性的结构,移植者需要根据自己使用的网卡特性来完成这些函数。
loopif.c为协议栈提供回环网络接口功能。使用这个接口可以实现本地两个进程之间的数据传递。
slipif.cSLIP(串行链路IP),提供一种在串行链路上传送IP数据包的函数定义,移植者需要根据自己使用的串行线路特性来实现这些函数。
ppp文件夹包含了PPP 协议实现的源代码。PPP 协议即点对点协议,它提供了 一种在点对点线路上传输多协议数据包的标准格式,PPP 协议为链路的建立、控制与认证提供 了标准。起初PPP 主要用来替代SLIP 这种简单的串行链路数据传输协议,但是由于其完整的认证机制,后来在以太网上也引入了PPP 机制,即PPPoE,它已成为近年来小区宽带拨号上网的主要方式。使用PPPoE,为用户的上网计费、配置、接入等提供了方便。LwIP 提供了对PPPoE 的支持,在ppp文件夹下的PPPoE.c文件中有相关的函数实现。

四.include文件夹

LwIP使用的各种头文件。与各源码目录相对应。
在这里插入图片描述

文件说明
netif文件夹主要是针对netif目录中源文件的各种头文件。
posix文件夹主要是针对POSIX标准,进行的封装API。
lwip文件夹LwIP的各种头文件,其中需要注意的有opt.h 文件,它包含了所有LwIP内核参数的默认配置值;还有init.h 文件,它包含了与当前 LwIP 源代码信息相关的宏定义,如协议版本号、是否为官方版等
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、付费专栏及课程。

余额充值