pred.c revision 54912
1/*- 2 * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org> 3 * Ian Donaldson <iand@labtam.labtam.oz.au> 4 * Carsten Bormann <cabo@cs.tu-berlin.de> 5 * Dave Rand <dlr@bungi.com>/<dave_rand@novell.com> 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD: head/usr.sbin/ppp/pred.c 54912 1999-12-20 20:29:47Z brian $ 30 */ 31 32#include <sys/types.h> 33 34#include <stdlib.h> 35#include <string.h> 36#include <termios.h> 37 38#include "defs.h" 39#include "layer.h" 40#include "mbuf.h" 41#include "log.h" 42#include "timer.h" 43#include "fsm.h" 44#include "lqr.h" 45#include "hdlc.h" 46#include "lcp.h" 47#include "ccp.h" 48#include "throughput.h" 49#include "link.h" 50#include "pred.h" 51 52/* The following hash code is the heart of the algorithm: 53 * It builds a sliding hash sum of the previous 3-and-a-bit characters 54 * which will be used to index the guess table. 55 * A better hash function would result in additional compression, 56 * at the expense of time. 57 */ 58#define HASH(state, x) state->hash = (state->hash << 4) ^ (x) 59#define GUESS_TABLE_SIZE 65536 60 61struct pred1_state { 62 u_short hash; 63 u_char dict[GUESS_TABLE_SIZE]; 64}; 65 66static int 67compress(struct pred1_state *state, u_char *source, u_char *dest, int len) 68{ 69 int i, bitmask; 70 unsigned char *flagdest, flags, *orgdest; 71 72 orgdest = dest; 73 while (len) { 74 flagdest = dest++; 75 flags = 0; /* All guess wrong initially */ 76 for (bitmask = 1, i = 0; i < 8 && len; i++, bitmask <<= 1) { 77 if (state->dict[state->hash] == *source) { 78 flags |= bitmask; /* Guess was right - don't output */ 79 } else { 80 state->dict[state->hash] = *source; 81 *dest++ = *source; /* Guess wrong, output char */ 82 } 83 HASH(state, *source++); 84 len--; 85 } 86 *flagdest = flags; 87 } 88 return (dest - orgdest); 89} 90 91static void 92SyncTable(struct pred1_state *state, u_char *source, u_char *dest, int len) 93{ 94 while (len--) { 95 *dest++ = state->dict[state->hash] = *source; 96 HASH(state, *source++); 97 } 98} 99 100static int 101decompress(struct pred1_state *state, u_char *source, u_char *dest, int len) 102{ 103 int i, bitmask; 104 unsigned char flags, *orgdest; 105 106 orgdest = dest; 107 while (len) { 108 flags = *source++; 109 len--; 110 for (i = 0, bitmask = 1; i < 8; i++, bitmask <<= 1) { 111 if (flags & bitmask) { 112 *dest = state->dict[state->hash]; /* Guess correct */ 113 } else { 114 if (!len) 115 break; /* we seem to be really done -- cabo */ 116 state->dict[state->hash] = *source; /* Guess wrong */ 117 *dest = *source++; /* Read from source */ 118 len--; 119 } 120 HASH(state, *dest++); 121 } 122 } 123 return (dest - orgdest); 124} 125 126static void 127Pred1Term(void *v) 128{ 129 struct pred1_state *state = (struct pred1_state *)v; 130 free(state); 131} 132 133static void 134Pred1ResetInput(void *v) 135{ 136 struct pred1_state *state = (struct pred1_state *)v; 137 state->hash = 0; 138 memset(state->dict, '\0', sizeof state->dict); 139 log_Printf(LogCCP, "Predictor1: Input channel reset\n"); 140} 141 142static void 143Pred1ResetOutput(void *v) 144{ 145 struct pred1_state *state = (struct pred1_state *)v; 146 state->hash = 0; 147 memset(state->dict, '\0', sizeof state->dict); 148 log_Printf(LogCCP, "Predictor1: Output channel reset\n"); 149} 150 151static void * 152Pred1InitInput(struct lcp_opt *o) 153{ 154 struct pred1_state *state; 155 state = (struct pred1_state *)malloc(sizeof(struct pred1_state)); 156 if (state != NULL) 157 Pred1ResetInput(state); 158 return state; 159} 160 161static void * 162Pred1InitOutput(struct lcp_opt *o) 163{ 164 struct pred1_state *state; 165 state = (struct pred1_state *)malloc(sizeof(struct pred1_state)); 166 if (state != NULL) 167 Pred1ResetOutput(state); 168 return state; 169} 170 171static struct mbuf * 172Pred1Output(void *v, struct ccp *ccp, struct link *l, int pri, u_short *proto, 173 struct mbuf *bp) 174{ 175 struct pred1_state *state = (struct pred1_state *)v; 176 struct mbuf *mwp; 177 u_char *cp, *wp, *hp; 178 int orglen, len; 179 u_char bufp[MAX_MTU + 2]; 180 u_short fcs; 181 182 orglen = m_length(bp) + 2; /* add count of proto */ 183 mwp = m_get((orglen + 2) / 8 * 9 + 12, MB_CCPOUT); 184 hp = wp = MBUF_CTOP(mwp); 185 cp = bufp; 186 *wp++ = *cp++ = orglen >> 8; 187 *wp++ = *cp++ = orglen & 0377; 188 *cp++ = *proto >> 8; 189 *cp++ = *proto & 0377; 190 mbuf_Read(bp, cp, orglen - 2); 191 fcs = hdlc_Fcs(bufp, 2 + orglen); 192 fcs = ~fcs; 193 194 len = compress(state, bufp + 2, wp, orglen); 195 log_Printf(LogDEBUG, "Pred1Output: orglen (%d) --> len (%d)\n", orglen, len); 196 ccp->uncompout += orglen; 197 if (len < orglen) { 198 *hp |= 0x80; 199 wp += len; 200 ccp->compout += len; 201 } else { 202 memcpy(wp, bufp + 2, orglen); 203 wp += orglen; 204 ccp->compout += orglen; 205 } 206 207 *wp++ = fcs & 0377; 208 *wp++ = fcs >> 8; 209 mwp->m_len = wp - MBUF_CTOP(mwp); 210 *proto = ccp_Proto(ccp); 211 return mwp; 212} 213 214static struct mbuf * 215Pred1Input(void *v, struct ccp *ccp, u_short *proto, struct mbuf *bp) 216{ 217 struct pred1_state *state = (struct pred1_state *)v; 218 u_char *cp, *pp; 219 int len, olen, len1; 220 struct mbuf *wp; 221 u_char *bufp; 222 u_short fcs; 223 224 wp = m_get(MAX_MRU + 2, MB_CCPIN); 225 cp = MBUF_CTOP(bp); 226 olen = m_length(bp); 227 pp = bufp = MBUF_CTOP(wp); 228 *pp++ = *cp & 0177; 229 len = *cp++ << 8; 230 *pp++ = *cp; 231 len += *cp++; 232 ccp->uncompin += len & 0x7fff; 233 if (len & 0x8000) { 234 len1 = decompress(state, cp, pp, olen - 4); 235 ccp->compin += olen; 236 len &= 0x7fff; 237 if (len != len1) { /* Error is detected. Send reset request */ 238 log_Printf(LogCCP, "Pred1: Length error (got %d, not %d)\n", len1, len); 239 fsm_Reopen(&ccp->fsm); 240 m_freem(bp); 241 m_freem(wp); 242 return NULL; 243 } 244 cp += olen - 4; 245 pp += len1; 246 } else if (len + 4 != olen) { 247 log_Printf(LogCCP, "Pred1: Length error (got %d, not %d)\n", len + 4, olen); 248 fsm_Reopen(&ccp->fsm); 249 m_freem(wp); 250 m_freem(bp); 251 return NULL; 252 } else { 253 ccp->compin += len; 254 SyncTable(state, cp, pp, len); 255 cp += len; 256 pp += len; 257 } 258 *pp++ = *cp++; /* CRC */ 259 *pp++ = *cp++; 260 fcs = hdlc_Fcs(bufp, wp->m_len = pp - bufp); 261 if (fcs == GOODFCS) { 262 wp->m_offset += 2; /* skip length */ 263 wp->m_len -= 4; /* skip length & CRC */ 264 pp = MBUF_CTOP(wp); 265 *proto = *pp++; 266 if (*proto & 1) { 267 wp->m_offset++; 268 wp->m_len--; 269 } else { 270 wp->m_offset += 2; 271 wp->m_len -= 2; 272 *proto = (*proto << 8) | *pp++; 273 } 274 m_freem(bp); 275 return wp; 276 } else { 277 const char *pre = *MBUF_CTOP(bp) & 0x80 ? "" : "un"; 278 log_Printf(LogDEBUG, "Pred1Input: fcs = 0x%04x (%scompressed), len = 0x%x," 279 " olen = 0x%x\n", fcs, pre, len, olen); 280 log_Printf(LogCCP, "%s: Bad %scompressed CRC-16\n", 281 ccp->fsm.link->name, pre); 282 fsm_Reopen(&ccp->fsm); 283 m_freem(wp); 284 } 285 m_freem(bp); 286 return NULL; 287} 288 289static void 290Pred1DictSetup(void *v, struct ccp *ccp, u_short proto, struct mbuf *bp) 291{ 292} 293 294static const char * 295Pred1DispOpts(struct lcp_opt *o) 296{ 297 return NULL; 298} 299 300static void 301Pred1InitOptsOutput(struct lcp_opt *o, const struct ccp_config *cfg) 302{ 303 o->len = 2; 304} 305 306static int 307Pred1SetOptsOutput(struct lcp_opt *o) 308{ 309 if (o->len != 2) { 310 o->len = 2; 311 return MODE_NAK; 312 } 313 return MODE_ACK; 314} 315 316static int 317Pred1SetOptsInput(struct lcp_opt *o, const struct ccp_config *cfg) 318{ 319 return Pred1SetOptsOutput(o); 320} 321 322const struct ccp_algorithm Pred1Algorithm = { 323 TY_PRED1, 324 CCP_NEG_PRED1, 325 Pred1DispOpts, 326 { 327 Pred1SetOptsInput, 328 Pred1InitInput, 329 Pred1Term, 330 Pred1ResetInput, 331 Pred1Input, 332 Pred1DictSetup 333 }, 334 { 335 Pred1InitOptsOutput, 336 Pred1SetOptsOutput, 337 Pred1InitOutput, 338 Pred1Term, 339 Pred1ResetOutput, 340 Pred1Output 341 }, 342}; 343