vjcomp.c revision 32663
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.15 1998/01/11 17:50:46 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 "timer.h"
36#include "fsm.h"
37#include "lcpproto.h"
38#include "slcompress.h"
39#include "hdlc.h"
40#include "ipcp.h"
41#include "vjcomp.h"
42
43#define MAX_VJHEADER 16		/* Maximum size of compressed header */
44
45static struct slcompress cslc;
46
47void
48VjInit(int max_state)
49{
50  sl_compress_init(&cslc, max_state);
51}
52
53void
54SendPppFrame(struct mbuf * bp)
55{
56  int type;
57  u_short proto;
58  u_short cproto = IpcpInfo.his_compproto >> 16;
59
60  LogPrintf(LogDEBUG, "SendPppFrame: proto = %x\n", IpcpInfo.his_compproto);
61  if (((struct ip *) MBUF_CTOP(bp))->ip_p == IPPROTO_TCP
62      && cproto == PROTO_VJCOMP) {
63    type = sl_compress_tcp(bp, (struct ip *)MBUF_CTOP(bp), &cslc,
64                           IpcpInfo.his_compproto & 0xff);
65    LogPrintf(LogDEBUG, "SendPppFrame: type = %x\n", type);
66    switch (type) {
67    case TYPE_IP:
68      proto = PROTO_IP;
69      break;
70    case TYPE_UNCOMPRESSED_TCP:
71      proto = PROTO_VJUNCOMP;
72      break;
73    case TYPE_COMPRESSED_TCP:
74      proto = PROTO_VJCOMP;
75      break;
76    default:
77      LogPrintf(LogERROR, "Unknown frame type %x\n", type);
78      pfree(bp);
79      return;
80    }
81  } else
82    proto = PROTO_IP;
83  HdlcOutput(PRI_NORMAL, proto, bp);
84}
85
86static struct mbuf *
87VjUncompressTcp(struct mbuf * bp, u_char type)
88{
89  u_char *bufp;
90  int len, olen, rlen;
91  struct mbuf *nbp;
92  u_char work[MAX_HDR + MAX_VJHEADER];	/* enough to hold TCP/IP header */
93
94  olen = len = plength(bp);
95  if (type == TYPE_UNCOMPRESSED_TCP) {
96
97    /*
98     * Uncompressed packet does NOT change its size, so that we can use mbuf
99     * space for uncompression job.
100     */
101    bufp = MBUF_CTOP(bp);
102    len = sl_uncompress_tcp(&bufp, len, type, &cslc);
103    if (len <= 0) {
104      pfree(bp);
105      bp = NULL;
106    }
107    return (bp);
108  }
109
110  /*
111   * Handle compressed packet. 1) Read upto MAX_VJHEADER bytes into work
112   * space. 2) Try to uncompress it. 3) Compute amount of necesary space. 4)
113   * Copy unread data info there.
114   */
115  if (len > MAX_VJHEADER)
116    len = MAX_VJHEADER;
117  rlen = len;
118  bufp = work + MAX_HDR;
119  bp = mbread(bp, bufp, rlen);
120  len = sl_uncompress_tcp(&bufp, olen, type, &cslc);
121  if (len <= 0) {
122    pfree(bp);
123    return NULL;
124  }
125  len -= olen;
126  len += rlen;
127  nbp = mballoc(len, MB_VJCOMP);
128  memcpy(MBUF_CTOP(nbp), bufp, len);
129  nbp->next = bp;
130  return (nbp);
131}
132
133struct mbuf *
134VjCompInput(struct mbuf * bp, int proto)
135{
136  u_char type;
137
138  LogPrintf(LogDEBUG, "VjCompInput: proto %02x\n", proto);
139  LogDumpBp(LogDEBUG, "Raw packet info:", bp);
140
141  switch (proto) {
142  case PROTO_VJCOMP:
143    type = TYPE_COMPRESSED_TCP;
144    break;
145  case PROTO_VJUNCOMP:
146    type = TYPE_UNCOMPRESSED_TCP;
147    break;
148  default:
149    LogPrintf(LogERROR, "VjCompInput...???\n");
150    return (bp);
151  }
152  bp = VjUncompressTcp(bp, type);
153  return (bp);
154}
155
156const char *
157vj2asc(u_int32_t val)
158{
159  static char asc[50];
160
161  sprintf(asc, "%d VJ slots %s slot compression",
162          (int)((val>>8)&15)+1, val & 1 ?  "with" : "without");
163  return asc;
164}
165