1.前言

2.Initialization for simple lwIP

查看doc/rawapi.txt来获得更多官方信息

#if NO_SYS
/* Network interface variables */
struct ip_addr ipaddr, netmask, gw;
struct netif netif;
/* Set network address variables */
IP4_ADDR(&gw, ,,,);
IP4_ADDR(&ipaddr, ,,,);
IP4_ADDR(&netmask, ,,,);
/* The lwIP single-threaded core: initialize the network stack */
lwip_init();
#else
/* lwIP in a multi-threaded system: initialize the network stack */
tcpip_init(tcpip_init_done, tcpip_init_done_param);
/* implicitly calls lwip_init();
start new thread calls tcpip_init_done(tcpip_init_done_param);
when tcpip init done tcpip_init_done and tcpip_init_done_param are user-defined (may be NULL). */
/* todo: wait for tcpip_init_done() ? */
#endif
/* Bring up the network interface */
/* Hint: netif_init(); was already called by lwip_init(); above */
netif_add(&netif, &ipaddr, &netmask, &gw, NULL, ethhw_init, ethernet_input);
/* ethhw_init() is user-defined */
/* use ip_input instead of ethernet_input for non-ethernet hardware */
/* (this function is assigned to netif.input and should be called by the hardware driver) */
netif_set_default(&netif);
netif_set_up(&netif);

更多关于tcpip_init的详细信息请查看Initialization using tcpip.c。关于网络接口的管理详情请看Network interfaces management

在初始化完后,会发生什么呢?你可以继续阅读lwIP with or without an operating system。完整的例子可以在contrib/ports/unix/proj/路径中找到。

3. Initialization using tcpip.c

如果你打算使用tcpip线程,下面是lwIP初始化的例子:

#include "lwip/tcpip.h"  

 struct netif my_netif;  

 err_t my_ip_init(struct ip_addr *ipaddr,
struct ip_addr *netmask,
struct ip_addr *gw,
err_t (* init)(struct netif *netif),
err_t (* input)(struct pbuf *p, struct netif *netif)) { // Start the TCP/IP thread & init stuff
tcpip_init(NULL, NULL);
// WARNING: This must only be run after the OS has been started.
// Typically this is the case, however, if not, you must place this
// in a post-OS initialization
return netifapi_netif_add(&my_netif, ipaddr, netmask, gw, NULL, init, input);
}

注:这个例子虽然假设使用静态IP,但是你可以将netif_set_up替换成dhcp_start或者autoip_start(详细请看Network interfaces management)

4.Network interfaces management

lwIP网络物理层的设备驱动被一个网络接口结构体所描述,与BSD很相似。网络接口保存在一个全局链表内,且被下一个指针所链接。

4.1 Starting a network interface

Step One: Add the interface

为了创建一个新的网络接口,用户 为结构体 netif分配内存空间(但是初始化它的任何部分),并调用netif_add:

struct *netif netif_add(struct netif *mynetif, struct ip_addr *ipaddr, struct ip_addr *netmask,
struct ip_addr *gw, void *state, err_t (* init)(struct netif *netif),
err_t (* input)(struct pbuf *p, struct netif *netif));

用户指定一个IP地址,子网掩码和网关地址给该接口,这些值随后可以被改变

(1)state

State是一个特定于驱动的结构体,该结构体定义了许多其他”state”的必要信息来完成驱动的功能。

一些驱动会要求先设置state变量,然后再调用netif_add,但是很多时候要求这个参数为NULL。检查你的驱动可以获得更多信息

(2)init

init要求一个设备驱动初始化的函数,该函数一旦被调用netif结构体已经被netif_add准备好了。如果该驱动已经被初始化在你其它地方的代码,这个参数可以设为NULL

(3)input

当它接收到新的数据包时,参数input将会被一个驱动调用。这个参数将会需要如下一些函数:

ethernet_input

如果你不是在一个单线程环境中使用lwIP协议栈并且该驱动使用ARP(比如,以太网设备),则该驱动将会调用该函数来处理ARP数据包包括IP数据包。

ip_input

如果你不是在一个单线程环境中使用lwIP协议栈并且该接口并不是一个以太网设备,则该驱动会直接调用IP协议。

tcpip_ethinput

如果你使用tcpip应用线程(查看lwIP多线程),则该驱动将使用ARP,并且要定义lwIP的选项ETHARP_TCPIP_ETHINPUT。该函数将被驱动使用来将所有的IP和ARP数据包发给input函数

tcpip_input

如果你使用tcpip应用线程并且已经定义了ETHART_TCPIP_INPUT选项。则该函数被驱动使用来将IP数据包发给input函数。(驱动会分开ARP数据包并将它直接发送给ARP模块)。(注意:在lwip1.4.1里并没有tcpip_ethinput(),tcp_input将会处理所有的ARP数据包)

Step Two: Bring the interface up(打开接口)

一个接口如果是up状态,那么你的应用程序就可以使用该接口进行输入和输出,如果是down状态,则相反。因此,在使用该接口前,你一定要打开该接口(状态为up)。打开接口动作的完成取决于该接口如何得到IP地址。打开/关闭(up/dwon)函数对每种获得IP地址方式的区别如下所示:

n 静态IP地址:使用netif_set_up和netif_set_down。在你将接口打开前,你要确保已经设置了IP地址(通过netif_add或者netif_set_addr调用)。

n DHCP:使用dhcp_start和dhcp_stop。dhcp_start函数将会打开该接口,当它获得一个地址时设置IP地址。

n AUTOIP:使用autoip_start和autoip_stop。当它选中一个IP地址时,Autoip将会打开该接口并设置IP地址。

为了测试一个netif是否打开,你可以使用netif_is_up

4.2 Further netif management

如果你加入一个netif接口,则该接口将会被当成默认的接口,于是你可以调用netif_set_default来确定默认的接口。当IP协议栈试图选择合适的路径(路由)来发送数据包时,如果该协议栈不能决定使用哪个正确的接口,那么它将通过默认的接口发送数据。如果你使用一个静态IP地址,那么你可以通过函数netif_set_ipaddr,netif_set_gw,和netif_set_netmask来设置该网络接口的相关的参数。当然你也可以使用netif_set_addr来一次设置这3个地址参数。当在文件lwipopts.h中设置LWIP_NETIF_STATUS_CALLBACK时,则该协议栈将提供一个状态回调函数。当接口打开或关闭时,则该状态回调函数将会被调用。举例,当你想要记录IP地址(该IP地址通过DHCP来获得)并想知道该地址的变化情况时,你就可以使用状态函数的钩子函数。设置一个netif接口的状态回调函数如下所示:

void netif_set_status_callback(struct netif *netif, netif_status_callback_fn status_callback);

LwIP Application Developers Manual11---Initializing lwIP的更多相关文章

  1. LwIP Application Developers Manual2---Protocols概览

    1.前言 本文是对LwIP Application Developers Manual的翻译 lwIP是模块化的并支持广泛的协议,这些大部分协议可以被裁减从而减小代码的尺寸 2.协议概览 链路层和网络 ...

  2. LwIP Application Developers Manual1---介绍

    1.前言 本文主要是对LwIP Application Developers Manual的翻译 2.读者(应用开发手册的读者) 谁适合读这份手册 网络应用的开发者 想了解lwIP的网络应用开发者 阅 ...

  3. LwIP Application Developers Manual5---高层协议之DHCP,AUTOIP,SNMP,PPP

    1.前言 本文主要讲述高层协议,包括DHCP 2.DHCP 2.1 从应用的角度看DHCP 你必须确保在编译和链接时使能DHCP,可通过在文件lwipopts.h里面定义LWIP_DHCP选项,该选项 ...

  4. LwIP Application Developers Manual7---lwIP with or without an operating system

    1.前言 最近有一些讨论关于lwIP如何在单机的环境(比如,没有一个多线程的操作系统)使用. 本文的目的就是描述lwIP如何在无多线程操作系统或有多线程操作系统环境中运行 2.lwIP单线程内核 2. ...

  5. LwIP Application Developers Manual9---LwIP and multithreading

    1.前言 lwIP的内核并不是线程安全的.如果我们必须在多线程环境里使用lwIP,那么我们必须使用“upper”API层的函数(netconn或sockets).当使用raw API时,你需要自己保护 ...

  6. LwIP Application Developers Manual8---Sample lwIP applications

    1.前言 你已经编译lwIP协议栈在你的目标平台上,并且网络驱动正常工作.你可以ping你的设备. 干得好,为你感到骄傲.虽然一个设备可以响应ping,但并不能算一个完整的应用. 现在你可以通过网络接 ...

  7. LwIP Application Developers Manual5---高层协议之DNS

    1.前言 lwIP提供一个基本的DNS客户端(1.3.0后引进),通过使用DNS(Domain Name System)协议来允许应用程序解决主机名到地址的转换. 在文件lwipopts.h里面定义L ...

  8. LwIP Application Developers Manual4---传输层之UDP、TCP

    1.前言 本文主要讲解传输层协议UDP TCP 2.UDP 2.1 UDP from an application perspective 2.2 UDP support history in lwI ...

  9. LwIP Application Developers Manual3---链路层和网络层协议之IPV6,ICMP,IGMP

    1.前言 本文主要讲述链路层和网络层的协议IPV6,ICMP 2.IPV6 2.1 IPV6特性 IPv6是IPv4的更新.其最显著的差别在于地址空间由32位转换成128位 2.2 从应用的角度看IP ...

  10. LwIP Application Developers Manual12---Configuring lwIP

    1.前言 2.LwIP makefiles With minimal features C_SOURCES = \ src/api/err.c \ src/core/init.c \ src/core ...

随机推荐

  1. influxDB和grafana

    influxdb启动服务 sudo service influxdb start 登录数据库 influx 在influxDB中,measurement相当于sql中的table, 插入measure ...

  2. springboot配置jsp

    spring.mvc.view.prefix= /WEB-INF/jsp/ spring.mvc.view.suffix= .jsp pom.xml <!--jsp支持--> <!- ...

  3. 使用Nexus配置Maven私有仓库

    使用Nexus配置Maven私有仓库 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.安装配置Nexus 1>.下载nexus 下载地址:https://www.sonat ...

  4. CentOS7 下 Hadoop 分布式部署

    Hadoop 服务划分 使用三台节点,集群部署规划如下 服务\主机 hadoop1 hadoop2 hadoop3 HDFS NameNode DataNode DataNode SecondaryN ...

  5. python 深浅拷贝 for循环删除

    ###########################总结########################### 1. 基础数据类型补充 大多数的基本数据类型的知识.已经学完了 a='aaaa' ls ...

  6. React 记录(2)

    入门教程:https://www.reactjscn.com/tutorial/tutorial.html 慢慢学习:对照教程文档,逐句猜解,截图 React官网:https://reactjs.or ...

  7. ACM-ICPC 2018 南京赛区网络预赛 C GDY(模拟)

    https://nanti.jisuanke.com/t/30992 题意 把m张牌(牌上数字范围是1-13)放到栈里n个人,每个人首次从栈顶取5张牌,轮流取取完牌后,第1个人出他手里最小的牌,然后2 ...

  8. redis的安装与简单使用

    redis的安装与简单使用: Redis简介: redis的应用场景: 1.关于关系型数据库和nosql数据库 关系型数据库是基于关系表的数据库,最终会将数据持久化到磁盘上,而nosql数据     ...

  9. C++引用以及定义常见问题总结

    最近在做数据结构的实验,暴露了一些典型问题,这里总结一下,避免以后出错 编译时报一个特别长的错:“无法解析的外部符号”,行号还是1,应该看一下定义的成员函数有没有还没有实现就被调用的,实验接口普遍挺多 ...

  10. 访问权限,public private protected

    百度经验这篇文章很不错:https://jingyan.baidu.com/article/bad08e1e8e9a9b09c851219f.html