HTTPClient using static IP

Dependencies:   mbed

Committer:
mr_q
Date:
Mon May 30 11:53:37 2011 +0000
Revision:
0:d8f2f7d5f31b
v0.01 Draft

Who changed what in which revision?

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