Example self-announcing webserver which controls a servo through a smallHTML userinterface.

Dependencies:   mbed

Committer:
dirkx
Date:
Sat Aug 14 15:56:01 2010 +0000
Revision:
0:a259777c45a3

        

Who changed what in which revision?

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