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