In udp_send, pbuf_alloc is called only if pbuf_header(p, UDP_HLEN) fails. When you allocate a pbuf, you tell it what protocol you are going to use. So if you ask for 256 bytes for a UDP datagram, it will allocate 256+UDPHEADER+LINKHEADER bytes. You get the pbuf back and write to pbuf.payload[0].
Well, pbuf fooled you by actually setting payload to point to a memory address that really wasn't the start of the pbuf buffer space. Later on, it will subtract backwards from the current payload pointer as it goes through the stack. So if you write to payload[0], then the UDP header will get written to payload[-8] and the link header will get written to payload[-28]. Of course, every layer thinks it's writing to the front of the buffer, and the subtraction actually happens in that pbuf_header function. Udp_send doesn't know that you set the buffer up properly, so it first TRIES to call pbuf_header, and if you messed up by passing it a pbuf that didn't have room for the header, then it allocates a new pbuf to chain in front of the one you gave it. (You may not have messed up... for example, you may be passing a PBUF_REF or PBUF_ROM type, which doesn't have room for a header anyway) -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of David Baird Sent: Friday, July 13, 2007 1:23 PM To: Mailing list for lwIP users Subject: Re: [lwip-users] udp_send: space for IP header is not being allocated? On 7/13/07, Robin Iddon <[EMAIL PROTECTED]> wrote: > I might be barking up the wrong tree here, but when you allocate the > pbuf for your user data you pass in a flag that says "TRANSPORT" and > this means that when the pbuf is allocated the transport header space > is pre-allocated. Now, if you're using UDP raw interface, I am not sure > in what way that is different from the normal udp_send(). Many thanks Robin. But now I have another question: why does ``udp_send`` bother to allocate more pbufs. It seems like that should not be required since the space is already supposed to be pre-allocated? Sorry for stepping on anyone's toes by calling this a bug. In order to figure this out I was looking at doc/rawapi.txt, reading the lwIP source code, and looking at examples on the web. Somehow I missed PBUF_TRANSPORT detail. I think that the doc/rawapi.txt file could be improved by mentioning the proper way to allocate memory. With that said, I've attached a patch :-) -David Index: doc/rawapi.txt =================================================================== RCS file: /sources/lwip/lwip/doc/rawapi.txt,v retrieving revision 1.7 diff -u -r1.7 rawapi.txt --- doc/rawapi.txt 2 Mar 2007 19:35:15 -0000 1.7 +++ doc/rawapi.txt 13 Jul 2007 20:17:15 -0000 @@ -279,7 +279,11 @@ - err_t udp_send(struct udp_pcb *pcb, struct pbuf *p) - Sends the pbuf p. The pbuf is not deallocated. + Sends the pbuf p. The pbuf is not deallocated. To allocate pbufs, + make sure to use the PBUF_TRANSPORT flag to ensure that enough space + is allocated for UDP and IP headers, e.g.: + + p = pbuf_alloc(PBUF_TRANSPORT, num_bytes, PBUF_POOL); - void udp_recv(struct udp_pcb *pcb, void (* recv)(void *arg, struct udp_pcb *upcb, _______________________________________________ lwip-users mailing list [email protected] http://lists.nongnu.org/mailman/listinfo/lwip-users _______________________________________________ lwip-users mailing list [email protected] http://lists.nongnu.org/mailman/listinfo/lwip-users
