| Another hack for SO_LINGER in Linux. - geomyidae - A small C-based gopherd. | |
| git clone git://bitreich.org/geomyidae/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfri… | |
| Log | |
| Files | |
| Refs | |
| Tags | |
| README | |
| LICENSE | |
| --- | |
| commit 1908f14b2d4df2a0530fc4c7ca308220c89871c0 | |
| parent 2c95cfddd2b4d2aca43bdcad09cdadd3214a10c9 | |
| Author: Christoph Lohmann <[email protected]> | |
| Date: Sun, 18 Feb 2018 09:33:39 +0100 | |
| Another hack for SO_LINGER in Linux. | |
| In case some connection stalled, wait 30 seconds until it is closed. | |
| DDoS is still possible, but that is not geomyidae's fault. | |
| Diffstat: | |
| M ind.c | 40 ++++++++++++++++++++++++++++-… | |
| 1 file changed, 36 insertions(+), 4 deletions(-) | |
| --- | |
| diff --git a/ind.c b/ind.c | |
| @@ -57,12 +57,22 @@ filetype type[] = { | |
| int | |
| pendingbytes(int sock) | |
| { | |
| - int pending; | |
| + int pending, rval; | |
| pending = 0; | |
| + rval = 0; | |
| #ifdef TIOCOUTQ | |
| - ioctl(sock, TIOCOUTQ, &pending); | |
| + rval = ioctl(sock, TIOCOUTQ, &pending); | |
| +#else | |
| +#ifdef SIOCOUTQ | |
| + rval = ioctl(sock, SIOCOUTQ, &pending); | |
| #endif | |
| +#endif | |
| + | |
| + if (rval != 0) { | |
| + printf("rval = %d\n", rval); | |
| + return 0; | |
| + } | |
| return pending; | |
| } | |
| @@ -70,8 +80,30 @@ pendingbytes(int sock) | |
| void | |
| waitforpendingbytes(int sock) | |
| { | |
| - while (pendingbytes(sock) > 0) | |
| - usleep(10); | |
| + int npending, opending, tries; | |
| + | |
| + npending = opending = tries = 0; | |
| + | |
| + /* | |
| + * Wait until there is nothing pending or the connection stalled for | |
| + * 30 seconds. | |
| + */ | |
| + while ((npending = pendingbytes(sock)) > 0 && tries < 30000000) { | |
| + if (opending != 0) { | |
| + if (opending != npending) { | |
| + tries = 0; | |
| + } else { | |
| + if (tries == 0) { | |
| + tries = 1; | |
| + } else { | |
| + tries += tries; | |
| + } | |
| + } | |
| + } | |
| + opending = npending; | |
| + | |
| + usleep(tries); | |
| + } | |
| } | |
| int |