ReactOS 0.4.15-dev-7788-g1ad9096
pbuf.h File Reference
#include "lwip/opt.h"
#include "lwip/err.h"
Include dependency graph for pbuf.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  pbuf
 

Macros

#define LWIP_SUPPORT_CUSTOM_PBUF   (IP_FRAG && !IP_FRAG_USES_STATIC_BUF && !LWIP_NETIF_TX_SINGLE_PBUF)
 
#define PBUF_TRANSPORT_HLEN   20
 
#define PBUF_IP_HLEN   20
 
#define PBUF_FLAG_PUSH   0x01U
 
#define PBUF_FLAG_IS_CUSTOM   0x02U
 
#define PBUF_FLAG_MCASTLOOP   0x04U
 
#define PBUF_FLAG_LLBCAST   0x08U
 
#define PBUF_FLAG_LLMCAST   0x10U
 
#define PBUF_FLAG_TCP_FIN   0x20U
 
#define pbuf_init()
 

Enumerations

enum  pbuf_layer { PBUF_TRANSPORT , PBUF_IP , PBUF_LINK , PBUF_RAW }
 
enum  pbuf_type { PBUF_RAM , PBUF_ROM , PBUF_REF , PBUF_POOL }
 

Functions

struct pbufpbuf_alloc (pbuf_layer l, u16_t length, pbuf_type type)
 
void pbuf_realloc (struct pbuf *p, u16_t size)
 
u8_t pbuf_header (struct pbuf *p, s16_t header_size)
 
void pbuf_ref (struct pbuf *p)
 
u8_t pbuf_free (struct pbuf *p)
 
u8_t pbuf_clen (struct pbuf *p)
 
void pbuf_cat (struct pbuf *head, struct pbuf *tail)
 
void pbuf_chain (struct pbuf *head, struct pbuf *tail)
 
struct pbufpbuf_dechain (struct pbuf *p)
 
err_t pbuf_copy (struct pbuf *p_to, struct pbuf *p_from)
 
u16_t pbuf_copy_partial (struct pbuf *p, void *dataptr, u16_t len, u16_t offset)
 
err_t pbuf_take (struct pbuf *buf, const void *dataptr, u16_t len)
 
struct pbufpbuf_coalesce (struct pbuf *p, pbuf_layer layer)
 
u8_t pbuf_get_at (struct pbuf *p, u16_t offset)
 
u16_t pbuf_memcmp (struct pbuf *p, u16_t offset, const void *s2, u16_t n)
 
u16_t pbuf_memfind (struct pbuf *p, const void *mem, u16_t mem_len, u16_t start_offset)
 
u16_t pbuf_strstr (struct pbuf *p, const char *substr)
 

Macro Definition Documentation

◆ LWIP_SUPPORT_CUSTOM_PBUF

#define LWIP_SUPPORT_CUSTOM_PBUF   (IP_FRAG && !IP_FRAG_USES_STATIC_BUF && !LWIP_NETIF_TX_SINGLE_PBUF)

Currently, the pbuf_custom code is only needed for one specific configuration of IP_FRAG

Definition at line 45 of file pbuf.h.

◆ PBUF_FLAG_IS_CUSTOM

#define PBUF_FLAG_IS_CUSTOM   0x02U

indicates this is a custom pbuf: pbuf_free and pbuf_header handle such a a pbuf differently

Definition at line 69 of file pbuf.h.

◆ PBUF_FLAG_LLBCAST

#define PBUF_FLAG_LLBCAST   0x08U

indicates this pbuf was received as link-level broadcast

Definition at line 73 of file pbuf.h.

◆ PBUF_FLAG_LLMCAST

#define PBUF_FLAG_LLMCAST   0x10U

indicates this pbuf was received as link-level multicast

Definition at line 75 of file pbuf.h.

◆ PBUF_FLAG_MCASTLOOP

#define PBUF_FLAG_MCASTLOOP   0x04U

indicates this pbuf is UDP multicast to be looped back

Definition at line 71 of file pbuf.h.

◆ PBUF_FLAG_PUSH

#define PBUF_FLAG_PUSH   0x01U

indicates this packet's data should be immediately passed to the application

Definition at line 66 of file pbuf.h.

◆ PBUF_FLAG_TCP_FIN

#define PBUF_FLAG_TCP_FIN   0x20U

indicates this pbuf includes a TCP FIN flag

Definition at line 77 of file pbuf.h.

◆ pbuf_init

#define pbuf_init ( )

Definition at line 144 of file pbuf.h.

◆ PBUF_IP_HLEN

#define PBUF_IP_HLEN   20

Definition at line 48 of file pbuf.h.

◆ PBUF_TRANSPORT_HLEN

#define PBUF_TRANSPORT_HLEN   20

Definition at line 47 of file pbuf.h.

Enumeration Type Documentation

◆ pbuf_layer

Enumerator
PBUF_TRANSPORT 
PBUF_IP 
PBUF_LINK 
PBUF_RAW 

Definition at line 50 of file pbuf.h.

50 {
52 PBUF_IP,
pbuf_layer
Definition: pbuf.h:50
@ PBUF_RAW
Definition: pbuf.h:54
@ PBUF_TRANSPORT
Definition: pbuf.h:51
@ PBUF_LINK
Definition: pbuf.h:53
@ PBUF_IP
Definition: pbuf.h:52

◆ pbuf_type

Enumerator
PBUF_RAM 
PBUF_ROM 
PBUF_REF 
PBUF_POOL 

Definition at line 57 of file pbuf.h.

57 {
58 PBUF_RAM, /* pbuf data is stored in RAM */
59 PBUF_ROM, /* pbuf data is stored in ROM */
60 PBUF_REF, /* pbuf comes from the pbuf pool */
61 PBUF_POOL /* pbuf payload refers to RAM */
62} pbuf_type;
pbuf_type
Definition: pbuf.h:57
@ PBUF_ROM
Definition: pbuf.h:59
@ PBUF_RAM
Definition: pbuf.h:58
@ PBUF_REF
Definition: pbuf.h:60
@ PBUF_POOL
Definition: pbuf.h:61

Function Documentation

◆ pbuf_alloc()

struct pbuf * pbuf_alloc ( pbuf_layer  layer,
u16_t  length,
pbuf_type  type 
)

Allocates a pbuf of the given type (possibly a chain for PBUF_POOL type).

The actual memory allocated for the pbuf is determined by the layer at which the pbuf is allocated and the requested size (from the size parameter).

Parameters
layerflag to define header size
lengthsize of the pbuf's payload
typethis parameter decides how and where the pbuf should be allocated as follows:
  • PBUF_RAM: buffer memory for pbuf is allocated as one large chunk. This includes protocol headers as well.
  • PBUF_ROM: no buffer memory is allocated for the pbuf, even for protocol headers. Additional headers must be prepended by allocating another pbuf and chain in to the front of the ROM pbuf. It is assumed that the memory used is really similar to ROM in that it is immutable and will not be changed. Memory which is dynamic should generally not be attached to PBUF_ROM pbufs. Use PBUF_REF instead.
  • PBUF_REF: no buffer memory is allocated for the pbuf, even for protocol headers. It is assumed that the pbuf is only being used in a single thread. If the pbuf gets queued, then pbuf_take should be called to copy the buffer.
  • PBUF_POOL: the pbuf is allocated as a pbuf chain, with pbufs from the pbuf pool that is allocated during pbuf_init().
Returns
the allocated pbuf. If multiple pbufs where allocated, this is the first pbuf of a pbuf chain.

Definition at line 207 of file pbuf.c.

208{
209 struct pbuf *p, *q, *r;
211 s32_t rem_len; /* remaining length */
212 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc(length=%"U16_F")\n", length));
213
214 /* determine header offset */
215 switch (layer) {
216 case PBUF_TRANSPORT:
217 /* add room for transport (often TCP) layer header */
219 break;
220 case PBUF_IP:
221 /* add room for IP layer header */
223 break;
224 case PBUF_LINK:
225 /* add room for link layer header */
227 break;
228 case PBUF_RAW:
229 offset = 0;
230 break;
231 default:
232 LWIP_ASSERT("pbuf_alloc: bad pbuf layer", 0);
233 return NULL;
234 }
235
236 switch (type) {
237 case PBUF_POOL:
238 /* allocate head of pbuf chain into p */
239 p = (struct pbuf *)memp_malloc(MEMP_PBUF_POOL);
240 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc: allocated pbuf %p\n", (void *)p));
241 if (p == NULL) {
243 return NULL;
244 }
245 p->type = type;
246 p->next = NULL;
247
248 /* make the payload pointer point 'offset' bytes into pbuf data memory */
249 p->payload = LWIP_MEM_ALIGN((void *)((u8_t *)p + (SIZEOF_STRUCT_PBUF + offset)));
250 LWIP_ASSERT("pbuf_alloc: pbuf p->payload properly aligned",
251 ((mem_ptr_t)p->payload % MEM_ALIGNMENT) == 0);
252 /* the total length of the pbuf chain is the requested size */
253 p->tot_len = length;
254 /* set the length of the first pbuf in the chain */
256 LWIP_ASSERT("check p->payload + p->len does not overflow pbuf",
257 ((u8_t*)p->payload + p->len <=
259 LWIP_ASSERT("PBUF_POOL_BUFSIZE must be bigger than MEM_ALIGNMENT",
261 /* set reference count (needed here in case we fail) */
262 p->ref = 1;
263
264 /* now allocate the tail of the pbuf chain */
265
266 /* remember first pbuf for linkage in next iteration */
267 r = p;
268 /* remaining length to be allocated */
269 rem_len = length - p->len;
270 /* any remaining pbufs to be allocated? */
271 while (rem_len > 0) {
272 q = (struct pbuf *)memp_malloc(MEMP_PBUF_POOL);
273 if (q == NULL) {
275 /* free chain so far allocated */
276 pbuf_free(p);
277 /* bail out unsuccesfully */
278 return NULL;
279 }
280 q->type = type;
281 q->flags = 0;
282 q->next = NULL;
283 /* make previous pbuf point to this pbuf */
284 r->next = q;
285 /* set total length of this pbuf and next in chain */
286 LWIP_ASSERT("rem_len < max_u16_t", rem_len < 0xffff);
287 q->tot_len = (u16_t)rem_len;
288 /* this pbuf length is pool size, unless smaller sized tail */
289 q->len = LWIP_MIN((u16_t)rem_len, PBUF_POOL_BUFSIZE_ALIGNED);
290 q->payload = (void *)((u8_t *)q + SIZEOF_STRUCT_PBUF);
291 LWIP_ASSERT("pbuf_alloc: pbuf q->payload properly aligned",
292 ((mem_ptr_t)q->payload % MEM_ALIGNMENT) == 0);
293 LWIP_ASSERT("check p->payload + p->len does not overflow pbuf",
294 ((u8_t*)p->payload + p->len <=
296 q->ref = 1;
297 /* calculate remaining length to be allocated */
298 rem_len -= q->len;
299 /* remember this pbuf for linkage in next iteration */
300 r = q;
301 }
302 /* end of chain */
303 /*r->next = NULL;*/
304
305 break;
306 case PBUF_RAM:
307 /* If pbuf is to be allocated in RAM, allocate memory for it. */
309 if (p == NULL) {
310 return NULL;
311 }
312 /* Set up internal structure of the pbuf. */
313 p->payload = LWIP_MEM_ALIGN((void *)((u8_t *)p + SIZEOF_STRUCT_PBUF + offset));
314 p->len = p->tot_len = length;
315 p->next = NULL;
316 p->type = type;
317
318 LWIP_ASSERT("pbuf_alloc: pbuf->payload properly aligned",
319 ((mem_ptr_t)p->payload % MEM_ALIGNMENT) == 0);
320 break;
321 /* pbuf references existing (non-volatile static constant) ROM payload? */
322 case PBUF_ROM:
323 /* pbuf references existing (externally allocated) RAM payload? */
324 case PBUF_REF:
325 /* only allocate memory for the pbuf structure */
326 p = (struct pbuf *)memp_malloc(MEMP_PBUF);
327 if (p == NULL) {
329 ("pbuf_alloc: Could not allocate MEMP_PBUF for PBUF_%s.\n",
330 (type == PBUF_ROM) ? "ROM" : "REF"));
331 return NULL;
332 }
333 /* caller must set this field properly, afterwards */
334 p->payload = NULL;
335 p->len = p->tot_len = length;
336 p->next = NULL;
337 p->type = type;
338 break;
339 default:
340 LWIP_ASSERT("pbuf_alloc: erroneous type", 0);
341 return NULL;
342 }
343 /* set reference count */
344 p->ref = 1;
345 /* set flags */
346 p->flags = 0;
347 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc(length=%"U16_F") == %p\n", length, (void *)p));
348 return p;
349}
#define MEM_ALIGNMENT
Definition: d3d9_helpers.c:15
#define LWIP_MIN(x, y)
Definition: def.h:44
#define NULL
Definition: types.h:112
#define U16_F
Definition: cc.h:36
ULONG_PTR mem_ptr_t
Definition: cc.h:33
signed long s32_t
Definition: cc.h:30
unsigned char u8_t
Definition: cc.h:23
unsigned short u16_t
Definition: cc.h:24
void * mem_malloc(mem_size_t size)
Definition: mem.c:494
#define LWIP_DBG_LEVEL_SERIOUS
Definition: debug.h:47
#define LWIP_DEBUGF(debug, message)
Definition: debug.h:95
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:66
#define LWIP_DBG_TRACE
Definition: debug.h:57
#define LWIP_MEM_ALIGN(addr)
Definition: mem.h:116
#define LWIP_MEM_ALIGN_SIZE(size)
Definition: mem.h:101
GLuint GLuint GLsizei GLenum type
Definition: gl.h:1545
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
GLdouble GLdouble GLdouble GLdouble q
Definition: gl.h:2063
GLuint GLsizei GLsizei * length
Definition: glext.h:6040
GLfloat GLfloat p
Definition: glext.h:8902
GLenum GLuint GLint GLint layer
Definition: glext.h:7007
GLintptr offset
Definition: glext.h:5920
#define PBUF_DEBUG
Definition: lwipopts.h:125
void * memp_malloc(memp_t type)
Definition: memp.c:390
#define PBUF_LINK_HLEN
Definition: opt.h:1095
#define PBUF_POOL_BUFSIZE_ALIGNED
Definition: pbuf.c:85
#define SIZEOF_STRUCT_PBUF
Definition: pbuf.c:82
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:618
#define PBUF_POOL_IS_EMPTY()
Definition: pbuf.c:88
#define PBUF_IP_HLEN
Definition: pbuf.h:48
#define PBUF_TRANSPORT_HLEN
Definition: pbuf.h:47
struct define * next
Definition: compiler.c:65
Definition: pbuf.h:79

Referenced by create_arp_response(), LibIPInsertPacket(), pbuf_coalesce(), START_TEST(), tcp_create_segment_wnd(), and test_tcp_netif_output().

◆ pbuf_cat()

void pbuf_cat ( struct pbuf h,
struct pbuf t 
)

Concatenate two pbufs (each may be a pbuf chain) and take over the caller's reference of the tail pbuf.

Note
The caller MAY NOT reference the tail pbuf afterwards. Use pbuf_chain() for that purpose.
See also
pbuf_chain()

Definition at line 745 of file pbuf.c.

746{
747 struct pbuf *p;
748
749 LWIP_ERROR("(h != NULL) && (t != NULL) (programmer violates API)",
750 ((h != NULL) && (t != NULL)), return;);
751
752 /* proceed to last pbuf of chain */
753 for (p = h; p->next != NULL; p = p->next) {
754 /* add total length of second chain to all totals of first chain */
755 p->tot_len += t->tot_len;
756 }
757 /* { p is last pbuf of first h chain, p->next == NULL } */
758 LWIP_ASSERT("p->tot_len == p->len (of last pbuf in chain)", p->tot_len == p->len);
759 LWIP_ASSERT("p->next == NULL", p->next == NULL);
760 /* add total length of second chain to last pbuf total of first chain */
761 p->tot_len += t->tot_len;
762 /* chain last pbuf of head (p) with first of tail (t) */
763 p->next = t;
764 /* p->next now references t, but the caller will drop its reference to t,
765 * so netto there is no change to the reference count of t.
766 */
767}
#define LWIP_ERROR(message, expression, handler)
Definition: debug.h:74
GLdouble GLdouble t
Definition: gl.h:2047
GLfloat GLfloat GLfloat GLfloat h
Definition: glext.h:7723

Referenced by pbuf_chain(), and test_tcp_netif_output().

◆ pbuf_chain()

void pbuf_chain ( struct pbuf h,
struct pbuf t 
)

Chain two pbufs (or pbuf chains) together.

The caller MUST call pbuf_free(t) once it has stopped using it. Use pbuf_cat() instead if you no longer use t.

Parameters
hhead pbuf (chain)
ttail pbuf (chain)
Note
The pbufs MUST belong to the same packet.
MAY NOT be called on a packet queue.

The ->tot_len fields of all pbufs of the head chain are adjusted. The ->next field of the last pbuf of the head chain is adjusted. The ->ref field of the first pbuf of the tail chain is adjusted.

Definition at line 786 of file pbuf.c.

787{
788 pbuf_cat(h, t);
789 /* t is now referenced by h */
790 pbuf_ref(t);
791 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_chain: %p references %p\n", (void *)h, (void *)t));
792}
void pbuf_ref(struct pbuf *p)
Definition: pbuf.c:723
void pbuf_cat(struct pbuf *h, struct pbuf *t)
Definition: pbuf.c:745

◆ pbuf_clen()

u8_t pbuf_clen ( struct pbuf p)

Count number of pbufs in a chain

Parameters
pfirst pbuf of chain
Returns
the number of pbufs in a chain

Definition at line 704 of file pbuf.c.

705{
706 u8_t len;
707
708 len = 0;
709 while (p != NULL) {
710 ++len;
711 p = p->next;
712 }
713 return len;
714}
GLenum GLsizei len
Definition: glext.h:6722

Referenced by tcp_oos_pbuf_count().

◆ pbuf_coalesce()

struct pbuf * pbuf_coalesce ( struct pbuf p,
pbuf_layer  layer 
)

Creates a single pbuf out of a queue of pbufs.

Remarks
: Either the source pbuf 'p' is freed by this function or the original pbuf 'p' is returned, therefore the caller has to check the result!
Parameters
pthe source pbuf
layerpbuf_layer of the new pbuf
Returns
a new, single pbuf (p->next is NULL) or the old pbuf if allocation fails

Definition at line 1010 of file pbuf.c.

1011{
1012 struct pbuf *q;
1013 err_t err;
1014 if (p->next == NULL) {
1015 return p;
1016 }
1017 q = pbuf_alloc(layer, p->tot_len, PBUF_RAM);
1018 if (q == NULL) {
1019 /* @todo: what do we do now? */
1020 return p;
1021 }
1022 err = pbuf_copy(q, p);
1023 LWIP_ASSERT("pbuf_copy failed", err == ERR_OK);
1024 pbuf_free(p);
1025 return q;
1026}
#define ERR_OK
Definition: err.h:52
s8_t err_t
Definition: err.h:47
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
Definition: pbuf.c:207
err_t pbuf_copy(struct pbuf *p_to, struct pbuf *p_from)
Definition: pbuf.c:852
#define err(...)

◆ pbuf_copy()

err_t pbuf_copy ( struct pbuf p_to,
struct pbuf p_from 
)

Create PBUF_RAM copies of pbufs.

Used to queue packets on behalf of the lwIP stack, such as ARP based queueing.

Note
You MUST explicitly use p = pbuf_take(p);
Only one packet is copied, no packet queue!
Parameters
p_topbuf destination of the copy
p_frompbuf source of the copy
Returns
ERR_OK if pbuf was copied ERR_ARG if one of the pbufs is NULL or p_to is not big enough to hold p_from

Definition at line 852 of file pbuf.c.

853{
854 u16_t offset_to=0, offset_from=0, len;
855
856 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_copy(%p, %p)\n",
857 (void*)p_to, (void*)p_from));
858
859 /* is the target big enough to hold the source? */
860 LWIP_ERROR("pbuf_copy: target not big enough to hold source", ((p_to != NULL) &&
861 (p_from != NULL) && (p_to->tot_len >= p_from->tot_len)), return ERR_ARG;);
862
863 /* iterate through pbuf chain */
864 do
865 {
866 /* copy one part of the original chain */
867 if ((p_to->len - offset_to) >= (p_from->len - offset_from)) {
868 /* complete current p_from fits into current p_to */
869 len = p_from->len - offset_from;
870 } else {
871 /* current p_from does not fit into current p_to */
872 len = p_to->len - offset_to;
873 }
874 MEMCPY((u8_t*)p_to->payload + offset_to, (u8_t*)p_from->payload + offset_from, len);
875 offset_to += len;
876 offset_from += len;
877 LWIP_ASSERT("offset_to <= p_to->len", offset_to <= p_to->len);
878 LWIP_ASSERT("offset_from <= p_from->len", offset_from <= p_from->len);
879 if (offset_from >= p_from->len) {
880 /* on to next p_from (if any) */
881 offset_from = 0;
882 p_from = p_from->next;
883 }
884 if (offset_to == p_to->len) {
885 /* on to next p_to (if any) */
886 offset_to = 0;
887 p_to = p_to->next;
888 LWIP_ERROR("p_to != NULL", (p_to != NULL) || (p_from == NULL) , return ERR_ARG;);
889 }
890
891 if((p_from != NULL) && (p_from->len == p_from->tot_len)) {
892 /* don't copy more than one packet! */
893 LWIP_ERROR("pbuf_copy() does not allow packet queues!\n",
894 (p_from->next == NULL), return ERR_VAL;);
895 }
896 if((p_to != NULL) && (p_to->len == p_to->tot_len)) {
897 /* don't copy more than one packet! */
898 LWIP_ERROR("pbuf_copy() does not allow packet queues!\n",
899 (p_to->next == NULL), return ERR_VAL;);
900 }
901 } while (p_from);
902 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_copy: end of chain reached.\n"));
903 return ERR_OK;
904}
#define ERR_ARG
Definition: err.h:70
#define ERR_VAL
Definition: err.h:58
#define MEMCPY(DST, SRC, BYTES)
Definition: macros.h:231
u16_t tot_len
Definition: pbuf.h:93
struct pbuf * next
Definition: pbuf.h:81
u16_t len
Definition: pbuf.h:96
void * payload
Definition: pbuf.h:84

Referenced by pbuf_coalesce(), and test_tcp_netif_output().

◆ pbuf_copy_partial()

u16_t pbuf_copy_partial ( struct pbuf buf,
void dataptr,
u16_t  len,
u16_t  offset 
)

Copy (part of) the contents of a packet buffer to an application supplied buffer.

Parameters
bufthe pbuf from which to copy data
dataptrthe application supplied buffer
lenlength of data to copy (dataptr must be big enough). No more than buf->tot_len will be copied, irrespective of len
offsetoffset into the packet buffer from where to begin copying len bytes
Returns
the number of bytes copied, or 0 on failure

Definition at line 918 of file pbuf.c.

919{
920 struct pbuf *p;
921 u16_t left;
922 u16_t buf_copy_len;
923 u16_t copied_total = 0;
924
925 LWIP_ERROR("pbuf_copy_partial: invalid buf", (buf != NULL), return 0;);
926 LWIP_ERROR("pbuf_copy_partial: invalid dataptr", (dataptr != NULL), return 0;);
927
928 left = 0;
929
930 if((buf == NULL) || (dataptr == NULL)) {
931 return 0;
932 }
933
934 /* Note some systems use byte copy if dataptr or one of the pbuf payload pointers are unaligned. */
935 for(p = buf; len != 0 && p != NULL; p = p->next) {
936 if ((offset != 0) && (offset >= p->len)) {
937 /* don't copy from this buffer -> on to the next */
938 offset -= p->len;
939 } else {
940 /* copy from this buffer. maybe only partially. */
941 buf_copy_len = p->len - offset;
942 if (buf_copy_len > len)
943 buf_copy_len = len;
944 /* copy the necessary parts of the buffer */
945 MEMCPY(&((char*)dataptr)[left], &((char*)p->payload)[offset], buf_copy_len);
946 copied_total += buf_copy_len;
947 left += buf_copy_len;
948 len -= buf_copy_len;
949 offset = 0;
950 }
951 }
952 return copied_total;
953}
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLint left
Definition: glext.h:7726
int const JOCTET * dataptr
Definition: jpeglib.h:1031

Referenced by LibTCPGetDataFromConnectionQueue(), and test_tcp_tx_full_window_lost().

◆ pbuf_dechain()

struct pbuf * pbuf_dechain ( struct pbuf p)

Dechains the first pbuf from its succeeding pbufs in the chain.

Makes p->tot_len field equal to p->len.

Parameters
ppbuf to dechain
Returns
remainder of the pbuf chain, or NULL if it was de-allocated.
Note
May not be called on a packet queue.

Definition at line 803 of file pbuf.c.

804{
805 struct pbuf *q;
806 u8_t tail_gone = 1;
807 /* tail */
808 q = p->next;
809 /* pbuf has successor in chain? */
810 if (q != NULL) {
811 /* assert tot_len invariant: (p->tot_len == p->len + (p->next? p->next->tot_len: 0) */
812 LWIP_ASSERT("p->tot_len == p->len + q->tot_len", q->tot_len == p->tot_len - p->len);
813 /* enforce invariant if assertion is disabled */
814 q->tot_len = p->tot_len - p->len;
815 /* decouple pbuf from remainder */
816 p->next = NULL;
817 /* total length of pbuf p is its own length only */
818 p->tot_len = p->len;
819 /* q is no longer referenced by p, free it */
820 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_dechain: unreferencing %p\n", (void *)q));
821 tail_gone = pbuf_free(q);
822 if (tail_gone > 0) {
824 ("pbuf_dechain: deallocated %p (as it is no longer referenced)\n", (void *)q));
825 }
826 /* return remaining tail or NULL if deallocated */
827 }
828 /* assert tot_len invariant: (p->tot_len == p->len + (p->next? p->next->tot_len: 0) */
829 LWIP_ASSERT("p->tot_len == p->len", p->tot_len == p->len);
830 return ((tail_gone > 0) ? NULL : q);
831}

◆ pbuf_free()

u8_t pbuf_free ( struct pbuf p)

Dereference a pbuf chain or queue and deallocate any no-longer-used pbufs at the head of this chain or queue.

Decrements the pbuf reference count. If it reaches zero, the pbuf is deallocated.

For a pbuf chain, this is repeated for each pbuf in the chain, up to the first pbuf which has a non-zero reference count after decrementing. So, when all reference counts are one, the whole chain is free'd.

Parameters
pThe pbuf (chain) to be dereferenced.
Returns
the number of pbufs that were de-allocated from the head of the chain.
Note
MUST NOT be called on a packet queue (Not verified to work yet).
the reference counter of a pbuf equals the number of pointers that refer to the pbuf (or into the pbuf).

Definition at line 618 of file pbuf.c.

619{
620 u16_t type;
621 struct pbuf *q;
622 u8_t count;
623
624 if (p == NULL) {
625 LWIP_ASSERT("p != NULL", p != NULL);
626 /* if assertions are disabled, proceed with debug output */
628 ("pbuf_free(p == NULL) was called.\n"));
629 return 0;
630 }
631 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free(%p)\n", (void *)p));
632
634
635 LWIP_ASSERT("pbuf_free: sane type",
636 p->type == PBUF_RAM || p->type == PBUF_ROM ||
637 p->type == PBUF_REF || p->type == PBUF_POOL);
638
639 count = 0;
640 /* de-allocate all consecutive pbufs from the head of the chain that
641 * obtain a zero reference count after decrementing*/
642 while (p != NULL) {
643 u16_t ref;
644 SYS_ARCH_DECL_PROTECT(old_level);
645 /* Since decrementing ref cannot be guaranteed to be a single machine operation
646 * we must protect it. We put the new ref into a local variable to prevent
647 * further protection. */
648 SYS_ARCH_PROTECT(old_level);
649 /* all pbufs in a chain are referenced at least once */
650 LWIP_ASSERT("pbuf_free: p->ref > 0", p->ref > 0);
651 /* decrease reference count (number of pointers to pbuf) */
652 ref = --(p->ref);
653 SYS_ARCH_UNPROTECT(old_level);
654 /* this pbuf is no longer referenced to? */
655 if (ref == 0) {
656 /* remember next pbuf in chain for next iteration */
657 q = p->next;
658 LWIP_DEBUGF( PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free: deallocating %p\n", (void *)p));
659 type = p->type;
660#if LWIP_SUPPORT_CUSTOM_PBUF
661 /* is this a custom pbuf? */
662 if ((p->flags & PBUF_FLAG_IS_CUSTOM) != 0) {
663 struct pbuf_custom *pc = (struct pbuf_custom*)p;
664 LWIP_ASSERT("pc->custom_free_function != NULL", pc->custom_free_function != NULL);
665 pc->custom_free_function(p);
666 } else
667#endif /* LWIP_SUPPORT_CUSTOM_PBUF */
668 {
669 /* is this a pbuf from the pool? */
670 if (type == PBUF_POOL) {
671 memp_free(MEMP_PBUF_POOL, p);
672 /* is this a ROM or RAM referencing pbuf? */
673 } else if (type == PBUF_ROM || type == PBUF_REF) {
674 memp_free(MEMP_PBUF, p);
675 /* type == PBUF_RAM */
676 } else {
677 mem_free(p);
678 }
679 }
680 count++;
681 /* proceed to next pbuf */
682 p = q;
683 /* p->ref > 0, this pbuf is still referenced to */
684 /* (and so the remaining pbufs in chain as well) */
685 } else {
686 LWIP_DEBUGF( PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free: %p has ref %"U16_F", ending here.\n", (void *)p, ref));
687 /* stop walking through the chain */
688 p = NULL;
689 }
690 }
691 PERF_STOP("pbuf_free");
692 /* return number of de-allocated pbufs */
693 return count;
694}
#define mem_free(ptr, bsize)
Definition: types.h:124
#define SYS_ARCH_UNPROTECT(lev)
Definition: cc.h:56
#define SYS_ARCH_PROTECT(lev)
Definition: cc.h:55
#define SYS_ARCH_DECL_PROTECT(lev)
Definition: cc.h:54
GLuint GLuint GLsizei count
Definition: gl.h:1545
void memp_free(memp_t type, void *mem)
Definition: memp.c:435
#define PBUF_FLAG_IS_CUSTOM
Definition: pbuf.h:69
#define PERF_START
Definition: perf.h:3
#define PERF_STOP
Definition: perf.h:4
u16_t ref
Definition: pbuf.h:109
Definition: send.c:48

Referenced by InternalRecvEventHandler(), ip_forward(), ip_input(), LibTCPEmptyQueue(), pbuf_alloc(), pbuf_coalesce(), pbuf_dechain(), pbuf_free_int(), pbuf_realloc(), START_TEST(), test_tcp_counters_recv(), and test_tcp_tx_full_window_lost().

◆ pbuf_get_at()

u8_t pbuf_get_at ( struct pbuf p,
u16_t  offset 
)

Get one byte from the specified position in a pbuf WARNING: returns zero for offset >= p->tot_len

Parameters
ppbuf to parse
offsetoffset into p of the byte to return
Returns
byte at an offset into p OR ZERO IF 'offset' >= p->tot_len

Definition at line 1077 of file pbuf.c.

1078{
1079 u16_t copy_from = offset;
1080 struct pbuf* q = p;
1081
1082 /* get the correct pbuf */
1083 while ((q != NULL) && (q->len <= copy_from)) {
1084 copy_from -= q->len;
1085 q = q->next;
1086 }
1087 /* return requested data if pbuf is OK */
1088 if ((q != NULL) && (q->len > copy_from)) {
1089 return ((u8_t*)q->payload)[copy_from];
1090 }
1091 return 0;
1092}

Referenced by pbuf_memcmp().

◆ pbuf_header()

u8_t pbuf_header ( struct pbuf p,
s16_t  header_size_increment 
)

Adjusts the payload pointer to hide or reveal headers in the payload.

Adjusts the ->payload pointer so that space for a header (dis)appears in the pbuf payload.

The ->payload, ->tot_len and ->len fields are adjusted.

Parameters
ppbuf to change the header size.
header_size_incrementNumber of bytes to increment header size which increases the size of the pbuf. New space is on the front. (Using a negative value decreases the header size.) If hdr_size_inc is 0, this function does nothing and returns succesful.

PBUF_ROM and PBUF_REF type buffers cannot have their sizes increased, so the call will fail. A check is made that the increase in header size does not move the payload pointer in front of the start of the buffer.

Returns
non-zero on failure, zero on success.

Definition at line 511 of file pbuf.c.

512{
513 u16_t type;
514 void *payload;
515 u16_t increment_magnitude;
516
517 LWIP_ASSERT("p != NULL", p != NULL);
518 if ((header_size_increment == 0) || (p == NULL)) {
519 return 0;
520 }
521
522 if (header_size_increment < 0){
523 increment_magnitude = -header_size_increment;
524 /* Check that we aren't going to move off the end of the pbuf */
525 LWIP_ERROR("increment_magnitude <= p->len", (increment_magnitude <= p->len), return 1;);
526 } else {
527 increment_magnitude = header_size_increment;
528#if 0
529 /* Can't assert these as some callers speculatively call
530 pbuf_header() to see if it's OK. Will return 1 below instead. */
531 /* Check that we've got the correct type of pbuf to work with */
532 LWIP_ASSERT("p->type == PBUF_RAM || p->type == PBUF_POOL",
533 p->type == PBUF_RAM || p->type == PBUF_POOL);
534 /* Check that we aren't going to move off the beginning of the pbuf */
535 LWIP_ASSERT("p->payload - increment_magnitude >= p + SIZEOF_STRUCT_PBUF",
536 (u8_t *)p->payload - increment_magnitude >= (u8_t *)p + SIZEOF_STRUCT_PBUF);
537#endif
538 }
539
540 type = p->type;
541 /* remember current payload pointer */
542 payload = p->payload;
543
544 /* pbuf types containing payloads? */
545 if (type == PBUF_RAM || type == PBUF_POOL) {
546 /* set new payload pointer */
547 p->payload = (u8_t *)p->payload - header_size_increment;
548 /* boundary check fails? */
549 if ((u8_t *)p->payload < (u8_t *)p + SIZEOF_STRUCT_PBUF) {
551 ("pbuf_header: failed as %p < %p (not enough space for new header size)\n",
552 (void *)p->payload, (void *)(p + 1)));
553 /* restore old payload pointer */
554 p->payload = payload;
555 /* bail out unsuccesfully */
556 return 1;
557 }
558 /* pbuf types refering to external payloads? */
559 } else if (type == PBUF_REF || type == PBUF_ROM) {
560 /* hide a header in the payload? */
561 if ((header_size_increment < 0) && (increment_magnitude <= p->len)) {
562 /* increase payload pointer */
563 p->payload = (u8_t *)p->payload - header_size_increment;
564 } else {
565 /* cannot expand payload to front (yet!)
566 * bail out unsuccesfully */
567 return 1;
568 }
569 } else {
570 /* Unknown type */
571 LWIP_ASSERT("bad pbuf type", 0);
572 return 1;
573 }
574 /* modify pbuf length fields */
575 p->len += header_size_increment;
576 p->tot_len += header_size_increment;
577
578 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_header: old %p new %p (%"S16_F")\n",
579 (void *)payload, (void *)p->payload, header_size_increment));
580
581 return 0;
582}
#define S16_F
Definition: cc.h:37
if(dx< 0)
Definition: linetemp.h:194

Referenced by ip_input(), ip_output_if(), and tcp_create_segment_wnd().

◆ pbuf_memcmp()

u16_t pbuf_memcmp ( struct pbuf p,
u16_t  offset,
const void s2,
u16_t  n 
)

Compare pbuf contents at specified offset with memory s2, both of length n

Parameters
ppbuf to compare
offsetoffset into p at wich to start comparing
s2buffer to compare
nlength of buffer to compare
Returns
zero if equal, nonzero otherwise (0xffff if p is too short, diffoffset+1 otherwise)

Definition at line 1104 of file pbuf.c.

1105{
1106 u16_t start = offset;
1107 struct pbuf* q = p;
1108
1109 /* get the correct pbuf */
1110 while ((q != NULL) && (q->len <= start)) {
1111 start -= q->len;
1112 q = q->next;
1113 }
1114 /* return requested data if pbuf is OK */
1115 if ((q != NULL) && (q->len > start)) {
1116 u16_t i;
1117 for(i = 0; i < n; i++) {
1118 u8_t a = pbuf_get_at(q, start + i);
1119 u8_t b = ((u8_t*)s2)[i];
1120 if (a != b) {
1121 return i+1;
1122 }
1123 }
1124 return 0;
1125 }
1126 return 0xffff;
1127}
GLuint start
Definition: gl.h:1545
GLdouble n
Definition: glext.h:7729
GLboolean GLboolean GLboolean b
Definition: glext.h:6204
GLboolean GLboolean GLboolean GLboolean a
Definition: glext.h:6204
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
struct S2 s2
u8_t pbuf_get_at(struct pbuf *p, u16_t offset)
Definition: pbuf.c:1077

Referenced by pbuf_memfind().

◆ pbuf_memfind()

u16_t pbuf_memfind ( struct pbuf p,
const void mem,
u16_t  mem_len,
u16_t  start_offset 
)

Find occurrence of mem (with length mem_len) in pbuf p, starting at offset start_offset.

Parameters
ppbuf to search, maximum length is 0xFFFE since 0xFFFF is used as return value 'not found'
memsearch for the contents of this buffer
mem_lenlength of 'mem'
start_offsetoffset into p at which to start searching
Returns
0xFFFF if substr was not found in p or the index where it was found

Definition at line 1140 of file pbuf.c.

1141{
1142 u16_t i;
1143 u16_t max = p->tot_len - mem_len;
1144 if (p->tot_len >= mem_len + start_offset) {
1145 for(i = start_offset; i <= max; ) {
1146 u16_t plus = pbuf_memcmp(p, i, mem, mem_len);
1147 if (plus == 0) {
1148 return i;
1149 } else {
1150 i += plus;
1151 }
1152 }
1153 }
1154 return 0xFFFF;
1155}
u16_t pbuf_memcmp(struct pbuf *p, u16_t offset, const void *s2, u16_t n)
Definition: pbuf.c:1104
Definition: mem.c:156
#define max(a, b)
Definition: svc.c:63

Referenced by pbuf_strstr().

◆ pbuf_realloc()

void pbuf_realloc ( struct pbuf p,
u16_t  new_len 
)

Shrink a pbuf chain to a desired length.

Parameters
ppbuf to shrink.
new_lendesired new length of pbuf chain

Depending on the desired length, the first few pbufs in a chain might be skipped and left unchanged. The new last pbuf in the chain will be resized, and any remaining pbufs will be freed.

Note
If the pbuf is ROM/REF, only the ->tot_len and ->len fields are adjusted.
May not be called on a packet queue.
Despite its name, pbuf_realloc cannot grow the size of a pbuf (chain).

Definition at line 430 of file pbuf.c.

431{
432 struct pbuf *q;
433 u16_t rem_len; /* remaining length */
434 s32_t grow;
435
436 LWIP_ASSERT("pbuf_realloc: p != NULL", p != NULL);
437 LWIP_ASSERT("pbuf_realloc: sane p->type", p->type == PBUF_POOL ||
438 p->type == PBUF_ROM ||
439 p->type == PBUF_RAM ||
440 p->type == PBUF_REF);
441
442 /* desired length larger than current length? */
443 if (new_len >= p->tot_len) {
444 /* enlarging not yet supported */
445 return;
446 }
447
448 /* the pbuf chain grows by (new_len - p->tot_len) bytes
449 * (which may be negative in case of shrinking) */
450 grow = new_len - p->tot_len;
451
452 /* first, step over any pbufs that should remain in the chain */
453 rem_len = new_len;
454 q = p;
455 /* should this pbuf be kept? */
456 while (rem_len > q->len) {
457 /* decrease remaining length by pbuf length */
458 rem_len -= q->len;
459 /* decrease total length indicator */
460 LWIP_ASSERT("grow < max_u16_t", grow < 0xffff);
461 q->tot_len += (u16_t)grow;
462 /* proceed to next pbuf in chain */
463 q = q->next;
464 LWIP_ASSERT("pbuf_realloc: q != NULL", q != NULL);
465 }
466 /* we have now reached the new last pbuf (in q) */
467 /* rem_len == desired length for pbuf q */
468
469 /* shrink allocated memory for PBUF_RAM */
470 /* (other types merely adjust their length fields */
471 if ((q->type == PBUF_RAM) && (rem_len != q->len)) {
472 /* reallocate and adjust the length of the pbuf that will be split */
473 q = (struct pbuf *)mem_trim(q, (u16_t)((u8_t *)q->payload - (u8_t *)q) + rem_len);
474 LWIP_ASSERT("mem_trim returned q == NULL", q != NULL);
475 }
476 /* adjust length fields for new last pbuf */
477 q->len = rem_len;
478 q->tot_len = q->len;
479
480 /* any remaining pbufs in chain? */
481 if (q->next != NULL) {
482 /* free remaining pbufs in chain */
483 pbuf_free(q->next);
484 }
485 /* q is last packet in chain */
486 q->next = NULL;
487
488}
#define mem_trim(_m_, _s_)
Definition: cc.h:20

Referenced by ip_input().

◆ pbuf_ref()

void pbuf_ref ( struct pbuf p)

Increment the reference count of the pbuf.

Parameters
ppbuf to increase reference counter of

Definition at line 723 of file pbuf.c.

724{
725 SYS_ARCH_DECL_PROTECT(old_level);
726 /* pbuf given? */
727 if (p != NULL) {
728 SYS_ARCH_PROTECT(old_level);
729 ++(p->ref);
730 SYS_ARCH_UNPROTECT(old_level);
731 }
732}

Referenced by pbuf_chain().

◆ pbuf_strstr()

u16_t pbuf_strstr ( struct pbuf p,
const char substr 
)

Find occurrence of substr with length substr_len in pbuf p, start at offset start_offset WARNING: in contrast to strstr(), this one does not stop at the first \0 in the pbuf/source string!

Parameters
ppbuf to search, maximum length is 0xFFFE since 0xFFFF is used as return value 'not found'
substrstring to search for in p, maximum length is 0xFFFE
Returns
0xFFFF if substr was not found in p or the index where it was found

Definition at line 1168 of file pbuf.c.

1169{
1170 size_t substr_len;
1171 if ((substr == NULL) || (substr[0] == 0) || (p->tot_len == 0xFFFF)) {
1172 return 0xFFFF;
1173 }
1174 substr_len = strlen(substr);
1175 if (substr_len >= 0xFFFF) {
1176 return 0xFFFF;
1177 }
1178 return pbuf_memfind(p, substr, (u16_t)substr_len, 0);
1179}
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
static substr_t substr(const WCHAR *str, size_t len)
Definition: internet.h:203
u16_t pbuf_memfind(struct pbuf *p, const void *mem, u16_t mem_len, u16_t start_offset)
Definition: pbuf.c:1140

◆ pbuf_take()

err_t pbuf_take ( struct pbuf buf,
const void dataptr,
u16_t  len 
)

Copy application supplied data into a pbuf. This function can only be used to copy the equivalent of buf->tot_len data.

Parameters
bufpbuf to fill with data
dataptrapplication supplied data buffer
lenlength of the application supplied data buffer
Returns
ERR_OK if successful, ERR_MEM if the pbuf is not big enough

Definition at line 966 of file pbuf.c.

967{
968 struct pbuf *p;
969 u16_t buf_copy_len;
970 u16_t total_copy_len = len;
971 u16_t copied_total = 0;
972
973 LWIP_ERROR("pbuf_take: invalid buf", (buf != NULL), return 0;);
974 LWIP_ERROR("pbuf_take: invalid dataptr", (dataptr != NULL), return 0;);
975
976 if ((buf == NULL) || (dataptr == NULL) || (buf->tot_len < len)) {
977 return ERR_ARG;
978 }
979
980 /* Note some systems use byte copy if dataptr or one of the pbuf payload pointers are unaligned. */
981 for(p = buf; total_copy_len != 0; p = p->next) {
982 LWIP_ASSERT("pbuf_take: invalid pbuf", p != NULL);
983 buf_copy_len = total_copy_len;
984 if (buf_copy_len > p->len) {
985 /* this pbuf cannot hold all remaining data */
986 buf_copy_len = p->len;
987 }
988 /* copy the necessary parts of the buffer */
989 MEMCPY(p->payload, &((char*)dataptr)[copied_total], buf_copy_len);
990 total_copy_len -= buf_copy_len;
991 copied_total += buf_copy_len;
992 }
993 LWIP_ASSERT("did not copy all data", total_copy_len == 0 && copied_total == len);
994 return ERR_OK;
995}

Referenced by tcp_create_segment_wnd().