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.
29 /*----- Data structures ---------------------------------------------------*/
31 /* --- Socket source options --- */
33 typedef struct ssource_opts
{
39 static ssource_opts ssgo
= { 256, 0, 5 };
41 #define SOCKOPT_LIMIT 0u
42 #define SOCKOPT_NOLIMIT 1u
43 #define SOCKOPT_ONESHOT 2u
45 /* --- Socket source --- */
47 typedef struct ssource
{
56 /* --- Socket target --- */
58 typedef struct starget
{
64 /* --- Socket target endpoint --- */
66 typedef struct stept
{
72 /* --- Socket source endpoint --- */
74 typedef struct ssept
{
79 /*----- Protocol table ----------------------------------------------------*/
81 static addr_ops
*addrs
[] = { &inet_ops
, &un_ops
, 0 };
83 /*----- Other persistent variables ----------------------------------------*/
85 static addr_opts gsao
= { 0 }, gtao
= { 0 };
87 /*----- Parsing address types ---------------------------------------------*/
89 /* --- @getaddrtype@ --- *
91 * Arguments: @scanner *sc@ = pointer to scanner (for error reporting)
92 * @const char *p@ = pointer to protocol name
93 * @int abbrev@ = nonzero to allow abbreviations
95 * Returns: Pointer to address operations table or null.
97 * Use: Looks up a protocol name. Handy when parsing addresses and
98 * other bits of configuration. Returns null if no matching
102 static addr_ops
*getaddrtype(scanner
*sc
, const char *p
, int abbrev
)
105 addr_ops
*chosen
= 0;
106 size_t sz
= strlen(p
);
108 for (ops
= addrs
; *ops
; ops
++) {
109 if (strncmp((*ops
)->name
, p
, sz
) == 0) {
110 if ((*ops
)->name
[sz
] == 0)
112 else if (chosen
&& abbrev
)
113 error(sc
, "ambiguous socket address type `%s'", p
);
122 /* --- @getaddr@ --- *
124 * Arguments: @scanner *sc@ = pointer to scanner to read from
125 * @unsigned type@ = address type (@ADDR_SRC@ or @ADDR_DEST@)
127 * Returns: Pointer to an address successfully read.
129 * Use: Reads an optionally qualified address.
132 static addr
*getaddr(scanner
*sc
, unsigned type
)
141 if (sc
->t
== CTOK_WORD
)
142 ops
= getaddrtype(sc
, sc
->d
.buf
, abbrev
);
146 error(sc
, "unknown socket address type `%s'", sc
->d
.buf
);
152 return (ops
->read(sc
, type
));
155 /*----- Socket endpoints --------------------------------------------------*/
157 /* --- @wclose@ --- */
159 static void sept_wclose(endpt
*e
)
161 shutdown(e
->out
->fd
, 1);
164 /* --- @close@ (source) --- */
166 static void ss_listen(ssource */
*ss*/
);
168 static void ssept_close(endpt
*e
)
170 ssept
*ee
= (ssept
*)e
;
172 if (ee
->s
->o
.opt
== SOCKOPT_LIMIT
) {
174 if (ee
->s
->o
.conn
== 1)
178 REFFD_DEC(ee
->e
.out
);
183 /* --- @close@ (target) --- */
185 static void stept_close(endpt
*e
)
187 stept
*ee
= (stept
*)e
;
189 if (ee
->e
.f
& EPF_PENDING
)
193 REFFD_DEC(ee
->e
.out
);
201 /* --- @starget_connected@ --- *
203 * Arguments: @int fd@ = file descriptor now ready for use
204 * @void *p@ = pointer to an endpoint structure
208 * Use: Handles successful connection of the target endpoint.
211 void starget_connected(int fd
, void *p
)
216 fw_log(-1, "[%s] connection failed: %s", e
->desc
, strerror(errno
));
219 reffd
*r
= reffd_init(fd
);
222 fdflags(fd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
223 setsockopt(fd
, SOL_SOCKET
, SO_OOBINLINE
, &opt
, sizeof(opt
));
224 e
->e
.in
= e
->e
.out
= r
;
225 e
->e
.f
&= ~EPF_PENDING
;
227 endpt_join(&e
->e
, e
->e
.other
);
231 /* --- Socket endpoint definition --- */
233 static endpt_ops ssept_ops
= {
234 0, 0, sept_wclose
, ssept_close
237 static endpt_ops stept_ops
= {
238 0, 0, sept_wclose
, stept_close
241 /*----- Source definition -------------------------------------------------*/
243 /* --- @option@ --- */
245 static int ssource_option(source
*s
, scanner
*sc
)
247 ssource
*ss
= (ssource
*)s
;
248 ssource_opts
*sso
= ss ?
&ss
->o
: &ssgo
;
250 CONF_BEGIN(sc
, "socket", "socket source")
252 /* --- Make sure the next token is a word --- */
254 if (sc
->t
!= CTOK_WORD
)
255 error(sc
, "parse error, option keyword expected");
257 /* --- Handle options at this level --- */
259 if (strcmp(sc
->d
.buf
, "conn") == 0) {
263 if (sc
->t
!= CTOK_WORD
)
264 error(sc
, "parse error, expected `unlimited', `one-shot' or number");
265 if (isdigit((unsigned char)sc
->d
.buf
[0])) {
266 sso
->conn
= atoi(sc
->d
.buf
);
268 error(sc
, "argument of `conn' must be positive");
269 sso
->opt
= SOCKOPT_LIMIT
;
273 sso
->opt
= 1 + (1 & conf_enum(sc
,
274 "unlimited,one-shot,infinite",
275 ENUM_ABBREV
, "`conn' option"));
280 if (strcmp(sc
->d
.buf
, "listen") == 0) {
284 if (sc
->t
!= CTOK_WORD
|| !isdigit((unsigned char)sc
->d
.buf
[0]))
285 error(sc
, "parse error, expected number");
286 sso
->listen
= atoi(sc
->d
.buf
);
287 if (sso
->listen
== 0)
288 error(sc
, "argument of `listen' must be positive");
293 if (strcmp(sc
->d
.buf
, "logging") == 0 ||
294 strcmp(sc
->d
.buf
, "log") == 0) {
295 addr_opts
*ao
= ss ? ss
->ao
: &gsao
;
299 if (conf_enum(sc
, "no,yes", ENUM_ABBREV
, "logging status"))
300 ao
->f
&= ~ADDRF_NOLOG
;
302 ao
->f
|= ADDRF_NOLOG
;
306 /* --- Pass the option around the various address types --- */
309 if (ss
->a
->ops
->option
&& ss
->a
->ops
->option(sc
, ss
->ao
, ADDR_SRC
))
313 for (a
= addrs
; *a
; a
++) {
314 if ((*a
)->option
&& (*a
)->option(sc
, 0, ADDR_GLOBAL
))
319 /* --- Nobody understood the option --- */
326 static source
*ssource_read(scanner
*sc
)
330 (void)(conf_prefix(sc
, "socket") || conf_prefix(sc
, "sk"));
331 ss
= CREATE(ssource
);
332 ss
->s
.ops
= &ssource_ops
;
335 ss
->a
= getaddr(sc
, ADDR_SRC
);
336 if (ss
->a
->ops
->initsrcopts
)
337 ss
->ao
= ss
->a
->ops
->initsrcopts();
339 ss
->ao
= CREATE(addr_opts
);
346 /* --- @ss_accept@ --- *
348 * Arguments: @int fd@ = file descriptor to accept from
349 * @unsigned mode@ = what's ready with the descriptor
350 * @void *p@ = pointer to the source definition
354 * Use: Accepts an incoming connection and attaches it to a target
358 static void ssource_destroy(source */
*s*/
);
360 static void ss_accept(int fd
, unsigned mode
, void *p
)
367 /* --- Make the file descriptor --- */
371 if ((r
= ss
->a
->ops
->accept(fd
, ss
->ao
, ss
->s
.desc
)) == 0)
373 setsockopt(r
->fd
, SOL_SOCKET
, SO_OOBINLINE
, &opt
, sizeof(opt
));
374 fdflags(r
->fd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
377 /* --- Make an endpoint --- */
380 e
->e
.ops
= &ssept_ops
;
384 e
->e
.in
= e
->e
.out
= r
;
388 /* --- Obtain the target endpoint and let rip --- */
390 if ((ee
= ss
->t
->ops
->create(ss
->t
, ss
->s
.desc
)) == 0) {
398 /* --- Remove the listening socket if necessary --- */
404 if (!(ss
->ao
->f
& ADDRF_NOLOG
))
405 fw_log(-1, "[%s] maximum connections reached", ss
->s
.desc
);
408 if (ss
->a
->ops
->unbind
)
409 ss
->a
->ops
->unbind(ss
->a
);
412 case SOCKOPT_NOLIMIT
:
414 case SOCKOPT_ONESHOT
:
417 if (ss
->a
->ops
->unbind
)
418 ss
->a
->ops
->unbind(ss
->a
);
419 ssource_destroy(&ss
->s
);
423 /* --- Let everything else happen --- */
425 endpt_join(&e
->e
, ee
);
428 /* --- @ss_listen@ --- *
430 * Arguments: @ssource *ss@ = source to listen on
434 * Use: Sets the socket to listen again, if it stopped for some
435 * reason. This is a copy of the code in the @read@ function,
436 * because it has different (wildly different) error handling
440 static void ss_listen(ssource
*ss
)
445 if (!(ss
->ao
->f
& ADDRF_NOLOG
))
446 fw_log(-1, "[%s] reattaching listener", ss
->s
.desc
);
448 /* --- Make the socket --- */
450 if ((fd
= ss
->a
->ops
->bind(ss
->a
, ss
->ao
)) < 0) {
451 fw_log(-1, "[%s] couldn't create socket: %s",
452 ss
->s
.desc
, strerror(errno
));
456 /* --- Set it to listen for connections --- */
458 setsockopt(fd
, SOL_SOCKET
, SO_OOBINLINE
, &opt
, sizeof(opt
));
459 fdflags(fd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
460 if (listen(fd
, ss
->o
.listen
)) {
461 fw_log(-1, "[%s] couldn't listen on socket: %s",
462 ss
->s
.desc
, strerror(errno
));
466 /* --- Set the listener up again --- */
472 /* --- Tidy up if it failed --- *
474 * I'll just remove the entire source.
481 ssource_destroy(&ss
->s
);
484 /* --- @attach@ --- */
486 static void ssource_attach(source
*s
, scanner
*sc
, target
*t
)
488 ssource
*ss
= (ssource
*)s
;
494 /* --- Initialize the description string --- */
498 dstr_puts(&d
, "socket.");
499 ss
->a
->ops
->print(ss
->a
, ADDR_SRC
, &d
);
500 dstr_puts(&d
, " -> ");
501 dstr_puts(&d
, ss
->t
->desc
);
502 ss
->s
.desc
= xstrdup(d
.buf
);
506 /* --- Confirm the address --- */
508 if (ss
->a
->ops
->confirm
)
509 ss
->a
->ops
->confirm(ss
->a
, ADDR_SRC
, ss
->ao
);
511 /* --- Initialize the socket for listening --- */
513 if ((fd
= ss
->a
->ops
->bind(ss
->a
, ss
->ao
)) < 0)
514 error(sc
, "couldn't bind socket `%s': %s", ss
->s
.desc
, strerror(errno
));
516 /* --- Set it to listen for connections --- */
518 setsockopt(fd
, SOL_SOCKET
, SO_OOBINLINE
, &opt
, sizeof(opt
));
519 fdflags(fd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
520 if (listen(fd
, ss
->o
.listen
)) {
521 error(sc
, "couldn't listen on socket `%s': %s",
522 ss
->s
.desc
, strerror(errno
));
525 /* --- We're ready to go now --- */
527 sel_initfile(sel
, &ss
->r
, fd
, SEL_READ
, ss_accept
, ss
);
533 /* --- @destroy@ --- */
535 static void ssource_destroy(source
*s
)
537 ssource
*ss
= (ssource
*)s
;
539 if (ss
->o
.conn
|| ss
->o
.opt
!= SOCKOPT_LIMIT
) {
542 if (ss
->a
->ops
->unbind
)
543 ss
->a
->ops
->unbind(ss
->a
);
545 if (ss
->a
->ops
->freesrcopts
)
546 ss
->a
->ops
->freesrcopts(ss
->ao
);
550 ss
->a
->ops
->destroy(ss
->a
);
551 ss
->t
->ops
->destroy(ss
->t
);
552 source_remove(&ss
->s
);
557 /* --- Source definition block --- */
559 source_ops ssource_ops
= {
561 ssource_option
, ssource_read
, ssource_attach
, ssource_destroy
564 /*----- Target definition -------------------------------------------------*/
566 /* --- @options@ --- */
568 static int starget_option(target
*t
, scanner
*sc
)
570 starget
*st
= (starget
*)t
;
572 CONF_BEGIN(sc
, "starget", "socket target")
574 /* --- Pass the option around the various address types --- */
577 if (st
->a
->ops
->option
&& st
->a
->ops
->option(sc
, st
->ao
, ADDR_DEST
))
580 /* We'd have done it already if it was global */
589 static target
*starget_read(scanner
*sc
)
594 (void)(conf_prefix(sc
, "socket") || conf_prefix(sc
, "sk"));
595 st
= CREATE(starget
);
596 st
->t
.ops
= &starget_ops
;
597 st
->a
= getaddr(sc
, ADDR_DEST
);
598 if (st
->a
->ops
->inittargopts
)
599 st
->ao
= st
->a
->ops
->inittargopts();
601 st
->ao
= CREATE(addr_opts
);
604 dstr_puts(&d
, "socket.");
605 st
->a
->ops
->print(st
->a
, ADDR_DEST
, &d
);
606 st
->t
.desc
= xstrdup(d
.buf
);
611 /* --- @confirm@ --- */
613 static void starget_confirm(target
*t
)
615 starget
*st
= (starget
*)t
;
617 if (st
->a
->ops
->confirm
)
618 st
->a
->ops
->confirm(st
->a
, ADDR_DEST
, st
->ao
);
621 /* --- @create@ --- */
623 static endpt
*starget_create(target
*t
, const char *desc
)
625 starget
*st
= (starget
*)t
;
626 stept
*e
= CREATE(stept
);
628 e
->e
.ops
= &stept_ops
;
630 e
->e
.f
= EPF_FILE
| EPF_PENDING
;
632 e
->desc
= xstrdup(desc
);
633 if (st
->a
->ops
->connect(st
->a
, st
->ao
, &e
->c
, &e
->e
)) {
634 fw_log(-1, "[%s] couldn't connect: %s", e
->desc
, strerror(errno
));
642 /* --- @destroy@ --- */
644 static void starget_destroy(target
*t
)
646 starget
*st
= (starget
*)t
;
647 if (st
->a
->ops
->freetargopts
)
648 st
->a
->ops
->freetargopts(st
->ao
);
651 st
->a
->ops
->destroy(st
->a
);
656 /* --- Socket target definition block --- */
658 target_ops starget_ops
= {
660 starget_option
, starget_read
, starget_confirm
,
661 starget_create
, starget_destroy
664 /*----- That's all, folks -------------------------------------------------*/