1 Star 0 Fork 34

红枫软件 / Open-NPStack

forked from Neo-T / Open-NPStack 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

onps网络协议栈

背景

大约是06年,因项目之需我开始接触应用于单片机系统的国外开源tcp/ip协议栈——LwIP,并借此顺势创作了我的第一本印成铅字的书——《嵌入式网络系统设计——基于Atmel ARM7系列》。这本书的反响还不错,好多人给我发msn(可惜这么好的一个即时通讯工具就这么被微软放弃了,好多联系人就此失联, :persevere: )或邮件咨询相关问题。在我原来的写作计划中,这本书的出版只是一个开始,接下来还要写第二本——系统介绍LwIP包含的ppp协议栈的移植、应用及设计实现等相关内容。但,事与愿违,这本书跳票了,且这一跳就是十二年……

细细想来,当初跳票的主因有二:其一,因家庭、工作等致可支配时间太少;其二,缺乏足够的ppp协议相关知识及技术储备致信心不足,畏首畏尾,裹足不前。但,这件事始终是我的一个遗憾。十二年的时间,不长亦不短,但足够让心底的遗憾变成一粒小小的种子并茁壮成长为一棵梦想的参天大树。

如今,世界来到了疫情肆虐的二零年代。我的可支配时间多了起来,技术能力亦远非当年可比。梦想之树到了开花结果的时候了。遥想当初,入行还没几年,技术能力有限,我只能站在大神的肩膀上研究如何移植、使用LwIP,ppp栈碰都没敢碰。现在,如果还只是延续十几年前的工作,那这件事做起来就无甚意义。基于对自身技术实力的准确认识,我决定自己从零开始搭建一个完整的网络协议栈。终,历6个月余,onps协议栈(onps,open net protocol stack)完成初版开发,并内部测试通过。十余年的遗憾今日得偿。另,从业20余年,内心终有一个做核心基础软件的梦。今,这二之梦想亦借此得偿。

新莺初啼,总免不了会有诸多不尽如人意的地方。开源,则可与志趣相投者共享、共用、共研,历诸位严苛手段使之快速迭代,快速成熟,比肩LwIP可期 :blush:

简介

onps是一个开源且完全自主开发的国产网络协议栈,适用于资源受限的单片机系统,提供完整地ethernet/ppp/tcp/ip协议族实现,同时提供sntp、dns、ping等网络工具,支持以太网环境下dhcp动态ip地址申请,也支持动态及静态路由表。协议栈还封装实现了一个伯克利套接字(Berkeley sockets)层。该层并没有完全按照Berkeley sockets标准设计实现,而是我根据以往socket编程经验,以方便用户使用、简化用户编码为设计目标,重新声明并定义了一组常见socket接口函数:

  • socket:创建一个socket,目前仅支持udp和tcp两种类型
  • close:关闭一个socket,释放当前占用的协议栈资源
  • connect:与目标tcp服务器建立连接(阻塞型)或绑定一个固定的udp服务器地址
  • connect_nb:与目标tcp服务器建立连接(非阻塞型)
  • is_tcp_connected:获取当前tcp链路的连接状态
  • send:数据发送函数,tcp链路下为阻塞型
  • send_nb:数据发送函数,非阻塞型
  • is_tcp_send_ok:数据是否已成功送达tcp链路的对端(收到tcp ack报文)
  • sendto:udp数据发送函数,发送数据到指定目标地址
  • recv:数据接收函数,udp/tcp链路通用
  • recvfrom:数据接收函数,用于udp链路,接收数据的同时函数会返回数据源的地址信息
  • socket_set_rcv_timeout:设定recv()函数接收等待的时长,单位:秒
  • bind:绑定一个固定端口、地址
  • listen:tcp服务器进入监听状态
  • accept:接受一个到达的tcp连接请求
  • tcpsrv_recv_poll:tcp服务器专用函数,等待任意一个或多个tcp客户端数据到达信号
  • socket_get_last_error:获取socket最近一次发生的错误信息
  • socket_get_last_error_code:获取socket最近一次发生的错误编码

协议栈简化了传统BSD socket编程需要的一些繁琐操作,将一些不必要的操作细节改为底层实现,比如select/poll模型、阻塞及非阻塞读写操作等。简化并不意味着推翻,socket接口函数的基本定义、主要参数、使用方法并没有改变,你完全可以根据以往经验及编程习惯快速上手并熟练使用onps栈sockets。 无须过多关注协议栈底层,利用socket api编程即可完全满足复杂通讯应用的需求,而不像LwIp一样需要使用它自定义的一组接口函数才能达成同样的目标。

为了适应单片机系统对内存使用极度变态的苛刻要求,onps协议栈在设计之初即考虑采用写时零复制(zero copy)技术。用户层数据在向下层协议传递过程中,协议栈采用buf list链表技术将它们链接到一起,直至将其发送出去,均无须任何内存复制操作。另外,协议栈采用buddy算法提供安全、可靠的动态内存管理功能,以期最大限度地提高协议栈运行过程中的内存利用率并尽可能地减少内存碎片。

不同于本世纪00到10年代初,单片机的应用场景中ucosii等rtos尚未大规模普及,前后台系统还大行其道的时代,现如今大部分的应用场景下开发人员选择使用rtos已成为主流。因此,协议栈在设计之初即不支持前后台模式,其架构设计建立在时下流行的rtos(RT-Thread、ucosii/iii等)之上。协议栈移植的主要工作也就自然是针对不同rtos编写相关os适配层功能函数了。当然,如果你有着极其特定的应用场景,需要将onps栈移植到采用前后台模式的单片机上,我的建议是保留tcp/udp之下协议层的通讯处理逻辑,调整上层的系统架构使其适应目标系统运行模式。

软件架构

onps栈设计实现了一套完整的tcp/ip协议模型。从数据链路层到ip层,再到tcp/udp层以及之上的伯克利socket层,最后是用户自己的通讯应用层,onps栈实现了全栈覆盖,能够满足绝大部分的网络编程需求。其架构如下: onps栈架构

可以看出,其与传统的网络编程模型并没有什么不同,用户仍然是继续利用socket api编写常见的tcp及udp网络应用。同时你还可以利用协议栈提供的几个网络工具进行网络校时、dns查询等操作。

目录结构

名称 描述
bsd 伯克利sockets层的相关接口函数实现源文件
ethernet 以太网协议族如ethernet-ii/arp及emac层、dhcp客户端等的相关实现源文件
include 协议栈的头文件
ip ip及其上层icmp/tcp/udp协议族的相关实现源文件
mmu 协议栈内存管理模块的相关实现源文件
net_tools 网络工具实现源文件,如dns查询、网络校时、ping等
netif 网卡及路由管理等相关接口实现源文件
port 协议栈移植相关的源文件
ppp ppp链路层相关实现源文件,包括lcp/ipcp/chap/pap等协议族的实现源文件
TcpServerForStackTesting 用于协议栈测试的tcp服务器,IDE为vs2015开发,目标系统为win7及以上
test_code linux下的ppp拨号原理验证文件

移植及使用说明

协议栈支持主流的ARM Cortex系列MCU,支持Keil MDK、IAR等常见IDE。移植的核心工作就是完成RTOS模拟层的编写及适配,详细的移植说明请参考《onps网络协议栈移植及使用说明v1.0》一文,点此下载。本说明提供了STM32F103RCT6及STM32F407VET6两种硬件平台的移植样例,每种样例分别针对RT-Thread和ucosii两种RTOS。样例工程经过了严格的内部测试,可以直接使用。

社区支持

您可以随时访问 onps栈官方网站 ,获取协议栈研发进度、后续计划、最新版本等相关信息。
如您在使用过程中遇到任何问题或建议,您可以到 onps栈交流社区 提出您的建议或问题,新版本发布也会在交流社区第一时间通知。
您也可以加入QQ群进行在线技术交流:
qq交流群

许可协议

Apache License 2.0开源许可协议

后续计划

  • tcp增加sack选项支持
  • 支持ipv6
  • 支持ftp客户端/服务器
  • 支持telnet客户端/服务器
  • 支持http客户端/服务器

捐赠

为了项目能够持续下去,期望得到您的支持,您可以扫描下面的二维码通过支付宝/微信向本项目捐款:

支付宝 微信

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

onps是一个开源且完全自主开发的国产网络协议栈。设计目标与LwIp相同,onps栈的目标系统同样是资源受限的单片机系统。提供完整的tcp/ip协议族实现,同时提供sntp、dns、ping等网络工具,支持以太网环境下dhcp动态ip地址申请,也支持动态及静态路由表。协议栈还封装实现了一个伯克利套接字(Berkeley sockets)层。协议栈使用ANSI C语言开发。 展开 收起
C/C++
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C/C++
1
https://gitee.com/akinggw/open-npstack.git
git@gitee.com:akinggw/open-npstack.git
akinggw
open-npstack
Open-NPStack
master

搜索帮助