Philippe Bazot / Mbed 2 deprecated DU4SmartCities

Dependencies:   C12832 EthernetInterface LM75B MMA7660 MQTT mbed-rtos mbed

Fork of IBMIoTClientEthernetExample by IBM Watson IoT

Committer:
icraggs
Date:
Wed Oct 01 13:27:35 2014 +0000
Revision:
8:80d49dd91542
Parent:
6:37b6d0d56190
Remove conditional compilation for IBM IoT settings

Who changed what in which revision?

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