Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
lwip_netbuf.c
00001 /** 00002 * @file 00003 * Network buffer management 00004 * 00005 */ 00006 00007 /* 00008 * Copyright (c) 2001-2004 Swedish Institute of Computer Science. 00009 * All rights reserved. 00010 * 00011 * Redistribution and use in source and binary forms, with or without modification, 00012 * are permitted provided that the following conditions are met: 00013 * 00014 * 1. Redistributions of source code must retain the above copyright notice, 00015 * this list of conditions and the following disclaimer. 00016 * 2. Redistributions in binary form must reproduce the above copyright notice, 00017 * this list of conditions and the following disclaimer in the documentation 00018 * and/or other materials provided with the distribution. 00019 * 3. The name of the author may not be used to endorse or promote products 00020 * derived from this software without specific prior written permission. 00021 * 00022 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 00023 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00024 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 00025 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00026 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 00027 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00028 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00029 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 00030 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 00031 * OF SUCH DAMAGE. 00032 * 00033 * This file is part of the lwIP TCP/IP stack. 00034 * 00035 * Author: Adam Dunkels <adam@sics.se> 00036 * 00037 */ 00038 00039 /** 00040 * @defgroup netbuf Network buffers 00041 * @ingroup netconn 00042 * Network buffer descriptor for @ref netconn. Based on @ref pbuf internally 00043 * to avoid copying data around.\n 00044 * Buffers must not be shared accross multiple threads, all functions except 00045 * netbuf_new() and netbuf_delete() are not thread-safe. 00046 */ 00047 00048 #include "lwip/opt.h" 00049 00050 #if LWIP_NETCONN /* don't build if not configured for use in lwipopts.h */ 00051 00052 #include "lwip/netbuf.h" 00053 #include "lwip/memp.h" 00054 00055 #include <string.h> 00056 00057 /** 00058 * @ingroup netbuf 00059 * Create (allocate) and initialize a new netbuf. 00060 * The netbuf doesn't yet contain a packet buffer! 00061 * 00062 * @return a pointer to a new netbuf 00063 * NULL on lack of memory 00064 */ 00065 struct 00066 netbuf *netbuf_new(void) 00067 { 00068 struct netbuf *buf; 00069 00070 buf = (struct netbuf *)memp_malloc(MEMP_NETBUF); 00071 if (buf != NULL) { 00072 buf->p = NULL; 00073 buf->ptr = NULL; 00074 ip_addr_set_zero(&buf->addr); 00075 buf->port = 0; 00076 #if LWIP_NETBUF_RECVINFO || LWIP_CHECKSUM_ON_COPY 00077 #if LWIP_CHECKSUM_ON_COPY 00078 buf->flags = 0; 00079 #endif /* LWIP_CHECKSUM_ON_COPY */ 00080 buf->toport_chksum = 0; 00081 #if LWIP_NETBUF_RECVINFO 00082 ip_addr_set_zero(&buf->toaddr); 00083 #endif /* LWIP_NETBUF_RECVINFO */ 00084 #endif /* LWIP_NETBUF_RECVINFO || LWIP_CHECKSUM_ON_COPY */ 00085 return buf; 00086 } else { 00087 return NULL; 00088 } 00089 } 00090 00091 /** 00092 * @ingroup netbuf 00093 * Deallocate a netbuf allocated by netbuf_new(). 00094 * 00095 * @param buf pointer to a netbuf allocated by netbuf_new() 00096 */ 00097 void 00098 netbuf_delete(struct netbuf *buf) 00099 { 00100 if (buf != NULL) { 00101 if (buf->p != NULL) { 00102 pbuf_free(buf->p); 00103 buf->p = buf->ptr = NULL; 00104 } 00105 memp_free(MEMP_NETBUF, buf); 00106 } 00107 } 00108 00109 /** 00110 * @ingroup netbuf 00111 * Allocate memory for a packet buffer for a given netbuf. 00112 * 00113 * @param buf the netbuf for which to allocate a packet buffer 00114 * @param size the size of the packet buffer to allocate 00115 * @return pointer to the allocated memory 00116 * NULL if no memory could be allocated 00117 */ 00118 void * 00119 netbuf_alloc(struct netbuf *buf, u16_t size) 00120 { 00121 LWIP_ERROR("netbuf_alloc: invalid buf", (buf != NULL), return NULL;); 00122 00123 /* Deallocate any previously allocated memory. */ 00124 if (buf->p != NULL) { 00125 pbuf_free(buf->p); 00126 } 00127 buf->p = pbuf_alloc(PBUF_TRANSPORT, size, PBUF_RAM); 00128 if (buf->p == NULL) { 00129 return NULL; 00130 } 00131 LWIP_ASSERT("check that first pbuf can hold size", 00132 (buf->p->len >= size)); 00133 buf->ptr = buf->p; 00134 return buf->p->payload; 00135 } 00136 00137 /** 00138 * @ingroup netbuf 00139 * Free the packet buffer included in a netbuf 00140 * 00141 * @param buf pointer to the netbuf which contains the packet buffer to free 00142 */ 00143 void 00144 netbuf_free(struct netbuf *buf) 00145 { 00146 LWIP_ERROR("netbuf_free: invalid buf", (buf != NULL), return;); 00147 if (buf->p != NULL) { 00148 pbuf_free(buf->p); 00149 } 00150 buf->p = buf->ptr = NULL; 00151 } 00152 00153 /** 00154 * @ingroup netbuf 00155 * Let a netbuf reference existing (non-volatile) data. 00156 * 00157 * @param buf netbuf which should reference the data 00158 * @param dataptr pointer to the data to reference 00159 * @param size size of the data 00160 * @return ERR_OK if data is referenced 00161 * ERR_MEM if data couldn't be referenced due to lack of memory 00162 */ 00163 err_t 00164 netbuf_ref(struct netbuf *buf, const void *dataptr, u16_t size) 00165 { 00166 LWIP_ERROR("netbuf_ref: invalid buf", (buf != NULL), return ERR_ARG;); 00167 if (buf->p != NULL) { 00168 pbuf_free(buf->p); 00169 } 00170 buf->p = pbuf_alloc(PBUF_TRANSPORT, 0, PBUF_REF); 00171 if (buf->p == NULL) { 00172 buf->ptr = NULL; 00173 return ERR_MEM; 00174 } 00175 ((struct pbuf_rom*)buf->p)->payload = dataptr; 00176 buf->p->len = buf->p->tot_len = size; 00177 buf->ptr = buf->p; 00178 return ERR_OK; 00179 } 00180 00181 /** 00182 * @ingroup netbuf 00183 * Chain one netbuf to another (@see pbuf_chain) 00184 * 00185 * @param head the first netbuf 00186 * @param tail netbuf to chain after head, freed by this function, may not be reference after returning 00187 */ 00188 void 00189 netbuf_chain(struct netbuf *head, struct netbuf *tail) 00190 { 00191 LWIP_ERROR("netbuf_ref: invalid head", (head != NULL), return;); 00192 LWIP_ERROR("netbuf_chain: invalid tail", (tail != NULL), return;); 00193 pbuf_cat(head->p, tail->p); 00194 head->ptr = head->p; 00195 memp_free(MEMP_NETBUF, tail); 00196 } 00197 00198 /** 00199 * @ingroup netbuf 00200 * Get the data pointer and length of the data inside a netbuf. 00201 * 00202 * @param buf netbuf to get the data from 00203 * @param dataptr pointer to a void pointer where to store the data pointer 00204 * @param len pointer to an u16_t where the length of the data is stored 00205 * @return ERR_OK if the information was retrieved, 00206 * ERR_BUF on error. 00207 */ 00208 err_t 00209 netbuf_data(struct netbuf *buf, void **dataptr, u16_t *len) 00210 { 00211 LWIP_ERROR("netbuf_data: invalid buf", (buf != NULL), return ERR_ARG;); 00212 LWIP_ERROR("netbuf_data: invalid dataptr", (dataptr != NULL), return ERR_ARG;); 00213 LWIP_ERROR("netbuf_data: invalid len", (len != NULL), return ERR_ARG;); 00214 00215 if (buf->ptr == NULL) { 00216 return ERR_BUF; 00217 } 00218 *dataptr = buf->ptr->payload; 00219 *len = buf->ptr->len; 00220 return ERR_OK; 00221 } 00222 00223 /** 00224 * @ingroup netbuf 00225 * Move the current data pointer of a packet buffer contained in a netbuf 00226 * to the next part. 00227 * The packet buffer itself is not modified. 00228 * 00229 * @param buf the netbuf to modify 00230 * @return -1 if there is no next part 00231 * 1 if moved to the next part but now there is no next part 00232 * 0 if moved to the next part and there are still more parts 00233 */ 00234 s8_t 00235 netbuf_next(struct netbuf *buf) 00236 { 00237 LWIP_ERROR("netbuf_free: invalid buf", (buf != NULL), return -1;); 00238 if (buf->ptr->next == NULL) { 00239 return -1; 00240 } 00241 buf->ptr = buf->ptr->next; 00242 if (buf->ptr->next == NULL) { 00243 return 1; 00244 } 00245 return 0; 00246 } 00247 00248 /** 00249 * @ingroup netbuf 00250 * Move the current data pointer of a packet buffer contained in a netbuf 00251 * to the beginning of the packet. 00252 * The packet buffer itself is not modified. 00253 * 00254 * @param buf the netbuf to modify 00255 */ 00256 void 00257 netbuf_first(struct netbuf *buf) 00258 { 00259 LWIP_ERROR("netbuf_free: invalid buf", (buf != NULL), return;); 00260 buf->ptr = buf->p; 00261 } 00262 00263 #endif /* LWIP_NETCONN */
Generated on Tue Jul 12 2022 17:34:46 by
