1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
Summary: Make timeouts work with glibc 2.x.
Contributor: Joe Pepin <jdp@ll.mit.edu>
Index: netcat-1.10/netcat.c
===================================================================
--- netcat-1.10.orig/netcat.c
+++ netcat-1.10/netcat.c
@@ -59,6 +59,14 @@
#define RAND rand
#endif /* HAVE_RANDOM */
+/* #define POSIX_SETJMP /* If you want timeouts to work under the */
+ /* posixly correct, yet non-standard glibc-2.x*/
+ /* then define this- you may also need it for */
+ /* IRIX, and maybe some others */
+#ifdef LINUX
+#define POSIX_SETJMP
+#endif
+
/* includes: */
#include <sys/time.h> /* timeval, time_t */
#include <setjmp.h> /* jmp_buf et al */
@@ -109,7 +117,11 @@ struct port_poop {
#define PINF struct port_poop
/* globals: */
+#ifdef POSIX_SETJMP
+sigjmp_buf jbuf; /* timer crud */
+#else
jmp_buf jbuf; /* timer crud */
+#endif
int jval = 0; /* timer crud */
int netfd = -1;
int ofd = 0; /* hexdump output fd */
@@ -235,7 +247,11 @@ void tmtravel ()
alarm (0);
if (jval == 0)
bail ("spurious timer interrupt!");
+#ifdef POSIX_SETJMP
+ siglongjmp (jbuf, jval);
+#else
longjmp (jbuf, jval);
+#endif
}
/* arm_timer :
@@ -747,12 +763,21 @@ Linux is also still a loss at 1.3.x it l
/* wrap connect inside a timer, and hit it */
arm_timer (1, o_wait);
+#ifdef POSIX_SETJMP
+ if (sigsetjmp (jbuf,1) == 0) {
+ rr = connect (nnetfd, (SA *)remend, sizeof (SA));
+ } else { /* setjmp: connect failed... */
+ rr = -1;
+ errno = ETIMEDOUT; /* fake it */
+ }
+#else
if (setjmp (jbuf) == 0) {
rr = connect (nnetfd, (SA *)remend, sizeof (SA));
} else { /* setjmp: connect failed... */
rr = -1;
errno = ETIMEDOUT; /* fake it */
}
+#endif
arm_timer (0, 0);
if (rr == 0)
return (nnetfd);
@@ -824,7 +849,11 @@ int dolisten (rad, rp, lad, lp)
if (o_udpmode) {
x = sizeof (SA); /* retval for recvfrom */
arm_timer (2, o_wait); /* might as well timeout this, too */
+#ifdef POSIX_SETJMP
+ if (sigsetjmp (jbuf,1) == 0) { /* do timeout for initial connect */
+#else
if (setjmp (jbuf) == 0) { /* do timeout for initial connect */
+#endif
rr = recvfrom /* and here we block... */
(nnetfd, bigbuf_net, BIGSIZ, MSG_PEEK, (SA *) remend, &x);
Debug (("dolisten/recvfrom ding, rr = %d, netbuf %s ", rr, bigbuf_net))
@@ -849,10 +878,17 @@ Debug (("dolisten/recvfrom ding, rr = %d
/* fall here for TCP */
x = sizeof (SA); /* retval for accept */
arm_timer (2, o_wait); /* wrap this in a timer, too; 0 = forever */
+#ifdef POSIX_SETJMP
+ if (sigsetjmp (jbuf,1) == 0) {
+ rr = accept (nnetfd, (SA *)remend, &x);
+ } else
+ goto dol_tmo; /* timeout */
+#else
if (setjmp (jbuf) == 0) {
rr = accept (nnetfd, (SA *)remend, &x);
} else
goto dol_tmo; /* timeout */
+#endif
arm_timer (0, 0);
close (nnetfd); /* dump the old socket */
nnetfd = rr; /* here's our new one */
|