vjcomp.c revision 25561
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.6 1997/02/22 16:11:05 peter Exp $
21 *
22 *  TODO:
23 */
24#include "fsm.h"
25#include "lcpproto.h"
26#include <netinet/in_systm.h>
27#include <netinet/ip.h>
28#include "slcompress.h"
29#include "hdlc.h"
30#include "ipcp.h"
31
32#define MAX_VJHEADER 16	/* Maximum size of compressed header */
33
34struct slcompress cslc;
35
36void
37VjInit()
38{
39  sl_compress_init(&cslc);
40}
41
42void
43SendPppFrame(bp)
44struct mbuf *bp;
45{
46  int type;
47  int proto;
48  int cproto = IpcpInfo.his_compproto >> 16;
49
50#ifdef DEBUG
51  logprintf("SendPppFrame: proto = %x\n", IpcpInfo.his_compproto);
52#endif
53  if (((struct ip *)MBUF_CTOP(bp))->ip_p == IPPROTO_TCP
54      && cproto== PROTO_VJCOMP) {
55    type = sl_compress_tcp(bp, (struct ip *)MBUF_CTOP(bp), &cslc, IpcpInfo.his_compproto & 0xff);
56
57#ifdef DEBUG
58    logprintf("type = %x\n", type);
59#endif
60    switch (type) {
61    case TYPE_IP:
62      proto = PROTO_IP;
63      break;
64    case TYPE_UNCOMPRESSED_TCP:
65      proto = PROTO_VJUNCOMP;
66      break;
67    case TYPE_COMPRESSED_TCP:
68      proto = PROTO_VJCOMP;
69      break;
70    default:
71      logprintf("unknown type %x\n", type);
72      pfree(bp);
73      return;
74    }
75  } else
76    proto = PROTO_IP;
77  HdlcOutput(PRI_NORMAL, proto, bp);
78}
79
80static struct mbuf *
81VjUncompressTcp(bp, type)
82struct mbuf *bp;
83u_char type;
84{
85  u_char *bufp;
86  int len, olen, rlen;
87  struct mbuf *nbp;
88  u_char work[MAX_HDR+MAX_VJHEADER];   /* enough to hold TCP/IP header */
89
90  olen = len = plength(bp);
91  if (type == TYPE_UNCOMPRESSED_TCP) {
92    /*
93     * Uncompressed packet does NOT change its size, so that we can
94     * use mbuf space for uncompression job.
95     */
96    bufp = MBUF_CTOP(bp);
97    len = sl_uncompress_tcp(&bufp, len, type, &cslc);
98    if (len <= 0) {
99      pfree(bp);
100      bp = NULLBUFF;
101    }
102    return(bp);
103  }
104  /*
105   *  Handle compressed packet.
106   *    1) Read upto MAX_VJHEADER bytes into work space.
107   *	2) Try to uncompress it.
108   *    3) Compute amount of necesary space.
109   *    4) Copy unread data info there.
110   */
111  if (len > MAX_VJHEADER) len = MAX_VJHEADER;
112  rlen = len;
113  bufp = work + MAX_HDR;
114  bp = mbread(bp, bufp, rlen);
115  len = sl_uncompress_tcp(&bufp, olen, type, &cslc);
116  if (len <= 0) {
117    pfree(bp);
118    return NULLBUFF;
119  }
120  len -= olen;
121  len += rlen;
122  nbp = mballoc(len, MB_VJCOMP);
123  bcopy(bufp, MBUF_CTOP(nbp), len);
124  nbp->next = bp;
125  return(nbp);
126}
127
128struct mbuf *
129VjCompInput(bp, proto)
130struct mbuf *bp;
131int proto;
132{
133  u_char type;
134
135#ifdef DEBUG
136  logprintf("VjCompInput (%02x):\n", proto);
137  DumpBp(bp);
138#endif
139
140  switch (proto) {
141  case PROTO_VJCOMP:
142    type = TYPE_COMPRESSED_TCP;
143    break;
144  case PROTO_VJUNCOMP:
145    type = TYPE_UNCOMPRESSED_TCP;
146    break;
147  default:
148    logprintf("???\n");
149    return(bp);
150  }
151  bp = VjUncompressTcp(bp, type);
152  return(bp);
153}
154