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/mdup.h>
91 #include <mLib/mdwopt.h>
92 #include <mLib/quis.h>
93 #include <mLib/report.h>
95 #include <mLib/selbuf.h>
102 /*----- Other subtleties --------------------------------------------------*/
104 #if defined(HAVE_DECL_ENVIRON) && !HAVE_DECL_ENVIRON
105 extern char **environ
;
108 /*----- Main program ------------------------------------------------------*/
110 /* --- The global select state --- */
112 extern sel_state
*sel
;
114 /* --- Help text --- */
116 extern const char grammar_text
[];
117 extern const char option_text
[];
119 /* --- @fw_log@ --- *
121 * Arguments: @time_t t@ = when the connection occurred or (@-1@)
122 * @const char *fmt@ = format string to fill in
123 * @...@ = other arguments
127 * Use: Logs a connection.
130 extern void fw_log(time_t /*t*/, const char */
*fmt*/
, ...);
132 /* --- @fw_inc@, @fw_dec@ --- *
138 * Use: Increments or decrements the active thing count. `fwd' won't
139 * quit while there are active things.
142 extern void fw_inc(void);
143 extern void fw_dec(void);
145 /*----- Channel management ------------------------------------------------*/
147 /* --- Magic numbers --- */
149 #define CHAN_BUFSZ 4096
151 /* --- Channel structure --- */
153 typedef struct chan
{
154 unsigned base
, len
; /* Base and length of data */
155 unsigned f
; /* Various interesting flags */
156 void (*func
)(void */
*p*/
); /* Function to call on closure */
157 void *p
; /* Argument to pass function */
158 sel_file r
, w
; /* Reader and writer selectors */
159 char buf
[CHAN_BUFSZ
]; /* The actual data buffer */
162 #define CHANF_CLOSE 1u /* Close channel when buffer empty */
163 #define CHANF_READY 2u /* The channel destination exists */
165 /* --- @chan_close@ --- *
167 * Arguments: @chan *c@ = pointer to channel
171 * Use: Closes down a channel prematurely.
174 extern void chan_close(chan */
*c*/
);
176 /* --- @chan_dest@ --- *
178 * Arguments: @chan *c@ = pointer to channel
179 * @int fd@ = destination file descriptor for channel
183 * Use: Sets the channel's destination so it knows where to put
187 extern void chan_dest(chan */
*c*/
, int /*fd*/);
189 /* --- @chan_open@ --- *
191 * Arguments: @chan *c@ = pointer to channel to open
192 * @int from, to@ = source and destination file descriptors
193 * @void (*func)(void *p)@ = function to call on closure
194 * @void *p@ = argument to pass to function
198 * Use: Opens a channel. Data is copied from the source to the
199 * destination. The @to@ argument may be @-1@ if the file
200 * descriptor isn't known yet.
203 extern void chan_open(chan */
*c*/
, int /*from*/, int /*to*/,
204 void (*/
*func*/
)(void */
*p*/
), void */
*p*/
);
206 /*----- Character scanners ------------------------------------------------*/
208 /* --- A low-level scanner source --- */
210 typedef struct scansrc
{
211 struct scansrc
*next
; /* Next one in the list */
212 struct scansrc_ops
*ops
; /* Pointer to operations table */
213 char *src
; /* Name of this source */
214 int line
; /* Current line number */
215 dstr pushback
; /* Pushback characters */
216 char *tok
; /* Token pushback */
217 unsigned t
; /* Token type pushback */
220 /* --- Scanner source operations --- */
222 typedef struct scansrc_ops
{
223 int (*scan
)(scansrc */
*ss*/
); /* Read another character */
224 void (*destroy
)(scansrc */
*ss*/
); /* Destroy an unwanted source */
227 /* --- A character scanner --- */
229 typedef struct scanner
{
230 scansrc
*head
, **tail
; /* Scanner list head and tail */
231 int t
; /* Token type */
232 dstr d
; /* Current token value */
233 const char *wbegin
, *wcont
; /* Parsing exception strings */
236 /* --- @scan_file@ --- *
238 * Arguments: @FILE *fp@ = pointer to file descriptor
239 * @const char *name@ = pointer to source file name
240 * @unsigned f@ = flags
242 * Returns: A scanner source.
244 * Use: Creates a new scanner source for reading from a file.
247 #define SCF_NOCLOSE 1u /* Don't close @fp@ when finished */
249 extern scansrc
*scan_file(FILE */
*fp*/
, const char */
*name*/
,
252 /* --- @scan_argv@ --- *
254 * Arguments: @char **av@ = pointer to argument array (null terminated)
256 * Returns: A scanner source.
258 * Use: Creates a new scanner source for reading from an @argv@
262 extern scansrc
*scan_argv(char **/
*av*/
);
266 * Arguments: @scanner *sc@ = pointer to main scanner context
268 * Returns: Character read, or end-of-file.
270 * Use: Scans a character from a source of characters.
273 extern int scan(scanner */
*sc*/
);
275 /* --- @unscan@ --- *
277 * Arguments: @scanner *sc@ = pointer to main scanner context
278 * @int ch@ = character to unscan
282 * Use: Scans a character from a source of characters.
285 extern void unscan(scanner */
*sc*/
, int /*ch*/);
287 /* --- @scan_push@ --- *
289 * Arguments: @scanner *sc@ = pointer to main scanner context
290 * @scansrc *ss@ = souorce to push
294 * Use: Pushes a scanner source onto the front of the queue.
297 extern void scan_push(scanner */
*sc*/
, scansrc */
*ss*/
);
299 /* --- @scan_add@ --- *
301 * Arguments: @scanner *sc@ = pointer to main scanner context
302 * @scansrc *ss@ = souorce to push
306 * Use: Adds a scanner source onto the end of the queue.
309 extern void scan_add(scanner */
*sc*/
, scansrc */
*ss*/
);
311 /* --- @scan_create@ --- *
313 * Arguments: @scanner *sc@ = scanner context to initialize
317 * Use: Initializes a scanner block ready for use.
320 extern void scan_create(scanner */
*sc*/
);
322 /* --- @scan_destroy@ --- *
324 * Arguments: @scanner *sc@ = pointer to scanner context
328 * Use: Destroys a scanner and all the sources attached to it.
331 extern void scan_destroy(scanner */
*sc*/
);
333 /*----- Configuration parsing ---------------------------------------------*/
335 /* --- Magical constants --- */
337 #define CTOK_EOF (-1)
338 #define CTOK_WORD 256
340 /* --- @conf_undelim@ --- *
342 * Arguments: @scanner *sc@ = pointer to scanner definition
343 * @const char *d, *dd@ = pointer to characters to escape
347 * Use: Modifies the tokenizer. Characters in the first list will
348 * always be considered to begin a word. Characters in the
349 * second list will always be allowed to continue a word.
352 extern void conf_undelim(scanner */
*sc*/
,
353 const char */
*d*/
, const char */
*dd*/
);
357 * Arguments: @scanner *sc@ = pointer to scanner definition
359 * Returns: Type of token scanned.
361 * Use: Reads the next token from the character scanner.
364 extern int token(scanner */
*sc*/
);
368 * Arguments: @scanner *sc@ = pointer to scanner definition
369 * @const char *msg@ = message skeleton string
370 * @...@ = extra arguments for the skeleton
374 * Use: Reports an error at the current scanner location.
377 extern void error(scanner */
*sc*/
, const char */
*msg*/
, ...);
379 /* --- @pushback@ --- *
381 * Arguments: @scanner *sc@ = pointer to scanner definition
385 * Use: Pushes the current token back. This is normally a precursor
386 * to pushing a new scanner source.
389 extern void pushback(scanner */
*sc*/
);
391 /* --- @conf_enum@ --- *
393 * Arguments: @scanner *sc@ = pointer to a scanner object
394 * @const char *list@ = comma-separated things to allow
395 * @unsigned @f = flags for the search
396 * @const char *err@ = error message if not found
398 * Returns: Index into list, zero-based, or @-1@.
400 * Use: Checks whether the current token is a string which matches
401 * one of the comma-separated items given. The return value is
402 * the index (zero-based) of the matched string in the list.
404 * The flags control the behaviour if no exact match is found.
405 * If @ENUM_ABBREV@ is set, and the current token is a left
406 * substring of exactly one of the possibilities, then that one
407 * is chosen. If @ENUM_NONE@ is set, the value @-1@ is
408 * returned; otherwise an error is reported and the program is
412 #define ENUM_ABBREV 1u
415 extern int conf_enum(scanner */
*sc*/
, const char */
*list*/
,
416 unsigned /*flags*/, const char */
*err*/
);
418 /* --- @conf_prefix@ --- *
420 * Arguments: @scanner *sc@ = pointer to a scanner object
421 * @const char *p@ = pointer to prefix string to check
423 * Returns: Nonzero if the prefix matches.
425 * Use: If the current token is a word matching the given prefix
426 * string, then it and an optional `.' character are removed and
427 * a nonzero result is returned. Otherwise the current token is
428 * left as it is, and zero is returned.
430 * Typical options parsing code would remove an expected prefix,
431 * scan an option anyway (since qualifying prefixes are
432 * optional) and if a match is found, claim the option. If no
433 * match is found, and a prefix was stripped, then an error
434 * should be reported.
437 extern int conf_prefix(scanner */
*sc*/
, const char */
*p*/
);
439 /* --- @CONF_BEGIN@, @CONF_END@ --- *
441 * Arguments: @sc@ = scanner to read from
442 * @prefix@ = prefix to scan for
443 * @desc@ = description of what we're parsing
445 * Use: Bracket an options parsing routine. The current token is
446 * checked to see whether it matches the prefix. If so, it is
447 * removed and the following token examined. If that's a `.'
448 * then it's removed. If it's a `{' then the enclosed
449 * option-parsing code is executed in a loop until a matching
450 * '}' is found. If the options parser doesn't accept an
451 * option, the behaviour is dependent on whether a prefix was
452 * seen: if so, an error is reported; otherwse a zero return is
461 #define CONF_BEGIN(sc, prefix, desc) do { \
462 scanner *_conf_sc = (sc); \
463 const char *_conf_desc = (desc); \
464 int _conf_state = CS_PLAIN; \
466 /* --- Read the initial prefix --- */ \
468 if (_conf_sc->t == CTOK_WORD && \
469 strcmp(_conf_sc->d.buf, (prefix)) == 0) { \
471 _conf_state = CS_PREFIX; \
472 if (_conf_sc->t == '.') \
474 else if (_conf_sc->t == '{') { \
476 _conf_state = CS_BRACE; \
480 /* --- Ensure the next token is a word --- */ \
482 if (_conf_sc->t != CTOK_WORD) \
483 error(_conf_sc, "parse error, expected option keyword"); \
488 /* --- Reject an option --- * \
490 * We could get here as a result of an explicit @CONF_REJECT@ or \
491 * because the option wasn't accepted. \
496 if (_conf_state == CS_PLAIN) \
497 _conf_state = CS_UNKNOWN; \
499 error(_conf_sc, "unknown %s option `%s'", \
500 _conf_desc, _conf_sc->d.buf); \
503 /* --- Accept an option --- * \
505 * It's safe to drop through from above. Either an error will have \
506 * been reported, or the state is not @CS_BRACE@. \
510 if (_conf_state == CS_BRACE && _conf_sc->t == ';') \
512 } while (_conf_state == CS_BRACE && _conf_sc->t == CTOK_WORD); \
514 /* --- Check for a closing brace --- */ \
516 if (_conf_state == CS_BRACE) { \
517 if (_conf_sc->t == '}') \
520 error(_conf_sc, "parse error, expected `}'"); \
523 /* --- Return an appropriate value --- */ \
525 return (_conf_state != CS_UNKNOWN); \
528 /* --- @CONF_ACCEPT@, @CONF_REJECT@ --- *
532 * Use: Within an options parser (between @CONF_BEGIN@ and
533 * @CONF_END@), accept or reject an option.
536 #define CONF_ACCEPT goto _conf_accept
537 #define CONF_REJECT goto _conf_reject
539 /* --- @CONF_QUAL@ --- *
543 * Use: Evaluates to a nonzero value if the current option is
544 * qualified. This can be used to decide whether abbreviations
545 * for options should be accepted.
548 #define CONF_QUAL (_conf_state != CS_PLAIN)
550 /* --- @conf_name@ --- *
552 * Arguments: @scanner *sc@ = pointer to scanner
553 * @char delim@ = delimiter character to look for
554 * @dstr *d@ = pointer to dynamic string for output
558 * Use: Reads in a compound name consisting of words separated by
559 * delimiters. Leading and trailing delimiters are permitted,
560 * although they'll probably cause confusion if used. The name
561 * may be enclosed in square brackets if that helps at all.
563 * Examples of compound names are filenames (delimited by `/')
564 * and IP addresses (delimited by `.').
567 extern void conf_name(scanner */
*sc*/
, char /*delim*/, dstr */
*d*/
);
569 /* --- @conf_fname@ --- *
571 * Arguments: @scanner *sc@ = pointer to scanner
572 * @dstr *d@ = pointer to dynamic string for output
576 * Use: Reads a file name from the input and stores it in @d@.
579 extern void conf_fname(scanner */
*sc*/
, dstr */
*d*/
);
581 /*----- Reference-counted file descriptors --------------------------------*/
583 typedef struct reffd
{
586 void (*proc
)(void */
*p*/
);
590 /* --- @reffd_init@ --- *
592 * Arguments: @int fd@ = file descriptor
594 * Returns: Reference-counted file descriptor object.
596 * Use: Creates a refcounted file descriptor.
599 extern reffd
*reffd_init(int /*fd*/);
601 /* --- @reffd_handler@ --- *
603 * Arguments: @reffd *r@ = pointer to reference counted filehandle
604 * @void (*proc)(void *p)@ = procedure to call
609 * Use: Sets the reference counted file descriptor to call @proc@
610 * when it is no longer required.
613 extern void reffd_handler(reffd */
*r*/
, void (*/
*proc*/
)(void */
*p*/
),
616 /* --- @reffd_inc@ --- *
618 * Arguments: @reffd *r@ = pointer to reference counted filehandle
622 * Use: Increments the reference count for a file descriptor.
625 #define REFFD_INC(r) do { (r)->ref++; } while (0)
627 extern void reffd_inc(reffd */
*r*/
);
629 /* --- @reffd_dec@ --- *
631 * Arguments: @reffd *r@ = pointer to reference counted filehandle
635 * Use: Decrements the reference count for a file descriptor.
638 #define REFFD_DEC(r) do { \
641 if (_r->ref == 0) { \
649 extern void reffd_dec(reffd */
*r*/
);
651 /*----- Sources, targets and endpoints ------------------------------------*/
653 /* --- Basic endpoint structure --- */
655 typedef struct endpt
{
656 struct endpt_ops
*ops
; /* Pointer to operations table */
657 struct endpt
*other
; /* Pointer to sibling endpoint */
658 unsigned f
; /* Various flags */
659 struct tango
*t
; /* Private data structure */
660 reffd
*in
, *out
; /* File descriptors */
663 /* --- Endpoint flags --- */
665 #define EPF_PENDING 1u /* Endpoint creation in progress */
666 #define EPF_FILE 2u /* Endpoint smells like a file */
668 /* --- Endpoint operations table --- */
670 typedef struct endpt_ops
{
672 /* --- @attach@ --- *
674 * Arguments: @endpt *e@ = pointer to endpoint to be attached
675 * @reffd *in, *out@ = input and output file descriptors
679 * Use: Instructs a non-file endpoint to attach itself to a pair of
683 void (*attach
)(endpt */
*e*/
, reffd */
*in*/
, reffd */
*out*/
);
687 * Arguments: @endpt *e@ = pointer to endpoint in question
688 * @endpt *f@ = pointer to a file endpoint
692 * Use: Informs a non-file endpoint of a file endpoint which will
693 * want to be closed when it's finished with. At that time, the
694 * endpoint should arrange to have both itself and its partner
695 * closed. If no file is registered, the endpoint manager will
696 * close both endpoints itself.
699 void (*file
)(endpt */
*e*/
, endpt */
*f*/
);
701 /* --- @wclose@ --- *
703 * Arguments: @endpt *e@ = endpoint to be partially closed
707 * Use: Announces that the endpoint will not be written to any more.
710 void (*wclose
)(endpt */
*e*/
);
714 * Arguments: @endpt *e@ = endpoint to be closed
718 * Use: Completely closes an endpoint. The endpoint's data may be
719 * freed, although some endpoints may wish to delay freeing for
723 void (*close
)(endpt */
*e*/
);
727 /* --- A basic target object --- */
729 typedef struct target
{
730 struct target_ops
*ops
;
734 /* --- Forwarding target operations --- */
736 typedef struct target_ops
{
737 const char *name
; /* Name of this target */
739 /* --- @option@ --- *
741 * Arguments: @target *t@ = pointer to target object, or zero if global
742 * @scanner *sc@ = scanner to read from
744 * Returns: Nonzero to claim the option.
746 * Use: Handles an option string from the configuration file.
749 int (*option
)(target */
*t*/
, scanner */
*sc*/
);
753 * Arguments: @scanner *sc@ = pointer to scanner to read from
755 * Returns: Pointer to a target object to claim, null to reject.
757 * Use: Parses a target description from the configuration file.
758 * Only the socket target is allowed to omit the prefix on a
759 * target specification.
762 target
*(*read
)(scanner */
*sc*/
);
764 /* --- @confirm@ --- *
766 * Arguments: @target *t@ = pointer to target
770 * Use: Confirms configuration of a target.
773 void (*confirm
)(target */
*t*/
);
775 /* --- @create@ --- *
777 * Arguments: @target *t@ = pointer to target
778 * @const char *desc@ = description of connection
780 * Returns: Pointer to a created endpoint.
782 * Use: Generates a target endpoint for communication.
785 endpt
*(*create
)(target */
*t*/
, const char */
*desc*/
);
787 /* --- @destroy@ --- *
789 * Arguments: @target *t@ = pointer to target
793 * Use: Destroys a target.
796 void (*destroy
)(target */
*t*/
);
800 /* --- A basic source object --- */
802 typedef struct source
{
803 struct source
*next
, *prev
;
804 struct source_ops
*ops
;
808 /* --- Forwarding source operations --- */
810 typedef struct source_ops
{
811 const char *name
; /* Name of this source */
813 /* --- @option@ --- *
815 * Arguments: @scanner *sc@ = scanner to read from
816 * @source *s@ = pointer to source object, or zero if global
818 * Returns: Nonzero to claim the option.
820 * Use: Handles an option string from the configuration file.
823 int (*option
)(source */
*s*/
, scanner */
*sc*/
);
827 * Arguments: @scanner *sc@ = pointer to scanner to read from
829 * Returns: Pointer to a source object to claim, null to reject.
831 * Use: Parses a source description from the configuration file.
832 * Only the socket source is allowed to omit the prefix on a
833 * source specification.
836 source
*(*read
)(scanner */
*sc*/
);
838 /* --- @attach@ --- *
840 * Arguments: @source *s@ = pointer to source
841 * @scanner *sc@ = scanner (for error reporting)
842 * @target *t@ = pointer to target to attach
846 * Use: Attaches a target to a source.
849 void (*attach
)(source */
*s*/
, scanner */
*sc*/
, target */
*t*/
);
851 /* --- @destroy@ --- *
853 * Arguments: @source *s@ = pointer to source
857 * Use: Destroys a source. Used when closing the system down, for
858 * example as a result of a signal.
861 void (*destroy
)(source */
*s*/
);
865 /* --- @endpt_kill@ --- *
867 * Arguments: @endpt *a@ = an endpoint
871 * Use: Kills an endpoint. If the endpoint is joined to another, the
872 * other endpoint is also killed, as is the connection between
873 * them (and that's the tricky bit).
876 extern void endpt_kill(endpt */
*a*/
);
878 /* --- @endpt_killall@ --- *
884 * Use: Destroys all current endpoint connections. Used when
888 extern void endpt_killall(void);
890 /* --- @endpt_join@ --- *
892 * Arguments: @endpt *a@ = pointer to first endpoint
893 * @endpt *b@ = pointer to second endpoint
897 * Use: Joins two endpoints together.
900 extern void endpt_join(endpt */
*a*/
, endpt */
*b*/
);
902 /* --- @source_add@ --- *
904 * Arguments: @source *s@ = pointer to a source
908 * Use: Adds a source to the master list. Only do this for passive
909 * sources (e.g., listening sockets), not active sources (e.g.,
910 * executable programs).
913 extern void source_add(source */
*s*/
);
915 /* --- @source_remove@ --- *
917 * Arguments: @source *s@ = pointer to a source
921 * Use: Removes a source from the master list.
924 extern void source_remove(source */
*s*/
);
926 /* --- @source_killall@ --- *
932 * Use: Frees all sources.
935 extern void source_killall(void);
937 /*----- The exec source and target ----------------------------------------*/
939 extern source_ops xsource_ops
;
940 extern target_ops xtarget_ops
;
942 /* --- @exec_init@ --- *
948 * Use: Initializes the executable problem source and target.
951 extern void exec_init(void);
953 /*----- The file source and target ----------------------------------------*/
955 extern source_ops fsource_ops
;
956 extern target_ops ftarget_ops
;
958 /*----- The socket source and target --------------------------------------*/
960 extern source_ops ssource_ops
;
961 extern target_ops starget_ops
;
963 /* --- @starget_connected@ --- *
965 * Arguments: @int fd@ = file descriptor now ready for use
966 * @void *p@ = pointer to an endpoint structure
970 * Use: Handles successful connection of the target endpoint.
973 extern void starget_connected(int /*fd*/, void */
*p*/
);
975 /*----- Handling of file attributes ---------------------------------------*/
977 /* --- File attribute options structure --- */
979 typedef struct fattr
{
985 /* --- Shared global options --- */
987 extern fattr fattr_global
;
989 /* --- @fattr_init@ --- *
991 * Arguments: @fattr *f@ = pointer to file attributes
995 * Use: Initializes a set of file attributes to default values.
998 extern void fattr_init(fattr */
*f*/
);
1000 /* --- @fattr_option@ --- *
1002 * Arguments: @scanner *sc@ = pointer to scanner to read
1003 * @fattr *f@ = pointer to file attributes to set
1005 * Returns: Whether the option was clamed.
1007 * Use: Reads file attributes from a scanner.
1010 extern int fattr_option(scanner */
*sc*/
, fattr */
*f*/
);
1012 /* --- @fattr_apply@ --- *
1014 * Arguments: @const char *file@ = pointer to filename
1015 * @fattr *f@ = pointer to attribute set
1017 * Returns: @-1@ if it failed.
1019 * Use: Applies file attributes to a file. For best results, try to
1020 * create the file with the right permissions and so on. This
1021 * call will fix everything up, but there are potential races
1022 * which might catch you out if you're not careful.
1025 extern int fattr_apply(const char */
*file*/
, fattr */
*f*/
);
1027 /*----- Making privileged connections -------------------------------------*/
1029 /* --- @privconn_split@ --- *
1031 * Arguments: @sel_state *s@ = select state
1035 * Use: Splits off the privileged binding code into a separate
1039 extern void privconn_split(sel_state */
*s*/
);
1041 /* --- @privconn_adddest@ --- *
1043 * Arguments: @struct in_addr peer@ = address to connect to
1044 * @unsigned port@ = port to connect to
1046 * Returns: Index for this destination address, or @-1@ if not
1049 * Use: Adds a valid destination for a privileged connection.
1052 extern int privconn_adddest(struct in_addr
/*peer*/, unsigned /*port*/);
1054 /* --- @privconn_connect@ --- *
1056 * Arguments: @conn *c@ = connection structure to fill in
1057 * @sel_state *s@ = pointer to select state to attach to
1058 * @int i@ = address index to connect to
1059 * @struct in_addr bind@ = address to bind to
1060 * @void (*func)(int, void *)@ = function to call on connect
1061 * @void *p@ = argument for the function
1063 * Returns: Zero on success, @-1@ on failure.
1065 * Use: Sets up a privileged connection job.
1068 extern int privconn_connect(conn */
*c*/
, sel_state */
*s*/
,
1069 int /*i*/, struct in_addr
/*bind*/,
1070 void (*/
*func*/
)(int, void *), void */
*p*/
);
1072 /*----- Identifying remote clients ----------------------------------------*/
1074 typedef struct id_req
{
1075 struct sockaddr_in lsin
; /* Local address of connection */
1076 struct sockaddr_in rsin
; /* Remote address of connection */
1077 const char *desc
; /* Description of connection */
1078 const char *act
; /* Action taken by server */
1079 reffd
*r
; /* Pointer to file descriptor */
1082 /* --- @identify@ --- *
1084 * Arguments: @const id_req *q@ = pointer to request block
1088 * Use: Starts a background ident lookup and reverse-resolve job
1089 * which will, eventually, report a message to the system log.
1092 extern void identify(const id_req */
*q*/
);
1094 /*----- Host-based access control -----------------------------------------*/
1096 /* --- An access control entry --- */
1098 typedef struct acl_entry
{
1099 struct acl_entry
*next
; /* Next entry in the list */
1100 const struct acl_ops
*ops
; /* Operations for the ACL entry */
1101 unsigned act
; /* What to do with matching hosts */
1104 #define ACL_DENY 0 /* Deny access to matching conns */
1105 #define ACL_ALLOW 1 /* Allow access to matching conns */
1106 #define ACL_PERM 1u /* Bit mask for permission bit */
1108 /* --- Host-based access control --- */
1110 typedef struct acl_host
{
1111 acl_entry a
; /* Base structure */
1112 struct in_addr addr
, mask
; /* Address and netmask */
1115 /* --- ACL methods --- */
1117 typedef struct acl_ops
{
1118 int (*check
)(void */
*a*/
, struct in_addr
/*addr*/, unsigned /*port*/);
1119 void (*dump
)(void */
*a*/
, FILE */
*fp*/
);
1120 void (*free
)(void */
*a*/
);
1123 /* --- @acl_check@ --- *
1125 * Arguments: @acl_entry *a@ = pointer to ACL to check against
1126 * @struct in_addr addr@ = address to check
1127 * @unsigned port@ = port number to check
1128 * @int *act@ = verdict (should initially be @ACT_ALLOW@)
1130 * Returns: Zero if undecided, nonzero if a rule matched.
1132 * Use: Checks an address against an ACL.
1135 extern int acl_check(acl_entry */
*a*/
,
1136 struct in_addr
/*addr*/, unsigned /*port*/,
1139 /* --- @acl_dump@ --- *
1141 * Arguments: @acl_entry *a@ = pointer to ACL to dump
1142 * @FILE *fp@ = pointer to stream to dump on
1146 * Use: Dumps an access control list to an output stream.
1149 extern void acl_dump(acl_entry */
*a*/
, FILE */
*fp*/
);
1151 /* --- @acl_free@ --- *
1153 * Arguments: @acl_entry *a@ = pointer to a list of ACLs
1157 * Use: Frees all of the memory used by an ACL.
1160 extern void acl_free(acl_entry */
*a*/
);
1162 /* --- @acl_addhost@ --- *
1164 * Arguments: @acl_entry ***a@ = address of pointer to list tail
1165 * @unsigned act@ = what to do with matching addresses
1166 * @struct in_addr addr, mask@ = address and mask to match
1170 * Use: Adds a host-authentication entry to the end of an access
1174 extern void acl_addhost(acl_entry
***/
*a*/
, unsigned /*act*/,
1175 struct in_addr
/*addr*/, struct in_addr
/*mask*/);
1177 /* --- @acl_addpriv@ --- *
1179 * Arguments: @acl_entry ***a@ = address of pointer to list tail
1180 * @unsigned act@ = what to do with matching addresses
1184 * Use: Adds a privileged-port check to the end of an access control
1188 extern void acl_addpriv(acl_entry
***/
*a*/
, unsigned /*act*/);
1190 /*----- Network addresses -------------------------------------------------*/
1192 /* --- A generic socket address --- *
1194 * Not all systems understand @sa_len@ fields. (In particular, Linux
1195 * doesn't.) Some fairly ugly hacking is then performed on particular
1199 typedef struct addr
{
1200 struct addr_ops
*ops
;
1204 #define ADDRSZ(sz) (sizeof(addr) + (sz))
1206 /* --- Address configuration --- *
1208 * An address family will want to extend this.
1211 typedef struct addr_opts
{
1215 #define ADDRF_NOLOG 1u
1217 /* --- Address types --- *
1219 * For things like Internet addresses, source and destinations look
1229 /* --- Description of an address type handler --- */
1231 typedef struct addr_ops
{
1232 const char *name
; /* Protocol's internal name */
1236 * Arguments: @scanner *sc@ = pointer to scanner to read from
1237 * @unsigned type@ = type of address to be read
1239 * Returns: A filled-in socket address.
1241 * Use: Parses a textual representation of a socket address.
1244 addr
*(*read
)(scanner */
*sc*/
, unsigned /*type*/);
1246 /* --- @destroy@ --- *
1248 * Arguments: @addr *a@ = pointer to an address block
1252 * Use: Disposes of an address block in some suitable fashion.
1255 void (*destroy
)(addr */
*a*/
);
1257 /* --- @print@ --- *
1259 * Arguments: @addr *a@ = pointer to socket address to read
1260 * @unsigned type@ = type of address to be written
1261 * @dstr *d@ = string on which to write the description
1265 * Use: Writes a textual representation of a socket address to
1269 void (*print
)(addr */
*a*/
, unsigned /*type*/, dstr */
*d*/
);
1271 /* --- @initsrcopts@ --- *
1275 * Returns: A pointer to a protocol-specific data block for a listener
1277 * Use: Creates a data block for a listener. This is attached to the
1278 * listener data structure. Options can then be requested, and
1279 * are added to the block when necessary.
1282 addr_opts
*(*initsrcopts
)(void);
1284 /* --- @option@ --- *
1286 * Arguments: @scanner *sc@ = pointer to a scanner to read from
1287 * @unsigned type@ = kind of option this is
1288 * @addr_opts *ao@ = data block to modify (from @init@), or null
1290 * Returns: Nonzero to claim the option.
1292 * Use: Parses a source option, either global or listener-specific.
1295 int (*option
)(scanner */
*sc*/
, addr_opts */
*ao*/
, unsigned /*type*/);
1297 /* --- @confirm@ --- *
1299 * Arguments: @addr *a@ = pointer to an address structure
1300 * @unsigned type@ = kind of address this is
1301 * @addr_opts *ao@ = address options
1305 * Use: Called during initialization when an address is fully
1309 void (*confirm
)(addr */
*a*/
, unsigned /*type*/, addr_opts */
*ao*/
);
1311 /* --- @freesrcopts@ --- *
1313 * Arguments: @addr_opts *ao@ = data block to remove
1317 * Use: Throws away all the configuration data for an address type.
1320 void (*freesrcopts
)(addr_opts */
*ao*/
);
1324 * Arguments: @addr *a@ = the address to bind to
1325 * @addr_opts *ao@ = the address options
1327 * Returns: File descriptor of bound socket if OK, or @-1@ on error.
1329 * Use: Binds a listening socket. The tedious stuff with @listen@
1333 int (*bind
)(addr */
*a*/
, addr_opts */
*ao*/
);
1335 /* --- @unbind@ --- *
1337 * Arguments: @addr *a@ = pointer to an address
1341 * Use: Unbinds an address. This is used when tidying up. The main
1342 * purpose is to let the Unix-domain handler remove its socket
1343 * node from the filesystem.
1346 void (*unbind
)(addr */
*a*/
);
1348 /* --- @accept@ --- *
1350 * Arguments: @int fd@ = listening file descriptor
1351 * @addr_opts *ao@ = data block to get configuration from
1352 * @const char *desc@ = description of the listener
1354 * Returns: Pointer to a reference counted file descriptor.
1356 * Use: Accepts, verifies and logs an incoming connection.
1359 reffd
*(*accept
)(int /*fd*/, addr_opts */
*ao*/
, const char */
*desc*/
);
1361 /* --- @inittargopts@ --- *
1365 * Returns: A pointer to a protocol-specific data block for a connecter
1367 * Use: Creates a data block for a target. This is attached to the
1368 * target data structure. Options can then be requested, and
1369 * are added to the block when necessary.
1372 addr_opts
*(*inittargopts
)(void);
1374 /* --- @freetargopts@ --- *
1376 * Arguments: @addr_opts *ao@ = data block to remove
1380 * Use: Throws away all the configuration data for an address type.
1383 void (*freetargopts
)(addr_opts */
*ao*/
);
1385 /* --- @connect@ --- *
1387 * Arguments: @addr *a@ = destination address
1388 * @addr_opts *ao@ = target address options
1389 * @conn *c@ = connection structure
1390 * @endpt *e@ = endpoint structure
1392 * Returns: Zero if OK, @-1@ on some error.
1394 * Use: Requests that a connection be made, or at least set in
1395 * motion. An address may do one of these things:
1399 * * Call @starget_connected@ with @-1@ or a connected file
1400 * descriptor and the pointer @e@.
1402 * * Call @conn_init@ or @conn_fd@, giving @starget_connected@
1403 * and @e@ as the function to call.
1406 int (*connect
)(addr */
*a*/
, addr_opts */
*ao*/
, conn */
*c*/
, endpt */
*e*/
);
1410 /* --- Address types --- */
1412 extern addr_ops un_ops
;
1413 extern addr_ops inet_ops
;
1415 /*----- That's all, folks -------------------------------------------------*/