Wed 20 Jun 2007 01:12:46 PM UTC, original submission:
I have discovered what I think is a bug in udp_input().
The problem is the pcb search and the local_match variable.
The code does something similar to:
local_match = 0;
for (pcb in udp_pcbs) {
if (local_end_matches()) {
some_stuff()
local_match = 1;
}
if (local_match != 0 && remote_end_matches()) {
break;
}
}
This means that if some pcb matches on the local side, the loop
will continue to search for a better match. A "later" pcb that matches on the remote side only will be considered a "full match" and the execution will break out of the for loop. The local side of that pcb need not neccesarry match the packet though.
Setting local_match to zero for each loop solves the problem.
Does that break anything else?
The following app reproduces the problem:
#define BUFLEN 64
void Lwip_UDP_Echo_Thread(void)
{
struct netconn *inconn;
struct netconn *outconn;
struct netbuf *buf;
struct ip_addr *addr;
unsigned short port;
struct ip_addr someaddr;
outconn = netconn_new(NETCONN_UDP);
inconn = netconn_new(NETCONN_UDP);
/* Need this connect() to place this pcb last in the pcb list. */
IP4_ADDR(&someaddr, 192, 168, 255, 255);
netconn_connect(outconn, &someaddr, 1234);
netconn_bind(inconn, NULL, 1111);
while (1)
{
buf = netconn_recv(inconn);
if (buf != NULL)
{
addr = netbuf_fromaddr(buf);
port = netbuf_fromport(buf);
/* Echo packet back to sender */
netconn_connect(outconn, addr, port);
netconn_send(outconn, buf);
netbuf_delete(buf);
}
}
}
The first packet sent to this app will be echoed back correctly, but all later incoming packets will be forwarded to the outconn connection instead of inconn.
|