Netservices modded to read fragmented HTTP respsonse/payload from special purpose server - 180 bytes only

Committer:
RodColeman
Date:
Thu Sep 08 10:41:36 2011 +0000
Revision:
0:8f5825f330b0
setDataLen hacked to 180bytes

Who changed what in which revision?

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