2 /* Copyright Gerhard Rieger and contributors (see file CHANGES) */
3 /* Published under the GNU General Public License V.2, see file COPYING */
5 #ifndef __xio_h_included
6 #define __xio_h_included 1
16 #define HAVE_STRUCT_LINGER 1
18 #define LINETERM_RAW 0
20 #define LINETERM_CRNL 2
25 /* the flags argument of xioopen */
26 #define XIO_RDONLY O_RDONLY /* asserted to be 0 */
27 #define XIO_WRONLY O_WRONLY /* asserted to be 1 */
28 #define XIO_RDWR O_RDWR /* asserted to be 2 */
29 #define XIO_ACCMODE (XIO_RDONLY|XIO_WRONLY|XIO_RDWR) /* must be 3 */
30 #define XIO_MAYFORK 4 /* address is allowed to fork the program (fork) */
31 #define XIO_MAYCHILD 8 /* address is allowed to fork off a child (exec)*/
32 #define XIO_MAYEXEC 16 /* address is allowed to exec a prog (exec+nofork) */
33 #define XIO_MAYCONVERT 32 /* address is allowed to perform modifications on the
34 stream data, e.g. SSL, REALDINE; CRLF */
36 /* the status flags of xiofile_t */
37 #define XIO_DOESFORK XIO_MAYFORK
38 #define XIO_DOESCHILD XIO_MAYCHILD
39 #define XIO_DOESEXEC XIO_MAYEXEC
40 #define XIO_DOESCONVERT XIO_MAYCONVERT
43 /* methods for reading and writing, and for related checks */
44 #define XIODATA_READMASK 0xf000 /* mask for basic r/w method */
45 #define XIOREAD_STREAM 0x1000 /* read() (default) */
46 #define XIOREAD_RECV 0x2000 /* recvfrom() */
47 #define XIOREAD_PTY 0x4000 /* handle EIO */
48 #define XIOREAD_READLINE 0x5000 /* ... */
49 #define XIOREAD_OPENSSL 0x6000 /* SSL_read() */
50 #define XIODATA_WRITEMASK 0x0f00 /* mask for basic r/w method */
51 #define XIOWRITE_STREAM 0x0100 /* write() (default) */
52 #define XIOWRITE_SENDTO 0x0200 /* sendto() */
53 #define XIOWRITE_PIPE 0x0300 /* write() to alternate (pipe) Fd */
54 #define XIOWRITE_2PIPE 0x0400 /* write() to alternate (2pipe) Fd */
55 #define XIOWRITE_READLINE 0x0500 /* check for prompt */
56 #define XIOWRITE_OPENSSL 0x0600 /* SSL_write() */
57 /* modifiers to XIODATA_READ_RECV */
58 #define XIOREAD_RECV_CHECKPORT 0x0001 /* recv, check peer port */
59 #define XIOREAD_RECV_CHECKADDR 0x0002 /* recv, check peer address */
60 #define XIOREAD_RECV_CHECKRANGE 0x0004 /* recv, check if peer addr in range */
61 #define XIOREAD_RECV_ONESHOT 0x0008 /* give EOF after first packet */
62 #define XIOREAD_RECV_SKIPIP 0x0010 /* recv, skip IPv4 header */
63 #define XIOREAD_RECV_FROM 0x0020 /* remember peer for replying */
66 #define XIODATA_MASK (XIODATA_READMASK|XIODATA_WRITEMASK)
67 #define XIODATA_STREAM (XIOREAD_STREAM|XIOWRITE_STREAM)
68 #define XIODATA_RECVFROM (XIOREAD_RECV|XIOWRITE_SENDTO|XIOREAD_RECV_CHECKPORT|XIOREAD_RECV_CHECKADDR|XIOREAD_RECV_FROM)
69 #define XIODATA_RECVFROM_SKIPIP (XIODATA_RECVFROM|XIOREAD_RECV_SKIPIP)
70 #define XIODATA_RECVFROM_ONE (XIODATA_RECVFROM|XIOREAD_RECV_ONESHOT)
71 #define XIODATA_RECVFROM_SKIPIP_ONE (XIODATA_RECVFROM_SKIPIP|XIOREAD_RECV_ONESHOT)
72 #define XIODATA_RECV (XIOREAD_RECV|XIOWRITE_SENDTO|XIOREAD_RECV_CHECKRANGE)
73 #define XIODATA_RECV_SKIPIP (XIODATA_RECV|XIOREAD_RECV_SKIPIP)
74 #define XIODATA_PIPE (XIOREAD_STREAM|XIOWRITE_PIPE)
75 #define XIODATA_2PIPE (XIOREAD_STREAM|XIOWRITE_2PIPE)
76 #define XIODATA_PTY (XIOREAD_PTY|XIOWRITE_STREAM)
77 #define XIODATA_READLINE (XIOREAD_READLINE|XIOWRITE_STREAM)
78 #define XIODATA_OPENSSL (XIOREAD_OPENSSL|XIOWRITE_OPENSSL)
81 /* these are the values allowed for the "enum xiotag tag" flag of the "struct
82 single" and "union bipipe" (xiofile_t) structures. */
84 XIO_TAG_INVALID
, /* the record is not in use */
85 XIO_TAG_RDONLY
, /* this is a single read-only stream */
86 XIO_TAG_WRONLY
, /* this is a single write-only stream */
87 XIO_TAG_RDWR
, /* this is a single read-write stream */
88 XIO_TAG_DUAL
/* this is a dual stream, consisting of two single
92 /* global XIO options/parameters */
97 const char *optionsep
;
99 char ip6portsep
; /* do not change, might be hardcoded somewhere! */
100 char logopt
; /* 'm' means "switch to syslog when entering daemon mode" */
101 const char *syslogfac
; /* syslog facility (only with mixed mode) */
102 char default_ip
; /* default prot.fam for IP based listen ('4' or '6') */
103 char preferred_ip
; /* preferred prot.fam. for name resolution ('0' for
104 unspecified, '4', or '6') */
107 /* pack the description of a lock file */
109 const char *lockfile
; /* name of lockfile; NULL if no locking */
110 bool waitlock
; /* dont't exit when already locked */
111 struct timespec intervall
; /* polling intervall */
114 extern xioopts_t xioopts
;
118 /* a non-dual file descriptor */
119 typedef struct single
{
120 enum xiotag tag
; /* see enum xiotag */
121 const struct addrdesc
*addr
;
123 /* until here, keep consistent with bipipe.common !!! */
125 unsigned int retry
; /* retry opening this many times */
126 bool forever
; /* retry opening forever */
127 struct timespec intervall
; /* wait so long between retries */
128 #endif /* WITH_RETRY */
129 bool ignoreeof
; /* option ignoreeof; do not pass eof condition to app*/
130 int eof
; /* 1..exec'd child has died, but no explicit eof
132 2..fd0 has reached EOF (definitely; never with
134 size_t wsize
; /* write always this size; 0..all available */
135 size_t readbytes
; /* read only so many bytes; 0...unlimited */
136 size_t actbytes
; /* so many bytes still to be read (when readbytes!=0)*/
137 xiolock_t lock
; /* parameters of lockfile */
138 bool havelock
; /* we are happy owner of the above lock */
139 bool cool_write
; /* downlevel EPIPE, ECONNRESET to notice */
140 /* until here, keep consistent with bipipe.dual ! */
141 int argc
; /* number of fields in argv */
142 const char *argv
[MAXARGV
]; /* address keyword, required args */
143 struct opt
*opts
; /* the options of this address */
144 int lineterm
; /* 0..dont touch; 1..CR; 2..CRNL on extern data */
146 bool opt_unlink_close
; /* option unlink_close */
147 char *unlink_close
; /* name of a symlink or unix socket to be removed */
150 XIOSHUT_UNSPEC
, /* standard (address dependent) behaviour */
151 XIOSHUT_NONE
, /* do nothing on shutdown */
152 XIOSHUT_CLOSE
, /* close the FD */
153 XIOSHUT_DOWN
, /* shutdown() */
154 XIOSHUT_NULL
/* send an empty packet (dgram socket) */
157 END_UNSPEC
, /* after init, when no end-close... option */
158 END_NONE
, /* no action */
159 END_CLOSE
, /* close() */
160 END_SHUTDOWN
, /* shutdown() */
161 END_KILL
, /* has subprocess */
162 END_CLOSE_KILL
, /* first close fd, then kill subprocess */
163 END_SHUTDOWN_KILL
/* first shutdown fd, then kill subprocess */
166 union sockaddr_union peersa
;
168 #endif /* _WITH_SOCKET */
170 bool ttyvalid
; /* the following struct is valid */
171 struct termios savetty
; /* save orig tty settings for later restore */
172 #endif /* WITH_TERMIOS */
173 int (*sigchild
)(struct single
*); /* callback after sigchild */
174 pid_t ppid
; /* parent pid, only if we send it signals */
175 int escape
; /* escape character; -1 for no escape */
176 bool actescape
; /* escape character found in input data */
179 int fdout
; /* use fd for output */
183 struct timeval connect_timeout
; /* how long to hang in connect() */
184 union sockaddr_union la
; /* local socket address */
185 bool null_eof
; /* with dgram: empty packet means EOF */
187 struct xiorange range
; /* restrictions for peer address */
188 #if _WITH_IP4 || _WITH_IP6
190 unsigned int res_opts
[2]; /* bits to be set in _res.options are
191 at [0], bits to be cleared are at [1] */
193 uint16_t sourceport
; /* host byte order */
195 #if (WITH_TCP || WITH_UDP) && WITH_LIBWRAP
199 char *hosts_allow_table
;
200 char *hosts_deny_table
;
203 #endif /* _WITH_IP4 || _WITH_IP6 */
208 #endif /* WITH_UNIX */
210 #endif /* _WITH_SOCKET */
212 pid_t pid
; /* child PID, with EXEC: */
213 int fdout
; /* use fd for output if two pipes */
218 char *prompt
; /* static prompt, passed to readline() */
219 size_t dynbytes
; /* length of buffer for dynamic prompt */
220 char *dynprompt
; /* the dynamic prompt */
221 char *dynend
; /* current end of dynamic prompt */
223 bool hasnoecho
; /* following regex is set */
224 regex_t noecho
; /* if it matches the prompt, input is silent */
227 #endif /* WITH_READLINE */
230 struct timeval connect_timeout
; /* how long to hang in connect() */
234 #endif /* WITH_OPENSSL */
237 short iff_opts
[2]; /* ifr flags, using OFUNC_OFFSET_MASKS */
239 #endif /* WITH_TUN */
243 /* rw: 0..read, 1..write, 2..r/w */
244 /* when implementing a new address type take care of following topics:
245 . be aware that xioopen_single is used for O_RDONLY, O_WRONLY, and O_RDWR data
246 . which options are allowed (option groups)
247 . implement application of all these options
248 . set FD_CLOEXEC on new file descriptors BEFORE the cloexec option might be
253 typedef union bipipe
{
257 const struct addrdesc
*addr
;
260 struct single stream
;
263 const struct addrdesc
*addr
;
264 int flags
; /* compatible to fcntl(.., F_GETFL, ..) */
266 unsigned retry
; /* retry opening this many times */
267 bool forever
; /* retry opening forever */
268 struct timespec intervall
; /* wait so long between retries */
269 #endif /* WITH_RETRY */
271 int eof
; /* fd0 has reached EOF */
272 size_t wsize
; /* write always this size; 0..all available */
273 size_t readbytes
; /* read only so many bytes; 0...unlimited */
274 size_t actbytes
; /* so many bytes still to be read */
275 xiolock_t lock
; /* parameters of lockfile */
276 bool havelock
; /* we are happy owner of the above lock */
277 xiosingle_t
*stream
[2]; /* input stream, output stream */
283 const char *defname
; /* main (canonical) name of address */
284 int directions
; /* 1..read, 2..write, 3..both */
285 int (*func
)(int argc
, const char *argv
[], struct opt
*opts
, int rw
, xiofile_t
*fd
, unsigned groups
,
286 int arg1
, int arg2
, int arg3
);
296 #define XIO_WRITABLE(s) (((s)->common.flags+1)&2)
297 #define XIO_READABLE(s) (((s)->common.flags+1)&1)
298 #define XIO_RDSTREAM(s) (((s)->tag==XIO_TAG_DUAL)?(s)->dual.stream[0]:&(s)->stream)
299 #define XIO_WRSTREAM(s) (((s)->tag==XIO_TAG_DUAL)?(s)->dual.stream[1]:&(s)->stream)
300 #define XIO_GETRDFD(s) (((s)->tag==XIO_TAG_DUAL)?(s)->dual.stream[0]->fd:(s)->stream.fd)
301 #define XIO_GETWRFD(s) (((s)->tag==XIO_TAG_DUAL)?(s)->dual.stream[1]->fd:(((s)->stream.dtype&XIODATA_WRITEMASK)==XIOWRITE_2PIPE)?(s)->stream.para.exec.fdout:(((s)->stream.dtype&XIODATA_WRITEMASK)==XIOWRITE_PIPE)?(s)->stream.para.bipipe.fdout:(s)->stream.fd)
302 #define XIO_EOF(s) (XIO_RDSTREAM(s)->eof && !XIO_RDSTREAM(s)->ignoreeof)
304 typedef unsigned long flags_t
;
312 #if HAVE_TYPE_LONGLONG
313 long long u_longlong
;
322 unsigned long u_ulong
;
323 unsigned short u_ushort
;
331 struct timeval u_timeval
;
332 #if HAVE_STRUCT_LINGER
333 struct linger u_linger
;
334 #endif /* HAVE_STRUCT_LINGER */
335 #if HAVE_STRUCT_TIMESPEC
336 struct timespec u_timespec
;
337 #endif /* HAVE_STRUCT_TIMESPEC */
338 #if HAVE_STRUCT_IP_MREQ || HAVE_STRUCT_IP_MREQN
341 char *param2
; /* address, interface */
342 #if HAVE_STRUCT_IP_MREQN
343 char ifindex
[IF_NAMESIZE
+1];
348 struct in_addr u_ip4addr
;
354 #if HAVE_BASIC_OFF_T==3
356 #elif HAVE_BASIC_OFF_T==5
357 # define u_off u_long
358 #elif HAVE_BASIC_OFF_T==7
359 # define u_off u_longlong
361 # error "unexpected size of off_t, please report this as bug"
364 #if defined(HAVE_BASIC_OFF64_T) && HAVE_BASIC_OFF64_T
365 # if HAVE_BASIC_OFF64_T==5
366 # define u_off64 u_long
367 # elif HAVE_BASIC_OFF64_T==7
368 # define u_off64 u_longlong
370 # error "unexpected size of off64_t, please report this as bug"
372 #endif /* defined(HAVE_BASIC_OFF64_T) && HAVE_BASIC_OFF64_T */
375 /* this handles option instances, for communication between subroutines */
377 const struct optdesc
*desc
;
378 union integral value
;
379 union integral value2
;
380 union integral value3
;
383 extern const char *PIPESEP
;
384 extern xiofile_t
*sock
[XIO_MAXSOCK
];
386 extern int num_child
;
388 /* return values of xioopensingle */
390 #define STAT_WARNING 1
392 #define STAT_NOACTION 3 /* by retropt_* when option not applied */
393 #define STAT_RETRYNOW -1 /* only after timeouts useful ? */
394 #define STAT_RETRYLATER -2 /* address cannot be opened, but user might
395 change something in the filesystem etc. to
396 make this process succeed later. */
397 #define STAT_NORETRY -3 /* address syntax error, not implemented etc;
398 not even by external changes correctable */
400 extern int xioinitialize(void);
401 extern int xioinitialize2(void);
402 extern pid_t
xio_fork(bool subchild
, int level
);
403 extern int xio_forked_inchild(void);
404 extern int xiosetopt(char what
, const char *arg
);
405 extern int xioinqopt(char what
, char *arg
, size_t n
);
406 extern xiofile_t
*xioopen(const char *args
, int flags
);
407 extern int xioopensingle(char *addr
, struct single
*xfd
, int xioflags
);
408 extern int xioopenhelp(FILE *of
, int level
);
410 /* must be outside function for use by childdied handler */
411 extern xiofile_t
*sock1
, *sock2
;
413 extern pid_t diedunknown
[NUMUNKNOWN
]; /* child died before it is registered */
414 #define diedunknown1 (diedunknown[0])
415 #define diedunknown2 (diedunknown[1])
416 #define diedunknown3 (diedunknown[2])
417 #define diedunknown4 (diedunknown[3])
419 extern int xiosetsigchild(xiofile_t
*xfd
, int (*callback
)(struct single
*));
420 extern int xiosetchilddied(void);
421 extern int xio_opt_signal(pid_t pid
, int signum
);
422 extern void childdied(int signum
);
424 extern ssize_t
xioread(xiofile_t
*sock1
, void *buff
, size_t bufsiz
);
425 extern ssize_t
xiopending(xiofile_t
*sock1
);
426 extern ssize_t
xiowrite(xiofile_t
*sock1
, const void *buff
, size_t bufsiz
);
427 extern int xioshutdown(xiofile_t
*sock
, int how
);
429 extern int xioclose(xiofile_t
*sock
);
430 extern void xioexit(void);
432 extern int (*xiohook_newchild
)(void); /* xio calls this function from a new child process */
434 #endif /* !defined(__xio_h_included) */