ReactOS 0.4.16-dev-297-gc569aee
pbuf.c File Reference
#include "lwip/opt.h"
#include "lwip/pbuf.h"
#include "lwip/stats.h"
#include "lwip/def.h"
#include "lwip/mem.h"
#include "lwip/memp.h"
#include "lwip/sys.h"
#include "lwip/netif.h"
#include <string.h>
Include dependency graph for pbuf.c:

Go to the source code of this file.

Macros

#define SIZEOF_STRUCT_PBUF   LWIP_MEM_ALIGN_SIZE(sizeof(struct pbuf))
 
#define PBUF_POOL_BUFSIZE_ALIGNED   LWIP_MEM_ALIGN_SIZE(PBUF_POOL_BUFSIZE)
 
#define PBUF_POOL_IS_EMPTY()
 

Functions

static const struct pbufpbuf_skip_const (const struct pbuf *in, u16_t in_offset, u16_t *out_offset)
 
static void pbuf_init_alloced_pbuf (struct pbuf *p, void *payload, u16_t tot_len, u16_t len, pbuf_type type, u8_t flags)
 
struct pbufpbuf_alloc (pbuf_layer layer, u16_t length, pbuf_type type)
 
struct pbufpbuf_alloc_reference (void *payload, u16_t length, pbuf_type type)
 
void pbuf_realloc (struct pbuf *p, u16_t new_len)
 
static u8_t pbuf_add_header_impl (struct pbuf *p, size_t header_size_increment, u8_t force)
 
u8_t pbuf_add_header (struct pbuf *p, size_t header_size_increment)
 
u8_t pbuf_add_header_force (struct pbuf *p, size_t header_size_increment)
 
u8_t pbuf_remove_header (struct pbuf *p, size_t header_size_decrement)
 
static u8_t pbuf_header_impl (struct pbuf *p, s16_t header_size_increment, u8_t force)
 
u8_t pbuf_header (struct pbuf *p, s16_t header_size_increment)
 
u8_t pbuf_header_force (struct pbuf *p, s16_t header_size_increment)
 
struct pbufpbuf_free_header (struct pbuf *q, u16_t size)
 
u8_t pbuf_free (struct pbuf *p)
 
u16_t pbuf_clen (const struct pbuf *p)
 
void pbuf_ref (struct pbuf *p)
 
void pbuf_cat (struct pbuf *h, struct pbuf *t)
 
void pbuf_chain (struct pbuf *h, struct pbuf *t)
 
struct pbufpbuf_dechain (struct pbuf *p)
 
err_t pbuf_copy (struct pbuf *p_to, const struct pbuf *p_from)
 
err_t pbuf_copy_partial_pbuf (struct pbuf *p_to, const struct pbuf *p_from, u16_t copy_len, u16_t offset)
 
u16_t pbuf_copy_partial (const struct pbuf *buf, void *dataptr, u16_t len, u16_t offset)
 
voidpbuf_get_contiguous (const struct pbuf *p, void *buffer, size_t bufsize, u16_t len, u16_t offset)
 
struct pbufpbuf_skip (struct pbuf *in, u16_t in_offset, u16_t *out_offset)
 
err_t pbuf_take (struct pbuf *buf, const void *dataptr, u16_t len)
 
err_t pbuf_take_at (struct pbuf *buf, const void *dataptr, u16_t len, u16_t offset)
 
struct pbufpbuf_coalesce (struct pbuf *p, pbuf_layer layer)
 
struct pbufpbuf_clone (pbuf_layer layer, pbuf_type type, struct pbuf *p)
 
u8_t pbuf_get_at (const struct pbuf *p, u16_t offset)
 
int pbuf_try_get_at (const struct pbuf *p, u16_t offset)
 
void pbuf_put_at (struct pbuf *p, u16_t offset, u8_t data)
 
u16_t pbuf_memcmp (const struct pbuf *p, u16_t offset, const void *s2, u16_t n)
 
u16_t pbuf_memfind (const struct pbuf *p, const void *mem, u16_t mem_len, u16_t start_offset)
 
u16_t pbuf_strstr (const struct pbuf *p, const char *substr)
 

Detailed Description

Packet buffer management

Definition in file pbuf.c.

Macro Definition Documentation

◆ PBUF_POOL_BUFSIZE_ALIGNED

#define PBUF_POOL_BUFSIZE_ALIGNED   LWIP_MEM_ALIGN_SIZE(PBUF_POOL_BUFSIZE)

Definition at line 92 of file pbuf.c.

◆ PBUF_POOL_IS_EMPTY

#define PBUF_POOL_IS_EMPTY ( )

Definition at line 98 of file pbuf.c.

◆ SIZEOF_STRUCT_PBUF

#define SIZEOF_STRUCT_PBUF   LWIP_MEM_ALIGN_SIZE(sizeof(struct pbuf))

Definition at line 89 of file pbuf.c.

Function Documentation

◆ pbuf_add_header()

u8_t pbuf_add_header ( struct pbuf p,
size_t  header_size_increment 
)

Adjusts the payload pointer to reveal headers in the payload.

Adjusts the ->payload pointer so that space for a header 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. If header_size_increment is 0, this function does nothing and returns successful.

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 554 of file pbuf.c.

555{
556 return pbuf_add_header_impl(p, header_size_increment, 0);
557}
GLfloat GLfloat p
Definition: glext.h:8902
static u8_t pbuf_add_header_impl(struct pbuf *p, size_t header_size_increment, u8_t force)
Definition: pbuf.c:476

Referenced by test_udp_create_test_packet().

◆ pbuf_add_header_force()

u8_t pbuf_add_header_force ( struct pbuf p,
size_t  header_size_increment 
)

Same as pbuf_add_header but does not check if 'header_size > 0' is allowed. This is used internally only, to allow PBUF_REF for RX.

Definition at line 564 of file pbuf.c.

565{
566 return pbuf_add_header_impl(p, header_size_increment, 1);
567}

◆ pbuf_add_header_impl()

static u8_t pbuf_add_header_impl ( struct pbuf p,
size_t  header_size_increment,
u8_t  force 
)
static

Adjusts the payload pointer to reveal headers in the payload.

See also
pbuf_add_header.
Parameters
ppbuf to change the header size.
header_size_incrementNumber of bytes to increment header size.
forceAllow 'header_size_increment > 0' for PBUF_REF/PBUF_ROM types
Returns
non-zero on failure, zero on success.

Definition at line 476 of file pbuf.c.

477{
478 u16_t type_internal;
479 void *payload;
480 u16_t increment_magnitude;
481
482 LWIP_ASSERT("p != NULL", p != NULL);
483 if ((p == NULL) || (header_size_increment > 0xFFFF)) {
484 return 1;
485 }
486 if (header_size_increment == 0) {
487 return 0;
488 }
489
490 increment_magnitude = (u16_t)header_size_increment;
491 /* Do not allow tot_len to wrap as a result. */
492 if ((u16_t)(increment_magnitude + p->tot_len) < increment_magnitude) {
493 return 1;
494 }
495
496 type_internal = p->type_internal;
497
498 /* pbuf types containing payloads? */
499 if (type_internal & PBUF_TYPE_FLAG_STRUCT_DATA_CONTIGUOUS) {
500 /* set new payload pointer */
501 payload = (u8_t *)p->payload - header_size_increment;
502 /* boundary check fails? */
503 if ((u8_t *)payload < (u8_t *)p + SIZEOF_STRUCT_PBUF) {
505 ("pbuf_add_header: failed as %p < %p (not enough space for new header size)\n",
506 (void *)payload, (void *)((u8_t *)p + SIZEOF_STRUCT_PBUF)));
507 /* bail out unsuccessfully */
508 return 1;
509 }
510 /* pbuf types referring to external payloads? */
511 } else {
512 /* hide a header in the payload? */
513 if (force) {
514 payload = (u8_t *)p->payload - header_size_increment;
515 } else {
516 /* cannot expand payload to front (yet!)
517 * bail out unsuccessfully */
518 return 1;
519 }
520 }
521 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_add_header: old %p new %p (%"U16_F")\n",
522 (void *)p->payload, (void *)payload, increment_magnitude));
523
524 /* modify pbuf fields */
525 p->payload = payload;
526 p->len = (u16_t)(p->len + increment_magnitude);
527 p->tot_len = (u16_t)(p->tot_len + increment_magnitude);
528
529
530 return 0;
531}
#define NULL
Definition: types.h:112
#define U16_F
Definition: cc.h:19
#define LWIP_DEBUGF(debug, message)
Definition: debug.h:158
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:116
uint8_t u8_t
Definition: arch.h:125
uint16_t u16_t
Definition: arch.h:127
#define LWIP_DBG_TRACE
Definition: debug.h:83
#define PBUF_DEBUG
Definition: opt.h:3343
if(dx< 0)
Definition: linetemp.h:194
#define SIZEOF_STRUCT_PBUF
Definition: pbuf.c:89
#define PBUF_TYPE_FLAG_STRUCT_DATA_CONTIGUOUS
Definition: pbuf.h:119

Referenced by pbuf_add_header(), pbuf_add_header_force(), and pbuf_header_impl().

◆ pbuf_clen()

u16_t pbuf_clen ( const 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 811 of file pbuf.c.

812{
813 u16_t len;
814
815 len = 0;
816 while (p != NULL) {
817 ++len;
818 p = p->next;
819 }
820 return len;
821}
GLenum GLsizei len
Definition: glext.h:6722

◆ 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 914 of file pbuf.c.

915{
916 struct pbuf *q;
917 u8_t tail_gone = 1;
918 /* tail */
919 q = p->next;
920 /* pbuf has successor in chain? */
921 if (q != NULL) {
922 /* assert tot_len invariant: (p->tot_len == p->len + (p->next? p->next->tot_len: 0) */
923 LWIP_ASSERT("p->tot_len == p->len + q->tot_len", q->tot_len == p->tot_len - p->len);
924 /* enforce invariant if assertion is disabled */
925 q->tot_len = (u16_t)(p->tot_len - p->len);
926 /* decouple pbuf from remainder */
927 p->next = NULL;
928 /* total length of pbuf p is its own length only */
929 p->tot_len = p->len;
930 /* q is no longer referenced by p, free it */
931 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_dechain: unreferencing %p\n", (void *)q));
932 tail_gone = pbuf_free(q);
933 if (tail_gone > 0) {
935 ("pbuf_dechain: deallocated %p (as it is no longer referenced)\n", (void *)q));
936 }
937 /* return remaining tail or NULL if deallocated */
938 }
939 /* assert tot_len invariant: (p->tot_len == p->len + (p->next? p->next->tot_len: 0) */
940 LWIP_ASSERT("p->tot_len == p->len", p->tot_len == p->len);
941 return ((tail_gone > 0) ? NULL : q);
942}
GLdouble GLdouble GLdouble GLdouble q
Definition: gl.h:2063
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:727
Definition: pbuf.h:186

◆ pbuf_free_header()

struct pbuf * pbuf_free_header ( struct pbuf q,
u16_t  size 
)

Similar to pbuf_header(-size) but de-refs header pbufs for (size >= p->len)

Parameters
qpbufs to operate on
sizeThe number of bytes to remove from the beginning of the pbuf list. While size >= p->len, pbufs are freed. ATTENTION: this is the opposite direction as pbuf_header, but takes an u16_t not s16_t!
Returns
the new head pbuf

Definition at line 674 of file pbuf.c.

675{
676 struct pbuf *p = q;
677 u16_t free_left = size;
678 while (free_left && p) {
679 if (free_left >= p->len) {
680 struct pbuf *f = p;
681 free_left = (u16_t)(free_left - p->len);
682 p = p->next;
683 f->next = NULL;
684 pbuf_free(f);
685 } else {
686 pbuf_remove_header(p, free_left);
687 free_left = 0;
688 }
689 }
690 return p;
691}
GLsizeiptr size
Definition: glext.h:5919
GLfloat f
Definition: glext.h:7540
u8_t pbuf_remove_header(struct pbuf *p, size_t header_size_decrement)
Definition: pbuf.c:585

◆ 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 header_size_increment is 0, this function does nothing and returns successful.

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 649 of file pbuf.c.

650{
651 return pbuf_header_impl(p, header_size_increment, 0);
652}
static u8_t pbuf_header_impl(struct pbuf *p, s16_t header_size_increment, u8_t force)
Definition: pbuf.c:619

Referenced by START_TEST(), tcp_create_segment_wnd(), and test_tcp_input().

◆ pbuf_header_force()

u8_t pbuf_header_force ( struct pbuf p,
s16_t  header_size_increment 
)

Same as pbuf_header but does not check if 'header_size > 0' is allowed. This is used internally only, to allow PBUF_REF for RX.

Definition at line 659 of file pbuf.c.

660{
661 return pbuf_header_impl(p, header_size_increment, 1);
662}

◆ pbuf_header_impl()

static u8_t pbuf_header_impl ( struct pbuf p,
s16_t  header_size_increment,
u8_t  force 
)
static

Definition at line 619 of file pbuf.c.

620{
621 if (header_size_increment < 0) {
622 return pbuf_remove_header(p, (size_t) - header_size_increment);
623 } else {
624 return pbuf_add_header_impl(p, (size_t)header_size_increment, force);
625 }
626}

Referenced by pbuf_header(), and pbuf_header_force().

◆ pbuf_init_alloced_pbuf()

static void pbuf_init_alloced_pbuf ( struct pbuf p,
void payload,
u16_t  tot_len,
u16_t  len,
pbuf_type  type,
u8_t  flags 
)
static

Definition at line 179 of file pbuf.c.

180{
181 p->next = NULL;
182 p->payload = payload;
183 p->tot_len = tot_len;
184 p->len = len;
185 p->type_internal = (u8_t)type;
186 p->flags = flags;
187 p->ref = 1;
188 p->if_idx = NETIF_NO_INDEX;
189}
GLuint GLuint GLsizei GLenum type
Definition: gl.h:1545
GLbitfield flags
Definition: glext.h:7161
#define NETIF_NO_INDEX
Definition: netif.h:579

Referenced by pbuf_alloc(), and pbuf_alloc_reference().

◆ pbuf_remove_header()

u8_t pbuf_remove_header ( struct pbuf p,
size_t  header_size_decrement 
)

Adjusts the payload pointer to hide headers in the payload.

Adjusts the ->payload pointer so that space for a header disappears in the pbuf payload.

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

Parameters
ppbuf to change the header size.
header_size_decrementNumber of bytes to decrement header size which decreases the size of the pbuf. If header_size_decrement is 0, this function does nothing and returns successful.
Returns
non-zero on failure, zero on success.

Definition at line 585 of file pbuf.c.

586{
587 void *payload;
588 u16_t increment_magnitude;
589
590 LWIP_ASSERT("p != NULL", p != NULL);
591 if ((p == NULL) || (header_size_decrement > 0xFFFF)) {
592 return 1;
593 }
594 if (header_size_decrement == 0) {
595 return 0;
596 }
597
598 increment_magnitude = (u16_t)header_size_decrement;
599 /* Check that we aren't going to move off the end of the pbuf */
600 LWIP_ERROR("increment_magnitude <= p->len", (increment_magnitude <= p->len), return 1;);
601
602 /* remember current payload pointer */
603 payload = p->payload;
604 LWIP_UNUSED_ARG(payload); /* only used in LWIP_DEBUGF below */
605
606 /* increase payload pointer (guarded by length check above) */
607 p->payload = (u8_t *)p->payload + header_size_decrement;
608 /* modify pbuf length fields */
609 p->len = (u16_t)(p->len - increment_magnitude);
610 p->tot_len = (u16_t)(p->tot_len - increment_magnitude);
611
612 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_remove_header: old %p new %p (%"U16_F")\n",
613 (void *)payload, (void *)p->payload, increment_magnitude));
614
615 return 0;
616}
#define LWIP_ERROR(message, expression, handler)
Definition: debug.h:130
#define LWIP_UNUSED_ARG(x)
Definition: arch.h:373

Referenced by pbuf_free_header(), and pbuf_header_impl().

◆ pbuf_skip_const()

static const struct pbuf * pbuf_skip_const ( const struct pbuf in,
u16_t  in_offset,
u16_t out_offset 
)
static

Definition at line 1183 of file pbuf.c.

1184{
1185 u16_t offset_left = in_offset;
1186 const struct pbuf *q = in;
1187
1188 /* get the correct pbuf */
1189 while ((q != NULL) && (q->len <= offset_left)) {
1190 offset_left = (u16_t)(offset_left - q->len);
1191 q = q->next;
1192 }
1193 if (out_offset != NULL) {
1194 *out_offset = offset_left;
1195 }
1196 return q;
1197}
GLuint in
Definition: glext.h:9616

Referenced by pbuf_get_contiguous(), pbuf_skip(), and pbuf_try_get_at().

◆ pbuf_strstr()

u16_t pbuf_strstr ( const 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 1534 of file pbuf.c.

1535{
1536 size_t substr_len;
1537 if ((substr == NULL) || (substr[0] == 0) || (p->tot_len == 0xFFFF)) {
1538 return 0xFFFF;
1539 }
1540 substr_len = strlen(substr);
1541 if (substr_len >= 0xFFFF) {
1542 return 0xFFFF;
1543 }
1544 return pbuf_memfind(p, substr, (u16_t)substr_len, 0);
1545}
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
u16_t pbuf_memfind(const struct pbuf *p, const void *mem, u16_t mem_len, u16_t start_offset)
Definition: pbuf.c:1507
static substr_t substr(const WCHAR *str, size_t len)
Definition: internet.h:203