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
);
382 /* --- @ss_accept@ --- *
384 * Arguments: @int fd@ = file descriptor to accept from
385 * @unsigned mode@ = what's ready with the descriptor
386 * @void *p@ = pointer to the source definition
390 * Use: Accepts an incoming connection and attaches it to a target
394 static void ssource_destroy(source */
*s*/
);
396 static void ss_accept(int fd
, unsigned mode
, void *p
)
403 /* --- Make the file descriptor --- */
407 if ((r
= ss
->a
->ops
->accept(fd
, ss
->ao
, ss
->s
.desc
)) == 0)
409 setsockopt(r
->fd
, SOL_SOCKET
, SO_OOBINLINE
, &opt
, sizeof(opt
));
410 fdflags(r
->fd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
413 /* --- Make an endpoint --- */
416 e
->e
.ops
= &ssept_ops
;
420 e
->e
.in
= e
->e
.out
= r
;
424 /* --- Obtain the target endpoint and let rip --- */
426 if ((ee
= ss
->t
->ops
->create(ss
->t
, ss
->s
.desc
)) == 0) {
434 /* --- Remove the listening socket if necessary --- */
440 if (!(ss
->ao
->f
& ADDRF_NOLOG
))
441 fw_log(-1, "[%s] maximum connections reached", ss
->s
.desc
);
444 if (ss
->a
->ops
->unbind
)
445 ss
->a
->ops
->unbind(ss
->a
);
448 case SOCKOPT_NOLIMIT
:
450 case SOCKOPT_ONESHOT
:
453 if (ss
->a
->ops
->unbind
)
454 ss
->a
->ops
->unbind(ss
->a
);
455 ssource_destroy(&ss
->s
);
459 /* --- Let everything else happen --- */
461 endpt_join(&e
->e
, ee
);
464 /* --- @ss_listen@ --- *
466 * Arguments: @ssource *ss@ = source to listen on
470 * Use: Sets the socket to listen again, if it stopped for some
471 * reason. This is a copy of the code in the @read@ function,
472 * because it has different (wildly different) error handling
476 static void ss_listen(ssource
*ss
)
481 if (!(ss
->ao
->f
& ADDRF_NOLOG
))
482 fw_log(-1, "[%s] reattaching listener", ss
->s
.desc
);
484 /* --- Make the socket --- */
486 if ((fd
= ss
->a
->ops
->bind(ss
->a
, ss
->ao
)) < 0) {
487 fw_log(-1, "[%s] couldn't create socket: %s",
488 ss
->s
.desc
, strerror(errno
));
492 /* --- Set it to listen for connections --- */
494 setsockopt(fd
, SOL_SOCKET
, SO_OOBINLINE
, &opt
, sizeof(opt
));
495 fdflags(fd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
496 if (listen(fd
, ss
->o
.listen
)) {
497 fw_log(-1, "[%s] couldn't listen on socket: %s",
498 ss
->s
.desc
, strerror(errno
));
502 /* --- Set the listener up again --- */
508 /* --- Tidy up if it failed --- *
510 * I'll just remove the entire source.
517 ssource_destroy(&ss
->s
);
520 /* --- @attach@ --- */
522 static void ssource_attach(source
*s
, scanner
*sc
, target
*t
)
524 ssource
*ss
= (ssource
*)s
;
530 /* --- Initialize the description string --- */
534 dstr_puts(&d
, "socket.");
535 ss
->a
->ops
->print(ss
->a
, ADDR_SRC
, &d
);
536 dstr_puts(&d
, " -> ");
537 dstr_puts(&d
, ss
->t
->desc
);
538 ss
->s
.desc
= xstrdup(d
.buf
);
542 /* --- Confirm the address --- */
544 if (ss
->a
->ops
->confirm
)
545 ss
->a
->ops
->confirm(ss
->a
, ADDR_SRC
, ss
->ao
);
547 /* --- Initialize the socket for listening --- */
549 if ((fd
= ss
->a
->ops
->bind(ss
->a
, ss
->ao
)) < 0)
550 error(sc
, "couldn't bind socket `%s': %s", ss
->s
.desc
, strerror(errno
));
552 /* --- Set it to listen for connections --- */
554 setsockopt(fd
, SOL_SOCKET
, SO_OOBINLINE
, &opt
, sizeof(opt
));
555 fdflags(fd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
556 if (listen(fd
, ss
->o
.listen
)) {
557 error(sc
, "couldn't listen on socket `%s': %s",
558 ss
->s
.desc
, strerror(errno
));
561 /* --- We're ready to go now --- */
563 sel_initfile(sel
, &ss
->r
, fd
, SEL_READ
, ss_accept
, ss
);
569 /* --- @destroy@ --- */
571 static void ssource_destroy(source
*s
)
573 ssource
*ss
= (ssource
*)s
;
575 if (ss
->o
.conn
|| ss
->o
.opt
!= SOCKOPT_LIMIT
) {
578 if (ss
->a
->ops
->unbind
)
579 ss
->a
->ops
->unbind(ss
->a
);
581 if (ss
->a
->ops
->freesrcopts
)
582 ss
->a
->ops
->freesrcopts(ss
->ao
);
586 ss
->a
->ops
->destroy(ss
->a
);
587 ss
->t
->ops
->destroy(ss
->t
);
588 source_remove(&ss
->s
);
593 /* --- Source definition block --- */
595 source_ops ssource_ops
= {
597 ssource_option
, ssource_read
, ssource_attach
, ssource_destroy
600 /*----- Target definition -------------------------------------------------*/
602 /* --- @options@ --- */
604 static int starget_option(target
*t
, scanner
*sc
)
606 starget
*st
= (starget
*)t
;
608 CONF_BEGIN(sc
, "starget", "socket target")
610 /* --- Pass the option around the various address types --- */
613 if (st
->a
->ops
->option
&& st
->a
->ops
->option(sc
, st
->ao
, ADDR_DEST
))
616 /* We'd have done it already if it was global */
625 static target
*starget_read(scanner
*sc
)
630 (void)(conf_prefix(sc
, "socket") || conf_prefix(sc
, "sk"));
631 st
= CREATE(starget
);
632 st
->t
.ops
= &starget_ops
;
633 st
->a
= getaddr(sc
, ADDR_DEST
);
634 if (st
->a
->ops
->inittargopts
)
635 st
->ao
= st
->a
->ops
->inittargopts();
637 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 -------------------------------------------------*/