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
73static FILE *inslip;
74
75/* for statistics */
76long slip_sent = 0;
77long slip_received = 0;
78
79int slipfd = 0;
80
81#define PROGRESS(s) do { } while(0)
82
83#define SLIP_END 0300
84#define SLIP_ESC 0333
85#define SLIP_ESC_END 0334
86#define SLIP_ESC_ESC 0335
87
88/*---------------------------------------------------------------------------*/
89static void *
90get_in_addr(struct sockaddr *sa)
91{
92 if(sa->sa_family == AF_INET) {
93 return &(((struct sockaddr_in *)sa)->sin_addr);
94 }
95 return &(((struct sockaddr_in6 *)sa)->sin6_addr);
96}
97/*---------------------------------------------------------------------------*/
98static int
99connect_to_server(const char *host, const char *port)
100{
101 /* Setup TCP connection */
102 struct addrinfo hints, *servinfo, *p;
103 char s[INET6_ADDRSTRLEN];
104 int rv, fd;
105
106 memset(&hints, 0, sizeof hints);
107 hints.ai_family = AF_UNSPEC;
108 hints.ai_socktype = SOCK_STREAM;
109
110 if((rv = getaddrinfo(host, port, &hints, &servinfo)) != 0) {
111 err(EXIT_FAILURE, "getaddrinfo: %s", gai_strerror(rv));
112 return -1;
113 }
114
115 /* loop through all the results and connect to the first we can */
116 for(p = servinfo; p != NULL; p = p->ai_next) {
117 if((fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
118 perror("client: socket");
119 continue;
120 }
121
122 if(connect(fd, p->ai_addr, p->ai_addrlen) == -1) {
123 close(fd);
124 perror("client: connect");
125 continue;
126 }
127 break;
128 }
129
130 if(p == NULL) {
131 err(EXIT_FAILURE, "can't connect to ``%s:%s''", host, port);
132 return -1;
133 }
134
135 fcntl(fd, F_SETFL, O_NONBLOCK);
136
137 inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr),
138 s, sizeof(s));
139
140 /* all done with this structure */
141 freeaddrinfo(servinfo);
142 return fd;
143}
144/*---------------------------------------------------------------------------*/
145int
146is_sensible_string(const unsigned char *s, int len)
147{
148 int i;
149 for(i = 1; i < len; i++) {
150 if(s[i] == 0 || s[i] == '\r' || s[i] == '\n' || s[i] == '\t') {
151 continue;
152 } else if(s[i] < ' ' || '~' < s[i]) {
153 return 0;
154 }
155 }
156 return 1;
157}
158/*---------------------------------------------------------------------------*/
159void
160slip_packet_input(unsigned char *data, int len)
161{
162 packetbuf_copyfrom(data, len);
163 if(slip_config_verbose > 0) {
164 printf("Packet input over SLIP: %d\n", len);
165 }
166 NETSTACK_MAC.input();
167}
168/*---------------------------------------------------------------------------*/
169/*
170 * Read from serial, when we have a packet call slip_packet_input. No output
171 * buffering, input buffered by stdio.
172 */
173void
174serial_input(FILE *inslip)
175{
176 static unsigned char inbuf[2048];
177 static int inbufptr = 0;
178 int ret, i;
179 unsigned char c;
180
181#ifdef linux
182 ret = fread(&c, 1, 1, inslip);
183 if(ret == -1 || ret == 0) {
184 err(EXIT_FAILURE, "serial_input: read");
185 }
186 goto after_fread;
187#endif
188
189read_more:
190 if(inbufptr >= sizeof(inbuf)) {
191 fprintf(stderr, "*** dropping large %d byte packet\n", inbufptr);
192 inbufptr = 0;
193 }
194 ret = fread(&c, 1, 1, inslip);
195#ifdef linux
196after_fread:
197#endif
198 if(ret == -1) {
199 err(EXIT_FAILURE, "serial_input: read");
200 }
201 if(ret == 0) {
202 clearerr(inslip);
203 return;
204 }
205 slip_received++;
206 switch(c) {
207 case SLIP_END:
208 if(inbufptr > 0) {
209 if(inbuf[0] == '!') {
210 command_context = CMD_CONTEXT_RADIO;
211 cmd_input(inbuf, inbufptr);
212 } else if(inbuf[0] == '?') {
213#define DEBUG_LINE_MARKER '\r'
214 } else if(inbuf[0] == DEBUG_LINE_MARKER) {
215 fwrite(inbuf + 1, inbufptr - 1, 1, stdout);
216 } else if(is_sensible_string(inbuf, inbufptr)) {
217 if(slip_config_verbose == 1) { /* strings already echoed below for verbose>1 */
218 fwrite(inbuf, inbufptr, 1, stdout);
219 }
220 } else {
221 if(slip_config_verbose > 2) {
222 printf("Packet from SLIP of length %d - write TUN\n", inbufptr);
223 if(slip_config_verbose > 4) {
224#if WIRESHARK_IMPORT_FORMAT
225 printf("0000");
226 for(i = 0; i < inbufptr; i++) {
227 printf(" %02x", inbuf[i]);
228 }
229#else
230 printf(" ");
231 for(i = 0; i < inbufptr; i++) {
232 printf("%02x", inbuf[i]);
233 if((i & 3) == 3) {
234 printf(" ");
235 }
236 if((i & 15) == 15) {
237 printf("\n ");
238 }
239 }
240#endif
241 printf("\n");
242 }
243 }
244 slip_packet_input(inbuf, inbufptr);
245 }
246 inbufptr = 0;
247 }
248 break;
249
250 case SLIP_ESC:
251 if(fread(&c, 1, 1, inslip) != 1) {
252 clearerr(inslip);
253 /* Put ESC back and give up! */
254 ungetc(SLIP_ESC, inslip);
255 return;
256 }
257
258 switch(c) {
259 case SLIP_ESC_END:
260 c = SLIP_END;
261 break;
262 case SLIP_ESC_ESC:
263 c = SLIP_ESC;
264 break;
265 }
266 /* FALLTHROUGH */
267 default:
268 inbuf[inbufptr++] = c;
269
270 /* Echo lines as they are received for verbose=2,3,5+ */
271 /* Echo all printable characters for verbose==4 */
272 if(slip_config_verbose == 4) {
273 if(c == 0 || c == '\r' || c == '\n' || c == '\t' || (c >= ' ' && c <= '~')) {
274 fwrite(&c, 1, 1, stdout);
275 }
276 } else if(slip_config_verbose >= 2) {
277 if(c == '\n' && is_sensible_string(inbuf, inbufptr)) {
278 fwrite(inbuf, inbufptr, 1, stdout);
279 inbufptr = 0;
280 }
281 }
282 break;
283 }
284
285 goto read_more;
286}
287unsigned char slip_buf[2048];
288int slip_end, slip_begin, slip_packet_end, slip_packet_count;
289static struct timer send_delay_timer;
290/* delay between slip packets */
291static clock_time_t send_delay = SEND_DELAY;
292/*---------------------------------------------------------------------------*/
293static void
294slip_send(int fd, unsigned char c)
295{
296 if(slip_end >= sizeof(slip_buf)) {
297 err(EXIT_FAILURE, "slip_send overflow");
298 }
299 slip_buf[slip_end] = c;
300 slip_end++;
301 slip_sent++;
302 if(c == SLIP_END) {
303 /* Full packet received. */
304 slip_packet_count++;
305 if(slip_packet_end == 0) {
306 slip_packet_end = slip_end;
307 }
308 }
309}
310/*---------------------------------------------------------------------------*/
311int
312slip_empty()
313{
314 return slip_packet_end == 0;
315}
316/*---------------------------------------------------------------------------*/
317void
318slip_flushbuf(int fd)
319{
320 int n;
321
322 if(slip_empty()) {
323 return;
324 }
325
326 n = write(fd, slip_buf + slip_begin, slip_packet_end - slip_begin);
327
328 if(n == -1 && errno != EAGAIN) {
329 err(EXIT_FAILURE, "slip_flushbuf write failed");
330 } else if(n == -1) {
331 PROGRESS("Q"); /* Outqueue is full! */
332 } else {
333 slip_begin += n;
334 if(slip_begin == slip_packet_end) {
335 slip_packet_count--;
336 if(slip_end > slip_packet_end) {
337 memmove(slip_buf, slip_buf + slip_packet_end,
338 slip_end - slip_packet_end);
339 }
340 slip_end -= slip_packet_end;
341 slip_begin = slip_packet_end = 0;
342 if(slip_end > 0) {
343 /* Find end of next slip packet */
344 for(n = 1; n < slip_end; n++) {
345 if(slip_buf[n] == SLIP_END) {
346 slip_packet_end = n + 1;
347 break;
348 }
349 }
350 /* a delay between slip packets to avoid losing data */
351 if(send_delay > 0) {
352 timer_set(&send_delay_timer, send_delay);
353 }
354 }
355 }
356 }
357}
358/*---------------------------------------------------------------------------*/
359static void
360write_to_serial(int outfd, const uint8_t *inbuf, int len)
361{
362 const uint8_t *p = inbuf;
363 int i;
364
365 if(slip_config_verbose > 2) {
366 printf("Packet from TUN of length %d - write SLIP\n", len);
367 if(slip_config_verbose > 4) {
368#if WIRESHARK_IMPORT_FORMAT
369 printf("0000");
370 for(i = 0; i < len; i++) {
371 printf(" %02x", p[i]);
372 }
373#else
374 printf(" ");
375 for(i = 0; i < len; i++) {
376 printf("%02x", p[i]);
377 if((i & 3) == 3) {
378 printf(" ");
379 }
380 if((i & 15) == 15) {
381 printf("\n ");
382 }
383 }
384#endif
385 printf("\n");
386 }
387 }
388
389 /* It would be ``nice'' to send a SLIP_END here but it's not
390 * really necessary.
391 */
392 /* slip_send(outfd, SLIP_END); */
393
394 for(i = 0; i < len; i++) {
395 switch(p[i]) {
396 case SLIP_END:
397 slip_send(outfd, SLIP_ESC);
398 slip_send(outfd, SLIP_ESC_END);
399 break;
400 case SLIP_ESC:
401 slip_send(outfd, SLIP_ESC);
402 slip_send(outfd, SLIP_ESC_ESC);
403 break;
404 default:
405 slip_send(outfd, p[i]);
406 break;
407 }
408 }
409 slip_send(outfd, SLIP_END);
410 PROGRESS("t");
411}
412/*---------------------------------------------------------------------------*/
413/* writes an 802.15.4 packet to slip-radio */
414void
415write_to_slip(const uint8_t *buf, int len)
416{
417 if(slipfd > 0) {
418 write_to_serial(slipfd, buf, len);
419 }
420}
421/*---------------------------------------------------------------------------*/
422static void
423stty_telos(int fd)
424{
425 struct termios tty;
426 speed_t speed = slip_config_b_rate;
427 int i;
428
429 if(tcflush(fd, TCIOFLUSH) == -1) {
430 err(EXIT_FAILURE, "tcflush");
431 }
432
433 if(tcgetattr(fd, &tty) == -1) {
434 err(1, "tcgetattr");
435 }
436
437 cfmakeraw(&tty);
438
439 /* Nonblocking read. */
440 tty.c_cc[VTIME] = 0;
441 tty.c_cc[VMIN] = 0;
442 if(slip_config_flowcontrol) {
443 tty.c_cflag |= CRTSCTS;
444 } else {
445 tty.c_cflag &= ~CRTSCTS;
446 }
447 tty.c_cflag &= ~HUPCL;
448 tty.c_cflag &= ~CLOCAL;
449
450 cfsetispeed(&tty, speed);
451 cfsetospeed(&tty, speed);
452
453 if(tcsetattr(fd, TCSAFLUSH, &tty) == -1) {
454 err(EXIT_FAILURE, "tcsetattr");
455 }
456
457#if 1
458 /* Nonblocking read and write. */
459 /* if(fcntl(fd, F_SETFL, O_NONBLOCK) == -1) err(EXIT_FAILURE, "fcntl"); */
460
461 tty.c_cflag |= CLOCAL;
462 if(tcsetattr(fd, TCSAFLUSH, &tty) == -1) {
463 err(EXIT_FAILURE, "tcsetattr");
464 }
465
466 i = TIOCM_DTR;
467 if(ioctl(fd, TIOCMBIS, &i) == -1) {
468 err(EXIT_FAILURE, "ioctl");
469 }
470#endif
471
472 usleep(10 * 1000); /* Wait for hardware 10ms. */
473
474 /* Flush input and output buffers. */
475 if(tcflush(fd, TCIOFLUSH) == -1) {
476 err(EXIT_FAILURE, "tcflush");
477 }
478}
479/*---------------------------------------------------------------------------*/
480static int
481set_fd(fd_set *rset, fd_set *wset)
482{
483 /* Anything to flush? */
484 if(!slip_empty() && (send_delay == 0 || timer_expired(&send_delay_timer))) {
485 FD_SET(slipfd, wset);
486 }
487
488 FD_SET(slipfd, rset); /* Read from slip ASAP! */
489 return 1;
490}
491/*---------------------------------------------------------------------------*/
492static void
493handle_fd(fd_set *rset, fd_set *wset)
494{
495 if(FD_ISSET(slipfd, rset)) {
496 serial_input(inslip);
497 }
498
499 if(FD_ISSET(slipfd, wset)) {
500 slip_flushbuf(slipfd);
501 }
502}
503/*---------------------------------------------------------------------------*/
504static const struct select_callback slip_callback = { set_fd, handle_fd };
505/*---------------------------------------------------------------------------*/
506void
507slip_init(void)
508{
509 setvbuf(stdout, NULL, _IOLBF, 0); /* Line buffered output. */
510
511 if(slip_config_host != NULL) {
512 if(slip_config_port == NULL) {
513 slip_config_port = "60001";
514 }
515 slipfd = connect_to_server(slip_config_host, slip_config_port);
516 if(slipfd == -1) {
517 err(EXIT_FAILURE, "can't connect to ``%s:%s''", slip_config_host,
518 slip_config_port);
519 }
520 } else if(slip_config_siodev != NULL) {
521 if(strcmp(slip_config_siodev, "null") == 0) {
522 /* Disable slip */
523 return;
524 }
525 slipfd = open(slip_config_siodev, O_RDWR | O_NONBLOCK);
526 if(slipfd == -1) {
527 err(EXIT_FAILURE, "can't open siodev ``%s''", slip_config_siodev);
528 }
529 } else {
530 static const char *siodevs[] = {
531 "/dev/ttyUSB0", "/dev/cuaU0", "/dev/ucom0" /* linux, fbsd6, fbsd5 */
532 };
533 for(int i = 0; i < 3; i++) {
534 slip_config_siodev = siodevs[i];
535 slipfd = open(slip_config_siodev, O_RDWR | O_NONBLOCK);
536 if(slipfd != -1) {
537 break;
538 }
539 }
540 if(slipfd == -1) {
541 err(EXIT_FAILURE, "can't open siodev");
542 }
543 }
544
545 select_set_callback(slipfd, &slip_callback);
546
547 if(slip_config_host != NULL) {
548 fprintf(stderr, "********SLIP opened to ``%s:%s''\n", slip_config_host,
549 slip_config_port);
550 } else {
551 fprintf(stderr, "********SLIP started on ``/dev/%s''\n", slip_config_siodev);
552 stty_telos(slipfd);
553 }
554
555 timer_set(&send_delay_timer, 0);
556 slip_send(slipfd, SLIP_END);
557 inslip = fdopen(slipfd, "r");
558 if(inslip == NULL) {
559 err(EXIT_FAILURE, "slip_init: fdopen");
560 }
561}
562/*---------------------------------------------------------------------------*/
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