Donatien Garnier / LwIPNetworking

Dependencies:   DebugLib Socket lwip lwip-sys

Dependents:   EthernetInterface

Fork of NetworkingCoreLib by Donatien Garnier

Committer:
donatien
Date:
Thu May 24 14:49:40 2012 +0000
Revision:
0:013f5d54248c
Initial Commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
donatien 0:013f5d54248c 1 /**
donatien 0:013f5d54248c 2 * @file
donatien 0:013f5d54248c 3 * Network buffer management
donatien 0:013f5d54248c 4 *
donatien 0:013f5d54248c 5 */
donatien 0:013f5d54248c 6
donatien 0:013f5d54248c 7 /*
donatien 0:013f5d54248c 8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
donatien 0:013f5d54248c 9 * All rights reserved.
donatien 0:013f5d54248c 10 *
donatien 0:013f5d54248c 11 * Redistribution and use in source and binary forms, with or without modification,
donatien 0:013f5d54248c 12 * are permitted provided that the following conditions are met:
donatien 0:013f5d54248c 13 *
donatien 0:013f5d54248c 14 * 1. Redistributions of source code must retain the above copyright notice,
donatien 0:013f5d54248c 15 * this list of conditions and the following disclaimer.
donatien 0:013f5d54248c 16 * 2. Redistributions in binary form must reproduce the above copyright notice,
donatien 0:013f5d54248c 17 * this list of conditions and the following disclaimer in the documentation
donatien 0:013f5d54248c 18 * and/or other materials provided with the distribution.
donatien 0:013f5d54248c 19 * 3. The name of the author may not be used to endorse or promote products
donatien 0:013f5d54248c 20 * derived from this software without specific prior written permission.
donatien 0:013f5d54248c 21 *
donatien 0:013f5d54248c 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
donatien 0:013f5d54248c 23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
donatien 0:013f5d54248c 24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
donatien 0:013f5d54248c 25 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
donatien 0:013f5d54248c 26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
donatien 0:013f5d54248c 27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
donatien 0:013f5d54248c 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
donatien 0:013f5d54248c 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
donatien 0:013f5d54248c 30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
donatien 0:013f5d54248c 31 * OF SUCH DAMAGE.
donatien 0:013f5d54248c 32 *
donatien 0:013f5d54248c 33 * This file is part of the lwIP TCP/IP stack.
donatien 0:013f5d54248c 34 *
donatien 0:013f5d54248c 35 * Author: Adam Dunkels <adam@sics.se>
donatien 0:013f5d54248c 36 *
donatien 0:013f5d54248c 37 */
donatien 0:013f5d54248c 38
donatien 0:013f5d54248c 39 #include "lwip/opt.h"
donatien 0:013f5d54248c 40
donatien 0:013f5d54248c 41 #if LWIP_NETCONN /* don't build if not configured for use in lwipopts.h */
donatien 0:013f5d54248c 42
donatien 0:013f5d54248c 43 #include "lwip/netbuf.h"
donatien 0:013f5d54248c 44 #include "lwip/memp.h"
donatien 0:013f5d54248c 45
donatien 0:013f5d54248c 46 #include <string.h>
donatien 0:013f5d54248c 47
donatien 0:013f5d54248c 48 /**
donatien 0:013f5d54248c 49 * Create (allocate) and initialize a new netbuf.
donatien 0:013f5d54248c 50 * The netbuf doesn't yet contain a packet buffer!
donatien 0:013f5d54248c 51 *
donatien 0:013f5d54248c 52 * @return a pointer to a new netbuf
donatien 0:013f5d54248c 53 * NULL on lack of memory
donatien 0:013f5d54248c 54 */
donatien 0:013f5d54248c 55 struct
donatien 0:013f5d54248c 56 netbuf *netbuf_new(void)
donatien 0:013f5d54248c 57 {
donatien 0:013f5d54248c 58 struct netbuf *buf;
donatien 0:013f5d54248c 59
donatien 0:013f5d54248c 60 buf = (struct netbuf *)memp_malloc(MEMP_NETBUF);
donatien 0:013f5d54248c 61 if (buf != NULL) {
donatien 0:013f5d54248c 62 buf->p = NULL;
donatien 0:013f5d54248c 63 buf->ptr = NULL;
donatien 0:013f5d54248c 64 ip_addr_set_any(&buf->addr);
donatien 0:013f5d54248c 65 buf->port = 0;
donatien 0:013f5d54248c 66 #if LWIP_NETBUF_RECVINFO || LWIP_CHECKSUM_ON_COPY
donatien 0:013f5d54248c 67 #if LWIP_CHECKSUM_ON_COPY
donatien 0:013f5d54248c 68 buf->flags = 0;
donatien 0:013f5d54248c 69 #endif /* LWIP_CHECKSUM_ON_COPY */
donatien 0:013f5d54248c 70 buf->toport_chksum = 0;
donatien 0:013f5d54248c 71 #if LWIP_NETBUF_RECVINFO
donatien 0:013f5d54248c 72 ip_addr_set_any(&buf->toaddr);
donatien 0:013f5d54248c 73 #endif /* LWIP_NETBUF_RECVINFO */
donatien 0:013f5d54248c 74 #endif /* LWIP_NETBUF_RECVINFO || LWIP_CHECKSUM_ON_COPY */
donatien 0:013f5d54248c 75 return buf;
donatien 0:013f5d54248c 76 } else {
donatien 0:013f5d54248c 77 return NULL;
donatien 0:013f5d54248c 78 }
donatien 0:013f5d54248c 79 }
donatien 0:013f5d54248c 80
donatien 0:013f5d54248c 81 /**
donatien 0:013f5d54248c 82 * Deallocate a netbuf allocated by netbuf_new().
donatien 0:013f5d54248c 83 *
donatien 0:013f5d54248c 84 * @param buf pointer to a netbuf allocated by netbuf_new()
donatien 0:013f5d54248c 85 */
donatien 0:013f5d54248c 86 void
donatien 0:013f5d54248c 87 netbuf_delete(struct netbuf *buf)
donatien 0:013f5d54248c 88 {
donatien 0:013f5d54248c 89 if (buf != NULL) {
donatien 0:013f5d54248c 90 if (buf->p != NULL) {
donatien 0:013f5d54248c 91 pbuf_free(buf->p);
donatien 0:013f5d54248c 92 buf->p = buf->ptr = NULL;
donatien 0:013f5d54248c 93 }
donatien 0:013f5d54248c 94 memp_free(MEMP_NETBUF, buf);
donatien 0:013f5d54248c 95 }
donatien 0:013f5d54248c 96 }
donatien 0:013f5d54248c 97
donatien 0:013f5d54248c 98 /**
donatien 0:013f5d54248c 99 * Allocate memory for a packet buffer for a given netbuf.
donatien 0:013f5d54248c 100 *
donatien 0:013f5d54248c 101 * @param buf the netbuf for which to allocate a packet buffer
donatien 0:013f5d54248c 102 * @param size the size of the packet buffer to allocate
donatien 0:013f5d54248c 103 * @return pointer to the allocated memory
donatien 0:013f5d54248c 104 * NULL if no memory could be allocated
donatien 0:013f5d54248c 105 */
donatien 0:013f5d54248c 106 void *
donatien 0:013f5d54248c 107 netbuf_alloc(struct netbuf *buf, u16_t size)
donatien 0:013f5d54248c 108 {
donatien 0:013f5d54248c 109 LWIP_ERROR("netbuf_alloc: invalid buf", (buf != NULL), return NULL;);
donatien 0:013f5d54248c 110
donatien 0:013f5d54248c 111 /* Deallocate any previously allocated memory. */
donatien 0:013f5d54248c 112 if (buf->p != NULL) {
donatien 0:013f5d54248c 113 pbuf_free(buf->p);
donatien 0:013f5d54248c 114 }
donatien 0:013f5d54248c 115 buf->p = pbuf_alloc(PBUF_TRANSPORT, size, PBUF_RAM);
donatien 0:013f5d54248c 116 if (buf->p == NULL) {
donatien 0:013f5d54248c 117 return NULL;
donatien 0:013f5d54248c 118 }
donatien 0:013f5d54248c 119 LWIP_ASSERT("check that first pbuf can hold size",
donatien 0:013f5d54248c 120 (buf->p->len >= size));
donatien 0:013f5d54248c 121 buf->ptr = buf->p;
donatien 0:013f5d54248c 122 return buf->p->payload;
donatien 0:013f5d54248c 123 }
donatien 0:013f5d54248c 124
donatien 0:013f5d54248c 125 /**
donatien 0:013f5d54248c 126 * Free the packet buffer included in a netbuf
donatien 0:013f5d54248c 127 *
donatien 0:013f5d54248c 128 * @param buf pointer to the netbuf which contains the packet buffer to free
donatien 0:013f5d54248c 129 */
donatien 0:013f5d54248c 130 void
donatien 0:013f5d54248c 131 netbuf_free(struct netbuf *buf)
donatien 0:013f5d54248c 132 {
donatien 0:013f5d54248c 133 LWIP_ERROR("netbuf_free: invalid buf", (buf != NULL), return;);
donatien 0:013f5d54248c 134 if (buf->p != NULL) {
donatien 0:013f5d54248c 135 pbuf_free(buf->p);
donatien 0:013f5d54248c 136 }
donatien 0:013f5d54248c 137 buf->p = buf->ptr = NULL;
donatien 0:013f5d54248c 138 }
donatien 0:013f5d54248c 139
donatien 0:013f5d54248c 140 /**
donatien 0:013f5d54248c 141 * Let a netbuf reference existing (non-volatile) data.
donatien 0:013f5d54248c 142 *
donatien 0:013f5d54248c 143 * @param buf netbuf which should reference the data
donatien 0:013f5d54248c 144 * @param dataptr pointer to the data to reference
donatien 0:013f5d54248c 145 * @param size size of the data
donatien 0:013f5d54248c 146 * @return ERR_OK if data is referenced
donatien 0:013f5d54248c 147 * ERR_MEM if data couldn't be referenced due to lack of memory
donatien 0:013f5d54248c 148 */
donatien 0:013f5d54248c 149 err_t
donatien 0:013f5d54248c 150 netbuf_ref(struct netbuf *buf, const void *dataptr, u16_t size)
donatien 0:013f5d54248c 151 {
donatien 0:013f5d54248c 152 LWIP_ERROR("netbuf_ref: invalid buf", (buf != NULL), return ERR_ARG;);
donatien 0:013f5d54248c 153 if (buf->p != NULL) {
donatien 0:013f5d54248c 154 pbuf_free(buf->p);
donatien 0:013f5d54248c 155 }
donatien 0:013f5d54248c 156 buf->p = pbuf_alloc(PBUF_TRANSPORT, 0, PBUF_REF);
donatien 0:013f5d54248c 157 if (buf->p == NULL) {
donatien 0:013f5d54248c 158 buf->ptr = NULL;
donatien 0:013f5d54248c 159 return ERR_MEM;
donatien 0:013f5d54248c 160 }
donatien 0:013f5d54248c 161 buf->p->payload = (void*)dataptr;
donatien 0:013f5d54248c 162 buf->p->len = buf->p->tot_len = size;
donatien 0:013f5d54248c 163 buf->ptr = buf->p;
donatien 0:013f5d54248c 164 return ERR_OK;
donatien 0:013f5d54248c 165 }
donatien 0:013f5d54248c 166
donatien 0:013f5d54248c 167 /**
donatien 0:013f5d54248c 168 * Chain one netbuf to another (@see pbuf_chain)
donatien 0:013f5d54248c 169 *
donatien 0:013f5d54248c 170 * @param head the first netbuf
donatien 0:013f5d54248c 171 * @param tail netbuf to chain after head, freed by this function, may not be reference after returning
donatien 0:013f5d54248c 172 */
donatien 0:013f5d54248c 173 void
donatien 0:013f5d54248c 174 netbuf_chain(struct netbuf *head, struct netbuf *tail)
donatien 0:013f5d54248c 175 {
donatien 0:013f5d54248c 176 LWIP_ERROR("netbuf_ref: invalid head", (head != NULL), return;);
donatien 0:013f5d54248c 177 LWIP_ERROR("netbuf_chain: invalid tail", (tail != NULL), return;);
donatien 0:013f5d54248c 178 pbuf_cat(head->p, tail->p);
donatien 0:013f5d54248c 179 head->ptr = head->p;
donatien 0:013f5d54248c 180 memp_free(MEMP_NETBUF, tail);
donatien 0:013f5d54248c 181 }
donatien 0:013f5d54248c 182
donatien 0:013f5d54248c 183 /**
donatien 0:013f5d54248c 184 * Get the data pointer and length of the data inside a netbuf.
donatien 0:013f5d54248c 185 *
donatien 0:013f5d54248c 186 * @param buf netbuf to get the data from
donatien 0:013f5d54248c 187 * @param dataptr pointer to a void pointer where to store the data pointer
donatien 0:013f5d54248c 188 * @param len pointer to an u16_t where the length of the data is stored
donatien 0:013f5d54248c 189 * @return ERR_OK if the information was retreived,
donatien 0:013f5d54248c 190 * ERR_BUF on error.
donatien 0:013f5d54248c 191 */
donatien 0:013f5d54248c 192 err_t
donatien 0:013f5d54248c 193 netbuf_data(struct netbuf *buf, void **dataptr, u16_t *len)
donatien 0:013f5d54248c 194 {
donatien 0:013f5d54248c 195 LWIP_ERROR("netbuf_data: invalid buf", (buf != NULL), return ERR_ARG;);
donatien 0:013f5d54248c 196 LWIP_ERROR("netbuf_data: invalid dataptr", (dataptr != NULL), return ERR_ARG;);
donatien 0:013f5d54248c 197 LWIP_ERROR("netbuf_data: invalid len", (len != NULL), return ERR_ARG;);
donatien 0:013f5d54248c 198
donatien 0:013f5d54248c 199 if (buf->ptr == NULL) {
donatien 0:013f5d54248c 200 return ERR_BUF;
donatien 0:013f5d54248c 201 }
donatien 0:013f5d54248c 202 *dataptr = buf->ptr->payload;
donatien 0:013f5d54248c 203 *len = buf->ptr->len;
donatien 0:013f5d54248c 204 return ERR_OK;
donatien 0:013f5d54248c 205 }
donatien 0:013f5d54248c 206
donatien 0:013f5d54248c 207 /**
donatien 0:013f5d54248c 208 * Move the current data pointer of a packet buffer contained in a netbuf
donatien 0:013f5d54248c 209 * to the next part.
donatien 0:013f5d54248c 210 * The packet buffer itself is not modified.
donatien 0:013f5d54248c 211 *
donatien 0:013f5d54248c 212 * @param buf the netbuf to modify
donatien 0:013f5d54248c 213 * @return -1 if there is no next part
donatien 0:013f5d54248c 214 * 1 if moved to the next part but now there is no next part
donatien 0:013f5d54248c 215 * 0 if moved to the next part and there are still more parts
donatien 0:013f5d54248c 216 */
donatien 0:013f5d54248c 217 s8_t
donatien 0:013f5d54248c 218 netbuf_next(struct netbuf *buf)
donatien 0:013f5d54248c 219 {
donatien 0:013f5d54248c 220 LWIP_ERROR("netbuf_free: invalid buf", (buf != NULL), return -1;);
donatien 0:013f5d54248c 221 if (buf->ptr->next == NULL) {
donatien 0:013f5d54248c 222 return -1;
donatien 0:013f5d54248c 223 }
donatien 0:013f5d54248c 224 buf->ptr = buf->ptr->next;
donatien 0:013f5d54248c 225 if (buf->ptr->next == NULL) {
donatien 0:013f5d54248c 226 return 1;
donatien 0:013f5d54248c 227 }
donatien 0:013f5d54248c 228 return 0;
donatien 0:013f5d54248c 229 }
donatien 0:013f5d54248c 230
donatien 0:013f5d54248c 231 /**
donatien 0:013f5d54248c 232 * Move the current data pointer of a packet buffer contained in a netbuf
donatien 0:013f5d54248c 233 * to the beginning of the packet.
donatien 0:013f5d54248c 234 * The packet buffer itself is not modified.
donatien 0:013f5d54248c 235 *
donatien 0:013f5d54248c 236 * @param buf the netbuf to modify
donatien 0:013f5d54248c 237 */
donatien 0:013f5d54248c 238 void
donatien 0:013f5d54248c 239 netbuf_first(struct netbuf *buf)
donatien 0:013f5d54248c 240 {
donatien 0:013f5d54248c 241 LWIP_ERROR("netbuf_free: invalid buf", (buf != NULL), return;);
donatien 0:013f5d54248c 242 buf->ptr = buf->p;
donatien 0:013f5d54248c 243 }
donatien 0:013f5d54248c 244
donatien 0:013f5d54248c 245 #endif /* LWIP_NETCONN */