1995-01-31 06:29:58 +00:00
|
|
|
/*
|
|
|
|
* PPP Async HDLC Module
|
|
|
|
*
|
|
|
|
* Written by Toshiharu OHNO (tony-o@iij.ad.jp)
|
|
|
|
*
|
|
|
|
* Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms are permitted
|
|
|
|
* provided that the above copyright notice and this paragraph are
|
|
|
|
* duplicated in all such forms and that any documentation,
|
|
|
|
* advertising materials, and other materials related to such
|
|
|
|
* distribution and use acknowledge that the software was developed
|
|
|
|
* by the Internet Initiative Japan, Inc. The name of the
|
|
|
|
* IIJ may not be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
*
|
1997-08-25 00:29:32 +00:00
|
|
|
* $Id: async.c,v 1.10 1997/06/23 23:08:23 brian Exp $
|
1995-05-30 03:57:47 +00:00
|
|
|
*
|
1995-01-31 06:29:58 +00:00
|
|
|
*/
|
|
|
|
#include "fsm.h"
|
|
|
|
#include "hdlc.h"
|
|
|
|
#include "lcp.h"
|
|
|
|
#include "lcpproto.h"
|
|
|
|
#include "modem.h"
|
1997-05-26 00:44:10 +00:00
|
|
|
#include "loadalias.h"
|
1995-02-26 12:18:08 +00:00
|
|
|
#include "vars.h"
|
1996-01-11 17:48:59 +00:00
|
|
|
#include "os.h"
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
#define HDLCSIZE (MAX_MRU*2+6)
|
|
|
|
|
|
|
|
struct async_state {
|
|
|
|
int mode;
|
|
|
|
int length;
|
|
|
|
u_char hbuff[HDLCSIZE]; /* recv buffer */
|
|
|
|
u_char xbuff[HDLCSIZE]; /* xmit buffer */
|
|
|
|
u_long my_accmap;
|
|
|
|
u_long his_accmap;
|
1997-08-25 00:29:32 +00:00
|
|
|
} AsyncState;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
#define MODE_HUNT 0x01
|
|
|
|
#define MODE_ESC 0x02
|
|
|
|
|
|
|
|
void
|
|
|
|
AsyncInit()
|
|
|
|
{
|
|
|
|
struct async_state *stp = &AsyncState;
|
|
|
|
|
|
|
|
stp->mode = MODE_HUNT;
|
1995-02-26 12:18:08 +00:00
|
|
|
stp->length = 0;
|
1995-01-31 06:29:58 +00:00
|
|
|
stp->my_accmap = stp->his_accmap = 0xffffffff;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1997-08-25 00:29:32 +00:00
|
|
|
SetLinkParams(struct lcpstate * lcp)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
struct async_state *stp = &AsyncState;
|
|
|
|
|
|
|
|
stp->my_accmap = lcp->want_accmap;
|
|
|
|
stp->his_accmap = lcp->his_accmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Encode into async HDLC byte code if necessary
|
|
|
|
*/
|
|
|
|
static void
|
1997-08-25 00:29:32 +00:00
|
|
|
HdlcPutByte(u_char ** cp, u_char c, int proto)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
u_char *wp;
|
|
|
|
|
|
|
|
wp = *cp;
|
1997-08-25 00:29:32 +00:00
|
|
|
if ((c < 0x20 && (proto == PROTO_LCP || (AsyncState.his_accmap & (1 << c))))
|
|
|
|
|| (c == HDLC_ESC) || (c == HDLC_SYN)) {
|
1995-01-31 06:29:58 +00:00
|
|
|
*wp++ = HDLC_ESC;
|
|
|
|
c ^= HDLC_XOR;
|
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
if (EscMap[32] && EscMap[c >> 3] & (1 << (c & 7))) {
|
1995-01-31 06:29:58 +00:00
|
|
|
*wp++ = HDLC_ESC;
|
|
|
|
c ^= HDLC_XOR;
|
|
|
|
}
|
|
|
|
*wp++ = c;
|
|
|
|
*cp = wp;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1997-08-25 00:29:32 +00:00
|
|
|
AsyncOutput(int pri, struct mbuf * bp, int proto)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
struct async_state *hs = &AsyncState;
|
|
|
|
u_char *cp, *sp, *ep;
|
|
|
|
struct mbuf *wp;
|
|
|
|
int cnt;
|
|
|
|
|
|
|
|
if (plength(bp) > HDLCSIZE) {
|
|
|
|
pfree(bp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cp = hs->xbuff;
|
|
|
|
ep = cp + HDLCSIZE - 10;
|
|
|
|
wp = bp;
|
1997-08-25 00:29:32 +00:00
|
|
|
*cp++ = HDLC_SYN;
|
1995-01-31 06:29:58 +00:00
|
|
|
while (wp) {
|
|
|
|
sp = MBUF_CTOP(wp);
|
|
|
|
for (cnt = wp->cnt; cnt > 0; cnt--) {
|
|
|
|
HdlcPutByte(&cp, *sp++, proto);
|
|
|
|
if (cp >= ep) {
|
|
|
|
pfree(bp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wp = wp->next;
|
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
*cp++ = HDLC_SYN;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
cnt = cp - hs->xbuff;
|
1997-06-09 03:27:43 +00:00
|
|
|
LogDumpBuff(LogASYNC, "WriteModem", hs->xbuff, cnt);
|
1997-08-25 00:29:32 +00:00
|
|
|
WriteModem(pri, (char *) hs->xbuff, cnt);
|
1995-01-31 06:29:58 +00:00
|
|
|
OsAddOutOctets(cnt);
|
|
|
|
pfree(bp);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct mbuf *
|
1997-08-25 00:29:32 +00:00
|
|
|
AsyncDecode(u_char c)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
struct async_state *hs = &AsyncState;
|
|
|
|
struct mbuf *bp;
|
|
|
|
|
|
|
|
if ((hs->mode & MODE_HUNT) && c != HDLC_SYN)
|
1997-08-25 00:29:32 +00:00
|
|
|
return (NULLBUFF);
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case HDLC_SYN:
|
|
|
|
hs->mode &= ~MODE_HUNT;
|
1997-08-25 00:29:32 +00:00
|
|
|
if (hs->length) { /* packet is ready. */
|
1995-01-31 06:29:58 +00:00
|
|
|
bp = mballoc(hs->length, MB_ASYNC);
|
|
|
|
mbwrite(bp, hs->hbuff, hs->length);
|
|
|
|
hs->length = 0;
|
1997-08-25 00:29:32 +00:00
|
|
|
return (bp);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case HDLC_ESC:
|
|
|
|
if (!(hs->mode & MODE_ESC)) {
|
|
|
|
hs->mode |= MODE_ESC;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Fall into ... */
|
|
|
|
default:
|
|
|
|
if (hs->length >= HDLCSIZE) {
|
|
|
|
/* packet is too large, discard it */
|
1997-06-23 23:08:23 +00:00
|
|
|
LogPrintf(LogERROR, "Packet too large (%d), discarding.\n", hs->length);
|
1995-01-31 06:29:58 +00:00
|
|
|
hs->length = 0;
|
|
|
|
hs->mode = MODE_HUNT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (hs->mode & MODE_ESC) {
|
|
|
|
c ^= HDLC_XOR;
|
|
|
|
hs->mode &= ~MODE_ESC;
|
|
|
|
}
|
|
|
|
hs->hbuff[hs->length++] = c;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return NULLBUFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1997-08-25 00:29:32 +00:00
|
|
|
AsyncInput(u_char * buff, int cnt)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
struct mbuf *bp;
|
|
|
|
|
|
|
|
OsAddInOctets(cnt);
|
1995-02-26 12:18:08 +00:00
|
|
|
if (DEV_IS_SYNC) {
|
|
|
|
bp = mballoc(cnt, MB_ASYNC);
|
|
|
|
bcopy(buff, MBUF_CTOP(bp), cnt);
|
|
|
|
bp->cnt = cnt;
|
|
|
|
HdlcInput(bp);
|
|
|
|
} else {
|
|
|
|
while (cnt > 0) {
|
|
|
|
bp = AsyncDecode(*buff++);
|
|
|
|
if (bp)
|
1997-08-25 00:29:32 +00:00
|
|
|
HdlcInput(bp);
|
1995-02-26 12:18:08 +00:00
|
|
|
cnt--;
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|