3 * $Id: socket.c,v 1.7 2001/06/22 19:37:00 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.7 2001/06/22 19:37:00 mdw
33 * New @conn_init@ interface.
35 * Revision 1.6 2001/02/03 20:30:03 mdw
36 * Support re-reading config files on SIGHUP.
38 * Revision 1.5 2000/03/23 23:20:42 mdw
39 * Remove listener even if connection option isn't SOCKOPT_LIMITED.
41 * Revision 1.4 1999/12/22 15:44:25 mdw
44 * Revision 1.3 1999/10/22 22:48:36 mdw
45 * New connection options: unlimited concurrent connections, and one-shot
48 * Revision 1.2 1999/07/27 18:30:53 mdw
49 * Various minor portability fixes.
51 * Revision 1.1 1999/07/26 23:33:32 mdw
52 * New sources and targets.
56 /*----- Header files ------------------------------------------------------*/
67 #include <sys/types.h>
70 #include <sys/socket.h>
71 #include <netinet/in.h>
72 #include <arpa/inet.h>
75 #include <mLib/alloc.h>
76 #include <mLib/conn.h>
77 #include <mLib/dstr.h>
78 #include <mLib/fdflags.h>
93 /*----- Data structures ---------------------------------------------------*/
95 /* --- Socket source options --- */
97 typedef struct ssource_opts {
102 static ssource_opts ssgo = { 256, 0 };
104 #define SOCKOPT_LIMIT 0u
105 #define SOCKOPT_NOLIMIT 1u
106 #define SOCKOPT_ONESHOT 2u
108 /* --- Socket source --- */
110 typedef struct ssource {
119 /* --- Socket target --- */
121 typedef struct starget {
127 /* --- Socket target endpoint --- */
129 typedef struct stept {
135 /* --- Socket source endpoint --- */
137 typedef struct ssept {
142 /*----- Protocol table ----------------------------------------------------*/
144 static addr_ops *addrs[] = { &inet_ops, &un_ops, 0 };
146 /*----- Other persistent variables ----------------------------------------*/
148 static addr_opts gao = { 0 };
150 /*----- Parsing address types ---------------------------------------------*/
152 /* --- @getaddrtype@ --- *
154 * Arguments: @scanner *sc@ = pointer to scanner (for error reporting)
155 * @const char *p@ = pointer to protocol name
156 * @int abbrev@ = nonzero to allow abbreviations
158 * Returns: Pointer to address operations table or null.
160 * Use: Looks up a protocol name. Handy when parsing addresses and
161 * other bits of configuration. Returns null if no matching
165 static addr_ops *getaddrtype(scanner *sc, const char *p, int abbrev)
168 addr_ops *chosen = 0;
169 size_t sz = strlen(p);
171 for (ops = addrs; *ops; ops++) {
172 if (strncmp((*ops)->name, p, sz) == 0) {
173 if ((*ops)->name[sz] == 0)
175 else if (chosen && abbrev)
176 error(sc, "ambiguous socket address type `%s'", p);
185 /* --- @getaddr@ --- *
187 * Arguments: @scanner *sc@ = pointer to scanner to read from
188 * @unsigned type@ = address type (@ADDR_SRC@ or @ADDR_DEST@)
190 * Returns: Pointer to an address successfully read.
192 * Use: Reads an optionally qualified address.
195 static addr *getaddr(scanner *sc, unsigned type)
204 if (sc->t == CTOK_WORD)
205 ops = getaddrtype(sc, sc->d.buf, abbrev);
209 error(sc, "unknown socket address type `%s'", sc->d.buf);
215 return (ops->read(sc, type));
218 /*----- Socket endpoints --------------------------------------------------*/
220 /* --- @wclose@ --- */
222 static void sept_wclose(endpt *e)
224 shutdown(e->out->fd, 1);
227 /* --- @close@ (source) --- */
229 static void ss_listen(ssource */*ss*/);
231 static void ssept_close(endpt *e)
233 ssept *ee = (ssept *)e;
235 if (ee->s->o.opt == SOCKOPT_LIMIT) {
237 if (ee->s->o.conn == 1)
241 REFFD_DEC(ee->e.out);
246 /* --- @close@ (target) --- */
248 static void stept_close(endpt *e)
250 stept *ee = (stept *)e;
252 if (ee->e.f & EPF_PENDING)
256 REFFD_DEC(ee->e.out);
264 /* --- @stept_go@ --- *
266 * Arguments: @int fd@ = file descriptor now ready for use
267 * @void *p@ = pointer to an endpoint structure
271 * Use: Handles successful connection of the target endpoint.
274 static void stept_go(int fd, void *p)
279 fw_log(-1, "[%s] connection failed: %s", e->desc, strerror(errno));
282 reffd *r = reffd_init(fd);
284 e->e.in = e->e.out = r;
285 e->e.f &= ~EPF_PENDING;
287 endpt_join(&e->e, e->e.other);
291 /* --- Socket endpoint definition --- */
293 static endpt_ops ssept_ops = {
294 0, 0, sept_wclose, ssept_close
297 static endpt_ops stept_ops = {
298 0, 0, sept_wclose, stept_close
301 /*----- Source definition -------------------------------------------------*/
303 /* --- @option@ --- */
305 static int ssource_option(source *s, scanner *sc)
307 ssource *ss = (ssource *)s;
308 ssource_opts *sso = ss ? &ss->o : &ssgo;
310 CONF_BEGIN(sc, "socket", "socket")
312 /* --- Make sure the next token is a word --- */
314 if (sc->t != CTOK_WORD)
315 error(sc, "parse error, option keyword expected");
317 /* --- Handle options at this level --- */
319 if (strcmp(sc->d.buf, "conn") == 0) {
323 if (sc->t != CTOK_WORD)
324 error(sc, "parse error, expected `unlimited', `one-shot' or number");
325 if (isdigit((unsigned char)sc->d.buf[0])) {
326 sso->conn = atoi(sc->d.buf);
328 error(sc, "argument of `conn' must be positive");
329 sso->opt = SOCKOPT_LIMIT;
333 sso->opt = 1 + (1 & conf_enum(sc,
334 "unlimited,one-shot,infinite",
335 ENUM_ABBREV, "`conn' option"));
340 if (strcmp(sc->d.buf, "logging") == 0 ||
341 strcmp(sc->d.buf, "log") == 0) {
342 addr_opts *ao = ss ? ss->ao : &gao;
346 if (conf_enum(sc, "no,yes", ENUM_ABBREV, "logging status"))
347 ao->f &= ~ADDRF_NOLOG;
349 ao->f |= ADDRF_NOLOG;
353 /* --- Pass the option around the various address types --- */
356 if (ss->a->ops->option && ss->a->ops->option(sc, ss ? ss->ao : 0))
360 for (a = addrs; *a; a++) {
361 if ((*a)->option && (*a)->option(sc, 0))
366 /* --- Nobody understood the option --- */
373 static source *ssource_read(scanner *sc)
377 (void)(conf_prefix(sc, "socket") || conf_prefix(sc, "sk"));
378 ss = CREATE(ssource);
379 ss->s.ops = &ssource_ops;
382 ss->a = getaddr(sc, ADDR_SRC);
383 if (ss->a->ops->initopts)
384 ss->ao = ss->a->ops->initopts();
386 ss->ao = CREATE(addr_opts);
392 /* --- @ss_accept@ --- *
394 * Arguments: @int fd@ = file descriptor to accept from
395 * @unsigned mode@ = what's ready with the descriptor
396 * @void *p@ = pointer to the source definition
400 * Use: Accepts an incoming connection and attaches it to a target
404 static void ssource_destroy(source */*s*/);
406 static void ss_accept(int fd, unsigned mode, void *p)
413 /* --- Make the file descriptor --- */
417 if ((r = ss->a->ops->accept(fd, ss->ao, ss->s.desc)) == 0)
419 setsockopt(r->fd, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(opt));
420 fdflags(r->fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
423 /* --- Make an endpoint --- */
426 e->e.ops = &ssept_ops;
430 e->e.in = e->e.out = r;
434 /* --- Obtain the target endpoint and let rip --- */
436 if ((ee = ss->t->ops->create(ss->t, ss->s.desc)) == 0) {
444 /* --- Remove the listening socket if necessary --- */
450 if (!(ss->ao->f & ADDRF_NOLOG))
451 fw_log(-1, "[%s] maximum connections reached", ss->s.desc);
454 if (ss->a->ops->unbind)
455 ss->a->ops->unbind(ss->a);
458 case SOCKOPT_NOLIMIT:
460 case SOCKOPT_ONESHOT:
463 if (ss->a->ops->unbind)
464 ss->a->ops->unbind(ss->a);
465 ssource_destroy(&ss->s);
469 /* --- Let everything else happen --- */
471 endpt_join(&e->e, ee);
474 /* --- @ss_listen@ --- *
476 * Arguments: @ssource *ss@ = source to listen on
480 * Use: Sets the socket to listen again, if it stopped for some
481 * reason. This is a copy of the code in the @read@ function,
482 * because it has different (wildly different) error handling
486 static void ss_listen(ssource *ss)
488 gen_addr *ga = (gen_addr *)ss->a;
491 if (!(ss->ao->f & ADDRF_NOLOG))
492 fw_log(-1, "[%s] reattaching listener", ss->s.desc);
494 /* --- Make the socket --- */
496 if ((fd = socket(ga->a.ops->pf, SOCK_STREAM, 0)) < 0) {
497 fw_log(-1, "[%s] couldn't create socket: %s",
498 ss->s.desc, strerror(errno));
502 /* --- Set it to allow address reuse --- */
506 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
507 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
510 /* --- Bind it to the right port --- */
512 if (bind(fd, &ga->sa, ga->a.sz)) {
513 fw_log(-1, "[%s] couldn't bind socket: %s", ss->s.desc, strerror(errno));
516 if (ga->a.ops->bound)
517 ga->a.ops->bound(&ga->a, ss->ao);
519 /* --- Set it to listen for connections --- */
522 fw_log(-1, "[%s] couldn't listen on socket: %s",
523 ss->s.desc, strerror(errno));
527 /* --- Set the listener up again --- */
533 /* --- Tidy up if it failed --- *
535 * I'll just remove the entire source.
542 ssource_destroy(&ss->s);
545 /* --- @attach@ --- */
547 static void ssource_attach(source *s, scanner *sc, target *t)
549 ssource *ss = (ssource *)s;
554 /* --- Initialize the description string --- */
558 dstr_puts(&d, "socket.");
559 ss->a->ops->print(ss->a, ADDR_SRC, &d);
560 dstr_puts(&d, " -> ");
561 dstr_puts(&d, ss->t->desc);
562 ss->s.desc = xstrdup(d.buf);
566 /* --- Initialize the socket for listening --- */
569 gen_addr *ga = (gen_addr *)ss->a;
571 /* --- Make the socket --- */
573 if ((fd = socket(ga->a.ops->pf, SOCK_STREAM, 0)) < 0)
574 error(sc, "couldn't create socket: %s", strerror(errno));
576 /* --- Set it to allow address reuse --- */
580 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
581 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
584 /* --- Bind it to the right port --- */
586 if (bind(fd, &ga->sa, ga->a.sz))
587 error(sc, "couldn't bind to %s: %s", ss->s.desc, strerror(errno));
588 if (ga->a.ops->bound)
589 ga->a.ops->bound(&ga->a, ss->ao);
591 /* --- Set it to listen for connections --- */
594 error(sc, "couldn't listen on socket: %s", strerror(errno));
597 /* --- We're ready to go now --- */
599 sel_initfile(sel, &ss->r, fd, SEL_READ, ss_accept, ss);
605 /* --- @destroy@ --- */
607 static void ssource_destroy(source *s)
609 ssource *ss = (ssource *)s;
611 if (ss->o.conn || ss->o.opt != SOCKOPT_LIMIT) {
614 if (ss->a->ops->unbind)
615 ss->a->ops->unbind(ss->a);
617 if (ss->a->ops->freeopts)
618 ss->a->ops->freeopts(ss->ao);
622 ss->a->ops->destroy(ss->a);
623 ss->t->ops->destroy(ss->t);
624 source_remove(&ss->s);
629 /* --- Source definition block --- */
631 source_ops ssource_ops = {
633 ssource_option, ssource_read, ssource_attach, ssource_destroy
636 /*----- Target definition -------------------------------------------------*/
640 static target *starget_read(scanner *sc)
645 (void)(conf_prefix(sc, "socket") || conf_prefix(sc, "sk"));
646 st = CREATE(starget);
647 st->t.ops = &starget_ops;
648 st->a = getaddr(sc, ADDR_DEST);
649 dstr_puts(&d, "socket.");
650 st->a->ops->print(st->a, ADDR_DEST, &d);
651 st->t.desc = xstrdup(d.buf);
656 /* --- @create@ --- *
658 * Arguments: @target *t@ = pointer to target
659 * @const char *desc@ = description of connection
661 * Returns: Pointer to a created endpoint.
663 * Use: Generates a target endpoint for communication.
666 static endpt *starget_create(target *t, const char *desc)
668 starget *st = (starget *)t;
669 stept *e = CREATE(stept);
671 gen_addr *ga = (gen_addr *)st->a;
674 if ((fd = socket(st->a->ops->pf, SOCK_STREAM, 0)) < 0)
676 setsockopt(fd, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(opt));
677 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
678 e->e.ops = &stept_ops;
680 e->e.f = EPF_FILE | EPF_PENDING;
682 e->desc = xstrdup(desc);
684 if (conn_init(&e->c, sel, fd, &ga->sa, ga->a.sz, stept_go, e)) {
685 fw_log(-1, "[%s] connection failed: %s", e->desc, strerror(errno));
693 /* --- @destroy@ --- */
695 static void starget_destroy(target *t)
697 starget *st = (starget *)t;
698 st->a->ops->destroy(st->a);
703 /* --- Socket target definition block --- */
705 target_ops starget_ops = {
707 0, starget_read, starget_create, starget_destroy
710 /*----- That's all, folks -------------------------------------------------*/