3 * Socket source and target definitions
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the `fw' port forwarder.
12 * `fw' 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 * `fw' 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 `fw'; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 /*----- Header files ------------------------------------------------------*/
38 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
46 #include <mLib/alloc.h>
47 #include <mLib/conn.h>
48 #include <mLib/dstr.h>
49 #include <mLib/fdflags.h>
64 /*----- Data structures ---------------------------------------------------*/
66 /* --- Socket source options --- */
68 typedef struct ssource_opts {
74 static ssource_opts ssgo = { 256, 0, 5 };
76 #define SOCKOPT_LIMIT 0u
77 #define SOCKOPT_NOLIMIT 1u
78 #define SOCKOPT_ONESHOT 2u
80 /* --- Socket source --- */
82 typedef struct ssource {
91 /* --- Socket target --- */
93 typedef struct starget {
99 /* --- Socket target endpoint --- */
101 typedef struct stept {
107 /* --- Socket source endpoint --- */
109 typedef struct ssept {
114 /*----- Protocol table ----------------------------------------------------*/
116 static addr_ops *addrs[] = { &inet_ops, &un_ops, 0 };
118 /*----- Other persistent variables ----------------------------------------*/
120 static addr_opts gsao = { 0 }, gtao = { 0 };
122 /*----- Parsing address types ---------------------------------------------*/
124 /* --- @getaddrtype@ --- *
126 * Arguments: @scanner *sc@ = pointer to scanner (for error reporting)
127 * @const char *p@ = pointer to protocol name
128 * @int abbrev@ = nonzero to allow abbreviations
130 * Returns: Pointer to address operations table or null.
132 * Use: Looks up a protocol name. Handy when parsing addresses and
133 * other bits of configuration. Returns null if no matching
137 static addr_ops *getaddrtype(scanner *sc, const char *p, int abbrev)
140 addr_ops *chosen = 0;
141 size_t sz = strlen(p);
143 for (ops = addrs; *ops; ops++) {
144 if (strncmp((*ops)->name, p, sz) == 0) {
145 if ((*ops)->name[sz] == 0)
147 else if (chosen && abbrev)
148 error(sc, "ambiguous socket address type `%s'", p);
157 /* --- @getaddr@ --- *
159 * Arguments: @scanner *sc@ = pointer to scanner to read from
160 * @unsigned type@ = address type (@ADDR_SRC@ or @ADDR_DEST@)
162 * Returns: Pointer to an address successfully read.
164 * Use: Reads an optionally qualified address.
167 static addr *getaddr(scanner *sc, unsigned type)
176 if (sc->t == CTOK_WORD)
177 ops = getaddrtype(sc, sc->d.buf, abbrev);
181 error(sc, "unknown socket address type `%s'", sc->d.buf);
187 return (ops->read(sc, type));
190 /*----- Socket endpoints --------------------------------------------------*/
192 /* --- @wclose@ --- */
194 static void sept_wclose(endpt *e)
196 shutdown(e->out->fd, 1);
199 /* --- @close@ (source) --- */
201 static void ss_listen(ssource */*ss*/);
203 static void ssept_close(endpt *e)
205 ssept *ee = (ssept *)e;
207 if (ee->s->o.opt == SOCKOPT_LIMIT) {
209 if (ee->s->o.conn == 1)
213 REFFD_DEC(ee->e.out);
218 /* --- @close@ (target) --- */
220 static void stept_close(endpt *e)
222 stept *ee = (stept *)e;
224 if (ee->e.f & EPF_PENDING)
228 REFFD_DEC(ee->e.out);
236 /* --- @starget_connected@ --- *
238 * Arguments: @int fd@ = file descriptor now ready for use
239 * @void *p@ = pointer to an endpoint structure
243 * Use: Handles successful connection of the target endpoint.
246 void starget_connected(int fd, void *p)
251 fw_log(-1, "[%s] connection failed: %s", e->desc, strerror(errno));
254 reffd *r = reffd_init(fd);
257 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
258 setsockopt(fd, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(opt));
259 e->e.in = e->e.out = r;
260 e->e.f &= ~EPF_PENDING;
262 endpt_join(&e->e, e->e.other);
266 /* --- Socket endpoint definition --- */
268 static endpt_ops ssept_ops = {
269 0, 0, sept_wclose, ssept_close
272 static endpt_ops stept_ops = {
273 0, 0, sept_wclose, stept_close
276 /*----- Source definition -------------------------------------------------*/
278 /* --- @option@ --- */
280 static int ssource_option(source *s, scanner *sc)
282 ssource *ss = (ssource *)s;
283 ssource_opts *sso = ss ? &ss->o : &ssgo;
285 CONF_BEGIN(sc, "socket", "socket source")
287 /* --- Make sure the next token is a word --- */
289 if (sc->t != CTOK_WORD)
290 error(sc, "parse error, option keyword expected");
292 /* --- Handle options at this level --- */
294 if (strcmp(sc->d.buf, "conn") == 0) {
298 if (sc->t != CTOK_WORD)
299 error(sc, "parse error, expected `unlimited', `one-shot' or number");
300 if (isdigit((unsigned char)sc->d.buf[0])) {
301 sso->conn = atoi(sc->d.buf);
303 error(sc, "argument of `conn' must be positive");
304 sso->opt = SOCKOPT_LIMIT;
308 sso->opt = 1 + (1 & conf_enum(sc,
309 "unlimited,one-shot,infinite",
310 ENUM_ABBREV, "`conn' option"));
315 if (strcmp(sc->d.buf, "listen") == 0) {
319 if (sc->t != CTOK_WORD || !isdigit((unsigned char)sc->d.buf[0]))
320 error(sc, "parse error, expected number");
321 sso->listen = atoi(sc->d.buf);
322 if (sso->listen == 0)
323 error(sc, "argument of `listen' must be positive");
328 if (strcmp(sc->d.buf, "logging") == 0 ||
329 strcmp(sc->d.buf, "log") == 0) {
330 addr_opts *ao = ss ? ss->ao : &gsao;
334 if (conf_enum(sc, "no,yes", ENUM_ABBREV, "logging status"))
335 ao->f &= ~ADDRF_NOLOG;
337 ao->f |= ADDRF_NOLOG;
341 /* --- Pass the option around the various address types --- */
344 if (ss->a->ops->option && ss->a->ops->option(sc, ss->ao, ADDR_SRC))
348 for (a = addrs; *a; a++) {
349 if ((*a)->option && (*a)->option(sc, 0, ADDR_GLOBAL))
354 /* --- Nobody understood the option --- */
361 static source *ssource_read(scanner *sc)
365 (void)(conf_prefix(sc, "socket") || conf_prefix(sc, "sk"));
366 ss = CREATE(ssource);
367 ss->s.ops = &ssource_ops;
370 ss->a = getaddr(sc, ADDR_SRC);
371 if (ss->a->ops->initsrcopts)
372 ss->ao = ss->a->ops->initsrcopts();
374 ss->ao = CREATE(addr_opts);
381 /* --- @ss_accept@ --- *
383 * Arguments: @int fd@ = file descriptor to accept from
384 * @unsigned mode@ = what's ready with the descriptor
385 * @void *p@ = pointer to the source definition
389 * Use: Accepts an incoming connection and attaches it to a target
393 static void ssource_destroy(source */*s*/);
395 static void ss_accept(int fd, unsigned mode, void *p)
402 /* --- Make the file descriptor --- */
406 if ((r = ss->a->ops->accept(fd, ss->ao, ss->s.desc)) == 0)
408 setsockopt(r->fd, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(opt));
409 fdflags(r->fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
412 /* --- Make an endpoint --- */
415 e->e.ops = &ssept_ops;
419 e->e.in = e->e.out = r;
423 /* --- Obtain the target endpoint and let rip --- */
425 if ((ee = ss->t->ops->create(ss->t, ss->s.desc)) == 0) {
433 /* --- Remove the listening socket if necessary --- */
439 if (!(ss->ao->f & ADDRF_NOLOG))
440 fw_log(-1, "[%s] maximum connections reached", ss->s.desc);
443 if (ss->a->ops->unbind)
444 ss->a->ops->unbind(ss->a);
447 case SOCKOPT_NOLIMIT:
449 case SOCKOPT_ONESHOT:
452 if (ss->a->ops->unbind)
453 ss->a->ops->unbind(ss->a);
454 ssource_destroy(&ss->s);
458 /* --- Let everything else happen --- */
460 endpt_join(&e->e, ee);
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.
516 ssource_destroy(&ss->s);
519 /* --- @attach@ --- */
521 static void ssource_attach(source *s, scanner *sc, target *t)
523 ssource *ss = (ssource *)s;
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 /* --- @destroy@ --- */
570 static void ssource_destroy(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);
585 ss->a->ops->destroy(ss->a);
586 ss->t->ops->destroy(ss->t);
587 source_remove(&ss->s);
592 /* --- Source definition block --- */
594 source_ops ssource_ops = {
596 ssource_option, ssource_read, ssource_attach, ssource_destroy
599 /*----- Target definition -------------------------------------------------*/
601 /* --- @options@ --- */
603 static int starget_option(target *t, scanner *sc)
605 starget *st = (starget *)t;
607 CONF_BEGIN(sc, "starget", "socket target")
609 /* --- Pass the option around the various address types --- */
612 if (st->a->ops->option && st->a->ops->option(sc, st->ao, ADDR_DEST))
615 /* We'd have done it already if it was global */
624 static target *starget_read(scanner *sc)
629 (void)(conf_prefix(sc, "socket") || conf_prefix(sc, "sk"));
630 st = CREATE(starget);
631 st->t.ops = &starget_ops;
632 st->a = getaddr(sc, ADDR_DEST);
633 if (st->a->ops->inittargopts)
634 st->ao = st->a->ops->inittargopts();
636 st->ao = CREATE(addr_opts);
639 dstr_puts(&d, "socket.");
640 st->a->ops->print(st->a, ADDR_DEST, &d);
641 st->t.desc = xstrdup(d.buf);
646 /* --- @confirm@ --- */
648 static void starget_confirm(target *t)
650 starget *st = (starget *)t;
652 if (st->a->ops->confirm)
653 st->a->ops->confirm(st->a, ADDR_DEST, st->ao);
656 /* --- @create@ --- */
658 static endpt *starget_create(target *t, const char *desc)
660 starget *st = (starget *)t;
661 stept *e = CREATE(stept);
663 e->e.ops = &stept_ops;
665 e->e.f = EPF_FILE | EPF_PENDING;
667 e->desc = xstrdup(desc);
668 if (st->a->ops->connect(st->a, st->ao, &e->c, &e->e)) {
669 fw_log(-1, "[%s] couldn't connect: %s", e->desc, strerror(errno));
677 /* --- @destroy@ --- */
679 static void starget_destroy(target *t)
681 starget *st = (starget *)t;
682 if (st->a->ops->freetargopts)
683 st->a->ops->freetargopts(st->ao);
686 st->a->ops->destroy(st->a);
691 /* --- Socket target definition block --- */
693 target_ops starget_ops = {
695 starget_option, starget_read, starget_confirm,
696 starget_create, starget_destroy
699 /*----- That's all, folks -------------------------------------------------*/