Official mbed lwIP library (version 1.4.0)

Dependents:   LwIPNetworking NetServicesMin EthernetInterface EthernetInterface_RSF ... more

Legacy Networking Libraries

This is an mbed 2 networking library. For mbed OS 5, lwip has been integrated with built-in networking interfaces. The networking libraries have been revised to better support additional network stacks and thread safety here.

This library is based on the code of lwIP v1.4.0

Copyright (c) 2001, 2002 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:
mbed_official
Date:
Fri Jun 22 09:25:39 2012 +0000
Revision:
0:51ac1d130fd4
Initial import from lwip-1.4.0: http://download.savannah.gnu.org/releases/lwip/lwip-1.4.0.zip

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:51ac1d130fd4 1 /**
mbed_official 0:51ac1d130fd4 2 * @file
mbed_official 0:51ac1d130fd4 3 * Functions common to all TCP/IPv6 modules, such as the Internet checksum and the
mbed_official 0:51ac1d130fd4 4 * byte order functions.
mbed_official 0:51ac1d130fd4 5 *
mbed_official 0:51ac1d130fd4 6 */
mbed_official 0:51ac1d130fd4 7
mbed_official 0:51ac1d130fd4 8 /*
mbed_official 0:51ac1d130fd4 9 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
mbed_official 0:51ac1d130fd4 10 * All rights reserved.
mbed_official 0:51ac1d130fd4 11 *
mbed_official 0:51ac1d130fd4 12 * Redistribution and use in source and binary forms, with or without modification,
mbed_official 0:51ac1d130fd4 13 * are permitted provided that the following conditions are met:
mbed_official 0:51ac1d130fd4 14 *
mbed_official 0:51ac1d130fd4 15 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 0:51ac1d130fd4 16 * this list of conditions and the following disclaimer.
mbed_official 0:51ac1d130fd4 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 0:51ac1d130fd4 18 * this list of conditions and the following disclaimer in the documentation
mbed_official 0:51ac1d130fd4 19 * and/or other materials provided with the distribution.
mbed_official 0:51ac1d130fd4 20 * 3. The name of the author may not be used to endorse or promote products
mbed_official 0:51ac1d130fd4 21 * derived from this software without specific prior written permission.
mbed_official 0:51ac1d130fd4 22 *
mbed_official 0:51ac1d130fd4 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
mbed_official 0:51ac1d130fd4 24 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
mbed_official 0:51ac1d130fd4 25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
mbed_official 0:51ac1d130fd4 26 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
mbed_official 0:51ac1d130fd4 27 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
mbed_official 0:51ac1d130fd4 28 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
mbed_official 0:51ac1d130fd4 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
mbed_official 0:51ac1d130fd4 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
mbed_official 0:51ac1d130fd4 31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
mbed_official 0:51ac1d130fd4 32 * OF SUCH DAMAGE.
mbed_official 0:51ac1d130fd4 33 *
mbed_official 0:51ac1d130fd4 34 * This file is part of the lwIP TCP/IP stack.
mbed_official 0:51ac1d130fd4 35 *
mbed_official 0:51ac1d130fd4 36 * Author: Adam Dunkels <adam@sics.se>
mbed_official 0:51ac1d130fd4 37 *
mbed_official 0:51ac1d130fd4 38 */
mbed_official 0:51ac1d130fd4 39
mbed_official 0:51ac1d130fd4 40 #include "lwip/opt.h"
mbed_official 0:51ac1d130fd4 41
mbed_official 0:51ac1d130fd4 42 #include "lwip/def.h"
mbed_official 0:51ac1d130fd4 43 #include "lwip/inet.h"
mbed_official 0:51ac1d130fd4 44
mbed_official 0:51ac1d130fd4 45 /* chksum:
mbed_official 0:51ac1d130fd4 46 *
mbed_official 0:51ac1d130fd4 47 * Sums up all 16 bit words in a memory portion. Also includes any odd byte.
mbed_official 0:51ac1d130fd4 48 * This function is used by the other checksum functions.
mbed_official 0:51ac1d130fd4 49 *
mbed_official 0:51ac1d130fd4 50 * For now, this is not optimized. Must be optimized for the particular processor
mbed_official 0:51ac1d130fd4 51 * arcitecture on which it is to run. Preferebly coded in assembler.
mbed_official 0:51ac1d130fd4 52 */
mbed_official 0:51ac1d130fd4 53
mbed_official 0:51ac1d130fd4 54 static u32_t
mbed_official 0:51ac1d130fd4 55 chksum(void *dataptr, u16_t len)
mbed_official 0:51ac1d130fd4 56 {
mbed_official 0:51ac1d130fd4 57 u16_t *sdataptr = dataptr;
mbed_official 0:51ac1d130fd4 58 u32_t acc;
mbed_official 0:51ac1d130fd4 59
mbed_official 0:51ac1d130fd4 60
mbed_official 0:51ac1d130fd4 61 for(acc = 0; len > 1; len -= 2) {
mbed_official 0:51ac1d130fd4 62 acc += *sdataptr++;
mbed_official 0:51ac1d130fd4 63 }
mbed_official 0:51ac1d130fd4 64
mbed_official 0:51ac1d130fd4 65 /* add up any odd byte */
mbed_official 0:51ac1d130fd4 66 if (len == 1) {
mbed_official 0:51ac1d130fd4 67 acc += htons((u16_t)(*(u8_t *)dataptr) << 8);
mbed_official 0:51ac1d130fd4 68 }
mbed_official 0:51ac1d130fd4 69
mbed_official 0:51ac1d130fd4 70 return acc;
mbed_official 0:51ac1d130fd4 71
mbed_official 0:51ac1d130fd4 72 }
mbed_official 0:51ac1d130fd4 73
mbed_official 0:51ac1d130fd4 74 /* inet_chksum_pseudo:
mbed_official 0:51ac1d130fd4 75 *
mbed_official 0:51ac1d130fd4 76 * Calculates the pseudo Internet checksum used by TCP and UDP for a pbuf chain.
mbed_official 0:51ac1d130fd4 77 */
mbed_official 0:51ac1d130fd4 78
mbed_official 0:51ac1d130fd4 79 u16_t
mbed_official 0:51ac1d130fd4 80 inet_chksum_pseudo(struct pbuf *p,
mbed_official 0:51ac1d130fd4 81 struct ip_addr *src, struct ip_addr *dest,
mbed_official 0:51ac1d130fd4 82 u8_t proto, u32_t proto_len)
mbed_official 0:51ac1d130fd4 83 {
mbed_official 0:51ac1d130fd4 84 u32_t acc;
mbed_official 0:51ac1d130fd4 85 struct pbuf *q;
mbed_official 0:51ac1d130fd4 86 u8_t swapped, i;
mbed_official 0:51ac1d130fd4 87
mbed_official 0:51ac1d130fd4 88 acc = 0;
mbed_official 0:51ac1d130fd4 89 swapped = 0;
mbed_official 0:51ac1d130fd4 90 for(q = p; q != NULL; q = q->next) {
mbed_official 0:51ac1d130fd4 91 acc += chksum(q->payload, q->len);
mbed_official 0:51ac1d130fd4 92 while (acc >> 16) {
mbed_official 0:51ac1d130fd4 93 acc = (acc & 0xffff) + (acc >> 16);
mbed_official 0:51ac1d130fd4 94 }
mbed_official 0:51ac1d130fd4 95 if (q->len % 2 != 0) {
mbed_official 0:51ac1d130fd4 96 swapped = 1 - swapped;
mbed_official 0:51ac1d130fd4 97 acc = ((acc & 0xff) << 8) | ((acc & 0xff00) >> 8);
mbed_official 0:51ac1d130fd4 98 }
mbed_official 0:51ac1d130fd4 99 }
mbed_official 0:51ac1d130fd4 100
mbed_official 0:51ac1d130fd4 101 if (swapped) {
mbed_official 0:51ac1d130fd4 102 acc = ((acc & 0xff) << 8) | ((acc & 0xff00) >> 8);
mbed_official 0:51ac1d130fd4 103 }
mbed_official 0:51ac1d130fd4 104
mbed_official 0:51ac1d130fd4 105 for(i = 0; i < 8; i++) {
mbed_official 0:51ac1d130fd4 106 acc += ((u16_t *)src->addr)[i] & 0xffff;
mbed_official 0:51ac1d130fd4 107 acc += ((u16_t *)dest->addr)[i] & 0xffff;
mbed_official 0:51ac1d130fd4 108 while (acc >> 16) {
mbed_official 0:51ac1d130fd4 109 acc = (acc & 0xffff) + (acc >> 16);
mbed_official 0:51ac1d130fd4 110 }
mbed_official 0:51ac1d130fd4 111 }
mbed_official 0:51ac1d130fd4 112 acc += (u16_t)htons((u16_t)proto);
mbed_official 0:51ac1d130fd4 113 acc += ((u16_t *)&proto_len)[0] & 0xffff;
mbed_official 0:51ac1d130fd4 114 acc += ((u16_t *)&proto_len)[1] & 0xffff;
mbed_official 0:51ac1d130fd4 115
mbed_official 0:51ac1d130fd4 116 while (acc >> 16) {
mbed_official 0:51ac1d130fd4 117 acc = (acc & 0xffff) + (acc >> 16);
mbed_official 0:51ac1d130fd4 118 }
mbed_official 0:51ac1d130fd4 119 return ~(acc & 0xffff);
mbed_official 0:51ac1d130fd4 120 }
mbed_official 0:51ac1d130fd4 121
mbed_official 0:51ac1d130fd4 122 /* inet_chksum:
mbed_official 0:51ac1d130fd4 123 *
mbed_official 0:51ac1d130fd4 124 * Calculates the Internet checksum over a portion of memory. Used primarely for IP
mbed_official 0:51ac1d130fd4 125 * and ICMP.
mbed_official 0:51ac1d130fd4 126 */
mbed_official 0:51ac1d130fd4 127
mbed_official 0:51ac1d130fd4 128 u16_t
mbed_official 0:51ac1d130fd4 129 inet_chksum(void *dataptr, u16_t len)
mbed_official 0:51ac1d130fd4 130 {
mbed_official 0:51ac1d130fd4 131 u32_t acc, sum;
mbed_official 0:51ac1d130fd4 132
mbed_official 0:51ac1d130fd4 133 acc = chksum(dataptr, len);
mbed_official 0:51ac1d130fd4 134 sum = (acc & 0xffff) + (acc >> 16);
mbed_official 0:51ac1d130fd4 135 sum += (sum >> 16);
mbed_official 0:51ac1d130fd4 136 return ~(sum & 0xffff);
mbed_official 0:51ac1d130fd4 137 }
mbed_official 0:51ac1d130fd4 138
mbed_official 0:51ac1d130fd4 139 u16_t
mbed_official 0:51ac1d130fd4 140 inet_chksum_pbuf(struct pbuf *p)
mbed_official 0:51ac1d130fd4 141 {
mbed_official 0:51ac1d130fd4 142 u32_t acc;
mbed_official 0:51ac1d130fd4 143 struct pbuf *q;
mbed_official 0:51ac1d130fd4 144 u8_t swapped;
mbed_official 0:51ac1d130fd4 145
mbed_official 0:51ac1d130fd4 146 acc = 0;
mbed_official 0:51ac1d130fd4 147 swapped = 0;
mbed_official 0:51ac1d130fd4 148 for(q = p; q != NULL; q = q->next) {
mbed_official 0:51ac1d130fd4 149 acc += chksum(q->payload, q->len);
mbed_official 0:51ac1d130fd4 150 while (acc >> 16) {
mbed_official 0:51ac1d130fd4 151 acc = (acc & 0xffff) + (acc >> 16);
mbed_official 0:51ac1d130fd4 152 }
mbed_official 0:51ac1d130fd4 153 if (q->len % 2 != 0) {
mbed_official 0:51ac1d130fd4 154 swapped = 1 - swapped;
mbed_official 0:51ac1d130fd4 155 acc = (acc & 0xff << 8) | (acc & 0xff00 >> 8);
mbed_official 0:51ac1d130fd4 156 }
mbed_official 0:51ac1d130fd4 157 }
mbed_official 0:51ac1d130fd4 158
mbed_official 0:51ac1d130fd4 159 if (swapped) {
mbed_official 0:51ac1d130fd4 160 acc = ((acc & 0xff) << 8) | ((acc & 0xff00) >> 8);
mbed_official 0:51ac1d130fd4 161 }
mbed_official 0:51ac1d130fd4 162 return ~(acc & 0xffff);
mbed_official 0:51ac1d130fd4 163 }