tcp_bind/tcp_connect: Omit checking for the same port if no port specified

No port specified means to use a random port.
tcp_new_port() returns a new (free) local TCP port number on success.
So in this case we don't need iterating all lists to test if the port
number is used, tcp_new_port() alreay ensures the port is not used.

Signed-off-by: Axel Lin <axel.lin@ingics.com>
This commit is contained in:
Axel Lin 2016-01-10 18:28:07 +08:00 committed by Dirk Ziegelmeier
parent b68e801975
commit 5056d375f2

View File

@ -464,8 +464,7 @@ tcp_bind(struct tcp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port)
if (port == 0) { if (port == 0) {
return ERR_BUF; return ERR_BUF;
} }
} } else {
/* Check if the address already is in use (on all lists) */ /* Check if the address already is in use (on all lists) */
for (i = 0; i < max_pcb_list; i++) { for (i = 0; i < max_pcb_list; i++) {
for (cpcb = *tcp_pcb_lists[i]; cpcb != NULL; cpcb = cpcb->next) { for (cpcb = *tcp_pcb_lists[i]; cpcb != NULL; cpcb = cpcb->next) {
@ -489,6 +488,7 @@ tcp_bind(struct tcp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port)
} }
} }
} }
}
if (!ip_addr_isany(ipaddr)) { if (!ip_addr_isany(ipaddr)) {
ip_addr_set(&pcb->local_ip, ipaddr); ip_addr_set(&pcb->local_ip, ipaddr);
@ -785,7 +785,7 @@ tcp_connect(struct tcp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port,
if (pcb->local_port == 0) { if (pcb->local_port == 0) {
return ERR_BUF; return ERR_BUF;
} }
} } else {
#if SO_REUSE #if SO_REUSE
if (ip_get_option(pcb, SOF_REUSEADDR)) { if (ip_get_option(pcb, SOF_REUSEADDR)) {
/* Since SOF_REUSEADDR allows reusing a local address, we have to make sure /* Since SOF_REUSEADDR allows reusing a local address, we have to make sure
@ -807,6 +807,8 @@ tcp_connect(struct tcp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port,
} }
} }
#endif /* SO_REUSE */ #endif /* SO_REUSE */
}
iss = tcp_next_iss(); iss = tcp_next_iss();
pcb->rcv_nxt = 0; pcb->rcv_nxt = 0;
pcb->snd_nxt = iss; pcb->snd_nxt = iss;