Located at: lwip-x.x.x/src/CORE/tcp. c
Prototype: err_t tcp_bind (struct tcp_pcb * PCB, struct ip_addr * ipaddr, u16_t port)
Function: bind to a local port number and IP address.
Function source code:
/** <Br/> * binds the connection to a local port number and IP address. if the <br/> * IP address is not given (I. E ., ipaddr = NULL), the IP address of <br/> * the outgoing network interface is used instead. <br/> * the IP address is not provided (ipaddr = NULL ), then the IP address is replaced by the output Network Interface <br/> * @ Param PCB the tcp_pcb to bind (no check is done whether this PCB is <br/> * already bound !) <Br/> * @ Param ipaddr the local IP address to bind to (use ip_addr_any to bind <br/> * to any local address <br/> * @ Param port the local port to bind to <br/> * @ return err_use if the port is already in use <br/> * err_ OK if bound is successfully bound <br/> */<br/> err_t <br/> tcp_bind (struct tcp_pcb * PCB, struct ip_addr * ipaddr, u16_t port) <br/>{< br/> struct tcp_pcb * CPCB; </P> <p> lwip_error ("tcp_connect: Can o Nly bind in State closed ", PCB-> state = closed, return err_isconn); </P> <p> If (Port = 0) {<br/> Port = tcp_new_port (); // return a new (free) local TCP port number <br/>}< br/>/* check if the address already is in use. */<br/>/* Check the listen PCB. */<br/>/* Check */<br/> for (CPCB = (struct tcp_pcb *) tcp_listen_rjs.pcb;/* Note 1 */<br/> CPCB! = NULL; CPCB = CPCB-> next) {<br/> If (CPCB-> local_port = port) {<br/> If (ip_addr_isany (& (CPCB-> local_ip)/* All IP addresses in the monitored PCB list are 0 or no IP addresses are assigned, the function returns 1 */| <br/> ip_addr_isany (ipaddr)/* Check parameter IP Address */| <br/> ip_addr_cmp (& (CPCB-> local_ip ), ipaddr)/* check whether the IP address of the parameter is the same as the IP address in the monitored PCB list */{<br/> return err_use; // returned port used information <br/>}< br/>/* Check the connected PCB. check the currently connected PCB */<br/> for (CPCB = tcp_acti Ve_pcb; <br/> CPCB! = NULL; CPCB = CPCB-> next) {<br/> If (CPCB-> local_port = port) {<br/> If (ip_addr_isany (& (CPCB-> local_ip) | <br/> ip_addr_isany (ipaddr) | <br/> ip_addr_cmp (& (CPCB-> local_ip), ipaddr) {<br/> return err_use; <br/>}< br/>/* Check the bound, not yet connected PCB. check the bound but not connected PCB */<br/> for (CPCB = tcp_bound_pcb; CPCB! = NULL; CPCB = CPCB-> next) {<br/> If (CPCB-> local_port = port) {<br/> If (ip_addr_isany (& (CPCB-> local_ip) | <br/> ip_addr_isany (ipaddr) | <br/> ip_addr_cmp (& (CPCB-> local_ip), ipaddr) {<br/> return err_use; <br/>}< br/>/* @ todo: Until so_reuseaddr is implemented (see task #6995 on savanner ), <br/> * we have to check the PCB in time-Wait state, also: */<br/> for (CPCB = TCP _ Tw_pcb; CPCB! = NULL; CPCB = CPCB-> next) {<br/> If (CPCB-> local_port = port) {<br/> If (ip_addr_cmp (& (CPCB-> local_ip), ipaddr) {<br/> return err_use; <br/>}</P> <p> If (! Ip_addr_isany (ipaddr) {<br/> PCB-> local_ip = * ipaddr; // ip address to the currently connected PCB <br/>}< br/> PCB-> local_port = port; <br/> tcp_reg (& tcp_bound_pcb, PCB ); // Add the current PCB to the bound tcp_pcb list <br/> lwip_debugf (tcp_debug, ("tcp_bind: bind to port %" u16_f "/N", Port )); <br/> return err_ OK; <br/>}< br/>
Analysis: Most of the code of this function is used to check whether the given IP address and port number are suitable. If so, assign the given IP address and port number to the current PCB, update the list of bound tcp_pcb, and return err_ OK. if the given parameter is not suitable, err_use is returned.
If the ipaddr parameter is ip_addr_any, it indicates that it is bound to any local address. What is ip_addr_any? The lwip-1.3.0/src/include/IPv4/LWIP/ip_addr.h defines:
# Define ip_addr_any (struct ip_addr *) & ip_addr_any)
Ip_addr_any is an ip_addr variable, which has the following declaration in 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 };
Therefore, ip_addr_any is equal to 0x00000000ul. 0.0.0.0 indicates the broadcast address on the IP address, that is, any address.
NOTE 1: The following definitions are available in TCP. h:
/* The tcp pcb lists. tcp pcb list */
Union tcp_listen_pcbs_t {/* List of all tcp pcb in listen state. All tcp pcb lists that enter the listening state */
Struct tcp_pcb_listen * listen_pcb;
Struct tcp_pcb * PCB;
};
Extern Union tcp_listen_pcbs_t tcp_listen_pcb;