Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

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