Merge pull request #676 from mcr/mcr_tunslip1

some additions to tunslip6 to make it a bit easier to diagnose problems with it
This commit is contained in:
Simon Duquennoy 2015-09-28 13:19:26 +02:00
commit 2398b1c287

View File

@ -34,7 +34,7 @@
/* Below define allows importing saved output into Wireshark as "Raw IP" packet type */ /* Below define allows importing saved output into Wireshark as "Raw IP" packet type */
#define WIRESHARK_IMPORT_FORMAT 1 #define WIRESHARK_IMPORT_FORMAT 1
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdarg.h> #include <stdarg.h>
@ -62,7 +62,7 @@ const char *netmask;
int slipfd = 0; int slipfd = 0;
uint16_t basedelay=0,delaymsec=0; uint16_t basedelay=0,delaymsec=0;
uint32_t startsec,startmsec,delaystartsec,delaystartmsec; uint32_t startsec,startmsec,delaystartsec,delaystartmsec;
int timestamp = 0, flowcontrol=0; int timestamp = 0, flowcontrol=0, showprogress=0;
int ssystem(const char *fmt, ...) int ssystem(const char *fmt, ...)
__attribute__((__format__ (__printf__, 1, 2))); __attribute__((__format__ (__printf__, 1, 2)));
@ -71,8 +71,7 @@ void write_to_serial(int outfd, void *inbuf, int len);
void slip_send(int fd, unsigned char c); void slip_send(int fd, unsigned char c);
void slip_send_char(int fd, unsigned char c); void slip_send_char(int fd, unsigned char c);
//#define PROGRESS(s) fprintf(stderr, s) #define PROGRESS(s) if(showprogress) fprintf(stderr, s)
#define PROGRESS(s) do { } while (0)
char tundev[1024] = { "" }; char tundev[1024] = { "" };
@ -116,7 +115,7 @@ stamptime(void)
time_t t; time_t t;
struct tm *tmp; struct tm *tmp;
char timec[20]; char timec[20];
gettimeofday(&tv, NULL) ; gettimeofday(&tv, NULL) ;
msecs=tv.tv_usec/1000; msecs=tv.tv_usec/1000;
secs=tv.tv_sec; secs=tv.tv_sec;
@ -187,7 +186,7 @@ serial_to_tun(FILE *inslip, int outfd)
clearerr(inslip); clearerr(inslip);
return; return;
} }
/* fprintf(stderr, ".");*/ PROGRESS(".");
switch(c) { switch(c) {
case SLIP_END: case SLIP_END:
if(inbufptr > 0) { if(inbufptr > 0) {
@ -225,8 +224,7 @@ serial_to_tun(FILE *inslip, int outfd)
inet_pton(AF_INET6, ipaddr, &addr); inet_pton(AF_INET6, ipaddr, &addr);
if(timestamp) stamptime(); if(timestamp) stamptime();
fprintf(stderr,"*** Address:%s => %02x%02x:%02x%02x:%02x%02x:%02x%02x\n", fprintf(stderr,"*** Address:%s => %02x%02x:%02x%02x:%02x%02x:%02x%02x\n",
// printf("*** Address:%s => %02x%02x:%02x%02x:%02x%02x:%02x%02x\n", ipaddr,
ipaddr,
addr.s6_addr[0], addr.s6_addr[1], addr.s6_addr[0], addr.s6_addr[1],
addr.s6_addr[2], addr.s6_addr[3], addr.s6_addr[2], addr.s6_addr[3],
addr.s6_addr[4], addr.s6_addr[5], addr.s6_addr[4], addr.s6_addr[5],
@ -240,13 +238,21 @@ serial_to_tun(FILE *inslip, int outfd)
slip_send(slipfd, SLIP_END); slip_send(slipfd, SLIP_END);
} }
#define DEBUG_LINE_MARKER '\r' #define DEBUG_LINE_MARKER '\r'
} else if(uip.inbuf[0] == DEBUG_LINE_MARKER) { if(uip.inbuf[0] == DEBUG_LINE_MARKER ||
fwrite(uip.inbuf + 1, inbufptr - 1, 1, stdout); is_sensible_string(uip.inbuf, inbufptr)) {
} else if(is_sensible_string(uip.inbuf, inbufptr)) { unsigned char *out = uip.inbuf;
if(verbose==1) { /* strings already echoed below for verbose>1 */ unsigned int len = inbufptr;
if (timestamp) stamptime(); if(uip.inbuf[0] == DEBUG_LINE_MARKER) {
fwrite(uip.inbuf, inbufptr, 1, stdout); out++;
} len--;
}
fprintf(stderr, "\n***");
fwrite(out, len, 1, stderr);
fprintf(stderr, "***\n");
} else {
fprintf(stderr,
"serial_to_tun: drop packet len=%d\n", inbufptr);
}
} else { } else {
if(verbose>2) { if(verbose>2) {
if (timestamp) stamptime(); if (timestamp) stamptime();
@ -310,7 +316,7 @@ serial_to_tun(FILE *inslip, int outfd)
if(c=='\n') if(timestamp) stamptime(); if(c=='\n') if(timestamp) stamptime();
} }
} }
break; break;
} }
@ -358,7 +364,7 @@ void
slip_flushbuf(int fd) slip_flushbuf(int fd)
{ {
int n; int n;
if(slip_empty()) { if(slip_empty()) {
return; return;
} }
@ -514,6 +520,7 @@ tun_alloc(char *dev, int tap)
int fd, err; int fd, err;
if( (fd = open("/dev/net/tun", O_RDWR)) < 0 ) { if( (fd = open("/dev/net/tun", O_RDWR)) < 0 ) {
perror("can not open /dev/net/tun");
return -1; return -1;
} }
@ -530,8 +537,12 @@ tun_alloc(char *dev, int tap)
if((err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ) { if((err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ) {
close(fd); close(fd);
fprintf(stderr, "can not tunsetiff to %s (flags=%08x): %s\n", dev, ifr.ifr_flags,
strerror(errno));
return err; return err;
} }
/* get resulting tunnel name */
strcpy(dev, ifr.ifr_name); strcpy(dev, ifr.ifr_name);
return fd; return fd;
} }
@ -636,7 +647,7 @@ ifconf(const char *tundev, const char *ipaddr)
} else { } else {
cc=0; cc=0;
digit = c-'0'; digit = c-'0';
if (digit > 9) if (digit > 9)
digit = 10 + (c & 0xdf) - 'A'; digit = 10 + (c & 0xdf) - 'A';
a[ai] = (a[ai] << 4) + digit; a[ai] = (a[ai] << 4) + digit;
} }
@ -698,13 +709,14 @@ main(int argc, char **argv)
const char *port = NULL; const char *port = NULL;
const char *prog; const char *prog;
int baudrate = -2; int baudrate = -2;
int ipa_enable = 1;
int tap = 0; int tap = 0;
slipfd = 0; slipfd = 0;
prog = argv[0]; prog = argv[0];
setvbuf(stdout, NULL, _IOLBF, 0); /* Line buffered output. */ setvbuf(stdout, NULL, _IOLBF, 0); /* Line buffered output. */
while((c = getopt(argc, argv, "B:HLhs:t:v::d::a:p:T")) != -1) { while((c = getopt(argc, argv, "B:HILPhs:t:v::d::a:p:T")) != -1) {
switch(c) { switch(c) {
case 'B': case 'B':
baudrate = atoi(optarg); baudrate = atoi(optarg);
@ -713,11 +725,15 @@ main(int argc, char **argv)
case 'H': case 'H':
flowcontrol=1; flowcontrol=1;
break; break;
case 'L': case 'L':
timestamp=1; timestamp=1;
break; break;
case 'P':
showprogress=1;
break;
case 's': case 's':
if(strncmp("/dev/", optarg, 5) == 0) { if(strncmp("/dev/", optarg, 5) == 0) {
siodev = optarg + 5; siodev = optarg + 5;
@ -726,6 +742,11 @@ main(int argc, char **argv)
} }
break; break;
case 'I':
ipa_enable = 0;
fprintf(stderr, "Will not inquire about IP address using IPA=\n");
break;
case 't': case 't':
if(strncmp("/dev/", optarg, 5) == 0) { if(strncmp("/dev/", optarg, 5) == 0) {
strncpy(tundev, optarg + 5, sizeof(tundev)); strncpy(tundev, optarg + 5, sizeof(tundev));
@ -755,7 +776,7 @@ main(int argc, char **argv)
case 'T': case 'T':
tap = 1; tap = 1;
break; break;
case '?': case '?':
case 'h': case 'h':
default: default:
@ -916,7 +937,7 @@ exit(1);
if(inslip == NULL) err(1, "main: fdopen"); if(inslip == NULL) err(1, "main: fdopen");
tunfd = tun_alloc(tundev, tap); tunfd = tun_alloc(tundev, tap);
if(tunfd == -1) err(1, "main: open"); if(tunfd == -1) err(1, "main: open /dev/tun");
if (timestamp) stamptime(); if (timestamp) stamptime();
fprintf(stderr, "opened %s device ``/dev/%s''\n", fprintf(stderr, "opened %s device ``/dev/%s''\n",
tap ? "tap" : "tun", tundev); tap ? "tap" : "tun", tundev);
@ -933,16 +954,15 @@ exit(1);
FD_ZERO(&rset); FD_ZERO(&rset);
FD_ZERO(&wset); FD_ZERO(&wset);
/* do not send IPA all the time... - add get MAC later... */ if(got_sigalarm && ipa_enable) {
/* if(got_sigalarm) { */ /* Send "?IPA". */
/* /\* Send "?IPA". *\/ */ slip_send(slipfd, '?');
/* slip_send(slipfd, '?'); */ slip_send(slipfd, 'I');
/* slip_send(slipfd, 'I'); */ slip_send(slipfd, 'P');
/* slip_send(slipfd, 'P'); */ slip_send(slipfd, 'A');
/* slip_send(slipfd, 'A'); */ slip_send(slipfd, SLIP_END);
/* slip_send(slipfd, SLIP_END); */ got_sigalarm = 0;
/* got_sigalarm = 0; */ }
/* } */
if(!slip_empty()) { /* Anything to flush? */ if(!slip_empty()) { /* Anything to flush? */
FD_SET(slipfd, &wset); FD_SET(slipfd, &wset);
@ -950,7 +970,7 @@ exit(1);
FD_SET(slipfd, &rset); /* Read from slip ASAP! */ FD_SET(slipfd, &rset); /* Read from slip ASAP! */
if(slipfd > maxfd) maxfd = slipfd; if(slipfd > maxfd) maxfd = slipfd;
/* We only have one packet at a time queued for slip output. */ /* We only have one packet at a time queued for slip output. */
if(slip_empty()) { if(slip_empty()) {
FD_SET(tunfd, &rset); FD_SET(tunfd, &rset);
@ -964,12 +984,12 @@ exit(1);
if(FD_ISSET(slipfd, &rset)) { if(FD_ISSET(slipfd, &rset)) {
serial_to_tun(inslip, tunfd); serial_to_tun(inslip, tunfd);
} }
if(FD_ISSET(slipfd, &wset)) { if(FD_ISSET(slipfd, &wset)) {
slip_flushbuf(slipfd); slip_flushbuf(slipfd);
sigalarm_reset(); if(ipa_enable) sigalarm_reset();
} }
/* Optional delay between outgoing packets */ /* Optional delay between outgoing packets */
/* Base delay times number of 6lowpan fragments to be sent */ /* Base delay times number of 6lowpan fragments to be sent */
if(delaymsec) { if(delaymsec) {
@ -985,7 +1005,7 @@ exit(1);
if(slip_empty() && FD_ISSET(tunfd, &rset)) { if(slip_empty() && FD_ISSET(tunfd, &rset)) {
size=tun_to_serial(tunfd, slipfd); size=tun_to_serial(tunfd, slipfd);
slip_flushbuf(slipfd); slip_flushbuf(slipfd);
sigalarm_reset(); if(ipa_enable) sigalarm_reset();
if(basedelay) { if(basedelay) {
struct timeval tv; struct timeval tv;
gettimeofday(&tv, NULL) ; gettimeofday(&tv, NULL) ;