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