-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathahdlc.c
163 lines (157 loc) · 3.27 KB
/
ahdlc.c
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
/* Asynchronous HDLC routines */
#include "global.h"
#include "ahdlc.h"
#include "crc.h"
#include "trace.h" /******/
static uint8 *putbyte(uint8 *,uint8);
void
init_hdlc(hp,maxsize)
struct ahdlc *hp;
int maxsize;
{
hp->escaped = 0;
hp->hunt = 0;
hp->inframe = NULL;
hp->maxsize = maxsize;
hp->fcs = FCS_START;
hp->rxframes = 0;
hp->aborts = 0;
hp->toobigs = 0;
hp->crcerrs = 0;
}
/* Process incoming data. Return completed packets, NULL otherwise */
struct mbuf *
ahdlcrx(ap,c)
struct ahdlc *ap; /* HDLC Receiver control block */
uint8 c;
{
struct mbuf *bp;
if(c == HDLC_ESC_ASYNC){
ap->escaped = 1;
return NULL;
}
if(c != HDLC_FLAG){
if(ap->hunt)
return NULL; /* Ignore until next packet */
/* Normal character within packet */
if(ap->escaped){
c ^= HDLC_ESC_COMPL;
ap->escaped = 0;
}
if(ap->inframe == NULL)
ap->inframe = ambufw(ap->maxsize);
if(ap->inframe->cnt == ap->maxsize){
/* Frame too large */
ap->toobigs++;
#ifdef debug
printf("FRAME TOO LARGE (>%u bytes)\n",ap->maxsize);
#endif
free_p(&ap->inframe);
ap->inframe = NULL;
ap->escaped = 0;
ap->fcs = FCS_START;
ap->hunt = 1;
return NULL;
}
/* Store character, update FCS */
ap->inframe->data[ap->inframe->cnt++] = c;
ap->fcs = FCS(ap->fcs,c);
return NULL;
}
/* We get here only if the character is a flag */
if(ap->escaped){
/* ESC, FLAG is frame abort */
ap->aborts++;
#ifdef debug
printf("AHDLC ABORT, cnt = %u\n",ap->inframe->cnt);
#endif
ap->hunt = 1;
ap->escaped = 0;
free_p(&ap->inframe);
ap->inframe = NULL;
ap->fcs = FCS_START;
return NULL;
}
if(ap->hunt){
/* Found flag in hunt mode. Reset for new frame */
ap->hunt = 0;
return NULL;
}
if(ap->inframe == NULL){
/* Padding flags, ignore */
return NULL;
}
if(ap->fcs != FCS_FINAL){
/* CRC error */
ap->crcerrs++;
#ifdef debug
printf("AHDLC CRC ERROR, cnt = %u\n",ap->inframe->cnt);
hex_dump(stdout,&ap->inframe);
#endif
free_p(&ap->inframe);
ap->inframe = NULL;
ap->fcs = FCS_START;
return NULL;
}
if(ap->inframe->cnt < 2){
/* Runt frame */
ap->runts++;
#ifdef debug
printf("AHDLC RUNT, cnt = %u\n",ap->inframe->cnt);
#endif
free_p(&ap->inframe);
ap->inframe = NULL;
ap->fcs = FCS_START;
return NULL;
}
/* Normal end-of-frame */
ap->rxframes++;
bp = ap->inframe;
ap->inframe = NULL;
ap->fcs = FCS_START;
bp->cnt -= 2;
#ifdef debug
printf("Normal AHDLC receive, len %u\n",bp->cnt);
#endif
return bp;
}
/* Encode a packet in asynchronous HDLC for transmission */
struct mbuf *
ahdlctx(bp)
struct mbuf *bp;
{
struct mbuf *obp;
uint8 *cp;
int c;
uint16 fcs;
fcs = FCS_START;
obp = ambufw(5+2*len_p(bp)); /* Allocate worst-case */
cp = obp->data;
while((c = PULLCHAR(&bp)) != -1){
fcs = FCS(fcs,c);
cp = putbyte(cp,c);
}
free_p(&bp); /* Shouldn't be necessary */
fcs ^= 0xffff;
cp = putbyte(cp,fcs);
cp = putbyte(cp,fcs >> 8);
*cp++ = HDLC_FLAG;
obp->cnt = cp - obp->data;
return obp;
}
static uint8 *
putbyte(cp,c)
uint8 *cp;
uint8 c;
{
switch(c){
case HDLC_FLAG:
case HDLC_ESC_ASYNC:
*cp++ = HDLC_ESC_ASYNC;
*cp++ = c ^ HDLC_ESC_COMPL;
break;
default:
*cp++ = c;
}
return cp;
}