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

@ -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)));
@ -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] = { "" };
@ -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) {
@ -225,7 +224,6 @@ 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,
addr.s6_addr[0], addr.s6_addr[1],
addr.s6_addr[2], addr.s6_addr[3],
@ -240,12 +238,20 @@ 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) {
@ -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;
}
@ -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;
}
@ -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);
@ -718,6 +730,10 @@ main(int argc, char **argv)
timestamp=1;
break;
case 'P':
showprogress=1;
break;
case 's':
if(strncmp("/dev/", optarg, 5) == 0) {
siodev = optarg + 5;
@ -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));
@ -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);
@ -933,16 +954,15 @@ 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);
@ -967,7 +987,7 @@ exit(1);
if(FD_ISSET(slipfd, &wset)) {
slip_flushbuf(slipfd);
sigalarm_reset();
if(ipa_enable) sigalarm_reset();
}
/* Optional delay between outgoing packets */
@ -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) ;