uc-sdk
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
pbuf.c
Go to the documentation of this file.
1 
32 /*
33  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without modification,
37  * are permitted provided that the following conditions are met:
38  *
39  * 1. Redistributions of source code must retain the above copyright notice,
40  * this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright notice,
42  * this list of conditions and the following disclaimer in the documentation
43  * and/or other materials provided with the distribution.
44  * 3. The name of the author may not be used to endorse or promote products
45  * derived from this software without specific prior written permission.
46  *
47  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
48  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
49  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
50  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
51  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
52  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
53  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
54  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
55  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
56  * OF SUCH DAMAGE.
57  *
58  * This file is part of the lwIP TCP/IP stack.
59  *
60  * Author: Adam Dunkels <adam@sics.se>
61  *
62  */
63 
64 #include "lwip/opt.h"
65 
66 #include "lwip/stats.h"
67 #include "lwip/def.h"
68 #include "lwip/mem.h"
69 #include "lwip/memp.h"
70 #include "lwip/pbuf.h"
71 #include "lwip/sys.h"
72 #include "arch/perf.h"
73 #if TCP_QUEUE_OOSEQ
74 #include "lwip/tcp_impl.h"
75 #endif
76 #if LWIP_CHECKSUM_ON_COPY
77 #include "lwip/inet_chksum.h"
78 #endif
79 
80 #include <string.h>
81 
82 #define SIZEOF_STRUCT_PBUF LWIP_MEM_ALIGN_SIZE(sizeof(struct pbuf))
83 /* Since the pool is created in memp, PBUF_POOL_BUFSIZE will be automatically
84  aligned there. Therefore, PBUF_POOL_BUFSIZE_ALIGNED can be used here. */
85 #define PBUF_POOL_BUFSIZE_ALIGNED LWIP_MEM_ALIGN_SIZE(PBUF_POOL_BUFSIZE)
86 
87 #if !LWIP_TCP || !TCP_QUEUE_OOSEQ || NO_SYS
88 #define PBUF_POOL_IS_EMPTY()
89 #else /* !LWIP_TCP || !TCP_QUEUE_OOSEQ || NO_SYS */
90 
91 #ifndef PBUF_POOL_FREE_OOSEQ
92 #define PBUF_POOL_FREE_OOSEQ 1
93 #endif /* PBUF_POOL_FREE_OOSEQ */
94 
95 #if PBUF_POOL_FREE_OOSEQ
96 #include "lwip/tcpip.h"
97 #define PBUF_POOL_IS_EMPTY() pbuf_pool_is_empty()
98 static u8_t pbuf_free_ooseq_queued;
107 static void
108 pbuf_free_ooseq(void* arg)
109 {
110  struct tcp_pcb* pcb;
111  SYS_ARCH_DECL_PROTECT(old_level);
112  LWIP_UNUSED_ARG(arg);
113 
114  SYS_ARCH_PROTECT(old_level);
115  pbuf_free_ooseq_queued = 0;
116  SYS_ARCH_UNPROTECT(old_level);
117 
118  for (pcb = tcp_active_pcbs; NULL != pcb; pcb = pcb->next) {
119  if (NULL != pcb->ooseq) {
121  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free_ooseq: freeing out-of-sequence pbufs\n"));
122  tcp_segs_free(pcb->ooseq);
123  pcb->ooseq = NULL;
124  return;
125  }
126  }
127 }
128 
130 static void
131 pbuf_pool_is_empty(void)
132 {
133  u8_t queued;
134  SYS_ARCH_DECL_PROTECT(old_level);
135 
136  SYS_ARCH_PROTECT(old_level);
137  queued = pbuf_free_ooseq_queued;
138  pbuf_free_ooseq_queued = 1;
139  SYS_ARCH_UNPROTECT(old_level);
140 
141  if(!queued) {
142  /* queue a call to pbuf_free_ooseq if not already queued */
143  if(tcpip_callback_with_block(pbuf_free_ooseq, NULL, 0) != ERR_OK) {
144  SYS_ARCH_PROTECT(old_level);
145  pbuf_free_ooseq_queued = 0;
146  SYS_ARCH_UNPROTECT(old_level);
147  }
148  }
149 }
150 #endif /* PBUF_POOL_FREE_OOSEQ */
151 #endif /* !LWIP_TCP || !TCP_QUEUE_OOSEQ || NO_SYS */
152 
184 struct pbuf *
186 {
187  struct pbuf *p, *q, *r;
188  u16_t offset;
189  s32_t rem_len; /* remaining length */
190  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc(length=%"U16_F")\n", length));
191 
192  /* determine header offset */
193  offset = 0;
194  switch (layer) {
195  case PBUF_TRANSPORT:
196  /* add room for transport (often TCP) layer header */
197  offset += PBUF_TRANSPORT_HLEN;
198  /* FALLTHROUGH */
199  case PBUF_IP:
200  /* add room for IP layer header */
201  offset += PBUF_IP_HLEN;
202  /* FALLTHROUGH */
203  case PBUF_LINK:
204  /* add room for link layer header */
205  offset += PBUF_LINK_HLEN;
206  break;
207  case PBUF_RAW:
208  break;
209  default:
210  LWIP_ASSERT("pbuf_alloc: bad pbuf layer", 0);
211  return NULL;
212  }
213 
214  switch (type) {
215  case PBUF_POOL:
216  /* allocate head of pbuf chain into p */
217  p = (struct pbuf *)memp_malloc(MEMP_PBUF_POOL);
218  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc: allocated pbuf %p\n", (void *)p));
219  if (p == NULL) {
221  return NULL;
222  }
223  p->type = type;
224  p->next = NULL;
225 
226  /* make the payload pointer point 'offset' bytes into pbuf data memory */
227  p->payload = LWIP_MEM_ALIGN((void *)((u8_t *)p + (SIZEOF_STRUCT_PBUF + offset)));
228  LWIP_ASSERT("pbuf_alloc: pbuf p->payload properly aligned",
229  ((mem_ptr_t)p->payload % MEM_ALIGNMENT) == 0);
230  /* the total length of the pbuf chain is the requested size */
231  p->tot_len = length;
232  /* set the length of the first pbuf in the chain */
234  LWIP_ASSERT("check p->payload + p->len does not overflow pbuf",
235  ((u8_t*)p->payload + p->len <=
237  LWIP_ASSERT("PBUF_POOL_BUFSIZE must be bigger than MEM_ALIGNMENT",
239  /* set reference count (needed here in case we fail) */
240  p->ref = 1;
241 
242  /* now allocate the tail of the pbuf chain */
243 
244  /* remember first pbuf for linkage in next iteration */
245  r = p;
246  /* remaining length to be allocated */
247  rem_len = length - p->len;
248  /* any remaining pbufs to be allocated? */
249  while (rem_len > 0) {
250  q = (struct pbuf *)memp_malloc(MEMP_PBUF_POOL);
251  if (q == NULL) {
253  /* free chain so far allocated */
254  pbuf_free(p);
255  /* bail out unsuccesfully */
256  return NULL;
257  }
258  q->type = type;
259  q->flags = 0;
260  q->next = NULL;
261  /* make previous pbuf point to this pbuf */
262  r->next = q;
263  /* set total length of this pbuf and next in chain */
264  LWIP_ASSERT("rem_len < max_u16_t", rem_len < 0xffff);
265  q->tot_len = (u16_t)rem_len;
266  /* this pbuf length is pool size, unless smaller sized tail */
268  q->payload = (void *)((u8_t *)q + SIZEOF_STRUCT_PBUF);
269  LWIP_ASSERT("pbuf_alloc: pbuf q->payload properly aligned",
270  ((mem_ptr_t)q->payload % MEM_ALIGNMENT) == 0);
271  LWIP_ASSERT("check p->payload + p->len does not overflow pbuf",
272  ((u8_t*)p->payload + p->len <=
274  q->ref = 1;
275  /* calculate remaining length to be allocated */
276  rem_len -= q->len;
277  /* remember this pbuf for linkage in next iteration */
278  r = q;
279  }
280  /* end of chain */
281  /*r->next = NULL;*/
282 
283  break;
284  case PBUF_RAM:
285  /* If pbuf is to be allocated in RAM, allocate memory for it. */
287  if (p == NULL) {
288  return NULL;
289  }
290  /* Set up internal structure of the pbuf. */
291  p->payload = LWIP_MEM_ALIGN((void *)((u8_t *)p + SIZEOF_STRUCT_PBUF + offset));
292  p->len = p->tot_len = length;
293  p->next = NULL;
294  p->type = type;
295 
296  LWIP_ASSERT("pbuf_alloc: pbuf->payload properly aligned",
297  ((mem_ptr_t)p->payload % MEM_ALIGNMENT) == 0);
298  break;
299  /* pbuf references existing (non-volatile static constant) ROM payload? */
300  case PBUF_ROM:
301  /* pbuf references existing (externally allocated) RAM payload? */
302  case PBUF_REF:
303  /* only allocate memory for the pbuf structure */
304  p = (struct pbuf *)memp_malloc(MEMP_PBUF);
305  if (p == NULL) {
307  ("pbuf_alloc: Could not allocate MEMP_PBUF for PBUF_%s.\n",
308  (type == PBUF_ROM) ? "ROM" : "REF"));
309  return NULL;
310  }
311  /* caller must set this field properly, afterwards */
312  p->payload = NULL;
313  p->len = p->tot_len = length;
314  p->next = NULL;
315  p->type = type;
316  break;
317  default:
318  LWIP_ASSERT("pbuf_alloc: erroneous type", 0);
319  return NULL;
320  }
321  /* set reference count */
322  p->ref = 1;
323  /* set flags */
324  p->flags = 0;
325  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc(length=%"U16_F") == %p\n", length, (void *)p));
326  return p;
327 }
328 
329 #if LWIP_SUPPORT_CUSTOM_PBUF
330 
343 struct pbuf*
344 pbuf_alloced_custom(pbuf_layer l, u16_t length, pbuf_type type, struct pbuf_custom *p,
345  void *payload_mem, u16_t payload_mem_len)
346 {
347  u16_t offset;
348  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloced_custom(length=%"U16_F")\n", length));
349 
350  /* determine header offset */
351  offset = 0;
352  switch (l) {
353  case PBUF_TRANSPORT:
354  /* add room for transport (often TCP) layer header */
355  offset += PBUF_TRANSPORT_HLEN;
356  /* FALLTHROUGH */
357  case PBUF_IP:
358  /* add room for IP layer header */
359  offset += PBUF_IP_HLEN;
360  /* FALLTHROUGH */
361  case PBUF_LINK:
362  /* add room for link layer header */
363  offset += PBUF_LINK_HLEN;
364  break;
365  case PBUF_RAW:
366  break;
367  default:
368  LWIP_ASSERT("pbuf_alloced_custom: bad pbuf layer", 0);
369  return NULL;
370  }
371 
372  if (LWIP_MEM_ALIGN_SIZE(offset) + length < payload_mem_len) {
373  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_LEVEL_WARNING, ("pbuf_alloced_custom(length=%"U16_F") buffer too short\n", length));
374  return NULL;
375  }
376 
377  p->pbuf.next = NULL;
378  if (payload_mem != NULL) {
379  p->pbuf.payload = LWIP_MEM_ALIGN((void *)((u8_t *)payload_mem + offset));
380  } else {
381  p->pbuf.payload = NULL;
382  }
383  p->pbuf.flags = PBUF_FLAG_IS_CUSTOM;
384  p->pbuf.len = p->pbuf.tot_len = length;
385  p->pbuf.type = type;
386  p->pbuf.ref = 1;
387  return &p->pbuf;
388 }
389 #endif /* LWIP_SUPPORT_CUSTOM_PBUF */
390 
406 void
407 pbuf_realloc(struct pbuf *p, u16_t new_len)
408 {
409  struct pbuf *q;
410  u16_t rem_len; /* remaining length */
411  s32_t grow;
412 
413  LWIP_ASSERT("pbuf_realloc: p != NULL", p != NULL);
414  LWIP_ASSERT("pbuf_realloc: sane p->type", p->type == PBUF_POOL ||
415  p->type == PBUF_ROM ||
416  p->type == PBUF_RAM ||
417  p->type == PBUF_REF);
418 
419  /* desired length larger than current length? */
420  if (new_len >= p->tot_len) {
421  /* enlarging not yet supported */
422  return;
423  }
424 
425  /* the pbuf chain grows by (new_len - p->tot_len) bytes
426  * (which may be negative in case of shrinking) */
427  grow = new_len - p->tot_len;
428 
429  /* first, step over any pbufs that should remain in the chain */
430  rem_len = new_len;
431  q = p;
432  /* should this pbuf be kept? */
433  while (rem_len > q->len) {
434  /* decrease remaining length by pbuf length */
435  rem_len -= q->len;
436  /* decrease total length indicator */
437  LWIP_ASSERT("grow < max_u16_t", grow < 0xffff);
438  q->tot_len += (u16_t)grow;
439  /* proceed to next pbuf in chain */
440  q = q->next;
441  LWIP_ASSERT("pbuf_realloc: q != NULL", q != NULL);
442  }
443  /* we have now reached the new last pbuf (in q) */
444  /* rem_len == desired length for pbuf q */
445 
446  /* shrink allocated memory for PBUF_RAM */
447  /* (other types merely adjust their length fields */
448  if ((q->type == PBUF_RAM) && (rem_len != q->len)) {
449  /* reallocate and adjust the length of the pbuf that will be split */
450  q = (struct pbuf *)mem_trim(q, (u16_t)((u8_t *)q->payload - (u8_t *)q) + rem_len);
451  LWIP_ASSERT("mem_trim returned q == NULL", q != NULL);
452  }
453  /* adjust length fields for new last pbuf */
454  q->len = rem_len;
455  q->tot_len = q->len;
456 
457  /* any remaining pbufs in chain? */
458  if (q->next != NULL) {
459  /* free remaining pbufs in chain */
460  pbuf_free(q->next);
461  }
462  /* q is last packet in chain */
463  q->next = NULL;
464 
465 }
466 
487 u8_t
488 pbuf_header(struct pbuf *p, s16_t header_size_increment)
489 {
490  u16_t type;
491  void *payload;
492  u16_t increment_magnitude;
493 
494  LWIP_ASSERT("p != NULL", p != NULL);
495  if ((header_size_increment == 0) || (p == NULL)) {
496  return 0;
497  }
498 
499  if (header_size_increment < 0){
500  increment_magnitude = -header_size_increment;
501  /* Check that we aren't going to move off the end of the pbuf */
502  LWIP_ERROR("increment_magnitude <= p->len", (increment_magnitude <= p->len), return 1;);
503  } else {
504  increment_magnitude = header_size_increment;
505 #if 0
506  /* Can't assert these as some callers speculatively call
507  pbuf_header() to see if it's OK. Will return 1 below instead. */
508  /* Check that we've got the correct type of pbuf to work with */
509  LWIP_ASSERT("p->type == PBUF_RAM || p->type == PBUF_POOL",
510  p->type == PBUF_RAM || p->type == PBUF_POOL);
511  /* Check that we aren't going to move off the beginning of the pbuf */
512  LWIP_ASSERT("p->payload - increment_magnitude >= p + SIZEOF_STRUCT_PBUF",
513  (u8_t *)p->payload - increment_magnitude >= (u8_t *)p + SIZEOF_STRUCT_PBUF);
514 #endif
515  }
516 
517  type = p->type;
518  /* remember current payload pointer */
519  payload = p->payload;
520 
521  /* pbuf types containing payloads? */
522  if (type == PBUF_RAM || type == PBUF_POOL) {
523  /* set new payload pointer */
524  p->payload = (u8_t *)p->payload - header_size_increment;
525  /* boundary check fails? */
526  if ((u8_t *)p->payload < (u8_t *)p + SIZEOF_STRUCT_PBUF) {
528  ("pbuf_header: failed as %p < %p (not enough space for new header size)\n",
529  (void *)p->payload, (void *)(p + 1)));
530  /* restore old payload pointer */
531  p->payload = payload;
532  /* bail out unsuccesfully */
533  return 1;
534  }
535  /* pbuf types refering to external payloads? */
536  } else if (type == PBUF_REF || type == PBUF_ROM) {
537  /* hide a header in the payload? */
538  if ((header_size_increment < 0) && (increment_magnitude <= p->len)) {
539  /* increase payload pointer */
540  p->payload = (u8_t *)p->payload - header_size_increment;
541  } else {
542  /* cannot expand payload to front (yet!)
543  * bail out unsuccesfully */
544  return 1;
545  }
546  } else {
547  /* Unknown type */
548  LWIP_ASSERT("bad pbuf type", 0);
549  return 1;
550  }
551  /* modify pbuf length fields */
552  p->len += header_size_increment;
553  p->tot_len += header_size_increment;
554 
555  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_header: old %p new %p (%"S16_F")\n",
556  (void *)payload, (void *)p->payload, header_size_increment));
557 
558  return 0;
559 }
560 
594 u8_t
595 pbuf_free(struct pbuf *p)
596 {
597  u16_t type;
598  struct pbuf *q;
599  u8_t count;
600 
601  if (p == NULL) {
602  LWIP_ASSERT("p != NULL", p != NULL);
603  /* if assertions are disabled, proceed with debug output */
605  ("pbuf_free(p == NULL) was called.\n"));
606  return 0;
607  }
608  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free(%p)\n", (void *)p));
609 
610  PERF_START;
611 
612  LWIP_ASSERT("pbuf_free: sane type",
613  p->type == PBUF_RAM || p->type == PBUF_ROM ||
614  p->type == PBUF_REF || p->type == PBUF_POOL);
615 
616  count = 0;
617  /* de-allocate all consecutive pbufs from the head of the chain that
618  * obtain a zero reference count after decrementing*/
619  while (p != NULL) {
620  u16_t ref;
621  SYS_ARCH_DECL_PROTECT(old_level);
622  /* Since decrementing ref cannot be guaranteed to be a single machine operation
623  * we must protect it. We put the new ref into a local variable to prevent
624  * further protection. */
625  SYS_ARCH_PROTECT(old_level);
626  /* all pbufs in a chain are referenced at least once */
627  LWIP_ASSERT("pbuf_free: p->ref > 0", p->ref > 0);
628  /* decrease reference count (number of pointers to pbuf) */
629  ref = --(p->ref);
630  SYS_ARCH_UNPROTECT(old_level);
631  /* this pbuf is no longer referenced to? */
632  if (ref == 0) {
633  /* remember next pbuf in chain for next iteration */
634  q = p->next;
635  LWIP_DEBUGF( PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free: deallocating %p\n", (void *)p));
636  type = p->type;
637 #if LWIP_SUPPORT_CUSTOM_PBUF
638  /* is this a custom pbuf? */
639  if ((p->flags & PBUF_FLAG_IS_CUSTOM) != 0) {
640  struct pbuf_custom *pc = (struct pbuf_custom*)p;
641  LWIP_ASSERT("pc->custom_free_function != NULL", pc->custom_free_function != NULL);
642  pc->custom_free_function(p);
643  } else
644 #endif /* LWIP_SUPPORT_CUSTOM_PBUF */
645  {
646  /* is this a pbuf from the pool? */
647  if (type == PBUF_POOL) {
648  memp_free(MEMP_PBUF_POOL, p);
649  /* is this a ROM or RAM referencing pbuf? */
650  } else if (type == PBUF_ROM || type == PBUF_REF) {
651  memp_free(MEMP_PBUF, p);
652  /* type == PBUF_RAM */
653  } else {
654  mem_free(p);
655  }
656  }
657  count++;
658  /* proceed to next pbuf */
659  p = q;
660  /* p->ref > 0, this pbuf is still referenced to */
661  /* (and so the remaining pbufs in chain as well) */
662  } else {
663  LWIP_DEBUGF( PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free: %p has ref %"U16_F", ending here.\n", (void *)p, ref));
664  /* stop walking through the chain */
665  p = NULL;
666  }
667  }
668  PERF_STOP("pbuf_free");
669  /* return number of de-allocated pbufs */
670  return count;
671 }
672 
680 u8_t
681 pbuf_clen(struct pbuf *p)
682 {
683  u8_t len;
684 
685  len = 0;
686  while (p != NULL) {
687  ++len;
688  p = p->next;
689  }
690  return len;
691 }
692 
699 void
700 pbuf_ref(struct pbuf *p)
701 {
702  SYS_ARCH_DECL_PROTECT(old_level);
703  /* pbuf given? */
704  if (p != NULL) {
705  SYS_ARCH_PROTECT(old_level);
706  ++(p->ref);
707  SYS_ARCH_UNPROTECT(old_level);
708  }
709 }
710 
721 void
722 pbuf_cat(struct pbuf *h, struct pbuf *t)
723 {
724  struct pbuf *p;
725 
726  LWIP_ERROR("(h != NULL) && (t != NULL) (programmer violates API)",
727  ((h != NULL) && (t != NULL)), return;);
728 
729  /* proceed to last pbuf of chain */
730  for (p = h; p->next != NULL; p = p->next) {
731  /* add total length of second chain to all totals of first chain */
732  p->tot_len += t->tot_len;
733  }
734  /* { p is last pbuf of first h chain, p->next == NULL } */
735  LWIP_ASSERT("p->tot_len == p->len (of last pbuf in chain)", p->tot_len == p->len);
736  LWIP_ASSERT("p->next == NULL", p->next == NULL);
737  /* add total length of second chain to last pbuf total of first chain */
738  p->tot_len += t->tot_len;
739  /* chain last pbuf of head (p) with first of tail (t) */
740  p->next = t;
741  /* p->next now references t, but the caller will drop its reference to t,
742  * so netto there is no change to the reference count of t.
743  */
744 }
745 
762 void
763 pbuf_chain(struct pbuf *h, struct pbuf *t)
764 {
765  pbuf_cat(h, t);
766  /* t is now referenced by h */
767  pbuf_ref(t);
768  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_chain: %p references %p\n", (void *)h, (void *)t));
769 }
770 
779 struct pbuf *
780 pbuf_dechain(struct pbuf *p)
781 {
782  struct pbuf *q;
783  u8_t tail_gone = 1;
784  /* tail */
785  q = p->next;
786  /* pbuf has successor in chain? */
787  if (q != NULL) {
788  /* assert tot_len invariant: (p->tot_len == p->len + (p->next? p->next->tot_len: 0) */
789  LWIP_ASSERT("p->tot_len == p->len + q->tot_len", q->tot_len == p->tot_len - p->len);
790  /* enforce invariant if assertion is disabled */
791  q->tot_len = p->tot_len - p->len;
792  /* decouple pbuf from remainder */
793  p->next = NULL;
794  /* total length of pbuf p is its own length only */
795  p->tot_len = p->len;
796  /* q is no longer referenced by p, free it */
797  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_dechain: unreferencing %p\n", (void *)q));
798  tail_gone = pbuf_free(q);
799  if (tail_gone > 0) {
801  ("pbuf_dechain: deallocated %p (as it is no longer referenced)\n", (void *)q));
802  }
803  /* return remaining tail or NULL if deallocated */
804  }
805  /* assert tot_len invariant: (p->tot_len == p->len + (p->next? p->next->tot_len: 0) */
806  LWIP_ASSERT("p->tot_len == p->len", p->tot_len == p->len);
807  return ((tail_gone > 0) ? NULL : q);
808 }
809 
828 err_t
829 pbuf_copy(struct pbuf *p_to, struct pbuf *p_from)
830 {
831  u16_t offset_to=0, offset_from=0, len;
832 
833  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_copy(%p, %p)\n",
834  (void*)p_to, (void*)p_from));
835 
836  /* is the target big enough to hold the source? */
837  LWIP_ERROR("pbuf_copy: target not big enough to hold source", ((p_to != NULL) &&
838  (p_from != NULL) && (p_to->tot_len >= p_from->tot_len)), return ERR_ARG;);
839 
840  /* iterate through pbuf chain */
841  do
842  {
843  LWIP_ASSERT("p_to != NULL", p_to != NULL);
844  /* copy one part of the original chain */
845  if ((p_to->len - offset_to) >= (p_from->len - offset_from)) {
846  /* complete current p_from fits into current p_to */
847  len = p_from->len - offset_from;
848  } else {
849  /* current p_from does not fit into current p_to */
850  len = p_to->len - offset_to;
851  }
852  MEMCPY((u8_t*)p_to->payload + offset_to, (u8_t*)p_from->payload + offset_from, len);
853  offset_to += len;
854  offset_from += len;
855  LWIP_ASSERT("offset_to <= p_to->len", offset_to <= p_to->len);
856  if (offset_to == p_to->len) {
857  /* on to next p_to (if any) */
858  offset_to = 0;
859  p_to = p_to->next;
860  }
861  LWIP_ASSERT("offset_from <= p_from->len", offset_from <= p_from->len);
862  if (offset_from >= p_from->len) {
863  /* on to next p_from (if any) */
864  offset_from = 0;
865  p_from = p_from->next;
866  }
867 
868  if((p_from != NULL) && (p_from->len == p_from->tot_len)) {
869  /* don't copy more than one packet! */
870  LWIP_ERROR("pbuf_copy() does not allow packet queues!\n",
871  (p_from->next == NULL), return ERR_VAL;);
872  }
873  if((p_to != NULL) && (p_to->len == p_to->tot_len)) {
874  /* don't copy more than one packet! */
875  LWIP_ERROR("pbuf_copy() does not allow packet queues!\n",
876  (p_to->next == NULL), return ERR_VAL;);
877  }
878  } while (p_from);
879  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_copy: end of chain reached.\n"));
880  return ERR_OK;
881 }
882 
894 u16_t
895 pbuf_copy_partial(struct pbuf *buf, void *dataptr, u16_t len, u16_t offset)
896 {
897  struct pbuf *p;
898  u16_t left;
899  u16_t buf_copy_len;
900  u16_t copied_total = 0;
901 
902  LWIP_ERROR("pbuf_copy_partial: invalid buf", (buf != NULL), return 0;);
903  LWIP_ERROR("pbuf_copy_partial: invalid dataptr", (dataptr != NULL), return 0;);
904 
905  left = 0;
906 
907  if((buf == NULL) || (dataptr == NULL)) {
908  return 0;
909  }
910 
911  /* Note some systems use byte copy if dataptr or one of the pbuf payload pointers are unaligned. */
912  for(p = buf; len != 0 && p != NULL; p = p->next) {
913  if ((offset != 0) && (offset >= p->len)) {
914  /* don't copy from this buffer -> on to the next */
915  offset -= p->len;
916  } else {
917  /* copy from this buffer. maybe only partially. */
918  buf_copy_len = p->len - offset;
919  if (buf_copy_len > len)
920  buf_copy_len = len;
921  /* copy the necessary parts of the buffer */
922  MEMCPY(&((char*)dataptr)[left], &((char*)p->payload)[offset], buf_copy_len);
923  copied_total += buf_copy_len;
924  left += buf_copy_len;
925  len -= buf_copy_len;
926  offset = 0;
927  }
928  }
929  return copied_total;
930 }
931 
942 err_t
943 pbuf_take(struct pbuf *buf, const void *dataptr, u16_t len)
944 {
945  struct pbuf *p;
946  u16_t buf_copy_len;
947  u16_t total_copy_len = len;
948  u16_t copied_total = 0;
949 
950  LWIP_ERROR("pbuf_take: invalid buf", (buf != NULL), return 0;);
951  LWIP_ERROR("pbuf_take: invalid dataptr", (dataptr != NULL), return 0;);
952 
953  if ((buf == NULL) || (dataptr == NULL) || (buf->tot_len < len)) {
954  return ERR_ARG;
955  }
956 
957  /* Note some systems use byte copy if dataptr or one of the pbuf payload pointers are unaligned. */
958  for(p = buf; total_copy_len != 0; p = p->next) {
959  LWIP_ASSERT("pbuf_take: invalid pbuf", p != NULL);
960  buf_copy_len = total_copy_len;
961  if (buf_copy_len > p->len) {
962  /* this pbuf cannot hold all remaining data */
963  buf_copy_len = p->len;
964  }
965  /* copy the necessary parts of the buffer */
966  MEMCPY(p->payload, &((char*)dataptr)[copied_total], buf_copy_len);
967  total_copy_len -= buf_copy_len;
968  copied_total += buf_copy_len;
969  }
970  LWIP_ASSERT("did not copy all data", total_copy_len == 0 && copied_total == len);
971  return ERR_OK;
972 }
973 
986 struct pbuf*
987 pbuf_coalesce(struct pbuf *p, pbuf_layer layer)
988 {
989  struct pbuf *q;
990  err_t err;
991  if (p->next == NULL) {
992  return p;
993  }
994  q = pbuf_alloc(layer, p->tot_len, PBUF_RAM);
995  if (q == NULL) {
996  /* @todo: what do we do now? */
997  return p;
998  }
999  err = pbuf_copy(q, p);
1000  LWIP_ASSERT("pbuf_copy failed", err == ERR_OK);
1001  pbuf_free(p);
1002  return q;
1003 }
1004 
1005 #if LWIP_CHECKSUM_ON_COPY
1006 
1018 err_t
1019 pbuf_fill_chksum(struct pbuf *p, u16_t start_offset, const void *dataptr,
1020  u16_t len, u16_t *chksum)
1021 {
1022  u32_t acc;
1023  u16_t copy_chksum;
1024  char *dst_ptr;
1025  LWIP_ASSERT("p != NULL", p != NULL);
1026  LWIP_ASSERT("dataptr != NULL", dataptr != NULL);
1027  LWIP_ASSERT("chksum != NULL", chksum != NULL);
1028  LWIP_ASSERT("len != 0", len != 0);
1029 
1030  if ((start_offset >= p->len) || (start_offset + len > p->len)) {
1031  return ERR_ARG;
1032  }
1033 
1034  dst_ptr = ((char*)p->payload) + start_offset;
1035  copy_chksum = LWIP_CHKSUM_COPY(dst_ptr, dataptr, len);
1036  if ((start_offset & 1) != 0) {
1037  copy_chksum = SWAP_BYTES_IN_WORD(copy_chksum);
1038  }
1039  acc = *chksum;
1040  acc += copy_chksum;
1041  *chksum = FOLD_U32T(acc);
1042  return ERR_OK;
1043 }
1044 #endif /* LWIP_CHECKSUM_ON_COPY */
1045 
1053 u8_t
1054 pbuf_get_at(struct pbuf* p, u16_t offset)
1055 {
1056  u16_t copy_from = offset;
1057  struct pbuf* q = p;
1058 
1059  /* get the correct pbuf */
1060  while ((q != NULL) && (q->len <= copy_from)) {
1061  copy_from -= q->len;
1062  q = q->next;
1063  }
1064  /* return requested data if pbuf is OK */
1065  if ((q != NULL) && (q->len > copy_from)) {
1066  return ((u8_t*)q->payload)[copy_from];
1067  }
1068  return 0;
1069 }
1070 
1080 u16_t
1081 pbuf_memcmp(struct pbuf* p, u16_t offset, const void* s2, u16_t n)
1082 {
1083  u16_t start = offset;
1084  struct pbuf* q = p;
1085 
1086  /* get the correct pbuf */
1087  while ((q != NULL) && (q->len <= start)) {
1088  start -= q->len;
1089  q = q->next;
1090  }
1091  /* return requested data if pbuf is OK */
1092  if ((q != NULL) && (q->len > start)) {
1093  u16_t i;
1094  for(i = 0; i < n; i++) {
1095  u8_t a = pbuf_get_at(q, start + i);
1096  u8_t b = ((u8_t*)s2)[i];
1097  if (a != b) {
1098  return i+1;
1099  }
1100  }
1101  return 0;
1102  }
1103  return 0xffff;
1104 }
1105 
1116 u16_t
1117 pbuf_memfind(struct pbuf* p, const void* mem, u16_t mem_len, u16_t start_offset)
1118 {
1119  u16_t i;
1120  u16_t max = p->tot_len - mem_len;
1121  if (p->tot_len >= mem_len + start_offset) {
1122  for(i = start_offset; i <= max; ) {
1123  u16_t plus = pbuf_memcmp(p, i, mem, mem_len);
1124  if (plus == 0) {
1125  return i;
1126  } else {
1127  i += plus;
1128  }
1129  }
1130  }
1131  return 0xFFFF;
1132 }
1133 
1144 u16_t
1145 pbuf_strstr(struct pbuf* p, const char* substr)
1146 {
1147  size_t substr_len;
1148  if ((substr == NULL) || (substr[0] == 0) || (p->tot_len == 0xFFFF)) {
1149  return 0xFFFF;
1150  }
1151  substr_len = strlen(substr);
1152  if (substr_len >= 0xFFFF) {
1153  return 0xFFFF;
1154  }
1155  return pbuf_memfind(p, substr, (u16_t)substr_len, 0);
1156 }