From: mdw Date: Fri, 22 Oct 1999 22:48:36 +0000 (+0000) Subject: New connection options: unlimited concurrent connections, and one-shot X-Git-Tag: 1.2.0^0 X-Git-Url: https://git.distorted.org.uk/~mdw/fwd/commitdiff_plain/0fe44b098ee6e9b906ab42ab66879db5917d7b6f New connection options: unlimited concurrent connections, and one-shot listening sockets. --- diff --git a/socket.c b/socket.c index 6df5c4d..d3a5134 100644 --- a/socket.c +++ b/socket.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: socket.c,v 1.2 1999/07/27 18:30:53 mdw Exp $ + * $Id: socket.c,v 1.3 1999/10/22 22:48:36 mdw Exp $ * * Socket source and target definitions * @@ -29,6 +29,10 @@ /*----- Revision history --------------------------------------------------* * * $Log: socket.c,v $ + * Revision 1.3 1999/10/22 22:48:36 mdw + * New connection options: unlimited concurrent connections, and one-shot + * listening sockets. + * * Revision 1.2 1999/07/27 18:30:53 mdw * Various minor portability fixes. * @@ -43,6 +47,7 @@ #include #include +#include #include #include #include @@ -78,10 +83,15 @@ /* --- Socket source options --- */ typedef struct ssource_opts { + unsigned opt; unsigned conn; } ssource_opts; -static ssource_opts ssgo = { 256 }; +static ssource_opts ssgo = { 256, 0 }; + +#define SOCKOPT_LIMIT 0u +#define SOCKOPT_NOLIMIT 1u +#define SOCKOPT_ONESHOT 2u /* --- Socket source --- */ @@ -213,11 +223,11 @@ static void ssept_close(endpt *e) { ssept *ee = (ssept *)e; - if (!ee->s->o.conn) { - ee->s->o.conn++; - ss_listen(ee->s); - } else + if (ee->s->o.opt == SOCKOPT_LIMIT) { ee->s->o.conn++; + if (ee->s->o.conn == 1) + ss_listen(ee->s); + } REFFD_DEC(ee->e.in); REFFD_DEC(ee->e.out); fw_dec(); @@ -293,11 +303,11 @@ static void stept_go(int fd, void *p) /* --- Socket endpoint definition --- */ static endpt_ops ssept_ops = { - 0, sept_wclose, ssept_close + 0, 0, sept_wclose, ssept_close }; static endpt_ops stept_ops = { - 0, sept_wclose, stept_close + 0, 0, sept_wclose, stept_close }; /*----- Source definition -------------------------------------------------*/ @@ -322,12 +332,20 @@ static int ssource_option(source *s, scanner *sc) token(sc); if (sc->t == '=') token(sc); - if (sc->t != CTOK_WORD || !isdigit((unsigned char)sc->d.buf[0])) - error(sc, "parse error, argument of `conn' must be a number"); - sso->conn = atoi(sc->d.buf); - if (sso->conn == 0) - error(sc, "argument of `conn' must be positive"); - token(sc); + if (sc->t != CTOK_WORD) + error(sc, "parse error, expected `unlimited', `one-shot' or number"); + if (isdigit((unsigned char)sc->d.buf[0])) { + sso->conn = atoi(sc->d.buf); + if (sso->conn == 0) + error(sc, "argument of `conn' must be positive"); + sso->opt = SOCKOPT_LIMIT; + token(sc); + } else { + sso->conn = 0; + sso->opt = 1 + (1 & conf_enum(sc, + "unlimited,one-shot,infinite", + ENUM_ABBREV, "`conn' option")); + } CONF_ACCEPT; } @@ -395,6 +413,8 @@ static source *ssource_read(scanner *sc) * endpoint. */ +static void ssource_destroy(source */*s*/); + static void ss_accept(int fd, unsigned mode, void *p) { ssource *ss = p; @@ -431,21 +451,35 @@ static void ss_accept(int fd, unsigned mode, void *p) DESTROY(e); return; } + fw_inc(); /* --- Remove the listening socket if necessary --- */ - ss->o.conn--; - if (!ss->o.conn) { - fw_log(-1, "[%s] maximum connections reached", ss->s.desc); - sel_rmfile(&ss->r); - close(ss->r.fd); - if (ss->a->ops->unbind) - ss->a->ops->unbind(ss->a); + switch (ss->o.opt) { + case SOCKOPT_LIMIT: + ss->o.conn--; + if (!ss->o.conn) { + if (!(ss->ao->f & ADDRF_NOLOG)) + fw_log(-1, "[%s] maximum connections reached", ss->s.desc); + sel_rmfile(&ss->r); + close(ss->r.fd); + if (ss->a->ops->unbind) + ss->a->ops->unbind(ss->a); + } + break; + case SOCKOPT_NOLIMIT: + break; + case SOCKOPT_ONESHOT: + sel_rmfile(&ss->r); + close(ss->r.fd); + if (ss->a->ops->unbind) + ss->a->ops->unbind(ss->a); + ssource_destroy(&ss->s); + break; } /* --- Let everything else happen --- */ - fw_inc(); endpt_join(&e->e, ee); } @@ -461,14 +495,13 @@ static void ss_accept(int fd, unsigned mode, void *p) * behaviour. */ -static void ssource_destroy(source */*s*/); - static void ss_listen(ssource *ss) { gen_addr *ga = (gen_addr *)ss->a; int fd; - fw_log(-1, "[%s] reattaching listener", ss->s.desc); + if (!(ss->ao->f & ADDRF_NOLOG)) + fw_log(-1, "[%s] reattaching listener", ss->s.desc); /* --- Make the socket --- */