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