Leest de waarde van een sensor binnen een maakt deze beschikbaar via internet

Dependencies:   NTPClient_NetServices mbed

Committer:
hendrikvincent
Date:
Mon Dec 02 09:01:23 2013 +0000
Revision:
0:05ccbd4f84f1
eerste programma;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hendrikvincent 0:05ccbd4f84f1 1 /*
hendrikvincent 0:05ccbd4f84f1 2 * Routines to compress and uncompess tcp packets (for transmission
hendrikvincent 0:05ccbd4f84f1 3 * over low speed serial lines.
hendrikvincent 0:05ccbd4f84f1 4 *
hendrikvincent 0:05ccbd4f84f1 5 * Copyright (c) 1989 Regents of the University of California.
hendrikvincent 0:05ccbd4f84f1 6 * All rights reserved.
hendrikvincent 0:05ccbd4f84f1 7 *
hendrikvincent 0:05ccbd4f84f1 8 * Redistribution and use in source and binary forms are permitted
hendrikvincent 0:05ccbd4f84f1 9 * provided that the above copyright notice and this paragraph are
hendrikvincent 0:05ccbd4f84f1 10 * duplicated in all such forms and that any documentation,
hendrikvincent 0:05ccbd4f84f1 11 * advertising materials, and other materials related to such
hendrikvincent 0:05ccbd4f84f1 12 * distribution and use acknowledge that the software was developed
hendrikvincent 0:05ccbd4f84f1 13 * by the University of California, Berkeley. The name of the
hendrikvincent 0:05ccbd4f84f1 14 * University may not be used to endorse or promote products derived
hendrikvincent 0:05ccbd4f84f1 15 * from this software without specific prior written permission.
hendrikvincent 0:05ccbd4f84f1 16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
hendrikvincent 0:05ccbd4f84f1 17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
hendrikvincent 0:05ccbd4f84f1 18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
hendrikvincent 0:05ccbd4f84f1 19 *
hendrikvincent 0:05ccbd4f84f1 20 * Van Jacobson (van@helios.ee.lbl.gov), Dec 31, 1989:
hendrikvincent 0:05ccbd4f84f1 21 * Initial distribution.
hendrikvincent 0:05ccbd4f84f1 22 *
hendrikvincent 0:05ccbd4f84f1 23 * Modified June 1993 by Paul Mackerras, paulus@cs.anu.edu.au,
hendrikvincent 0:05ccbd4f84f1 24 * so that the entire packet being decompressed doesn't have
hendrikvincent 0:05ccbd4f84f1 25 * to be in contiguous memory (just the compressed header).
hendrikvincent 0:05ccbd4f84f1 26 *
hendrikvincent 0:05ccbd4f84f1 27 * Modified March 1998 by Guy Lancaster, glanca@gesn.com,
hendrikvincent 0:05ccbd4f84f1 28 * for a 16 bit processor.
hendrikvincent 0:05ccbd4f84f1 29 */
hendrikvincent 0:05ccbd4f84f1 30
hendrikvincent 0:05ccbd4f84f1 31 #include "lwip/opt.h"
hendrikvincent 0:05ccbd4f84f1 32
hendrikvincent 0:05ccbd4f84f1 33 #if PPP_SUPPORT /* don't build if not configured for use in lwipopts.h */
hendrikvincent 0:05ccbd4f84f1 34
hendrikvincent 0:05ccbd4f84f1 35 #include "ppp.h"
hendrikvincent 0:05ccbd4f84f1 36 #include "pppdebug.h"
hendrikvincent 0:05ccbd4f84f1 37
hendrikvincent 0:05ccbd4f84f1 38 #include "vj.h"
hendrikvincent 0:05ccbd4f84f1 39
hendrikvincent 0:05ccbd4f84f1 40 #include <string.h>
hendrikvincent 0:05ccbd4f84f1 41
hendrikvincent 0:05ccbd4f84f1 42 #if VJ_SUPPORT
hendrikvincent 0:05ccbd4f84f1 43
hendrikvincent 0:05ccbd4f84f1 44 #if LINK_STATS
hendrikvincent 0:05ccbd4f84f1 45 #define INCR(counter) ++comp->stats.counter
hendrikvincent 0:05ccbd4f84f1 46 #else
hendrikvincent 0:05ccbd4f84f1 47 #define INCR(counter)
hendrikvincent 0:05ccbd4f84f1 48 #endif
hendrikvincent 0:05ccbd4f84f1 49
hendrikvincent 0:05ccbd4f84f1 50 void
hendrikvincent 0:05ccbd4f84f1 51 vj_compress_init(struct vjcompress *comp)
hendrikvincent 0:05ccbd4f84f1 52 {
hendrikvincent 0:05ccbd4f84f1 53 register u_char i;
hendrikvincent 0:05ccbd4f84f1 54 register struct cstate *tstate = comp->tstate;
hendrikvincent 0:05ccbd4f84f1 55
hendrikvincent 0:05ccbd4f84f1 56 #if MAX_SLOTS == 0
hendrikvincent 0:05ccbd4f84f1 57 memset((char *)comp, 0, sizeof(*comp));
hendrikvincent 0:05ccbd4f84f1 58 #endif
hendrikvincent 0:05ccbd4f84f1 59 comp->maxSlotIndex = MAX_SLOTS - 1;
hendrikvincent 0:05ccbd4f84f1 60 comp->compressSlot = 0; /* Disable slot ID compression by default. */
hendrikvincent 0:05ccbd4f84f1 61 for (i = MAX_SLOTS - 1; i > 0; --i) {
hendrikvincent 0:05ccbd4f84f1 62 tstate[i].cs_id = i;
hendrikvincent 0:05ccbd4f84f1 63 tstate[i].cs_next = &tstate[i - 1];
hendrikvincent 0:05ccbd4f84f1 64 }
hendrikvincent 0:05ccbd4f84f1 65 tstate[0].cs_next = &tstate[MAX_SLOTS - 1];
hendrikvincent 0:05ccbd4f84f1 66 tstate[0].cs_id = 0;
hendrikvincent 0:05ccbd4f84f1 67 comp->last_cs = &tstate[0];
hendrikvincent 0:05ccbd4f84f1 68 comp->last_recv = 255;
hendrikvincent 0:05ccbd4f84f1 69 comp->last_xmit = 255;
hendrikvincent 0:05ccbd4f84f1 70 comp->flags = VJF_TOSS;
hendrikvincent 0:05ccbd4f84f1 71 }
hendrikvincent 0:05ccbd4f84f1 72
hendrikvincent 0:05ccbd4f84f1 73
hendrikvincent 0:05ccbd4f84f1 74 /* ENCODE encodes a number that is known to be non-zero. ENCODEZ
hendrikvincent 0:05ccbd4f84f1 75 * checks for zero (since zero has to be encoded in the long, 3 byte
hendrikvincent 0:05ccbd4f84f1 76 * form).
hendrikvincent 0:05ccbd4f84f1 77 */
hendrikvincent 0:05ccbd4f84f1 78 #define ENCODE(n) { \
hendrikvincent 0:05ccbd4f84f1 79 if ((u_short)(n) >= 256) { \
hendrikvincent 0:05ccbd4f84f1 80 *cp++ = 0; \
hendrikvincent 0:05ccbd4f84f1 81 cp[1] = (u_char)(n); \
hendrikvincent 0:05ccbd4f84f1 82 cp[0] = (u_char)((n) >> 8); \
hendrikvincent 0:05ccbd4f84f1 83 cp += 2; \
hendrikvincent 0:05ccbd4f84f1 84 } else { \
hendrikvincent 0:05ccbd4f84f1 85 *cp++ = (u_char)(n); \
hendrikvincent 0:05ccbd4f84f1 86 } \
hendrikvincent 0:05ccbd4f84f1 87 }
hendrikvincent 0:05ccbd4f84f1 88 #define ENCODEZ(n) { \
hendrikvincent 0:05ccbd4f84f1 89 if ((u_short)(n) >= 256 || (u_short)(n) == 0) { \
hendrikvincent 0:05ccbd4f84f1 90 *cp++ = 0; \
hendrikvincent 0:05ccbd4f84f1 91 cp[1] = (u_char)(n); \
hendrikvincent 0:05ccbd4f84f1 92 cp[0] = (u_char)((n) >> 8); \
hendrikvincent 0:05ccbd4f84f1 93 cp += 2; \
hendrikvincent 0:05ccbd4f84f1 94 } else { \
hendrikvincent 0:05ccbd4f84f1 95 *cp++ = (u_char)(n); \
hendrikvincent 0:05ccbd4f84f1 96 } \
hendrikvincent 0:05ccbd4f84f1 97 }
hendrikvincent 0:05ccbd4f84f1 98
hendrikvincent 0:05ccbd4f84f1 99 #define DECODEL(f) { \
hendrikvincent 0:05ccbd4f84f1 100 if (*cp == 0) {\
hendrikvincent 0:05ccbd4f84f1 101 u32_t tmp = ntohl(f) + ((cp[1] << 8) | cp[2]); \
hendrikvincent 0:05ccbd4f84f1 102 (f) = htonl(tmp); \
hendrikvincent 0:05ccbd4f84f1 103 cp += 3; \
hendrikvincent 0:05ccbd4f84f1 104 } else { \
hendrikvincent 0:05ccbd4f84f1 105 u32_t tmp = ntohl(f) + (u32_t)*cp++; \
hendrikvincent 0:05ccbd4f84f1 106 (f) = htonl(tmp); \
hendrikvincent 0:05ccbd4f84f1 107 } \
hendrikvincent 0:05ccbd4f84f1 108 }
hendrikvincent 0:05ccbd4f84f1 109
hendrikvincent 0:05ccbd4f84f1 110 #define DECODES(f) { \
hendrikvincent 0:05ccbd4f84f1 111 if (*cp == 0) {\
hendrikvincent 0:05ccbd4f84f1 112 u_short tmp = ntohs(f) + (((u_short)cp[1] << 8) | cp[2]); \
hendrikvincent 0:05ccbd4f84f1 113 (f) = htons(tmp); \
hendrikvincent 0:05ccbd4f84f1 114 cp += 3; \
hendrikvincent 0:05ccbd4f84f1 115 } else { \
hendrikvincent 0:05ccbd4f84f1 116 u_short tmp = ntohs(f) + (u_short)*cp++; \
hendrikvincent 0:05ccbd4f84f1 117 (f) = htons(tmp); \
hendrikvincent 0:05ccbd4f84f1 118 } \
hendrikvincent 0:05ccbd4f84f1 119 }
hendrikvincent 0:05ccbd4f84f1 120
hendrikvincent 0:05ccbd4f84f1 121 #define DECODEU(f) { \
hendrikvincent 0:05ccbd4f84f1 122 if (*cp == 0) {\
hendrikvincent 0:05ccbd4f84f1 123 (f) = htons(((u_short)cp[1] << 8) | cp[2]); \
hendrikvincent 0:05ccbd4f84f1 124 cp += 3; \
hendrikvincent 0:05ccbd4f84f1 125 } else { \
hendrikvincent 0:05ccbd4f84f1 126 (f) = htons((u_short)*cp++); \
hendrikvincent 0:05ccbd4f84f1 127 } \
hendrikvincent 0:05ccbd4f84f1 128 }
hendrikvincent 0:05ccbd4f84f1 129
hendrikvincent 0:05ccbd4f84f1 130 /*
hendrikvincent 0:05ccbd4f84f1 131 * vj_compress_tcp - Attempt to do Van Jacobson header compression on a
hendrikvincent 0:05ccbd4f84f1 132 * packet. This assumes that nb and comp are not null and that the first
hendrikvincent 0:05ccbd4f84f1 133 * buffer of the chain contains a valid IP header.
hendrikvincent 0:05ccbd4f84f1 134 * Return the VJ type code indicating whether or not the packet was
hendrikvincent 0:05ccbd4f84f1 135 * compressed.
hendrikvincent 0:05ccbd4f84f1 136 */
hendrikvincent 0:05ccbd4f84f1 137 u_int
hendrikvincent 0:05ccbd4f84f1 138 vj_compress_tcp(struct vjcompress *comp, struct pbuf *pb)
hendrikvincent 0:05ccbd4f84f1 139 {
hendrikvincent 0:05ccbd4f84f1 140 register struct ip_hdr *ip = (struct ip_hdr *)pb->payload;
hendrikvincent 0:05ccbd4f84f1 141 register struct cstate *cs = comp->last_cs->cs_next;
hendrikvincent 0:05ccbd4f84f1 142 register u_short hlen = IPH_HL(ip);
hendrikvincent 0:05ccbd4f84f1 143 register struct tcp_hdr *oth;
hendrikvincent 0:05ccbd4f84f1 144 register struct tcp_hdr *th;
hendrikvincent 0:05ccbd4f84f1 145 register u_short deltaS, deltaA;
hendrikvincent 0:05ccbd4f84f1 146 register u_long deltaL;
hendrikvincent 0:05ccbd4f84f1 147 register u_int changes = 0;
hendrikvincent 0:05ccbd4f84f1 148 u_char new_seq[16];
hendrikvincent 0:05ccbd4f84f1 149 register u_char *cp = new_seq;
hendrikvincent 0:05ccbd4f84f1 150
hendrikvincent 0:05ccbd4f84f1 151 /*
hendrikvincent 0:05ccbd4f84f1 152 * Check that the packet is IP proto TCP.
hendrikvincent 0:05ccbd4f84f1 153 */
hendrikvincent 0:05ccbd4f84f1 154 if (IPH_PROTO(ip) != IP_PROTO_TCP) {
hendrikvincent 0:05ccbd4f84f1 155 return (TYPE_IP);
hendrikvincent 0:05ccbd4f84f1 156 }
hendrikvincent 0:05ccbd4f84f1 157
hendrikvincent 0:05ccbd4f84f1 158 /*
hendrikvincent 0:05ccbd4f84f1 159 * Bail if this is an IP fragment or if the TCP packet isn't
hendrikvincent 0:05ccbd4f84f1 160 * `compressible' (i.e., ACK isn't set or some other control bit is
hendrikvincent 0:05ccbd4f84f1 161 * set).
hendrikvincent 0:05ccbd4f84f1 162 */
hendrikvincent 0:05ccbd4f84f1 163 if ((IPH_OFFSET(ip) & PP_HTONS(0x3fff)) || pb->tot_len < 40) {
hendrikvincent 0:05ccbd4f84f1 164 return (TYPE_IP);
hendrikvincent 0:05ccbd4f84f1 165 }
hendrikvincent 0:05ccbd4f84f1 166 th = (struct tcp_hdr *)&((long *)ip)[hlen];
hendrikvincent 0:05ccbd4f84f1 167 if ((TCPH_FLAGS(th) & (TCP_SYN|TCP_FIN|TCP_RST|TCP_ACK)) != TCP_ACK) {
hendrikvincent 0:05ccbd4f84f1 168 return (TYPE_IP);
hendrikvincent 0:05ccbd4f84f1 169 }
hendrikvincent 0:05ccbd4f84f1 170 /*
hendrikvincent 0:05ccbd4f84f1 171 * Packet is compressible -- we're going to send either a
hendrikvincent 0:05ccbd4f84f1 172 * COMPRESSED_TCP or UNCOMPRESSED_TCP packet. Either way we need
hendrikvincent 0:05ccbd4f84f1 173 * to locate (or create) the connection state. Special case the
hendrikvincent 0:05ccbd4f84f1 174 * most recently used connection since it's most likely to be used
hendrikvincent 0:05ccbd4f84f1 175 * again & we don't have to do any reordering if it's used.
hendrikvincent 0:05ccbd4f84f1 176 */
hendrikvincent 0:05ccbd4f84f1 177 INCR(vjs_packets);
hendrikvincent 0:05ccbd4f84f1 178 if (!ip_addr_cmp(&ip->src, &cs->cs_ip.src)
hendrikvincent 0:05ccbd4f84f1 179 || !ip_addr_cmp(&ip->dest, &cs->cs_ip.dest)
hendrikvincent 0:05ccbd4f84f1 180 || *(long *)th != ((long *)&cs->cs_ip)[IPH_HL(&cs->cs_ip)]) {
hendrikvincent 0:05ccbd4f84f1 181 /*
hendrikvincent 0:05ccbd4f84f1 182 * Wasn't the first -- search for it.
hendrikvincent 0:05ccbd4f84f1 183 *
hendrikvincent 0:05ccbd4f84f1 184 * States are kept in a circularly linked list with
hendrikvincent 0:05ccbd4f84f1 185 * last_cs pointing to the end of the list. The
hendrikvincent 0:05ccbd4f84f1 186 * list is kept in lru order by moving a state to the
hendrikvincent 0:05ccbd4f84f1 187 * head of the list whenever it is referenced. Since
hendrikvincent 0:05ccbd4f84f1 188 * the list is short and, empirically, the connection
hendrikvincent 0:05ccbd4f84f1 189 * we want is almost always near the front, we locate
hendrikvincent 0:05ccbd4f84f1 190 * states via linear search. If we don't find a state
hendrikvincent 0:05ccbd4f84f1 191 * for the datagram, the oldest state is (re-)used.
hendrikvincent 0:05ccbd4f84f1 192 */
hendrikvincent 0:05ccbd4f84f1 193 register struct cstate *lcs;
hendrikvincent 0:05ccbd4f84f1 194 register struct cstate *lastcs = comp->last_cs;
hendrikvincent 0:05ccbd4f84f1 195
hendrikvincent 0:05ccbd4f84f1 196 do {
hendrikvincent 0:05ccbd4f84f1 197 lcs = cs; cs = cs->cs_next;
hendrikvincent 0:05ccbd4f84f1 198 INCR(vjs_searches);
hendrikvincent 0:05ccbd4f84f1 199 if (ip_addr_cmp(&ip->src, &cs->cs_ip.src)
hendrikvincent 0:05ccbd4f84f1 200 && ip_addr_cmp(&ip->dest, &cs->cs_ip.dest)
hendrikvincent 0:05ccbd4f84f1 201 && *(long *)th == ((long *)&cs->cs_ip)[IPH_HL(&cs->cs_ip)]) {
hendrikvincent 0:05ccbd4f84f1 202 goto found;
hendrikvincent 0:05ccbd4f84f1 203 }
hendrikvincent 0:05ccbd4f84f1 204 } while (cs != lastcs);
hendrikvincent 0:05ccbd4f84f1 205
hendrikvincent 0:05ccbd4f84f1 206 /*
hendrikvincent 0:05ccbd4f84f1 207 * Didn't find it -- re-use oldest cstate. Send an
hendrikvincent 0:05ccbd4f84f1 208 * uncompressed packet that tells the other side what
hendrikvincent 0:05ccbd4f84f1 209 * connection number we're using for this conversation.
hendrikvincent 0:05ccbd4f84f1 210 * Note that since the state list is circular, the oldest
hendrikvincent 0:05ccbd4f84f1 211 * state points to the newest and we only need to set
hendrikvincent 0:05ccbd4f84f1 212 * last_cs to update the lru linkage.
hendrikvincent 0:05ccbd4f84f1 213 */
hendrikvincent 0:05ccbd4f84f1 214 INCR(vjs_misses);
hendrikvincent 0:05ccbd4f84f1 215 comp->last_cs = lcs;
hendrikvincent 0:05ccbd4f84f1 216 hlen += TCPH_OFFSET(th);
hendrikvincent 0:05ccbd4f84f1 217 hlen <<= 2;
hendrikvincent 0:05ccbd4f84f1 218 /* Check that the IP/TCP headers are contained in the first buffer. */
hendrikvincent 0:05ccbd4f84f1 219 if (hlen > pb->len) {
hendrikvincent 0:05ccbd4f84f1 220 return (TYPE_IP);
hendrikvincent 0:05ccbd4f84f1 221 }
hendrikvincent 0:05ccbd4f84f1 222 goto uncompressed;
hendrikvincent 0:05ccbd4f84f1 223
hendrikvincent 0:05ccbd4f84f1 224 found:
hendrikvincent 0:05ccbd4f84f1 225 /*
hendrikvincent 0:05ccbd4f84f1 226 * Found it -- move to the front on the connection list.
hendrikvincent 0:05ccbd4f84f1 227 */
hendrikvincent 0:05ccbd4f84f1 228 if (cs == lastcs) {
hendrikvincent 0:05ccbd4f84f1 229 comp->last_cs = lcs;
hendrikvincent 0:05ccbd4f84f1 230 } else {
hendrikvincent 0:05ccbd4f84f1 231 lcs->cs_next = cs->cs_next;
hendrikvincent 0:05ccbd4f84f1 232 cs->cs_next = lastcs->cs_next;
hendrikvincent 0:05ccbd4f84f1 233 lastcs->cs_next = cs;
hendrikvincent 0:05ccbd4f84f1 234 }
hendrikvincent 0:05ccbd4f84f1 235 }
hendrikvincent 0:05ccbd4f84f1 236
hendrikvincent 0:05ccbd4f84f1 237 oth = (struct tcp_hdr *)&((long *)&cs->cs_ip)[hlen];
hendrikvincent 0:05ccbd4f84f1 238 deltaS = hlen;
hendrikvincent 0:05ccbd4f84f1 239 hlen += TCPH_OFFSET(th);
hendrikvincent 0:05ccbd4f84f1 240 hlen <<= 2;
hendrikvincent 0:05ccbd4f84f1 241 /* Check that the IP/TCP headers are contained in the first buffer. */
hendrikvincent 0:05ccbd4f84f1 242 if (hlen > pb->len) {
hendrikvincent 0:05ccbd4f84f1 243 PPPDEBUG(LOG_INFO, ("vj_compress_tcp: header len %d spans buffers\n", hlen));
hendrikvincent 0:05ccbd4f84f1 244 return (TYPE_IP);
hendrikvincent 0:05ccbd4f84f1 245 }
hendrikvincent 0:05ccbd4f84f1 246
hendrikvincent 0:05ccbd4f84f1 247 /*
hendrikvincent 0:05ccbd4f84f1 248 * Make sure that only what we expect to change changed. The first
hendrikvincent 0:05ccbd4f84f1 249 * line of the `if' checks the IP protocol version, header length &
hendrikvincent 0:05ccbd4f84f1 250 * type of service. The 2nd line checks the "Don't fragment" bit.
hendrikvincent 0:05ccbd4f84f1 251 * The 3rd line checks the time-to-live and protocol (the protocol
hendrikvincent 0:05ccbd4f84f1 252 * check is unnecessary but costless). The 4th line checks the TCP
hendrikvincent 0:05ccbd4f84f1 253 * header length. The 5th line checks IP options, if any. The 6th
hendrikvincent 0:05ccbd4f84f1 254 * line checks TCP options, if any. If any of these things are
hendrikvincent 0:05ccbd4f84f1 255 * different between the previous & current datagram, we send the
hendrikvincent 0:05ccbd4f84f1 256 * current datagram `uncompressed'.
hendrikvincent 0:05ccbd4f84f1 257 */
hendrikvincent 0:05ccbd4f84f1 258 if (((u_short *)ip)[0] != ((u_short *)&cs->cs_ip)[0]
hendrikvincent 0:05ccbd4f84f1 259 || ((u_short *)ip)[3] != ((u_short *)&cs->cs_ip)[3]
hendrikvincent 0:05ccbd4f84f1 260 || ((u_short *)ip)[4] != ((u_short *)&cs->cs_ip)[4]
hendrikvincent 0:05ccbd4f84f1 261 || TCPH_OFFSET(th) != TCPH_OFFSET(oth)
hendrikvincent 0:05ccbd4f84f1 262 || (deltaS > 5 && BCMP(ip + 1, &cs->cs_ip + 1, (deltaS - 5) << 2))
hendrikvincent 0:05ccbd4f84f1 263 || (TCPH_OFFSET(th) > 5 && BCMP(th + 1, oth + 1, (TCPH_OFFSET(th) - 5) << 2))) {
hendrikvincent 0:05ccbd4f84f1 264 goto uncompressed;
hendrikvincent 0:05ccbd4f84f1 265 }
hendrikvincent 0:05ccbd4f84f1 266
hendrikvincent 0:05ccbd4f84f1 267 /*
hendrikvincent 0:05ccbd4f84f1 268 * Figure out which of the changing fields changed. The
hendrikvincent 0:05ccbd4f84f1 269 * receiver expects changes in the order: urgent, window,
hendrikvincent 0:05ccbd4f84f1 270 * ack, seq (the order minimizes the number of temporaries
hendrikvincent 0:05ccbd4f84f1 271 * needed in this section of code).
hendrikvincent 0:05ccbd4f84f1 272 */
hendrikvincent 0:05ccbd4f84f1 273 if (TCPH_FLAGS(th) & TCP_URG) {
hendrikvincent 0:05ccbd4f84f1 274 deltaS = ntohs(th->urgp);
hendrikvincent 0:05ccbd4f84f1 275 ENCODEZ(deltaS);
hendrikvincent 0:05ccbd4f84f1 276 changes |= NEW_U;
hendrikvincent 0:05ccbd4f84f1 277 } else if (th->urgp != oth->urgp) {
hendrikvincent 0:05ccbd4f84f1 278 /* argh! URG not set but urp changed -- a sensible
hendrikvincent 0:05ccbd4f84f1 279 * implementation should never do this but RFC793
hendrikvincent 0:05ccbd4f84f1 280 * doesn't prohibit the change so we have to deal
hendrikvincent 0:05ccbd4f84f1 281 * with it. */
hendrikvincent 0:05ccbd4f84f1 282 goto uncompressed;
hendrikvincent 0:05ccbd4f84f1 283 }
hendrikvincent 0:05ccbd4f84f1 284
hendrikvincent 0:05ccbd4f84f1 285 if ((deltaS = (u_short)(ntohs(th->wnd) - ntohs(oth->wnd))) != 0) {
hendrikvincent 0:05ccbd4f84f1 286 ENCODE(deltaS);
hendrikvincent 0:05ccbd4f84f1 287 changes |= NEW_W;
hendrikvincent 0:05ccbd4f84f1 288 }
hendrikvincent 0:05ccbd4f84f1 289
hendrikvincent 0:05ccbd4f84f1 290 if ((deltaL = ntohl(th->ackno) - ntohl(oth->ackno)) != 0) {
hendrikvincent 0:05ccbd4f84f1 291 if (deltaL > 0xffff) {
hendrikvincent 0:05ccbd4f84f1 292 goto uncompressed;
hendrikvincent 0:05ccbd4f84f1 293 }
hendrikvincent 0:05ccbd4f84f1 294 deltaA = (u_short)deltaL;
hendrikvincent 0:05ccbd4f84f1 295 ENCODE(deltaA);
hendrikvincent 0:05ccbd4f84f1 296 changes |= NEW_A;
hendrikvincent 0:05ccbd4f84f1 297 }
hendrikvincent 0:05ccbd4f84f1 298
hendrikvincent 0:05ccbd4f84f1 299 if ((deltaL = ntohl(th->seqno) - ntohl(oth->seqno)) != 0) {
hendrikvincent 0:05ccbd4f84f1 300 if (deltaL > 0xffff) {
hendrikvincent 0:05ccbd4f84f1 301 goto uncompressed;
hendrikvincent 0:05ccbd4f84f1 302 }
hendrikvincent 0:05ccbd4f84f1 303 deltaS = (u_short)deltaL;
hendrikvincent 0:05ccbd4f84f1 304 ENCODE(deltaS);
hendrikvincent 0:05ccbd4f84f1 305 changes |= NEW_S;
hendrikvincent 0:05ccbd4f84f1 306 }
hendrikvincent 0:05ccbd4f84f1 307
hendrikvincent 0:05ccbd4f84f1 308 switch(changes) {
hendrikvincent 0:05ccbd4f84f1 309 case 0:
hendrikvincent 0:05ccbd4f84f1 310 /*
hendrikvincent 0:05ccbd4f84f1 311 * Nothing changed. If this packet contains data and the
hendrikvincent 0:05ccbd4f84f1 312 * last one didn't, this is probably a data packet following
hendrikvincent 0:05ccbd4f84f1 313 * an ack (normal on an interactive connection) and we send
hendrikvincent 0:05ccbd4f84f1 314 * it compressed. Otherwise it's probably a retransmit,
hendrikvincent 0:05ccbd4f84f1 315 * retransmitted ack or window probe. Send it uncompressed
hendrikvincent 0:05ccbd4f84f1 316 * in case the other side missed the compressed version.
hendrikvincent 0:05ccbd4f84f1 317 */
hendrikvincent 0:05ccbd4f84f1 318 if (IPH_LEN(ip) != IPH_LEN(&cs->cs_ip) &&
hendrikvincent 0:05ccbd4f84f1 319 ntohs(IPH_LEN(&cs->cs_ip)) == hlen) {
hendrikvincent 0:05ccbd4f84f1 320 break;
hendrikvincent 0:05ccbd4f84f1 321 }
hendrikvincent 0:05ccbd4f84f1 322
hendrikvincent 0:05ccbd4f84f1 323 /* (fall through) */
hendrikvincent 0:05ccbd4f84f1 324
hendrikvincent 0:05ccbd4f84f1 325 case SPECIAL_I:
hendrikvincent 0:05ccbd4f84f1 326 case SPECIAL_D:
hendrikvincent 0:05ccbd4f84f1 327 /*
hendrikvincent 0:05ccbd4f84f1 328 * actual changes match one of our special case encodings --
hendrikvincent 0:05ccbd4f84f1 329 * send packet uncompressed.
hendrikvincent 0:05ccbd4f84f1 330 */
hendrikvincent 0:05ccbd4f84f1 331 goto uncompressed;
hendrikvincent 0:05ccbd4f84f1 332
hendrikvincent 0:05ccbd4f84f1 333 case NEW_S|NEW_A:
hendrikvincent 0:05ccbd4f84f1 334 if (deltaS == deltaA && deltaS == ntohs(IPH_LEN(&cs->cs_ip)) - hlen) {
hendrikvincent 0:05ccbd4f84f1 335 /* special case for echoed terminal traffic */
hendrikvincent 0:05ccbd4f84f1 336 changes = SPECIAL_I;
hendrikvincent 0:05ccbd4f84f1 337 cp = new_seq;
hendrikvincent 0:05ccbd4f84f1 338 }
hendrikvincent 0:05ccbd4f84f1 339 break;
hendrikvincent 0:05ccbd4f84f1 340
hendrikvincent 0:05ccbd4f84f1 341 case NEW_S:
hendrikvincent 0:05ccbd4f84f1 342 if (deltaS == ntohs(IPH_LEN(&cs->cs_ip)) - hlen) {
hendrikvincent 0:05ccbd4f84f1 343 /* special case for data xfer */
hendrikvincent 0:05ccbd4f84f1 344 changes = SPECIAL_D;
hendrikvincent 0:05ccbd4f84f1 345 cp = new_seq;
hendrikvincent 0:05ccbd4f84f1 346 }
hendrikvincent 0:05ccbd4f84f1 347 break;
hendrikvincent 0:05ccbd4f84f1 348 }
hendrikvincent 0:05ccbd4f84f1 349
hendrikvincent 0:05ccbd4f84f1 350 deltaS = (u_short)(ntohs(IPH_ID(ip)) - ntohs(IPH_ID(&cs->cs_ip)));
hendrikvincent 0:05ccbd4f84f1 351 if (deltaS != 1) {
hendrikvincent 0:05ccbd4f84f1 352 ENCODEZ(deltaS);
hendrikvincent 0:05ccbd4f84f1 353 changes |= NEW_I;
hendrikvincent 0:05ccbd4f84f1 354 }
hendrikvincent 0:05ccbd4f84f1 355 if (TCPH_FLAGS(th) & TCP_PSH) {
hendrikvincent 0:05ccbd4f84f1 356 changes |= TCP_PUSH_BIT;
hendrikvincent 0:05ccbd4f84f1 357 }
hendrikvincent 0:05ccbd4f84f1 358 /*
hendrikvincent 0:05ccbd4f84f1 359 * Grab the cksum before we overwrite it below. Then update our
hendrikvincent 0:05ccbd4f84f1 360 * state with this packet's header.
hendrikvincent 0:05ccbd4f84f1 361 */
hendrikvincent 0:05ccbd4f84f1 362 deltaA = ntohs(th->chksum);
hendrikvincent 0:05ccbd4f84f1 363 BCOPY(ip, &cs->cs_ip, hlen);
hendrikvincent 0:05ccbd4f84f1 364
hendrikvincent 0:05ccbd4f84f1 365 /*
hendrikvincent 0:05ccbd4f84f1 366 * We want to use the original packet as our compressed packet.
hendrikvincent 0:05ccbd4f84f1 367 * (cp - new_seq) is the number of bytes we need for compressed
hendrikvincent 0:05ccbd4f84f1 368 * sequence numbers. In addition we need one byte for the change
hendrikvincent 0:05ccbd4f84f1 369 * mask, one for the connection id and two for the tcp checksum.
hendrikvincent 0:05ccbd4f84f1 370 * So, (cp - new_seq) + 4 bytes of header are needed. hlen is how
hendrikvincent 0:05ccbd4f84f1 371 * many bytes of the original packet to toss so subtract the two to
hendrikvincent 0:05ccbd4f84f1 372 * get the new packet size.
hendrikvincent 0:05ccbd4f84f1 373 */
hendrikvincent 0:05ccbd4f84f1 374 deltaS = (u_short)(cp - new_seq);
hendrikvincent 0:05ccbd4f84f1 375 if (!comp->compressSlot || comp->last_xmit != cs->cs_id) {
hendrikvincent 0:05ccbd4f84f1 376 comp->last_xmit = cs->cs_id;
hendrikvincent 0:05ccbd4f84f1 377 hlen -= deltaS + 4;
hendrikvincent 0:05ccbd4f84f1 378 if(pbuf_header(pb, -hlen)){
hendrikvincent 0:05ccbd4f84f1 379 /* Can we cope with this failing? Just assert for now */
hendrikvincent 0:05ccbd4f84f1 380 LWIP_ASSERT("pbuf_header failed\n", 0);
hendrikvincent 0:05ccbd4f84f1 381 }
hendrikvincent 0:05ccbd4f84f1 382 cp = (u_char *)pb->payload;
hendrikvincent 0:05ccbd4f84f1 383 *cp++ = (u_char)(changes | NEW_C);
hendrikvincent 0:05ccbd4f84f1 384 *cp++ = cs->cs_id;
hendrikvincent 0:05ccbd4f84f1 385 } else {
hendrikvincent 0:05ccbd4f84f1 386 hlen -= deltaS + 3;
hendrikvincent 0:05ccbd4f84f1 387 if(pbuf_header(pb, -hlen)) {
hendrikvincent 0:05ccbd4f84f1 388 /* Can we cope with this failing? Just assert for now */
hendrikvincent 0:05ccbd4f84f1 389 LWIP_ASSERT("pbuf_header failed\n", 0);
hendrikvincent 0:05ccbd4f84f1 390 }
hendrikvincent 0:05ccbd4f84f1 391 cp = (u_char *)pb->payload;
hendrikvincent 0:05ccbd4f84f1 392 *cp++ = (u_char)changes;
hendrikvincent 0:05ccbd4f84f1 393 }
hendrikvincent 0:05ccbd4f84f1 394 *cp++ = (u_char)(deltaA >> 8);
hendrikvincent 0:05ccbd4f84f1 395 *cp++ = (u_char)deltaA;
hendrikvincent 0:05ccbd4f84f1 396 BCOPY(new_seq, cp, deltaS);
hendrikvincent 0:05ccbd4f84f1 397 INCR(vjs_compressed);
hendrikvincent 0:05ccbd4f84f1 398 return (TYPE_COMPRESSED_TCP);
hendrikvincent 0:05ccbd4f84f1 399
hendrikvincent 0:05ccbd4f84f1 400 /*
hendrikvincent 0:05ccbd4f84f1 401 * Update connection state cs & send uncompressed packet (that is,
hendrikvincent 0:05ccbd4f84f1 402 * a regular ip/tcp packet but with the 'conversation id' we hope
hendrikvincent 0:05ccbd4f84f1 403 * to use on future compressed packets in the protocol field).
hendrikvincent 0:05ccbd4f84f1 404 */
hendrikvincent 0:05ccbd4f84f1 405 uncompressed:
hendrikvincent 0:05ccbd4f84f1 406 BCOPY(ip, &cs->cs_ip, hlen);
hendrikvincent 0:05ccbd4f84f1 407 IPH_PROTO_SET(ip, cs->cs_id);
hendrikvincent 0:05ccbd4f84f1 408 comp->last_xmit = cs->cs_id;
hendrikvincent 0:05ccbd4f84f1 409 return (TYPE_UNCOMPRESSED_TCP);
hendrikvincent 0:05ccbd4f84f1 410 }
hendrikvincent 0:05ccbd4f84f1 411
hendrikvincent 0:05ccbd4f84f1 412 /*
hendrikvincent 0:05ccbd4f84f1 413 * Called when we may have missed a packet.
hendrikvincent 0:05ccbd4f84f1 414 */
hendrikvincent 0:05ccbd4f84f1 415 void
hendrikvincent 0:05ccbd4f84f1 416 vj_uncompress_err(struct vjcompress *comp)
hendrikvincent 0:05ccbd4f84f1 417 {
hendrikvincent 0:05ccbd4f84f1 418 comp->flags |= VJF_TOSS;
hendrikvincent 0:05ccbd4f84f1 419 INCR(vjs_errorin);
hendrikvincent 0:05ccbd4f84f1 420 }
hendrikvincent 0:05ccbd4f84f1 421
hendrikvincent 0:05ccbd4f84f1 422 /*
hendrikvincent 0:05ccbd4f84f1 423 * "Uncompress" a packet of type TYPE_UNCOMPRESSED_TCP.
hendrikvincent 0:05ccbd4f84f1 424 * Return 0 on success, -1 on failure.
hendrikvincent 0:05ccbd4f84f1 425 */
hendrikvincent 0:05ccbd4f84f1 426 int
hendrikvincent 0:05ccbd4f84f1 427 vj_uncompress_uncomp(struct pbuf *nb, struct vjcompress *comp)
hendrikvincent 0:05ccbd4f84f1 428 {
hendrikvincent 0:05ccbd4f84f1 429 register u_int hlen;
hendrikvincent 0:05ccbd4f84f1 430 register struct cstate *cs;
hendrikvincent 0:05ccbd4f84f1 431 register struct ip_hdr *ip;
hendrikvincent 0:05ccbd4f84f1 432
hendrikvincent 0:05ccbd4f84f1 433 ip = (struct ip_hdr *)nb->payload;
hendrikvincent 0:05ccbd4f84f1 434 hlen = IPH_HL(ip) << 2;
hendrikvincent 0:05ccbd4f84f1 435 if (IPH_PROTO(ip) >= MAX_SLOTS
hendrikvincent 0:05ccbd4f84f1 436 || hlen + sizeof(struct tcp_hdr) > nb->len
hendrikvincent 0:05ccbd4f84f1 437 || (hlen += TCPH_OFFSET(((struct tcp_hdr *)&((char *)ip)[hlen])) << 2)
hendrikvincent 0:05ccbd4f84f1 438 > nb->len
hendrikvincent 0:05ccbd4f84f1 439 || hlen > MAX_HDR) {
hendrikvincent 0:05ccbd4f84f1 440 PPPDEBUG(LOG_INFO, ("vj_uncompress_uncomp: bad cid=%d, hlen=%d buflen=%d\n",
hendrikvincent 0:05ccbd4f84f1 441 IPH_PROTO(ip), hlen, nb->len));
hendrikvincent 0:05ccbd4f84f1 442 comp->flags |= VJF_TOSS;
hendrikvincent 0:05ccbd4f84f1 443 INCR(vjs_errorin);
hendrikvincent 0:05ccbd4f84f1 444 return -1;
hendrikvincent 0:05ccbd4f84f1 445 }
hendrikvincent 0:05ccbd4f84f1 446 cs = &comp->rstate[comp->last_recv = IPH_PROTO(ip)];
hendrikvincent 0:05ccbd4f84f1 447 comp->flags &=~ VJF_TOSS;
hendrikvincent 0:05ccbd4f84f1 448 IPH_PROTO_SET(ip, IP_PROTO_TCP);
hendrikvincent 0:05ccbd4f84f1 449 BCOPY(ip, &cs->cs_ip, hlen);
hendrikvincent 0:05ccbd4f84f1 450 cs->cs_hlen = (u_short)hlen;
hendrikvincent 0:05ccbd4f84f1 451 INCR(vjs_uncompressedin);
hendrikvincent 0:05ccbd4f84f1 452 return 0;
hendrikvincent 0:05ccbd4f84f1 453 }
hendrikvincent 0:05ccbd4f84f1 454
hendrikvincent 0:05ccbd4f84f1 455 /*
hendrikvincent 0:05ccbd4f84f1 456 * Uncompress a packet of type TYPE_COMPRESSED_TCP.
hendrikvincent 0:05ccbd4f84f1 457 * The packet is composed of a buffer chain and the first buffer
hendrikvincent 0:05ccbd4f84f1 458 * must contain an accurate chain length.
hendrikvincent 0:05ccbd4f84f1 459 * The first buffer must include the entire compressed TCP/IP header.
hendrikvincent 0:05ccbd4f84f1 460 * This procedure replaces the compressed header with the uncompressed
hendrikvincent 0:05ccbd4f84f1 461 * header and returns the length of the VJ header.
hendrikvincent 0:05ccbd4f84f1 462 */
hendrikvincent 0:05ccbd4f84f1 463 int
hendrikvincent 0:05ccbd4f84f1 464 vj_uncompress_tcp(struct pbuf **nb, struct vjcompress *comp)
hendrikvincent 0:05ccbd4f84f1 465 {
hendrikvincent 0:05ccbd4f84f1 466 u_char *cp;
hendrikvincent 0:05ccbd4f84f1 467 struct tcp_hdr *th;
hendrikvincent 0:05ccbd4f84f1 468 struct cstate *cs;
hendrikvincent 0:05ccbd4f84f1 469 u_short *bp;
hendrikvincent 0:05ccbd4f84f1 470 struct pbuf *n0 = *nb;
hendrikvincent 0:05ccbd4f84f1 471 u32_t tmp;
hendrikvincent 0:05ccbd4f84f1 472 u_int vjlen, hlen, changes;
hendrikvincent 0:05ccbd4f84f1 473
hendrikvincent 0:05ccbd4f84f1 474 INCR(vjs_compressedin);
hendrikvincent 0:05ccbd4f84f1 475 cp = (u_char *)n0->payload;
hendrikvincent 0:05ccbd4f84f1 476 changes = *cp++;
hendrikvincent 0:05ccbd4f84f1 477 if (changes & NEW_C) {
hendrikvincent 0:05ccbd4f84f1 478 /*
hendrikvincent 0:05ccbd4f84f1 479 * Make sure the state index is in range, then grab the state.
hendrikvincent 0:05ccbd4f84f1 480 * If we have a good state index, clear the 'discard' flag.
hendrikvincent 0:05ccbd4f84f1 481 */
hendrikvincent 0:05ccbd4f84f1 482 if (*cp >= MAX_SLOTS) {
hendrikvincent 0:05ccbd4f84f1 483 PPPDEBUG(LOG_INFO, ("vj_uncompress_tcp: bad cid=%d\n", *cp));
hendrikvincent 0:05ccbd4f84f1 484 goto bad;
hendrikvincent 0:05ccbd4f84f1 485 }
hendrikvincent 0:05ccbd4f84f1 486
hendrikvincent 0:05ccbd4f84f1 487 comp->flags &=~ VJF_TOSS;
hendrikvincent 0:05ccbd4f84f1 488 comp->last_recv = *cp++;
hendrikvincent 0:05ccbd4f84f1 489 } else {
hendrikvincent 0:05ccbd4f84f1 490 /*
hendrikvincent 0:05ccbd4f84f1 491 * this packet has an implicit state index. If we've
hendrikvincent 0:05ccbd4f84f1 492 * had a line error since the last time we got an
hendrikvincent 0:05ccbd4f84f1 493 * explicit state index, we have to toss the packet.
hendrikvincent 0:05ccbd4f84f1 494 */
hendrikvincent 0:05ccbd4f84f1 495 if (comp->flags & VJF_TOSS) {
hendrikvincent 0:05ccbd4f84f1 496 PPPDEBUG(LOG_INFO, ("vj_uncompress_tcp: tossing\n"));
hendrikvincent 0:05ccbd4f84f1 497 INCR(vjs_tossed);
hendrikvincent 0:05ccbd4f84f1 498 return (-1);
hendrikvincent 0:05ccbd4f84f1 499 }
hendrikvincent 0:05ccbd4f84f1 500 }
hendrikvincent 0:05ccbd4f84f1 501 cs = &comp->rstate[comp->last_recv];
hendrikvincent 0:05ccbd4f84f1 502 hlen = IPH_HL(&cs->cs_ip) << 2;
hendrikvincent 0:05ccbd4f84f1 503 th = (struct tcp_hdr *)&((u_char *)&cs->cs_ip)[hlen];
hendrikvincent 0:05ccbd4f84f1 504 th->chksum = htons((*cp << 8) | cp[1]);
hendrikvincent 0:05ccbd4f84f1 505 cp += 2;
hendrikvincent 0:05ccbd4f84f1 506 if (changes & TCP_PUSH_BIT) {
hendrikvincent 0:05ccbd4f84f1 507 TCPH_SET_FLAG(th, TCP_PSH);
hendrikvincent 0:05ccbd4f84f1 508 } else {
hendrikvincent 0:05ccbd4f84f1 509 TCPH_UNSET_FLAG(th, TCP_PSH);
hendrikvincent 0:05ccbd4f84f1 510 }
hendrikvincent 0:05ccbd4f84f1 511
hendrikvincent 0:05ccbd4f84f1 512 switch (changes & SPECIALS_MASK) {
hendrikvincent 0:05ccbd4f84f1 513 case SPECIAL_I:
hendrikvincent 0:05ccbd4f84f1 514 {
hendrikvincent 0:05ccbd4f84f1 515 register u32_t i = ntohs(IPH_LEN(&cs->cs_ip)) - cs->cs_hlen;
hendrikvincent 0:05ccbd4f84f1 516 /* some compilers can't nest inline assembler.. */
hendrikvincent 0:05ccbd4f84f1 517 tmp = ntohl(th->ackno) + i;
hendrikvincent 0:05ccbd4f84f1 518 th->ackno = htonl(tmp);
hendrikvincent 0:05ccbd4f84f1 519 tmp = ntohl(th->seqno) + i;
hendrikvincent 0:05ccbd4f84f1 520 th->seqno = htonl(tmp);
hendrikvincent 0:05ccbd4f84f1 521 }
hendrikvincent 0:05ccbd4f84f1 522 break;
hendrikvincent 0:05ccbd4f84f1 523
hendrikvincent 0:05ccbd4f84f1 524 case SPECIAL_D:
hendrikvincent 0:05ccbd4f84f1 525 /* some compilers can't nest inline assembler.. */
hendrikvincent 0:05ccbd4f84f1 526 tmp = ntohl(th->seqno) + ntohs(IPH_LEN(&cs->cs_ip)) - cs->cs_hlen;
hendrikvincent 0:05ccbd4f84f1 527 th->seqno = htonl(tmp);
hendrikvincent 0:05ccbd4f84f1 528 break;
hendrikvincent 0:05ccbd4f84f1 529
hendrikvincent 0:05ccbd4f84f1 530 default:
hendrikvincent 0:05ccbd4f84f1 531 if (changes & NEW_U) {
hendrikvincent 0:05ccbd4f84f1 532 TCPH_SET_FLAG(th, TCP_URG);
hendrikvincent 0:05ccbd4f84f1 533 DECODEU(th->urgp);
hendrikvincent 0:05ccbd4f84f1 534 } else {
hendrikvincent 0:05ccbd4f84f1 535 TCPH_UNSET_FLAG(th, TCP_URG);
hendrikvincent 0:05ccbd4f84f1 536 }
hendrikvincent 0:05ccbd4f84f1 537 if (changes & NEW_W) {
hendrikvincent 0:05ccbd4f84f1 538 DECODES(th->wnd);
hendrikvincent 0:05ccbd4f84f1 539 }
hendrikvincent 0:05ccbd4f84f1 540 if (changes & NEW_A) {
hendrikvincent 0:05ccbd4f84f1 541 DECODEL(th->ackno);
hendrikvincent 0:05ccbd4f84f1 542 }
hendrikvincent 0:05ccbd4f84f1 543 if (changes & NEW_S) {
hendrikvincent 0:05ccbd4f84f1 544 DECODEL(th->seqno);
hendrikvincent 0:05ccbd4f84f1 545 }
hendrikvincent 0:05ccbd4f84f1 546 break;
hendrikvincent 0:05ccbd4f84f1 547 }
hendrikvincent 0:05ccbd4f84f1 548 if (changes & NEW_I) {
hendrikvincent 0:05ccbd4f84f1 549 DECODES(cs->cs_ip._id);
hendrikvincent 0:05ccbd4f84f1 550 } else {
hendrikvincent 0:05ccbd4f84f1 551 IPH_ID_SET(&cs->cs_ip, ntohs(IPH_ID(&cs->cs_ip)) + 1);
hendrikvincent 0:05ccbd4f84f1 552 IPH_ID_SET(&cs->cs_ip, htons(IPH_ID(&cs->cs_ip)));
hendrikvincent 0:05ccbd4f84f1 553 }
hendrikvincent 0:05ccbd4f84f1 554
hendrikvincent 0:05ccbd4f84f1 555 /*
hendrikvincent 0:05ccbd4f84f1 556 * At this point, cp points to the first byte of data in the
hendrikvincent 0:05ccbd4f84f1 557 * packet. Fill in the IP total length and update the IP
hendrikvincent 0:05ccbd4f84f1 558 * header checksum.
hendrikvincent 0:05ccbd4f84f1 559 */
hendrikvincent 0:05ccbd4f84f1 560 vjlen = (u_short)(cp - (u_char*)n0->payload);
hendrikvincent 0:05ccbd4f84f1 561 if (n0->len < vjlen) {
hendrikvincent 0:05ccbd4f84f1 562 /*
hendrikvincent 0:05ccbd4f84f1 563 * We must have dropped some characters (crc should detect
hendrikvincent 0:05ccbd4f84f1 564 * this but the old slip framing won't)
hendrikvincent 0:05ccbd4f84f1 565 */
hendrikvincent 0:05ccbd4f84f1 566 PPPDEBUG(LOG_INFO, ("vj_uncompress_tcp: head buffer %d too short %d\n",
hendrikvincent 0:05ccbd4f84f1 567 n0->len, vjlen));
hendrikvincent 0:05ccbd4f84f1 568 goto bad;
hendrikvincent 0:05ccbd4f84f1 569 }
hendrikvincent 0:05ccbd4f84f1 570
hendrikvincent 0:05ccbd4f84f1 571 #if BYTE_ORDER == LITTLE_ENDIAN
hendrikvincent 0:05ccbd4f84f1 572 tmp = n0->tot_len - vjlen + cs->cs_hlen;
hendrikvincent 0:05ccbd4f84f1 573 IPH_LEN_SET(&cs->cs_ip, htons((u_short)tmp));
hendrikvincent 0:05ccbd4f84f1 574 #else
hendrikvincent 0:05ccbd4f84f1 575 IPH_LEN_SET(&cs->cs_ip, htons(n0->tot_len - vjlen + cs->cs_hlen));
hendrikvincent 0:05ccbd4f84f1 576 #endif
hendrikvincent 0:05ccbd4f84f1 577
hendrikvincent 0:05ccbd4f84f1 578 /* recompute the ip header checksum */
hendrikvincent 0:05ccbd4f84f1 579 bp = (u_short *) &cs->cs_ip;
hendrikvincent 0:05ccbd4f84f1 580 IPH_CHKSUM_SET(&cs->cs_ip, 0);
hendrikvincent 0:05ccbd4f84f1 581 for (tmp = 0; hlen > 0; hlen -= 2) {
hendrikvincent 0:05ccbd4f84f1 582 tmp += *bp++;
hendrikvincent 0:05ccbd4f84f1 583 }
hendrikvincent 0:05ccbd4f84f1 584 tmp = (tmp & 0xffff) + (tmp >> 16);
hendrikvincent 0:05ccbd4f84f1 585 tmp = (tmp & 0xffff) + (tmp >> 16);
hendrikvincent 0:05ccbd4f84f1 586 IPH_CHKSUM_SET(&cs->cs_ip, (u_short)(~tmp));
hendrikvincent 0:05ccbd4f84f1 587
hendrikvincent 0:05ccbd4f84f1 588 /* Remove the compressed header and prepend the uncompressed header. */
hendrikvincent 0:05ccbd4f84f1 589 if(pbuf_header(n0, -((s16_t)(vjlen)))) {
hendrikvincent 0:05ccbd4f84f1 590 /* Can we cope with this failing? Just assert for now */
hendrikvincent 0:05ccbd4f84f1 591 LWIP_ASSERT("pbuf_header failed\n", 0);
hendrikvincent 0:05ccbd4f84f1 592 goto bad;
hendrikvincent 0:05ccbd4f84f1 593 }
hendrikvincent 0:05ccbd4f84f1 594
hendrikvincent 0:05ccbd4f84f1 595 if(LWIP_MEM_ALIGN(n0->payload) != n0->payload) {
hendrikvincent 0:05ccbd4f84f1 596 struct pbuf *np, *q;
hendrikvincent 0:05ccbd4f84f1 597 u8_t *bufptr;
hendrikvincent 0:05ccbd4f84f1 598
hendrikvincent 0:05ccbd4f84f1 599 np = pbuf_alloc(PBUF_RAW, n0->len + cs->cs_hlen, PBUF_POOL);
hendrikvincent 0:05ccbd4f84f1 600 if(!np) {
hendrikvincent 0:05ccbd4f84f1 601 PPPDEBUG(LOG_WARNING, ("vj_uncompress_tcp: realign failed\n"));
hendrikvincent 0:05ccbd4f84f1 602 goto bad;
hendrikvincent 0:05ccbd4f84f1 603 }
hendrikvincent 0:05ccbd4f84f1 604
hendrikvincent 0:05ccbd4f84f1 605 if(pbuf_header(np, -cs->cs_hlen)) {
hendrikvincent 0:05ccbd4f84f1 606 /* Can we cope with this failing? Just assert for now */
hendrikvincent 0:05ccbd4f84f1 607 LWIP_ASSERT("pbuf_header failed\n", 0);
hendrikvincent 0:05ccbd4f84f1 608 goto bad;
hendrikvincent 0:05ccbd4f84f1 609 }
hendrikvincent 0:05ccbd4f84f1 610
hendrikvincent 0:05ccbd4f84f1 611 bufptr = (u8_t *)n0->payload;
hendrikvincent 0:05ccbd4f84f1 612 for(q = np; q != NULL; q = q->next) {
hendrikvincent 0:05ccbd4f84f1 613 MEMCPY(q->payload, bufptr, q->len);
hendrikvincent 0:05ccbd4f84f1 614 bufptr += q->len;
hendrikvincent 0:05ccbd4f84f1 615 }
hendrikvincent 0:05ccbd4f84f1 616
hendrikvincent 0:05ccbd4f84f1 617 if(n0->next) {
hendrikvincent 0:05ccbd4f84f1 618 pbuf_chain(np, n0->next);
hendrikvincent 0:05ccbd4f84f1 619 pbuf_dechain(n0);
hendrikvincent 0:05ccbd4f84f1 620 }
hendrikvincent 0:05ccbd4f84f1 621 pbuf_free(n0);
hendrikvincent 0:05ccbd4f84f1 622 n0 = np;
hendrikvincent 0:05ccbd4f84f1 623 }
hendrikvincent 0:05ccbd4f84f1 624
hendrikvincent 0:05ccbd4f84f1 625 if(pbuf_header(n0, cs->cs_hlen)) {
hendrikvincent 0:05ccbd4f84f1 626 struct pbuf *np;
hendrikvincent 0:05ccbd4f84f1 627
hendrikvincent 0:05ccbd4f84f1 628 LWIP_ASSERT("vj_uncompress_tcp: cs->cs_hlen <= PBUF_POOL_BUFSIZE", cs->cs_hlen <= PBUF_POOL_BUFSIZE);
hendrikvincent 0:05ccbd4f84f1 629 np = pbuf_alloc(PBUF_RAW, cs->cs_hlen, PBUF_POOL);
hendrikvincent 0:05ccbd4f84f1 630 if(!np) {
hendrikvincent 0:05ccbd4f84f1 631 PPPDEBUG(LOG_WARNING, ("vj_uncompress_tcp: prepend failed\n"));
hendrikvincent 0:05ccbd4f84f1 632 goto bad;
hendrikvincent 0:05ccbd4f84f1 633 }
hendrikvincent 0:05ccbd4f84f1 634 pbuf_cat(np, n0);
hendrikvincent 0:05ccbd4f84f1 635 n0 = np;
hendrikvincent 0:05ccbd4f84f1 636 }
hendrikvincent 0:05ccbd4f84f1 637 LWIP_ASSERT("n0->len >= cs->cs_hlen", n0->len >= cs->cs_hlen);
hendrikvincent 0:05ccbd4f84f1 638 MEMCPY(n0->payload, &cs->cs_ip, cs->cs_hlen);
hendrikvincent 0:05ccbd4f84f1 639
hendrikvincent 0:05ccbd4f84f1 640 *nb = n0;
hendrikvincent 0:05ccbd4f84f1 641
hendrikvincent 0:05ccbd4f84f1 642 return vjlen;
hendrikvincent 0:05ccbd4f84f1 643
hendrikvincent 0:05ccbd4f84f1 644 bad:
hendrikvincent 0:05ccbd4f84f1 645 comp->flags |= VJF_TOSS;
hendrikvincent 0:05ccbd4f84f1 646 INCR(vjs_errorin);
hendrikvincent 0:05ccbd4f84f1 647 return (-1);
hendrikvincent 0:05ccbd4f84f1 648 }
hendrikvincent 0:05ccbd4f84f1 649
hendrikvincent 0:05ccbd4f84f1 650 #endif /* VJ_SUPPORT */
hendrikvincent 0:05ccbd4f84f1 651
hendrikvincent 0:05ccbd4f84f1 652 #endif /* PPP_SUPPORT */