|
||||
Warning, cross-references for /kernel/lwip/api/netbuf.c need to be fixed.
0001 /** 0002 * @file 0003 * Network buffer management 0004 * 0005 */ 0006 0007 /* 0008 * Copyright (c) 2001-2004 Swedish Institute of Computer Science. 0009 * All rights reserved. 0010 * 0011 * Redistribution and use in source and binary forms, with or without modification, 0012 * are permitted provided that the following conditions are met: 0013 * 0014 * 1. Redistributions of source code must retain the above copyright notice, 0015 * this list of conditions and the following disclaimer. 0016 * 2. Redistributions in binary form must reproduce the above copyright notice, 0017 * this list of conditions and the following disclaimer in the documentation 0018 * and/or other materials provided with the distribution. 0019 * 3. The name of the author may not be used to endorse or promote products 0020 * derived from this software without specific prior written permission. 0021 * 0022 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 0023 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 0024 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 0025 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 0026 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 0027 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 0028 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 0029 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 0030 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 0031 * OF SUCH DAMAGE. 0032 * 0033 * This file is part of the lwIP TCP/IP stack. 0034 * 0035 * Author: Adam Dunkels <adam@sics.se> 0036 * 0037 */ 0038 0039 #include "lwip/opt.h" 0040 0041 #if LWIP_NETCONN /* don't build if not configured for use in lwipopts.h */ 0042 0043 #include "lwip/netbuf.h" 0044 #include "lwip/memp.h" 0045 0046 #include <string.h> 0047 0048 /** 0049 * Create (allocate) and initialize a new netbuf. 0050 * The netbuf doesn't yet contain a packet buffer! 0051 * 0052 * @return a pointer to a new netbuf 0053 * NULL on lack of memory 0054 */ 0055 struct 0056 netbuf *netbuf_new(void) 0057 { 0058 struct netbuf *buf; 0059 0060 buf = memp_malloc(MEMP_NETBUF); 0061 if (buf != NULL) { 0062 buf->p = NULL; 0063 buf->ptr = NULL; 0064 buf->addr = NULL; 0065 buf->port = 0; 0066 #if LWIP_NETBUF_RECVINFO 0067 buf->toaddr = NULL; 0068 buf->toport = 0; 0069 #endif /* LWIP_NETBUF_RECVINFO */ 0070 return buf; 0071 } else { 0072 return NULL; 0073 } 0074 } 0075 0076 /** 0077 * Deallocate a netbuf allocated by netbuf_new(). 0078 * 0079 * @param buf pointer to a netbuf allocated by netbuf_new() 0080 */ 0081 void 0082 netbuf_delete(struct netbuf *buf) 0083 { 0084 if (buf != NULL) { 0085 if (buf->p != NULL) { 0086 pbuf_free(buf->p); 0087 buf->p = buf->ptr = NULL; 0088 } 0089 memp_free(MEMP_NETBUF, buf); 0090 } 0091 } 0092 0093 /** 0094 * Allocate memory for a packet buffer for a given netbuf. 0095 * 0096 * @param buf the netbuf for which to allocate a packet buffer 0097 * @param size the size of the packet buffer to allocate 0098 * @return pointer to the allocated memory 0099 * NULL if no memory could be allocated 0100 */ 0101 void * 0102 netbuf_alloc(struct netbuf *buf, u16_t size) 0103 { 0104 LWIP_ERROR("netbuf_alloc: invalid buf", (buf != NULL), return NULL;); 0105 0106 /* Deallocate any previously allocated memory. */ 0107 if (buf->p != NULL) { 0108 pbuf_free(buf->p); 0109 } 0110 buf->p = pbuf_alloc(PBUF_TRANSPORT, size, PBUF_RAM); 0111 if (buf->p == NULL) { 0112 return NULL; 0113 } 0114 LWIP_ASSERT("check that first pbuf can hold size", 0115 (buf->p->len >= size)); 0116 buf->ptr = buf->p; 0117 return buf->p->payload; 0118 } 0119 0120 /** 0121 * Free the packet buffer included in a netbuf 0122 * 0123 * @param buf pointer to the netbuf which contains the packet buffer to free 0124 */ 0125 void 0126 netbuf_free(struct netbuf *buf) 0127 { 0128 LWIP_ERROR("netbuf_free: invalid buf", (buf != NULL), return;); 0129 if (buf->p != NULL) { 0130 pbuf_free(buf->p); 0131 } 0132 buf->p = buf->ptr = NULL; 0133 } 0134 0135 /** 0136 * Let a netbuf reference existing (non-volatile) data. 0137 * 0138 * @param buf netbuf which should reference the data 0139 * @param dataptr pointer to the data to reference 0140 * @param size size of the data 0141 * @return ERR_OK if data is referenced 0142 * ERR_MEM if data couldn't be referenced due to lack of memory 0143 */ 0144 err_t 0145 netbuf_ref(struct netbuf *buf, const void *dataptr, u16_t size) 0146 { 0147 LWIP_ERROR("netbuf_ref: invalid buf", (buf != NULL), return ERR_ARG;); 0148 if (buf->p != NULL) { 0149 pbuf_free(buf->p); 0150 } 0151 buf->p = pbuf_alloc(PBUF_TRANSPORT, 0, PBUF_REF); 0152 if (buf->p == NULL) { 0153 buf->ptr = NULL; 0154 return ERR_MEM; 0155 } 0156 buf->p->payload = (void*)dataptr; 0157 buf->p->len = buf->p->tot_len = size; 0158 buf->ptr = buf->p; 0159 return ERR_OK; 0160 } 0161 0162 /** 0163 * Chain one netbuf to another (@see pbuf_chain) 0164 * 0165 * @param head the first netbuf 0166 * @param tail netbuf to chain after head, freed by this function, may not be reference after returning 0167 */ 0168 void 0169 netbuf_chain(struct netbuf *head, struct netbuf *tail) 0170 { 0171 LWIP_ERROR("netbuf_ref: invalid head", (head != NULL), return;); 0172 LWIP_ERROR("netbuf_chain: invalid tail", (tail != NULL), return;); 0173 pbuf_cat(head->p, tail->p); 0174 head->ptr = head->p; 0175 memp_free(MEMP_NETBUF, tail); 0176 } 0177 0178 /** 0179 * Get the data pointer and length of the data inside a netbuf. 0180 * 0181 * @param buf netbuf to get the data from 0182 * @param dataptr pointer to a void pointer where to store the data pointer 0183 * @param len pointer to an u16_t where the length of the data is stored 0184 * @return ERR_OK if the information was retreived, 0185 * ERR_BUF on error. 0186 */ 0187 err_t 0188 netbuf_data(struct netbuf *buf, void **dataptr, u16_t *len) 0189 { 0190 LWIP_ERROR("netbuf_data: invalid buf", (buf != NULL), return ERR_ARG;); 0191 LWIP_ERROR("netbuf_data: invalid dataptr", (dataptr != NULL), return ERR_ARG;); 0192 LWIP_ERROR("netbuf_data: invalid len", (len != NULL), return ERR_ARG;); 0193 0194 if (buf->ptr == NULL) { 0195 return ERR_BUF; 0196 } 0197 *dataptr = buf->ptr->payload; 0198 *len = buf->ptr->len; 0199 return ERR_OK; 0200 } 0201 0202 /** 0203 * Move the current data pointer of a packet buffer contained in a netbuf 0204 * to the next part. 0205 * The packet buffer itself is not modified. 0206 * 0207 * @param buf the netbuf to modify 0208 * @return -1 if there is no next part 0209 * 1 if moved to the next part but now there is no next part 0210 * 0 if moved to the next part and there are still more parts 0211 */ 0212 s8_t 0213 netbuf_next(struct netbuf *buf) 0214 { 0215 LWIP_ERROR("netbuf_free: invalid buf", (buf != NULL), return -1;); 0216 if (buf->ptr->next == NULL) { 0217 return -1; 0218 } 0219 buf->ptr = buf->ptr->next; 0220 if (buf->ptr->next == NULL) { 0221 return 1; 0222 } 0223 return 0; 0224 } 0225 0226 /** 0227 * Move the current data pointer of a packet buffer contained in a netbuf 0228 * to the beginning of the packet. 0229 * The packet buffer itself is not modified. 0230 * 0231 * @param buf the netbuf to modify 0232 */ 0233 void 0234 netbuf_first(struct netbuf *buf) 0235 { 0236 LWIP_ERROR("netbuf_free: invalid buf", (buf != NULL), return;); 0237 buf->ptr = buf->p; 0238 } 0239 0240 #endif /* LWIP_NETCONN */
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 1.2.0 LXR engine. |