1998-01-29 00:44:16 +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.
|
|
|
|
*
|
1998-02-02 19:33:40 +00:00
|
|
|
* $Id: async.c,v 1.15.2.3 1998/02/02 19:32:00 brian Exp $
|
1998-01-29 00:44:16 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <termios.h>
|
|
|
|
|
|
|
|
#include "command.h"
|
|
|
|
#include "mbuf.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "defs.h"
|
|
|
|
#include "timer.h"
|
|
|
|
#include "fsm.h"
|
|
|
|
#include "hdlc.h"
|
|
|
|
#include "lcp.h"
|
|
|
|
#include "lcpproto.h"
|
|
|
|
#include "modem.h"
|
|
|
|
#include "loadalias.h"
|
|
|
|
#include "vars.h"
|
|
|
|
#include "async.h"
|
1998-01-30 19:46:07 +00:00
|
|
|
#include "throughput.h"
|
|
|
|
#include "link.h"
|
1998-01-29 00:49:32 +00:00
|
|
|
#include "physical.h"
|
1998-01-29 00:44:16 +00:00
|
|
|
|
|
|
|
#define MODE_HUNT 0x01
|
|
|
|
#define MODE_ESC 0x02
|
|
|
|
|
|
|
|
void
|
1998-02-02 19:33:40 +00:00
|
|
|
async_Init(struct async *async)
|
1998-01-29 00:44:16 +00:00
|
|
|
{
|
1998-02-02 19:33:40 +00:00
|
|
|
async->mode = MODE_HUNT;
|
|
|
|
async->length = 0;
|
|
|
|
async->my_accmap = async->his_accmap = 0xffffffff;
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-02-02 19:33:40 +00:00
|
|
|
async_SetLinkParams(struct async *async, struct lcpstate *lcp)
|
1998-01-29 00:44:16 +00:00
|
|
|
{
|
1998-02-02 19:33:40 +00:00
|
|
|
async->my_accmap = lcp->want_accmap;
|
|
|
|
async->his_accmap = lcp->his_accmap;
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Encode into async HDLC byte code if necessary
|
|
|
|
*/
|
|
|
|
static void
|
1998-02-02 19:33:40 +00:00
|
|
|
HdlcPutByte(struct async *async, u_char **cp, u_char c, int proto)
|
1998-01-29 00:44:16 +00:00
|
|
|
{
|
|
|
|
u_char *wp;
|
|
|
|
|
|
|
|
wp = *cp;
|
1998-02-02 19:33:40 +00:00
|
|
|
if ((c < 0x20 && (proto == PROTO_LCP || (async->his_accmap & (1 << c))))
|
1998-01-29 00:44:16 +00:00
|
|
|
|| (c == HDLC_ESC) || (c == HDLC_SYN)) {
|
|
|
|
*wp++ = HDLC_ESC;
|
|
|
|
c ^= HDLC_XOR;
|
|
|
|
}
|
|
|
|
if (EscMap[32] && EscMap[c >> 3] & (1 << (c & 7))) {
|
|
|
|
*wp++ = HDLC_ESC;
|
|
|
|
c ^= HDLC_XOR;
|
|
|
|
}
|
|
|
|
*wp++ = c;
|
|
|
|
*cp = wp;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-02-02 19:33:40 +00:00
|
|
|
async_Output(int pri, struct mbuf *bp, int proto, struct physical *physical)
|
1998-01-29 00:44:16 +00:00
|
|
|
{
|
|
|
|
u_char *cp, *sp, *ep;
|
|
|
|
struct mbuf *wp;
|
|
|
|
int cnt;
|
|
|
|
|
|
|
|
if (plength(bp) > HDLCSIZE) {
|
|
|
|
pfree(bp);
|
|
|
|
return;
|
|
|
|
}
|
1998-02-02 19:33:40 +00:00
|
|
|
cp = physical->async.xbuff;
|
1998-01-29 00:44:16 +00:00
|
|
|
ep = cp + HDLCSIZE - 10;
|
|
|
|
wp = bp;
|
|
|
|
*cp++ = HDLC_SYN;
|
|
|
|
while (wp) {
|
|
|
|
sp = MBUF_CTOP(wp);
|
|
|
|
for (cnt = wp->cnt; cnt > 0; cnt--) {
|
1998-02-02 19:33:40 +00:00
|
|
|
HdlcPutByte(&physical->async, &cp, *sp++, proto);
|
1998-01-29 00:44:16 +00:00
|
|
|
if (cp >= ep) {
|
|
|
|
pfree(bp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wp = wp->next;
|
|
|
|
}
|
|
|
|
*cp++ = HDLC_SYN;
|
|
|
|
|
1998-02-02 19:33:40 +00:00
|
|
|
cnt = cp - physical->async.xbuff;
|
|
|
|
LogDumpBuff(LogASYNC, "WriteModem", physical->async.xbuff, cnt);
|
|
|
|
link_Write(physical2link(physical), pri, (char *)physical->async.xbuff, cnt);
|
|
|
|
link_AddOutOctets(physical2link(physical), cnt);
|
1998-01-29 00:44:16 +00:00
|
|
|
pfree(bp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct mbuf *
|
1998-02-02 19:33:40 +00:00
|
|
|
async_Decode(struct async *async, u_char c)
|
1998-01-29 00:44:16 +00:00
|
|
|
{
|
|
|
|
struct mbuf *bp;
|
|
|
|
|
1998-02-02 19:33:40 +00:00
|
|
|
if ((async->mode & MODE_HUNT) && c != HDLC_SYN)
|
1998-01-29 00:44:16 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case HDLC_SYN:
|
1998-02-02 19:33:40 +00:00
|
|
|
async->mode &= ~MODE_HUNT;
|
|
|
|
if (async->length) { /* packet is ready. */
|
|
|
|
bp = mballoc(async->length, MB_ASYNC);
|
|
|
|
mbwrite(bp, async->hbuff, async->length);
|
|
|
|
async->length = 0;
|
1998-01-29 00:44:16 +00:00
|
|
|
return bp;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case HDLC_ESC:
|
1998-02-02 19:33:40 +00:00
|
|
|
if (!(async->mode & MODE_ESC)) {
|
|
|
|
async->mode |= MODE_ESC;
|
1998-01-29 00:44:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Fall into ... */
|
|
|
|
default:
|
1998-02-02 19:33:40 +00:00
|
|
|
if (async->length >= HDLCSIZE) {
|
1998-01-29 00:44:16 +00:00
|
|
|
/* packet is too large, discard it */
|
1998-02-02 19:33:40 +00:00
|
|
|
LogPrintf(LogERROR, "Packet too large (%d), discarding.\n", async->length);
|
|
|
|
async->length = 0;
|
|
|
|
async->mode = MODE_HUNT;
|
1998-01-29 00:44:16 +00:00
|
|
|
break;
|
|
|
|
}
|
1998-02-02 19:33:40 +00:00
|
|
|
if (async->mode & MODE_ESC) {
|
1998-01-29 00:44:16 +00:00
|
|
|
c ^= HDLC_XOR;
|
1998-02-02 19:33:40 +00:00
|
|
|
async->mode &= ~MODE_ESC;
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
1998-02-02 19:33:40 +00:00
|
|
|
async->hbuff[async->length++] = c;
|
1998-01-29 00:44:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-02-02 19:33:40 +00:00
|
|
|
async_Input(struct bundle *bundle, u_char *buff, int cnt,
|
|
|
|
struct physical *physical)
|
1998-01-29 00:44:16 +00:00
|
|
|
{
|
|
|
|
struct mbuf *bp;
|
|
|
|
|
1998-01-30 19:46:07 +00:00
|
|
|
link_AddInOctets(physical2link(physical), cnt);
|
|
|
|
|
1998-01-29 00:49:32 +00:00
|
|
|
if (Physical_IsSync(physical)) {
|
1998-01-29 00:44:16 +00:00
|
|
|
bp = mballoc(cnt, MB_ASYNC);
|
|
|
|
memcpy(MBUF_CTOP(bp), buff, cnt);
|
|
|
|
bp->cnt = cnt;
|
1998-02-02 19:32:16 +00:00
|
|
|
HdlcInput(bundle, bp, physical);
|
1998-01-29 00:44:16 +00:00
|
|
|
} else {
|
|
|
|
while (cnt > 0) {
|
1998-02-02 19:33:40 +00:00
|
|
|
bp = async_Decode(&physical->async, *buff++);
|
1998-01-29 00:44:16 +00:00
|
|
|
if (bp)
|
1998-02-02 19:32:16 +00:00
|
|
|
HdlcInput(bundle, bp, physical);
|
1998-01-29 00:44:16 +00:00
|
|
|
cnt--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|