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