vjcomp.c revision 32439
1/*
2 *	       Input/Output VJ Compressed packets
3 *
4 *	    Written by Toshiharu OHNO (tony-o@iij.ad.jp)
5 *
6 *   Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
7 *
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by the Internet Initiative Japan, Inc.  The name of the
14 * IIJ may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 *
20 * $Id: vjcomp.c,v 1.14 1997/12/03 10:23:54 brian Exp $
21 *
22 *  TODO:
23 */
24#include <sys/types.h>
25#include <netinet/in.h>
26#include <netinet/in_systm.h>
27#include <netinet/ip.h>
28
29#include <stdio.h>
30#include <string.h>
31
32#include "command.h"
33#include "mbuf.h"
34#include "log.h"
35#include "defs.h"
36#include "timer.h"
37#include "fsm.h"
38#include "lcpproto.h"
39#include "slcompress.h"
40#include "hdlc.h"
41#include "ipcp.h"
42#include "vjcomp.h"
43
44#define MAX_VJHEADER 16		/* Maximum size of compressed header */
45
46struct slcompress cslc;
47
48void
49VjInit(int max_state)
50{
51  sl_compress_init(&cslc, max_state);
52}
53
54void
55SendPppFrame(struct mbuf * bp)
56{
57  int type;
58  u_short proto;
59  u_short cproto = IpcpInfo.his_compproto >> 16;
60
61  LogPrintf(LogDEBUG, "SendPppFrame: proto = %x\n", IpcpInfo.his_compproto);
62  if (((struct ip *) MBUF_CTOP(bp))->ip_p == IPPROTO_TCP
63      && cproto == PROTO_VJCOMP) {
64    type = sl_compress_tcp(bp, (struct ip *)MBUF_CTOP(bp), &cslc,
65                           IpcpInfo.his_compproto & 0xff);
66    LogPrintf(LogDEBUG, "SendPppFrame: type = %x\n", type);
67    switch (type) {
68    case TYPE_IP:
69      proto = PROTO_IP;
70      break;
71    case TYPE_UNCOMPRESSED_TCP:
72      proto = PROTO_VJUNCOMP;
73      break;
74    case TYPE_COMPRESSED_TCP:
75      proto = PROTO_VJCOMP;
76      break;
77    default:
78      LogPrintf(LogERROR, "Unknown frame type %x\n", type);
79      pfree(bp);
80      return;
81    }
82  } else
83    proto = PROTO_IP;
84  HdlcOutput(PRI_NORMAL, proto, bp);
85}
86
87static struct mbuf *
88VjUncompressTcp(struct mbuf * bp, u_char type)
89{
90  u_char *bufp;
91  int len, olen, rlen;
92  struct mbuf *nbp;
93  u_char work[MAX_HDR + MAX_VJHEADER];	/* enough to hold TCP/IP header */
94
95  olen = len = plength(bp);
96  if (type == TYPE_UNCOMPRESSED_TCP) {
97
98    /*
99     * Uncompressed packet does NOT change its size, so that we can use mbuf
100     * space for uncompression job.
101     */
102    bufp = MBUF_CTOP(bp);
103    len = sl_uncompress_tcp(&bufp, len, type, &cslc);
104    if (len <= 0) {
105      pfree(bp);
106      bp = NULLBUFF;
107    }
108    return (bp);
109  }
110
111  /*
112   * Handle compressed packet. 1) Read upto MAX_VJHEADER bytes into work
113   * space. 2) Try to uncompress it. 3) Compute amount of necesary space. 4)
114   * Copy unread data info there.
115   */
116  if (len > MAX_VJHEADER)
117    len = MAX_VJHEADER;
118  rlen = len;
119  bufp = work + MAX_HDR;
120  bp = mbread(bp, bufp, rlen);
121  len = sl_uncompress_tcp(&bufp, olen, type, &cslc);
122  if (len <= 0) {
123    pfree(bp);
124    return NULLBUFF;
125  }
126  len -= olen;
127  len += rlen;
128  nbp = mballoc(len, MB_VJCOMP);
129  memcpy(MBUF_CTOP(nbp), bufp, len);
130  nbp->next = bp;
131  return (nbp);
132}
133
134struct mbuf *
135VjCompInput(struct mbuf * bp, int proto)
136{
137  u_char type;
138
139  LogPrintf(LogDEBUG, "VjCompInput: proto %02x\n", proto);
140  LogDumpBp(LogDEBUG, "Raw packet info:", bp);
141
142  switch (proto) {
143  case PROTO_VJCOMP:
144    type = TYPE_COMPRESSED_TCP;
145    break;
146  case PROTO_VJUNCOMP:
147    type = TYPE_UNCOMPRESSED_TCP;
148    break;
149  default:
150    LogPrintf(LogERROR, "VjCompInput...???\n");
151    return (bp);
152  }
153  bp = VjUncompressTcp(bp, type);
154  return (bp);
155}
156
157const char *
158vj2asc(u_int32_t val)
159{
160  static char asc[50];
161
162  sprintf(asc, "%d VJ slots %s slot compression",
163          (int)((val>>8)&15)+1, val & 1 ?  "with" : "without");
164  return asc;
165}
166