1997-12-21 12:11:13 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1997 Brian Somers <brian@Awfulhak.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1999-08-28 01:35:59 +00:00
|
|
|
* $FreeBSD$
|
1997-12-03 10:23:54 +00:00
|
|
|
*/
|
|
|
|
|
1998-04-07 00:54:26 +00:00
|
|
|
#include <sys/types.h>
|
1997-12-03 10:23:54 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <zlib.h>
|
|
|
|
|
|
|
|
#include "mbuf.h"
|
|
|
|
#include "log.h"
|
1998-01-30 01:33:46 +00:00
|
|
|
#include "timer.h"
|
1998-01-31 02:48:30 +00:00
|
|
|
#include "fsm.h"
|
1997-12-03 10:23:54 +00:00
|
|
|
#include "ccp.h"
|
|
|
|
#include "deflate.h"
|
|
|
|
|
|
|
|
/* Our state */
|
|
|
|
struct deflate_state {
|
|
|
|
u_short seqno;
|
Implement Reset{Req,Ack} properly, as per rfc 1962.
(I completely mis-read the rfc last time 'round!)
This means:
o Better CCP/WARN Reset diagnostics.
o After we've sent a REQ and before we've received an ACK, we drop
incoming compressed data and send another REQ.
o Before sending an ACK, re-sequence all pending PRI_NORMAL data in
the modem queue so that pending packets won't get to the peer
*after* the ResetAck.
o Send ACKs with the `identifier' from the REQ frame.
o After we've received a correct ACK, duplicate ACKs are ok (and will
reset our history).
o Incorrect ACKs (not matching the last REQ) are moaned about and dropped.
Also,
o Calculate the correct FCS after compressing a packet. DEFLATE
*may* produce an mbuf with more than a single link in the chain,
but HdlcOutput didn't know how to calculate the FCS :-(
o Make `struct fsm'::reqid a u_char, not an int.
This fix will prevent us from sending id `255' 2,000,000,000 times
before wrapping to `0' for another 2,000,000,000 sends :-/
o Bump the version number a little.
The end result: DEFLATE now works over an unreliable link layer.
I can txfr a 1.5Mb kernel over a (rather bad) null-modem
cable at an average of 21679 bytes per second using rcp.
Repeat after me: Don't test compression using a loopback ppp/tcp setup as
we never lose packets and therefore never have to reset!
1998-01-10 01:55:11 +00:00
|
|
|
int uncomp_rec;
|
1998-03-17 22:29:12 +00:00
|
|
|
int winsize;
|
1997-12-03 10:23:54 +00:00
|
|
|
z_stream cx;
|
|
|
|
};
|
|
|
|
|
|
|
|
static char garbage[10];
|
|
|
|
static u_char EMPTY_BLOCK[4] = { 0x00, 0x00, 0xff, 0xff };
|
|
|
|
|
1999-12-20 20:29:51 +00:00
|
|
|
#define DEFLATE_CHUNK_LEN (1536 - sizeof(struct mbuf))
|
1997-12-03 10:23:54 +00:00
|
|
|
|
2001-06-18 15:00:22 +00:00
|
|
|
static int
|
1998-03-17 22:29:12 +00:00
|
|
|
DeflateResetOutput(void *v)
|
1997-12-03 10:23:54 +00:00
|
|
|
{
|
1998-03-17 22:29:12 +00:00
|
|
|
struct deflate_state *state = (struct deflate_state *)v;
|
|
|
|
|
|
|
|
state->seqno = 0;
|
|
|
|
state->uncomp_rec = 0;
|
|
|
|
deflateReset(&state->cx);
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogCCP, "Deflate: Output channel reset\n");
|
2001-06-18 15:00:22 +00:00
|
|
|
|
|
|
|
return 1; /* Ask FSM to ACK */
|
1997-12-03 10:23:54 +00:00
|
|
|
}
|
|
|
|
|
1999-05-08 11:07:56 +00:00
|
|
|
static struct mbuf *
|
|
|
|
DeflateOutput(void *v, struct ccp *ccp, struct link *l, int pri, u_short *proto,
|
1998-02-23 00:38:44 +00:00
|
|
|
struct mbuf *mp)
|
1997-12-03 10:23:54 +00:00
|
|
|
{
|
1998-03-17 22:29:12 +00:00
|
|
|
struct deflate_state *state = (struct deflate_state *)v;
|
1997-12-03 10:23:54 +00:00
|
|
|
u_char *wp, *rp;
|
|
|
|
int olen, ilen, len, res, flush;
|
|
|
|
struct mbuf *mo_head, *mo, *mi_head, *mi;
|
|
|
|
|
1999-12-20 20:29:47 +00:00
|
|
|
ilen = m_length(mp);
|
1999-05-08 11:07:56 +00:00
|
|
|
log_Printf(LogDEBUG, "DeflateOutput: Proto %02x (%d bytes)\n", *proto, ilen);
|
1998-05-01 19:26:12 +00:00
|
|
|
log_DumpBp(LogDEBUG, "DeflateOutput: Compress packet:", mp);
|
1997-12-03 10:23:54 +00:00
|
|
|
|
|
|
|
/* Stuff the protocol in front of the input */
|
1999-12-20 20:29:47 +00:00
|
|
|
mi_head = mi = m_get(2, MB_CCPOUT);
|
|
|
|
mi->m_next = mp;
|
1997-12-03 10:23:54 +00:00
|
|
|
rp = MBUF_CTOP(mi);
|
1999-05-08 11:07:56 +00:00
|
|
|
if (*proto < 0x100) { /* Compress the protocol */
|
|
|
|
rp[0] = *proto & 0377;
|
1999-12-20 20:29:47 +00:00
|
|
|
mi->m_len = 1;
|
1997-12-03 10:23:54 +00:00
|
|
|
} else { /* Don't compress the protocol */
|
1999-05-08 11:07:56 +00:00
|
|
|
rp[0] = *proto >> 8;
|
|
|
|
rp[1] = *proto & 0377;
|
1999-12-20 20:29:47 +00:00
|
|
|
mi->m_len = 2;
|
1997-12-03 10:23:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate the initial output mbuf */
|
1999-12-20 20:29:47 +00:00
|
|
|
mo_head = mo = m_get(DEFLATE_CHUNK_LEN, MB_CCPOUT);
|
|
|
|
mo->m_len = 2;
|
1997-12-03 10:23:54 +00:00
|
|
|
wp = MBUF_CTOP(mo);
|
1998-03-17 22:29:12 +00:00
|
|
|
*wp++ = state->seqno >> 8;
|
|
|
|
*wp++ = state->seqno & 0377;
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogDEBUG, "DeflateOutput: Seq %d\n", state->seqno);
|
1998-03-17 22:29:12 +00:00
|
|
|
state->seqno++;
|
1997-12-03 10:23:54 +00:00
|
|
|
|
|
|
|
/* Set up the deflation context */
|
1998-03-17 22:29:12 +00:00
|
|
|
state->cx.next_out = wp;
|
|
|
|
state->cx.avail_out = DEFLATE_CHUNK_LEN - 2;
|
|
|
|
state->cx.next_in = MBUF_CTOP(mi);
|
1999-12-20 20:29:47 +00:00
|
|
|
state->cx.avail_in = mi->m_len;
|
1997-12-03 10:23:54 +00:00
|
|
|
flush = Z_NO_FLUSH;
|
|
|
|
|
|
|
|
olen = 0;
|
|
|
|
while (1) {
|
1998-03-17 22:29:12 +00:00
|
|
|
if ((res = deflate(&state->cx, flush)) != Z_OK) {
|
1997-12-03 10:23:54 +00:00
|
|
|
if (res == Z_STREAM_END)
|
|
|
|
break; /* Done */
|
1998-06-16 19:40:42 +00:00
|
|
|
log_Printf(LogWARN, "DeflateOutput: deflate returned %d (%s)\n",
|
1998-03-17 22:29:12 +00:00
|
|
|
res, state->cx.msg ? state->cx.msg : "");
|
1999-12-20 20:29:47 +00:00
|
|
|
m_freem(mo_head);
|
|
|
|
m_free(mi_head);
|
1998-03-17 22:29:12 +00:00
|
|
|
state->seqno--;
|
1999-05-08 11:07:56 +00:00
|
|
|
return mp; /* Our dictionary's probably dead now :-( */
|
1997-12-03 10:23:54 +00:00
|
|
|
}
|
|
|
|
|
1998-03-17 22:29:12 +00:00
|
|
|
if (flush == Z_SYNC_FLUSH && state->cx.avail_out != 0)
|
1997-12-03 10:23:54 +00:00
|
|
|
break;
|
|
|
|
|
1999-12-20 20:29:47 +00:00
|
|
|
if (state->cx.avail_in == 0 && mi->m_next != NULL) {
|
|
|
|
mi = mi->m_next;
|
1998-03-17 22:29:12 +00:00
|
|
|
state->cx.next_in = MBUF_CTOP(mi);
|
1999-12-20 20:29:47 +00:00
|
|
|
state->cx.avail_in = mi->m_len;
|
|
|
|
if (mi->m_next == NULL)
|
1997-12-03 10:23:54 +00:00
|
|
|
flush = Z_SYNC_FLUSH;
|
|
|
|
}
|
|
|
|
|
1998-03-17 22:29:12 +00:00
|
|
|
if (state->cx.avail_out == 0) {
|
1999-12-20 20:29:47 +00:00
|
|
|
mo->m_next = m_get(DEFLATE_CHUNK_LEN, MB_CCPOUT);
|
|
|
|
olen += (mo->m_len = DEFLATE_CHUNK_LEN);
|
|
|
|
mo = mo->m_next;
|
|
|
|
mo->m_len = 0;
|
1998-03-17 22:29:12 +00:00
|
|
|
state->cx.next_out = MBUF_CTOP(mo);
|
|
|
|
state->cx.avail_out = DEFLATE_CHUNK_LEN;
|
1997-12-03 10:23:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-12-20 20:29:47 +00:00
|
|
|
olen += (mo->m_len = DEFLATE_CHUNK_LEN - state->cx.avail_out);
|
1997-12-03 10:23:54 +00:00
|
|
|
olen -= 4; /* exclude the trailing EMPTY_BLOCK */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the output packet (including seqno and excluding the EMPTY_BLOCK)
|
1998-05-15 18:21:12 +00:00
|
|
|
* got bigger, send the original.
|
1997-12-03 10:23:54 +00:00
|
|
|
*/
|
|
|
|
if (olen >= ilen) {
|
1999-12-20 20:29:47 +00:00
|
|
|
m_freem(mo_head);
|
|
|
|
m_free(mi_head);
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogDEBUG, "DeflateOutput: %d => %d: Uncompressible (0x%04x)\n",
|
1999-05-08 11:07:56 +00:00
|
|
|
ilen, olen, *proto);
|
1998-02-23 00:38:44 +00:00
|
|
|
ccp->uncompout += ilen;
|
|
|
|
ccp->compout += ilen; /* We measure this stuff too */
|
1999-05-08 11:07:56 +00:00
|
|
|
return mp;
|
1997-12-03 10:23:54 +00:00
|
|
|
}
|
|
|
|
|
1999-12-20 20:29:47 +00:00
|
|
|
m_freem(mi_head);
|
1997-12-03 10:23:54 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Lose the last four bytes of our output.
|
|
|
|
* XXX: We should probably assert that these are the same as the
|
|
|
|
* contents of EMPTY_BLOCK.
|
|
|
|
*/
|
1999-12-20 20:29:47 +00:00
|
|
|
mo = mo_head;
|
|
|
|
for (len = mo->m_len; len < olen; mo = mo->m_next, len += mo->m_len)
|
1997-12-03 10:23:54 +00:00
|
|
|
;
|
1999-12-20 20:29:47 +00:00
|
|
|
mo->m_len -= len - olen;
|
|
|
|
if (mo->m_next != NULL) {
|
|
|
|
m_freem(mo->m_next);
|
|
|
|
mo->m_next = NULL;
|
1997-12-03 10:23:54 +00:00
|
|
|
}
|
|
|
|
|
1998-02-23 00:38:44 +00:00
|
|
|
ccp->uncompout += ilen;
|
|
|
|
ccp->compout += olen;
|
1997-12-03 10:23:54 +00:00
|
|
|
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogDEBUG, "DeflateOutput: %d => %d bytes, proto 0x%04x\n",
|
1999-05-08 11:07:56 +00:00
|
|
|
ilen, olen, *proto);
|
1997-12-03 10:23:54 +00:00
|
|
|
|
1999-05-08 11:07:56 +00:00
|
|
|
*proto = ccp_Proto(ccp);
|
|
|
|
return mo_head;
|
1997-12-03 10:23:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1998-03-17 22:29:12 +00:00
|
|
|
DeflateResetInput(void *v)
|
1997-12-03 10:23:54 +00:00
|
|
|
{
|
1998-03-17 22:29:12 +00:00
|
|
|
struct deflate_state *state = (struct deflate_state *)v;
|
|
|
|
|
|
|
|
state->seqno = 0;
|
|
|
|
state->uncomp_rec = 0;
|
|
|
|
inflateReset(&state->cx);
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogCCP, "Deflate: Input channel reset\n");
|
1997-12-03 10:23:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct mbuf *
|
1998-03-17 22:29:12 +00:00
|
|
|
DeflateInput(void *v, struct ccp *ccp, u_short *proto, struct mbuf *mi)
|
1997-12-03 10:23:54 +00:00
|
|
|
{
|
1998-03-17 22:29:12 +00:00
|
|
|
struct deflate_state *state = (struct deflate_state *)v;
|
1997-12-03 10:23:54 +00:00
|
|
|
struct mbuf *mo, *mo_head, *mi_head;
|
|
|
|
u_char *wp;
|
|
|
|
int ilen, olen;
|
|
|
|
int seq, flush, res, first;
|
|
|
|
u_char hdr[2];
|
|
|
|
|
1998-05-01 19:26:12 +00:00
|
|
|
log_DumpBp(LogDEBUG, "DeflateInput: Decompress packet:", mi);
|
|
|
|
mi_head = mi = mbuf_Read(mi, hdr, 2);
|
1997-12-03 10:23:54 +00:00
|
|
|
ilen = 2;
|
|
|
|
|
|
|
|
/* Check the sequence number. */
|
|
|
|
seq = (hdr[0] << 8) + hdr[1];
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogDEBUG, "DeflateInput: Seq %d\n", seq);
|
1998-03-17 22:29:12 +00:00
|
|
|
if (seq != state->seqno) {
|
|
|
|
if (seq <= state->uncomp_rec)
|
Implement Reset{Req,Ack} properly, as per rfc 1962.
(I completely mis-read the rfc last time 'round!)
This means:
o Better CCP/WARN Reset diagnostics.
o After we've sent a REQ and before we've received an ACK, we drop
incoming compressed data and send another REQ.
o Before sending an ACK, re-sequence all pending PRI_NORMAL data in
the modem queue so that pending packets won't get to the peer
*after* the ResetAck.
o Send ACKs with the `identifier' from the REQ frame.
o After we've received a correct ACK, duplicate ACKs are ok (and will
reset our history).
o Incorrect ACKs (not matching the last REQ) are moaned about and dropped.
Also,
o Calculate the correct FCS after compressing a packet. DEFLATE
*may* produce an mbuf with more than a single link in the chain,
but HdlcOutput didn't know how to calculate the FCS :-(
o Make `struct fsm'::reqid a u_char, not an int.
This fix will prevent us from sending id `255' 2,000,000,000 times
before wrapping to `0' for another 2,000,000,000 sends :-/
o Bump the version number a little.
The end result: DEFLATE now works over an unreliable link layer.
I can txfr a 1.5Mb kernel over a (rather bad) null-modem
cable at an average of 21679 bytes per second using rcp.
Repeat after me: Don't test compression using a loopback ppp/tcp setup as
we never lose packets and therefore never have to reset!
1998-01-10 01:55:11 +00:00
|
|
|
/*
|
|
|
|
* So the peer's started at zero again - fine ! If we're wrong,
|
|
|
|
* inflate() will fail. This is better than getting into a loop
|
|
|
|
* trying to get a ResetReq to a busy sender.
|
|
|
|
*/
|
1998-03-17 22:29:12 +00:00
|
|
|
state->seqno = seq;
|
1997-12-28 02:17:06 +00:00
|
|
|
else {
|
1999-03-11 01:49:15 +00:00
|
|
|
log_Printf(LogCCP, "DeflateInput: Seq error: Got %d, expected %d\n",
|
1998-03-17 22:29:12 +00:00
|
|
|
seq, state->seqno);
|
1999-12-20 20:29:47 +00:00
|
|
|
m_freem(mi_head);
|
1998-05-01 19:26:12 +00:00
|
|
|
ccp_SendResetReq(&ccp->fsm);
|
1997-12-28 02:17:06 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-12-03 10:23:54 +00:00
|
|
|
}
|
1998-03-17 22:29:12 +00:00
|
|
|
state->seqno++;
|
|
|
|
state->uncomp_rec = 0;
|
1997-12-03 10:23:54 +00:00
|
|
|
|
|
|
|
/* Allocate an output mbuf */
|
1999-12-20 20:29:47 +00:00
|
|
|
mo_head = mo = m_get(DEFLATE_CHUNK_LEN, MB_CCPIN);
|
1997-12-03 10:23:54 +00:00
|
|
|
|
|
|
|
/* Our proto starts with 0 if it's compressed */
|
|
|
|
wp = MBUF_CTOP(mo);
|
|
|
|
wp[0] = '\0';
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We set avail_out to 1 initially so we can look at the first
|
|
|
|
* byte of the output and decide whether we have a compressed
|
|
|
|
* proto field.
|
|
|
|
*/
|
1998-03-17 22:29:12 +00:00
|
|
|
state->cx.next_in = MBUF_CTOP(mi);
|
1999-12-20 20:29:47 +00:00
|
|
|
state->cx.avail_in = mi->m_len;
|
1998-03-17 22:29:12 +00:00
|
|
|
state->cx.next_out = wp + 1;
|
|
|
|
state->cx.avail_out = 1;
|
1999-12-20 20:29:47 +00:00
|
|
|
ilen += mi->m_len;
|
1997-12-03 10:23:54 +00:00
|
|
|
|
1999-12-20 20:29:47 +00:00
|
|
|
flush = mi->m_next ? Z_NO_FLUSH : Z_SYNC_FLUSH;
|
1997-12-03 10:23:54 +00:00
|
|
|
first = 1;
|
|
|
|
olen = 0;
|
|
|
|
|
|
|
|
while (1) {
|
1998-03-17 22:29:12 +00:00
|
|
|
if ((res = inflate(&state->cx, flush)) != Z_OK) {
|
1997-12-03 10:23:54 +00:00
|
|
|
if (res == Z_STREAM_END)
|
|
|
|
break; /* Done */
|
1999-03-11 01:49:15 +00:00
|
|
|
log_Printf(LogCCP, "DeflateInput: inflate returned %d (%s)\n",
|
1998-03-17 22:29:12 +00:00
|
|
|
res, state->cx.msg ? state->cx.msg : "");
|
1999-12-20 20:29:47 +00:00
|
|
|
m_freem(mo_head);
|
|
|
|
m_freem(mi);
|
1998-05-01 19:26:12 +00:00
|
|
|
ccp_SendResetReq(&ccp->fsm);
|
1997-12-03 10:23:54 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1998-03-17 22:29:12 +00:00
|
|
|
if (flush == Z_SYNC_FLUSH && state->cx.avail_out != 0)
|
1997-12-03 10:23:54 +00:00
|
|
|
break;
|
|
|
|
|
1999-12-20 20:29:47 +00:00
|
|
|
if (state->cx.avail_in == 0 && mi && (mi = m_free(mi)) != NULL) {
|
1997-12-03 10:23:54 +00:00
|
|
|
/* underflow */
|
1998-03-17 22:29:12 +00:00
|
|
|
state->cx.next_in = MBUF_CTOP(mi);
|
1999-12-20 20:29:47 +00:00
|
|
|
ilen += (state->cx.avail_in = mi->m_len);
|
|
|
|
if (mi->m_next == NULL)
|
1997-12-03 10:23:54 +00:00
|
|
|
flush = Z_SYNC_FLUSH;
|
|
|
|
}
|
|
|
|
|
1998-04-25 10:49:52 +00:00
|
|
|
if (state->cx.avail_out == 0) {
|
1997-12-03 10:23:54 +00:00
|
|
|
/* overflow */
|
|
|
|
if (first) {
|
|
|
|
if (!(wp[1] & 1)) {
|
|
|
|
/* 2 byte proto, shuffle it back in output */
|
|
|
|
wp[0] = wp[1];
|
1998-03-17 22:29:12 +00:00
|
|
|
state->cx.next_out--;
|
|
|
|
state->cx.avail_out = DEFLATE_CHUNK_LEN-1;
|
1997-12-03 10:23:54 +00:00
|
|
|
} else
|
1998-03-17 22:29:12 +00:00
|
|
|
state->cx.avail_out = DEFLATE_CHUNK_LEN-2;
|
1997-12-03 10:23:54 +00:00
|
|
|
first = 0;
|
|
|
|
} else {
|
1999-12-20 20:29:47 +00:00
|
|
|
olen += (mo->m_len = DEFLATE_CHUNK_LEN);
|
|
|
|
mo->m_next = m_get(DEFLATE_CHUNK_LEN, MB_CCPIN);
|
|
|
|
mo = mo->m_next;
|
1998-03-17 22:29:12 +00:00
|
|
|
state->cx.next_out = MBUF_CTOP(mo);
|
|
|
|
state->cx.avail_out = DEFLATE_CHUNK_LEN;
|
1997-12-03 10:23:54 +00:00
|
|
|
}
|
1998-03-13 01:36:10 +00:00
|
|
|
}
|
1997-12-03 10:23:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mi != NULL)
|
1999-12-20 20:29:47 +00:00
|
|
|
m_freem(mi);
|
1997-12-03 10:23:54 +00:00
|
|
|
|
|
|
|
if (first) {
|
1999-03-11 01:49:15 +00:00
|
|
|
log_Printf(LogCCP, "DeflateInput: Length error\n");
|
1999-12-20 20:29:47 +00:00
|
|
|
m_freem(mo_head);
|
1998-05-01 19:26:12 +00:00
|
|
|
ccp_SendResetReq(&ccp->fsm);
|
1997-12-03 10:23:54 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1999-12-20 20:29:47 +00:00
|
|
|
olen += (mo->m_len = DEFLATE_CHUNK_LEN - state->cx.avail_out);
|
1997-12-03 10:23:54 +00:00
|
|
|
|
|
|
|
*proto = ((u_short)wp[0] << 8) | wp[1];
|
1999-12-20 20:29:47 +00:00
|
|
|
mo_head->m_offset += 2;
|
|
|
|
mo_head->m_len -= 2;
|
1997-12-03 10:23:54 +00:00
|
|
|
olen -= 2;
|
|
|
|
|
1998-02-23 00:38:44 +00:00
|
|
|
ccp->compin += ilen;
|
|
|
|
ccp->uncompin += olen;
|
1997-12-03 10:23:54 +00:00
|
|
|
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogDEBUG, "DeflateInput: %d => %d bytes, proto 0x%04x\n",
|
1997-12-03 10:23:54 +00:00
|
|
|
ilen, olen, *proto);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Simulate an EMPTY_BLOCK so that our dictionary stays in sync.
|
|
|
|
* The peer will have silently removed this!
|
|
|
|
*/
|
1998-03-17 22:29:12 +00:00
|
|
|
state->cx.next_out = garbage;
|
|
|
|
state->cx.avail_out = sizeof garbage;
|
|
|
|
state->cx.next_in = EMPTY_BLOCK;
|
|
|
|
state->cx.avail_in = sizeof EMPTY_BLOCK;
|
|
|
|
inflate(&state->cx, Z_SYNC_FLUSH);
|
1997-12-03 10:23:54 +00:00
|
|
|
|
|
|
|
return mo_head;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1998-03-17 22:29:12 +00:00
|
|
|
DeflateDictSetup(void *v, struct ccp *ccp, u_short proto, struct mbuf *mi)
|
1997-12-03 10:23:54 +00:00
|
|
|
{
|
1998-03-17 22:29:12 +00:00
|
|
|
struct deflate_state *state = (struct deflate_state *)v;
|
1997-12-08 20:22:12 +00:00
|
|
|
int res, flush, expect_error;
|
1997-12-03 10:23:54 +00:00
|
|
|
u_char *rp;
|
|
|
|
struct mbuf *mi_head;
|
|
|
|
short len;
|
|
|
|
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogDEBUG, "DeflateDictSetup: Got seq %d\n", state->seqno);
|
1997-12-03 10:23:54 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Stuff an ``uncompressed data'' block header followed by the
|
|
|
|
* protocol in front of the input
|
|
|
|
*/
|
1999-12-20 20:29:47 +00:00
|
|
|
mi_head = m_get(7, MB_CCPOUT);
|
|
|
|
mi_head->m_next = mi;
|
|
|
|
len = m_length(mi);
|
1997-12-03 10:23:54 +00:00
|
|
|
mi = mi_head;
|
|
|
|
rp = MBUF_CTOP(mi);
|
|
|
|
if (proto < 0x100) { /* Compress the protocol */
|
|
|
|
rp[5] = proto & 0377;
|
1999-12-20 20:29:47 +00:00
|
|
|
mi->m_len = 6;
|
1997-12-03 10:23:54 +00:00
|
|
|
len++;
|
|
|
|
} else { /* Don't compress the protocol */
|
|
|
|
rp[5] = proto >> 8;
|
|
|
|
rp[6] = proto & 0377;
|
1999-12-20 20:29:47 +00:00
|
|
|
mi->m_len = 7;
|
1997-12-03 10:23:54 +00:00
|
|
|
len += 2;
|
|
|
|
}
|
|
|
|
rp[0] = 0x80; /* BITS: 100xxxxx */
|
|
|
|
rp[1] = len & 0377; /* The length */
|
|
|
|
rp[2] = len >> 8;
|
|
|
|
rp[3] = (~len) & 0377; /* One's compliment of the length */
|
|
|
|
rp[4] = (~len) >> 8;
|
|
|
|
|
1998-03-17 22:29:12 +00:00
|
|
|
state->cx.next_in = rp;
|
1999-12-20 20:29:47 +00:00
|
|
|
state->cx.avail_in = mi->m_len;
|
1998-03-17 22:29:12 +00:00
|
|
|
state->cx.next_out = garbage;
|
|
|
|
state->cx.avail_out = sizeof garbage;
|
1997-12-03 10:23:54 +00:00
|
|
|
flush = Z_NO_FLUSH;
|
1997-12-08 20:22:12 +00:00
|
|
|
expect_error = 0;
|
1997-12-03 10:23:54 +00:00
|
|
|
|
|
|
|
while (1) {
|
1998-03-17 22:29:12 +00:00
|
|
|
if ((res = inflate(&state->cx, flush)) != Z_OK) {
|
1997-12-03 10:23:54 +00:00
|
|
|
if (res == Z_STREAM_END)
|
|
|
|
break; /* Done */
|
1997-12-08 20:22:12 +00:00
|
|
|
if (expect_error && res == Z_BUF_ERROR)
|
|
|
|
break;
|
1999-03-11 01:49:15 +00:00
|
|
|
log_Printf(LogCCP, "DeflateDictSetup: inflate returned %d (%s)\n",
|
1998-03-17 22:29:12 +00:00
|
|
|
res, state->cx.msg ? state->cx.msg : "");
|
1999-03-11 01:49:15 +00:00
|
|
|
log_Printf(LogCCP, "DeflateDictSetup: avail_in %d, avail_out %d\n",
|
1998-03-17 22:29:12 +00:00
|
|
|
state->cx.avail_in, state->cx.avail_out);
|
1998-05-01 19:26:12 +00:00
|
|
|
ccp_SendResetReq(&ccp->fsm);
|
1999-12-20 20:29:47 +00:00
|
|
|
m_free(mi_head); /* lose our allocated ``head'' buf */
|
1997-12-03 10:23:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1998-03-17 22:29:12 +00:00
|
|
|
if (flush == Z_SYNC_FLUSH && state->cx.avail_out != 0)
|
1997-12-03 10:23:54 +00:00
|
|
|
break;
|
|
|
|
|
1999-12-20 20:29:47 +00:00
|
|
|
if (state->cx.avail_in == 0 && mi && (mi = mi->m_next) != NULL) {
|
1997-12-03 10:23:54 +00:00
|
|
|
/* underflow */
|
1998-03-17 22:29:12 +00:00
|
|
|
state->cx.next_in = MBUF_CTOP(mi);
|
1999-12-20 20:29:47 +00:00
|
|
|
state->cx.avail_in = mi->m_len;
|
|
|
|
if (mi->m_next == NULL)
|
1997-12-03 10:23:54 +00:00
|
|
|
flush = Z_SYNC_FLUSH;
|
|
|
|
}
|
|
|
|
|
1998-03-17 22:29:12 +00:00
|
|
|
if (state->cx.avail_out == 0) {
|
|
|
|
if (state->cx.avail_in == 0)
|
1997-12-08 20:22:12 +00:00
|
|
|
/*
|
|
|
|
* This seems to be a bug in libz ! If inflate() finished
|
|
|
|
* with 0 avail_in and 0 avail_out *and* this is the end of
|
|
|
|
* our input *and* inflate() *has* actually written all the
|
|
|
|
* output it's going to, it *doesn't* return Z_STREAM_END !
|
|
|
|
* When we subsequently call it with no more input, it gives
|
|
|
|
* us Z_BUF_ERROR :-( It seems pretty safe to ignore this
|
|
|
|
* error (the dictionary seems to stay in sync). In the worst
|
|
|
|
* case, we'll drop the next compressed packet and do a
|
|
|
|
* CcpReset() then.
|
|
|
|
*/
|
|
|
|
expect_error = 1;
|
1997-12-03 10:23:54 +00:00
|
|
|
/* overflow */
|
1998-03-17 22:29:12 +00:00
|
|
|
state->cx.next_out = garbage;
|
|
|
|
state->cx.avail_out = sizeof garbage;
|
1997-12-03 10:23:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-02-23 00:38:44 +00:00
|
|
|
ccp->compin += len;
|
|
|
|
ccp->uncompin += len;
|
1997-12-03 10:23:54 +00:00
|
|
|
|
1998-03-17 22:29:12 +00:00
|
|
|
state->seqno++;
|
|
|
|
state->uncomp_rec++;
|
1999-12-20 20:29:47 +00:00
|
|
|
m_free(mi_head); /* lose our allocated ``head'' buf */
|
1997-12-03 10:23:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
2002-04-16 23:57:09 +00:00
|
|
|
DeflateDispOpts(struct fsm_opt *o)
|
1997-12-03 10:23:54 +00:00
|
|
|
{
|
1998-06-15 19:06:58 +00:00
|
|
|
static char disp[7]; /* Must be used immediately */
|
1997-12-03 10:23:54 +00:00
|
|
|
|
|
|
|
sprintf(disp, "win %d", (o->data[0]>>4) + 8);
|
|
|
|
return disp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-06-12 00:33:17 +00:00
|
|
|
DeflateInitOptsOutput(struct bundle *bundle, struct fsm_opt *o,
|
|
|
|
const struct ccp_config *cfg)
|
1997-12-03 10:23:54 +00:00
|
|
|
{
|
2002-04-16 23:57:09 +00:00
|
|
|
o->hdr.len = 4;
|
1998-03-17 22:29:12 +00:00
|
|
|
o->data[0] = ((cfg->deflate.out.winsize - 8) << 4) + 8;
|
1997-12-03 10:23:54 +00:00
|
|
|
o->data[1] = '\0';
|
|
|
|
}
|
|
|
|
|
1998-03-17 22:29:12 +00:00
|
|
|
static int
|
2002-06-12 00:33:17 +00:00
|
|
|
DeflateSetOptsOutput(struct bundle *bundle, struct fsm_opt *o,
|
|
|
|
const struct ccp_config *cfg)
|
1997-12-03 10:23:54 +00:00
|
|
|
{
|
2002-04-16 23:57:09 +00:00
|
|
|
if (o->hdr.len != 4 || (o->data[0] & 15) != 8 || o->data[1] != '\0')
|
1998-03-17 22:29:12 +00:00
|
|
|
return MODE_REJ;
|
1997-12-03 23:28:02 +00:00
|
|
|
|
1998-03-17 22:29:12 +00:00
|
|
|
if ((o->data[0] >> 4) + 8 > 15) {
|
|
|
|
o->data[0] = ((15 - 8) << 4) + 8;
|
|
|
|
return MODE_NAK;
|
|
|
|
}
|
1997-12-03 10:23:54 +00:00
|
|
|
|
1998-03-17 22:29:12 +00:00
|
|
|
return MODE_ACK;
|
1997-12-03 10:23:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2002-06-12 00:33:17 +00:00
|
|
|
DeflateSetOptsInput(struct bundle *bundle, struct fsm_opt *o,
|
|
|
|
const struct ccp_config *cfg)
|
1997-12-03 10:23:54 +00:00
|
|
|
{
|
1998-03-17 22:29:12 +00:00
|
|
|
int want;
|
|
|
|
|
2002-04-16 23:57:09 +00:00
|
|
|
if (o->hdr.len != 4 || (o->data[0] & 15) != 8 || o->data[1] != '\0')
|
1997-12-03 10:23:54 +00:00
|
|
|
return MODE_REJ;
|
1998-03-17 22:29:12 +00:00
|
|
|
|
|
|
|
want = (o->data[0] >> 4) + 8;
|
|
|
|
if (cfg->deflate.in.winsize == 0) {
|
|
|
|
if (want < 8 || want > 15) {
|
|
|
|
o->data[0] = ((15 - 8) << 4) + 8;
|
|
|
|
}
|
|
|
|
} else if (want != cfg->deflate.in.winsize) {
|
|
|
|
o->data[0] = ((cfg->deflate.in.winsize - 8) << 4) + 8;
|
1997-12-03 10:23:54 +00:00
|
|
|
return MODE_NAK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return MODE_ACK;
|
|
|
|
}
|
|
|
|
|
1998-03-17 22:29:12 +00:00
|
|
|
static void *
|
2002-06-12 00:33:17 +00:00
|
|
|
DeflateInitInput(struct bundle *bundle, struct fsm_opt *o)
|
1997-12-03 10:23:54 +00:00
|
|
|
{
|
1998-03-17 22:29:12 +00:00
|
|
|
struct deflate_state *state;
|
|
|
|
|
|
|
|
state = (struct deflate_state *)malloc(sizeof(struct deflate_state));
|
|
|
|
if (state != NULL) {
|
|
|
|
state->winsize = (o->data[0] >> 4) + 8;
|
|
|
|
state->cx.zalloc = NULL;
|
|
|
|
state->cx.opaque = NULL;
|
|
|
|
state->cx.zfree = NULL;
|
|
|
|
state->cx.next_out = NULL;
|
|
|
|
if (inflateInit2(&state->cx, -state->winsize) == Z_OK)
|
|
|
|
DeflateResetInput(state);
|
|
|
|
else {
|
|
|
|
free(state);
|
|
|
|
state = NULL;
|
|
|
|
}
|
|
|
|
}
|
1997-12-03 10:23:54 +00:00
|
|
|
|
1998-03-17 22:29:12 +00:00
|
|
|
return state;
|
1997-12-03 23:28:02 +00:00
|
|
|
}
|
|
|
|
|
1998-03-17 22:29:12 +00:00
|
|
|
static void *
|
2002-06-12 00:33:17 +00:00
|
|
|
DeflateInitOutput(struct bundle *bundle, struct fsm_opt *o)
|
1997-12-03 23:28:02 +00:00
|
|
|
{
|
1998-03-17 22:29:12 +00:00
|
|
|
struct deflate_state *state;
|
|
|
|
|
|
|
|
state = (struct deflate_state *)malloc(sizeof(struct deflate_state));
|
|
|
|
if (state != NULL) {
|
|
|
|
state->winsize = (o->data[0] >> 4) + 8;
|
|
|
|
state->cx.zalloc = NULL;
|
|
|
|
state->cx.opaque = NULL;
|
|
|
|
state->cx.zfree = NULL;
|
|
|
|
state->cx.next_in = NULL;
|
|
|
|
if (deflateInit2(&state->cx, Z_DEFAULT_COMPRESSION, 8,
|
|
|
|
-state->winsize, 8, Z_DEFAULT_STRATEGY) == Z_OK)
|
|
|
|
DeflateResetOutput(state);
|
|
|
|
else {
|
|
|
|
free(state);
|
|
|
|
state = NULL;
|
|
|
|
}
|
|
|
|
}
|
1997-12-03 23:28:02 +00:00
|
|
|
|
1998-03-17 22:29:12 +00:00
|
|
|
return state;
|
1997-12-03 10:23:54 +00:00
|
|
|
}
|
|
|
|
|
1998-03-17 22:29:12 +00:00
|
|
|
static void
|
|
|
|
DeflateTermInput(void *v)
|
1997-12-03 10:23:54 +00:00
|
|
|
{
|
1998-03-17 22:29:12 +00:00
|
|
|
struct deflate_state *state = (struct deflate_state *)v;
|
1997-12-03 10:23:54 +00:00
|
|
|
|
1998-03-17 22:29:12 +00:00
|
|
|
inflateEnd(&state->cx);
|
|
|
|
free(state);
|
1997-12-03 10:23:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1998-03-17 22:29:12 +00:00
|
|
|
DeflateTermOutput(void *v)
|
1997-12-03 10:23:54 +00:00
|
|
|
{
|
1998-03-17 22:29:12 +00:00
|
|
|
struct deflate_state *state = (struct deflate_state *)v;
|
1997-12-03 10:23:54 +00:00
|
|
|
|
1998-03-17 22:29:12 +00:00
|
|
|
deflateEnd(&state->cx);
|
|
|
|
free(state);
|
1997-12-03 10:23:54 +00:00
|
|
|
}
|
|
|
|
|
1997-12-03 23:28:02 +00:00
|
|
|
const struct ccp_algorithm PppdDeflateAlgorithm = {
|
2001-02-04 22:53:11 +00:00
|
|
|
TY_PPPD_DEFLATE, /* Older versions of pppd expected this ``type'' */
|
1998-04-16 00:26:21 +00:00
|
|
|
CCP_NEG_DEFLATE24,
|
1997-12-03 23:28:02 +00:00
|
|
|
DeflateDispOpts,
|
2001-06-18 15:00:22 +00:00
|
|
|
ccp_DefaultUsable,
|
|
|
|
ccp_DefaultRequired,
|
1997-12-03 23:28:02 +00:00
|
|
|
{
|
1998-03-17 22:29:12 +00:00
|
|
|
DeflateSetOptsInput,
|
1997-12-03 23:28:02 +00:00
|
|
|
DeflateInitInput,
|
|
|
|
DeflateTermInput,
|
|
|
|
DeflateResetInput,
|
|
|
|
DeflateInput,
|
|
|
|
DeflateDictSetup
|
|
|
|
},
|
|
|
|
{
|
2001-07-03 22:20:19 +00:00
|
|
|
0,
|
1998-03-17 22:29:12 +00:00
|
|
|
DeflateInitOptsOutput,
|
|
|
|
DeflateSetOptsOutput,
|
1997-12-03 23:28:02 +00:00
|
|
|
DeflateInitOutput,
|
|
|
|
DeflateTermOutput,
|
|
|
|
DeflateResetOutput,
|
|
|
|
DeflateOutput
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
1997-12-03 10:23:54 +00:00
|
|
|
const struct ccp_algorithm DeflateAlgorithm = {
|
1997-12-03 23:28:02 +00:00
|
|
|
TY_DEFLATE, /* rfc 1979 */
|
1998-04-16 00:26:21 +00:00
|
|
|
CCP_NEG_DEFLATE,
|
1997-12-03 10:23:54 +00:00
|
|
|
DeflateDispOpts,
|
2001-06-18 15:00:22 +00:00
|
|
|
ccp_DefaultUsable,
|
|
|
|
ccp_DefaultRequired,
|
1997-12-03 10:23:54 +00:00
|
|
|
{
|
1998-03-17 22:29:12 +00:00
|
|
|
DeflateSetOptsInput,
|
1997-12-03 10:23:54 +00:00
|
|
|
DeflateInitInput,
|
|
|
|
DeflateTermInput,
|
|
|
|
DeflateResetInput,
|
|
|
|
DeflateInput,
|
|
|
|
DeflateDictSetup
|
|
|
|
},
|
|
|
|
{
|
2001-07-03 22:20:19 +00:00
|
|
|
0,
|
1998-03-17 22:29:12 +00:00
|
|
|
DeflateInitOptsOutput,
|
|
|
|
DeflateSetOptsOutput,
|
1997-12-03 10:23:54 +00:00
|
|
|
DeflateInitOutput,
|
|
|
|
DeflateTermOutput,
|
|
|
|
DeflateResetOutput,
|
|
|
|
DeflateOutput
|
|
|
|
},
|
|
|
|
};
|