Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include "lwip/opt.h"
00041
00042 #include "lwip/def.h"
00043 #include "lwip/inet.h"
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 static u32_t
00055 chksum(void *dataptr, u16_t len)
00056 {
00057 u16_t *sdataptr = dataptr;
00058 u32_t acc;
00059
00060
00061 for(acc = 0; len > 1; len -= 2) {
00062 acc += *sdataptr++;
00063 }
00064
00065
00066 if (len == 1) {
00067 acc += htons((u16_t)(*(u8_t *)dataptr) << 8);
00068 }
00069
00070 return acc;
00071
00072 }
00073
00074
00075
00076
00077
00078
00079 u16_t
00080 inet_chksum_pseudo(struct pbuf *p,
00081 struct ip_addr *src, struct ip_addr *dest,
00082 u8_t proto, u32_t proto_len)
00083 {
00084 u32_t acc;
00085 struct pbuf *q;
00086 u8_t swapped, i;
00087
00088 acc = 0;
00089 swapped = 0;
00090 for(q = p; q != NULL; q = q->next) {
00091 acc += chksum(q->payload, q->len);
00092 while (acc >> 16) {
00093 acc = (acc & 0xffff) + (acc >> 16);
00094 }
00095 if (q->len % 2 != 0) {
00096 swapped = 1 - swapped;
00097 acc = ((acc & 0xff) << 8) | ((acc & 0xff00) >> 8);
00098 }
00099 }
00100
00101 if (swapped) {
00102 acc = ((acc & 0xff) << 8) | ((acc & 0xff00) >> 8);
00103 }
00104
00105 for(i = 0; i < 8; i++) {
00106 acc += ((u16_t *)src->addr)[i] & 0xffff;
00107 acc += ((u16_t *)dest->addr)[i] & 0xffff;
00108 while (acc >> 16) {
00109 acc = (acc & 0xffff) + (acc >> 16);
00110 }
00111 }
00112 acc += (u16_t)htons((u16_t)proto);
00113 acc += ((u16_t *)&proto_len)[0] & 0xffff;
00114 acc += ((u16_t *)&proto_len)[1] & 0xffff;
00115
00116 while (acc >> 16) {
00117 acc = (acc & 0xffff) + (acc >> 16);
00118 }
00119 return ~(acc & 0xffff);
00120 }
00121
00122
00123
00124
00125
00126
00127
00128 u16_t
00129 inet_chksum(void *dataptr, u16_t len)
00130 {
00131 u32_t acc, sum;
00132
00133 acc = chksum(dataptr, len);
00134 sum = (acc & 0xffff) + (acc >> 16);
00135 sum += (sum >> 16);
00136 return ~(sum & 0xffff);
00137 }
00138
00139 u16_t
00140 inet_chksum_pbuf(struct pbuf *p)
00141 {
00142 u32_t acc;
00143 struct pbuf *q;
00144 u8_t swapped;
00145
00146 acc = 0;
00147 swapped = 0;
00148 for(q = p; q != NULL; q = q->next) {
00149 acc += chksum(q->payload, q->len);
00150 while (acc >> 16) {
00151 acc = (acc & 0xffff) + (acc >> 16);
00152 }
00153 if (q->len % 2 != 0) {
00154 swapped = 1 - swapped;
00155 acc = (acc & 0xff << 8) | (acc & 0xff00 >> 8);
00156 }
00157 }
00158
00159 if (swapped) {
00160 acc = ((acc & 0xff) << 8) | ((acc & 0xff00) >> 8);
00161 }
00162 return ~(acc & 0xffff);
00163 }