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