vjcomp.c revision 6735
16059Samurai/*
26059Samurai *	       Input/Output VJ Compressed packets
36059Samurai *
46059Samurai *	    Written by Toshiharu OHNO (tony-o@iij.ad.jp)
56059Samurai *
66059Samurai *   Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
76059Samurai *
86059Samurai * Redistribution and use in source and binary forms are permitted
96059Samurai * provided that the above copyright notice and this paragraph are
106059Samurai * duplicated in all such forms and that any documentation,
116059Samurai * advertising materials, and other materials related to such
126059Samurai * distribution and use acknowledge that the software was developed
136059Samurai * by the Internet Initiative Japan, Inc.  The name of the
146059Samurai * IIJ may not be used to endorse or promote products derived
156059Samurai * from this software without specific prior written permission.
166059Samurai * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
176059Samurai * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
186059Samurai * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
196059Samurai *
206059Samurai * $Id:$
216735Samurai *
226059Samurai *  TODO:
236059Samurai */
246059Samurai#include "fsm.h"
256059Samurai#include "lcpproto.h"
266059Samurai#include <netinet/in_systm.h>
276059Samurai#include <netinet/ip.h>
286059Samurai#include "slcompress.h"
296059Samurai#include "hdlc.h"
306059Samurai#include "ipcp.h"
316059Samurai
326059Samurai#define MAX_VJHEADER 16	/* Maximum size of compressed header */
336059Samurai
346059Samuraistruct slcompress cslc;
356059Samurai
366059Samuraivoid
376059SamuraiVjInit()
386059Samurai{
396059Samurai  sl_compress_init(&cslc);
406059Samurai}
416059Samurai
426059Samuraivoid
436735SamuraiSendPppFrame(pri, bp)
446059Samuraiint pri;
456059Samuraistruct mbuf *bp;
466059Samurai{
476059Samurai  int type;
486059Samurai  int proto;
496059Samurai  int cproto = IpcpInfo.his_compproto >> 16;
506059Samurai
516059Samurai#ifdef DEBUG
526735Samurai  logprintf("SendPppFrame: proto = %x\n", IpcpInfo.his_compproto);
536059Samurai#endif
546059Samurai  if (cproto== PROTO_VJCOMP) {
556735Samurai    type = sl_compress_tcp(bp, (struct ip *)MBUF_CTOP(bp), &cslc, IpcpInfo.his_compproto & 0xff);
566059Samurai
576059Samurai#ifdef DEBUG
586059Samurai    logprintf("type = %x\n", type);
596059Samurai#endif
606059Samurai    switch (type) {
616059Samurai    case TYPE_IP:
626059Samurai      proto = PROTO_IP;
636059Samurai      break;
646059Samurai    case TYPE_UNCOMPRESSED_TCP:
656059Samurai      proto = PROTO_VJUNCOMP;
666059Samurai      break;
676059Samurai    case TYPE_COMPRESSED_TCP:
686059Samurai      proto = PROTO_VJCOMP;
696059Samurai      break;
706059Samurai    default:
716059Samurai      logprintf("unknown type %x\n", type);
726059Samurai      pfree(bp);
736059Samurai      return;
746059Samurai    }
756059Samurai  } else
766059Samurai    proto = PROTO_IP;
776059Samurai  HdlcOutput(pri, proto, bp);
786059Samurai}
796059Samurai
806059Samuraistatic struct mbuf *
816059SamuraiVjUncompressTcp(bp, type)
826059Samuraistruct mbuf *bp;
836059Samuraiu_char type;
846059Samurai{
856059Samurai  u_char *bufp;
866059Samurai  int len, olen, rlen;
876059Samurai  struct mbuf *nbp;
886059Samurai  u_char work[MAX_HDR+MAX_VJHEADER];   /* enough to hold TCP/IP header */
896059Samurai
906059Samurai  olen = len = plength(bp);
916059Samurai  if (type == TYPE_UNCOMPRESSED_TCP) {
926059Samurai    /*
936059Samurai     * Uncompressed packet does NOT change its size, so that we can
946059Samurai     * use mbuf space for uncompression job.
956059Samurai     */
966059Samurai    bufp = MBUF_CTOP(bp);
976059Samurai    len = sl_uncompress_tcp(&bufp, len, type, &cslc);
986735Samurai    if (len <= 0) {
996735Samurai      pfree(bp);
1006735Samurai      bp = NULLBUFF;
1016735Samurai    }
1026059Samurai    return(bp);
1036059Samurai  }
1046059Samurai  /*
1056059Samurai   *  Handle compressed packet.
1066059Samurai   *    1) Read upto MAX_VJHEADER bytes into work space.
1076059Samurai   *	2) Try to uncompress it.
1086059Samurai   *    3) Compute amount of necesary space.
1096059Samurai   *    4) Copy unread data info there.
1106059Samurai   */
1116059Samurai  if (len > MAX_VJHEADER) len = MAX_VJHEADER;
1126059Samurai  rlen = len;
1136059Samurai  bufp = work + MAX_HDR;
1146059Samurai  bp = mbread(bp, bufp, rlen);
1156059Samurai  len = sl_uncompress_tcp(&bufp, olen, type, &cslc);
1166735Samurai  if (len <= 0) {
1176735Samurai    pfree(bp);
1186735Samurai    return NULLBUFF;
1196735Samurai  }
1206059Samurai  len -= olen;
1216059Samurai  len += rlen;
1226059Samurai  nbp = mballoc(len, MB_VJCOMP);
1236059Samurai  bcopy(bufp, MBUF_CTOP(nbp), len);
1246059Samurai  nbp->next = bp;
1256059Samurai  return(nbp);
1266059Samurai}
1276059Samurai
1286059Samuraistruct mbuf *
1296059SamuraiVjCompInput(bp, proto)
1306059Samuraistruct mbuf *bp;
1316059Samuraiint proto;
1326059Samurai{
1336059Samurai  u_char type;
1346059Samurai
1356059Samurai#ifdef DEBUG
1366059Samurai  logprintf("VjCompInput (%02x):\n", proto);
1376059Samurai  DumpBp(bp);
1386059Samurai#endif
1396059Samurai
1406059Samurai  switch (proto) {
1416059Samurai  case PROTO_VJCOMP:
1426059Samurai    type = TYPE_COMPRESSED_TCP;
1436059Samurai    break;
1446059Samurai  case PROTO_VJUNCOMP:
1456059Samurai    type = TYPE_UNCOMPRESSED_TCP;
1466059Samurai    break;
1476059Samurai  default:
1486059Samurai    logprintf("???\n");
1496059Samurai    return(bp);
1506059Samurai  }
1516059Samurai  bp = VjUncompressTcp(bp, type);
1526059Samurai  return(bp);
1536059Samurai}
154