Skip to content

Commit

Permalink
Merge pull request #676 from mcr/mcr_tunslip1
Browse files Browse the repository at this point in the history
some additions to tunslip6 to make it a bit easier to diagnose problems with it
  • Loading branch information
simonduq committed Sep 28, 2015
2 parents 222a754 + 11c3dcc commit 2398b1c
Showing 1 changed file with 57 additions and 37 deletions.
94 changes: 57 additions & 37 deletions tools/tunslip6.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

/* Below define allows importing saved output into Wireshark as "Raw IP" packet type */
#define WIRESHARK_IMPORT_FORMAT 1

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
Expand Down Expand Up @@ -62,7 +62,7 @@ const char *netmask;
int slipfd = 0;
uint16_t basedelay=0,delaymsec=0;
uint32_t startsec,startmsec,delaystartsec,delaystartmsec;
int timestamp = 0, flowcontrol=0;
int timestamp = 0, flowcontrol=0, showprogress=0;

int ssystem(const char *fmt, ...)
__attribute__((__format__ (__printf__, 1, 2)));
Expand All @@ -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_char(int fd, unsigned char c);

//#define PROGRESS(s) fprintf(stderr, s)
#define PROGRESS(s) do { } while (0)
#define PROGRESS(s) if(showprogress) fprintf(stderr, s)

char tundev[1024] = { "" };

Expand Down Expand Up @@ -116,7 +115,7 @@ stamptime(void)
time_t t;
struct tm *tmp;
char timec[20];

gettimeofday(&tv, NULL) ;
msecs=tv.tv_usec/1000;
secs=tv.tv_sec;
Expand Down Expand Up @@ -187,7 +186,7 @@ serial_to_tun(FILE *inslip, int outfd)
clearerr(inslip);
return;
}
/* fprintf(stderr, ".");*/
PROGRESS(".");
switch(c) {
case SLIP_END:
if(inbufptr > 0) {
Expand Down Expand Up @@ -225,8 +224,7 @@ serial_to_tun(FILE *inslip, int outfd)
inet_pton(AF_INET6, ipaddr, &addr);
if(timestamp) stamptime();
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[2], addr.s6_addr[3],
addr.s6_addr[4], addr.s6_addr[5],
Expand All @@ -240,13 +238,21 @@ serial_to_tun(FILE *inslip, int outfd)
slip_send(slipfd, SLIP_END);
}
#define DEBUG_LINE_MARKER '\r'
} else if(uip.inbuf[0] == DEBUG_LINE_MARKER) {
fwrite(uip.inbuf + 1, inbufptr - 1, 1, stdout);
} else if(is_sensible_string(uip.inbuf, inbufptr)) {
if(verbose==1) { /* strings already echoed below for verbose>1 */
if (timestamp) stamptime();
fwrite(uip.inbuf, inbufptr, 1, stdout);
}
if(uip.inbuf[0] == DEBUG_LINE_MARKER ||
is_sensible_string(uip.inbuf, inbufptr)) {
unsigned char *out = uip.inbuf;
unsigned int len = inbufptr;
if(uip.inbuf[0] == DEBUG_LINE_MARKER) {
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 {
if(verbose>2) {
if (timestamp) stamptime();
Expand Down Expand Up @@ -310,7 +316,7 @@ serial_to_tun(FILE *inslip, int outfd)
if(c=='\n') if(timestamp) stamptime();
}
}

break;
}

Expand Down Expand Up @@ -358,7 +364,7 @@ void
slip_flushbuf(int fd)
{
int n;

if(slip_empty()) {
return;
}
Expand Down Expand Up @@ -514,6 +520,7 @@ tun_alloc(char *dev, int tap)
int fd, err;

if( (fd = open("/dev/net/tun", O_RDWR)) < 0 ) {
perror("can not open /dev/net/tun");
return -1;
}

Expand All @@ -530,8 +537,12 @@ tun_alloc(char *dev, int tap)

if((err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ) {
close(fd);
fprintf(stderr, "can not tunsetiff to %s (flags=%08x): %s\n", dev, ifr.ifr_flags,
strerror(errno));
return err;
}

/* get resulting tunnel name */
strcpy(dev, ifr.ifr_name);
return fd;
}
Expand Down Expand Up @@ -636,7 +647,7 @@ ifconf(const char *tundev, const char *ipaddr)
} else {
cc=0;
digit = c-'0';
if (digit > 9)
if (digit > 9)
digit = 10 + (c & 0xdf) - 'A';
a[ai] = (a[ai] << 4) + digit;
}
Expand Down Expand Up @@ -698,13 +709,14 @@ main(int argc, char **argv)
const char *port = NULL;
const char *prog;
int baudrate = -2;
int ipa_enable = 1;
int tap = 0;
slipfd = 0;

prog = argv[0];
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) {
case 'B':
baudrate = atoi(optarg);
Expand All @@ -713,11 +725,15 @@ main(int argc, char **argv)
case 'H':
flowcontrol=1;
break;

case 'L':
timestamp=1;
break;

case 'P':
showprogress=1;
break;

case 's':
if(strncmp("/dev/", optarg, 5) == 0) {
siodev = optarg + 5;
Expand All @@ -726,6 +742,11 @@ main(int argc, char **argv)
}
break;

case 'I':
ipa_enable = 0;
fprintf(stderr, "Will not inquire about IP address using IPA=\n");
break;

case 't':
if(strncmp("/dev/", optarg, 5) == 0) {
strncpy(tundev, optarg + 5, sizeof(tundev));
Expand Down Expand Up @@ -755,7 +776,7 @@ main(int argc, char **argv)
case 'T':
tap = 1;
break;

case '?':
case 'h':
default:
Expand Down Expand Up @@ -916,7 +937,7 @@ exit(1);
if(inslip == NULL) err(1, "main: fdopen");

tunfd = tun_alloc(tundev, tap);
if(tunfd == -1) err(1, "main: open");
if(tunfd == -1) err(1, "main: open /dev/tun");
if (timestamp) stamptime();
fprintf(stderr, "opened %s device ``/dev/%s''\n",
tap ? "tap" : "tun", tundev);
Expand All @@ -933,24 +954,23 @@ exit(1);
FD_ZERO(&rset);
FD_ZERO(&wset);

/* do not send IPA all the time... - add get MAC later... */
/* if(got_sigalarm) { */
/* /\* Send "?IPA". *\/ */
/* slip_send(slipfd, '?'); */
/* slip_send(slipfd, 'I'); */
/* slip_send(slipfd, 'P'); */
/* slip_send(slipfd, 'A'); */
/* slip_send(slipfd, SLIP_END); */
/* got_sigalarm = 0; */
/* } */
if(got_sigalarm && ipa_enable) {
/* Send "?IPA". */
slip_send(slipfd, '?');
slip_send(slipfd, 'I');
slip_send(slipfd, 'P');
slip_send(slipfd, 'A');
slip_send(slipfd, SLIP_END);
got_sigalarm = 0;
}

if(!slip_empty()) { /* Anything to flush? */
FD_SET(slipfd, &wset);
}

FD_SET(slipfd, &rset); /* Read from slip ASAP! */
if(slipfd > maxfd) maxfd = slipfd;

/* We only have one packet at a time queued for slip output. */
if(slip_empty()) {
FD_SET(tunfd, &rset);
Expand All @@ -964,12 +984,12 @@ exit(1);
if(FD_ISSET(slipfd, &rset)) {
serial_to_tun(inslip, tunfd);
}

if(FD_ISSET(slipfd, &wset)) {
slip_flushbuf(slipfd);
sigalarm_reset();
if(ipa_enable) sigalarm_reset();
}

/* Optional delay between outgoing packets */
/* Base delay times number of 6lowpan fragments to be sent */
if(delaymsec) {
Expand All @@ -985,7 +1005,7 @@ exit(1);
if(slip_empty() && FD_ISSET(tunfd, &rset)) {
size=tun_to_serial(tunfd, slipfd);
slip_flushbuf(slipfd);
sigalarm_reset();
if(ipa_enable) sigalarm_reset();
if(basedelay) {
struct timeval tv;
gettimeofday(&tv, NULL) ;
Expand Down

0 comments on commit 2398b1c

Please sign in to comment.