This library is stripped down version of NetServices library. HTTP server and client function is NOT supported.

Dependents:   imu-daq-eth

Committer:
idinor
Date:
Wed Jul 20 11:45:39 2011 +0000
Revision:
0:dcf3c92487ca

        

Who changed what in which revision?

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