3 * $Id: socket.c,v 1.12 2004/04/08 01:36:25 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 /*----- Header files ------------------------------------------------------*/
40 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <arpa/inet.h>
48 #include <mLib/alloc.h>
49 #include <mLib/conn.h>
50 #include <mLib/dstr.h>
51 #include <mLib/fdflags.h>
66 /*----- Data structures ---------------------------------------------------*/
68 /* --- Socket source options --- */
70 typedef struct ssource_opts {
76 static ssource_opts ssgo = { 256, 0, 5 };
78 #define SOCKOPT_LIMIT 0u
79 #define SOCKOPT_NOLIMIT 1u
80 #define SOCKOPT_ONESHOT 2u
82 /* --- Socket source --- */
84 typedef struct ssource {
93 /* --- Socket target --- */
95 typedef struct starget {
101 /* --- Socket target endpoint --- */
103 typedef struct stept {
109 /* --- Socket source endpoint --- */
111 typedef struct ssept {
116 /*----- Protocol table ----------------------------------------------------*/
118 static addr_ops *addrs[] = { &inet_ops, &un_ops, 0 };
120 /*----- Other persistent variables ----------------------------------------*/
122 static addr_opts gsao = { 0 }, gtao = { 0 };
124 /*----- Parsing address types ---------------------------------------------*/
126 /* --- @getaddrtype@ --- *
128 * Arguments: @scanner *sc@ = pointer to scanner (for error reporting)
129 * @const char *p@ = pointer to protocol name
130 * @int abbrev@ = nonzero to allow abbreviations
132 * Returns: Pointer to address operations table or null.
134 * Use: Looks up a protocol name. Handy when parsing addresses and
135 * other bits of configuration. Returns null if no matching
139 static addr_ops *getaddrtype(scanner *sc, const char *p, int abbrev)
142 addr_ops *chosen = 0;
143 size_t sz = strlen(p);
145 for (ops = addrs; *ops; ops++) {
146 if (strncmp((*ops)->name, p, sz) == 0) {
147 if ((*ops)->name[sz] == 0)
149 else if (chosen && abbrev)
150 error(sc, "ambiguous socket address type `%s'", p);
159 /* --- @getaddr@ --- *
161 * Arguments: @scanner *sc@ = pointer to scanner to read from
162 * @unsigned type@ = address type (@ADDR_SRC@ or @ADDR_DEST@)
164 * Returns: Pointer to an address successfully read.
166 * Use: Reads an optionally qualified address.
169 static addr *getaddr(scanner *sc, unsigned type)
178 if (sc->t == CTOK_WORD)
179 ops = getaddrtype(sc, sc->d.buf, abbrev);
183 error(sc, "unknown socket address type `%s'", sc->d.buf);
189 return (ops->read(sc, type));
192 /*----- Socket endpoints --------------------------------------------------*/
194 /* --- @wclose@ --- */
196 static void sept_wclose(endpt *e)
198 shutdown(e->out->fd, 1);
201 /* --- @close@ (source) --- */
203 static void ss_listen(ssource */*ss*/);
205 static void ssept_close(endpt *e)
207 ssept *ee = (ssept *)e;
209 if (ee->s->o.opt == SOCKOPT_LIMIT) {
211 if (ee->s->o.conn == 1)
215 REFFD_DEC(ee->e.out);
220 /* --- @close@ (target) --- */
222 static void stept_close(endpt *e)
224 stept *ee = (stept *)e;
226 if (ee->e.f & EPF_PENDING)
230 REFFD_DEC(ee->e.out);
238 /* --- @starget_connected@ --- *
240 * Arguments: @int fd@ = file descriptor now ready for use
241 * @void *p@ = pointer to an endpoint structure
245 * Use: Handles successful connection of the target endpoint.
248 void starget_connected(int fd, void *p)
253 fw_log(-1, "[%s] connection failed: %s", e->desc, strerror(errno));
256 reffd *r = reffd_init(fd);
259 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
260 setsockopt(fd, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(opt));
261 e->e.in = e->e.out = r;
262 e->e.f &= ~EPF_PENDING;
264 endpt_join(&e->e, e->e.other);
268 /* --- Socket endpoint definition --- */
270 static endpt_ops ssept_ops = {
271 0, 0, sept_wclose, ssept_close
274 static endpt_ops stept_ops = {
275 0, 0, sept_wclose, stept_close
278 /*----- Source definition -------------------------------------------------*/
280 /* --- @option@ --- */
282 static int ssource_option(source *s, scanner *sc)
284 ssource *ss = (ssource *)s;
285 ssource_opts *sso = ss ? &ss->o : &ssgo;
287 CONF_BEGIN(sc, "socket", "socket source")
289 /* --- Make sure the next token is a word --- */
291 if (sc->t != CTOK_WORD)
292 error(sc, "parse error, option keyword expected");
294 /* --- Handle options at this level --- */
296 if (strcmp(sc->d.buf, "conn") == 0) {
300 if (sc->t != CTOK_WORD)
301 error(sc, "parse error, expected `unlimited', `one-shot' or number");
302 if (isdigit((unsigned char)sc->d.buf[0])) {
303 sso->conn = atoi(sc->d.buf);
305 error(sc, "argument of `conn' must be positive");
306 sso->opt = SOCKOPT_LIMIT;
310 sso->opt = 1 + (1 & conf_enum(sc,
311 "unlimited,one-shot,infinite",
312 ENUM_ABBREV, "`conn' option"));
317 if (strcmp(sc->d.buf, "listen") == 0) {
321 if (sc->t != CTOK_WORD || !isdigit((unsigned char)sc->d.buf[0]))
322 error(sc, "parse error, expected number");
323 sso->listen = atoi(sc->d.buf);
324 if (sso->listen == 0)
325 error(sc, "argument of `listen' must be positive");
330 if (strcmp(sc->d.buf, "logging") == 0 ||
331 strcmp(sc->d.buf, "log") == 0) {
332 addr_opts *ao = ss ? ss->ao : &gsao;
336 if (conf_enum(sc, "no,yes", ENUM_ABBREV, "logging status"))
337 ao->f &= ~ADDRF_NOLOG;
339 ao->f |= ADDRF_NOLOG;
343 /* --- Pass the option around the various address types --- */
346 if (ss->a->ops->option && ss->a->ops->option(sc, ss->ao, ADDR_SRC))
350 for (a = addrs; *a; a++) {
351 if ((*a)->option && (*a)->option(sc, 0, ADDR_GLOBAL))
356 /* --- Nobody understood the option --- */
363 static source *ssource_read(scanner *sc)
367 (void)(conf_prefix(sc, "socket") || conf_prefix(sc, "sk"));
368 ss = CREATE(ssource);
369 ss->s.ops = &ssource_ops;
372 ss->a = getaddr(sc, ADDR_SRC);
373 if (ss->a->ops->initsrcopts)
374 ss->ao = ss->a->ops->initsrcopts();
376 ss->ao = CREATE(addr_opts);
383 /* --- @ss_accept@ --- *
385 * Arguments: @int fd@ = file descriptor to accept from
386 * @unsigned mode@ = what's ready with the descriptor
387 * @void *p@ = pointer to the source definition
391 * Use: Accepts an incoming connection and attaches it to a target
395 static void ssource_destroy(source */*s*/);
397 static void ss_accept(int fd, unsigned mode, void *p)
404 /* --- Make the file descriptor --- */
408 if ((r = ss->a->ops->accept(fd, ss->ao, ss->s.desc)) == 0)
410 setsockopt(r->fd, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(opt));
411 fdflags(r->fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
414 /* --- Make an endpoint --- */
417 e->e.ops = &ssept_ops;
421 e->e.in = e->e.out = r;
425 /* --- Obtain the target endpoint and let rip --- */
427 if ((ee = ss->t->ops->create(ss->t, ss->s.desc)) == 0) {
435 /* --- Remove the listening socket if necessary --- */
441 if (!(ss->ao->f & ADDRF_NOLOG))
442 fw_log(-1, "[%s] maximum connections reached", ss->s.desc);
445 if (ss->a->ops->unbind)
446 ss->a->ops->unbind(ss->a);
449 case SOCKOPT_NOLIMIT:
451 case SOCKOPT_ONESHOT:
454 if (ss->a->ops->unbind)
455 ss->a->ops->unbind(ss->a);
456 ssource_destroy(&ss->s);
460 /* --- Let everything else happen --- */
462 endpt_join(&e->e, ee);
465 /* --- @ss_listen@ --- *
467 * Arguments: @ssource *ss@ = source to listen on
471 * Use: Sets the socket to listen again, if it stopped for some
472 * reason. This is a copy of the code in the @read@ function,
473 * because it has different (wildly different) error handling
477 static void ss_listen(ssource *ss)
482 if (!(ss->ao->f & ADDRF_NOLOG))
483 fw_log(-1, "[%s] reattaching listener", ss->s.desc);
485 /* --- Make the socket --- */
487 if ((fd = ss->a->ops->bind(ss->a, ss->ao)) < 0) {
488 fw_log(-1, "[%s] couldn't create socket: %s",
489 ss->s.desc, strerror(errno));
493 /* --- Set it to listen for connections --- */
495 setsockopt(fd, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(opt));
496 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
497 if (listen(fd, ss->o.listen)) {
498 fw_log(-1, "[%s] couldn't listen on socket: %s",
499 ss->s.desc, strerror(errno));
503 /* --- Set the listener up again --- */
509 /* --- Tidy up if it failed --- *
511 * I'll just remove the entire source.
518 ssource_destroy(&ss->s);
521 /* --- @attach@ --- */
523 static void ssource_attach(source *s, scanner *sc, target *t)
525 ssource *ss = (ssource *)s;
531 /* --- Initialize the description string --- */
535 dstr_puts(&d, "socket.");
536 ss->a->ops->print(ss->a, ADDR_SRC, &d);
537 dstr_puts(&d, " -> ");
538 dstr_puts(&d, ss->t->desc);
539 ss->s.desc = xstrdup(d.buf);
543 /* --- Confirm the address --- */
545 if (ss->a->ops->confirm)
546 ss->a->ops->confirm(ss->a, ADDR_SRC, ss->ao);
548 /* --- Initialize the socket for listening --- */
550 if ((fd = ss->a->ops->bind(ss->a, ss->ao)) < 0)
551 error(sc, "couldn't bind socket `%s': %s", ss->s.desc, strerror(errno));
553 /* --- Set it to listen for connections --- */
555 setsockopt(fd, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(opt));
556 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
557 if (listen(fd, ss->o.listen)) {
558 error(sc, "couldn't listen on socket `%s': %s",
559 ss->s.desc, strerror(errno));
562 /* --- We're ready to go now --- */
564 sel_initfile(sel, &ss->r, fd, SEL_READ, ss_accept, ss);
570 /* --- @destroy@ --- */
572 static void ssource_destroy(source *s)
574 ssource *ss = (ssource *)s;
576 if (ss->o.conn || ss->o.opt != SOCKOPT_LIMIT) {
579 if (ss->a->ops->unbind)
580 ss->a->ops->unbind(ss->a);
582 if (ss->a->ops->freesrcopts)
583 ss->a->ops->freesrcopts(ss->ao);
587 ss->a->ops->destroy(ss->a);
588 ss->t->ops->destroy(ss->t);
589 source_remove(&ss->s);
594 /* --- Source definition block --- */
596 source_ops ssource_ops = {
598 ssource_option, ssource_read, ssource_attach, ssource_destroy
601 /*----- Target definition -------------------------------------------------*/
603 /* --- @options@ --- */
605 static int starget_option(target *t, scanner *sc)
607 starget *st = (starget *)t;
609 CONF_BEGIN(sc, "starget", "socket target")
611 /* --- Pass the option around the various address types --- */
614 if (st->a->ops->option && st->a->ops->option(sc, st->ao, ADDR_DEST))
617 /* We'd have done it already if it was global */
626 static target *starget_read(scanner *sc)
631 (void)(conf_prefix(sc, "socket") || conf_prefix(sc, "sk"));
632 st = CREATE(starget);
633 st->t.ops = &starget_ops;
634 st->a = getaddr(sc, ADDR_DEST);
635 if (st->a->ops->inittargopts)
636 st->ao = st->a->ops->inittargopts();
638 st->ao = CREATE(addr_opts);
641 dstr_puts(&d, "socket.");
642 st->a->ops->print(st->a, ADDR_DEST, &d);
643 st->t.desc = xstrdup(d.buf);
648 /* --- @confirm@ --- */
650 static void starget_confirm(target *t)
652 starget *st = (starget *)t;
654 if (st->a->ops->confirm)
655 st->a->ops->confirm(st->a, ADDR_DEST, st->ao);
658 /* --- @create@ --- */
660 static endpt *starget_create(target *t, const char *desc)
662 starget *st = (starget *)t;
663 stept *e = CREATE(stept);
665 e->e.ops = &stept_ops;
667 e->e.f = EPF_FILE | EPF_PENDING;
669 e->desc = xstrdup(desc);
670 if (st->a->ops->connect(st->a, st->ao, &e->c, &e->e)) {
671 fw_log(-1, "[%s] couldn't connect: %s", e->desc, strerror(errno));
679 /* --- @destroy@ --- */
681 static void starget_destroy(target *t)
683 starget *st = (starget *)t;
684 if (st->a->ops->freetargopts)
685 st->a->ops->freetargopts(st->ao);
688 st->a->ops->destroy(st->a);
693 /* --- Socket target definition block --- */
695 target_ops starget_ops = {
697 starget_option, starget_read, starget_confirm,
698 starget_create, starget_destroy
701 /*----- That's all, folks -------------------------------------------------*/