Skip to content

Commit

Permalink
Merge pull request #448 from appneta/Enhancement_#416_non-blocking_STDIN
Browse files Browse the repository at this point in the history
Enhancement #416 non blocking stdin
  • Loading branch information
fklassen authored Jan 22, 2018
2 parents 4b96ddd + cc7ae9f commit e8d05fb
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
- Travis CI build fails due to new build images (#432)
- Unable to build with libpcap 1.8.1 (#430)
- Unable to tcprewrite range of ports with --portmap (#422)
- Avoid non-blocking behaviour when using STDIN (#416)
- pcap containing >1020 packets produces invalid cache file (#415)
- heap-buffer-overflow in get_l2protocol (#410)
- heap-buffer-overflow in packet2tree (#409)
Expand Down
22 changes: 22 additions & 0 deletions src/common/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
#include <ctype.h>
#include <unistd.h>

#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif

#ifdef DEBUG
extern int debug;
#endif
Expand Down Expand Up @@ -335,3 +339,21 @@ int tcpr_random(uint32_t *seed)

return result;
}

/**
* #416 - Ensure STDIN is not left in non-blocking mode after closing
* a program. BSD and Unix derivatives should utilize `FIONBIO` due to known
* issues with reading from tty with a 0 byte read returning -1 opposed to 0.
*/
void restore_stdin(void)
{
#ifdef FIONBIO
int nb = 0;

ioctl(0, FIONBIO, &nb);
#else
fcntl(0, F_SETFL, fcntl(0, F_GETFL) | O_NONBLOCK);
#endif /* FIONBIO */


}
1 change: 1 addition & 0 deletions src/common/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ int read_hexstring(const char *l2string, u_char *hex, const int hexlen);
void packet_stats(const tcpreplay_stats_t *stats);
int format_date_time(struct timeval *when, char *buf, size_t len);
int tcpr_random(uint32_t *seed);
void restore_stdin(void);

/* our "safe" implimentations of functions which allocate memory */
#define safe_malloc(x) _our_safe_malloc(x, __FUNCTION__, __LINE__, __FILE__)
Expand Down
1 change: 1 addition & 0 deletions src/tcpbridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ main(int argc, char *argv[])
tcpdump_close(options.tcpdump);
#endif

restore_stdin();
return 0;
}

Expand Down
6 changes: 2 additions & 4 deletions src/tcpcapinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -332,13 +332,11 @@ main(int argc, char *argv[])
} else if (backwards && caplentoobig) {
printf("BAD_TS|TOOBIG");
}

}

}

exit(0);

restore_stdin();
return 0;
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/tcpliveplay.c
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ main(int argc, char **argv)
printf("----------------------------------------------------------\n\n");

free(sched);
restore_stdin();
return 0;
}
/*end of main() function*/
Expand Down
3 changes: 2 additions & 1 deletion src/tcpprep.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,9 @@ main(int argc, char *argv[])

/* close cache file */
close(out_file);
return 0;

restore_stdin();
return 0;
}


Expand Down
1 change: 1 addition & 0 deletions src/tcpreplay.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ main(int argc, char *argv[])
}
}
tcpreplay_close(ctx);
restore_stdin();
return 0;
} /* main() */

Expand Down
2 changes: 2 additions & 0 deletions src/tcprewrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ main(int argc, char *argv[])
#ifdef ENABLE_DMALLOC
dmalloc_shutdown();
#endif

restore_stdin();
return 0;
}

Expand Down

0 comments on commit e8d05fb

Please sign in to comment.