3 * Main header file for port forwarder
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the `fwd' port forwarder.
12 * `fwd' 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 * `fwd' 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 `fwd'; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 /*----- Header files ------------------------------------------------------*/
36 /* --- Configuration --- */
62 #include <sys/types.h>
68 #include <sys/socket.h>
70 #include <netinet/in.h>
71 #include <arpa/inet.h>
81 #include <mLib/alloc.h>
82 #include <mLib/bres.h>
83 #include <mLib/conn.h>
84 #include <mLib/darray.h>
85 #include <mLib/dstr.h>
87 #include <mLib/fdflags.h>
88 #include <mLib/fdpass.h>
89 #include <mLib/ident.h>
90 #include <mLib/macros.h>
91 #include <mLib/mdup.h>
92 #include <mLib/mdwopt.h>
93 #include <mLib/quis.h>
94 #include <mLib/report.h>
96 #include <mLib/selbuf.h>
100 #include <mLib/sym.h>
103 /*----- Other subtleties --------------------------------------------------*/
105 #if defined(HAVE_DECL_ENVIRON) && !HAVE_DECL_ENVIRON
106 extern char **environ
;
109 /*----- Resource limit names ----------------------------------------------*/
111 #if defined(RLIMIT_OFILE) && !defined(RLIMIT_NOFILE)
112 # define RLIMIT_NOFILE RLIMIT_OFILE
116 ;;; The resource-limit name table is very boring to type and less fun to
117 ;;; maintain. To make life less awful, put the names in this list and
118 ;;; evaluate the code to get Emacs to regenerate it.
120 (let ((limits '(as core cpu data fsize locks memlock msgqueue
121 nice nofile nproc rss rtprio sigpending stack
126 (search-forward (concat "***" "BEGIN rlimitlist" "***"))
127 (beginning-of-line 2)
128 (delete-region (point)
130 (search-forward "***END***")
133 (let ((avail (make-marker))
134 (list (make-marker)))
135 (set-marker avail (point))
136 (insert "#define RLIMITS(_)")
137 (set-marker list (point))
138 (dolist (limit (sort (copy-list limits) #'string<))
139 (let* ((name (symbol-name limit))
140 (constant (concat "RLIMIT_" (upcase name)))
141 (have (concat "HAVE_" constant "_P")))
143 (insert-before-markers (format (concat "#ifdef %s\n"
150 (insert-before-markers
151 (format " \\\n MAYBE_ITEM(_, %s, (%s, %s))"
152 have name constant))))
157 /***BEGIN rlimitlist***/
159 # define HAVE_RLIMIT_AS_P t
161 # define HAVE_RLIMIT_AS_P nil
164 # define HAVE_RLIMIT_CORE_P t
166 # define HAVE_RLIMIT_CORE_P nil
169 # define HAVE_RLIMIT_CPU_P t
171 # define HAVE_RLIMIT_CPU_P nil
174 # define HAVE_RLIMIT_DATA_P t
176 # define HAVE_RLIMIT_DATA_P nil
179 # define HAVE_RLIMIT_FSIZE_P t
181 # define HAVE_RLIMIT_FSIZE_P nil
184 # define HAVE_RLIMIT_LOCKS_P t
186 # define HAVE_RLIMIT_LOCKS_P nil
188 #ifdef RLIMIT_MEMLOCK
189 # define HAVE_RLIMIT_MEMLOCK_P t
191 # define HAVE_RLIMIT_MEMLOCK_P nil
193 #ifdef RLIMIT_MSGQUEUE
194 # define HAVE_RLIMIT_MSGQUEUE_P t
196 # define HAVE_RLIMIT_MSGQUEUE_P nil
199 # define HAVE_RLIMIT_NICE_P t
201 # define HAVE_RLIMIT_NICE_P nil
204 # define HAVE_RLIMIT_NOFILE_P t
206 # define HAVE_RLIMIT_NOFILE_P nil
209 # define HAVE_RLIMIT_NPROC_P t
211 # define HAVE_RLIMIT_NPROC_P nil
214 # define HAVE_RLIMIT_RSS_P t
216 # define HAVE_RLIMIT_RSS_P nil
219 # define HAVE_RLIMIT_RTPRIO_P t
221 # define HAVE_RLIMIT_RTPRIO_P nil
223 #ifdef RLIMIT_SIGPENDING
224 # define HAVE_RLIMIT_SIGPENDING_P t
226 # define HAVE_RLIMIT_SIGPENDING_P nil
229 # define HAVE_RLIMIT_STACK_P t
231 # define HAVE_RLIMIT_STACK_P nil
234 # define HAVE_RLIMIT_VMEM_P t
236 # define HAVE_RLIMIT_VMEM_P nil
239 MAYBE_ITEM(_, HAVE_RLIMIT_AS_P, (as, RLIMIT_AS)) \
240 MAYBE_ITEM(_, HAVE_RLIMIT_CORE_P, (core, RLIMIT_CORE)) \
241 MAYBE_ITEM(_, HAVE_RLIMIT_CPU_P, (cpu, RLIMIT_CPU)) \
242 MAYBE_ITEM(_, HAVE_RLIMIT_DATA_P, (data, RLIMIT_DATA)) \
243 MAYBE_ITEM(_, HAVE_RLIMIT_FSIZE_P, (fsize, RLIMIT_FSIZE)) \
244 MAYBE_ITEM(_, HAVE_RLIMIT_LOCKS_P, (locks, RLIMIT_LOCKS)) \
245 MAYBE_ITEM(_, HAVE_RLIMIT_MEMLOCK_P, (memlock, RLIMIT_MEMLOCK)) \
246 MAYBE_ITEM(_, HAVE_RLIMIT_MSGQUEUE_P, (msgqueue, RLIMIT_MSGQUEUE)) \
247 MAYBE_ITEM(_, HAVE_RLIMIT_NICE_P, (nice, RLIMIT_NICE)) \
248 MAYBE_ITEM(_, HAVE_RLIMIT_NOFILE_P, (nofile, RLIMIT_NOFILE)) \
249 MAYBE_ITEM(_, HAVE_RLIMIT_NPROC_P, (nproc, RLIMIT_NPROC)) \
250 MAYBE_ITEM(_, HAVE_RLIMIT_RSS_P, (rss, RLIMIT_RSS)) \
251 MAYBE_ITEM(_, HAVE_RLIMIT_RTPRIO_P, (rtprio, RLIMIT_RTPRIO)) \
252 MAYBE_ITEM(_, HAVE_RLIMIT_SIGPENDING_P, (sigpending, RLIMIT_SIGPENDING)) \
253 MAYBE_ITEM(_, HAVE_RLIMIT_STACK_P, (stack, RLIMIT_STACK)) \
254 MAYBE_ITEM(_, HAVE_RLIMIT_VMEM_P, (vmem, RLIMIT_VMEM))
257 /* --- The unpleasant conditional-output machinery --- */
259 #define MAYBE_ITEM(_, emitp, args) GLUE(MAYBE_ITEM_, emitp)(_, args)
260 #define MAYBE_ITEM_t(_, args) _ args
261 #define MAYBE_ITEM_nil(_, args)
263 /*----- Main program ------------------------------------------------------*/
265 /* --- The global select state --- */
267 extern sel_state
*sel
;
269 /* --- Help text --- */
271 extern const char grammar_text
[];
272 extern const char option_text
[];
274 /* --- @fw_log@ --- *
276 * Arguments: @time_t t@ = when the connection occurred or (@-1@)
277 * @const char *fmt@ = format string to fill in
278 * @...@ = other arguments
282 * Use: Logs a connection.
285 extern void PRINTF_LIKE(2, 3)
286 fw_log(time_t /*t*/, const char */
*fmt*/
, ...);
288 /* --- @fw_inc@, @fw_dec@ --- *
294 * Use: Increments or decrements the active thing count. `fwd' won't
295 * quit while there are active things.
298 extern void fw_inc(void);
299 extern void fw_dec(void);
301 /*----- Channel management ------------------------------------------------*/
303 /* --- Magic numbers --- */
305 #define CHAN_BUFSZ 4096
307 /* --- Channel structure --- */
309 typedef struct chan
{
310 unsigned base
, len
; /* Base and length of data */
311 unsigned f
; /* Various interesting flags */
312 void (*func
)(void */
*p*/
); /* Function to call on closure */
313 int err
; /* What's wrong with the channel */
314 void *p
; /* Argument to pass function */
315 sel_file r
, w
; /* Reader and writer selectors */
316 char buf
[CHAN_BUFSZ
]; /* The actual data buffer */
319 #define CHANF_CLOSE 1u /* Close channel when buffer empty */
320 #define CHANF_READY 2u /* The channel destination exists */
322 /* --- @chan_close@ --- *
324 * Arguments: @chan *c@ = pointer to channel
328 * Use: Closes down a channel prematurely.
331 extern void chan_close(chan */
*c*/
);
333 /* --- @chan_dest@ --- *
335 * Arguments: @chan *c@ = pointer to channel
336 * @int fd@ = destination file descriptor for channel
340 * Use: Sets the channel's destination so it knows where to put
344 extern void chan_dest(chan */
*c*/
, int /*fd*/);
346 /* --- @chan_open@ --- *
348 * Arguments: @chan *c@ = pointer to channel to open
349 * @int from, to@ = source and destination file descriptors
350 * @void (*func)(void *p)@ = function to call on closure
351 * @void *p@ = argument to pass to function
355 * Use: Opens a channel. Data is copied from the source to the
356 * destination. The @to@ argument may be @-1@ if the file
357 * descriptor isn't known yet.
360 extern void chan_open(chan */
*c*/
, int /*from*/, int /*to*/,
361 void (*/
*func*/
)(void */
*p*/
), void */
*p*/
);
363 /*----- Character scanners ------------------------------------------------*/
365 /* --- A low-level scanner source --- */
367 typedef struct scansrc
{
368 struct scansrc
*next
; /* Next one in the list */
369 struct scansrc_ops
*ops
; /* Pointer to operations table */
370 char *src
; /* Name of this source */
371 int line
; /* Current line number */
372 dstr pushback
; /* Pushback characters */
373 char *tok
; /* Token pushback */
374 unsigned t
; /* Token type pushback */
377 /* --- Scanner source operations --- */
379 typedef struct scansrc_ops
{
380 int (*scan
)(scansrc */
*ss*/
); /* Read another character */
381 void (*destroy
)(scansrc */
*ss*/
); /* Destroy an unwanted source */
384 /* --- A character scanner --- */
386 typedef struct scanner
{
387 scansrc
*head
, **tail
; /* Scanner list head and tail */
388 int t
; /* Token type */
389 dstr d
; /* Current token value */
390 const char *wbegin
, *wcont
; /* Parsing exception strings */
393 /* --- @scan_file@ --- *
395 * Arguments: @FILE *fp@ = pointer to file descriptor
396 * @const char *name@ = pointer to source file name
397 * @unsigned f@ = flags
399 * Returns: A scanner source.
401 * Use: Creates a new scanner source for reading from a file.
404 #define SCF_NOCLOSE 1u /* Don't close @fp@ when finished */
406 extern scansrc
*scan_file(FILE */
*fp*/
, const char */
*name*/
,
409 /* --- @scan_argv@ --- *
411 * Arguments: @char **av@ = pointer to argument array (null terminated)
413 * Returns: A scanner source.
415 * Use: Creates a new scanner source for reading from an @argv@
419 extern scansrc
*scan_argv(char **/
*av*/
);
423 * Arguments: @scanner *sc@ = pointer to main scanner context
425 * Returns: Character read, or end-of-file.
427 * Use: Scans a character from a source of characters.
430 extern int scan(scanner */
*sc*/
);
432 /* --- @unscan@ --- *
434 * Arguments: @scanner *sc@ = pointer to main scanner context
435 * @int ch@ = character to unscan
439 * Use: Scans a character from a source of characters.
442 extern void unscan(scanner */
*sc*/
, int /*ch*/);
444 /* --- @scan_push@ --- *
446 * Arguments: @scanner *sc@ = pointer to main scanner context
447 * @scansrc *ss@ = souorce to push
451 * Use: Pushes a scanner source onto the front of the queue.
454 extern void scan_push(scanner */
*sc*/
, scansrc */
*ss*/
);
456 /* --- @scan_add@ --- *
458 * Arguments: @scanner *sc@ = pointer to main scanner context
459 * @scansrc *ss@ = souorce to push
463 * Use: Adds a scanner source onto the end of the queue.
466 extern void scan_add(scanner */
*sc*/
, scansrc */
*ss*/
);
468 /* --- @scan_create@ --- *
470 * Arguments: @scanner *sc@ = scanner context to initialize
474 * Use: Initializes a scanner block ready for use.
477 extern void scan_create(scanner */
*sc*/
);
479 /* --- @scan_destroy@ --- *
481 * Arguments: @scanner *sc@ = pointer to scanner context
485 * Use: Destroys a scanner and all the sources attached to it.
488 extern void scan_destroy(scanner */
*sc*/
);
490 /*----- Configuration parsing ---------------------------------------------*/
492 /* --- Magical constants --- */
494 #define CTOK_EOF (-1)
495 #define CTOK_WORD 256
497 /* --- @conf_undelim@ --- *
499 * Arguments: @scanner *sc@ = pointer to scanner definition
500 * @const char *d, *dd@ = pointer to characters to escape
504 * Use: Modifies the tokenizer. Characters in the first list will
505 * always be considered to begin a word. Characters in the
506 * second list will always be allowed to continue a word.
509 extern void conf_undelim(scanner */
*sc*/
,
510 const char */
*d*/
, const char */
*dd*/
);
514 * Arguments: @scanner *sc@ = pointer to scanner definition
516 * Returns: Type of token scanned.
518 * Use: Reads the next token from the character scanner.
521 extern int token(scanner */
*sc*/
);
525 * Arguments: @scanner *sc@ = pointer to scanner definition
526 * @const char *msg@ = message skeleton string
527 * @...@ = extra arguments for the skeleton
531 * Use: Reports an error at the current scanner location.
534 extern void PRINTF_LIKE(2, 3) NORETURN
535 error(scanner */
*sc*/
, const char */
*msg*/
, ...);
537 /* --- @pushback@ --- *
539 * Arguments: @scanner *sc@ = pointer to scanner definition
543 * Use: Pushes the current token back. This is normally a precursor
544 * to pushing a new scanner source.
547 extern void pushback(scanner */
*sc*/
);
549 /* --- @conf_enum@ --- *
551 * Arguments: @scanner *sc@ = pointer to a scanner object
552 * @const char *list@ = comma-separated things to allow
553 * @unsigned @f = flags for the search
554 * @const char *err@ = error message if not found
556 * Returns: Index into list, zero-based, or @-1@.
558 * Use: Checks whether the current token is a string which matches
559 * one of the comma-separated items given. The return value is
560 * the index (zero-based) of the matched string in the list.
562 * The flags control the behaviour if no exact match is found.
563 * If @ENUM_ABBREV@ is set, and the current token is a left
564 * substring of exactly one of the possibilities, then that one
565 * is chosen. If @ENUM_NONE@ is set, the value @-1@ is
566 * returned; otherwise an error is reported and the program is
570 #define ENUM_ABBREV 1u
573 extern int conf_enum(scanner */
*sc*/
, const char */
*list*/
,
574 unsigned /*flags*/, const char */
*err*/
);
576 /* --- @conf_prefix@ --- *
578 * Arguments: @scanner *sc@ = pointer to a scanner object
579 * @const char *p@ = pointer to prefix string to check
581 * Returns: Nonzero if the prefix matches.
583 * Use: If the current token is a word matching the given prefix
584 * string, then it and an optional `.' character are removed and
585 * a nonzero result is returned. Otherwise the current token is
586 * left as it is, and zero is returned.
588 * Typical options parsing code would remove an expected prefix,
589 * scan an option anyway (since qualifying prefixes are
590 * optional) and if a match is found, claim the option. If no
591 * match is found, and a prefix was stripped, then an error
592 * should be reported.
595 extern int conf_prefix(scanner */
*sc*/
, const char */
*p*/
);
597 /* --- @CONF_BEGIN@, @CONF_END@ --- *
599 * Arguments: @sc@ = scanner to read from
600 * @prefix@ = prefix to scan for
601 * @desc@ = description of what we're parsing
603 * Use: Bracket an options parsing routine. The current token is
604 * checked to see whether it matches the prefix. If so, it is
605 * removed and the following token examined. If that's a `.'
606 * then it's removed. If it's a `{' then the enclosed
607 * option-parsing code is executed in a loop until a matching
608 * '}' is found. If the options parser doesn't accept an
609 * option, the behaviour is dependent on whether a prefix was
610 * seen: if so, an error is reported; otherwse a zero return is
619 #define CONF_BEGIN(sc, prefix, desc) do { \
620 scanner *_conf_sc = (sc); \
621 const char *_conf_desc = (desc); \
622 int _conf_state = CS_PLAIN; \
624 /* --- Read the initial prefix --- */ \
626 if (_conf_sc->t == CTOK_WORD && \
627 strcmp(_conf_sc->d.buf, (prefix)) == 0) { \
629 _conf_state = CS_PREFIX; \
630 if (_conf_sc->t == '.') \
632 else if (_conf_sc->t == '{') { \
634 _conf_state = CS_BRACE; \
638 /* --- Ensure the next token is a word --- */ \
640 if (_conf_sc->t != CTOK_WORD) \
641 error(_conf_sc, "parse error, expected option keyword"); \
646 /* --- Reject an option --- * \
648 * We could get here as a result of an explicit @CONF_REJECT@ or \
649 * because the option wasn't accepted. \
654 if (_conf_state == CS_PLAIN) \
655 _conf_state = CS_UNKNOWN; \
657 error(_conf_sc, "unknown %s option `%s'", \
658 _conf_desc, _conf_sc->d.buf); \
661 /* --- Accept an option --- * \
663 * It's safe to drop through from above. Either an error will have \
664 * been reported, or the state is not @CS_BRACE@. \
668 if (_conf_state == CS_BRACE && _conf_sc->t == ';') \
670 } while (_conf_state == CS_BRACE && _conf_sc->t == CTOK_WORD); \
672 /* --- Check for a closing brace --- */ \
674 if (_conf_state == CS_BRACE) { \
675 if (_conf_sc->t == '}') \
678 error(_conf_sc, "parse error, expected `}'"); \
681 /* --- Return an appropriate value --- */ \
683 return (_conf_state != CS_UNKNOWN); \
686 /* --- @CONF_ACCEPT@, @CONF_REJECT@ --- *
690 * Use: Within an options parser (between @CONF_BEGIN@ and
691 * @CONF_END@), accept or reject an option.
694 #define CONF_ACCEPT goto _conf_accept
695 #define CONF_REJECT goto _conf_reject
697 /* --- @CONF_QUAL@ --- *
701 * Use: Evaluates to a nonzero value if the current option is
702 * qualified. This can be used to decide whether abbreviations
703 * for options should be accepted.
706 #define CONF_QUAL (_conf_state != CS_PLAIN)
708 /* --- @conf_name@ --- *
710 * Arguments: @scanner *sc@ = pointer to scanner
711 * @char delim@ = delimiter character to look for
712 * @dstr *d@ = pointer to dynamic string for output
716 * Use: Reads in a compound name consisting of words separated by
717 * delimiters. Leading and trailing delimiters are permitted,
718 * although they'll probably cause confusion if used. The name
719 * may be enclosed in square brackets if that helps at all.
721 * Examples of compound names are filenames (delimited by `/')
722 * and IP addresses (delimited by `.').
725 extern void conf_name(scanner */
*sc*/
, char /*delim*/, dstr */
*d*/
);
727 /* --- @conf_fname@ --- *
729 * Arguments: @scanner *sc@ = pointer to scanner
730 * @dstr *d@ = pointer to dynamic string for output
734 * Use: Reads a file name from the input and stores it in @d@.
737 extern void conf_fname(scanner */
*sc*/
, dstr */
*d*/
);
739 /*----- Reference-counted file descriptors --------------------------------*/
741 typedef struct reffd
{
744 void (*proc
)(void */
*p*/
);
748 /* --- @reffd_init@ --- *
750 * Arguments: @int fd@ = file descriptor
752 * Returns: Reference-counted file descriptor object.
754 * Use: Creates a refcounted file descriptor.
757 extern reffd
*reffd_init(int /*fd*/);
759 /* --- @reffd_handler@ --- *
761 * Arguments: @reffd *r@ = pointer to reference counted filehandle
762 * @void (*proc)(void *p)@ = procedure to call
767 * Use: Sets the reference counted file descriptor to call @proc@
768 * when it is no longer required.
771 extern void reffd_handler(reffd */
*r*/
, void (*/
*proc*/
)(void */
*p*/
),
774 /* --- @reffd_inc@ --- *
776 * Arguments: @reffd *r@ = pointer to reference counted filehandle
780 * Use: Increments the reference count for a file descriptor.
783 #define REFFD_INC(r) do { (r)->ref++; } while (0)
785 extern void reffd_inc(reffd */
*r*/
);
787 /* --- @reffd_dec@ --- *
789 * Arguments: @reffd *r@ = pointer to reference counted filehandle
793 * Use: Decrements the reference count for a file descriptor.
796 #define REFFD_DEC(r) do { \
799 if (_r->ref == 0) { \
807 extern void reffd_dec(reffd */
*r*/
);
809 /*----- Sources, targets and endpoints ------------------------------------*/
811 /* --- Basic endpoint structure --- */
813 typedef struct endpt
{
814 struct endpt_ops
*ops
; /* Pointer to operations table */
815 struct endpt
*other
; /* Pointer to sibling endpoint */
816 unsigned f
; /* Various flags */
817 struct tango
*t
; /* Private data structure */
818 reffd
*in
, *out
; /* File descriptors */
821 /* --- Endpoint flags --- */
823 #define EPF_PENDING 1u /* Endpoint creation in progress */
824 #define EPF_FILE 2u /* Endpoint smells like a file */
826 /* --- Endpoint operations table --- */
828 typedef struct endpt_ops
{
830 /* --- @attach@ --- *
832 * Arguments: @endpt *e@ = pointer to endpoint to be attached
833 * @reffd *in, *out@ = input and output file descriptors
837 * Use: Instructs a non-file endpoint to attach itself to a pair of
841 void (*attach
)(endpt */
*e*/
, reffd */
*in*/
, reffd */
*out*/
);
845 * Arguments: @endpt *e@ = pointer to endpoint in question
846 * @endpt *f@ = pointer to a file endpoint
850 * Use: Informs a non-file endpoint of a file endpoint which will
851 * want to be closed when it's finished with. At that time, the
852 * endpoint should arrange to have both itself and its partner
853 * closed. If no file is registered, the endpoint manager will
854 * close both endpoints itself.
857 void (*file
)(endpt */
*e*/
, endpt */
*f*/
);
859 /* --- @wclose@ --- *
861 * Arguments: @endpt *e@ = endpoint to be partially closed
865 * Use: Announces that the endpoint will not be written to any more.
868 void (*wclose
)(endpt */
*e*/
);
872 * Arguments: @endpt *e@ = endpoint to be closed
876 * Use: Completely closes an endpoint. The endpoint's data may be
877 * freed, although some endpoints may wish to delay freeing for
881 void (*close
)(endpt */
*e*/
);
885 /* --- A basic target object --- */
887 typedef struct target
{
888 struct target_ops
*ops
;
892 /* --- Forwarding target operations --- */
894 typedef struct target_ops
{
895 const char *name
; /* Name of this target */
897 /* --- @option@ --- *
899 * Arguments: @target *t@ = pointer to target object, or zero if global
900 * @scanner *sc@ = scanner to read from
902 * Returns: Nonzero to claim the option.
904 * Use: Handles an option string from the configuration file.
907 int (*option
)(target */
*t*/
, scanner */
*sc*/
);
911 * Arguments: @scanner *sc@ = pointer to scanner to read from
913 * Returns: Pointer to a target object to claim, null to reject.
915 * Use: Parses a target description from the configuration file.
916 * Only the socket target is allowed to omit the prefix on a
917 * target specification.
920 target
*(*read
)(scanner */
*sc*/
);
922 /* --- @confirm@ --- *
924 * Arguments: @target *t@ = pointer to target
928 * Use: Confirms configuration of a target.
931 void (*confirm
)(target */
*t*/
);
933 /* --- @create@ --- *
935 * Arguments: @target *t@ = pointer to target
936 * @const char *desc@ = description of connection
938 * Returns: Pointer to a created endpoint.
940 * Use: Generates a target endpoint for communication.
943 endpt
*(*create
)(target */
*t*/
, const char */
*desc*/
);
945 /* --- @destroy@ --- *
947 * Arguments: @target *t@ = pointer to target
951 * Use: Destroys a target.
954 void (*destroy
)(target */
*t*/
);
958 /* --- A basic source object --- */
960 typedef struct source
{
961 struct source
*next
, *prev
;
962 struct source_ops
*ops
;
966 /* --- Forwarding source operations --- */
968 typedef struct source_ops
{
969 const char *name
; /* Name of this source */
971 /* --- @option@ --- *
973 * Arguments: @scanner *sc@ = scanner to read from
974 * @source *s@ = pointer to source object, or zero if global
976 * Returns: Nonzero to claim the option.
978 * Use: Handles an option string from the configuration file.
981 int (*option
)(source */
*s*/
, scanner */
*sc*/
);
985 * Arguments: @scanner *sc@ = pointer to scanner to read from
987 * Returns: Pointer to a source object to claim, null to reject.
989 * Use: Parses a source description from the configuration file.
990 * Only the socket source is allowed to omit the prefix on a
991 * source specification.
994 source
*(*read
)(scanner */
*sc*/
);
996 /* --- @attach@ --- *
998 * Arguments: @source *s@ = pointer to source
999 * @scanner *sc@ = scanner (for error reporting)
1000 * @target *t@ = pointer to target to attach
1004 * Use: Attaches a target to a source.
1007 void (*attach
)(source */
*s*/
, scanner */
*sc*/
, target */
*t*/
);
1009 /* --- @destroy@ --- *
1011 * Arguments: @source *s@ = pointer to source
1015 * Use: Destroys a source. Used when closing the system down, for
1016 * example as a result of a signal.
1019 void (*destroy
)(source */
*s*/
);
1023 /* --- @endpt_kill@ --- *
1025 * Arguments: @endpt *a@ = an endpoint
1029 * Use: Kills an endpoint. If the endpoint is joined to another, the
1030 * other endpoint is also killed, as is the connection between
1031 * them (and that's the tricky bit).
1034 extern void endpt_kill(endpt */
*a*/
);
1036 /* --- @endpt_killall@ --- *
1042 * Use: Destroys all current endpoint connections. Used when
1046 extern void endpt_killall(void);
1048 /* --- @endpt_join@ --- *
1050 * Arguments: @endpt *a@ = pointer to first endpoint
1051 * @endpt *b@ = pointer to second endpoint
1052 * @const char *desc@ = description of connection
1056 * Use: Joins two endpoints together. It's OK to join endpoints
1057 * which are already joined; in fact, the the right thing to do
1058 * when your endpoint decides that it's not pending any more is
1059 * to join it to its partner again.
1061 * If the endpoints are already connected then the description
1062 * string is ignored. The endpoint manager takes a copy of
1063 * the string, so you don't need to keep it around.
1066 extern void endpt_join(endpt */
*a*/
, endpt */
*b*/
, const char */
*desc*/
);
1068 /* --- @source_add@ --- *
1070 * Arguments: @source *s@ = pointer to a source
1074 * Use: Adds a source to the master list. Only do this for passive
1075 * sources (e.g., listening sockets), not active sources (e.g.,
1076 * executable programs).
1079 extern void source_add(source */
*s*/
);
1081 /* --- @source_remove@ --- *
1083 * Arguments: @source *s@ = pointer to a source
1087 * Use: Removes a source from the master list.
1090 extern void source_remove(source */
*s*/
);
1092 /* --- @source_killall@ --- *
1098 * Use: Frees all sources.
1101 extern void source_killall(void);
1103 /*----- The exec source and target ----------------------------------------*/
1105 extern source_ops xsource_ops
;
1106 extern target_ops xtarget_ops
;
1108 /* --- @exec_init@ --- *
1114 * Use: Initializes the executable problem source and target.
1117 extern void exec_init(void);
1119 /*----- The file source and target ----------------------------------------*/
1121 extern source_ops fsource_ops
;
1122 extern target_ops ftarget_ops
;
1124 /*----- The socket source and target --------------------------------------*/
1126 extern source_ops ssource_ops
;
1127 extern target_ops starget_ops
;
1129 /* --- @starget_connected@ --- *
1131 * Arguments: @int fd@ = file descriptor now ready for use
1132 * @void *p@ = pointer to an endpoint structure
1136 * Use: Handles successful connection of the target endpoint.
1139 extern void starget_connected(int /*fd*/, void */
*p*/
);
1141 /*----- Handling of file attributes ---------------------------------------*/
1143 /* --- File attribute options structure --- */
1145 typedef struct fattr
{
1151 /* --- Shared global options --- */
1153 extern fattr fattr_global
;
1155 /* --- @fattr_init@ --- *
1157 * Arguments: @fattr *f@ = pointer to file attributes
1161 * Use: Initializes a set of file attributes to default values.
1164 extern void fattr_init(fattr */
*f*/
);
1166 /* --- @fattr_option@ --- *
1168 * Arguments: @scanner *sc@ = pointer to scanner to read
1169 * @fattr *f@ = pointer to file attributes to set
1171 * Returns: Whether the option was clamed.
1173 * Use: Reads file attributes from a scanner.
1176 extern int fattr_option(scanner */
*sc*/
, fattr */
*f*/
);
1178 /* --- @fattr_apply@ --- *
1180 * Arguments: @const char *file@ = pointer to filename
1181 * @fattr *f@ = pointer to attribute set
1183 * Returns: @-1@ if it failed.
1185 * Use: Applies file attributes to a file. For best results, try to
1186 * create the file with the right permissions and so on. This
1187 * call will fix everything up, but there are potential races
1188 * which might catch you out if you're not careful.
1191 extern int fattr_apply(const char */
*file*/
, fattr */
*f*/
);
1193 /*----- Making privileged connections -------------------------------------*/
1195 /* --- @privconn_split@ --- *
1197 * Arguments: @sel_state *s@ = select state
1201 * Use: Splits off the privileged binding code into a separate
1205 extern void privconn_split(sel_state */
*s*/
);
1207 /* --- @privconn_adddest@ --- *
1209 * Arguments: @struct in_addr peer@ = address to connect to
1210 * @unsigned port@ = port to connect to
1212 * Returns: Index for this destination address, or @-1@ if not
1215 * Use: Adds a valid destination for a privileged connection.
1218 extern int privconn_adddest(struct in_addr
/*peer*/, unsigned /*port*/);
1220 /* --- @privconn_connect@ --- *
1222 * Arguments: @conn *c@ = connection structure to fill in
1223 * @sel_state *s@ = pointer to select state to attach to
1224 * @int i@ = address index to connect to
1225 * @struct in_addr bind@ = address to bind to
1226 * @void (*func)(int, void *)@ = function to call on connect
1227 * @void *p@ = argument for the function
1229 * Returns: Zero on success, @-1@ on failure.
1231 * Use: Sets up a privileged connection job.
1234 extern int privconn_connect(conn */
*c*/
, sel_state */
*s*/
,
1235 int /*i*/, struct in_addr
/*bind*/,
1236 void (*/
*func*/
)(int, void *), void */
*p*/
);
1238 /*----- Identifying remote clients ----------------------------------------*/
1240 typedef struct id_req
{
1241 struct sockaddr_in lsin
; /* Local address of connection */
1242 struct sockaddr_in rsin
; /* Remote address of connection */
1243 const char *desc
; /* Description of connection */
1244 const char *act
; /* Action taken by server */
1245 reffd
*r
; /* Pointer to file descriptor */
1248 /* --- @identify@ --- *
1250 * Arguments: @const id_req *q@ = pointer to request block
1254 * Use: Starts a background ident lookup and reverse-resolve job
1255 * which will, eventually, report a message to the system log.
1258 extern void identify(const id_req */
*q*/
);
1260 /*----- Host-based access control -----------------------------------------*/
1262 /* --- An access control entry --- */
1264 typedef struct acl_entry
{
1265 struct acl_entry
*next
; /* Next entry in the list */
1266 const struct acl_ops
*ops
; /* Operations for the ACL entry */
1267 unsigned act
; /* What to do with matching hosts */
1270 #define ACL_DENY 0 /* Deny access to matching conns */
1271 #define ACL_ALLOW 1 /* Allow access to matching conns */
1272 #define ACL_PERM 1u /* Bit mask for permission bit */
1274 /* --- Host-based access control --- */
1276 typedef struct acl_host
{
1277 acl_entry a
; /* Base structure */
1278 struct in_addr addr
, mask
; /* Address and netmask */
1281 /* --- ACL methods --- */
1283 typedef struct acl_ops
{
1284 int (*check
)(void */
*a*/
, struct in_addr
/*addr*/, unsigned /*port*/);
1285 void (*dump
)(void */
*a*/
, FILE */
*fp*/
);
1286 void (*free
)(void */
*a*/
);
1289 /* --- @acl_check@ --- *
1291 * Arguments: @acl_entry *a@ = pointer to ACL to check against
1292 * @struct in_addr addr@ = address to check
1293 * @unsigned port@ = port number to check
1294 * @int *act@ = verdict (should initially be @ACT_ALLOW@)
1296 * Returns: Zero if undecided, nonzero if a rule matched.
1298 * Use: Checks an address against an ACL.
1301 extern int acl_check(acl_entry */
*a*/
,
1302 struct in_addr
/*addr*/, unsigned /*port*/,
1305 /* --- @acl_dump@ --- *
1307 * Arguments: @acl_entry *a@ = pointer to ACL to dump
1308 * @FILE *fp@ = pointer to stream to dump on
1312 * Use: Dumps an access control list to an output stream.
1315 extern void acl_dump(acl_entry */
*a*/
, FILE */
*fp*/
);
1317 /* --- @acl_free@ --- *
1319 * Arguments: @acl_entry *a@ = pointer to a list of ACLs
1323 * Use: Frees all of the memory used by an ACL.
1326 extern void acl_free(acl_entry */
*a*/
);
1328 /* --- @acl_addhost@ --- *
1330 * Arguments: @acl_entry ***a@ = address of pointer to list tail
1331 * @unsigned act@ = what to do with matching addresses
1332 * @struct in_addr addr, mask@ = address and mask to match
1336 * Use: Adds a host-authentication entry to the end of an access
1340 extern void acl_addhost(acl_entry
***/
*a*/
, unsigned /*act*/,
1341 struct in_addr
/*addr*/, struct in_addr
/*mask*/);
1343 /* --- @acl_addpriv@ --- *
1345 * Arguments: @acl_entry ***a@ = address of pointer to list tail
1346 * @unsigned act@ = what to do with matching addresses
1350 * Use: Adds a privileged-port check to the end of an access control
1354 extern void acl_addpriv(acl_entry
***/
*a*/
, unsigned /*act*/);
1356 /*----- Network addresses -------------------------------------------------*/
1358 /* --- A generic socket address --- *
1360 * Not all systems understand @sa_len@ fields. (In particular, Linux
1361 * doesn't.) Some fairly ugly hacking is then performed on particular
1365 typedef struct addr
{
1366 struct addr_ops
*ops
;
1370 #define ADDRSZ(sz) (sizeof(addr) + (sz))
1372 /* --- Address configuration --- *
1374 * An address family will want to extend this.
1377 typedef struct addr_opts
{
1381 #define ADDRF_NOLOG 1u
1383 /* --- Address types --- *
1385 * For things like Internet addresses, source and destinations look
1395 /* --- Description of an address type handler --- */
1397 typedef struct addr_ops
{
1398 const char *name
; /* Protocol's internal name */
1402 * Arguments: @scanner *sc@ = pointer to scanner to read from
1403 * @unsigned type@ = type of address to be read
1405 * Returns: A filled-in socket address.
1407 * Use: Parses a textual representation of a socket address.
1410 addr
*(*read
)(scanner */
*sc*/
, unsigned /*type*/);
1412 /* --- @destroy@ --- *
1414 * Arguments: @addr *a@ = pointer to an address block
1418 * Use: Disposes of an address block in some suitable fashion.
1421 void (*destroy
)(addr */
*a*/
);
1423 /* --- @print@ --- *
1425 * Arguments: @addr *a@ = pointer to socket address to read
1426 * @unsigned type@ = type of address to be written
1427 * @dstr *d@ = string on which to write the description
1431 * Use: Writes a textual representation of a socket address to
1435 void (*print
)(addr */
*a*/
, unsigned /*type*/, dstr */
*d*/
);
1437 /* --- @initsrcopts@ --- *
1441 * Returns: A pointer to a protocol-specific data block for a listener
1443 * Use: Creates a data block for a listener. This is attached to the
1444 * listener data structure. Options can then be requested, and
1445 * are added to the block when necessary.
1448 addr_opts
*(*initsrcopts
)(void);
1450 /* --- @option@ --- *
1452 * Arguments: @scanner *sc@ = pointer to a scanner to read from
1453 * @unsigned type@ = kind of option this is
1454 * @addr_opts *ao@ = data block to modify (from @init@), or null
1456 * Returns: Nonzero to claim the option.
1458 * Use: Parses a source option, either global or listener-specific.
1461 int (*option
)(scanner */
*sc*/
, addr_opts */
*ao*/
, unsigned /*type*/);
1463 /* --- @confirm@ --- *
1465 * Arguments: @addr *a@ = pointer to an address structure
1466 * @unsigned type@ = kind of address this is
1467 * @addr_opts *ao@ = address options
1471 * Use: Called during initialization when an address is fully
1475 void (*confirm
)(addr */
*a*/
, unsigned /*type*/, addr_opts */
*ao*/
);
1477 /* --- @freesrcopts@ --- *
1479 * Arguments: @addr_opts *ao@ = data block to remove
1483 * Use: Throws away all the configuration data for an address type.
1486 void (*freesrcopts
)(addr_opts */
*ao*/
);
1490 * Arguments: @addr *a@ = the address to bind to
1491 * @addr_opts *ao@ = the address options
1493 * Returns: File descriptor of bound socket if OK, or @-1@ on error.
1495 * Use: Binds a listening socket. The tedious stuff with @listen@
1499 int (*bind
)(addr */
*a*/
, addr_opts */
*ao*/
);
1501 /* --- @unbind@ --- *
1503 * Arguments: @addr *a@ = pointer to an address
1507 * Use: Unbinds an address. This is used when tidying up. The main
1508 * purpose is to let the Unix-domain handler remove its socket
1509 * node from the filesystem.
1512 void (*unbind
)(addr */
*a*/
);
1514 /* --- @accept@ --- *
1516 * Arguments: @int fd@ = listening file descriptor
1517 * @addr_opts *ao@ = data block to get configuration from
1518 * @const char *desc@ = description of the listener
1520 * Returns: Pointer to a reference counted file descriptor.
1522 * Use: Accepts, verifies and logs an incoming connection.
1525 reffd
*(*accept
)(int /*fd*/, addr_opts */
*ao*/
, const char */
*desc*/
);
1527 /* --- @inittargopts@ --- *
1531 * Returns: A pointer to a protocol-specific data block for a connecter
1533 * Use: Creates a data block for a target. This is attached to the
1534 * target data structure. Options can then be requested, and
1535 * are added to the block when necessary.
1538 addr_opts
*(*inittargopts
)(void);
1540 /* --- @freetargopts@ --- *
1542 * Arguments: @addr_opts *ao@ = data block to remove
1546 * Use: Throws away all the configuration data for an address type.
1549 void (*freetargopts
)(addr_opts */
*ao*/
);
1551 /* --- @connect@ --- *
1553 * Arguments: @addr *a@ = destination address
1554 * @addr_opts *ao@ = target address options
1555 * @conn *c@ = connection structure
1556 * @endpt *e@ = endpoint structure
1558 * Returns: Zero if OK, @-1@ on some error.
1560 * Use: Requests that a connection be made, or at least set in
1561 * motion. An address may do one of these things:
1565 * * Call @starget_connected@ with @-1@ or a connected file
1566 * descriptor and the pointer @e@.
1568 * * Call @conn_init@ or @conn_fd@, giving @starget_connected@
1569 * and @e@ as the function to call.
1572 int (*connect
)(addr */
*a*/
, addr_opts */
*ao*/
, conn */
*c*/
, endpt */
*e*/
);
1576 /* --- Address types --- */
1578 extern addr_ops un_ops
;
1579 extern addr_ops inet_ops
;
1581 /*----- That's all, folks -------------------------------------------------*/