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/mdwopt.h>
91 #include <mLib/quis.h>
92 #include <mLib/report.h>
94 #include <mLib/selbuf.h>
101 /*----- Other subtleties --------------------------------------------------*/
103 #if defined(HAVE_DECL_ENVIRON) && !HAVE_DECL_ENVIRON
104 extern char **environ
;
107 /*----- Main program ------------------------------------------------------*/
109 /* --- The global select state --- */
111 extern sel_state
*sel
;
113 /* --- Help text --- */
115 extern const char grammar_text
[];
116 extern const char option_text
[];
118 /* --- @fw_log@ --- *
120 * Arguments: @time_t t@ = when the connection occurred or (@-1@)
121 * @const char *fmt@ = format string to fill in
122 * @...@ = other arguments
126 * Use: Logs a connection.
129 extern void fw_log(time_t /*t*/, const char */
*fmt*/
, ...);
131 /* --- @fw_inc@, @fw_dec@ --- *
137 * Use: Increments or decrements the active thing count. `fwd' won't
138 * quit while there are active things.
141 extern void fw_inc(void);
142 extern void fw_dec(void);
144 /*----- Channel management ------------------------------------------------*/
146 /* --- Magic numbers --- */
148 #define CHAN_BUFSZ 4096
150 /* --- Channel structure --- */
152 typedef struct chan
{
153 unsigned base
, len
; /* Base and length of data */
154 unsigned f
; /* Various interesting flags */
155 void (*func
)(void */
*p*/
); /* Function to call on closure */
156 void *p
; /* Argument to pass function */
157 sel_file r
, w
; /* Reader and writer selectors */
158 char buf
[CHAN_BUFSZ
]; /* The actual data buffer */
161 #define CHANF_CLOSE 1u /* Close channel when buffer empty */
162 #define CHANF_READY 2u /* The channel destination exists */
164 /* --- @chan_close@ --- *
166 * Arguments: @chan *c@ = pointer to channel
170 * Use: Closes down a channel prematurely.
173 extern void chan_close(chan */
*c*/
);
175 /* --- @chan_dest@ --- *
177 * Arguments: @chan *c@ = pointer to channel
178 * @int fd@ = destination file descriptor for channel
182 * Use: Sets the channel's destination so it knows where to put
186 extern void chan_dest(chan */
*c*/
, int /*fd*/);
188 /* --- @chan_open@ --- *
190 * Arguments: @chan *c@ = pointer to channel to open
191 * @int from, to@ = source and destination file descriptors
192 * @void (*func)(void *p)@ = function to call on closure
193 * @void *p@ = argument to pass to function
197 * Use: Opens a channel. Data is copied from the source to the
198 * destination. The @to@ argument may be @-1@ if the file
199 * descriptor isn't known yet.
202 extern void chan_open(chan */
*c*/
, int /*from*/, int /*to*/,
203 void (*/
*func*/
)(void */
*p*/
), void */
*p*/
);
205 /*----- Character scanners ------------------------------------------------*/
207 /* --- A low-level scanner source --- */
209 typedef struct scansrc
{
210 struct scansrc
*next
; /* Next one in the list */
211 struct scansrc_ops
*ops
; /* Pointer to operations table */
212 char *src
; /* Name of this source */
213 int line
; /* Current line number */
214 dstr pushback
; /* Pushback characters */
215 char *tok
; /* Token pushback */
216 unsigned t
; /* Token type pushback */
219 /* --- Scanner source operations --- */
221 typedef struct scansrc_ops
{
222 int (*scan
)(scansrc */
*ss*/
); /* Read another character */
223 void (*destroy
)(scansrc */
*ss*/
); /* Destroy an unwanted source */
226 /* --- A character scanner --- */
228 typedef struct scanner
{
229 scansrc
*head
, **tail
; /* Scanner list head and tail */
230 int t
; /* Token type */
231 dstr d
; /* Current token value */
232 const char *wbegin
, *wcont
; /* Parsing exception strings */
235 /* --- @scan_file@ --- *
237 * Arguments: @FILE *fp@ = pointer to file descriptor
238 * @const char *name@ = pointer to source file name
239 * @unsigned f@ = flags
241 * Returns: A scanner source.
243 * Use: Creates a new scanner source for reading from a file.
246 #define SCF_NOCLOSE 1u /* Don't close @fp@ when finished */
248 extern scansrc
*scan_file(FILE */
*fp*/
, const char */
*name*/
,
251 /* --- @scan_argv@ --- *
253 * Arguments: @char **av@ = pointer to argument array (null terminated)
255 * Returns: A scanner source.
257 * Use: Creates a new scanner source for reading from an @argv@
261 extern scansrc
*scan_argv(char **/
*av*/
);
265 * Arguments: @scanner *sc@ = pointer to main scanner context
267 * Returns: Character read, or end-of-file.
269 * Use: Scans a character from a source of characters.
272 extern int scan(scanner */
*sc*/
);
274 /* --- @unscan@ --- *
276 * Arguments: @scanner *sc@ = pointer to main scanner context
277 * @int ch@ = character to unscan
281 * Use: Scans a character from a source of characters.
284 extern void unscan(scanner */
*sc*/
, int /*ch*/);
286 /* --- @scan_push@ --- *
288 * Arguments: @scanner *sc@ = pointer to main scanner context
289 * @scansrc *ss@ = souorce to push
293 * Use: Pushes a scanner source onto the front of the queue.
296 extern void scan_push(scanner */
*sc*/
, scansrc */
*ss*/
);
298 /* --- @scan_add@ --- *
300 * Arguments: @scanner *sc@ = pointer to main scanner context
301 * @scansrc *ss@ = souorce to push
305 * Use: Adds a scanner source onto the end of the queue.
308 extern void scan_add(scanner */
*sc*/
, scansrc */
*ss*/
);
310 /* --- @scan_create@ --- *
312 * Arguments: @scanner *sc@ = scanner context to initialize
316 * Use: Initializes a scanner block ready for use.
319 extern void scan_create(scanner */
*sc*/
);
321 /* --- @scan_destroy@ --- *
323 * Arguments: @scanner *sc@ = pointer to scanner context
327 * Use: Destroys a scanner and all the sources attached to it.
330 extern void scan_destroy(scanner */
*sc*/
);
332 /*----- Configuration parsing ---------------------------------------------*/
334 /* --- Magical constants --- */
336 #define CTOK_EOF (-1)
337 #define CTOK_WORD 256
339 /* --- @conf_undelim@ --- *
341 * Arguments: @scanner *sc@ = pointer to scanner definition
342 * @const char *d, *dd@ = pointer to characters to escape
346 * Use: Modifies the tokenizer. Characters in the first list will
347 * always be considered to begin a word. Characters in the
348 * second list will always be allowed to continue a word.
351 extern void conf_undelim(scanner */
*sc*/
,
352 const char */
*d*/
, const char */
*dd*/
);
356 * Arguments: @scanner *sc@ = pointer to scanner definition
358 * Returns: Type of token scanned.
360 * Use: Reads the next token from the character scanner.
363 extern int token(scanner */
*sc*/
);
367 * Arguments: @scanner *sc@ = pointer to scanner definition
368 * @const char *msg@ = message skeleton string
369 * @...@ = extra arguments for the skeleton
373 * Use: Reports an error at the current scanner location.
376 extern void error(scanner */
*sc*/
, const char */
*msg*/
, ...);
378 /* --- @pushback@ --- *
380 * Arguments: @scanner *sc@ = pointer to scanner definition
384 * Use: Pushes the current token back. This is normally a precursor
385 * to pushing a new scanner source.
388 extern void pushback(scanner */
*sc*/
);
390 /* --- @conf_enum@ --- *
392 * Arguments: @scanner *sc@ = pointer to a scanner object
393 * @const char *list@ = comma-separated things to allow
394 * @unsigned @f = flags for the search
395 * @const char *err@ = error message if not found
397 * Returns: Index into list, zero-based, or @-1@.
399 * Use: Checks whether the current token is a string which matches
400 * one of the comma-separated items given. The return value is
401 * the index (zero-based) of the matched string in the list.
403 * The flags control the behaviour if no exact match is found.
404 * If @ENUM_ABBREV@ is set, and the current token is a left
405 * substring of exactly one of the possibilities, then that one
406 * is chosen. If @ENUM_NONE@ is set, the value @-1@ is
407 * returned; otherwise an error is reported and the program is
411 #define ENUM_ABBREV 1u
414 extern int conf_enum(scanner */
*sc*/
, const char */
*list*/
,
415 unsigned /*flags*/, const char */
*err*/
);
417 /* --- @conf_prefix@ --- *
419 * Arguments: @scanner *sc@ = pointer to a scanner object
420 * @const char *p@ = pointer to prefix string to check
422 * Returns: Nonzero if the prefix matches.
424 * Use: If the current token is a word matching the given prefix
425 * string, then it and an optional `.' character are removed and
426 * a nonzero result is returned. Otherwise the current token is
427 * left as it is, and zero is returned.
429 * Typical options parsing code would remove an expected prefix,
430 * scan an option anyway (since qualifying prefixes are
431 * optional) and if a match is found, claim the option. If no
432 * match is found, and a prefix was stripped, then an error
433 * should be reported.
436 extern int conf_prefix(scanner */
*sc*/
, const char */
*p*/
);
438 /* --- @CONF_BEGIN@, @CONF_END@ --- *
440 * Arguments: @sc@ = scanner to read from
441 * @prefix@ = prefix to scan for
442 * @desc@ = description of what we're parsing
444 * Use: Bracket an options parsing routine. The current token is
445 * checked to see whether it matches the prefix. If so, it is
446 * removed and the following token examined. If that's a `.'
447 * then it's removed. If it's a `{' then the enclosed
448 * option-parsing code is executed in a loop until a matching
449 * '}' is found. If the options parser doesn't accept an
450 * option, the behaviour is dependent on whether a prefix was
451 * seen: if so, an error is reported; otherwse a zero return is
460 #define CONF_BEGIN(sc, prefix, desc) do { \
461 scanner *_conf_sc = (sc); \
462 const char *_conf_desc = (desc); \
463 int _conf_state = CS_PLAIN; \
465 /* --- Read the initial prefix --- */ \
467 if (_conf_sc->t == CTOK_WORD && \
468 strcmp(_conf_sc->d.buf, (prefix)) == 0) { \
470 _conf_state = CS_PREFIX; \
471 if (_conf_sc->t == '.') \
473 else if (_conf_sc->t == '{') { \
475 _conf_state = CS_BRACE; \
479 /* --- Ensure the next token is a word --- */ \
481 if (_conf_sc->t != CTOK_WORD) \
482 error(_conf_sc, "parse error, expected option keyword"); \
487 /* --- Reject an option --- * \
489 * We could get here as a result of an explicit @CONF_REJECT@ or \
490 * because the option wasn't accepted. \
495 if (_conf_state == CS_PLAIN) \
496 _conf_state = CS_UNKNOWN; \
498 error(_conf_sc, "unknown %s option `%s'", \
499 _conf_desc, _conf_sc->d.buf); \
502 /* --- Accept an option --- * \
504 * It's safe to drop through from above. Either an error will have \
505 * been reported, or the state is not @CS_BRACE@. \
509 if (_conf_state == CS_BRACE && _conf_sc->t == ';') \
511 } while (_conf_state == CS_BRACE && _conf_sc->t == CTOK_WORD); \
513 /* --- Check for a closing brace --- */ \
515 if (_conf_state == CS_BRACE) { \
516 if (_conf_sc->t == '}') \
519 error(_conf_sc, "parse error, expected `}'"); \
522 /* --- Return an appropriate value --- */ \
524 return (_conf_state != CS_UNKNOWN); \
527 /* --- @CONF_ACCEPT@, @CONF_REJECT@ --- *
531 * Use: Within an options parser (between @CONF_BEGIN@ and
532 * @CONF_END@), accept or reject an option.
535 #define CONF_ACCEPT goto _conf_accept
536 #define CONF_REJECT goto _conf_reject
538 /* --- @CONF_QUAL@ --- *
542 * Use: Evaluates to a nonzero value if the current option is
543 * qualified. This can be used to decide whether abbreviations
544 * for options should be accepted.
547 #define CONF_QUAL (_conf_state != CS_PLAIN)
549 /* --- @conf_name@ --- *
551 * Arguments: @scanner *sc@ = pointer to scanner
552 * @char delim@ = delimiter character to look for
553 * @dstr *d@ = pointer to dynamic string for output
557 * Use: Reads in a compound name consisting of words separated by
558 * delimiters. Leading and trailing delimiters are permitted,
559 * although they'll probably cause confusion if used. The name
560 * may be enclosed in square brackets if that helps at all.
562 * Examples of compound names are filenames (delimited by `/')
563 * and IP addresses (delimited by `.').
566 extern void conf_name(scanner */
*sc*/
, char /*delim*/, dstr */
*d*/
);
568 /* --- @conf_fname@ --- *
570 * Arguments: @scanner *sc@ = pointer to scanner
571 * @dstr *d@ = pointer to dynamic string for output
575 * Use: Reads a file name from the input and stores it in @d@.
578 extern void conf_fname(scanner */
*sc*/
, dstr */
*d*/
);
580 /*----- Reference-counted file descriptors --------------------------------*/
582 typedef struct reffd
{
585 void (*proc
)(void */
*p*/
);
589 /* --- @reffd_init@ --- *
591 * Arguments: @int fd@ = file descriptor
593 * Returns: Reference-counted file descriptor object.
595 * Use: Creates a refcounted file descriptor.
598 extern reffd
*reffd_init(int /*fd*/);
600 /* --- @reffd_handler@ --- *
602 * Arguments: @reffd *r@ = pointer to reference counted filehandle
603 * @void (*proc)(void *p)@ = procedure to call
608 * Use: Sets the reference counted file descriptor to call @proc@
609 * when it is no longer required.
612 extern void reffd_handler(reffd */
*r*/
, void (*/
*proc*/
)(void */
*p*/
),
615 /* --- @reffd_inc@ --- *
617 * Arguments: @reffd *r@ = pointer to reference counted filehandle
621 * Use: Increments the reference count for a file descriptor.
624 #define REFFD_INC(r) do { (r)->ref++; } while (0)
626 extern void reffd_inc(reffd */
*r*/
);
628 /* --- @reffd_dec@ --- *
630 * Arguments: @reffd *r@ = pointer to reference counted filehandle
634 * Use: Decrements the reference count for a file descriptor.
637 #define REFFD_DEC(r) do { \
640 if (_r->ref == 0) { \
648 extern void reffd_dec(reffd */
*r*/
);
650 /*----- Sources, targets and endpoints ------------------------------------*/
652 /* --- Basic endpoint structure --- */
654 typedef struct endpt
{
655 struct endpt_ops
*ops
; /* Pointer to operations table */
656 struct endpt
*other
; /* Pointer to sibling endpoint */
657 unsigned f
; /* Various flags */
658 struct tango
*t
; /* Private data structure */
659 reffd
*in
, *out
; /* File descriptors */
662 /* --- Endpoint flags --- */
664 #define EPF_PENDING 1u /* Endpoint creation in progress */
665 #define EPF_FILE 2u /* Endpoint smells like a file */
667 /* --- Endpoint operations table --- */
669 typedef struct endpt_ops
{
671 /* --- @attach@ --- *
673 * Arguments: @endpt *e@ = pointer to endpoint to be attached
674 * @reffd *in, *out@ = input and output file descriptors
678 * Use: Instructs a non-file endpoint to attach itself to a pair of
682 void (*attach
)(endpt */
*e*/
, reffd */
*in*/
, reffd */
*out*/
);
686 * Arguments: @endpt *e@ = pointer to endpoint in question
687 * @endpt *f@ = pointer to a file endpoint
691 * Use: Informs a non-file endpoint of a file endpoint which will
692 * want to be closed when it's finished with. At that time, the
693 * endpoint should arrange to have both itself and its partner
694 * closed. If no file is registered, the endpoint manager will
695 * close both endpoints itself.
698 void (*file
)(endpt */
*e*/
, endpt */
*f*/
);
700 /* --- @wclose@ --- *
702 * Arguments: @endpt *e@ = endpoint to be partially closed
706 * Use: Announces that the endpoint will not be written to any more.
709 void (*wclose
)(endpt */
*e*/
);
713 * Arguments: @endpt *e@ = endpoint to be closed
717 * Use: Completely closes an endpoint. The endpoint's data may be
718 * freed, although some endpoints may wish to delay freeing for
722 void (*close
)(endpt */
*e*/
);
726 /* --- A basic target object --- */
728 typedef struct target
{
729 struct target_ops
*ops
;
733 /* --- Forwarding target operations --- */
735 typedef struct target_ops
{
736 const char *name
; /* Name of this target */
738 /* --- @option@ --- *
740 * Arguments: @target *t@ = pointer to target object, or zero if global
741 * @scanner *sc@ = scanner to read from
743 * Returns: Nonzero to claim the option.
745 * Use: Handles an option string from the configuration file.
748 int (*option
)(target */
*t*/
, scanner */
*sc*/
);
752 * Arguments: @scanner *sc@ = pointer to scanner to read from
754 * Returns: Pointer to a target object to claim, null to reject.
756 * Use: Parses a target description from the configuration file.
757 * Only the socket target is allowed to omit the prefix on a
758 * target specification.
761 target
*(*read
)(scanner */
*sc*/
);
763 /* --- @confirm@ --- *
765 * Arguments: @target *t@ = pointer to target
769 * Use: Confirms configuration of a target.
772 void (*confirm
)(target */
*t*/
);
774 /* --- @create@ --- *
776 * Arguments: @target *t@ = pointer to target
777 * @const char *desc@ = description of connection
779 * Returns: Pointer to a created endpoint.
781 * Use: Generates a target endpoint for communication.
784 endpt
*(*create
)(target */
*t*/
, const char */
*desc*/
);
786 /* --- @destroy@ --- *
788 * Arguments: @target *t@ = pointer to target
792 * Use: Destroys a target.
795 void (*destroy
)(target */
*t*/
);
799 /* --- A basic source object --- */
801 typedef struct source
{
802 struct source
*next
, *prev
;
803 struct source_ops
*ops
;
807 /* --- Forwarding source operations --- */
809 typedef struct source_ops
{
810 const char *name
; /* Name of this source */
812 /* --- @option@ --- *
814 * Arguments: @scanner *sc@ = scanner to read from
815 * @source *s@ = pointer to source object, or zero if global
817 * Returns: Nonzero to claim the option.
819 * Use: Handles an option string from the configuration file.
822 int (*option
)(source */
*s*/
, scanner */
*sc*/
);
826 * Arguments: @scanner *sc@ = pointer to scanner to read from
828 * Returns: Pointer to a source object to claim, null to reject.
830 * Use: Parses a source description from the configuration file.
831 * Only the socket source is allowed to omit the prefix on a
832 * source specification.
835 source
*(*read
)(scanner */
*sc*/
);
837 /* --- @attach@ --- *
839 * Arguments: @source *s@ = pointer to source
840 * @scanner *sc@ = scanner (for error reporting)
841 * @target *t@ = pointer to target to attach
845 * Use: Attaches a target to a source.
848 void (*attach
)(source */
*s*/
, scanner */
*sc*/
, target */
*t*/
);
850 /* --- @destroy@ --- *
852 * Arguments: @source *s@ = pointer to source
856 * Use: Destroys a source. Used when closing the system down, for
857 * example as a result of a signal.
860 void (*destroy
)(source */
*s*/
);
864 /* --- @endpt_kill@ --- *
866 * Arguments: @endpt *a@ = an endpoint
870 * Use: Kills an endpoint. If the endpoint is joined to another, the
871 * other endpoint is also killed, as is the connection between
872 * them (and that's the tricky bit).
875 extern void endpt_kill(endpt */
*a*/
);
877 /* --- @endpt_killall@ --- *
883 * Use: Destroys all current endpoint connections. Used when
887 extern void endpt_killall(void);
889 /* --- @endpt_join@ --- *
891 * Arguments: @endpt *a@ = pointer to first endpoint
892 * @endpt *b@ = pointer to second endpoint
896 * Use: Joins two endpoints together.
899 extern void endpt_join(endpt */
*a*/
, endpt */
*b*/
);
901 /* --- @source_add@ --- *
903 * Arguments: @source *s@ = pointer to a source
907 * Use: Adds a source to the master list. Only do this for passive
908 * sources (e.g., listening sockets), not active sources (e.g.,
909 * executable programs).
912 extern void source_add(source */
*s*/
);
914 /* --- @source_remove@ --- *
916 * Arguments: @source *s@ = pointer to a source
920 * Use: Removes a source from the master list.
923 extern void source_remove(source */
*s*/
);
925 /* --- @source_killall@ --- *
931 * Use: Frees all sources.
934 extern void source_killall(void);
936 /*----- The exec source and target ----------------------------------------*/
938 extern source_ops xsource_ops
;
939 extern target_ops xtarget_ops
;
941 /* --- @exec_init@ --- *
947 * Use: Initializes the executable problem source and target.
950 extern void exec_init(void);
952 /*----- The file source and target ----------------------------------------*/
954 extern source_ops fsource_ops
;
955 extern target_ops ftarget_ops
;
957 /*----- The socket source and target --------------------------------------*/
959 extern source_ops ssource_ops
;
960 extern target_ops starget_ops
;
962 /* --- @starget_connected@ --- *
964 * Arguments: @int fd@ = file descriptor now ready for use
965 * @void *p@ = pointer to an endpoint structure
969 * Use: Handles successful connection of the target endpoint.
972 extern void starget_connected(int /*fd*/, void */
*p*/
);
974 /*----- Handling of file attributes ---------------------------------------*/
976 /* --- File attribute options structure --- */
978 typedef struct fattr
{
984 /* --- Shared global options --- */
986 extern fattr fattr_global
;
988 /* --- @fattr_init@ --- *
990 * Arguments: @fattr *f@ = pointer to file attributes
994 * Use: Initializes a set of file attributes to default values.
997 extern void fattr_init(fattr */
*f*/
);
999 /* --- @fattr_option@ --- *
1001 * Arguments: @scanner *sc@ = pointer to scanner to read
1002 * @fattr *f@ = pointer to file attributes to set
1004 * Returns: Whether the option was clamed.
1006 * Use: Reads file attributes from a scanner.
1009 extern int fattr_option(scanner */
*sc*/
, fattr */
*f*/
);
1011 /* --- @fattr_apply@ --- *
1013 * Arguments: @const char *file@ = pointer to filename
1014 * @fattr *f@ = pointer to attribute set
1016 * Returns: @-1@ if it failed.
1018 * Use: Applies file attributes to a file. For best results, try to
1019 * create the file with the right permissions and so on. This
1020 * call will fix everything up, but there are potential races
1021 * which might catch you out if you're not careful.
1024 extern int fattr_apply(const char */
*file*/
, fattr */
*f*/
);
1026 /*----- Making privileged connections -------------------------------------*/
1028 /* --- @privconn_split@ --- *
1030 * Arguments: @sel_state *s@ = select state
1034 * Use: Splits off the privileged binding code into a separate
1038 extern void privconn_split(sel_state */
*s*/
);
1040 /* --- @privconn_adddest@ --- *
1042 * Arguments: @struct in_addr peer@ = address to connect to
1043 * @unsigned port@ = port to connect to
1045 * Returns: Index for this destination address, or @-1@ if not
1048 * Use: Adds a valid destination for a privileged connection.
1051 extern int privconn_adddest(struct in_addr
/*peer*/, unsigned /*port*/);
1053 /* --- @privconn_connect@ --- *
1055 * Arguments: @conn *c@ = connection structure to fill in
1056 * @sel_state *s@ = pointer to select state to attach to
1057 * @int i@ = address index to connect to
1058 * @struct in_addr bind@ = address to bind to
1059 * @void (*func)(int, void *)@ = function to call on connect
1060 * @void *p@ = argument for the function
1062 * Returns: Zero on success, @-1@ on failure.
1064 * Use: Sets up a privileged connection job.
1067 extern int privconn_connect(conn */
*c*/
, sel_state */
*s*/
,
1068 int /*i*/, struct in_addr
/*bind*/,
1069 void (*/
*func*/
)(int, void *), void */
*p*/
);
1071 /*----- Identifying remote clients ----------------------------------------*/
1073 typedef struct id_req
{
1074 struct sockaddr_in lsin
; /* Local address of connection */
1075 struct sockaddr_in rsin
; /* Remote address of connection */
1076 const char *desc
; /* Description of connection */
1077 const char *act
; /* Action taken by server */
1078 reffd
*r
; /* Pointer to file descriptor */
1081 /* --- @identify@ --- *
1083 * Arguments: @const id_req *q@ = pointer to request block
1087 * Use: Starts a background ident lookup and reverse-resolve job
1088 * which will, eventually, report a message to the system log.
1091 extern void identify(const id_req */
*q*/
);
1093 /*----- Host-based access control -----------------------------------------*/
1095 /* --- An access control entry --- */
1097 typedef struct acl_entry
{
1098 struct acl_entry
*next
; /* Next entry in the list */
1099 const struct acl_ops
*ops
; /* Operations for the ACL entry */
1100 unsigned act
; /* What to do with matching hosts */
1103 #define ACL_DENY 0 /* Deny access to matching conns */
1104 #define ACL_ALLOW 1 /* Allow access to matching conns */
1105 #define ACL_PERM 1u /* Bit mask for permission bit */
1107 /* --- Host-based access control --- */
1109 typedef struct acl_host
{
1110 acl_entry a
; /* Base structure */
1111 struct in_addr addr
, mask
; /* Address and netmask */
1114 /* --- ACL methods --- */
1116 typedef struct acl_ops
{
1117 int (*check
)(void */
*a*/
, struct in_addr
/*addr*/, unsigned /*port*/);
1118 void (*dump
)(void */
*a*/
, FILE */
*fp*/
);
1119 void (*free
)(void */
*a*/
);
1122 /* --- @acl_check@ --- *
1124 * Arguments: @acl_entry *a@ = pointer to ACL to check against
1125 * @struct in_addr addr@ = address to check
1126 * @unsigned port@ = port number to check
1127 * @int *act@ = verdict (should initially be @ACT_ALLOW@)
1129 * Returns: Zero if undecided, nonzero if a rule matched.
1131 * Use: Checks an address against an ACL.
1134 extern int acl_check(acl_entry */
*a*/
,
1135 struct in_addr
/*addr*/, unsigned /*port*/,
1138 /* --- @acl_dump@ --- *
1140 * Arguments: @acl_entry *a@ = pointer to ACL to dump
1141 * @FILE *fp@ = pointer to stream to dump on
1145 * Use: Dumps an access control list to an output stream.
1148 extern void acl_dump(acl_entry */
*a*/
, FILE */
*fp*/
);
1150 /* --- @acl_free@ --- *
1152 * Arguments: @acl_entry *a@ = pointer to a list of ACLs
1156 * Use: Frees all of the memory used by an ACL.
1159 extern void acl_free(acl_entry */
*a*/
);
1161 /* --- @acl_addhost@ --- *
1163 * Arguments: @acl_entry ***a@ = address of pointer to list tail
1164 * @unsigned act@ = what to do with matching addresses
1165 * @struct in_addr addr, mask@ = address and mask to match
1169 * Use: Adds a host-authentication entry to the end of an access
1173 extern void acl_addhost(acl_entry
***/
*a*/
, unsigned /*act*/,
1174 struct in_addr
/*addr*/, struct in_addr
/*mask*/);
1176 /* --- @acl_addpriv@ --- *
1178 * Arguments: @acl_entry ***a@ = address of pointer to list tail
1179 * @unsigned act@ = what to do with matching addresses
1183 * Use: Adds a privileged-port check to the end of an access control
1187 extern void acl_addpriv(acl_entry
***/
*a*/
, unsigned /*act*/);
1189 /*----- Network addresses -------------------------------------------------*/
1191 /* --- A generic socket address --- *
1193 * Not all systems understand @sa_len@ fields. (In particular, Linux
1194 * doesn't.) Some fairly ugly hacking is then performed on particular
1198 typedef struct addr
{
1199 struct addr_ops
*ops
;
1203 #define ADDRSZ(sz) (sizeof(addr) + (sz))
1205 /* --- Address configuration --- *
1207 * An address family will want to extend this.
1210 typedef struct addr_opts
{
1214 #define ADDRF_NOLOG 1u
1216 /* --- Address types --- *
1218 * For things like Internet addresses, source and destinations look
1228 /* --- Description of an address type handler --- */
1230 typedef struct addr_ops
{
1231 const char *name
; /* Protocol's internal name */
1235 * Arguments: @scanner *sc@ = pointer to scanner to read from
1236 * @unsigned type@ = type of address to be read
1238 * Returns: A filled-in socket address.
1240 * Use: Parses a textual representation of a socket address.
1243 addr
*(*read
)(scanner */
*sc*/
, unsigned /*type*/);
1245 /* --- @destroy@ --- *
1247 * Arguments: @addr *a@ = pointer to an address block
1251 * Use: Disposes of an address block in some suitable fashion.
1254 void (*destroy
)(addr */
*a*/
);
1256 /* --- @print@ --- *
1258 * Arguments: @addr *a@ = pointer to socket address to read
1259 * @unsigned type@ = type of address to be written
1260 * @dstr *d@ = string on which to write the description
1264 * Use: Writes a textual representation of a socket address to
1268 void (*print
)(addr */
*a*/
, unsigned /*type*/, dstr */
*d*/
);
1270 /* --- @initsrcopts@ --- *
1274 * Returns: A pointer to a protocol-specific data block for a listener
1276 * Use: Creates a data block for a listener. This is attached to the
1277 * listener data structure. Options can then be requested, and
1278 * are added to the block when necessary.
1281 addr_opts
*(*initsrcopts
)(void);
1283 /* --- @option@ --- *
1285 * Arguments: @scanner *sc@ = pointer to a scanner to read from
1286 * @unsigned type@ = kind of option this is
1287 * @addr_opts *ao@ = data block to modify (from @init@), or null
1289 * Returns: Nonzero to claim the option.
1291 * Use: Parses a source option, either global or listener-specific.
1294 int (*option
)(scanner */
*sc*/
, addr_opts */
*ao*/
, unsigned /*type*/);
1296 /* --- @confirm@ --- *
1298 * Arguments: @addr *a@ = pointer to an address structure
1299 * @unsigned type@ = kind of address this is
1300 * @addr_opts *ao@ = address options
1304 * Use: Called during initialization when an address is fully
1308 void (*confirm
)(addr */
*a*/
, unsigned /*type*/, addr_opts */
*ao*/
);
1310 /* --- @freesrcopts@ --- *
1312 * Arguments: @addr_opts *ao@ = data block to remove
1316 * Use: Throws away all the configuration data for an address type.
1319 void (*freesrcopts
)(addr_opts */
*ao*/
);
1323 * Arguments: @addr *a@ = the address to bind to
1324 * @addr_opts *ao@ = the address options
1326 * Returns: File descriptor of bound socket if OK, or @-1@ on error.
1328 * Use: Binds a listening socket. The tedious stuff with @listen@
1332 int (*bind
)(addr */
*a*/
, addr_opts */
*ao*/
);
1334 /* --- @unbind@ --- *
1336 * Arguments: @addr *a@ = pointer to an address
1340 * Use: Unbinds an address. This is used when tidying up. The main
1341 * purpose is to let the Unix-domain handler remove its socket
1342 * node from the filesystem.
1345 void (*unbind
)(addr */
*a*/
);
1347 /* --- @accept@ --- *
1349 * Arguments: @int fd@ = listening file descriptor
1350 * @addr_opts *ao@ = data block to get configuration from
1351 * @const char *desc@ = description of the listener
1353 * Returns: Pointer to a reference counted file descriptor.
1355 * Use: Accepts, verifies and logs an incoming connection.
1358 reffd
*(*accept
)(int /*fd*/, addr_opts */
*ao*/
, const char */
*desc*/
);
1360 /* --- @inittargopts@ --- *
1364 * Returns: A pointer to a protocol-specific data block for a connecter
1366 * Use: Creates a data block for a target. This is attached to the
1367 * target data structure. Options can then be requested, and
1368 * are added to the block when necessary.
1371 addr_opts
*(*inittargopts
)(void);
1373 /* --- @freetargopts@ --- *
1375 * Arguments: @addr_opts *ao@ = data block to remove
1379 * Use: Throws away all the configuration data for an address type.
1382 void (*freetargopts
)(addr_opts */
*ao*/
);
1384 /* --- @connect@ --- *
1386 * Arguments: @addr *a@ = destination address
1387 * @addr_opts *ao@ = target address options
1388 * @conn *c@ = connection structure
1389 * @endpt *e@ = endpoint structure
1391 * Returns: Zero if OK, @-1@ on some error.
1393 * Use: Requests that a connection be made, or at least set in
1394 * motion. An address may do one of these things:
1398 * * Call @starget_connected@ with @-1@ or a connected file
1399 * descriptor and the pointer @e@.
1401 * * Call @conn_init@ or @conn_fd@, giving @starget_connected@
1402 * and @e@ as the function to call.
1405 int (*connect
)(addr */
*a*/
, addr_opts */
*ao*/
, conn */
*c*/
, endpt */
*e*/
);
1409 /* --- Address types --- */
1411 extern addr_ops un_ops
;
1412 extern addr_ops inet_ops
;
1414 /*----- That's all, folks -------------------------------------------------*/