Ethernet test for ECE 4180 and others to find your IP address and do a simple HTTP GET request over port 80.

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Committer:
mkersh3
Date:
Thu Apr 04 05:26:09 2013 +0000
Revision:
0:e7ca326e76ee
Ethernet Test for ECE4180 and others to find their IP Address and do a simple HTTP GET request over port 80.

Who changed what in which revision?

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