Contiki-NG
Loading...
Searching...
No Matches
slip-dev.c
1/*
2 * Copyright (c) 2001, Adam Dunkels.
3 * Copyright (c) 2009, 2010 Joakim Eriksson, Niclas Finne, Dogan Yazar.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote
15 * products derived from this software without specific prior
16 * written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 */
31
32 /* Below define allows importing saved output into Wireshark as "Raw IP" packet type */
33#define WIRESHARK_IMPORT_FORMAT 1
34#include "contiki.h"
35
36#include <stdio.h>
37#include <stdlib.h>
38#include <stdarg.h>
39#include <string.h>
40#include <time.h>
41#include <sys/types.h>
42#include <unistd.h>
43#include <errno.h>
44#include <fcntl.h>
45#include <termios.h>
46#include <signal.h>
47#include <sys/ioctl.h>
48#include <sys/socket.h>
49#include <netinet/in.h>
50#include <arpa/inet.h>
51#include <netdb.h>
52#include <err.h>
53
54#include "net/netstack.h"
55#include "net/packetbuf.h"
56#include "cmd.h"
57#include "border-router-cmds.h"
58
59extern int slip_config_verbose;
60extern int slip_config_flowcontrol;
61extern const char *slip_config_siodev;
62extern const char *slip_config_host;
63extern const char *slip_config_port;
64extern uint16_t slip_config_basedelay;
65extern speed_t slip_config_b_rate;
66
67#ifdef SLIP_DEV_CONF_SEND_DELAY
68#define SEND_DELAY SLIP_DEV_CONF_SEND_DELAY
69#else
70#define SEND_DELAY 0
71#endif
72
73int devopen(const char *dev, int flags);
74
75static FILE *inslip;
76
77/* for statistics */
78long slip_sent = 0;
79long slip_received = 0;
80
81int slipfd = 0;
82
83#define PROGRESS(s) do { } while(0)
84
85#define SLIP_END 0300
86#define SLIP_ESC 0333
87#define SLIP_ESC_END 0334
88#define SLIP_ESC_ESC 0335
89
90/*---------------------------------------------------------------------------*/
91static void *
92get_in_addr(struct sockaddr *sa)
93{
94 if(sa->sa_family == AF_INET) {
95 return &(((struct sockaddr_in *)sa)->sin_addr);
96 }
97 return &(((struct sockaddr_in6 *)sa)->sin6_addr);
98}
99/*---------------------------------------------------------------------------*/
100static int
101connect_to_server(const char *host, const char *port)
102{
103 /* Setup TCP connection */
104 struct addrinfo hints, *servinfo, *p;
105 char s[INET6_ADDRSTRLEN];
106 int rv, fd;
107
108 memset(&hints, 0, sizeof hints);
109 hints.ai_family = AF_UNSPEC;
110 hints.ai_socktype = SOCK_STREAM;
111
112 if((rv = getaddrinfo(host, port, &hints, &servinfo)) != 0) {
113 err(1, "getaddrinfo: %s", gai_strerror(rv));
114 return -1;
115 }
116
117 /* loop through all the results and connect to the first we can */
118 for(p = servinfo; p != NULL; p = p->ai_next) {
119 if((fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
120 perror("client: socket");
121 continue;
122 }
123
124 if(connect(fd, p->ai_addr, p->ai_addrlen) == -1) {
125 close(fd);
126 perror("client: connect");
127 continue;
128 }
129 break;
130 }
131
132 if(p == NULL) {
133 err(1, "can't connect to ``%s:%s''", host, port);
134 return -1;
135 }
136
137 fcntl(fd, F_SETFL, O_NONBLOCK);
138
139 inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr),
140 s, sizeof(s));
141
142 /* all done with this structure */
143 freeaddrinfo(servinfo);
144 return fd;
145}
146/*---------------------------------------------------------------------------*/
147int
148is_sensible_string(const unsigned char *s, int len)
149{
150 int i;
151 for(i = 1; i < len; i++) {
152 if(s[i] == 0 || s[i] == '\r' || s[i] == '\n' || s[i] == '\t') {
153 continue;
154 } else if(s[i] < ' ' || '~' < s[i]) {
155 return 0;
156 }
157 }
158 return 1;
159}
160/*---------------------------------------------------------------------------*/
161void
162slip_packet_input(unsigned char *data, int len)
163{
164 packetbuf_copyfrom(data, len);
165 if(slip_config_verbose > 0) {
166 printf("Packet input over SLIP: %d\n", len);
167 }
168 NETSTACK_MAC.input();
169}
170/*---------------------------------------------------------------------------*/
171/*
172 * Read from serial, when we have a packet call slip_packet_input. No output
173 * buffering, input buffered by stdio.
174 */
175void
176serial_input(FILE *inslip)
177{
178 static unsigned char inbuf[2048];
179 static int inbufptr = 0;
180 int ret, i;
181 unsigned char c;
182
183#ifdef linux
184 ret = fread(&c, 1, 1, inslip);
185 if(ret == -1 || ret == 0) {
186 err(1, "serial_input: read");
187 }
188 goto after_fread;
189#endif
190
191read_more:
192 if(inbufptr >= sizeof(inbuf)) {
193 fprintf(stderr, "*** dropping large %d byte packet\n", inbufptr);
194 inbufptr = 0;
195 }
196 ret = fread(&c, 1, 1, inslip);
197#ifdef linux
198after_fread:
199#endif
200 if(ret == -1) {
201 err(1, "serial_input: read");
202 }
203 if(ret == 0) {
204 clearerr(inslip);
205 return;
206 }
207 slip_received++;
208 switch(c) {
209 case SLIP_END:
210 if(inbufptr > 0) {
211 if(inbuf[0] == '!') {
212 command_context = CMD_CONTEXT_RADIO;
213 cmd_input(inbuf, inbufptr);
214 } else if(inbuf[0] == '?') {
215#define DEBUG_LINE_MARKER '\r'
216 } else if(inbuf[0] == DEBUG_LINE_MARKER) {
217 fwrite(inbuf + 1, inbufptr - 1, 1, stdout);
218 } else if(is_sensible_string(inbuf, inbufptr)) {
219 if(slip_config_verbose == 1) { /* strings already echoed below for verbose>1 */
220 fwrite(inbuf, inbufptr, 1, stdout);
221 }
222 } else {
223 if(slip_config_verbose > 2) {
224 printf("Packet from SLIP of length %d - write TUN\n", inbufptr);
225 if(slip_config_verbose > 4) {
226#if WIRESHARK_IMPORT_FORMAT
227 printf("0000");
228 for(i = 0; i < inbufptr; i++) {
229 printf(" %02x", inbuf[i]);
230 }
231#else
232 printf(" ");
233 for(i = 0; i < inbufptr; i++) {
234 printf("%02x", inbuf[i]);
235 if((i & 3) == 3) {
236 printf(" ");
237 }
238 if((i & 15) == 15) {
239 printf("\n ");
240 }
241 }
242#endif
243 printf("\n");
244 }
245 }
246 slip_packet_input(inbuf, inbufptr);
247 }
248 inbufptr = 0;
249 }
250 break;
251
252 case SLIP_ESC:
253 if(fread(&c, 1, 1, inslip) != 1) {
254 clearerr(inslip);
255 /* Put ESC back and give up! */
256 ungetc(SLIP_ESC, inslip);
257 return;
258 }
259
260 switch(c) {
261 case SLIP_ESC_END:
262 c = SLIP_END;
263 break;
264 case SLIP_ESC_ESC:
265 c = SLIP_ESC;
266 break;
267 }
268 /* FALLTHROUGH */
269 default:
270 inbuf[inbufptr++] = c;
271
272 /* Echo lines as they are received for verbose=2,3,5+ */
273 /* Echo all printable characters for verbose==4 */
274 if(slip_config_verbose == 4) {
275 if(c == 0 || c == '\r' || c == '\n' || c == '\t' || (c >= ' ' && c <= '~')) {
276 fwrite(&c, 1, 1, stdout);
277 }
278 } else if(slip_config_verbose >= 2) {
279 if(c == '\n' && is_sensible_string(inbuf, inbufptr)) {
280 fwrite(inbuf, inbufptr, 1, stdout);
281 inbufptr = 0;
282 }
283 }
284 break;
285 }
286
287 goto read_more;
288}
289unsigned char slip_buf[2048];
290int slip_end, slip_begin, slip_packet_end, slip_packet_count;
291static struct timer send_delay_timer;
292/* delay between slip packets */
293static clock_time_t send_delay = SEND_DELAY;
294/*---------------------------------------------------------------------------*/
295static void
296slip_send(int fd, unsigned char c)
297{
298 if(slip_end >= sizeof(slip_buf)) {
299 err(1, "slip_send overflow");
300 }
301 slip_buf[slip_end] = c;
302 slip_end++;
303 slip_sent++;
304 if(c == SLIP_END) {
305 /* Full packet received. */
306 slip_packet_count++;
307 if(slip_packet_end == 0) {
308 slip_packet_end = slip_end;
309 }
310 }
311}
312/*---------------------------------------------------------------------------*/
313int
314slip_empty()
315{
316 return slip_packet_end == 0;
317}
318/*---------------------------------------------------------------------------*/
319void
320slip_flushbuf(int fd)
321{
322 int n;
323
324 if(slip_empty()) {
325 return;
326 }
327
328 n = write(fd, slip_buf + slip_begin, slip_packet_end - slip_begin);
329
330 if(n == -1 && errno != EAGAIN) {
331 err(1, "slip_flushbuf write failed");
332 } else if(n == -1) {
333 PROGRESS("Q"); /* Outqueue is full! */
334 } else {
335 slip_begin += n;
336 if(slip_begin == slip_packet_end) {
337 slip_packet_count--;
338 if(slip_end > slip_packet_end) {
339 memmove(slip_buf, slip_buf + slip_packet_end,
340 slip_end - slip_packet_end);
341 }
342 slip_end -= slip_packet_end;
343 slip_begin = slip_packet_end = 0;
344 if(slip_end > 0) {
345 /* Find end of next slip packet */
346 for(n = 1; n < slip_end; n++) {
347 if(slip_buf[n] == SLIP_END) {
348 slip_packet_end = n + 1;
349 break;
350 }
351 }
352 /* a delay between slip packets to avoid losing data */
353 if(send_delay > 0) {
354 timer_set(&send_delay_timer, send_delay);
355 }
356 }
357 }
358 }
359}
360/*---------------------------------------------------------------------------*/
361static void
362write_to_serial(int outfd, const uint8_t *inbuf, int len)
363{
364 const uint8_t *p = inbuf;
365 int i;
366
367 if(slip_config_verbose > 2) {
368 printf("Packet from TUN of length %d - write SLIP\n", len);
369 if(slip_config_verbose > 4) {
370#if WIRESHARK_IMPORT_FORMAT
371 printf("0000");
372 for(i = 0; i < len; i++) {
373 printf(" %02x", p[i]);
374 }
375#else
376 printf(" ");
377 for(i = 0; i < len; i++) {
378 printf("%02x", p[i]);
379 if((i & 3) == 3) {
380 printf(" ");
381 }
382 if((i & 15) == 15) {
383 printf("\n ");
384 }
385 }
386#endif
387 printf("\n");
388 }
389 }
390
391 /* It would be ``nice'' to send a SLIP_END here but it's not
392 * really necessary.
393 */
394 /* slip_send(outfd, SLIP_END); */
395
396 for(i = 0; i < len; i++) {
397 switch(p[i]) {
398 case SLIP_END:
399 slip_send(outfd, SLIP_ESC);
400 slip_send(outfd, SLIP_ESC_END);
401 break;
402 case SLIP_ESC:
403 slip_send(outfd, SLIP_ESC);
404 slip_send(outfd, SLIP_ESC_ESC);
405 break;
406 default:
407 slip_send(outfd, p[i]);
408 break;
409 }
410 }
411 slip_send(outfd, SLIP_END);
412 PROGRESS("t");
413}
414/*---------------------------------------------------------------------------*/
415/* writes an 802.15.4 packet to slip-radio */
416void
417write_to_slip(const uint8_t *buf, int len)
418{
419 if(slipfd > 0) {
420 write_to_serial(slipfd, buf, len);
421 }
422}
423/*---------------------------------------------------------------------------*/
424static void
425stty_telos(int fd)
426{
427 struct termios tty;
428 speed_t speed = slip_config_b_rate;
429 int i;
430
431 if(tcflush(fd, TCIOFLUSH) == -1) {
432 err(1, "tcflush");
433 }
434
435 if(tcgetattr(fd, &tty) == -1) {
436 err(1, "tcgetattr");
437 }
438
439 cfmakeraw(&tty);
440
441 /* Nonblocking read. */
442 tty.c_cc[VTIME] = 0;
443 tty.c_cc[VMIN] = 0;
444 if(slip_config_flowcontrol) {
445 tty.c_cflag |= CRTSCTS;
446 } else {
447 tty.c_cflag &= ~CRTSCTS;
448 }
449 tty.c_cflag &= ~HUPCL;
450 tty.c_cflag &= ~CLOCAL;
451
452 cfsetispeed(&tty, speed);
453 cfsetospeed(&tty, speed);
454
455 if(tcsetattr(fd, TCSAFLUSH, &tty) == -1) {
456 err(1, "tcsetattr");
457 }
458
459#if 1
460 /* Nonblocking read and write. */
461 /* if(fcntl(fd, F_SETFL, O_NONBLOCK) == -1) err(1, "fcntl"); */
462
463 tty.c_cflag |= CLOCAL;
464 if(tcsetattr(fd, TCSAFLUSH, &tty) == -1) {
465 err(1, "tcsetattr");
466 }
467
468 i = TIOCM_DTR;
469 if(ioctl(fd, TIOCMBIS, &i) == -1) {
470 err(1, "ioctl");
471 }
472#endif
473
474 usleep(10 * 1000); /* Wait for hardware 10ms. */
475
476 /* Flush input and output buffers. */
477 if(tcflush(fd, TCIOFLUSH) == -1) {
478 err(1, "tcflush");
479 }
480}
481/*---------------------------------------------------------------------------*/
482static int
483set_fd(fd_set *rset, fd_set *wset)
484{
485 /* Anything to flush? */
486 if(!slip_empty() && (send_delay == 0 || timer_expired(&send_delay_timer))) {
487 FD_SET(slipfd, wset);
488 }
489
490 FD_SET(slipfd, rset); /* Read from slip ASAP! */
491 return 1;
492}
493/*---------------------------------------------------------------------------*/
494static void
495handle_fd(fd_set *rset, fd_set *wset)
496{
497 if(FD_ISSET(slipfd, rset)) {
498 serial_input(inslip);
499 }
500
501 if(FD_ISSET(slipfd, wset)) {
502 slip_flushbuf(slipfd);
503 }
504}
505/*---------------------------------------------------------------------------*/
506static const struct select_callback slip_callback = { set_fd, handle_fd };
507/*---------------------------------------------------------------------------*/
508void
509slip_init(void)
510{
511 setvbuf(stdout, NULL, _IOLBF, 0); /* Line buffered output. */
512
513 if(slip_config_host != NULL) {
514 if(slip_config_port == NULL) {
515 slip_config_port = "60001";
516 }
517 slipfd = connect_to_server(slip_config_host, slip_config_port);
518 if(slipfd == -1) {
519 err(1, "can't connect to ``%s:%s''", slip_config_host, slip_config_port);
520 }
521 } else if(slip_config_siodev != NULL) {
522 if(strcmp(slip_config_siodev, "null") == 0) {
523 /* Disable slip */
524 return;
525 }
526 slipfd = devopen(slip_config_siodev, O_RDWR | O_NONBLOCK);
527 if(slipfd == -1) {
528 err(1, "can't open siodev ``/dev/%s''", slip_config_siodev);
529 }
530 } else {
531 static const char *siodevs[] = {
532 "ttyUSB0", "cuaU0", "ucom0" /* linux, fbsd6, fbsd5 */
533 };
534 int i;
535 for(i = 0; i < 3; i++) {
536 slip_config_siodev = siodevs[i];
537 slipfd = devopen(slip_config_siodev, O_RDWR | O_NONBLOCK);
538 if(slipfd != -1) {
539 break;
540 }
541 }
542 if(slipfd == -1) {
543 err(1, "can't open siodev");
544 }
545 }
546
547 select_set_callback(slipfd, &slip_callback);
548
549 if(slip_config_host != NULL) {
550 fprintf(stderr, "********SLIP opened to ``%s:%s''\n", slip_config_host,
551 slip_config_port);
552 } else {
553 fprintf(stderr, "********SLIP started on ``/dev/%s''\n", slip_config_siodev);
554 stty_telos(slipfd);
555 }
556
557 timer_set(&send_delay_timer, 0);
558 slip_send(slipfd, SLIP_END);
559 inslip = fdopen(slipfd, "r");
560 if(inslip == NULL) {
561 err(1, "slip_init: fdopen");
562 }
563}
564/*---------------------------------------------------------------------------*/
Sets up some commands for the border router.
Simple command handler.
int packetbuf_copyfrom(const void *from, uint16_t len)
Copy from external data into the packetbuf.
Definition packetbuf.c:84
void timer_set(struct timer *t, clock_time_t interval)
Set a timer.
Definition timer.c:64
bool timer_expired(struct timer *t)
Check if a timer has expired.
Definition timer.c:123
Include file for the Contiki low-layer network stack (NETSTACK)
Header file for the Packet buffer (packetbuf) management.
void(* input)(void)
Callback for getting notified of incoming packet.
Definition mac.h:78
A timer.
Definition timer.h:84