@@ -70,7 +70,7 @@ static char config_tundev[64] = "tun0";
70
70
71
71
72
72
#ifndef __CYGWIN__
73
- static int tunfd ;
73
+ static int tunfd = -1 ;
74
74
75
75
static int set_fd (fd_set * rset , fd_set * wset );
76
76
static void handle_fd (fd_set * rset , fd_set * wset );
@@ -234,7 +234,7 @@ static int
234
234
tun_output (uint8_t * data , int len )
235
235
{
236
236
/* fprintf(stderr, "*** Writing to tun...%d\n", len); */
237
- if (write (tunfd , data , len ) != len ) {
237
+ if (tunfd != -1 && write (tunfd , data , len ) != len ) {
238
238
err (1 , "serial_to_tun: write" );
239
239
return -1 ;
240
240
}
@@ -245,8 +245,15 @@ static int
245
245
tun_input (unsigned char * data , int maxlen )
246
246
{
247
247
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 ) {
249
255
err (1 , "tun_input: read" );
256
+ }
250
257
return size ;
251
258
}
252
259
@@ -267,6 +274,10 @@ output(const linkaddr_t *localdest)
267
274
static int
268
275
set_fd (fd_set * rset , fd_set * wset )
269
276
{
277
+ if (tunfd == -1 ) {
278
+ return 0 ;
279
+ }
280
+
270
281
FD_SET (tunfd , rset );
271
282
return 1 ;
272
283
}
@@ -278,6 +289,11 @@ handle_fd(fd_set *rset, fd_set *wset)
278
289
{
279
290
int size ;
280
291
292
+ if (tunfd == -1 ) {
293
+ /* tun is not open */
294
+ return ;
295
+ }
296
+
281
297
LOG_INFO ("Tun6-handle FD\n" );
282
298
283
299
if (FD_ISSET (tunfd , rset )) {
0 commit comments