Stripped down version of Segundos NetService library (http://mbed.org/users/segundo/libraries/NetServices ). I have removed all NetServices, and all functions which had been disabled. Use this version when you need only pure TCP or UDP functions - this library compiles faster.

Dependencies:   lwip lwip-sys

Dependents:   christmasLights device_server pop3demo device_server_udp ... more

Committer:
hlipka
Date:
Mon Jan 10 21:03:11 2011 +0000
Revision:
0:8b387bed54c2
initial version

Who changed what in which revision?

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