I have a problem getting this to work. Server only recieves half of the data being sent. Whats wrong

Dependencies:   mbed

Committer:
tax
Date:
Tue Mar 29 13:20:15 2011 +0000
Revision:
0:66300c77c6e9

        

Who changed what in which revision?

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