位于:位于:lwip-x.x.x/src/core/tcp.c
原型: err_ttcp_bind(struct tcp_pcb *pcb, struct ip_addr *ipaddr, u16_t port)
功能:绑定连接到一个本地端口号和IP地址.
函数源码:
/**
* Binds the connection to a local port number and IP address. If the
* IP address is not given (i.e., ipaddr == NULL), the IP address of
* the outgoing network interface is used instead.
*IP地址没有给出(即ipaddr==NULL),则IP地址由输出网络接口代替
* @param pcb the tcp_pcb to bind (no check is done whether this pcb is
* already bound!)
* @param ipaddr the local ip address to bind to (use IP_ADDR_ANY to bind
* to any local address
* @param port the local port to bind to
* @return ERR_USE if the port is already in use
* ERR_OK if bound 成功绑定
*/
err_t
tcp_bind(struct tcp_pcb *pcb, struct ip_addr *ipaddr, u16_t port)
{
struct tcp_pcb *cpcb;
LWIP_ERROR("tcp_connect: can only bind in state CLOSED", pcb->state == CLOSED, return ERR_ISCONN);
if (port == 0) {
port = tcp_new_port(); //返回一个新的(自由的)本地TCP端口号
}
/* Check if the address already is in use. */
/* Check the listen pcbs. */
/* 检查*/
for(cpcb = (struct tcp_pcb *)tcp_listen_pcbs.pcbs;/*注1 */
cpcb != NULL; cpcb = cpcb->next) {
if (cpcb->local_port == port) {
if (ip_addr_isany (&(cpcb->local_ip)) /*已监听pcb列表中IP地址全为0或没有分配IP地址,则函数返回1*/||
ip_addr_isany(ipaddr) /*检验参数IP地址*/||
ip_addr_cmp(&(cpcb->local_ip), ipaddr)) /*检验参数IP和已监听pcb列表中IP地址是否相同*/{
return ERR_USE; //返回端口已被使用信息
}
}
}
/* Check the connected pcbs. 检查当前连接的pcbs*/
for(cpcb = tcp_active_pcbs;
cpcb != NULL; cpcb = cpcb->next) {
if (cpcb->local_port == port) {
if (ip_addr_isany(&(cpcb->local_ip)) ||
ip_addr_isany(ipaddr) ||
ip_addr_cmp(&(cpcb->local_ip), ipaddr)) {
return ERR_USE;
}
}
}
/* Check the bound, not yet connected pcbs.检查已绑定的还没有连接的pcbs */
for(cpcb = tcp_bound_pcbs; cpcb != NULL; cpcb = cpcb->next) {
if (cpcb->local_port == port) {
if (ip_addr_isany(&(cpcb->local_ip)) ||
ip_addr_isany(ipaddr) ||
ip_addr_cmp(&(cpcb->local_ip), ipaddr)) {
return ERR_USE;
}
}
}
/* @todo: until SO_REUSEADDR is implemented (see task #6995 on savannah),
* we have to check the pcbs in TIME-WAIT state, also: */
for(cpcb = tcp_tw_pcbs; cpcb != NULL; cpcb = cpcb->next) {
if (cpcb->local_port == port) {
if (ip_addr_cmp(&(cpcb->local_ip), ipaddr)) {
return ERR_USE;
}
}
}
if (!ip_addr_isany(ipaddr)) {
pcb->local_ip = *ipaddr; //IP地址给当前连接的pcb
}
pcb->local_port = port;
TCP_REG(&tcp_bound_pcbs, pcb); //将当前pcb放入已绑定tcp_pcb列表
LWIP_DEBUGF(TCP_DEBUG, ("tcp_bind: bind to port %"U16_F"/n", port));
return ERR_OK;
}
分析:这个函数的大部分代码用于检验给出的IP地址和端口号是否合适,如果合适则将给出的IP地址和端口号赋给当前PCB,更新已绑定tcp_pcb列表并返回ERR_OK.如果给出的参数不合适,则返回ERR_USE.
参数ipaddr如果为IP_ADDR_ANY,表示绑定到任何本地地址,那么IP_ADDR_ANY是什么呢?在lwip-1.3.0/src/include/ipv4/lwip/ip_addr.h中定义了:
#defineIP_ADDR_ANY((struct ip_addr *)&ip_addr_any)
ip_addr_any是一个ip_addr型变量,在lwip-1.3.0/src/core/ipv4/ip_addr.c中有如下声明:
#define IP_ADDR_ANY_VALUE 0x00000000UL
const struct ip_addr ip_addr_any = { IP_ADDR_ANY_VALUE };
所以, IP_ADDR_ANY是等于0x00000000UL的.在IP地址上规定0.0.0.0为广播地址,也就是任意地址的意思.
注1:在tcp.h中有如下定义:
/* The TCP PCB lists. TCP PCB列表*/
union tcp_listen_pcbs_t { /* List of all TCP PCBs in LISTEN state.进入监听状态的所有TCP PCB列表*/
struct tcp_pcb_listen *listen_pcbs;
struct tcp_pcb *pcbs;
};
extern union tcp_listen_pcbs_t tcp_listen_pcbs;
转载自:http://blog.csdn.net/zhzht19861011/article/details/6188400
分享到:
相关推荐
stm32f107VCT 实现LWIP_TCP client程序,利用网络调试助手可以接收客户端发送的数据。
lwip 协议 API,快速入手,可作为检索,LwIP的RAW API接口及编程指南。...首先,介绍了RAW API的特点及优缺点,然后逐个介绍了LwIP提供的所有的RAW API函数,最后通过实例的形式介绍了这些API函数具体编程的方法。
LWIP中tcp_write函数的理解 对lwip协议的一些分析
lwip移植加tcpip客户端功能代码,在ucos系统基础上移植lwip,并使用raw接口进行客户端服务编写
ZLG_LwIP的RAW_API接口及编程指南 ZLG_LwIP的RAW_API接口及编程指南 ZLG_LwIP的RAW_API接口及编程指南 ZLG_LwIP的RAW_API接口及编程指南
stm32f107VCT 实现服务端socket 编程server端程序部分,网络调试助手可以进行数据检验。
STM32F4x7_ETH_LwIP_V1.0.0
本实验采用LWIP的RAW编程方法实现TCP Server功能,默认开启DHCP自动获取IP地址。电脑端打开网络调试助手,网络调试助手做客户端,所以选择TCP Client协议。开发板做TCP服务器。
freertos ,stm32f107,sd卡
LWIP源码及移植说明LWIP源码及移植说明LWIP源码及移植说明
lm3sapp_ethernet_lwip_rawapi.7z
LwIP在STM32 107裸机LwIP,无操作系统 包括DHCP client,Telnet, TFTP server, web server 的实现
TCP/IP 协议栈LwIP的设计与实现,翻译 焦海波,很好的资料。
LwIP with STM32 src code(F407)
TCP服务端,UDP服务端,把接收到TCP客户端的信号后转发到UDP客户端
LwIP的RAW_API接口及编程指南.pdf
经过修改lwip2.0的bug后的协议栈, 我现在正在使用的,希望对大家有用。
嵌入式设备lwip移植中需要用到的相关函数说明,很详细。
本代码为在ARMLPC2210平台上,LwIP协议在UC_OS_II操作系统上的移植和实现;主要芯片有LPC2210、RTL8019S、TLI6C554(网口转4路串口通信)开发环境为ADS1.2