3 * Socket source and target definitions
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the `fwd' port forwarder.
12 * `fwd' is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * `fwd' is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with `fwd'; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Data structures ---------------------------------------------------*/
31 /* --- Socket source options --- */
33 typedef struct ssource_opts {
40 static ssource_opts ssgo = { 256, 0, 5, 1 };
42 #define SOCKOPT_LIMIT 0u
43 #define SOCKOPT_NOLIMIT 1u
44 #define SOCKOPT_ONESHOT 2u
46 /* --- Socket source --- */
48 typedef struct ssource {
57 /* --- Socket target --- */
59 typedef struct starget {
65 /* --- Socket target endpoint --- */
67 typedef struct stept {
73 /* --- Socket source endpoint --- */
75 typedef struct ssept {
80 /*----- Protocol table ----------------------------------------------------*/
82 static addr_ops *addrs[] = { &inet_ops, &un_ops, 0 };
84 /*----- Other persistent variables ----------------------------------------*/
86 static addr_opts gsao = { 0 }, gtao = { 0 };
88 /*----- Parsing address types ---------------------------------------------*/
90 /* --- @getaddrtype@ --- *
92 * Arguments: @scanner *sc@ = pointer to scanner (for error reporting)
93 * @const char *p@ = pointer to protocol name
94 * @int abbrev@ = nonzero to allow abbreviations
96 * Returns: Pointer to address operations table or null.
98 * Use: Looks up a protocol name. Handy when parsing addresses and
99 * other bits of configuration. Returns null if no matching
103 static addr_ops *getaddrtype(scanner *sc, const char *p, int abbrev)
106 addr_ops *chosen = 0;
107 size_t sz = strlen(p);
109 for (ops = addrs; *ops; ops++) {
110 if (strncmp((*ops)->name, p, sz) == 0) {
111 if ((*ops)->name[sz] == 0)
113 else if (chosen && abbrev)
114 error(sc, "ambiguous socket address type `%s'", p);
123 /* --- @getaddr@ --- *
125 * Arguments: @scanner *sc@ = pointer to scanner to read from
126 * @unsigned type@ = address type (@ADDR_SRC@ or @ADDR_DEST@)
128 * Returns: Pointer to an address successfully read.
130 * Use: Reads an optionally qualified address.
133 static addr *getaddr(scanner *sc, unsigned type)
142 if (sc->t == CTOK_WORD)
143 ops = getaddrtype(sc, sc->d.buf, abbrev);
147 error(sc, "unknown socket address type `%s'", sc->d.buf);
153 return (ops->read(sc, type));
156 /*----- Socket endpoints --------------------------------------------------*/
158 /* --- @wclose@ --- */
160 static void sept_wclose(endpt *e)
162 shutdown(e->out->fd, 1);
165 /* --- @close@ (source) --- */
167 static void ss_listen(ssource */*ss*/);
169 static void ssept_close(endpt *e)
171 ssept *ee = (ssept *)e;
173 if ((ee->s->s.f&SF_ACTIVE) && ee->s->o.opt == SOCKOPT_LIMIT) {
175 if (ee->s->o.conn == 1)
179 REFFD_DEC(ee->e.out);
184 /* --- @close@ (target) --- */
186 static void stept_close(endpt *e)
188 stept *ee = (stept *)e;
190 if (ee->e.f & EPF_PENDING)
194 REFFD_DEC(ee->e.out);
202 /* --- @starget_connected@ --- *
204 * Arguments: @int fd@ = file descriptor now ready for use
205 * @void *p@ = pointer to an endpoint structure
209 * Use: Handles successful connection of the target endpoint.
212 void starget_connected(int fd, void *p)
217 fw_log(-1, "[%s] connection failed: %s", e->desc, strerror(errno));
220 reffd *r = reffd_init(fd);
223 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
224 setsockopt(fd, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(opt));
225 e->e.in = e->e.out = r;
226 e->e.f &= ~EPF_PENDING;
228 endpt_join(&e->e, e->e.other, 0);
232 /* --- Socket endpoint definition --- */
234 static endpt_ops ssept_ops = {
235 0, 0, sept_wclose, ssept_close
238 static endpt_ops stept_ops = {
239 0, 0, sept_wclose, stept_close
242 /*----- Source definition -------------------------------------------------*/
244 /* --- @option@ --- */
246 static int ssource_option(source *s, scanner *sc)
248 ssource *ss = (ssource *)s;
249 ssource_opts *sso = ss ? &ss->o : &ssgo;
251 CONF_BEGIN(sc, "socket", "socket source")
253 /* --- Make sure the next token is a word --- */
255 if (sc->t != CTOK_WORD)
256 error(sc, "parse error, option keyword expected");
258 /* --- Handle options at this level --- */
260 if (strcmp(sc->d.buf, "conn") == 0) {
264 if (sc->t != CTOK_WORD)
265 error(sc, "parse error, expected `unlimited', `one-shot' or number");
266 if (isdigit((unsigned char)sc->d.buf[0])) {
267 sso->conn = atoi(sc->d.buf);
269 error(sc, "argument of `conn' must be positive");
270 sso->opt = SOCKOPT_LIMIT;
274 sso->opt = 1 + (1 & conf_enum(sc,
275 "unlimited,one-shot,infinite",
276 ENUM_ABBREV, "`conn' option"));
281 if (strcmp(sc->d.buf, "listen") == 0) {
285 if (sc->t != CTOK_WORD || !isdigit((unsigned char)sc->d.buf[0]))
286 error(sc, "parse error, expected number");
287 sso->listen = atoi(sc->d.buf);
288 if (sso->listen == 0)
289 error(sc, "argument of `listen' must be positive");
294 if (strcmp(sc->d.buf, "accept") == 0 ||
295 strcmp(sc->d.buf, "accept-count") == 0) {
299 if (sc->t != CTOK_WORD)
300 error(sc, "parse error, expected `unlimited' or number");
301 else if (isdigit((unsigned char)sc->d.buf[0])) {
302 sso->naccept = atoi(sc->d.buf);
303 if (sso->naccept == 0)
304 error(sc, "argument of `accept-count' must be positive");
307 conf_enum(sc, "unlimited,infinite",
308 ENUM_ABBREV, "`accept-count' option");
314 if (strcmp(sc->d.buf, "logging") == 0 ||
315 strcmp(sc->d.buf, "log") == 0) {
316 addr_opts *ao = ss ? ss->ao : &gsao;
320 if (conf_enum(sc, "no,yes", ENUM_ABBREV, "logging status"))
321 ao->f &= ~ADDRF_NOLOG;
323 ao->f |= ADDRF_NOLOG;
327 /* --- Pass the option around the various address types --- */
330 if (ss->a->ops->option && ss->a->ops->option(sc, ss->ao, ADDR_SRC))
334 for (a = addrs; *a; a++) {
335 if ((*a)->option && (*a)->option(sc, 0, ADDR_GLOBAL))
340 /* --- Nobody understood the option --- */
347 static source *ssource_read(scanner *sc)
351 (void)(conf_prefix(sc, "socket") || conf_prefix(sc, "sk"));
352 ss = CREATE(ssource);
353 ss->s.ops = &ssource_ops;
358 ss->a = getaddr(sc, ADDR_SRC);
359 if (ss->a->ops->initsrcopts)
360 ss->ao = ss->a->ops->initsrcopts();
362 ss->ao = CREATE(addr_opts);
368 /* --- @ss_accept@ --- *
370 * Arguments: @int fd@ = file descriptor to accept from
371 * @unsigned mode@ = what's ready with the descriptor
372 * @void *p@ = pointer to the source definition
376 * Use: Accepts an incoming connection and attaches it to a target
380 static void ssource_destroy(source */*s*/);
382 static void ss_accept(int fd, unsigned mode, void *p)
393 /* --- Make the file descriptor --- */
397 if ((r = ss->a->ops->accept(fd, ss->ao, ss->s.desc)) == 0)
399 setsockopt(r->fd, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(opt));
400 fdflags(r->fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
403 /* --- Make an endpoint --- */
406 e->e.ops = &ssept_ops;
410 e->e.in = e->e.out = r;
414 /* --- Obtain the target endpoint and let rip --- */
416 if ((ee = ss->t->ops->create(ss->t, ss->s.desc)) == 0) {
424 /* --- Note that we've done one --- */
427 if (i >= ss->o.naccept)
430 /* --- Remove the listening socket if necessary --- */
436 if (!(ss->ao->f & ADDRF_NOLOG))
437 fw_log(-1, "[%s] maximum connections reached", ss->s.desc);
440 if (ss->a->ops->unbind)
441 ss->a->ops->unbind(ss->a);
445 case SOCKOPT_NOLIMIT:
447 case SOCKOPT_ONESHOT:
450 if (ss->a->ops->unbind)
451 ss->a->ops->unbind(ss->a);
457 /* --- Let everything else happen --- */
459 endpt_join(&e->e, ee, ss->s.desc);
463 /* --- @ss_listen@ --- *
465 * Arguments: @ssource *ss@ = source to listen on
469 * Use: Sets the socket to listen again, if it stopped for some
470 * reason. This is a copy of the code in the @read@ function,
471 * because it has different (wildly different) error handling
475 static void ss_listen(ssource *ss)
480 if (!(ss->ao->f & ADDRF_NOLOG))
481 fw_log(-1, "[%s] reattaching listener", ss->s.desc);
483 /* --- Make the socket --- */
485 if ((fd = ss->a->ops->bind(ss->a, ss->ao)) < 0) {
486 fw_log(-1, "[%s] couldn't create socket: %s",
487 ss->s.desc, strerror(errno));
491 /* --- Set it to listen for connections --- */
493 setsockopt(fd, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(opt));
494 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
495 if (listen(fd, ss->o.listen)) {
496 fw_log(-1, "[%s] couldn't listen on socket: %s",
497 ss->s.desc, strerror(errno));
501 /* --- Set the listener up again --- */
507 /* --- Tidy up if it failed --- *
509 * I'll just remove the entire source.
519 /* --- @attach@ --- */
521 static void ssource_attach(source *s, scanner *sc, target *t)
523 ssource *ss = (ssource *)s;
527 ss->t = t; target_inc(t);
529 /* --- Initialize the description string --- */
533 dstr_puts(&d, "socket.");
534 ss->a->ops->print(ss->a, ADDR_SRC, &d);
535 dstr_puts(&d, " -> ");
536 dstr_puts(&d, ss->t->desc);
537 ss->s.desc = xstrdup(d.buf);
541 /* --- Confirm the address --- */
543 if (ss->a->ops->confirm)
544 ss->a->ops->confirm(ss->a, ADDR_SRC, ss->ao);
546 /* --- Initialize the socket for listening --- */
548 if ((fd = ss->a->ops->bind(ss->a, ss->ao)) < 0)
549 error(sc, "couldn't bind socket `%s': %s", ss->s.desc, strerror(errno));
551 /* --- Set it to listen for connections --- */
553 setsockopt(fd, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(opt));
554 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
555 if (listen(fd, ss->o.listen)) {
556 error(sc, "couldn't listen on socket `%s': %s",
557 ss->s.desc, strerror(errno));
560 /* --- We're ready to go now --- */
562 sel_initfile(sel, &ss->r, fd, SEL_READ, ss_accept, ss);
568 /* --- @shutdown@ --- */
570 static void ssource_shutdown(source *s)
572 ssource *ss = (ssource *)s;
574 if (ss->o.conn || ss->o.opt != SOCKOPT_LIMIT) {
577 if (ss->a->ops->unbind)
578 ss->a->ops->unbind(ss->a);
580 if (ss->a->ops->freesrcopts)
581 ss->a->ops->freesrcopts(ss->ao);
584 ss->a->ops->destroy(ss->a);
585 source_remove(&ss->s);
590 /* --- @destroy@ --- */
592 static void ssource_destroy(source *s)
594 ssource *ss = (ssource *)s;
601 /* --- Source definition block --- */
603 source_ops ssource_ops = {
605 ssource_option, ssource_read, ssource_attach, ssource_shutdown, ssource_destroy
608 /*----- Target definition -------------------------------------------------*/
610 /* --- @options@ --- */
612 static int starget_option(target *t, scanner *sc)
614 starget *st = (starget *)t;
616 CONF_BEGIN(sc, "starget", "socket target")
618 /* --- Pass the option around the various address types --- */
621 if (st->a->ops->option && st->a->ops->option(sc, st->ao, ADDR_DEST))
624 /* We'd have done it already if it was global */
633 static target *starget_read(scanner *sc)
638 (void)(conf_prefix(sc, "socket") || conf_prefix(sc, "sk"));
639 st = CREATE(starget);
640 st->t.ops = &starget_ops;
642 st->a = getaddr(sc, ADDR_DEST);
643 if (st->a->ops->inittargopts)
644 st->ao = st->a->ops->inittargopts();
646 st->ao = CREATE(addr_opts);
649 dstr_puts(&d, "socket.");
650 st->a->ops->print(st->a, ADDR_DEST, &d);
651 st->t.desc = xstrdup(d.buf);
656 /* --- @confirm@ --- */
658 static void starget_confirm(target *t)
660 starget *st = (starget *)t;
662 if (st->a->ops->confirm)
663 st->a->ops->confirm(st->a, ADDR_DEST, st->ao);
666 /* --- @create@ --- */
668 static endpt *starget_create(target *t, const char *desc)
670 starget *st = (starget *)t;
671 stept *e = CREATE(stept);
673 e->e.ops = &stept_ops;
675 e->e.f = EPF_FILE | EPF_PENDING;
677 e->desc = xstrdup(desc);
678 if (st->a->ops->connect(st->a, st->ao, &e->c, &e->e)) {
679 fw_log(-1, "[%s] couldn't connect: %s", e->desc, strerror(errno));
687 /* --- @destroy@ --- */
689 static void starget_destroy(target *t)
691 starget *st = (starget *)t;
692 if (st->a->ops->freetargopts)
693 st->a->ops->freetargopts(st->ao);
696 st->a->ops->destroy(st->a);
701 /* --- Socket target definition block --- */
703 target_ops starget_ops = {
705 starget_option, starget_read, starget_confirm,
706 starget_create, starget_destroy
709 /*----- That's all, folks -------------------------------------------------*/