Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

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