3 * $Id: socket.c,v 1.10 2003/11/25 14:08:23 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.10 2003/11/25 14:08:23 mdw
33 * Debianization. Socket target options. Internet binding.
35 * Revision 1.9 2002/02/23 00:08:00 mdw
36 * Fix stupid bugs from the listen(2) change.
38 * Revision 1.8 2002/02/22 23:44:44 mdw
39 * Call @xfree@ rather than @free@. Add option to change the listen(2)
42 * Revision 1.7 2001/06/22 19:37:00 mdw
43 * New @conn_init@ interface.
45 * Revision 1.6 2001/02/03 20:30:03 mdw
46 * Support re-reading config files on SIGHUP.
48 * Revision 1.5 2000/03/23 23:20:42 mdw
49 * Remove listener even if connection option isn't SOCKOPT_LIMITED.
51 * Revision 1.4 1999/12/22 15:44:25 mdw
54 * Revision 1.3 1999/10/22 22:48:36 mdw
55 * New connection options: unlimited concurrent connections, and one-shot
58 * Revision 1.2 1999/07/27 18:30:53 mdw
59 * Various minor portability fixes.
61 * Revision 1.1 1999/07/26 23:33:32 mdw
62 * New sources and targets.
66 /*----- Header files ------------------------------------------------------*/
77 #include <sys/types.h>
80 #include <sys/socket.h>
81 #include <netinet/in.h>
82 #include <arpa/inet.h>
85 #include <mLib/alloc.h>
86 #include <mLib/conn.h>
87 #include <mLib/dstr.h>
88 #include <mLib/fdflags.h>
103 /*----- Data structures ---------------------------------------------------*/
105 /* --- Socket source options --- */
107 typedef struct ssource_opts
{
113 static ssource_opts ssgo
= { 256, 0, 5 };
115 #define SOCKOPT_LIMIT 0u
116 #define SOCKOPT_NOLIMIT 1u
117 #define SOCKOPT_ONESHOT 2u
119 /* --- Socket source --- */
121 typedef struct ssource
{
130 /* --- Socket target --- */
132 typedef struct starget
{
138 /* --- Socket target endpoint --- */
140 typedef struct stept
{
146 /* --- Socket source endpoint --- */
148 typedef struct ssept
{
153 /*----- Protocol table ----------------------------------------------------*/
155 static addr_ops
*addrs
[] = { &inet_ops
, &un_ops
, 0 };
157 /*----- Other persistent variables ----------------------------------------*/
159 static addr_opts gsao
= { 0 }, gtao
= { 0 };
161 /*----- Parsing address types ---------------------------------------------*/
163 /* --- @getaddrtype@ --- *
165 * Arguments: @scanner *sc@ = pointer to scanner (for error reporting)
166 * @const char *p@ = pointer to protocol name
167 * @int abbrev@ = nonzero to allow abbreviations
169 * Returns: Pointer to address operations table or null.
171 * Use: Looks up a protocol name. Handy when parsing addresses and
172 * other bits of configuration. Returns null if no matching
176 static addr_ops
*getaddrtype(scanner
*sc
, const char *p
, int abbrev
)
179 addr_ops
*chosen
= 0;
180 size_t sz
= strlen(p
);
182 for (ops
= addrs
; *ops
; ops
++) {
183 if (strncmp((*ops
)->name
, p
, sz
) == 0) {
184 if ((*ops
)->name
[sz
] == 0)
186 else if (chosen
&& abbrev
)
187 error(sc
, "ambiguous socket address type `%s'", p
);
196 /* --- @getaddr@ --- *
198 * Arguments: @scanner *sc@ = pointer to scanner to read from
199 * @unsigned type@ = address type (@ADDR_SRC@ or @ADDR_DEST@)
201 * Returns: Pointer to an address successfully read.
203 * Use: Reads an optionally qualified address.
206 static addr
*getaddr(scanner
*sc
, unsigned type
)
215 if (sc
->t
== CTOK_WORD
)
216 ops
= getaddrtype(sc
, sc
->d
.buf
, abbrev
);
220 error(sc
, "unknown socket address type `%s'", sc
->d
.buf
);
226 return (ops
->read(sc
, type
));
229 /*----- Socket endpoints --------------------------------------------------*/
231 /* --- @wclose@ --- */
233 static void sept_wclose(endpt
*e
)
235 shutdown(e
->out
->fd
, 1);
238 /* --- @close@ (source) --- */
240 static void ss_listen(ssource */
*ss*/
);
242 static void ssept_close(endpt
*e
)
244 ssept
*ee
= (ssept
*)e
;
246 if (ee
->s
->o
.opt
== SOCKOPT_LIMIT
) {
248 if (ee
->s
->o
.conn
== 1)
252 REFFD_DEC(ee
->e
.out
);
257 /* --- @close@ (target) --- */
259 static void stept_close(endpt
*e
)
261 stept
*ee
= (stept
*)e
;
263 if (ee
->e
.f
& EPF_PENDING
)
267 REFFD_DEC(ee
->e
.out
);
275 /* --- @starget_connected@ --- *
277 * Arguments: @int fd@ = file descriptor now ready for use
278 * @void *p@ = pointer to an endpoint structure
282 * Use: Handles successful connection of the target endpoint.
285 void starget_connected(int fd
, void *p
)
290 fw_log(-1, "[%s] connection failed: %s", e
->desc
, strerror(errno
));
293 reffd
*r
= reffd_init(fd
);
296 fdflags(fd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
297 setsockopt(fd
, SOL_SOCKET
, SO_OOBINLINE
, &opt
, sizeof(opt
));
298 e
->e
.in
= e
->e
.out
= r
;
299 e
->e
.f
&= ~EPF_PENDING
;
301 endpt_join(&e
->e
, e
->e
.other
);
305 /* --- Socket endpoint definition --- */
307 static endpt_ops ssept_ops
= {
308 0, 0, sept_wclose
, ssept_close
311 static endpt_ops stept_ops
= {
312 0, 0, sept_wclose
, stept_close
315 /*----- Source definition -------------------------------------------------*/
317 /* --- @option@ --- */
319 static int ssource_option(source
*s
, scanner
*sc
)
321 ssource
*ss
= (ssource
*)s
;
322 ssource_opts
*sso
= ss ?
&ss
->o
: &ssgo
;
324 CONF_BEGIN(sc
, "socket", "socket source")
326 /* --- Make sure the next token is a word --- */
328 if (sc
->t
!= CTOK_WORD
)
329 error(sc
, "parse error, option keyword expected");
331 /* --- Handle options at this level --- */
333 if (strcmp(sc
->d
.buf
, "conn") == 0) {
337 if (sc
->t
!= CTOK_WORD
)
338 error(sc
, "parse error, expected `unlimited', `one-shot' or number");
339 if (isdigit((unsigned char)sc
->d
.buf
[0])) {
340 sso
->conn
= atoi(sc
->d
.buf
);
342 error(sc
, "argument of `conn' must be positive");
343 sso
->opt
= SOCKOPT_LIMIT
;
347 sso
->opt
= 1 + (1 & conf_enum(sc
,
348 "unlimited,one-shot,infinite",
349 ENUM_ABBREV
, "`conn' option"));
354 if (strcmp(sc
->d
.buf
, "listen") == 0) {
358 if (sc
->t
!= CTOK_WORD
|| !isdigit((unsigned char)sc
->d
.buf
[0]))
359 error(sc
, "parse error, expected number");
360 sso
->listen
= atoi(sc
->d
.buf
);
361 if (sso
->listen
== 0)
362 error(sc
, "argument of `listen' must be positive");
367 if (strcmp(sc
->d
.buf
, "logging") == 0 ||
368 strcmp(sc
->d
.buf
, "log") == 0) {
369 addr_opts
*ao
= ss ? ss
->ao
: &gsao
;
373 if (conf_enum(sc
, "no,yes", ENUM_ABBREV
, "logging status"))
374 ao
->f
&= ~ADDRF_NOLOG
;
376 ao
->f
|= ADDRF_NOLOG
;
380 /* --- Pass the option around the various address types --- */
383 if (ss
->a
->ops
->option
&& ss
->a
->ops
->option(sc
, ss
->ao
, ADDR_SRC
))
387 for (a
= addrs
; *a
; a
++) {
388 if ((*a
)->option
&& (*a
)->option(sc
, 0, ADDR_GLOBAL
))
393 /* --- Nobody understood the option --- */
400 static source
*ssource_read(scanner
*sc
)
404 (void)(conf_prefix(sc
, "socket") || conf_prefix(sc
, "sk"));
405 ss
= CREATE(ssource
);
406 ss
->s
.ops
= &ssource_ops
;
409 ss
->a
= getaddr(sc
, ADDR_SRC
);
410 if (ss
->a
->ops
->initsrcopts
)
411 ss
->ao
= ss
->a
->ops
->initsrcopts();
413 ss
->ao
= CREATE(addr_opts
);
419 /* --- @ss_accept@ --- *
421 * Arguments: @int fd@ = file descriptor to accept from
422 * @unsigned mode@ = what's ready with the descriptor
423 * @void *p@ = pointer to the source definition
427 * Use: Accepts an incoming connection and attaches it to a target
431 static void ssource_destroy(source */
*s*/
);
433 static void ss_accept(int fd
, unsigned mode
, void *p
)
440 /* --- Make the file descriptor --- */
444 if ((r
= ss
->a
->ops
->accept(fd
, ss
->ao
, ss
->s
.desc
)) == 0)
446 setsockopt(r
->fd
, SOL_SOCKET
, SO_OOBINLINE
, &opt
, sizeof(opt
));
447 fdflags(r
->fd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
450 /* --- Make an endpoint --- */
453 e
->e
.ops
= &ssept_ops
;
457 e
->e
.in
= e
->e
.out
= r
;
461 /* --- Obtain the target endpoint and let rip --- */
463 if ((ee
= ss
->t
->ops
->create(ss
->t
, ss
->s
.desc
)) == 0) {
471 /* --- Remove the listening socket if necessary --- */
477 if (!(ss
->ao
->f
& ADDRF_NOLOG
))
478 fw_log(-1, "[%s] maximum connections reached", ss
->s
.desc
);
481 if (ss
->a
->ops
->unbind
)
482 ss
->a
->ops
->unbind(ss
->a
);
485 case SOCKOPT_NOLIMIT
:
487 case SOCKOPT_ONESHOT
:
490 if (ss
->a
->ops
->unbind
)
491 ss
->a
->ops
->unbind(ss
->a
);
492 ssource_destroy(&ss
->s
);
496 /* --- Let everything else happen --- */
498 endpt_join(&e
->e
, ee
);
501 /* --- @ss_listen@ --- *
503 * Arguments: @ssource *ss@ = source to listen on
507 * Use: Sets the socket to listen again, if it stopped for some
508 * reason. This is a copy of the code in the @read@ function,
509 * because it has different (wildly different) error handling
513 static void ss_listen(ssource
*ss
)
518 if (!(ss
->ao
->f
& ADDRF_NOLOG
))
519 fw_log(-1, "[%s] reattaching listener", ss
->s
.desc
);
521 /* --- Make the socket --- */
523 if ((fd
= ss
->a
->ops
->bind(ss
->a
, ss
->ao
)) < 0) {
524 fw_log(-1, "[%s] couldn't create socket: %s",
525 ss
->s
.desc
, strerror(errno
));
529 /* --- Set it to listen for connections --- */
531 setsockopt(fd
, SOL_SOCKET
, SO_OOBINLINE
, &opt
, sizeof(opt
));
532 fdflags(fd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
533 if (listen(fd
, ss
->o
.listen
)) {
534 fw_log(-1, "[%s] couldn't listen on socket: %s",
535 ss
->s
.desc
, strerror(errno
));
539 /* --- Set the listener up again --- */
545 /* --- Tidy up if it failed --- *
547 * I'll just remove the entire source.
554 ssource_destroy(&ss
->s
);
557 /* --- @attach@ --- */
559 static void ssource_attach(source
*s
, scanner
*sc
, target
*t
)
561 ssource
*ss
= (ssource
*)s
;
567 /* --- Initialize the description string --- */
571 dstr_puts(&d
, "socket.");
572 ss
->a
->ops
->print(ss
->a
, ADDR_SRC
, &d
);
573 dstr_puts(&d
, " -> ");
574 dstr_puts(&d
, ss
->t
->desc
);
575 ss
->s
.desc
= xstrdup(d
.buf
);
579 /* --- Initialize the socket for listening --- */
581 if ((fd
= ss
->a
->ops
->bind(ss
->a
, ss
->ao
)) < 0)
582 error(sc
, "couldn't bind socket `%s': %s", ss
->s
.desc
, strerror(errno
));
584 /* --- Set it to listen for connections --- */
586 setsockopt(fd
, SOL_SOCKET
, SO_OOBINLINE
, &opt
, sizeof(opt
));
587 fdflags(fd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
588 if (listen(fd
, ss
->o
.listen
)) {
589 error(sc
, "couldn't listen on socket `%s': %s",
590 ss
->s
.desc
, strerror(errno
));
593 /* --- We're ready to go now --- */
595 sel_initfile(sel
, &ss
->r
, fd
, SEL_READ
, ss_accept
, ss
);
601 /* --- @destroy@ --- */
603 static void ssource_destroy(source
*s
)
605 ssource
*ss
= (ssource
*)s
;
607 if (ss
->o
.conn
|| ss
->o
.opt
!= SOCKOPT_LIMIT
) {
610 if (ss
->a
->ops
->unbind
)
611 ss
->a
->ops
->unbind(ss
->a
);
613 if (ss
->a
->ops
->freesrcopts
)
614 ss
->a
->ops
->freesrcopts(ss
->ao
);
618 ss
->a
->ops
->destroy(ss
->a
);
619 ss
->t
->ops
->destroy(ss
->t
);
620 source_remove(&ss
->s
);
625 /* --- Source definition block --- */
627 source_ops ssource_ops
= {
629 ssource_option
, ssource_read
, ssource_attach
, ssource_destroy
632 /*----- Target definition -------------------------------------------------*/
634 /* --- @options@ --- */
636 static int starget_option(target
*t
, scanner
*sc
)
638 starget
*st
= (starget
*)t
;
640 CONF_BEGIN(sc
, "starget", "socket target")
642 /* --- Pass the option around the various address types --- */
645 if (st
->a
->ops
->option
&& st
->a
->ops
->option(sc
, st
->ao
, ADDR_DEST
))
648 /* We'd have done it already if it was global */
657 static target
*starget_read(scanner
*sc
)
662 (void)(conf_prefix(sc
, "socket") || conf_prefix(sc
, "sk"));
663 st
= CREATE(starget
);
664 st
->t
.ops
= &starget_ops
;
665 st
->a
= getaddr(sc
, ADDR_DEST
);
666 if (st
->a
->ops
->inittargopts
)
667 st
->ao
= st
->a
->ops
->inittargopts();
669 st
->ao
= CREATE(addr_opts
);
671 dstr_puts(&d
, "socket.");
672 st
->a
->ops
->print(st
->a
, ADDR_DEST
, &d
);
673 st
->t
.desc
= xstrdup(d
.buf
);
678 /* --- @create@ --- *
680 * Arguments: @target *t@ = pointer to target
681 * @const char *desc@ = description of connection
683 * Returns: Pointer to a created endpoint.
685 * Use: Generates a target endpoint for communication.
688 static endpt
*starget_create(target
*t
, const char *desc
)
690 starget
*st
= (starget
*)t
;
691 stept
*e
= CREATE(stept
);
693 e
->e
.ops
= &stept_ops
;
695 e
->e
.f
= EPF_FILE
| EPF_PENDING
;
697 e
->desc
= xstrdup(desc
);
698 if (st
->a
->ops
->connect(st
->a
, st
->ao
, &e
->c
, &e
->e
)) {
699 fw_log(-1, "[%s] couldn't connect: %s", e
->desc
, strerror(errno
));
707 /* --- @destroy@ --- */
709 static void starget_destroy(target
*t
)
711 starget
*st
= (starget
*)t
;
712 if (st
->a
->ops
->freetargopts
)
713 st
->a
->ops
->freetargopts(st
->ao
);
716 st
->a
->ops
->destroy(st
->a
);
721 /* --- Socket target definition block --- */
723 target_ops starget_ops
= {
725 starget_option
, starget_read
, starget_create
, starget_destroy
728 /*----- That's all, folks -------------------------------------------------*/