From 5c6117e698206fd14c6359135ffe1039388f4799 Mon Sep 17 00:00:00 2001
From: Niclas Finne <nfi@sics.se>
Date: Thu, 2 Nov 2017 01:25:47 +0100
Subject: [PATCH] Platform native: Avoid trying to use the tun when the tun is
not available. This allows a native platform application to run without
network.
---
arch/cpu/native/net/tun6-net.c | 22 +++++++++++++++++++---
1 file changed, 19 insertions(+), 3 deletions(-)
diff --git a/arch/cpu/native/net/tun6-net.c b/arch/cpu/native/net/tun6-net.c
index 33a7677e3d..d46d91d427 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)) {
--
GitLab