创建mbed

Dependencies:   EthernetInterface SDFileSystem mbed-rtos mbed

Committer:
sunyiming
Date:
Tue Mar 06 08:53:46 2018 +0000
Revision:
1:6465a3f5c58a
??OK

Who changed what in which revision?

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