async.c revision 45586
1159248Srwatson/* 2159248Srwatson * PPP Async HDLC Module 3159248Srwatson * 4159248Srwatson * Written by Toshiharu OHNO (tony-o@iij.ad.jp) 5159248Srwatson * 6159248Srwatson * Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd. 7159248Srwatson * 8159248Srwatson * Redistribution and use in source and binary forms are permitted 9159248Srwatson * provided that the above copyright notice and this paragraph are 10159248Srwatson * duplicated in all such forms and that any documentation, 11159248Srwatson * advertising materials, and other materials related to such 12159248Srwatson * distribution and use acknowledge that the software was developed 13159248Srwatson * by the Internet Initiative Japan, Inc. The name of the 14159248Srwatson * IIJ may not be used to endorse or promote products derived 15159248Srwatson * from this software without specific prior written permission. 16159248Srwatson * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 17159248Srwatson * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 18159248Srwatson * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 19159248Srwatson * 20159248Srwatson * $Id: async.c,v 1.17 1998/06/16 19:40:34 brian Exp $ 21159248Srwatson * 22159248Srwatson */ 23159248Srwatson#include <sys/types.h> 24159248Srwatson 25159248Srwatson#include <string.h> 26159248Srwatson#include <termios.h> 27159248Srwatson 28159248Srwatson#include "mbuf.h" 29159248Srwatson#include "log.h" 30159248Srwatson#include "defs.h" 31159248Srwatson#include "timer.h" 32159248Srwatson#include "fsm.h" 33159248Srwatson#include "lqr.h" 34159248Srwatson#include "hdlc.h" 35159248Srwatson#include "lcp.h" 36159248Srwatson#include "lcpproto.h" 37159248Srwatson#include "async.h" 38159248Srwatson#include "throughput.h" 39161630Srwatson#include "ccp.h" 40159248Srwatson#include "link.h" 41159248Srwatson#include "descriptor.h" 42159248Srwatson#include "physical.h" 43159248Srwatson 44159248Srwatson#define MODE_HUNT 0x01 45159248Srwatson#define MODE_ESC 0x02 46161630Srwatson 47159248Srwatsonvoid 48159248Srwatsonasync_Init(struct async *async) 49159248Srwatson{ 50161630Srwatson async->mode = MODE_HUNT; 51159248Srwatson async->length = 0; 52159248Srwatson async->my_accmap = async->his_accmap = 0xffffffff; 53159248Srwatson memset(async->cfg.EscMap, '\0', sizeof async->cfg.EscMap); 54159248Srwatson} 55161630Srwatson 56161630Srwatsonvoid 57161630Srwatsonasync_SetLinkParams(struct async *async, struct lcp *lcp) 58161630Srwatson{ 59161630Srwatson async->my_accmap = lcp->want_accmap; 60161630Srwatson async->his_accmap = lcp->his_accmap | lcp->want_accmap; 61159248Srwatson} 62159248Srwatson 63159248Srwatson/* 64159248Srwatson * Encode into async HDLC byte code if necessary 65159248Srwatson */ 66159248Srwatsonstatic void 67159248SrwatsonHdlcPutByte(struct async *async, u_char **cp, u_char c, int proto) 68159248Srwatson{ 69159248Srwatson u_char *wp; 70159248Srwatson 71159248Srwatson wp = *cp; 72159248Srwatson if ((c < 0x20 && (proto == PROTO_LCP || (async->his_accmap & (1 << c)))) 73161630Srwatson || (c == HDLC_ESC) || (c == HDLC_SYN)) { 74159248Srwatson *wp++ = HDLC_ESC; 75159248Srwatson c ^= HDLC_XOR; 76159248Srwatson } 77159248Srwatson if (async->cfg.EscMap[32] && async->cfg.EscMap[c >> 3] & (1 << (c & 7))) { 78161630Srwatson *wp++ = HDLC_ESC; 79159248Srwatson c ^= HDLC_XOR; 80159248Srwatson } 81159248Srwatson *wp++ = c; 82 *cp = wp; 83} 84 85void 86async_Output(int pri, struct mbuf *bp, int proto, struct physical *physical) 87{ 88 u_char *cp, *sp, *ep; 89 struct mbuf *wp; 90 int cnt; 91 92 if (mbuf_Length(bp) > HDLCSIZE) { 93 mbuf_Free(bp); 94 return; 95 } 96 cp = physical->async.xbuff; 97 ep = cp + HDLCSIZE - 10; 98 wp = bp; 99 *cp++ = HDLC_SYN; 100 while (wp) { 101 sp = MBUF_CTOP(wp); 102 for (cnt = wp->cnt; cnt > 0; cnt--) { 103 HdlcPutByte(&physical->async, &cp, *sp++, proto); 104 if (cp >= ep) { 105 mbuf_Free(bp); 106 return; 107 } 108 } 109 wp = wp->next; 110 } 111 *cp++ = HDLC_SYN; 112 113 cnt = cp - physical->async.xbuff; 114 log_DumpBuff(LogASYNC, "WriteModem", physical->async.xbuff, cnt); 115 link_Write(&physical->link, pri, (char *)physical->async.xbuff, cnt); 116 link_AddOutOctets(&physical->link, cnt); 117 mbuf_Free(bp); 118} 119 120static struct mbuf * 121async_Decode(struct async *async, u_char c) 122{ 123 struct mbuf *bp; 124 125 if ((async->mode & MODE_HUNT) && c != HDLC_SYN) 126 return NULL; 127 128 switch (c) { 129 case HDLC_SYN: 130 async->mode &= ~MODE_HUNT; 131 if (async->length) { /* packet is ready. */ 132 bp = mbuf_Alloc(async->length, MB_ASYNC); 133 mbuf_Write(bp, async->hbuff, async->length); 134 async->length = 0; 135 return bp; 136 } 137 break; 138 case HDLC_ESC: 139 if (!(async->mode & MODE_ESC)) { 140 async->mode |= MODE_ESC; 141 break; 142 } 143 /* Fall into ... */ 144 default: 145 if (async->length >= HDLCSIZE) { 146 /* packet is too large, discard it */ 147 log_Printf(LogWARN, "Packet too large (%d), discarding.\n", 148 async->length); 149 async->length = 0; 150 async->mode = MODE_HUNT; 151 break; 152 } 153 if (async->mode & MODE_ESC) { 154 c ^= HDLC_XOR; 155 async->mode &= ~MODE_ESC; 156 } 157 async->hbuff[async->length++] = c; 158 break; 159 } 160 return NULL; 161} 162 163void 164async_Input(struct bundle *bundle, u_char *buff, int cnt, 165 struct physical *physical) 166{ 167 struct mbuf *bp; 168 169 link_AddInOctets(&physical->link, cnt); 170 171 if (physical_IsSync(physical)) { 172 bp = mbuf_Alloc(cnt, MB_ASYNC); 173 memcpy(MBUF_CTOP(bp), buff, cnt); 174 bp->cnt = cnt; 175 hdlc_Input(bundle, bp, physical); 176 } else { 177 while (cnt > 0) { 178 bp = async_Decode(&physical->async, *buff++); 179 if (bp) 180 hdlc_Input(bundle, bp, physical); 181 cnt--; 182 } 183 } 184} 185