Raw TCP/IP interface for lwIP
Authors: Adam Dunkels, Leon Woestenberg, Christiaan Simons
lwIP provides three Application Program's Interfaces (APIs) for programs
to use for communication with the TCP/IP code:
* low-level "core" / "callback" or "raw" API.
* higher-level "sequential" API.
* BSD-style socket API.
The raw API (sometimes called native API) is an event-driven API designed
to be used without an operating system that implements zero-copy send and
receive. This API is also used by the core stack for interaction between
the various protocols. It is the only API available when running lwIP
without an operating system.
The sequential API provides a way for ordinary, sequential, programs
to use the lwIP stack. It is quite similar to the BSD socket API. The
model of execution is based on the blocking open-read-write-close
paradigm. Since the TCP/IP stack is event based by nature, the TCP/IP
code and the application program must reside in different execution
contexts (threads).
The socket API is a compatibility API for existing applications,
currently it is built on top of the sequential API. It is meant to
provide all functions needed to run socket API applications running
on other platforms (e.g. unix / windows etc.). However, due to limitations
in the specification of this API, there might be incompatibilities
that require small modifications of existing programs.
** Multithreading
lwIP started targeting single-threaded environments. When adding multi-
threading support, instead of making the core thread-safe, another
approach was chosen: there is one main thread running the lwIP core
(also known as the "tcpip_thread"). When running in a multithreaded
environment, raw API functions MUST only be called from the core thread
since raw API functions are not protected from concurrent access (aside
from pbuf- and memory management functions). Application threads using
the sequential- or socket API communicate with this main thread through
message passing.
As such, the list of functions that may be called from
other threads or an ISR is very limited! Only functions
from these API header files are thread-safe:
- api.h
- netbuf.h
- netdb.h
- netifapi.h
- pppapi.h
- sockets.h
- sys.h
Additionaly, memory (de-)allocation functions may be
called from multiple threads (not ISR!) with NO_SYS=0
since they are protected by SYS_LIGHTWEIGHT_PROT and/or
semaphores.
Netconn or Socket API functions are thread safe against the
core thread but they are not reentrant at the control block
granularity level. That is, a UDP or TCP control block must
not be shared among multiple threads without proper locking.
If SYS_LIGHTWEIGHT_PROT is set to 1 and
LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT is set to 1,
pbuf_free() may also be called from another thread or
an ISR (since only then, mem_free - for PBUF_RAM - may
be called from an ISR: otherwise, the HEAP is only
protected by semaphores).
** The remainder of this document discusses the "raw" API. **
The raw TCP/IP interface allows the application program to integrate
better with the TCP/IP code. Program execution is event based by
having callback functions being called from within the TCP/IP
code. The TCP/IP code and the application program both run in the same
thread. The sequential API has a much higher overhead and is not very
well suited for small systems since it forces a multithreaded paradigm
on the application.
The raw TCP/IP interface is not only faster in terms of code execution
time but is also less memory intensive. The drawback is that program
development is somewhat harder and application programs written for
the raw TCP/IP interface are more difficult to understand. Still, this
is the preferred way of writing applications that should be small in
code size and memory usage.
All APIs can be used simultaneously by different application
programs. In fact, the sequential API is implemented as an application
program using the raw TCP/IP interface.
Do not confuse the lwIP raw API with raw Ethernet or IP sockets.
The former is a way of interfacing the lwIP network stack (including
TCP and UDP), the later refers to processing raw Ethernet or IP data
instead of TCP connections or UDP packets.
Raw API applications may never block since all packet processing
(input and output) as well as timer processing (TCP mainly) is done
in a single execution context.
--- Callbacks
Program execution is driven by callbacks functions, which are then
invoked by the lwIP core when activity related to that application
occurs. A particular application may register to be notified via a
callback function for events such as incoming data available, outgoing
data sent, error notifications, poll timer expiration, connection
closed, etc. An application can provide a callback function to perform
processing for any or all of these events. Each callback is an ordinary
C function that is called from within the TCP/IP code. Every callback
function is passed the current TCP or UDP connection state as an
argument. Also, in order to be able to keep program specific state,
the callback functions are called with a program specified argument
that is independent of the TCP/IP state.
The function for setting the application connection state is:
- void tcp_arg(struct tcp_pcb *pcb, void *arg)
Specifies the program specific state that should be passed to all
other callback functions. The "pcb" argument is the current TCP
connection control block, and the "arg" argument is the argument
that will be passed to the callbacks.
--- TCP connection setup
The functions used for setting up connections is similar to that of
the sequential API and of the BSD socket API. A new TCP connection
identifier (i.e., a protocol control block - PCB) is created with the
tcp_new() function. This PCB can then be either set to listen for new
incoming connections or be explicitly connected to another host.
- struct tcp_pcb *tcp_new(void)
Creates a new connection identifier (PCB). If memory is not
available for creating the new pcb, NULL is returned.
- err_t tcp_bind(struct tcp_pcb *pcb, ip_addr_t *ipaddr,
u16_t port)
Binds the pcb to a local IP address and port number. The IP address
can be specified as IP_ADDR_ANY in order to bind the connection to
all local IP addresses.
If another connection is bound to the same port, the function will
return ERR_USE, otherwise ERR_OK is returned.
- struct tcp_pcb *tcp_listen(struct tcp_pcb *pcb)
Commands a pcb to start listening for incoming connections. When an
incoming connection is accepted, the function specified with the
tcp_accept() function will be called. The pcb will have to be bound
to a local port with the tcp_bind() function.
The tcp_listen() function returns a new connection identifier, and
the one passed as an argument to the function will be
deallocated. The reason for this behavior is that less memory is
needed for a connection that is listening, so tcp_listen() will
reclaim the memory needed for the original connection and allocate a
new smaller memory block for the listening connection.
tcp_listen() may return NULL if no memory was available for the
listening connection. If so, the memory associated with the pcb
passed as an argument to tcp_listen() will not be deallocated.
- struct tcp_pcb *tcp_listen_with_backlog(struct tcp_pcb *pcb, u8_t backlog)
Same as tcp_listen, but limits the number of outstanding connections
in the listen queue to the value specified by the backlog argument.
To use it, your need to set TCP_LISTEN_BACKLOG=1 in your lwipopts.h.
- void tcp_accept(struct tcp_pcb *pcb,
err_t (*
没有合适的资源?快使用搜索试试~ 我知道了~
GD32F10x_Firmware_Library_V2.2.2.rar GD32F10x_固件库
共1406个文件
h:569个
c:518个
txt:130个
需积分: 49 22 下载量 178 浏览量
2022-03-20
21:30:48
上传
评论
收藏 4.17MB RAR 举报
温馨提示
GD32F10x_Firmware_Library_V2.2.2.rar GD32F10x_固件库
资源详情
资源评论
资源推荐
收起资源包目录
GD32F10x_Firmware_Library_V2.2.2.rar
GD32F10x_固件库 (1406个子文件)
LCD.bin 82KB
LCD.bin 45KB
LED.bin 2KB
LED.bin 1KB
router_adv.bin 118B
icmp_ping.bin 98B
neighbor_solicitation.bin 86B
tcp_syn.bin 74B
udp_port_5000.bin 50B
arp_req.bin 42B
ffunicode.c 1.88MB
flash_data.c 282KB
flash_data.c 282KB
ff.c 229KB
picture.c 141KB
gd32f10x_enet.c 130KB
sdcard.c 96KB
sockets.c 93KB
httpd.c 88KB
gd32f10x_timer.c 85KB
lcp.c 74KB
lcd_font.c 73KB
dhcp.c 72KB
nd6.c 69KB
tcp_in.c 69KB
tcp.c 68KB
snmp_msg.c 67KB
auth.c 66KB
mdns.c 65KB
ipcp.c 64KB
eap.c 63KB
api_msg.c 62KB
tcp_out.c 58KB
dns.c 52KB
ppp.c 49KB
ccp.c 49KB
pbuf.c 47KB
etharp.c 46KB
mqtt.c 44KB
ipv6cp.c 42KB
gd32f10x_rcu.c 42KB
gd32f10x_can.c 42KB
snmp_core.c 42KB
lowpan6.c 41KB
ip6.c 39KB
test_dhcp.c 39KB
ip4.c 39KB
pppol2tp.c 39KB
udp.c 39KB
netif.c 38KB
pppoe.c 38KB
gd32f10x_adc.c 37KB
test_tcp_oos.c 36KB
gd32f103c_audio_codec.c 36KB
gd32f10x_audio_codec.c 36KB
makefsdata.c 35KB
system_gd32f10x.c 33KB
chap_ms.c 33KB
api_lib.c 32KB
system_gd32f10x.c 32KB
system_gd32f10x.c 32KB
system_gd32f10x.c 32KB
system_gd32f10x.c 32KB
system_gd32f10x.c 32KB
system_gd32f10x.c 32KB
system_gd32f10x.c 32KB
system_gd32f10x.c 32KB
system_gd32f10x.c 32KB
system_gd32f10x.c 32KB
system_gd32f10x.c 32KB
system_gd32f10x.c 32KB
system_gd32f10x.c 32KB
system_gd32f10x.c 32KB
system_gd32f10x.c 32KB
system_gd32f10x.c 32KB
system_gd32f10x.c 32KB
system_gd32f10x.c 32KB
system_gd32f10x.c 32KB
system_gd32f10x.c 32KB
system_gd32f10x.c 32KB
system_gd32f10x.c 32KB
system_gd32f10x.c 32KB
gd32f10x_fmc.c 31KB
gd32f10x_exmc.c 30KB
ccsbcs.c 29KB
pppos.c 29KB
test_mdns.c 29KB
snmp_mib2_ip.c 28KB
gd32f10x_sdio.c 28KB
gd32f10x_dma.c 28KB
ip4_frag.c 28KB
igmp.c 28KB
mem.c 27KB
ip6_frag.c 27KB
audio_core.c 26KB
at24cxx.c 26KB
lwiperf.c 26KB
gd32f10x_usart.c 25KB
gd32f10x_i2c.c 25KB
gd32f10x_spi.c 24KB
共 1406 条
- 1
- 2
- 3
- 4
- 5
- 6
- 15
weixin_45207764
- 粉丝: 16
- 资源: 36
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Screenshot_20240427_031602.jpg
- 网页PDF_2024年04月26日 23-46-14_QQ浏览器网页保存_QQ浏览器转格式(6).docx
- 直接插入排序,冒泡排序,直接选择排序.zip
- 在排序2的基础上,再次对快排进行优化,其次增加快排非递归,归并排序,归并排序非递归版.zip
- 实现了7种排序算法.三种复杂度排序.三种nlogn复杂度排序(堆排序,归并排序,快速排序)一种线性复杂度的排序.zip
- 冒泡排序 直接选择排序 直接插入排序 随机快速排序 归并排序 堆排序.zip
- 课设-内部排序算法比较 包括冒泡排序、直接插入排序、简单选择排序、快速排序、希尔排序、归并排序和堆排序.zip
- Python排序算法.zip
- C语言实现直接插入排序、希尔排序、选择排序、冒泡排序、堆排序、快速排序、归并排序、计数排序,并带图详解.zip
- 常用工具集参考用于图像等数据处理
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0