LwIP with PPP & Ethernet integration

Dependents:   NetworkingCoreLib

This is the mbed port of the LwIP stack: http://savannah.nongnu.org/projects/lwip/

It includes contributed content from NXP's port for LPCxxxx devices: http://www.lpcware.com/content/project/lightweight-ip-lwip-networking-stack

Licence

LwIP is licenced under the BSD licence:

Copyright (c) 2001-2004 Swedish Institute of Computer Science. 
All rights reserved. 
Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met: 
1. Redistributions of source code must retain the above copyright notice, 
this list of conditions and the following disclaimer. 
2. Redistributions in binary form must reproduce the above copyright notice, 
this list of conditions and the following disclaimer in the documentation 
and/or other materials provided with the distribution. 
3. The name of the author may not be used to endorse or promote products 
derived from this software without specific prior written permission. 
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
OF SUCH DAMAGE.
Committer:
donatien
Date:
Fri May 25 08:56:35 2012 +0000
Revision:
2:1a87f74b8e3b
Parent:
0:8e01dca41002
Removed compilation of EMAC driver when using PPP

Who changed what in which revision?

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