quit-timer.patch 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. Index: netcat-openbsd-1.89/netcat.c
  2. ===================================================================
  3. --- netcat-openbsd-1.89.orig/netcat.c 2008-06-19 16:49:57.000000000 -0400
  4. +++ netcat-openbsd-1.89/netcat.c 2008-06-19 16:50:46.000000000 -0400
  5. @@ -47,6 +47,7 @@
  6. #include <errno.h>
  7. #include <netdb.h>
  8. #include <poll.h>
  9. +#include <signal.h>
  10. #include <stdarg.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. @@ -82,6 +83,7 @@
  14. int nflag; /* Don't do name look up */
  15. char *Pflag; /* Proxy username */
  16. char *pflag; /* Localport flag */
  17. +int qflag = -1; /* Quit after some secs */
  18. int rflag; /* Random ports flag */
  19. char *sflag; /* Source Address */
  20. int tflag; /* Telnet Emulation */
  21. @@ -114,6 +116,7 @@
  22. static int connect_with_timeout(int fd, const struct sockaddr *sa,
  23. socklen_t salen, int ctimeout);
  24. +static void quit();
  25. int
  26. main(int argc, char *argv[])
  27. @@ -137,7 +140,7 @@
  28. sv = NULL;
  29. while ((ch = getopt(argc, argv,
  30. - "46Ddhi:jklnP:p:rSs:tT:Uuvw:X:x:zC")) != -1) {
  31. + "46Ddhi:jklnP:p:q:rSs:tT:Uuvw:X:x:zC")) != -1) {
  32. switch (ch) {
  33. case '4':
  34. family = AF_INET;
  35. @@ -187,6 +190,9 @@
  36. case 'p':
  37. pflag = optarg;
  38. break;
  39. + case 'q':
  40. + qflag = (int)strtoul(optarg, &endp, 10);
  41. + break;
  42. case 'r':
  43. rflag = 1;
  44. break;
  45. @@ -756,7 +762,17 @@
  46. }
  47. else if (pfd[1].revents & POLLHUP) {
  48. shutdown_wr:
  49. - shutdown(nfd, SHUT_WR);
  50. + /* if the user asked to exit on EOF, do it */
  51. + if (qflag == 0) {
  52. + shutdown(nfd, SHUT_WR);
  53. + close(wfd);
  54. + exit(0);
  55. + }
  56. + /* if user asked to die after a while, arrange for it */
  57. + if (qflag > 0) {
  58. + signal(SIGALRM, quit);
  59. + alarm(qflag);
  60. + }
  61. pfd[1].fd = -1;
  62. pfd[1].events = 0;
  63. }
  64. @@ -951,6 +967,7 @@
  65. \t-n Suppress name/port resolutions\n\
  66. \t-P proxyuser\tUsername for proxy authentication\n\
  67. \t-p port\t Specify local port for remote connects\n\
  68. + \t-q secs\t quit after EOF on stdin and delay of secs\n\
  69. \t-r Randomize remote ports\n "
  70. #ifdef TCP_MD5SIG
  71. " \t-S Enable the TCP MD5 signature option\n"
  72. @@ -979,3 +996,13 @@
  73. if (ret)
  74. exit(1);
  75. }
  76. +
  77. +/*
  78. + * quit()
  79. + * handler for a "-q" timeout (exit 0 instead of 1)
  80. + */
  81. +static void quit()
  82. +{
  83. + /* XXX: should explicitly close fds here */
  84. + exit(0);
  85. +}