2 * Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org>
3 * based on work by Toshiharu OHNO <tony-o@iij.ad.jp>
4 * Internet Initiative Japan, Inc (IIJ)
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * $FreeBSD: src/usr.sbin/ppp/async.c,v 1.23.2.3 2002/09/01 02:12:22 brian Exp $
29 * $DragonFly: src/usr.sbin/ppp/async.c,v 1.2 2003/06/17 04:30:00 dillon Exp $
32 #include <sys/types.h>
48 #include "throughput.h"
51 #include "descriptor.h"
54 #define MODE_HUNT 0x01
58 async_Init(struct async
*async
)
61 memset(async
->cfg
.EscMap
, '\0', sizeof async
->cfg
.EscMap
);
65 async_Setup(struct async
*async
)
67 async
->mode
= MODE_HUNT
;
69 async
->my_accmap
= async
->his_accmap
= 0xffffffff;
73 async_SetLinkParams(struct async
*async
, u_int32_t mymap
, u_int32_t hismap
)
75 async
->my_accmap
= mymap
;
76 async
->his_accmap
= hismap
| mymap
;
80 * Encode into async HDLC byte code
83 async_Encode(struct async
*async
, u_char
**cp
, u_char c
, int proto
)
88 if ((c
< 0x20 && (proto
== PROTO_LCP
|| (async
->his_accmap
& (1 << c
))))
89 || (c
== HDLC_ESC
) || (c
== HDLC_SYN
)) {
93 if (async
->cfg
.EscMap
[32] && async
->cfg
.EscMap
[c
>> 3] & (1 << (c
& 7))) {
102 async_LayerPush(struct bundle
*bundle
, struct link
*l
, struct mbuf
*bp
,
103 int pri
, u_short
*proto
)
105 struct physical
*p
= link2physical(l
);
106 u_char
*cp
, *sp
, *ep
;
110 if (!p
|| m_length(bp
) > HDLCSIZE
) {
116 ep
= cp
+ HDLCSIZE
- 10;
121 for (cnt
= wp
->m_len
; cnt
> 0; cnt
--) {
122 async_Encode(&p
->async
, &cp
, *sp
++, *proto
);
132 cnt
= cp
- p
->async
.xbuff
;
134 bp
= m_get(cnt
, MB_ASYNCOUT
);
135 memcpy(MBUF_CTOP(bp
), p
->async
.xbuff
, cnt
);
136 log_DumpBp(LogASYNC
, "Write", bp
);
142 async_Decode(struct async
*async
, u_char c
)
146 if ((async
->mode
& MODE_HUNT
) && c
!= HDLC_SYN
)
151 async
->mode
&= ~MODE_HUNT
;
152 if (async
->length
) { /* packet is ready. */
153 bp
= m_get(async
->length
, MB_ASYNCIN
);
154 mbuf_Write(bp
, async
->hbuff
, async
->length
);
160 if (!(async
->mode
& MODE_ESC
)) {
161 async
->mode
|= MODE_ESC
;
166 if (async
->length
>= HDLCSIZE
) {
167 /* packet is too large, discard it */
168 log_Printf(LogWARN
, "Packet too large (%d), discarding.\n",
171 async
->mode
= MODE_HUNT
;
174 if (async
->mode
& MODE_ESC
) {
176 async
->mode
&= ~MODE_ESC
;
178 async
->hbuff
[async
->length
++] = c
;
185 async_LayerPull(struct bundle
*b
, struct link
*l
, struct mbuf
*bp
,
188 struct mbuf
*nbp
, **last
;
189 struct physical
*p
= link2physical(l
);
194 log_Printf(LogERROR
, "Can't Pull an async packet from a logical link\n");
200 log_DumpBp(LogASYNC
, "Read", bp
);
203 for (cnt
= bp
->m_len
; cnt
; cnt
--) {
204 *last
= async_Decode(&p
->async
, *ch
++);
206 last
= &(*last
)->m_nextpkt
;
214 struct layer asynclayer
=
215 { LAYER_ASYNC
, "async", async_LayerPush
, async_LayerPull
};