3 * $Id: socket.c,v 1.11 2003/11/29 20:36:07 mdw Exp $
5 * Socket source and target definitions
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the `fw' port forwarder.
14 * `fw' is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * `fw' is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with `fw'; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.11 2003/11/29 20:36:07 mdw
33 * Privileged outgoing connections.
35 * Revision 1.10 2003/11/25 14:08:23 mdw
36 * Debianization. Socket target options. Internet binding.
38 * Revision 1.9 2002/02/23 00:08:00 mdw
39 * Fix stupid bugs from the listen(2) change.
41 * Revision 1.8 2002/02/22 23:44:44 mdw
42 * Call @xfree@ rather than @free@. Add option to change the listen(2)
45 * Revision 1.7 2001/06/22 19:37:00 mdw
46 * New @conn_init@ interface.
48 * Revision 1.6 2001/02/03 20:30:03 mdw
49 * Support re-reading config files on SIGHUP.
51 * Revision 1.5 2000/03/23 23:20:42 mdw
52 * Remove listener even if connection option isn't SOCKOPT_LIMITED.
54 * Revision 1.4 1999/12/22 15:44:25 mdw
57 * Revision 1.3 1999/10/22 22:48:36 mdw
58 * New connection options: unlimited concurrent connections, and one-shot
61 * Revision 1.2 1999/07/27 18:30:53 mdw
62 * Various minor portability fixes.
64 * Revision 1.1 1999/07/26 23:33:32 mdw
65 * New sources and targets.
69 /*----- Header files ------------------------------------------------------*/
80 #include <sys/types.h>
83 #include <sys/socket.h>
84 #include <netinet/in.h>
85 #include <arpa/inet.h>
88 #include <mLib/alloc.h>
89 #include <mLib/conn.h>
90 #include <mLib/dstr.h>
91 #include <mLib/fdflags.h>
106 /*----- Data structures ---------------------------------------------------*/
108 /* --- Socket source options --- */
110 typedef struct ssource_opts {
116 static ssource_opts ssgo = { 256, 0, 5 };
118 #define SOCKOPT_LIMIT 0u
119 #define SOCKOPT_NOLIMIT 1u
120 #define SOCKOPT_ONESHOT 2u
122 /* --- Socket source --- */
124 typedef struct ssource {
133 /* --- Socket target --- */
135 typedef struct starget {
141 /* --- Socket target endpoint --- */
143 typedef struct stept {
149 /* --- Socket source endpoint --- */
151 typedef struct ssept {
156 /*----- Protocol table ----------------------------------------------------*/
158 static addr_ops *addrs[] = { &inet_ops, &un_ops, 0 };
160 /*----- Other persistent variables ----------------------------------------*/
162 static addr_opts gsao = { 0 }, gtao = { 0 };
164 /*----- Parsing address types ---------------------------------------------*/
166 /* --- @getaddrtype@ --- *
168 * Arguments: @scanner *sc@ = pointer to scanner (for error reporting)
169 * @const char *p@ = pointer to protocol name
170 * @int abbrev@ = nonzero to allow abbreviations
172 * Returns: Pointer to address operations table or null.
174 * Use: Looks up a protocol name. Handy when parsing addresses and
175 * other bits of configuration. Returns null if no matching
179 static addr_ops *getaddrtype(scanner *sc, const char *p, int abbrev)
182 addr_ops *chosen = 0;
183 size_t sz = strlen(p);
185 for (ops = addrs; *ops; ops++) {
186 if (strncmp((*ops)->name, p, sz) == 0) {
187 if ((*ops)->name[sz] == 0)
189 else if (chosen && abbrev)
190 error(sc, "ambiguous socket address type `%s'", p);
199 /* --- @getaddr@ --- *
201 * Arguments: @scanner *sc@ = pointer to scanner to read from
202 * @unsigned type@ = address type (@ADDR_SRC@ or @ADDR_DEST@)
204 * Returns: Pointer to an address successfully read.
206 * Use: Reads an optionally qualified address.
209 static addr *getaddr(scanner *sc, unsigned type)
218 if (sc->t == CTOK_WORD)
219 ops = getaddrtype(sc, sc->d.buf, abbrev);
223 error(sc, "unknown socket address type `%s'", sc->d.buf);
229 return (ops->read(sc, type));
232 /*----- Socket endpoints --------------------------------------------------*/
234 /* --- @wclose@ --- */
236 static void sept_wclose(endpt *e)
238 shutdown(e->out->fd, 1);
241 /* --- @close@ (source) --- */
243 static void ss_listen(ssource */*ss*/);
245 static void ssept_close(endpt *e)
247 ssept *ee = (ssept *)e;
249 if (ee->s->o.opt == SOCKOPT_LIMIT) {
251 if (ee->s->o.conn == 1)
255 REFFD_DEC(ee->e.out);
260 /* --- @close@ (target) --- */
262 static void stept_close(endpt *e)
264 stept *ee = (stept *)e;
266 if (ee->e.f & EPF_PENDING)
270 REFFD_DEC(ee->e.out);
278 /* --- @starget_connected@ --- *
280 * Arguments: @int fd@ = file descriptor now ready for use
281 * @void *p@ = pointer to an endpoint structure
285 * Use: Handles successful connection of the target endpoint.
288 void starget_connected(int fd, void *p)
293 fw_log(-1, "[%s] connection failed: %s", e->desc, strerror(errno));
296 reffd *r = reffd_init(fd);
299 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
300 setsockopt(fd, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(opt));
301 e->e.in = e->e.out = r;
302 e->e.f &= ~EPF_PENDING;
304 endpt_join(&e->e, e->e.other);
308 /* --- Socket endpoint definition --- */
310 static endpt_ops ssept_ops = {
311 0, 0, sept_wclose, ssept_close
314 static endpt_ops stept_ops = {
315 0, 0, sept_wclose, stept_close
318 /*----- Source definition -------------------------------------------------*/
320 /* --- @option@ --- */
322 static int ssource_option(source *s, scanner *sc)
324 ssource *ss = (ssource *)s;
325 ssource_opts *sso = ss ? &ss->o : &ssgo;
327 CONF_BEGIN(sc, "socket", "socket source")
329 /* --- Make sure the next token is a word --- */
331 if (sc->t != CTOK_WORD)
332 error(sc, "parse error, option keyword expected");
334 /* --- Handle options at this level --- */
336 if (strcmp(sc->d.buf, "conn") == 0) {
340 if (sc->t != CTOK_WORD)
341 error(sc, "parse error, expected `unlimited', `one-shot' or number");
342 if (isdigit((unsigned char)sc->d.buf[0])) {
343 sso->conn = atoi(sc->d.buf);
345 error(sc, "argument of `conn' must be positive");
346 sso->opt = SOCKOPT_LIMIT;
350 sso->opt = 1 + (1 & conf_enum(sc,
351 "unlimited,one-shot,infinite",
352 ENUM_ABBREV, "`conn' option"));
357 if (strcmp(sc->d.buf, "listen") == 0) {
361 if (sc->t != CTOK_WORD || !isdigit((unsigned char)sc->d.buf[0]))
362 error(sc, "parse error, expected number");
363 sso->listen = atoi(sc->d.buf);
364 if (sso->listen == 0)
365 error(sc, "argument of `listen' must be positive");
370 if (strcmp(sc->d.buf, "logging") == 0 ||
371 strcmp(sc->d.buf, "log") == 0) {
372 addr_opts *ao = ss ? ss->ao : &gsao;
376 if (conf_enum(sc, "no,yes", ENUM_ABBREV, "logging status"))
377 ao->f &= ~ADDRF_NOLOG;
379 ao->f |= ADDRF_NOLOG;
383 /* --- Pass the option around the various address types --- */
386 if (ss->a->ops->option && ss->a->ops->option(sc, ss->ao, ADDR_SRC))
390 for (a = addrs; *a; a++) {
391 if ((*a)->option && (*a)->option(sc, 0, ADDR_GLOBAL))
396 /* --- Nobody understood the option --- */
403 static source *ssource_read(scanner *sc)
407 (void)(conf_prefix(sc, "socket") || conf_prefix(sc, "sk"));
408 ss = CREATE(ssource);
409 ss->s.ops = &ssource_ops;
412 ss->a = getaddr(sc, ADDR_SRC);
413 if (ss->a->ops->initsrcopts)
414 ss->ao = ss->a->ops->initsrcopts();
416 ss->ao = CREATE(addr_opts);
422 /* --- @ss_accept@ --- *
424 * Arguments: @int fd@ = file descriptor to accept from
425 * @unsigned mode@ = what's ready with the descriptor
426 * @void *p@ = pointer to the source definition
430 * Use: Accepts an incoming connection and attaches it to a target
434 static void ssource_destroy(source */*s*/);
436 static void ss_accept(int fd, unsigned mode, void *p)
443 /* --- Make the file descriptor --- */
447 if ((r = ss->a->ops->accept(fd, ss->ao, ss->s.desc)) == 0)
449 setsockopt(r->fd, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(opt));
450 fdflags(r->fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
453 /* --- Make an endpoint --- */
456 e->e.ops = &ssept_ops;
460 e->e.in = e->e.out = r;
464 /* --- Obtain the target endpoint and let rip --- */
466 if ((ee = ss->t->ops->create(ss->t, ss->s.desc)) == 0) {
474 /* --- Remove the listening socket if necessary --- */
480 if (!(ss->ao->f & ADDRF_NOLOG))
481 fw_log(-1, "[%s] maximum connections reached", ss->s.desc);
484 if (ss->a->ops->unbind)
485 ss->a->ops->unbind(ss->a);
488 case SOCKOPT_NOLIMIT:
490 case SOCKOPT_ONESHOT:
493 if (ss->a->ops->unbind)
494 ss->a->ops->unbind(ss->a);
495 ssource_destroy(&ss->s);
499 /* --- Let everything else happen --- */
501 endpt_join(&e->e, ee);
504 /* --- @ss_listen@ --- *
506 * Arguments: @ssource *ss@ = source to listen on
510 * Use: Sets the socket to listen again, if it stopped for some
511 * reason. This is a copy of the code in the @read@ function,
512 * because it has different (wildly different) error handling
516 static void ss_listen(ssource *ss)
521 if (!(ss->ao->f & ADDRF_NOLOG))
522 fw_log(-1, "[%s] reattaching listener", ss->s.desc);
524 /* --- Make the socket --- */
526 if ((fd = ss->a->ops->bind(ss->a, ss->ao)) < 0) {
527 fw_log(-1, "[%s] couldn't create socket: %s",
528 ss->s.desc, strerror(errno));
532 /* --- Set it to listen for connections --- */
534 setsockopt(fd, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(opt));
535 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
536 if (listen(fd, ss->o.listen)) {
537 fw_log(-1, "[%s] couldn't listen on socket: %s",
538 ss->s.desc, strerror(errno));
542 /* --- Set the listener up again --- */
548 /* --- Tidy up if it failed --- *
550 * I'll just remove the entire source.
557 ssource_destroy(&ss->s);
560 /* --- @attach@ --- */
562 static void ssource_attach(source *s, scanner *sc, target *t)
564 ssource *ss = (ssource *)s;
570 /* --- Initialize the description string --- */
574 dstr_puts(&d, "socket.");
575 ss->a->ops->print(ss->a, ADDR_SRC, &d);
576 dstr_puts(&d, " -> ");
577 dstr_puts(&d, ss->t->desc);
578 ss->s.desc = xstrdup(d.buf);
582 /* --- Confirm the address --- */
584 if (ss->a->ops->confirm)
585 ss->a->ops->confirm(ss->a, ADDR_SRC, ss->ao);
587 /* --- Initialize the socket for listening --- */
589 if ((fd = ss->a->ops->bind(ss->a, ss->ao)) < 0)
590 error(sc, "couldn't bind socket `%s': %s", ss->s.desc, strerror(errno));
592 /* --- Set it to listen for connections --- */
594 setsockopt(fd, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(opt));
595 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
596 if (listen(fd, ss->o.listen)) {
597 error(sc, "couldn't listen on socket `%s': %s",
598 ss->s.desc, strerror(errno));
601 /* --- We're ready to go now --- */
603 sel_initfile(sel, &ss->r, fd, SEL_READ, ss_accept, ss);
609 /* --- @destroy@ --- */
611 static void ssource_destroy(source *s)
613 ssource *ss = (ssource *)s;
615 if (ss->o.conn || ss->o.opt != SOCKOPT_LIMIT) {
618 if (ss->a->ops->unbind)
619 ss->a->ops->unbind(ss->a);
621 if (ss->a->ops->freesrcopts)
622 ss->a->ops->freesrcopts(ss->ao);
626 ss->a->ops->destroy(ss->a);
627 ss->t->ops->destroy(ss->t);
628 source_remove(&ss->s);
633 /* --- Source definition block --- */
635 source_ops ssource_ops = {
637 ssource_option, ssource_read, ssource_attach, ssource_destroy
640 /*----- Target definition -------------------------------------------------*/
642 /* --- @options@ --- */
644 static int starget_option(target *t, scanner *sc)
646 starget *st = (starget *)t;
648 CONF_BEGIN(sc, "starget", "socket target")
650 /* --- Pass the option around the various address types --- */
653 if (st->a->ops->option && st->a->ops->option(sc, st->ao, ADDR_DEST))
656 /* We'd have done it already if it was global */
665 static target *starget_read(scanner *sc)
670 (void)(conf_prefix(sc, "socket") || conf_prefix(sc, "sk"));
671 st = CREATE(starget);
672 st->t.ops = &starget_ops;
673 st->a = getaddr(sc, ADDR_DEST);
674 if (st->a->ops->inittargopts)
675 st->ao = st->a->ops->inittargopts();
677 st->ao = CREATE(addr_opts);
679 dstr_puts(&d, "socket.");
680 st->a->ops->print(st->a, ADDR_DEST, &d);
681 st->t.desc = xstrdup(d.buf);
686 /* --- @confirm@ --- */
688 static void starget_confirm(target *t)
690 starget *st = (starget *)t;
692 if (st->a->ops->confirm)
693 st->a->ops->confirm(st->a, ADDR_DEST, st->ao);
696 /* --- @create@ --- */
698 static endpt *starget_create(target *t, const char *desc)
700 starget *st = (starget *)t;
701 stept *e = CREATE(stept);
703 e->e.ops = &stept_ops;
705 e->e.f = EPF_FILE | EPF_PENDING;
707 e->desc = xstrdup(desc);
708 if (st->a->ops->connect(st->a, st->ao, &e->c, &e->e)) {
709 fw_log(-1, "[%s] couldn't connect: %s", e->desc, strerror(errno));
717 /* --- @destroy@ --- */
719 static void starget_destroy(target *t)
721 starget *st = (starget *)t;
722 if (st->a->ops->freetargopts)
723 st->a->ops->freetargopts(st->ao);
726 st->a->ops->destroy(st->a);
731 /* --- Socket target definition block --- */
733 target_ops starget_ops = {
735 starget_option, starget_read, starget_confirm,
736 starget_create, starget_destroy
739 /*----- That's all, folks -------------------------------------------------*/