diff --git a/arch/cpu/native/net/tun6-net.c b/arch/cpu/native/net/tun6-net.c index 33a7677e3dbb60084840ea6eaab195e04e3e1a1c..d46d91d4279a1932e76ea168bb36dc0bedd6e930 100644 --- a/arch/cpu/native/net/tun6-net.c +++ b/arch/cpu/native/net/tun6-net.c @@ -70,7 +70,7 @@ static char config_tundev[64] = "tun0"; #ifndef __CYGWIN__ -static int tunfd; +static int tunfd = -1; static int set_fd(fd_set *rset, fd_set *wset); static void handle_fd(fd_set *rset, fd_set *wset); @@ -234,7 +234,7 @@ static int tun_output(uint8_t *data, int len) { /* fprintf(stderr, "*** Writing to tun...%d\n", len); */ - if(write(tunfd, data, len) != len) { + if(tunfd != -1 && write(tunfd, data, len) != len) { err(1, "serial_to_tun: write"); return -1; } @@ -245,8 +245,15 @@ static int tun_input(unsigned char *data, int maxlen) { int size; - if((size = read(tunfd, data, maxlen)) == -1) + + if(tunfd == -1) { + /* tun is not open */ + return 0; + } + + if((size = read(tunfd, data, maxlen)) == -1) { err(1, "tun_input: read"); + } return size; } @@ -267,6 +274,10 @@ output(const linkaddr_t *localdest) static int set_fd(fd_set *rset, fd_set *wset) { + if(tunfd == -1) { + return 0; + } + FD_SET(tunfd, rset); return 1; } @@ -278,6 +289,11 @@ handle_fd(fd_set *rset, fd_set *wset) { int size; + if(tunfd == -1) { + /* tun is not open */ + return; + } + LOG_INFO("Tun6-handle FD\n"); if(FD_ISSET(tunfd, rset)) {