forked from pandax381/microps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslip.c
249 lines (228 loc) · 5.59 KB
/
slip.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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <poll.h>
#include <limits.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <pthread.h>
#include "util.h"
#include "net.h"
#include "ethernet.h"
#include "slip.h"
#define END 0x0c
#define ESC 0xdb
#define ESC_END 0xdc
#define ESC_ESC 0xdd
#define SLIP_TYPE_IP 0x0800
#define SLIP_MUT_DEFAULT 1006
struct slip_priv {
struct netdev *dev;
int fd;
int terminate;
pthread_t thread;
pthread_mutex_t mutex;
};
void
slip_dump (struct netdev *dev, uint8_t *packet, size_t plen) {
fprintf(stderr, " device: %s\n", dev->name);
fprintf(stderr, " length: %zu octets\n", plen);
hexdump(stderr, packet, plen);
}
static int
slip_open (struct netdev *dev, int opt) {
struct slip_priv *priv;
struct stat st;
struct sockaddr_un addr;
(void)opt;
priv = malloc(sizeof(struct slip_priv));
if (!priv) {
fprintf(stderr, "malloc(): error\n");
return -1;
}
priv->dev = dev;
if (stat(priv->dev->name, &st) == -1) {
perror("stat");
free(priv);
return -1;
}
switch (st.st_mode & S_IFMT) {
case S_IFCHR:
priv->fd = open(priv->dev->name, O_RDWR | O_NOCTTY | O_NDELAY);
if (priv->fd == -1) {
perror("open");
free(priv);
return -1;
}
break;
case S_IFSOCK:
priv->fd = socket(PF_UNIX, SOCK_STREAM, 0);
if (priv->fd == -1) {
perror("socket");
free(priv);
return -1;
}
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, priv->dev->name);
if (connect(priv->fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
perror("connect");
close(priv->fd);
free(priv);
return -1;
}
break;
default:
fprintf(stderr, "slip_priv_open(): support Charcter Device or UNIX Domain Socket only\n");
free(priv);
return -1;
}
priv->terminate = 0;
pthread_mutex_init(&priv->mutex, NULL);
dev->priv = priv;
return 0;
}
static int
slip_close (struct netdev *dev) {
struct slip_priv *priv;
priv = (struct slip_priv *)dev->priv;
close(priv->fd);
free(priv);
return 0;
}
static void *
slip_rx_thread (void *arg) {
struct netdev *dev;
struct slip_priv *priv;
struct pollfd pfd;
int c, overflow = 0;
uint8_t buf[1024];
ssize_t len = 0;
dev = (struct netdev *)arg;
priv = (struct slip_priv *)dev->priv;
pfd.fd = priv->fd;
pfd.events = POLLIN;
while (!priv->terminate) {
if (poll(&pfd, 1, 1000) <= 0) {
continue;
}
c = fdgetc(priv->fd);
switch (c) {
case END:
if (len) {
if (!overflow) {
#ifdef DEBUG
fprintf(stderr, ">>> slip_rx_thread <<<\n");
slip_dump(dev, buf, len);
#endif
dev->rx_handler(dev, SLIP_TYPE_IP, buf, len);
}
overflow = 0;
len = 0;
}
break;
case ESC:
c = fdgetc(priv->fd);
switch (c) {
case ESC_END:
c = END;
break;
case ESC_ESC:
c = ESC;
break;
}
/* fallthrough */
default:
if (!overflow) {
if (len == sizeof(buf)) {
fprintf(stderr, "slip_rx_thread: warnging, rx buffer is overflow !!!\n");
overflow = 1;
break;
}
buf[len++] = c;
}
break;
}
}
return NULL;
}
static int
slip_run (struct netdev *dev) {
struct slip_priv *priv;
int err;
priv = (struct slip_priv *)dev->priv;
err = pthread_create(&priv->thread, NULL, slip_rx_thread, dev);
if (err) {
fprintf(stderr, "pthread_create(): error, code=%d\n", err);
return -1;
}
return 0;
}
static int
slip_stop (struct netdev *dev) {
struct slip_priv *priv;
priv = dev->priv;
priv->terminate = 1;
pthread_join(priv->thread, NULL);
priv->thread = pthread_self();
priv->terminate = 0;
return 0;
}
static ssize_t
slip_tx (struct netdev *dev, uint16_t type, const uint8_t *payload, size_t plen, const void *dst) {
struct slip_priv *priv;
uint8_t *p;
(void)dst;
if (type != ETHERNET_TYPE_IP) {
return -1;
}
priv = (struct slip_priv *)dev->priv;
p = (uint8_t *)payload;
pthread_mutex_lock(&priv->mutex);
fdputc(priv->fd, END);
while (plen--) {
switch (*p) {
case END:
fdputc(priv->fd, ESC);
fdputc(priv->fd, ESC_END);
break;
case ESC:
fdputc(priv->fd, ESC);
fdputc(priv->fd, ESC_ESC);
break;
default:
fdputc(priv->fd, *p);
break;
}
p++;
}
fdputc(priv->fd, END);
pthread_mutex_unlock(&priv->mutex);
return plen;
}
static struct netdev_ops slip_ops = {
.open = slip_open,
.close = slip_close,
.run = slip_run,
.stop = slip_stop,
.tx = slip_tx
};
static struct netdev_def slip_def = {
.type = NETDEV_TYPE_SLIP,
.mtu = SLIP_MUT_DEFAULT,
.flags = NETDEV_FLAG_NOARP,
.hlen = 0,
.alen = 0,
.ops = &slip_ops
};
int
slip_init (void) {
netdev_driver_register(&slip_def);
return 0;
}