summaryrefslogtreecommitdiffstats
path: root/usr.sbin/ppp/async.c
blob: 9d0262224e5c5fbcf3063cc76cbf037cce3e16e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
 *	             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.
 *
 * $Id: async.c,v 1.13 1997/10/29 01:19:38 brian Exp $
 *
 */
#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 "os.h"
#include "async.h"

#define HDLCSIZE	(MAX_MRU*2+6)

static 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;
} AsyncState;

#define MODE_HUNT 0x01
#define MODE_ESC  0x02

void
AsyncInit()
{
  struct async_state *stp = &AsyncState;

  stp->mode = MODE_HUNT;
  stp->length = 0;
  stp->my_accmap = stp->his_accmap = 0xffffffff;
}

void
SetLinkParams(struct lcpstate *lcp)
{
  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
HdlcPutByte(u_char **cp, u_char c, int proto)
{
  u_char *wp;

  wp = *cp;
  if ((c < 0x20 && (proto == PROTO_LCP || (AsyncState.his_accmap & (1 << c))))
      || (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
AsyncOutput(int pri, struct mbuf *bp, int proto)
{
  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;
  *cp++ = HDLC_SYN;
  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;
  }
  *cp++ = HDLC_SYN;

  cnt = cp - hs->xbuff;
  LogDumpBuff(LogASYNC, "WriteModem", hs->xbuff, cnt);
  WriteModem(pri, (char *) hs->xbuff, cnt);
  ModemAddOutOctets(cnt);
  pfree(bp);
}

static struct mbuf *
AsyncDecode(u_char c)
{
  struct async_state *hs = &AsyncState;
  struct mbuf *bp;

  if ((hs->mode & MODE_HUNT) && c != HDLC_SYN)
    return (NULLBUFF);

  switch (c) {
  case HDLC_SYN:
    hs->mode &= ~MODE_HUNT;
    if (hs->length) {		/* packet is ready. */
      bp = mballoc(hs->length, MB_ASYNC);
      mbwrite(bp, hs->hbuff, hs->length);
      hs->length = 0;
      return (bp);
    }
    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 */
      LogPrintf(LogERROR, "Packet too large (%d), discarding.\n", hs->length);
      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
AsyncInput(u_char *buff, int cnt)
{
  struct mbuf *bp;

  ModemAddInOctets(cnt);
  if (DEV_IS_SYNC) {
    bp = mballoc(cnt, MB_ASYNC);
    memcpy(MBUF_CTOP(bp), buff, cnt);
    bp->cnt = cnt;
    HdlcInput(bp);
  } else {
    while (cnt > 0) {
      bp = AsyncDecode(*buff++);
      if (bp)
	HdlcInput(bp);
      cnt--;
    }
  }
}
OpenPOWER on IntegriCloud