NetTribute library with debug turned on in FShandler Donatien Garner -> Segundo Equipo -> this version

Committer:
hexley
Date:
Fri Nov 19 01:54:45 2010 +0000
Revision:
0:281d6ff68967

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hexley 0:281d6ff68967 1 /**
hexley 0:281d6ff68967 2 * @file
hexley 0:281d6ff68967 3 * Packet buffer management
hexley 0:281d6ff68967 4 *
hexley 0:281d6ff68967 5 * Packets are built from the pbuf data structure. It supports dynamic
hexley 0:281d6ff68967 6 * memory allocation for packet contents or can reference externally
hexley 0:281d6ff68967 7 * managed packet contents both in RAM and ROM. Quick allocation for
hexley 0:281d6ff68967 8 * incoming packets is provided through pools with fixed sized pbufs.
hexley 0:281d6ff68967 9 *
hexley 0:281d6ff68967 10 * A packet may span over multiple pbufs, chained as a singly linked
hexley 0:281d6ff68967 11 * list. This is called a "pbuf chain".
hexley 0:281d6ff68967 12 *
hexley 0:281d6ff68967 13 * Multiple packets may be queued, also using this singly linked list.
hexley 0:281d6ff68967 14 * This is called a "packet queue".
hexley 0:281d6ff68967 15 *
hexley 0:281d6ff68967 16 * So, a packet queue consists of one or more pbuf chains, each of
hexley 0:281d6ff68967 17 * which consist of one or more pbufs. CURRENTLY, PACKET QUEUES ARE
hexley 0:281d6ff68967 18 * NOT SUPPORTED!!! Use helper structs to queue multiple packets.
hexley 0:281d6ff68967 19 *
hexley 0:281d6ff68967 20 * The differences between a pbuf chain and a packet queue are very
hexley 0:281d6ff68967 21 * precise but subtle.
hexley 0:281d6ff68967 22 *
hexley 0:281d6ff68967 23 * The last pbuf of a packet has a ->tot_len field that equals the
hexley 0:281d6ff68967 24 * ->len field. It can be found by traversing the list. If the last
hexley 0:281d6ff68967 25 * pbuf of a packet has a ->next field other than NULL, more packets
hexley 0:281d6ff68967 26 * are on the queue.
hexley 0:281d6ff68967 27 *
hexley 0:281d6ff68967 28 * Therefore, looping through a pbuf of a single packet, has an
hexley 0:281d6ff68967 29 * loop end condition (tot_len == p->len), NOT (next == NULL).
hexley 0:281d6ff68967 30 */
hexley 0:281d6ff68967 31
hexley 0:281d6ff68967 32 /*
hexley 0:281d6ff68967 33 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
hexley 0:281d6ff68967 34 * All rights reserved.
hexley 0:281d6ff68967 35 *
hexley 0:281d6ff68967 36 * Redistribution and use in source and binary forms, with or without modification,
hexley 0:281d6ff68967 37 * are permitted provided that the following conditions are met:
hexley 0:281d6ff68967 38 *
hexley 0:281d6ff68967 39 * 1. Redistributions of source code must retain the above copyright notice,
hexley 0:281d6ff68967 40 * this list of conditions and the following disclaimer.
hexley 0:281d6ff68967 41 * 2. Redistributions in binary form must reproduce the above copyright notice,
hexley 0:281d6ff68967 42 * this list of conditions and the following disclaimer in the documentation
hexley 0:281d6ff68967 43 * and/or other materials provided with the distribution.
hexley 0:281d6ff68967 44 * 3. The name of the author may not be used to endorse or promote products
hexley 0:281d6ff68967 45 * derived from this software without specific prior written permission.
hexley 0:281d6ff68967 46 *
hexley 0:281d6ff68967 47 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
hexley 0:281d6ff68967 48 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
hexley 0:281d6ff68967 49 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
hexley 0:281d6ff68967 50 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
hexley 0:281d6ff68967 51 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
hexley 0:281d6ff68967 52 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
hexley 0:281d6ff68967 53 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
hexley 0:281d6ff68967 54 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
hexley 0:281d6ff68967 55 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
hexley 0:281d6ff68967 56 * OF SUCH DAMAGE.
hexley 0:281d6ff68967 57 *
hexley 0:281d6ff68967 58 * This file is part of the lwIP TCP/IP stack.
hexley 0:281d6ff68967 59 *
hexley 0:281d6ff68967 60 * Author: Adam Dunkels <adam@sics.se>
hexley 0:281d6ff68967 61 *
hexley 0:281d6ff68967 62 */
hexley 0:281d6ff68967 63
hexley 0:281d6ff68967 64 #include "lwip/opt.h"
hexley 0:281d6ff68967 65
hexley 0:281d6ff68967 66 #include "lwip/stats.h"
hexley 0:281d6ff68967 67 #include "lwip/def.h"
hexley 0:281d6ff68967 68 #include "lwip/mem.h"
hexley 0:281d6ff68967 69 #include "lwip/memp.h"
hexley 0:281d6ff68967 70 #include "lwip/pbuf.h"
hexley 0:281d6ff68967 71 #include "lwip/sys.h"
hexley 0:281d6ff68967 72 #include "arch/perf.h"
hexley 0:281d6ff68967 73 #if TCP_QUEUE_OOSEQ
hexley 0:281d6ff68967 74 #include "lwip/tcp_impl.h"
hexley 0:281d6ff68967 75 #endif
hexley 0:281d6ff68967 76 #if LWIP_CHECKSUM_ON_COPY
hexley 0:281d6ff68967 77 #include "lwip/inet_chksum.h"
hexley 0:281d6ff68967 78 #endif
hexley 0:281d6ff68967 79
hexley 0:281d6ff68967 80 #include <string.h>
hexley 0:281d6ff68967 81
hexley 0:281d6ff68967 82 #define SIZEOF_STRUCT_PBUF LWIP_MEM_ALIGN_SIZE(sizeof(struct pbuf))
hexley 0:281d6ff68967 83 /* Since the pool is created in memp, PBUF_POOL_BUFSIZE will be automatically
hexley 0:281d6ff68967 84 aligned there. Therefore, PBUF_POOL_BUFSIZE_ALIGNED can be used here. */
hexley 0:281d6ff68967 85 #define PBUF_POOL_BUFSIZE_ALIGNED LWIP_MEM_ALIGN_SIZE(PBUF_POOL_BUFSIZE)
hexley 0:281d6ff68967 86
hexley 0:281d6ff68967 87 #if !LWIP_TCP || !TCP_QUEUE_OOSEQ || NO_SYS
hexley 0:281d6ff68967 88 #define PBUF_POOL_IS_EMPTY()
hexley 0:281d6ff68967 89 #else /* !LWIP_TCP || !TCP_QUEUE_OOSEQ || NO_SYS */
hexley 0:281d6ff68967 90 /** Define this to 0 to prevent freeing ooseq pbufs when the PBUF_POOL is empty */
hexley 0:281d6ff68967 91 #ifndef PBUF_POOL_FREE_OOSEQ
hexley 0:281d6ff68967 92 #define PBUF_POOL_FREE_OOSEQ 1
hexley 0:281d6ff68967 93 #endif /* PBUF_POOL_FREE_OOSEQ */
hexley 0:281d6ff68967 94
hexley 0:281d6ff68967 95 #if PBUF_POOL_FREE_OOSEQ
hexley 0:281d6ff68967 96 #include "lwip/tcpip.h"
hexley 0:281d6ff68967 97 #define PBUF_POOL_IS_EMPTY() pbuf_pool_is_empty()
hexley 0:281d6ff68967 98 static u8_t pbuf_free_ooseq_queued;
hexley 0:281d6ff68967 99 /**
hexley 0:281d6ff68967 100 * Attempt to reclaim some memory from queued out-of-sequence TCP segments
hexley 0:281d6ff68967 101 * if we run out of pool pbufs. It's better to give priority to new packets
hexley 0:281d6ff68967 102 * if we're running out.
hexley 0:281d6ff68967 103 *
hexley 0:281d6ff68967 104 * This must be done in the correct thread context therefore this function
hexley 0:281d6ff68967 105 * can only be used with NO_SYS=0 and through tcpip_callback.
hexley 0:281d6ff68967 106 */
hexley 0:281d6ff68967 107 static void
hexley 0:281d6ff68967 108 pbuf_free_ooseq(void* arg)
hexley 0:281d6ff68967 109 {
hexley 0:281d6ff68967 110 struct tcp_pcb* pcb;
hexley 0:281d6ff68967 111 SYS_ARCH_DECL_PROTECT(old_level);
hexley 0:281d6ff68967 112 LWIP_UNUSED_ARG(arg);
hexley 0:281d6ff68967 113
hexley 0:281d6ff68967 114 SYS_ARCH_PROTECT(old_level);
hexley 0:281d6ff68967 115 pbuf_free_ooseq_queued = 0;
hexley 0:281d6ff68967 116 SYS_ARCH_UNPROTECT(old_level);
hexley 0:281d6ff68967 117
hexley 0:281d6ff68967 118 for (pcb = tcp_active_pcbs; NULL != pcb; pcb = pcb->next) {
hexley 0:281d6ff68967 119 if (NULL != pcb->ooseq) {
hexley 0:281d6ff68967 120 /** Free the ooseq pbufs of one PCB only */
hexley 0:281d6ff68967 121 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free_ooseq: freeing out-of-sequence pbufs\n"));
hexley 0:281d6ff68967 122 tcp_segs_free(pcb->ooseq);
hexley 0:281d6ff68967 123 pcb->ooseq = NULL;
hexley 0:281d6ff68967 124 return;
hexley 0:281d6ff68967 125 }
hexley 0:281d6ff68967 126 }
hexley 0:281d6ff68967 127 }
hexley 0:281d6ff68967 128
hexley 0:281d6ff68967 129 /** Queue a call to pbuf_free_ooseq if not already queued. */
hexley 0:281d6ff68967 130 static void
hexley 0:281d6ff68967 131 pbuf_pool_is_empty(void)
hexley 0:281d6ff68967 132 {
hexley 0:281d6ff68967 133 u8_t queued;
hexley 0:281d6ff68967 134 SYS_ARCH_DECL_PROTECT(old_level);
hexley 0:281d6ff68967 135
hexley 0:281d6ff68967 136 SYS_ARCH_PROTECT(old_level);
hexley 0:281d6ff68967 137 queued = pbuf_free_ooseq_queued;
hexley 0:281d6ff68967 138 pbuf_free_ooseq_queued = 1;
hexley 0:281d6ff68967 139 SYS_ARCH_UNPROTECT(old_level);
hexley 0:281d6ff68967 140
hexley 0:281d6ff68967 141 if(!queued) {
hexley 0:281d6ff68967 142 /* queue a call to pbuf_free_ooseq if not already queued */
hexley 0:281d6ff68967 143 if(tcpip_callback_with_block(pbuf_free_ooseq, NULL, 0) != ERR_OK) {
hexley 0:281d6ff68967 144 SYS_ARCH_PROTECT(old_level);
hexley 0:281d6ff68967 145 pbuf_free_ooseq_queued = 0;
hexley 0:281d6ff68967 146 SYS_ARCH_UNPROTECT(old_level);
hexley 0:281d6ff68967 147 }
hexley 0:281d6ff68967 148 }
hexley 0:281d6ff68967 149 }
hexley 0:281d6ff68967 150 #endif /* PBUF_POOL_FREE_OOSEQ */
hexley 0:281d6ff68967 151 #endif /* !LWIP_TCP || !TCP_QUEUE_OOSEQ || NO_SYS */
hexley 0:281d6ff68967 152
hexley 0:281d6ff68967 153 /**
hexley 0:281d6ff68967 154 * Allocates a pbuf of the given type (possibly a chain for PBUF_POOL type).
hexley 0:281d6ff68967 155 *
hexley 0:281d6ff68967 156 * The actual memory allocated for the pbuf is determined by the
hexley 0:281d6ff68967 157 * layer at which the pbuf is allocated and the requested size
hexley 0:281d6ff68967 158 * (from the size parameter).
hexley 0:281d6ff68967 159 *
hexley 0:281d6ff68967 160 * @param layer flag to define header size
hexley 0:281d6ff68967 161 * @param length size of the pbuf's payload
hexley 0:281d6ff68967 162 * @param type this parameter decides how and where the pbuf
hexley 0:281d6ff68967 163 * should be allocated as follows:
hexley 0:281d6ff68967 164 *
hexley 0:281d6ff68967 165 * - PBUF_RAM: buffer memory for pbuf is allocated as one large
hexley 0:281d6ff68967 166 * chunk. This includes protocol headers as well.
hexley 0:281d6ff68967 167 * - PBUF_ROM: no buffer memory is allocated for the pbuf, even for
hexley 0:281d6ff68967 168 * protocol headers. Additional headers must be prepended
hexley 0:281d6ff68967 169 * by allocating another pbuf and chain in to the front of
hexley 0:281d6ff68967 170 * the ROM pbuf. It is assumed that the memory used is really
hexley 0:281d6ff68967 171 * similar to ROM in that it is immutable and will not be
hexley 0:281d6ff68967 172 * changed. Memory which is dynamic should generally not
hexley 0:281d6ff68967 173 * be attached to PBUF_ROM pbufs. Use PBUF_REF instead.
hexley 0:281d6ff68967 174 * - PBUF_REF: no buffer memory is allocated for the pbuf, even for
hexley 0:281d6ff68967 175 * protocol headers. It is assumed that the pbuf is only
hexley 0:281d6ff68967 176 * being used in a single thread. If the pbuf gets queued,
hexley 0:281d6ff68967 177 * then pbuf_take should be called to copy the buffer.
hexley 0:281d6ff68967 178 * - PBUF_POOL: the pbuf is allocated as a pbuf chain, with pbufs from
hexley 0:281d6ff68967 179 * the pbuf pool that is allocated during pbuf_init().
hexley 0:281d6ff68967 180 *
hexley 0:281d6ff68967 181 * @return the allocated pbuf. If multiple pbufs where allocated, this
hexley 0:281d6ff68967 182 * is the first pbuf of a pbuf chain.
hexley 0:281d6ff68967 183 */
hexley 0:281d6ff68967 184 struct pbuf *
hexley 0:281d6ff68967 185 pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
hexley 0:281d6ff68967 186 {
hexley 0:281d6ff68967 187 struct pbuf *p, *q, *r;
hexley 0:281d6ff68967 188 u16_t offset;
hexley 0:281d6ff68967 189 s32_t rem_len; /* remaining length */
hexley 0:281d6ff68967 190 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc(length=%"U16_F")\n", length));
hexley 0:281d6ff68967 191
hexley 0:281d6ff68967 192 /* determine header offset */
hexley 0:281d6ff68967 193 offset = 0;
hexley 0:281d6ff68967 194 switch (layer) {
hexley 0:281d6ff68967 195 case PBUF_TRANSPORT:
hexley 0:281d6ff68967 196 /* add room for transport (often TCP) layer header */
hexley 0:281d6ff68967 197 offset += PBUF_TRANSPORT_HLEN;
hexley 0:281d6ff68967 198 /* FALLTHROUGH */
hexley 0:281d6ff68967 199 case PBUF_IP:
hexley 0:281d6ff68967 200 /* add room for IP layer header */
hexley 0:281d6ff68967 201 offset += PBUF_IP_HLEN;
hexley 0:281d6ff68967 202 /* FALLTHROUGH */
hexley 0:281d6ff68967 203 case PBUF_LINK:
hexley 0:281d6ff68967 204 /* add room for link layer header */
hexley 0:281d6ff68967 205 offset += PBUF_LINK_HLEN;
hexley 0:281d6ff68967 206 break;
hexley 0:281d6ff68967 207 case PBUF_RAW:
hexley 0:281d6ff68967 208 break;
hexley 0:281d6ff68967 209 default:
hexley 0:281d6ff68967 210 LWIP_ASSERT("pbuf_alloc: bad pbuf layer", 0);
hexley 0:281d6ff68967 211 return NULL;
hexley 0:281d6ff68967 212 }
hexley 0:281d6ff68967 213
hexley 0:281d6ff68967 214 switch (type) {
hexley 0:281d6ff68967 215 case PBUF_POOL:
hexley 0:281d6ff68967 216 /* allocate head of pbuf chain into p */
hexley 0:281d6ff68967 217 p = (struct pbuf *)memp_malloc(MEMP_PBUF_POOL);
hexley 0:281d6ff68967 218 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc: allocated pbuf %p\n", (void *)p));
hexley 0:281d6ff68967 219 if (p == NULL) {
hexley 0:281d6ff68967 220 PBUF_POOL_IS_EMPTY();
hexley 0:281d6ff68967 221 return NULL;
hexley 0:281d6ff68967 222 }
hexley 0:281d6ff68967 223 p->type = type;
hexley 0:281d6ff68967 224 p->next = NULL;
hexley 0:281d6ff68967 225
hexley 0:281d6ff68967 226 /* make the payload pointer point 'offset' bytes into pbuf data memory */
hexley 0:281d6ff68967 227 p->payload = LWIP_MEM_ALIGN((void *)((u8_t *)p + (SIZEOF_STRUCT_PBUF + offset)));
hexley 0:281d6ff68967 228 LWIP_ASSERT("pbuf_alloc: pbuf p->payload properly aligned",
hexley 0:281d6ff68967 229 ((mem_ptr_t)p->payload % MEM_ALIGNMENT) == 0);
hexley 0:281d6ff68967 230 /* the total length of the pbuf chain is the requested size */
hexley 0:281d6ff68967 231 p->tot_len = length;
hexley 0:281d6ff68967 232 /* set the length of the first pbuf in the chain */
hexley 0:281d6ff68967 233 p->len = LWIP_MIN(length, PBUF_POOL_BUFSIZE_ALIGNED - LWIP_MEM_ALIGN_SIZE(offset));
hexley 0:281d6ff68967 234 LWIP_ASSERT("check p->payload + p->len does not overflow pbuf",
hexley 0:281d6ff68967 235 ((u8_t*)p->payload + p->len <=
hexley 0:281d6ff68967 236 (u8_t*)p + SIZEOF_STRUCT_PBUF + PBUF_POOL_BUFSIZE_ALIGNED));
hexley 0:281d6ff68967 237 LWIP_ASSERT("PBUF_POOL_BUFSIZE must be bigger than MEM_ALIGNMENT",
hexley 0:281d6ff68967 238 (PBUF_POOL_BUFSIZE_ALIGNED - LWIP_MEM_ALIGN_SIZE(offset)) > 0 );
hexley 0:281d6ff68967 239 /* set reference count (needed here in case we fail) */
hexley 0:281d6ff68967 240 p->ref = 1;
hexley 0:281d6ff68967 241
hexley 0:281d6ff68967 242 /* now allocate the tail of the pbuf chain */
hexley 0:281d6ff68967 243
hexley 0:281d6ff68967 244 /* remember first pbuf for linkage in next iteration */
hexley 0:281d6ff68967 245 r = p;
hexley 0:281d6ff68967 246 /* remaining length to be allocated */
hexley 0:281d6ff68967 247 rem_len = length - p->len;
hexley 0:281d6ff68967 248 /* any remaining pbufs to be allocated? */
hexley 0:281d6ff68967 249 while (rem_len > 0) {
hexley 0:281d6ff68967 250 q = (struct pbuf *)memp_malloc(MEMP_PBUF_POOL);
hexley 0:281d6ff68967 251 if (q == NULL) {
hexley 0:281d6ff68967 252 PBUF_POOL_IS_EMPTY();
hexley 0:281d6ff68967 253 /* free chain so far allocated */
hexley 0:281d6ff68967 254 pbuf_free(p);
hexley 0:281d6ff68967 255 /* bail out unsuccesfully */
hexley 0:281d6ff68967 256 return NULL;
hexley 0:281d6ff68967 257 }
hexley 0:281d6ff68967 258 q->type = type;
hexley 0:281d6ff68967 259 q->flags = 0;
hexley 0:281d6ff68967 260 q->next = NULL;
hexley 0:281d6ff68967 261 /* make previous pbuf point to this pbuf */
hexley 0:281d6ff68967 262 r->next = q;
hexley 0:281d6ff68967 263 /* set total length of this pbuf and next in chain */
hexley 0:281d6ff68967 264 LWIP_ASSERT("rem_len < max_u16_t", rem_len < 0xffff);
hexley 0:281d6ff68967 265 q->tot_len = (u16_t)rem_len;
hexley 0:281d6ff68967 266 /* this pbuf length is pool size, unless smaller sized tail */
hexley 0:281d6ff68967 267 q->len = LWIP_MIN((u16_t)rem_len, PBUF_POOL_BUFSIZE_ALIGNED);
hexley 0:281d6ff68967 268 q->payload = (void *)((u8_t *)q + SIZEOF_STRUCT_PBUF);
hexley 0:281d6ff68967 269 LWIP_ASSERT("pbuf_alloc: pbuf q->payload properly aligned",
hexley 0:281d6ff68967 270 ((mem_ptr_t)q->payload % MEM_ALIGNMENT) == 0);
hexley 0:281d6ff68967 271 LWIP_ASSERT("check p->payload + p->len does not overflow pbuf",
hexley 0:281d6ff68967 272 ((u8_t*)p->payload + p->len <=
hexley 0:281d6ff68967 273 (u8_t*)p + SIZEOF_STRUCT_PBUF + PBUF_POOL_BUFSIZE_ALIGNED));
hexley 0:281d6ff68967 274 q->ref = 1;
hexley 0:281d6ff68967 275 /* calculate remaining length to be allocated */
hexley 0:281d6ff68967 276 rem_len -= q->len;
hexley 0:281d6ff68967 277 /* remember this pbuf for linkage in next iteration */
hexley 0:281d6ff68967 278 r = q;
hexley 0:281d6ff68967 279 }
hexley 0:281d6ff68967 280 /* end of chain */
hexley 0:281d6ff68967 281 /*r->next = NULL;*/
hexley 0:281d6ff68967 282
hexley 0:281d6ff68967 283 break;
hexley 0:281d6ff68967 284 case PBUF_RAM:
hexley 0:281d6ff68967 285 /* If pbuf is to be allocated in RAM, allocate memory for it. */
hexley 0:281d6ff68967 286 p = (struct pbuf*)mem_malloc(LWIP_MEM_ALIGN_SIZE(SIZEOF_STRUCT_PBUF + offset) + LWIP_MEM_ALIGN_SIZE(length));
hexley 0:281d6ff68967 287 if (p == NULL) {
hexley 0:281d6ff68967 288 return NULL;
hexley 0:281d6ff68967 289 }
hexley 0:281d6ff68967 290 /* Set up internal structure of the pbuf. */
hexley 0:281d6ff68967 291 p->payload = LWIP_MEM_ALIGN((void *)((u8_t *)p + SIZEOF_STRUCT_PBUF + offset));
hexley 0:281d6ff68967 292 p->len = p->tot_len = length;
hexley 0:281d6ff68967 293 p->next = NULL;
hexley 0:281d6ff68967 294 p->type = type;
hexley 0:281d6ff68967 295
hexley 0:281d6ff68967 296 LWIP_ASSERT("pbuf_alloc: pbuf->payload properly aligned",
hexley 0:281d6ff68967 297 ((mem_ptr_t)p->payload % MEM_ALIGNMENT) == 0);
hexley 0:281d6ff68967 298 break;
hexley 0:281d6ff68967 299 /* pbuf references existing (non-volatile static constant) ROM payload? */
hexley 0:281d6ff68967 300 case PBUF_ROM:
hexley 0:281d6ff68967 301 /* pbuf references existing (externally allocated) RAM payload? */
hexley 0:281d6ff68967 302 case PBUF_REF:
hexley 0:281d6ff68967 303 /* only allocate memory for the pbuf structure */
hexley 0:281d6ff68967 304 p = (struct pbuf *)memp_malloc(MEMP_PBUF);
hexley 0:281d6ff68967 305 if (p == NULL) {
hexley 0:281d6ff68967 306 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
hexley 0:281d6ff68967 307 ("pbuf_alloc: Could not allocate MEMP_PBUF for PBUF_%s.\n",
hexley 0:281d6ff68967 308 (type == PBUF_ROM) ? "ROM" : "REF"));
hexley 0:281d6ff68967 309 return NULL;
hexley 0:281d6ff68967 310 }
hexley 0:281d6ff68967 311 /* caller must set this field properly, afterwards */
hexley 0:281d6ff68967 312 p->payload = NULL;
hexley 0:281d6ff68967 313 p->len = p->tot_len = length;
hexley 0:281d6ff68967 314 p->next = NULL;
hexley 0:281d6ff68967 315 p->type = type;
hexley 0:281d6ff68967 316 break;
hexley 0:281d6ff68967 317 default:
hexley 0:281d6ff68967 318 LWIP_ASSERT("pbuf_alloc: erroneous type", 0);
hexley 0:281d6ff68967 319 return NULL;
hexley 0:281d6ff68967 320 }
hexley 0:281d6ff68967 321 /* set reference count */
hexley 0:281d6ff68967 322 p->ref = 1;
hexley 0:281d6ff68967 323 /* set flags */
hexley 0:281d6ff68967 324 p->flags = 0;
hexley 0:281d6ff68967 325 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc(length=%"U16_F") == %p\n", length, (void *)p));
hexley 0:281d6ff68967 326 return p;
hexley 0:281d6ff68967 327 }
hexley 0:281d6ff68967 328
hexley 0:281d6ff68967 329 #if LWIP_SUPPORT_CUSTOM_PBUF
hexley 0:281d6ff68967 330 /** Initialize a custom pbuf (already allocated).
hexley 0:281d6ff68967 331 *
hexley 0:281d6ff68967 332 * @param layer flag to define header size
hexley 0:281d6ff68967 333 * @param length size of the pbuf's payload
hexley 0:281d6ff68967 334 * @param type type of the pbuf (only used to treat the pbuf accordingly, as
hexley 0:281d6ff68967 335 * this function allocates no memory)
hexley 0:281d6ff68967 336 * @param p pointer to the custom pbuf to initialize (already allocated)
hexley 0:281d6ff68967 337 * @param payload_mem pointer to the buffer that is used for payload and headers,
hexley 0:281d6ff68967 338 * must be at least big enough to hold 'length' plus the header size,
hexley 0:281d6ff68967 339 * may be NULL if set later
hexley 0:281d6ff68967 340 * @param payload_mem_len the size of the 'payload_mem' buffer, must be at least
hexley 0:281d6ff68967 341 * big enough to hold 'length' plus the header size
hexley 0:281d6ff68967 342 */
hexley 0:281d6ff68967 343 struct pbuf*
hexley 0:281d6ff68967 344 pbuf_alloced_custom(pbuf_layer l, u16_t length, pbuf_type type, struct pbuf_custom *p,
hexley 0:281d6ff68967 345 void *payload_mem, u16_t payload_mem_len)
hexley 0:281d6ff68967 346 {
hexley 0:281d6ff68967 347 u16_t offset;
hexley 0:281d6ff68967 348 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloced_custom(length=%"U16_F")\n", length));
hexley 0:281d6ff68967 349
hexley 0:281d6ff68967 350 /* determine header offset */
hexley 0:281d6ff68967 351 offset = 0;
hexley 0:281d6ff68967 352 switch (l) {
hexley 0:281d6ff68967 353 case PBUF_TRANSPORT:
hexley 0:281d6ff68967 354 /* add room for transport (often TCP) layer header */
hexley 0:281d6ff68967 355 offset += PBUF_TRANSPORT_HLEN;
hexley 0:281d6ff68967 356 /* FALLTHROUGH */
hexley 0:281d6ff68967 357 case PBUF_IP:
hexley 0:281d6ff68967 358 /* add room for IP layer header */
hexley 0:281d6ff68967 359 offset += PBUF_IP_HLEN;
hexley 0:281d6ff68967 360 /* FALLTHROUGH */
hexley 0:281d6ff68967 361 case PBUF_LINK:
hexley 0:281d6ff68967 362 /* add room for link layer header */
hexley 0:281d6ff68967 363 offset += PBUF_LINK_HLEN;
hexley 0:281d6ff68967 364 break;
hexley 0:281d6ff68967 365 case PBUF_RAW:
hexley 0:281d6ff68967 366 break;
hexley 0:281d6ff68967 367 default:
hexley 0:281d6ff68967 368 LWIP_ASSERT("pbuf_alloced_custom: bad pbuf layer", 0);
hexley 0:281d6ff68967 369 return NULL;
hexley 0:281d6ff68967 370 }
hexley 0:281d6ff68967 371
hexley 0:281d6ff68967 372 if (LWIP_MEM_ALIGN_SIZE(offset) + length < payload_mem_len) {
hexley 0:281d6ff68967 373 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_LEVEL_WARNING, ("pbuf_alloced_custom(length=%"U16_F") buffer too short\n", length));
hexley 0:281d6ff68967 374 return NULL;
hexley 0:281d6ff68967 375 }
hexley 0:281d6ff68967 376
hexley 0:281d6ff68967 377 p->pbuf.next = NULL;
hexley 0:281d6ff68967 378 if (payload_mem != NULL) {
hexley 0:281d6ff68967 379 p->pbuf.payload = LWIP_MEM_ALIGN((void *)((u8_t *)payload_mem + offset));
hexley 0:281d6ff68967 380 } else {
hexley 0:281d6ff68967 381 p->pbuf.payload = NULL;
hexley 0:281d6ff68967 382 }
hexley 0:281d6ff68967 383 p->pbuf.flags = PBUF_FLAG_IS_CUSTOM;
hexley 0:281d6ff68967 384 p->pbuf.len = p->pbuf.tot_len = length;
hexley 0:281d6ff68967 385 p->pbuf.type = type;
hexley 0:281d6ff68967 386 p->pbuf.ref = 1;
hexley 0:281d6ff68967 387 return &p->pbuf;
hexley 0:281d6ff68967 388 }
hexley 0:281d6ff68967 389 #endif /* LWIP_SUPPORT_CUSTOM_PBUF */
hexley 0:281d6ff68967 390
hexley 0:281d6ff68967 391 /**
hexley 0:281d6ff68967 392 * Shrink a pbuf chain to a desired length.
hexley 0:281d6ff68967 393 *
hexley 0:281d6ff68967 394 * @param p pbuf to shrink.
hexley 0:281d6ff68967 395 * @param new_len desired new length of pbuf chain
hexley 0:281d6ff68967 396 *
hexley 0:281d6ff68967 397 * Depending on the desired length, the first few pbufs in a chain might
hexley 0:281d6ff68967 398 * be skipped and left unchanged. The new last pbuf in the chain will be
hexley 0:281d6ff68967 399 * resized, and any remaining pbufs will be freed.
hexley 0:281d6ff68967 400 *
hexley 0:281d6ff68967 401 * @note If the pbuf is ROM/REF, only the ->tot_len and ->len fields are adjusted.
hexley 0:281d6ff68967 402 * @note May not be called on a packet queue.
hexley 0:281d6ff68967 403 *
hexley 0:281d6ff68967 404 * @note Despite its name, pbuf_realloc cannot grow the size of a pbuf (chain).
hexley 0:281d6ff68967 405 */
hexley 0:281d6ff68967 406 void
hexley 0:281d6ff68967 407 pbuf_realloc(struct pbuf *p, u16_t new_len)
hexley 0:281d6ff68967 408 {
hexley 0:281d6ff68967 409 struct pbuf *q;
hexley 0:281d6ff68967 410 u16_t rem_len; /* remaining length */
hexley 0:281d6ff68967 411 s32_t grow;
hexley 0:281d6ff68967 412
hexley 0:281d6ff68967 413 LWIP_ASSERT("pbuf_realloc: p != NULL", p != NULL);
hexley 0:281d6ff68967 414 LWIP_ASSERT("pbuf_realloc: sane p->type", p->type == PBUF_POOL ||
hexley 0:281d6ff68967 415 p->type == PBUF_ROM ||
hexley 0:281d6ff68967 416 p->type == PBUF_RAM ||
hexley 0:281d6ff68967 417 p->type == PBUF_REF);
hexley 0:281d6ff68967 418
hexley 0:281d6ff68967 419 /* desired length larger than current length? */
hexley 0:281d6ff68967 420 if (new_len >= p->tot_len) {
hexley 0:281d6ff68967 421 /* enlarging not yet supported */
hexley 0:281d6ff68967 422 return;
hexley 0:281d6ff68967 423 }
hexley 0:281d6ff68967 424
hexley 0:281d6ff68967 425 /* the pbuf chain grows by (new_len - p->tot_len) bytes
hexley 0:281d6ff68967 426 * (which may be negative in case of shrinking) */
hexley 0:281d6ff68967 427 grow = new_len - p->tot_len;
hexley 0:281d6ff68967 428
hexley 0:281d6ff68967 429 /* first, step over any pbufs that should remain in the chain */
hexley 0:281d6ff68967 430 rem_len = new_len;
hexley 0:281d6ff68967 431 q = p;
hexley 0:281d6ff68967 432 /* should this pbuf be kept? */
hexley 0:281d6ff68967 433 while (rem_len > q->len) {
hexley 0:281d6ff68967 434 /* decrease remaining length by pbuf length */
hexley 0:281d6ff68967 435 rem_len -= q->len;
hexley 0:281d6ff68967 436 /* decrease total length indicator */
hexley 0:281d6ff68967 437 LWIP_ASSERT("grow < max_u16_t", grow < 0xffff);
hexley 0:281d6ff68967 438 q->tot_len += (u16_t)grow;
hexley 0:281d6ff68967 439 /* proceed to next pbuf in chain */
hexley 0:281d6ff68967 440 q = q->next;
hexley 0:281d6ff68967 441 LWIP_ASSERT("pbuf_realloc: q != NULL", q != NULL);
hexley 0:281d6ff68967 442 }
hexley 0:281d6ff68967 443 /* we have now reached the new last pbuf (in q) */
hexley 0:281d6ff68967 444 /* rem_len == desired length for pbuf q */
hexley 0:281d6ff68967 445
hexley 0:281d6ff68967 446 /* shrink allocated memory for PBUF_RAM */
hexley 0:281d6ff68967 447 /* (other types merely adjust their length fields */
hexley 0:281d6ff68967 448 if ((q->type == PBUF_RAM) && (rem_len != q->len)) {
hexley 0:281d6ff68967 449 /* reallocate and adjust the length of the pbuf that will be split */
hexley 0:281d6ff68967 450 q = (struct pbuf *)mem_trim(q, (u16_t)((u8_t *)q->payload - (u8_t *)q) + rem_len);
hexley 0:281d6ff68967 451 LWIP_ASSERT("mem_trim returned q == NULL", q != NULL);
hexley 0:281d6ff68967 452 }
hexley 0:281d6ff68967 453 /* adjust length fields for new last pbuf */
hexley 0:281d6ff68967 454 q->len = rem_len;
hexley 0:281d6ff68967 455 q->tot_len = q->len;
hexley 0:281d6ff68967 456
hexley 0:281d6ff68967 457 /* any remaining pbufs in chain? */
hexley 0:281d6ff68967 458 if (q->next != NULL) {
hexley 0:281d6ff68967 459 /* free remaining pbufs in chain */
hexley 0:281d6ff68967 460 pbuf_free(q->next);
hexley 0:281d6ff68967 461 }
hexley 0:281d6ff68967 462 /* q is last packet in chain */
hexley 0:281d6ff68967 463 q->next = NULL;
hexley 0:281d6ff68967 464
hexley 0:281d6ff68967 465 }
hexley 0:281d6ff68967 466
hexley 0:281d6ff68967 467 /**
hexley 0:281d6ff68967 468 * Adjusts the payload pointer to hide or reveal headers in the payload.
hexley 0:281d6ff68967 469 *
hexley 0:281d6ff68967 470 * Adjusts the ->payload pointer so that space for a header
hexley 0:281d6ff68967 471 * (dis)appears in the pbuf payload.
hexley 0:281d6ff68967 472 *
hexley 0:281d6ff68967 473 * The ->payload, ->tot_len and ->len fields are adjusted.
hexley 0:281d6ff68967 474 *
hexley 0:281d6ff68967 475 * @param p pbuf to change the header size.
hexley 0:281d6ff68967 476 * @param header_size_increment Number of bytes to increment header size which
hexley 0:281d6ff68967 477 * increases the size of the pbuf. New space is on the front.
hexley 0:281d6ff68967 478 * (Using a negative value decreases the header size.)
hexley 0:281d6ff68967 479 * If hdr_size_inc is 0, this function does nothing and returns succesful.
hexley 0:281d6ff68967 480 *
hexley 0:281d6ff68967 481 * PBUF_ROM and PBUF_REF type buffers cannot have their sizes increased, so
hexley 0:281d6ff68967 482 * the call will fail. A check is made that the increase in header size does
hexley 0:281d6ff68967 483 * not move the payload pointer in front of the start of the buffer.
hexley 0:281d6ff68967 484 * @return non-zero on failure, zero on success.
hexley 0:281d6ff68967 485 *
hexley 0:281d6ff68967 486 */
hexley 0:281d6ff68967 487 u8_t
hexley 0:281d6ff68967 488 pbuf_header(struct pbuf *p, s16_t header_size_increment)
hexley 0:281d6ff68967 489 {
hexley 0:281d6ff68967 490 u16_t type;
hexley 0:281d6ff68967 491 void *payload;
hexley 0:281d6ff68967 492 u16_t increment_magnitude;
hexley 0:281d6ff68967 493
hexley 0:281d6ff68967 494 LWIP_ASSERT("p != NULL", p != NULL);
hexley 0:281d6ff68967 495 if ((header_size_increment == 0) || (p == NULL)) {
hexley 0:281d6ff68967 496 return 0;
hexley 0:281d6ff68967 497 }
hexley 0:281d6ff68967 498
hexley 0:281d6ff68967 499 if (header_size_increment < 0){
hexley 0:281d6ff68967 500 increment_magnitude = -header_size_increment;
hexley 0:281d6ff68967 501 /* Check that we aren't going to move off the end of the pbuf */
hexley 0:281d6ff68967 502 LWIP_ERROR("increment_magnitude <= p->len", (increment_magnitude <= p->len), return 1;);
hexley 0:281d6ff68967 503 } else {
hexley 0:281d6ff68967 504 increment_magnitude = header_size_increment;
hexley 0:281d6ff68967 505 #if 0
hexley 0:281d6ff68967 506 /* Can't assert these as some callers speculatively call
hexley 0:281d6ff68967 507 pbuf_header() to see if it's OK. Will return 1 below instead. */
hexley 0:281d6ff68967 508 /* Check that we've got the correct type of pbuf to work with */
hexley 0:281d6ff68967 509 LWIP_ASSERT("p->type == PBUF_RAM || p->type == PBUF_POOL",
hexley 0:281d6ff68967 510 p->type == PBUF_RAM || p->type == PBUF_POOL);
hexley 0:281d6ff68967 511 /* Check that we aren't going to move off the beginning of the pbuf */
hexley 0:281d6ff68967 512 LWIP_ASSERT("p->payload - increment_magnitude >= p + SIZEOF_STRUCT_PBUF",
hexley 0:281d6ff68967 513 (u8_t *)p->payload - increment_magnitude >= (u8_t *)p + SIZEOF_STRUCT_PBUF);
hexley 0:281d6ff68967 514 #endif
hexley 0:281d6ff68967 515 }
hexley 0:281d6ff68967 516
hexley 0:281d6ff68967 517 type = p->type;
hexley 0:281d6ff68967 518 /* remember current payload pointer */
hexley 0:281d6ff68967 519 payload = p->payload;
hexley 0:281d6ff68967 520
hexley 0:281d6ff68967 521 /* pbuf types containing payloads? */
hexley 0:281d6ff68967 522 if (type == PBUF_RAM || type == PBUF_POOL) {
hexley 0:281d6ff68967 523 /* set new payload pointer */
hexley 0:281d6ff68967 524 p->payload = (u8_t *)p->payload - header_size_increment;
hexley 0:281d6ff68967 525 /* boundary check fails? */
hexley 0:281d6ff68967 526 if ((u8_t *)p->payload < (u8_t *)p + SIZEOF_STRUCT_PBUF) {
hexley 0:281d6ff68967 527 LWIP_DEBUGF( PBUF_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
hexley 0:281d6ff68967 528 ("pbuf_header: failed as %p < %p (not enough space for new header size)\n",
hexley 0:281d6ff68967 529 (void *)p->payload, (void *)(p + 1)));
hexley 0:281d6ff68967 530 /* restore old payload pointer */
hexley 0:281d6ff68967 531 p->payload = payload;
hexley 0:281d6ff68967 532 /* bail out unsuccesfully */
hexley 0:281d6ff68967 533 return 1;
hexley 0:281d6ff68967 534 }
hexley 0:281d6ff68967 535 /* pbuf types refering to external payloads? */
hexley 0:281d6ff68967 536 } else if (type == PBUF_REF || type == PBUF_ROM) {
hexley 0:281d6ff68967 537 /* hide a header in the payload? */
hexley 0:281d6ff68967 538 if ((header_size_increment < 0) && (increment_magnitude <= p->len)) {
hexley 0:281d6ff68967 539 /* increase payload pointer */
hexley 0:281d6ff68967 540 p->payload = (u8_t *)p->payload - header_size_increment;
hexley 0:281d6ff68967 541 } else {
hexley 0:281d6ff68967 542 /* cannot expand payload to front (yet!)
hexley 0:281d6ff68967 543 * bail out unsuccesfully */
hexley 0:281d6ff68967 544 return 1;
hexley 0:281d6ff68967 545 }
hexley 0:281d6ff68967 546 } else {
hexley 0:281d6ff68967 547 /* Unknown type */
hexley 0:281d6ff68967 548 LWIP_ASSERT("bad pbuf type", 0);
hexley 0:281d6ff68967 549 return 1;
hexley 0:281d6ff68967 550 }
hexley 0:281d6ff68967 551 /* modify pbuf length fields */
hexley 0:281d6ff68967 552 p->len += header_size_increment;
hexley 0:281d6ff68967 553 p->tot_len += header_size_increment;
hexley 0:281d6ff68967 554
hexley 0:281d6ff68967 555 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_header: old %p new %p (%"S16_F")\n",
hexley 0:281d6ff68967 556 (void *)payload, (void *)p->payload, header_size_increment));
hexley 0:281d6ff68967 557
hexley 0:281d6ff68967 558 return 0;
hexley 0:281d6ff68967 559 }
hexley 0:281d6ff68967 560
hexley 0:281d6ff68967 561 /**
hexley 0:281d6ff68967 562 * Dereference a pbuf chain or queue and deallocate any no-longer-used
hexley 0:281d6ff68967 563 * pbufs at the head of this chain or queue.
hexley 0:281d6ff68967 564 *
hexley 0:281d6ff68967 565 * Decrements the pbuf reference count. If it reaches zero, the pbuf is
hexley 0:281d6ff68967 566 * deallocated.
hexley 0:281d6ff68967 567 *
hexley 0:281d6ff68967 568 * For a pbuf chain, this is repeated for each pbuf in the chain,
hexley 0:281d6ff68967 569 * up to the first pbuf which has a non-zero reference count after
hexley 0:281d6ff68967 570 * decrementing. So, when all reference counts are one, the whole
hexley 0:281d6ff68967 571 * chain is free'd.
hexley 0:281d6ff68967 572 *
hexley 0:281d6ff68967 573 * @param p The pbuf (chain) to be dereferenced.
hexley 0:281d6ff68967 574 *
hexley 0:281d6ff68967 575 * @return the number of pbufs that were de-allocated
hexley 0:281d6ff68967 576 * from the head of the chain.
hexley 0:281d6ff68967 577 *
hexley 0:281d6ff68967 578 * @note MUST NOT be called on a packet queue (Not verified to work yet).
hexley 0:281d6ff68967 579 * @note the reference counter of a pbuf equals the number of pointers
hexley 0:281d6ff68967 580 * that refer to the pbuf (or into the pbuf).
hexley 0:281d6ff68967 581 *
hexley 0:281d6ff68967 582 * @internal examples:
hexley 0:281d6ff68967 583 *
hexley 0:281d6ff68967 584 * Assuming existing chains a->b->c with the following reference
hexley 0:281d6ff68967 585 * counts, calling pbuf_free(a) results in:
hexley 0:281d6ff68967 586 *
hexley 0:281d6ff68967 587 * 1->2->3 becomes ...1->3
hexley 0:281d6ff68967 588 * 3->3->3 becomes 2->3->3
hexley 0:281d6ff68967 589 * 1->1->2 becomes ......1
hexley 0:281d6ff68967 590 * 2->1->1 becomes 1->1->1
hexley 0:281d6ff68967 591 * 1->1->1 becomes .......
hexley 0:281d6ff68967 592 *
hexley 0:281d6ff68967 593 */
hexley 0:281d6ff68967 594 u8_t
hexley 0:281d6ff68967 595 pbuf_free(struct pbuf *p)
hexley 0:281d6ff68967 596 {
hexley 0:281d6ff68967 597 u16_t type;
hexley 0:281d6ff68967 598 struct pbuf *q;
hexley 0:281d6ff68967 599 u8_t count;
hexley 0:281d6ff68967 600
hexley 0:281d6ff68967 601 if (p == NULL) {
hexley 0:281d6ff68967 602 LWIP_ASSERT("p != NULL", p != NULL);
hexley 0:281d6ff68967 603 /* if assertions are disabled, proceed with debug output */
hexley 0:281d6ff68967 604 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
hexley 0:281d6ff68967 605 ("pbuf_free(p == NULL) was called.\n"));
hexley 0:281d6ff68967 606 return 0;
hexley 0:281d6ff68967 607 }
hexley 0:281d6ff68967 608 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free(%p)\n", (void *)p));
hexley 0:281d6ff68967 609
hexley 0:281d6ff68967 610 PERF_START;
hexley 0:281d6ff68967 611
hexley 0:281d6ff68967 612 LWIP_ASSERT("pbuf_free: sane type",
hexley 0:281d6ff68967 613 p->type == PBUF_RAM || p->type == PBUF_ROM ||
hexley 0:281d6ff68967 614 p->type == PBUF_REF || p->type == PBUF_POOL);
hexley 0:281d6ff68967 615
hexley 0:281d6ff68967 616 count = 0;
hexley 0:281d6ff68967 617 /* de-allocate all consecutive pbufs from the head of the chain that
hexley 0:281d6ff68967 618 * obtain a zero reference count after decrementing*/
hexley 0:281d6ff68967 619 while (p != NULL) {
hexley 0:281d6ff68967 620 u16_t ref;
hexley 0:281d6ff68967 621 SYS_ARCH_DECL_PROTECT(old_level);
hexley 0:281d6ff68967 622 /* Since decrementing ref cannot be guaranteed to be a single machine operation
hexley 0:281d6ff68967 623 * we must protect it. We put the new ref into a local variable to prevent
hexley 0:281d6ff68967 624 * further protection. */
hexley 0:281d6ff68967 625 SYS_ARCH_PROTECT(old_level);
hexley 0:281d6ff68967 626 /* all pbufs in a chain are referenced at least once */
hexley 0:281d6ff68967 627 LWIP_ASSERT("pbuf_free: p->ref > 0", p->ref > 0);
hexley 0:281d6ff68967 628 /* decrease reference count (number of pointers to pbuf) */
hexley 0:281d6ff68967 629 ref = --(p->ref);
hexley 0:281d6ff68967 630 SYS_ARCH_UNPROTECT(old_level);
hexley 0:281d6ff68967 631 /* this pbuf is no longer referenced to? */
hexley 0:281d6ff68967 632 if (ref == 0) {
hexley 0:281d6ff68967 633 /* remember next pbuf in chain for next iteration */
hexley 0:281d6ff68967 634 q = p->next;
hexley 0:281d6ff68967 635 LWIP_DEBUGF( PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free: deallocating %p\n", (void *)p));
hexley 0:281d6ff68967 636 type = p->type;
hexley 0:281d6ff68967 637 #if LWIP_SUPPORT_CUSTOM_PBUF
hexley 0:281d6ff68967 638 /* is this a custom pbuf? */
hexley 0:281d6ff68967 639 if ((p->flags & PBUF_FLAG_IS_CUSTOM) != 0) {
hexley 0:281d6ff68967 640 struct pbuf_custom *pc = (struct pbuf_custom*)p;
hexley 0:281d6ff68967 641 LWIP_ASSERT("pc->custom_free_function != NULL", pc->custom_free_function != NULL);
hexley 0:281d6ff68967 642 pc->custom_free_function(p);
hexley 0:281d6ff68967 643 } else
hexley 0:281d6ff68967 644 #endif /* LWIP_SUPPORT_CUSTOM_PBUF */
hexley 0:281d6ff68967 645 {
hexley 0:281d6ff68967 646 /* is this a pbuf from the pool? */
hexley 0:281d6ff68967 647 if (type == PBUF_POOL) {
hexley 0:281d6ff68967 648 memp_free(MEMP_PBUF_POOL, p);
hexley 0:281d6ff68967 649 /* is this a ROM or RAM referencing pbuf? */
hexley 0:281d6ff68967 650 } else if (type == PBUF_ROM || type == PBUF_REF) {
hexley 0:281d6ff68967 651 memp_free(MEMP_PBUF, p);
hexley 0:281d6ff68967 652 /* type == PBUF_RAM */
hexley 0:281d6ff68967 653 } else {
hexley 0:281d6ff68967 654 mem_free(p);
hexley 0:281d6ff68967 655 }
hexley 0:281d6ff68967 656 }
hexley 0:281d6ff68967 657 count++;
hexley 0:281d6ff68967 658 /* proceed to next pbuf */
hexley 0:281d6ff68967 659 p = q;
hexley 0:281d6ff68967 660 /* p->ref > 0, this pbuf is still referenced to */
hexley 0:281d6ff68967 661 /* (and so the remaining pbufs in chain as well) */
hexley 0:281d6ff68967 662 } else {
hexley 0:281d6ff68967 663 LWIP_DEBUGF( PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free: %p has ref %"U16_F", ending here.\n", (void *)p, ref));
hexley 0:281d6ff68967 664 /* stop walking through the chain */
hexley 0:281d6ff68967 665 p = NULL;
hexley 0:281d6ff68967 666 }
hexley 0:281d6ff68967 667 }
hexley 0:281d6ff68967 668 PERF_STOP("pbuf_free");
hexley 0:281d6ff68967 669 /* return number of de-allocated pbufs */
hexley 0:281d6ff68967 670 return count;
hexley 0:281d6ff68967 671 }
hexley 0:281d6ff68967 672
hexley 0:281d6ff68967 673 /**
hexley 0:281d6ff68967 674 * Count number of pbufs in a chain
hexley 0:281d6ff68967 675 *
hexley 0:281d6ff68967 676 * @param p first pbuf of chain
hexley 0:281d6ff68967 677 * @return the number of pbufs in a chain
hexley 0:281d6ff68967 678 */
hexley 0:281d6ff68967 679
hexley 0:281d6ff68967 680 u8_t
hexley 0:281d6ff68967 681 pbuf_clen(struct pbuf *p)
hexley 0:281d6ff68967 682 {
hexley 0:281d6ff68967 683 u8_t len;
hexley 0:281d6ff68967 684
hexley 0:281d6ff68967 685 len = 0;
hexley 0:281d6ff68967 686 while (p != NULL) {
hexley 0:281d6ff68967 687 ++len;
hexley 0:281d6ff68967 688 p = p->next;
hexley 0:281d6ff68967 689 }
hexley 0:281d6ff68967 690 return len;
hexley 0:281d6ff68967 691 }
hexley 0:281d6ff68967 692
hexley 0:281d6ff68967 693 /**
hexley 0:281d6ff68967 694 * Increment the reference count of the pbuf.
hexley 0:281d6ff68967 695 *
hexley 0:281d6ff68967 696 * @param p pbuf to increase reference counter of
hexley 0:281d6ff68967 697 *
hexley 0:281d6ff68967 698 */
hexley 0:281d6ff68967 699 void
hexley 0:281d6ff68967 700 pbuf_ref(struct pbuf *p)
hexley 0:281d6ff68967 701 {
hexley 0:281d6ff68967 702 SYS_ARCH_DECL_PROTECT(old_level);
hexley 0:281d6ff68967 703 /* pbuf given? */
hexley 0:281d6ff68967 704 if (p != NULL) {
hexley 0:281d6ff68967 705 SYS_ARCH_PROTECT(old_level);
hexley 0:281d6ff68967 706 ++(p->ref);
hexley 0:281d6ff68967 707 SYS_ARCH_UNPROTECT(old_level);
hexley 0:281d6ff68967 708 }
hexley 0:281d6ff68967 709 }
hexley 0:281d6ff68967 710
hexley 0:281d6ff68967 711 /**
hexley 0:281d6ff68967 712 * Concatenate two pbufs (each may be a pbuf chain) and take over
hexley 0:281d6ff68967 713 * the caller's reference of the tail pbuf.
hexley 0:281d6ff68967 714 *
hexley 0:281d6ff68967 715 * @note The caller MAY NOT reference the tail pbuf afterwards.
hexley 0:281d6ff68967 716 * Use pbuf_chain() for that purpose.
hexley 0:281d6ff68967 717 *
hexley 0:281d6ff68967 718 * @see pbuf_chain()
hexley 0:281d6ff68967 719 */
hexley 0:281d6ff68967 720
hexley 0:281d6ff68967 721 void
hexley 0:281d6ff68967 722 pbuf_cat(struct pbuf *h, struct pbuf *t)
hexley 0:281d6ff68967 723 {
hexley 0:281d6ff68967 724 struct pbuf *p;
hexley 0:281d6ff68967 725
hexley 0:281d6ff68967 726 LWIP_ERROR("(h != NULL) && (t != NULL) (programmer violates API)",
hexley 0:281d6ff68967 727 ((h != NULL) && (t != NULL)), return;);
hexley 0:281d6ff68967 728
hexley 0:281d6ff68967 729 /* proceed to last pbuf of chain */
hexley 0:281d6ff68967 730 for (p = h; p->next != NULL; p = p->next) {
hexley 0:281d6ff68967 731 /* add total length of second chain to all totals of first chain */
hexley 0:281d6ff68967 732 p->tot_len += t->tot_len;
hexley 0:281d6ff68967 733 }
hexley 0:281d6ff68967 734 /* { p is last pbuf of first h chain, p->next == NULL } */
hexley 0:281d6ff68967 735 LWIP_ASSERT("p->tot_len == p->len (of last pbuf in chain)", p->tot_len == p->len);
hexley 0:281d6ff68967 736 LWIP_ASSERT("p->next == NULL", p->next == NULL);
hexley 0:281d6ff68967 737 /* add total length of second chain to last pbuf total of first chain */
hexley 0:281d6ff68967 738 p->tot_len += t->tot_len;
hexley 0:281d6ff68967 739 /* chain last pbuf of head (p) with first of tail (t) */
hexley 0:281d6ff68967 740 p->next = t;
hexley 0:281d6ff68967 741 /* p->next now references t, but the caller will drop its reference to t,
hexley 0:281d6ff68967 742 * so netto there is no change to the reference count of t.
hexley 0:281d6ff68967 743 */
hexley 0:281d6ff68967 744 }
hexley 0:281d6ff68967 745
hexley 0:281d6ff68967 746 /**
hexley 0:281d6ff68967 747 * Chain two pbufs (or pbuf chains) together.
hexley 0:281d6ff68967 748 *
hexley 0:281d6ff68967 749 * The caller MUST call pbuf_free(t) once it has stopped
hexley 0:281d6ff68967 750 * using it. Use pbuf_cat() instead if you no longer use t.
hexley 0:281d6ff68967 751 *
hexley 0:281d6ff68967 752 * @param h head pbuf (chain)
hexley 0:281d6ff68967 753 * @param t tail pbuf (chain)
hexley 0:281d6ff68967 754 * @note The pbufs MUST belong to the same packet.
hexley 0:281d6ff68967 755 * @note MAY NOT be called on a packet queue.
hexley 0:281d6ff68967 756 *
hexley 0:281d6ff68967 757 * The ->tot_len fields of all pbufs of the head chain are adjusted.
hexley 0:281d6ff68967 758 * The ->next field of the last pbuf of the head chain is adjusted.
hexley 0:281d6ff68967 759 * The ->ref field of the first pbuf of the tail chain is adjusted.
hexley 0:281d6ff68967 760 *
hexley 0:281d6ff68967 761 */
hexley 0:281d6ff68967 762 void
hexley 0:281d6ff68967 763 pbuf_chain(struct pbuf *h, struct pbuf *t)
hexley 0:281d6ff68967 764 {
hexley 0:281d6ff68967 765 pbuf_cat(h, t);
hexley 0:281d6ff68967 766 /* t is now referenced by h */
hexley 0:281d6ff68967 767 pbuf_ref(t);
hexley 0:281d6ff68967 768 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_chain: %p references %p\n", (void *)h, (void *)t));
hexley 0:281d6ff68967 769 }
hexley 0:281d6ff68967 770
hexley 0:281d6ff68967 771 /**
hexley 0:281d6ff68967 772 * Dechains the first pbuf from its succeeding pbufs in the chain.
hexley 0:281d6ff68967 773 *
hexley 0:281d6ff68967 774 * Makes p->tot_len field equal to p->len.
hexley 0:281d6ff68967 775 * @param p pbuf to dechain
hexley 0:281d6ff68967 776 * @return remainder of the pbuf chain, or NULL if it was de-allocated.
hexley 0:281d6ff68967 777 * @note May not be called on a packet queue.
hexley 0:281d6ff68967 778 */
hexley 0:281d6ff68967 779 struct pbuf *
hexley 0:281d6ff68967 780 pbuf_dechain(struct pbuf *p)
hexley 0:281d6ff68967 781 {
hexley 0:281d6ff68967 782 struct pbuf *q;
hexley 0:281d6ff68967 783 u8_t tail_gone = 1;
hexley 0:281d6ff68967 784 /* tail */
hexley 0:281d6ff68967 785 q = p->next;
hexley 0:281d6ff68967 786 /* pbuf has successor in chain? */
hexley 0:281d6ff68967 787 if (q != NULL) {
hexley 0:281d6ff68967 788 /* assert tot_len invariant: (p->tot_len == p->len + (p->next? p->next->tot_len: 0) */
hexley 0:281d6ff68967 789 LWIP_ASSERT("p->tot_len == p->len + q->tot_len", q->tot_len == p->tot_len - p->len);
hexley 0:281d6ff68967 790 /* enforce invariant if assertion is disabled */
hexley 0:281d6ff68967 791 q->tot_len = p->tot_len - p->len;
hexley 0:281d6ff68967 792 /* decouple pbuf from remainder */
hexley 0:281d6ff68967 793 p->next = NULL;
hexley 0:281d6ff68967 794 /* total length of pbuf p is its own length only */
hexley 0:281d6ff68967 795 p->tot_len = p->len;
hexley 0:281d6ff68967 796 /* q is no longer referenced by p, free it */
hexley 0:281d6ff68967 797 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_dechain: unreferencing %p\n", (void *)q));
hexley 0:281d6ff68967 798 tail_gone = pbuf_free(q);
hexley 0:281d6ff68967 799 if (tail_gone > 0) {
hexley 0:281d6ff68967 800 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE,
hexley 0:281d6ff68967 801 ("pbuf_dechain: deallocated %p (as it is no longer referenced)\n", (void *)q));
hexley 0:281d6ff68967 802 }
hexley 0:281d6ff68967 803 /* return remaining tail or NULL if deallocated */
hexley 0:281d6ff68967 804 }
hexley 0:281d6ff68967 805 /* assert tot_len invariant: (p->tot_len == p->len + (p->next? p->next->tot_len: 0) */
hexley 0:281d6ff68967 806 LWIP_ASSERT("p->tot_len == p->len", p->tot_len == p->len);
hexley 0:281d6ff68967 807 return ((tail_gone > 0) ? NULL : q);
hexley 0:281d6ff68967 808 }
hexley 0:281d6ff68967 809
hexley 0:281d6ff68967 810 /**
hexley 0:281d6ff68967 811 *
hexley 0:281d6ff68967 812 * Create PBUF_RAM copies of pbufs.
hexley 0:281d6ff68967 813 *
hexley 0:281d6ff68967 814 * Used to queue packets on behalf of the lwIP stack, such as
hexley 0:281d6ff68967 815 * ARP based queueing.
hexley 0:281d6ff68967 816 *
hexley 0:281d6ff68967 817 * @note You MUST explicitly use p = pbuf_take(p);
hexley 0:281d6ff68967 818 *
hexley 0:281d6ff68967 819 * @note Only one packet is copied, no packet queue!
hexley 0:281d6ff68967 820 *
hexley 0:281d6ff68967 821 * @param p_to pbuf destination of the copy
hexley 0:281d6ff68967 822 * @param p_from pbuf source of the copy
hexley 0:281d6ff68967 823 *
hexley 0:281d6ff68967 824 * @return ERR_OK if pbuf was copied
hexley 0:281d6ff68967 825 * ERR_ARG if one of the pbufs is NULL or p_to is not big
hexley 0:281d6ff68967 826 * enough to hold p_from
hexley 0:281d6ff68967 827 */
hexley 0:281d6ff68967 828 err_t
hexley 0:281d6ff68967 829 pbuf_copy(struct pbuf *p_to, struct pbuf *p_from)
hexley 0:281d6ff68967 830 {
hexley 0:281d6ff68967 831 u16_t offset_to=0, offset_from=0, len;
hexley 0:281d6ff68967 832
hexley 0:281d6ff68967 833 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_copy(%p, %p)\n",
hexley 0:281d6ff68967 834 (void*)p_to, (void*)p_from));
hexley 0:281d6ff68967 835
hexley 0:281d6ff68967 836 /* is the target big enough to hold the source? */
hexley 0:281d6ff68967 837 LWIP_ERROR("pbuf_copy: target not big enough to hold source", ((p_to != NULL) &&
hexley 0:281d6ff68967 838 (p_from != NULL) && (p_to->tot_len >= p_from->tot_len)), return ERR_ARG;);
hexley 0:281d6ff68967 839
hexley 0:281d6ff68967 840 /* iterate through pbuf chain */
hexley 0:281d6ff68967 841 do
hexley 0:281d6ff68967 842 {
hexley 0:281d6ff68967 843 LWIP_ASSERT("p_to != NULL", p_to != NULL);
hexley 0:281d6ff68967 844 /* copy one part of the original chain */
hexley 0:281d6ff68967 845 if ((p_to->len - offset_to) >= (p_from->len - offset_from)) {
hexley 0:281d6ff68967 846 /* complete current p_from fits into current p_to */
hexley 0:281d6ff68967 847 len = p_from->len - offset_from;
hexley 0:281d6ff68967 848 } else {
hexley 0:281d6ff68967 849 /* current p_from does not fit into current p_to */
hexley 0:281d6ff68967 850 len = p_to->len - offset_to;
hexley 0:281d6ff68967 851 }
hexley 0:281d6ff68967 852 MEMCPY((u8_t*)p_to->payload + offset_to, (u8_t*)p_from->payload + offset_from, len);
hexley 0:281d6ff68967 853 offset_to += len;
hexley 0:281d6ff68967 854 offset_from += len;
hexley 0:281d6ff68967 855 LWIP_ASSERT("offset_to <= p_to->len", offset_to <= p_to->len);
hexley 0:281d6ff68967 856 if (offset_to == p_to->len) {
hexley 0:281d6ff68967 857 /* on to next p_to (if any) */
hexley 0:281d6ff68967 858 offset_to = 0;
hexley 0:281d6ff68967 859 p_to = p_to->next;
hexley 0:281d6ff68967 860 }
hexley 0:281d6ff68967 861 LWIP_ASSERT("offset_from <= p_from->len", offset_from <= p_from->len);
hexley 0:281d6ff68967 862 if (offset_from >= p_from->len) {
hexley 0:281d6ff68967 863 /* on to next p_from (if any) */
hexley 0:281d6ff68967 864 offset_from = 0;
hexley 0:281d6ff68967 865 p_from = p_from->next;
hexley 0:281d6ff68967 866 }
hexley 0:281d6ff68967 867
hexley 0:281d6ff68967 868 if((p_from != NULL) && (p_from->len == p_from->tot_len)) {
hexley 0:281d6ff68967 869 /* don't copy more than one packet! */
hexley 0:281d6ff68967 870 LWIP_ERROR("pbuf_copy() does not allow packet queues!\n",
hexley 0:281d6ff68967 871 (p_from->next == NULL), return ERR_VAL;);
hexley 0:281d6ff68967 872 }
hexley 0:281d6ff68967 873 if((p_to != NULL) && (p_to->len == p_to->tot_len)) {
hexley 0:281d6ff68967 874 /* don't copy more than one packet! */
hexley 0:281d6ff68967 875 LWIP_ERROR("pbuf_copy() does not allow packet queues!\n",
hexley 0:281d6ff68967 876 (p_to->next == NULL), return ERR_VAL;);
hexley 0:281d6ff68967 877 }
hexley 0:281d6ff68967 878 } while (p_from);
hexley 0:281d6ff68967 879 LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_copy: end of chain reached.\n"));
hexley 0:281d6ff68967 880 return ERR_OK;
hexley 0:281d6ff68967 881 }
hexley 0:281d6ff68967 882
hexley 0:281d6ff68967 883 /**
hexley 0:281d6ff68967 884 * Copy (part of) the contents of a packet buffer
hexley 0:281d6ff68967 885 * to an application supplied buffer.
hexley 0:281d6ff68967 886 *
hexley 0:281d6ff68967 887 * @param buf the pbuf from which to copy data
hexley 0:281d6ff68967 888 * @param dataptr the application supplied buffer
hexley 0:281d6ff68967 889 * @param len length of data to copy (dataptr must be big enough). No more
hexley 0:281d6ff68967 890 * than buf->tot_len will be copied, irrespective of len
hexley 0:281d6ff68967 891 * @param offset offset into the packet buffer from where to begin copying len bytes
hexley 0:281d6ff68967 892 * @return the number of bytes copied, or 0 on failure
hexley 0:281d6ff68967 893 */
hexley 0:281d6ff68967 894 u16_t
hexley 0:281d6ff68967 895 pbuf_copy_partial(struct pbuf *buf, void *dataptr, u16_t len, u16_t offset)
hexley 0:281d6ff68967 896 {
hexley 0:281d6ff68967 897 struct pbuf *p;
hexley 0:281d6ff68967 898 u16_t left;
hexley 0:281d6ff68967 899 u16_t buf_copy_len;
hexley 0:281d6ff68967 900 u16_t copied_total = 0;
hexley 0:281d6ff68967 901
hexley 0:281d6ff68967 902 LWIP_ERROR("pbuf_copy_partial: invalid buf", (buf != NULL), return 0;);
hexley 0:281d6ff68967 903 LWIP_ERROR("pbuf_copy_partial: invalid dataptr", (dataptr != NULL), return 0;);
hexley 0:281d6ff68967 904
hexley 0:281d6ff68967 905 left = 0;
hexley 0:281d6ff68967 906
hexley 0:281d6ff68967 907 if((buf == NULL) || (dataptr == NULL)) {
hexley 0:281d6ff68967 908 return 0;
hexley 0:281d6ff68967 909 }
hexley 0:281d6ff68967 910
hexley 0:281d6ff68967 911 /* Note some systems use byte copy if dataptr or one of the pbuf payload pointers are unaligned. */
hexley 0:281d6ff68967 912 for(p = buf; len != 0 && p != NULL; p = p->next) {
hexley 0:281d6ff68967 913 if ((offset != 0) && (offset >= p->len)) {
hexley 0:281d6ff68967 914 /* don't copy from this buffer -> on to the next */
hexley 0:281d6ff68967 915 offset -= p->len;
hexley 0:281d6ff68967 916 } else {
hexley 0:281d6ff68967 917 /* copy from this buffer. maybe only partially. */
hexley 0:281d6ff68967 918 buf_copy_len = p->len - offset;
hexley 0:281d6ff68967 919 if (buf_copy_len > len)
hexley 0:281d6ff68967 920 buf_copy_len = len;
hexley 0:281d6ff68967 921 /* copy the necessary parts of the buffer */
hexley 0:281d6ff68967 922 MEMCPY(&((char*)dataptr)[left], &((char*)p->payload)[offset], buf_copy_len);
hexley 0:281d6ff68967 923 copied_total += buf_copy_len;
hexley 0:281d6ff68967 924 left += buf_copy_len;
hexley 0:281d6ff68967 925 len -= buf_copy_len;
hexley 0:281d6ff68967 926 offset = 0;
hexley 0:281d6ff68967 927 }
hexley 0:281d6ff68967 928 }
hexley 0:281d6ff68967 929 return copied_total;
hexley 0:281d6ff68967 930 }
hexley 0:281d6ff68967 931
hexley 0:281d6ff68967 932 /**
hexley 0:281d6ff68967 933 * Copy application supplied data into a pbuf.
hexley 0:281d6ff68967 934 * This function can only be used to copy the equivalent of buf->tot_len data.
hexley 0:281d6ff68967 935 *
hexley 0:281d6ff68967 936 * @param buf pbuf to fill with data
hexley 0:281d6ff68967 937 * @param dataptr application supplied data buffer
hexley 0:281d6ff68967 938 * @param len length of the application supplied data buffer
hexley 0:281d6ff68967 939 *
hexley 0:281d6ff68967 940 * @return ERR_OK if successful, ERR_MEM if the pbuf is not big enough
hexley 0:281d6ff68967 941 */
hexley 0:281d6ff68967 942 err_t
hexley 0:281d6ff68967 943 pbuf_take(struct pbuf *buf, const void *dataptr, u16_t len)
hexley 0:281d6ff68967 944 {
hexley 0:281d6ff68967 945 struct pbuf *p;
hexley 0:281d6ff68967 946 u16_t buf_copy_len;
hexley 0:281d6ff68967 947 u16_t total_copy_len = len;
hexley 0:281d6ff68967 948 u16_t copied_total = 0;
hexley 0:281d6ff68967 949
hexley 0:281d6ff68967 950 LWIP_ERROR("pbuf_take: invalid buf", (buf != NULL), return 0;);
hexley 0:281d6ff68967 951 LWIP_ERROR("pbuf_take: invalid dataptr", (dataptr != NULL), return 0;);
hexley 0:281d6ff68967 952
hexley 0:281d6ff68967 953 if ((buf == NULL) || (dataptr == NULL) || (buf->tot_len < len)) {
hexley 0:281d6ff68967 954 return ERR_ARG;
hexley 0:281d6ff68967 955 }
hexley 0:281d6ff68967 956
hexley 0:281d6ff68967 957 /* Note some systems use byte copy if dataptr or one of the pbuf payload pointers are unaligned. */
hexley 0:281d6ff68967 958 for(p = buf; total_copy_len != 0; p = p->next) {
hexley 0:281d6ff68967 959 LWIP_ASSERT("pbuf_take: invalid pbuf", p != NULL);
hexley 0:281d6ff68967 960 buf_copy_len = total_copy_len;
hexley 0:281d6ff68967 961 if (buf_copy_len > p->len) {
hexley 0:281d6ff68967 962 /* this pbuf cannot hold all remaining data */
hexley 0:281d6ff68967 963 buf_copy_len = p->len;
hexley 0:281d6ff68967 964 }
hexley 0:281d6ff68967 965 /* copy the necessary parts of the buffer */
hexley 0:281d6ff68967 966 MEMCPY(p->payload, &((char*)dataptr)[copied_total], buf_copy_len);
hexley 0:281d6ff68967 967 total_copy_len -= buf_copy_len;
hexley 0:281d6ff68967 968 copied_total += buf_copy_len;
hexley 0:281d6ff68967 969 }
hexley 0:281d6ff68967 970 LWIP_ASSERT("did not copy all data", total_copy_len == 0 && copied_total == len);
hexley 0:281d6ff68967 971 return ERR_OK;
hexley 0:281d6ff68967 972 }
hexley 0:281d6ff68967 973
hexley 0:281d6ff68967 974 /**
hexley 0:281d6ff68967 975 * Creates a single pbuf out of a queue of pbufs.
hexley 0:281d6ff68967 976 *
hexley 0:281d6ff68967 977 * @remark: Either the source pbuf 'p' is freed by this function or the original
hexley 0:281d6ff68967 978 * pbuf 'p' is returned, therefore the caller has to check the result!
hexley 0:281d6ff68967 979 *
hexley 0:281d6ff68967 980 * @param p the source pbuf
hexley 0:281d6ff68967 981 * @param layer pbuf_layer of the new pbuf
hexley 0:281d6ff68967 982 *
hexley 0:281d6ff68967 983 * @return a new, single pbuf (p->next is NULL)
hexley 0:281d6ff68967 984 * or the old pbuf if allocation fails
hexley 0:281d6ff68967 985 */
hexley 0:281d6ff68967 986 struct pbuf*
hexley 0:281d6ff68967 987 pbuf_coalesce(struct pbuf *p, pbuf_layer layer)
hexley 0:281d6ff68967 988 {
hexley 0:281d6ff68967 989 struct pbuf *q;
hexley 0:281d6ff68967 990 err_t err;
hexley 0:281d6ff68967 991 if (p->next == NULL) {
hexley 0:281d6ff68967 992 return p;
hexley 0:281d6ff68967 993 }
hexley 0:281d6ff68967 994 q = pbuf_alloc(layer, p->tot_len, PBUF_RAM);
hexley 0:281d6ff68967 995 if (q == NULL) {
hexley 0:281d6ff68967 996 /* @todo: what do we do now? */
hexley 0:281d6ff68967 997 return p;
hexley 0:281d6ff68967 998 }
hexley 0:281d6ff68967 999 err = pbuf_copy(q, p);
hexley 0:281d6ff68967 1000 LWIP_ASSERT("pbuf_copy failed", err == ERR_OK);
hexley 0:281d6ff68967 1001 pbuf_free(p);
hexley 0:281d6ff68967 1002 return q;
hexley 0:281d6ff68967 1003 }
hexley 0:281d6ff68967 1004
hexley 0:281d6ff68967 1005 #if LWIP_CHECKSUM_ON_COPY
hexley 0:281d6ff68967 1006 /**
hexley 0:281d6ff68967 1007 * Copies data into a single pbuf (*not* into a pbuf queue!) and updates
hexley 0:281d6ff68967 1008 * the checksum while copying
hexley 0:281d6ff68967 1009 *
hexley 0:281d6ff68967 1010 * @param p the pbuf to copy data into
hexley 0:281d6ff68967 1011 * @param start_offset offset of p->payload where to copy the data to
hexley 0:281d6ff68967 1012 * @param dataptr data to copy into the pbuf
hexley 0:281d6ff68967 1013 * @param len length of data to copy into the pbuf
hexley 0:281d6ff68967 1014 * @param chksum pointer to the checksum which is updated
hexley 0:281d6ff68967 1015 * @return ERR_OK if successful, another error if the data does not fit
hexley 0:281d6ff68967 1016 * within the (first) pbuf (no pbuf queues!)
hexley 0:281d6ff68967 1017 */
hexley 0:281d6ff68967 1018 err_t
hexley 0:281d6ff68967 1019 pbuf_fill_chksum(struct pbuf *p, u16_t start_offset, const void *dataptr,
hexley 0:281d6ff68967 1020 u16_t len, u16_t *chksum)
hexley 0:281d6ff68967 1021 {
hexley 0:281d6ff68967 1022 u32_t acc;
hexley 0:281d6ff68967 1023 u16_t copy_chksum;
hexley 0:281d6ff68967 1024 char *dst_ptr;
hexley 0:281d6ff68967 1025 LWIP_ASSERT("p != NULL", p != NULL);
hexley 0:281d6ff68967 1026 LWIP_ASSERT("dataptr != NULL", dataptr != NULL);
hexley 0:281d6ff68967 1027 LWIP_ASSERT("chksum != NULL", chksum != NULL);
hexley 0:281d6ff68967 1028 LWIP_ASSERT("len != 0", len != 0);
hexley 0:281d6ff68967 1029
hexley 0:281d6ff68967 1030 if ((start_offset >= p->len) || (start_offset + len > p->len)) {
hexley 0:281d6ff68967 1031 return ERR_ARG;
hexley 0:281d6ff68967 1032 }
hexley 0:281d6ff68967 1033
hexley 0:281d6ff68967 1034 dst_ptr = ((char*)p->payload) + start_offset;
hexley 0:281d6ff68967 1035 copy_chksum = LWIP_CHKSUM_COPY(dst_ptr, dataptr, len);
hexley 0:281d6ff68967 1036 if ((start_offset & 1) != 0) {
hexley 0:281d6ff68967 1037 copy_chksum = SWAP_BYTES_IN_WORD(copy_chksum);
hexley 0:281d6ff68967 1038 }
hexley 0:281d6ff68967 1039 acc = *chksum;
hexley 0:281d6ff68967 1040 acc += copy_chksum;
hexley 0:281d6ff68967 1041 *chksum = FOLD_U32T(acc);
hexley 0:281d6ff68967 1042 return ERR_OK;
hexley 0:281d6ff68967 1043 }
hexley 0:281d6ff68967 1044 #endif /* LWIP_CHECKSUM_ON_COPY */
hexley 0:281d6ff68967 1045
hexley 0:281d6ff68967 1046 /** Get one byte from the specified position in a pbuf
hexley 0:281d6ff68967 1047 * WARNING: returns zero for offset >= p->tot_len
hexley 0:281d6ff68967 1048 *
hexley 0:281d6ff68967 1049 * @param p pbuf to parse
hexley 0:281d6ff68967 1050 * @param offset offset into p of the byte to return
hexley 0:281d6ff68967 1051 * @return byte at an offset into p OR ZERO IF 'offset' >= p->tot_len
hexley 0:281d6ff68967 1052 */
hexley 0:281d6ff68967 1053 u8_t
hexley 0:281d6ff68967 1054 pbuf_get_at(struct pbuf* p, u16_t offset)
hexley 0:281d6ff68967 1055 {
hexley 0:281d6ff68967 1056 u16_t copy_from = offset;
hexley 0:281d6ff68967 1057 struct pbuf* q = p;
hexley 0:281d6ff68967 1058
hexley 0:281d6ff68967 1059 /* get the correct pbuf */
hexley 0:281d6ff68967 1060 while ((q != NULL) && (q->len <= copy_from)) {
hexley 0:281d6ff68967 1061 copy_from -= q->len;
hexley 0:281d6ff68967 1062 q = q->next;
hexley 0:281d6ff68967 1063 }
hexley 0:281d6ff68967 1064 /* return requested data if pbuf is OK */
hexley 0:281d6ff68967 1065 if ((q != NULL) && (q->len > copy_from)) {
hexley 0:281d6ff68967 1066 return ((u8_t*)q->payload)[copy_from];
hexley 0:281d6ff68967 1067 }
hexley 0:281d6ff68967 1068 return 0;
hexley 0:281d6ff68967 1069 }
hexley 0:281d6ff68967 1070
hexley 0:281d6ff68967 1071 /** Compare pbuf contents at specified offset with memory s2, both of length n
hexley 0:281d6ff68967 1072 *
hexley 0:281d6ff68967 1073 * @param p pbuf to compare
hexley 0:281d6ff68967 1074 * @param offset offset into p at wich to start comparing
hexley 0:281d6ff68967 1075 * @param s2 buffer to compare
hexley 0:281d6ff68967 1076 * @param n length of buffer to compare
hexley 0:281d6ff68967 1077 * @return zero if equal, nonzero otherwise
hexley 0:281d6ff68967 1078 * (0xffff if p is too short, diffoffset+1 otherwise)
hexley 0:281d6ff68967 1079 */
hexley 0:281d6ff68967 1080 u16_t
hexley 0:281d6ff68967 1081 pbuf_memcmp(struct pbuf* p, u16_t offset, const void* s2, u16_t n)
hexley 0:281d6ff68967 1082 {
hexley 0:281d6ff68967 1083 u16_t start = offset;
hexley 0:281d6ff68967 1084 struct pbuf* q = p;
hexley 0:281d6ff68967 1085
hexley 0:281d6ff68967 1086 /* get the correct pbuf */
hexley 0:281d6ff68967 1087 while ((q != NULL) && (q->len <= start)) {
hexley 0:281d6ff68967 1088 start -= q->len;
hexley 0:281d6ff68967 1089 q = q->next;
hexley 0:281d6ff68967 1090 }
hexley 0:281d6ff68967 1091 /* return requested data if pbuf is OK */
hexley 0:281d6ff68967 1092 if ((q != NULL) && (q->len > start)) {
hexley 0:281d6ff68967 1093 u16_t i;
hexley 0:281d6ff68967 1094 for(i = 0; i < n; i++) {
hexley 0:281d6ff68967 1095 u8_t a = pbuf_get_at(q, start + i);
hexley 0:281d6ff68967 1096 u8_t b = ((u8_t*)s2)[i];
hexley 0:281d6ff68967 1097 if (a != b) {
hexley 0:281d6ff68967 1098 return i+1;
hexley 0:281d6ff68967 1099 }
hexley 0:281d6ff68967 1100 }
hexley 0:281d6ff68967 1101 return 0;
hexley 0:281d6ff68967 1102 }
hexley 0:281d6ff68967 1103 return 0xffff;
hexley 0:281d6ff68967 1104 }
hexley 0:281d6ff68967 1105
hexley 0:281d6ff68967 1106 /** Find occurrence of mem (with length mem_len) in pbuf p, starting at offset
hexley 0:281d6ff68967 1107 * start_offset.
hexley 0:281d6ff68967 1108 *
hexley 0:281d6ff68967 1109 * @param p pbuf to search, maximum length is 0xFFFE since 0xFFFF is used as
hexley 0:281d6ff68967 1110 * return value 'not found'
hexley 0:281d6ff68967 1111 * @param mem search for the contents of this buffer
hexley 0:281d6ff68967 1112 * @param mem_len length of 'mem'
hexley 0:281d6ff68967 1113 * @param start_offset offset into p at which to start searching
hexley 0:281d6ff68967 1114 * @return 0xFFFF if substr was not found in p or the index where it was found
hexley 0:281d6ff68967 1115 */
hexley 0:281d6ff68967 1116 u16_t
hexley 0:281d6ff68967 1117 pbuf_memfind(struct pbuf* p, const void* mem, u16_t mem_len, u16_t start_offset)
hexley 0:281d6ff68967 1118 {
hexley 0:281d6ff68967 1119 u16_t i;
hexley 0:281d6ff68967 1120 u16_t max = p->tot_len - mem_len;
hexley 0:281d6ff68967 1121 if (p->tot_len >= mem_len + start_offset) {
hexley 0:281d6ff68967 1122 for(i = start_offset; i <= max; ) {
hexley 0:281d6ff68967 1123 u16_t plus = pbuf_memcmp(p, i, mem, mem_len);
hexley 0:281d6ff68967 1124 if (plus == 0) {
hexley 0:281d6ff68967 1125 return i;
hexley 0:281d6ff68967 1126 } else {
hexley 0:281d6ff68967 1127 i += plus;
hexley 0:281d6ff68967 1128 }
hexley 0:281d6ff68967 1129 }
hexley 0:281d6ff68967 1130 }
hexley 0:281d6ff68967 1131 return 0xFFFF;
hexley 0:281d6ff68967 1132 }
hexley 0:281d6ff68967 1133
hexley 0:281d6ff68967 1134 /** Find occurrence of substr with length substr_len in pbuf p, start at offset
hexley 0:281d6ff68967 1135 * start_offset
hexley 0:281d6ff68967 1136 * WARNING: in contrast to strstr(), this one does not stop at the first \0 in
hexley 0:281d6ff68967 1137 * the pbuf/source string!
hexley 0:281d6ff68967 1138 *
hexley 0:281d6ff68967 1139 * @param p pbuf to search, maximum length is 0xFFFE since 0xFFFF is used as
hexley 0:281d6ff68967 1140 * return value 'not found'
hexley 0:281d6ff68967 1141 * @param substr string to search for in p, maximum length is 0xFFFE
hexley 0:281d6ff68967 1142 * @return 0xFFFF if substr was not found in p or the index where it was found
hexley 0:281d6ff68967 1143 */
hexley 0:281d6ff68967 1144 u16_t
hexley 0:281d6ff68967 1145 pbuf_strstr(struct pbuf* p, const char* substr)
hexley 0:281d6ff68967 1146 {
hexley 0:281d6ff68967 1147 size_t substr_len;
hexley 0:281d6ff68967 1148 if ((substr == NULL) || (substr[0] == 0) || (p->tot_len == 0xFFFF)) {
hexley 0:281d6ff68967 1149 return 0xFFFF;
hexley 0:281d6ff68967 1150 }
hexley 0:281d6ff68967 1151 substr_len = strlen(substr);
hexley 0:281d6ff68967 1152 if (substr_len >= 0xFFFF) {
hexley 0:281d6ff68967 1153 return 0xFFFF;
hexley 0:281d6ff68967 1154 }
hexley 0:281d6ff68967 1155 return pbuf_memfind(p, substr, (u16_t)substr_len, 0);
hexley 0:281d6ff68967 1156 }