Added assertion checking against dupes in pcb active list. Added comments.

This commit is contained in:
likewise 2003-04-01 14:52:41 +00:00
parent 7842a0190a
commit 7abb278b60

View File

@ -449,14 +449,15 @@ udp_bind(struct udp_pcb *pcb, struct ip_addr *ipaddr, u16_t port)
{ {
struct udp_pcb *ipcb; struct udp_pcb *ipcb;
u8_t rebind; u8_t rebind;
DEBUGF(UDP_DEBUG | DBG_TRACE | 3, ("udp_bind(ipaddr = %lx, port = %u)\n", ipaddr->addr, port));
rebind = 0; rebind = 0;
/* Check for double bind and rebind of the same pcb */ /* Check for double bind and rebind of the same pcb */
for(ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) { for(ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
/* is this UDP PCB already on active list? */ /* is this UDP PCB already on active list? */
if (pcb == ipcb) { if (pcb == ipcb) {
/* TODO: add assert that rebind is 0 here (pcb may /* pcb may occur at most once in active list */
occur at most once in list) */ LWIP_ASSERT("rebind == 0", rebind == 0);
/* pcb already in list, just rebind */
rebind = 1; rebind = 1;
} }
/* this code does not allow upper layer to share a UDP port for /* this code does not allow upper layer to share a UDP port for
@ -478,6 +479,7 @@ udp_bind(struct udp_pcb *pcb, struct ip_addr *ipaddr, u16_t port)
} }
/* bind local address */ /* bind local address */
ip_addr_set(&pcb->local_ip, ipaddr); ip_addr_set(&pcb->local_ip, ipaddr);
/* no port specified? */
if (port == 0) { if (port == 0) {
#ifndef UDP_LOCAL_PORT_RANGE_START #ifndef UDP_LOCAL_PORT_RANGE_START
#define UDP_LOCAL_PORT_RANGE_START 4096 #define UDP_LOCAL_PORT_RANGE_START 4096
@ -485,30 +487,31 @@ udp_bind(struct udp_pcb *pcb, struct ip_addr *ipaddr, u16_t port)
#endif #endif
port = UDP_LOCAL_PORT_RANGE_START; port = UDP_LOCAL_PORT_RANGE_START;
ipcb = udp_pcbs; ipcb = udp_pcbs;
while((ipcb != NULL) && (port != UDP_LOCAL_PORT_RANGE_END)) { while ((ipcb != NULL) && (port != UDP_LOCAL_PORT_RANGE_END)) {
if(ipcb->local_port == port) { if (ipcb->local_port == port) {
port++; port++;
ipcb = udp_pcbs; ipcb = udp_pcbs;
} else } else
ipcb = ipcb->next; ipcb = ipcb->next;
} }
if(ipcb) { if (ipcb != NULL) {
/* no more ports available in local range */ /* no more ports available in local range */
DEBUGF(UDP_DEBUG, ("udp_bind: out of free UDP ports\n")); DEBUGF(UDP_DEBUG, ("udp_bind: out of free UDP ports\n"));
return ERR_USE; return ERR_USE;
} }
} }
pcb->local_port = port; pcb->local_port = port;
/* We need to place the PCB on the list if not already there. */ /* pcb not active yet? */
if (rebind == 0) { if (rebind == 0) {
/* place the PCB on the active list if not already there */
pcb->next = udp_pcbs; pcb->next = udp_pcbs;
udp_pcbs = pcb; udp_pcbs = pcb;
} }
DEBUGF(UDP_DEBUG | DBG_TRACE | DBG_STATE, ("udp_bind: bound to %u.%u.%u.%u, port %u\n", DEBUGF(UDP_DEBUG | DBG_TRACE | DBG_STATE, ("udp_bind: bound to %u.%u.%u.%u, port %u\n",
(u8_t)(ntohl(ipaddr->addr) >> 24 & 0xff), (u8_t)(ntohl(ipaddr->addr) >> 24 & 0xff),
(u8_t)(ntohl(ipaddr->addr) >> 16 & 0xff), (u8_t)(ntohl(ipaddr->addr) >> 16 & 0xff),
(u8_t)(ntohl(ipaddr->addr) >> 8 & 0xff), (u8_t)(ntohl(ipaddr->addr) >> 8 & 0xff),
(u8_t)(ntohl(ipaddr->addr) & 0xff), port)); (u8_t)(ntohl(ipaddr->addr) & 0xff), port));
return ERR_OK; return ERR_OK;
} }
/** /**