Skip to content

Commit cbeac32

Browse files
authored
Merge pull request contiki-ng#148 from nfi/contrib/native-tun
Platform native: Avoid trying to use the tun when the tun is not available.
2 parents 77f4e75 + 5c6117e commit cbeac32

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

arch/cpu/native/net/tun6-net.c

+19-3
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ static char config_tundev[64] = "tun0";
7070

7171

7272
#ifndef __CYGWIN__
73-
static int tunfd;
73+
static int tunfd = -1;
7474

7575
static int set_fd(fd_set *rset, fd_set *wset);
7676
static void handle_fd(fd_set *rset, fd_set *wset);
@@ -234,7 +234,7 @@ static int
234234
tun_output(uint8_t *data, int len)
235235
{
236236
/* fprintf(stderr, "*** Writing to tun...%d\n", len); */
237-
if(write(tunfd, data, len) != len) {
237+
if(tunfd != -1 && write(tunfd, data, len) != len) {
238238
err(1, "serial_to_tun: write");
239239
return -1;
240240
}
@@ -245,8 +245,15 @@ static int
245245
tun_input(unsigned char *data, int maxlen)
246246
{
247247
int size;
248-
if((size = read(tunfd, data, maxlen)) == -1)
248+
249+
if(tunfd == -1) {
250+
/* tun is not open */
251+
return 0;
252+
}
253+
254+
if((size = read(tunfd, data, maxlen)) == -1) {
249255
err(1, "tun_input: read");
256+
}
250257
return size;
251258
}
252259

@@ -267,6 +274,10 @@ output(const linkaddr_t *localdest)
267274
static int
268275
set_fd(fd_set *rset, fd_set *wset)
269276
{
277+
if(tunfd == -1) {
278+
return 0;
279+
}
280+
270281
FD_SET(tunfd, rset);
271282
return 1;
272283
}
@@ -278,6 +289,11 @@ handle_fd(fd_set *rset, fd_set *wset)
278289
{
279290
int size;
280291

292+
if(tunfd == -1) {
293+
/* tun is not open */
294+
return;
295+
}
296+
281297
LOG_INFO("Tun6-handle FD\n");
282298

283299
if(FD_ISSET(tunfd, rset)) {

0 commit comments

Comments
 (0)