A version of LWIP, provided for backwards compatibility.

Dependents:   AA_DemoBoard DemoBoard HelloServerDemo DemoBoard_RangeIndicator ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers netbuf.c Source File

netbuf.c

Go to the documentation of this file.
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 #include "lwip/opt.h"
00040 
00041 #if LWIP_NETCONN /* don't build if not configured for use in lwipopts.h */
00042 
00043 #include "lwip/netbuf.h"
00044 #include "lwip/memp.h"
00045 
00046 #include <string.h>
00047 
00048 /**
00049  * Create (allocate) and initialize a new netbuf.
00050  * The netbuf doesn't yet contain a packet buffer!
00051  *
00052  * @return a pointer to a new netbuf
00053  *         NULL on lack of memory
00054  */
00055 struct
00056 netbuf *netbuf_new(void)
00057 {
00058   struct netbuf *buf;
00059 
00060   buf = memp_malloc(MEMP_NETBUF);
00061   if (buf != NULL) {
00062     buf->p = NULL;
00063     buf->ptr = NULL;
00064     buf->addr = NULL;
00065     return buf;
00066   } else {
00067     return NULL;
00068   }
00069 }
00070 
00071 /**
00072  * Deallocate a netbuf allocated by netbuf_new().
00073  *
00074  * @param buf pointer to a netbuf allocated by netbuf_new()
00075  */
00076 void
00077 netbuf_delete(struct netbuf *buf)
00078 {
00079   if (buf != NULL) {
00080     if (buf->p != NULL) {
00081       pbuf_free(buf->p);
00082       buf->p = buf->ptr = NULL;
00083     }
00084     memp_free(MEMP_NETBUF, buf);
00085   }
00086 }
00087 
00088 /**
00089  * Allocate memory for a packet buffer for a given netbuf.
00090  *
00091  * @param buf the netbuf for which to allocate a packet buffer
00092  * @param size the size of the packet buffer to allocate
00093  * @return pointer to the allocated memory
00094  *         NULL if no memory could be allocated
00095  */
00096 void *
00097 netbuf_alloc(struct netbuf *buf, u16_t size)
00098 {
00099   LWIP_ERROR("netbuf_alloc: invalid buf", (buf != NULL), return NULL;);
00100 
00101   /* Deallocate any previously allocated memory. */
00102   if (buf->p != NULL) {
00103     pbuf_free(buf->p);
00104   }
00105   buf->p = pbuf_alloc(PBUF_TRANSPORT, size, PBUF_RAM);
00106   if (buf->p == NULL) {
00107      return NULL;
00108   }
00109   LWIP_ASSERT("check that first pbuf can hold size",
00110              (buf->p->len >= size));
00111   buf->ptr = buf->p;
00112   return buf->p->payload;
00113 }
00114 
00115 /**
00116  * Free the packet buffer included in a netbuf
00117  *
00118  * @param buf pointer to the netbuf which contains the packet buffer to free
00119  */
00120 void
00121 netbuf_free(struct netbuf *buf)
00122 {
00123   LWIP_ERROR("netbuf_free: invalid buf", (buf != NULL), return;);
00124   if (buf->p != NULL) {
00125     pbuf_free(buf->p);
00126   }
00127   buf->p = buf->ptr = NULL;
00128 }
00129 
00130 /**
00131  * Let a netbuf reference existing (non-volatile) data.
00132  *
00133  * @param buf netbuf which should reference the data
00134  * @param dataptr pointer to the data to reference
00135  * @param size size of the data
00136  * @return ERR_OK if data is referenced
00137  *         ERR_MEM if data couldn't be referenced due to lack of memory
00138  */
00139 err_t
00140 netbuf_ref(struct netbuf *buf, const void *dataptr, u16_t size)
00141 {
00142   LWIP_ERROR("netbuf_ref: invalid buf", (buf != NULL), return ERR_ARG;);
00143   if (buf->p != NULL) {
00144     pbuf_free(buf->p);
00145   }
00146   buf->p = pbuf_alloc(PBUF_TRANSPORT, 0, PBUF_REF);
00147   if (buf->p == NULL) {
00148     buf->ptr = NULL;
00149     return ERR_MEM;
00150   }
00151   buf->p->payload = (void*)dataptr;
00152   buf->p->len = buf->p->tot_len = size;
00153   buf->ptr = buf->p;
00154   return ERR_OK;
00155 }
00156 
00157 /**
00158  * Chain one netbuf to another (@see pbuf_chain)
00159  *
00160  * @param head the first netbuf
00161  * @param tail netbuf to chain after head
00162  */
00163 void
00164 netbuf_chain(struct netbuf *head, struct netbuf *tail)
00165 {
00166   LWIP_ERROR("netbuf_ref: invalid head", (head != NULL), return;);
00167   LWIP_ERROR("netbuf_chain: invalid tail", (tail != NULL), return;);
00168   pbuf_chain(head->p, tail->p);
00169   head->ptr = head->p;
00170   memp_free(MEMP_NETBUF, tail);
00171 }
00172 
00173 /**
00174  * Get the data pointer and length of the data inside a netbuf.
00175  *
00176  * @param buf netbuf to get the data from
00177  * @param dataptr pointer to a void pointer where to store the data pointer
00178  * @param len pointer to an u16_t where the length of the data is stored
00179  * @return ERR_OK if the information was retreived,
00180  *         ERR_BUF on error.
00181  */
00182 err_t
00183 netbuf_data(struct netbuf *buf, void **dataptr, u16_t *len)
00184 {
00185   LWIP_ERROR("netbuf_data: invalid buf", (buf != NULL), return ERR_ARG;);
00186   LWIP_ERROR("netbuf_data: invalid dataptr", (dataptr != NULL), return ERR_ARG;);
00187   LWIP_ERROR("netbuf_data: invalid len", (len != NULL), return ERR_ARG;);
00188 
00189   if (buf->ptr == NULL) {
00190     return ERR_BUF;
00191   }
00192   *dataptr = buf->ptr->payload;
00193   *len = buf->ptr->len;
00194   return ERR_OK;
00195 }
00196 
00197 /**
00198  * Move the current data pointer of a packet buffer contained in a netbuf
00199  * to the next part.
00200  * The packet buffer itself is not modified.
00201  *
00202  * @param buf the netbuf to modify
00203  * @return -1 if there is no next part
00204  *         1  if moved to the next part but now there is no next part
00205  *         0  if moved to the next part and there are still more parts
00206  */
00207 s8_t
00208 netbuf_next(struct netbuf *buf)
00209 {
00210   LWIP_ERROR("netbuf_free: invalid buf", (buf != NULL), return -1;);
00211   if (buf->ptr->next == NULL) {
00212     return -1;
00213   }
00214   buf->ptr = buf->ptr->next;
00215   if (buf->ptr->next == NULL) {
00216     return 1;
00217   }
00218   return 0;
00219 }
00220 
00221 /**
00222  * Move the current data pointer of a packet buffer contained in a netbuf
00223  * to the beginning of the packet.
00224  * The packet buffer itself is not modified.
00225  *
00226  * @param buf the netbuf to modify
00227  */
00228 void
00229 netbuf_first(struct netbuf *buf)
00230 {
00231   LWIP_ERROR("netbuf_free: invalid buf", (buf != NULL), return;);
00232   buf->ptr = buf->p;
00233 }
00234 
00235 #endif /* LWIP_NETCONN */