Leest de waarde van een sensor binnen een maakt deze beschikbaar via internet

Dependencies:   NTPClient_NetServices mbed

Committer:
hendrikvincent
Date:
Mon Dec 02 09:01:23 2013 +0000
Revision:
0:05ccbd4f84f1
eerste programma;

Who changed what in which revision?

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