I have been doing a small tcp project, in C, on a Raspberry Pi Pico, experimenting with the lwIP api. One of the sources of help has been the pico-sdk-examples. When the lwip stack receives a message, it summons a callback to a method of the programmer's choice. In the pico examples, that method contains the following code for handling the message, which is passed into the callback in a structure known as a pbuf. You can chain pbufs together for longer messages.
//state is a protocol control
//p is a pbuf
// Receive the buffer
const uint16_t buffer_left = BUF_SIZE - state->recv_len;
state->recv_len += pbuf_copy_partial(p, state->buffer_recv + state->recv_len,
p->tot_len > buffer_left ? buffer_left : p->tot_len, 0);
tcp_recved(tpcb, p->tot_len);
My question is about the tcp_recved component. In the lwip documentation it says that
This function should be called by the application when it has processed the data. The purpose is to advertise a larger window when the data has been processed.
If the total_len is greater than the buffer size, and the buffer is filled, why does their code advertise that the tot_len was recved. Will that buffer overflow be lost? Also, why don't we use p->len?
In answer to why we don't use p->len, we use p->tot_len because we may want to read the entire pbuf chain. p->len might just be the first x characters, stored in the first pbuf; p->tot_len is the length of the entire string. However, I still do not know what happens if a message is too large.
Edit: If the message is larger than the buffer then the excess will be lost.