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