ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

netbuf.c
Go to the documentation of this file.
00001 
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 
00055 struct
00056 netbuf *netbuf_new(void)
00057 {
00058   struct netbuf *buf;
00059 
00060   buf = (struct netbuf *)memp_malloc(MEMP_NETBUF);
00061   if (buf != NULL) {
00062     buf->p = NULL;
00063     buf->ptr = NULL;
00064     ip_addr_set_any(&buf->addr);
00065     buf->port = 0;
00066 #if LWIP_NETBUF_RECVINFO || LWIP_CHECKSUM_ON_COPY
00067 #if LWIP_CHECKSUM_ON_COPY
00068     buf->flags = 0;
00069 #endif /* LWIP_CHECKSUM_ON_COPY */
00070     buf->toport_chksum = 0;
00071 #if LWIP_NETBUF_RECVINFO
00072     ip_addr_set_any(&buf->toaddr);
00073 #endif /* LWIP_NETBUF_RECVINFO */
00074 #endif /* LWIP_NETBUF_RECVINFO || LWIP_CHECKSUM_ON_COPY */
00075     return buf;
00076   } else {
00077     return NULL;
00078   }
00079 }
00080 
00086 void
00087 netbuf_delete(struct netbuf *buf)
00088 {
00089   if (buf != NULL) {
00090     if (buf->p != NULL) {
00091       pbuf_free(buf->p);
00092       buf->p = buf->ptr = NULL;
00093     }
00094     memp_free(MEMP_NETBUF, buf);
00095   }
00096 }
00097 
00106 void *
00107 netbuf_alloc(struct netbuf *buf, u16_t size)
00108 {
00109   LWIP_ERROR("netbuf_alloc: invalid buf", (buf != NULL), return NULL;);
00110 
00111   /* Deallocate any previously allocated memory. */
00112   if (buf->p != NULL) {
00113     pbuf_free(buf->p);
00114   }
00115   buf->p = pbuf_alloc(PBUF_TRANSPORT, size, PBUF_RAM);
00116   if (buf->p == NULL) {
00117      return NULL;
00118   }
00119   LWIP_ASSERT("check that first pbuf can hold size",
00120              (buf->p->len >= size));
00121   buf->ptr = buf->p;
00122   return buf->p->payload;
00123 }
00124 
00130 void
00131 netbuf_free(struct netbuf *buf)
00132 {
00133   LWIP_ERROR("netbuf_free: invalid buf", (buf != NULL), return;);
00134   if (buf->p != NULL) {
00135     pbuf_free(buf->p);
00136   }
00137   buf->p = buf->ptr = NULL;
00138 }
00139 
00149 err_t
00150 netbuf_ref(struct netbuf *buf, const void *dataptr, u16_t size)
00151 {
00152   LWIP_ERROR("netbuf_ref: invalid buf", (buf != NULL), return ERR_ARG;);
00153   if (buf->p != NULL) {
00154     pbuf_free(buf->p);
00155   }
00156   buf->p = pbuf_alloc(PBUF_TRANSPORT, 0, PBUF_REF);
00157   if (buf->p == NULL) {
00158     buf->ptr = NULL;
00159     return ERR_MEM;
00160   }
00161   buf->p->payload = (void*)dataptr;
00162   buf->p->len = buf->p->tot_len = size;
00163   buf->ptr = buf->p;
00164   return ERR_OK;
00165 }
00166 
00173 void
00174 netbuf_chain(struct netbuf *head, struct netbuf *tail)
00175 {
00176   LWIP_ERROR("netbuf_ref: invalid head", (head != NULL), return;);
00177   LWIP_ERROR("netbuf_chain: invalid tail", (tail != NULL), return;);
00178   pbuf_cat(head->p, tail->p);
00179   head->ptr = head->p;
00180   memp_free(MEMP_NETBUF, tail);
00181 }
00182 
00192 err_t
00193 netbuf_data(struct netbuf *buf, void **dataptr, u16_t *len)
00194 {
00195   LWIP_ERROR("netbuf_data: invalid buf", (buf != NULL), return ERR_ARG;);
00196   LWIP_ERROR("netbuf_data: invalid dataptr", (dataptr != NULL), return ERR_ARG;);
00197   LWIP_ERROR("netbuf_data: invalid len", (len != NULL), return ERR_ARG;);
00198 
00199   if (buf->ptr == NULL) {
00200     return ERR_BUF;
00201   }
00202   *dataptr = buf->ptr->payload;
00203   *len = buf->ptr->len;
00204   return ERR_OK;
00205 }
00206 
00217 s8_t
00218 netbuf_next(struct netbuf *buf)
00219 {
00220   LWIP_ERROR("netbuf_free: invalid buf", (buf != NULL), return -1;);
00221   if (buf->ptr->next == NULL) {
00222     return -1;
00223   }
00224   buf->ptr = buf->ptr->next;
00225   if (buf->ptr->next == NULL) {
00226     return 1;
00227   }
00228   return 0;
00229 }
00230 
00238 void
00239 netbuf_first(struct netbuf *buf)
00240 {
00241   LWIP_ERROR("netbuf_free: invalid buf", (buf != NULL), return;);
00242   buf->ptr = buf->p;
00243 }
00244 
00245 #endif /* LWIP_NETCONN */

Generated on Sun May 27 2012 04:36:00 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.