3 * $Id: socket.c,v 1.9 2002/02/23 00:08: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.9 2002/02/23 00:08:00 mdw
33 * Fix stupid bugs from the listen(2) change.
35 * Revision 1.8 2002/02/22 23:44:44 mdw
36 * Call @xfree@ rather than @free@. Add option to change the listen(2)
39 * Revision 1.7 2001/06/22 19:37:00 mdw
40 * New @conn_init@ interface.
42 * Revision 1.6 2001/02/03 20:30:03 mdw
43 * Support re-reading config files on SIGHUP.
45 * Revision 1.5 2000/03/23 23:20:42 mdw
46 * Remove listener even if connection option isn't SOCKOPT_LIMITED.
48 * Revision 1.4 1999/12/22 15:44:25 mdw
51 * Revision 1.3 1999/10/22 22:48:36 mdw
52 * New connection options: unlimited concurrent connections, and one-shot
55 * Revision 1.2 1999/07/27 18:30:53 mdw
56 * Various minor portability fixes.
58 * Revision 1.1 1999/07/26 23:33:32 mdw
59 * New sources and targets.
63 /*----- Header files ------------------------------------------------------*/
74 #include <sys/types.h>
77 #include <sys/socket.h>
78 #include <netinet/in.h>
79 #include <arpa/inet.h>
82 #include <mLib/alloc.h>
83 #include <mLib/conn.h>
84 #include <mLib/dstr.h>
85 #include <mLib/fdflags.h>
100 /*----- Data structures ---------------------------------------------------*/
102 /* --- Socket source options --- */
104 typedef struct ssource_opts
{
110 static ssource_opts ssgo
= { 256, 0, 5 };
112 #define SOCKOPT_LIMIT 0u
113 #define SOCKOPT_NOLIMIT 1u
114 #define SOCKOPT_ONESHOT 2u
116 /* --- Socket source --- */
118 typedef struct ssource
{
127 /* --- Socket target --- */
129 typedef struct starget
{
135 /* --- Socket target endpoint --- */
137 typedef struct stept
{
143 /* --- Socket source endpoint --- */
145 typedef struct ssept
{
150 /*----- Protocol table ----------------------------------------------------*/
152 static addr_ops
*addrs
[] = { &inet_ops
, &un_ops
, 0 };
154 /*----- Other persistent variables ----------------------------------------*/
156 static addr_opts gao
= { 0 };
158 /*----- Parsing address types ---------------------------------------------*/
160 /* --- @getaddrtype@ --- *
162 * Arguments: @scanner *sc@ = pointer to scanner (for error reporting)
163 * @const char *p@ = pointer to protocol name
164 * @int abbrev@ = nonzero to allow abbreviations
166 * Returns: Pointer to address operations table or null.
168 * Use: Looks up a protocol name. Handy when parsing addresses and
169 * other bits of configuration. Returns null if no matching
173 static addr_ops
*getaddrtype(scanner
*sc
, const char *p
, int abbrev
)
176 addr_ops
*chosen
= 0;
177 size_t sz
= strlen(p
);
179 for (ops
= addrs
; *ops
; ops
++) {
180 if (strncmp((*ops
)->name
, p
, sz
) == 0) {
181 if ((*ops
)->name
[sz
] == 0)
183 else if (chosen
&& abbrev
)
184 error(sc
, "ambiguous socket address type `%s'", p
);
193 /* --- @getaddr@ --- *
195 * Arguments: @scanner *sc@ = pointer to scanner to read from
196 * @unsigned type@ = address type (@ADDR_SRC@ or @ADDR_DEST@)
198 * Returns: Pointer to an address successfully read.
200 * Use: Reads an optionally qualified address.
203 static addr
*getaddr(scanner
*sc
, unsigned type
)
212 if (sc
->t
== CTOK_WORD
)
213 ops
= getaddrtype(sc
, sc
->d
.buf
, abbrev
);
217 error(sc
, "unknown socket address type `%s'", sc
->d
.buf
);
223 return (ops
->read(sc
, type
));
226 /*----- Socket endpoints --------------------------------------------------*/
228 /* --- @wclose@ --- */
230 static void sept_wclose(endpt
*e
)
232 shutdown(e
->out
->fd
, 1);
235 /* --- @close@ (source) --- */
237 static void ss_listen(ssource */
*ss*/
);
239 static void ssept_close(endpt
*e
)
241 ssept
*ee
= (ssept
*)e
;
243 if (ee
->s
->o
.opt
== SOCKOPT_LIMIT
) {
245 if (ee
->s
->o
.conn
== 1)
249 REFFD_DEC(ee
->e
.out
);
254 /* --- @close@ (target) --- */
256 static void stept_close(endpt
*e
)
258 stept
*ee
= (stept
*)e
;
260 if (ee
->e
.f
& EPF_PENDING
)
264 REFFD_DEC(ee
->e
.out
);
272 /* --- @stept_go@ --- *
274 * Arguments: @int fd@ = file descriptor now ready for use
275 * @void *p@ = pointer to an endpoint structure
279 * Use: Handles successful connection of the target endpoint.
282 static void stept_go(int fd
, void *p
)
287 fw_log(-1, "[%s] connection failed: %s", e
->desc
, strerror(errno
));
290 reffd
*r
= reffd_init(fd
);
292 e
->e
.in
= e
->e
.out
= r
;
293 e
->e
.f
&= ~EPF_PENDING
;
295 endpt_join(&e
->e
, e
->e
.other
);
299 /* --- Socket endpoint definition --- */
301 static endpt_ops ssept_ops
= {
302 0, 0, sept_wclose
, ssept_close
305 static endpt_ops stept_ops
= {
306 0, 0, sept_wclose
, stept_close
309 /*----- Source definition -------------------------------------------------*/
311 /* --- @option@ --- */
313 static int ssource_option(source
*s
, scanner
*sc
)
315 ssource
*ss
= (ssource
*)s
;
316 ssource_opts
*sso
= ss ?
&ss
->o
: &ssgo
;
318 CONF_BEGIN(sc
, "socket", "socket")
320 /* --- Make sure the next token is a word --- */
322 if (sc
->t
!= CTOK_WORD
)
323 error(sc
, "parse error, option keyword expected");
325 /* --- Handle options at this level --- */
327 if (strcmp(sc
->d
.buf
, "conn") == 0) {
331 if (sc
->t
!= CTOK_WORD
)
332 error(sc
, "parse error, expected `unlimited', `one-shot' or number");
333 if (isdigit((unsigned char)sc
->d
.buf
[0])) {
334 sso
->conn
= atoi(sc
->d
.buf
);
336 error(sc
, "argument of `conn' must be positive");
337 sso
->opt
= SOCKOPT_LIMIT
;
341 sso
->opt
= 1 + (1 & conf_enum(sc
,
342 "unlimited,one-shot,infinite",
343 ENUM_ABBREV
, "`conn' option"));
348 if (strcmp(sc
->d
.buf
, "listen") == 0) {
352 if (sc
->t
!= CTOK_WORD
|| !isdigit((unsigned char)sc
->d
.buf
[0]))
353 error(sc
, "parse error, expected number");
354 sso
->listen
= atoi(sc
->d
.buf
);
355 if (sso
->listen
== 0)
356 error(sc
, "argument of `listen' must be positive");
361 if (strcmp(sc
->d
.buf
, "logging") == 0 ||
362 strcmp(sc
->d
.buf
, "log") == 0) {
363 addr_opts
*ao
= ss ? ss
->ao
: &gao
;
367 if (conf_enum(sc
, "no,yes", ENUM_ABBREV
, "logging status"))
368 ao
->f
&= ~ADDRF_NOLOG
;
370 ao
->f
|= ADDRF_NOLOG
;
374 /* --- Pass the option around the various address types --- */
377 if (ss
->a
->ops
->option
&& ss
->a
->ops
->option(sc
, ss ? ss
->ao
: 0))
381 for (a
= addrs
; *a
; a
++) {
382 if ((*a
)->option
&& (*a
)->option(sc
, 0))
387 /* --- Nobody understood the option --- */
394 static source
*ssource_read(scanner
*sc
)
398 (void)(conf_prefix(sc
, "socket") || conf_prefix(sc
, "sk"));
399 ss
= CREATE(ssource
);
400 ss
->s
.ops
= &ssource_ops
;
403 ss
->a
= getaddr(sc
, ADDR_SRC
);
404 if (ss
->a
->ops
->initopts
)
405 ss
->ao
= ss
->a
->ops
->initopts();
407 ss
->ao
= CREATE(addr_opts
);
413 /* --- @ss_accept@ --- *
415 * Arguments: @int fd@ = file descriptor to accept from
416 * @unsigned mode@ = what's ready with the descriptor
417 * @void *p@ = pointer to the source definition
421 * Use: Accepts an incoming connection and attaches it to a target
425 static void ssource_destroy(source */
*s*/
);
427 static void ss_accept(int fd
, unsigned mode
, void *p
)
434 /* --- Make the file descriptor --- */
438 if ((r
= ss
->a
->ops
->accept(fd
, ss
->ao
, ss
->s
.desc
)) == 0)
440 setsockopt(r
->fd
, SOL_SOCKET
, SO_OOBINLINE
, &opt
, sizeof(opt
));
441 fdflags(r
->fd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
444 /* --- Make an endpoint --- */
447 e
->e
.ops
= &ssept_ops
;
451 e
->e
.in
= e
->e
.out
= r
;
455 /* --- Obtain the target endpoint and let rip --- */
457 if ((ee
= ss
->t
->ops
->create(ss
->t
, ss
->s
.desc
)) == 0) {
465 /* --- Remove the listening socket if necessary --- */
471 if (!(ss
->ao
->f
& ADDRF_NOLOG
))
472 fw_log(-1, "[%s] maximum connections reached", ss
->s
.desc
);
475 if (ss
->a
->ops
->unbind
)
476 ss
->a
->ops
->unbind(ss
->a
);
479 case SOCKOPT_NOLIMIT
:
481 case SOCKOPT_ONESHOT
:
484 if (ss
->a
->ops
->unbind
)
485 ss
->a
->ops
->unbind(ss
->a
);
486 ssource_destroy(&ss
->s
);
490 /* --- Let everything else happen --- */
492 endpt_join(&e
->e
, ee
);
495 /* --- @ss_listen@ --- *
497 * Arguments: @ssource *ss@ = source to listen on
501 * Use: Sets the socket to listen again, if it stopped for some
502 * reason. This is a copy of the code in the @read@ function,
503 * because it has different (wildly different) error handling
507 static void ss_listen(ssource
*ss
)
509 gen_addr
*ga
= (gen_addr
*)ss
->a
;
512 if (!(ss
->ao
->f
& ADDRF_NOLOG
))
513 fw_log(-1, "[%s] reattaching listener", ss
->s
.desc
);
515 /* --- Make the socket --- */
517 if ((fd
= socket(ga
->a
.ops
->pf
, SOCK_STREAM
, 0)) < 0) {
518 fw_log(-1, "[%s] couldn't create socket: %s",
519 ss
->s
.desc
, strerror(errno
));
523 /* --- Set it to allow address reuse --- */
527 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &opt
, sizeof(opt
));
528 fdflags(fd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
531 /* --- Bind it to the right port --- */
533 if (bind(fd
, &ga
->sa
, ga
->a
.sz
)) {
534 fw_log(-1, "[%s] couldn't bind socket: %s", ss
->s
.desc
, strerror(errno
));
537 if (ga
->a
.ops
->bound
)
538 ga
->a
.ops
->bound(&ga
->a
, ss
->ao
);
540 /* --- Set it to listen for connections --- */
542 if (listen(fd
, ss
->o
.listen
)) {
543 fw_log(-1, "[%s] couldn't listen on socket: %s",
544 ss
->s
.desc
, strerror(errno
));
548 /* --- Set the listener up again --- */
554 /* --- Tidy up if it failed --- *
556 * I'll just remove the entire source.
563 ssource_destroy(&ss
->s
);
566 /* --- @attach@ --- */
568 static void ssource_attach(source
*s
, scanner
*sc
, target
*t
)
570 ssource
*ss
= (ssource
*)s
;
575 /* --- Initialize the description string --- */
579 dstr_puts(&d
, "socket.");
580 ss
->a
->ops
->print(ss
->a
, ADDR_SRC
, &d
);
581 dstr_puts(&d
, " -> ");
582 dstr_puts(&d
, ss
->t
->desc
);
583 ss
->s
.desc
= xstrdup(d
.buf
);
587 /* --- Initialize the socket for listening --- */
590 gen_addr
*ga
= (gen_addr
*)ss
->a
;
592 /* --- Make the socket --- */
594 if ((fd
= socket(ga
->a
.ops
->pf
, SOCK_STREAM
, 0)) < 0)
595 error(sc
, "couldn't create socket: %s", strerror(errno
));
597 /* --- Set it to allow address reuse --- */
601 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &opt
, sizeof(opt
));
602 fdflags(fd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
605 /* --- Bind it to the right port --- */
607 if (bind(fd
, &ga
->sa
, ga
->a
.sz
))
608 error(sc
, "couldn't bind to %s: %s", ss
->s
.desc
, strerror(errno
));
609 if (ga
->a
.ops
->bound
)
610 ga
->a
.ops
->bound(&ga
->a
, ss
->ao
);
612 /* --- Set it to listen for connections --- */
614 if (listen(fd
, ss
->o
.listen
))
615 error(sc
, "couldn't listen on socket: %s", strerror(errno
));
618 /* --- We're ready to go now --- */
620 sel_initfile(sel
, &ss
->r
, fd
, SEL_READ
, ss_accept
, ss
);
626 /* --- @destroy@ --- */
628 static void ssource_destroy(source
*s
)
630 ssource
*ss
= (ssource
*)s
;
632 if (ss
->o
.conn
|| ss
->o
.opt
!= SOCKOPT_LIMIT
) {
635 if (ss
->a
->ops
->unbind
)
636 ss
->a
->ops
->unbind(ss
->a
);
638 if (ss
->a
->ops
->freeopts
)
639 ss
->a
->ops
->freeopts(ss
->ao
);
643 ss
->a
->ops
->destroy(ss
->a
);
644 ss
->t
->ops
->destroy(ss
->t
);
645 source_remove(&ss
->s
);
650 /* --- Source definition block --- */
652 source_ops ssource_ops
= {
654 ssource_option
, ssource_read
, ssource_attach
, ssource_destroy
657 /*----- Target definition -------------------------------------------------*/
661 static target
*starget_read(scanner
*sc
)
666 (void)(conf_prefix(sc
, "socket") || conf_prefix(sc
, "sk"));
667 st
= CREATE(starget
);
668 st
->t
.ops
= &starget_ops
;
669 st
->a
= getaddr(sc
, ADDR_DEST
);
670 dstr_puts(&d
, "socket.");
671 st
->a
->ops
->print(st
->a
, ADDR_DEST
, &d
);
672 st
->t
.desc
= xstrdup(d
.buf
);
677 /* --- @create@ --- *
679 * Arguments: @target *t@ = pointer to target
680 * @const char *desc@ = description of connection
682 * Returns: Pointer to a created endpoint.
684 * Use: Generates a target endpoint for communication.
687 static endpt
*starget_create(target
*t
, const char *desc
)
689 starget
*st
= (starget
*)t
;
690 stept
*e
= CREATE(stept
);
692 gen_addr
*ga
= (gen_addr
*)st
->a
;
695 if ((fd
= socket(st
->a
->ops
->pf
, SOCK_STREAM
, 0)) < 0)
697 setsockopt(fd
, SOL_SOCKET
, SO_OOBINLINE
, &opt
, sizeof(opt
));
698 fdflags(fd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
699 e
->e
.ops
= &stept_ops
;
701 e
->e
.f
= EPF_FILE
| EPF_PENDING
;
703 e
->desc
= xstrdup(desc
);
705 if (conn_init(&e
->c
, sel
, fd
, &ga
->sa
, ga
->a
.sz
, stept_go
, e
)) {
706 fw_log(-1, "[%s] connection failed: %s", e
->desc
, strerror(errno
));
714 /* --- @destroy@ --- */
716 static void starget_destroy(target
*t
)
718 starget
*st
= (starget
*)t
;
719 st
->a
->ops
->destroy(st
->a
);
724 /* --- Socket target definition block --- */
726 target_ops starget_ops
= {
728 0, starget_read
, starget_create
, starget_destroy
731 /*----- That's all, folks -------------------------------------------------*/