SAMV71 Xplained Ultra Software Package 1.3

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