HTTPClient using static IP

Dependencies:   mbed

Committer:
mr_q
Date:
Mon May 30 11:53:37 2011 +0000
Revision:
0:d8f2f7d5f31b
v0.01 Draft

Who changed what in which revision?

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