2 /* Copyright Gerhard Rieger 2001-2009 */
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 O_ACCMODE /* 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_UNLINK
, /* unlink() */
162 END_KILL
, /* has subprocess */
163 END_CLOSE_KILL
, /* first close fd, then kill subprocess */
164 END_SHUTDOWN_KILL
/* first shutdown fd, then kill subprocess */
167 union sockaddr_union peersa
;
169 #endif /* _WITH_SOCKET */
171 bool ttyvalid
; /* the following struct is valid */
172 struct termios savetty
; /* save orig tty settings for later restore */
173 #endif /* WITH_TERMIOS */
174 const char *name
; /* only with END_UNLINK */
175 int (*sigchild
)(struct single
*); /* callback after sigchild */
176 pid_t ppid
; /* parent pid, only if we send it signals */
177 int escape
; /* escape character; -1 for no escape */
178 bool actescape
; /* escape character found in input data */
181 int fdout
; /* use fd for output */
185 struct timeval connect_timeout
; /* how long to hang in connect() */
186 union sockaddr_union la
; /* local socket address */
187 bool null_eof
; /* with dgram: empty packet means EOF */
189 struct xiorange range
; /* restrictions for peer address */
190 #if _WITH_IP4 || _WITH_IP6
192 unsigned int res_opts
[2]; /* bits to be set in _res.options are
193 at [0], bits to be cleared are at [1] */
195 uint16_t sourceport
; /* host byte order */
197 #if (WITH_TCP || WITH_UDP) && WITH_LIBWRAP
201 char *hosts_allow_table
;
202 char *hosts_deny_table
;
205 #endif /* _WITH_IP4 || _WITH_IP6 */
207 #endif /* _WITH_SOCKET */
209 pid_t pid
; /* child PID, with EXEC: */
210 int fdout
; /* use fd for output if two pipes */
215 char *prompt
; /* static prompt, passed to readline() */
216 size_t dynbytes
; /* length of buffer for dynamic prompt */
217 char *dynprompt
; /* the dynamic prompt */
218 char *dynend
; /* current end of dynamic prompt */
220 bool hasnoecho
; /* following regex is set */
221 regex_t noecho
; /* if it matches the prompt, input is silent */
224 #endif /* WITH_READLINE */
227 struct timeval connect_timeout
; /* how long to hang in connect() */
231 #endif /* WITH_OPENSSL */
234 short iff_opts
[2]; /* ifr flags, using OFUNC_OFFSET_MASKS */
236 #endif /* WITH_TUN */
240 /* rw: 0..read, 1..write, 2..r/w */
241 /* when implementing a new address type take care of following topics:
242 . be aware that xioopen_single is used for O_RDONLY, O_WRONLY, and O_RDWR data
243 . which options are allowed (option groups)
244 . implement application of all these options
245 . set FD_CLOEXEC on new file descriptors BEFORE the cloexec option might be
250 typedef union bipipe
{
254 const struct addrdesc
*addr
;
257 struct single stream
;
260 const struct addrdesc
*addr
;
261 int flags
; /* compatible to fcntl(.., F_GETFL, ..) */
263 unsigned retry
; /* retry opening this many times */
264 bool forever
; /* retry opening forever */
265 struct timespec intervall
; /* wait so long between retries */
266 #endif /* WITH_RETRY */
268 int eof
; /* fd0 has reached EOF */
269 size_t wsize
; /* write always this size; 0..all available */
270 size_t readbytes
; /* read only so many bytes; 0...unlimited */
271 size_t actbytes
; /* so many bytes still to be read */
272 xiolock_t lock
; /* parameters of lockfile */
273 bool havelock
; /* we are happy owner of the above lock */
274 xiosingle_t
*stream
[2]; /* input stream, output stream */
280 const char *defname
; /* main (canonical) name of address */
281 int directions
; /* 1..read, 2..write, 3..both */
282 int (*func
)(int argc
, const char *argv
[], struct opt
*opts
, int rw
, xiofile_t
*fd
, unsigned groups
,
283 int arg1
, int arg2
, int arg3
);
293 #define XIO_WRITABLE(s) (((s)->common.flags+1)&2)
294 #define XIO_READABLE(s) (((s)->common.flags+1)&1)
295 #define XIO_RDSTREAM(s) (((s)->tag==XIO_TAG_DUAL)?(s)->dual.stream[0]:&(s)->stream)
296 #define XIO_WRSTREAM(s) (((s)->tag==XIO_TAG_DUAL)?(s)->dual.stream[1]:&(s)->stream)
297 #define XIO_GETRDFD(s) (((s)->tag==XIO_TAG_DUAL)?(s)->dual.stream[0]->fd:(s)->stream.fd)
298 #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)
299 #define XIO_EOF(s) (XIO_RDSTREAM(s)->eof && !XIO_RDSTREAM(s)->ignoreeof)
301 typedef unsigned long flags_t
;
309 #if HAVE_TYPE_LONGLONG
310 long long u_longlong
;
319 unsigned long u_ulong
;
320 unsigned short u_ushort
;
328 struct timeval u_timeval
;
329 #if HAVE_STRUCT_LINGER
330 struct linger u_linger
;
331 #endif /* HAVE_STRUCT_LINGER */
332 #if HAVE_STRUCT_TIMESPEC
333 struct timespec u_timespec
;
334 #endif /* HAVE_STRUCT_TIMESPEC */
335 #if HAVE_STRUCT_IP_MREQ || HAVE_STRUCT_IP_MREQN
338 char *param2
; /* address, interface */
339 #if HAVE_STRUCT_IP_MREQN
340 char ifindex
[IF_NAMESIZE
+1];
345 struct in_addr u_ip4addr
;
350 #define u_off u_long /* please report when this causes problems */
352 #if defined(HAVE_BASIC_OFF64_T) && HAVE_BASIC_OFF64_T
353 # if HAVE_BASIC_OFF64_T==5
354 # define u_off64 u_long
355 # elif HAVE_BASIC_OFF64_T==7
356 # define u_off64 u_longlong
358 # error "unexpected size of off64_t, please report this as bug"
360 #endif /* defined(HAVE_BASIC_OFF64_T) && HAVE_BASIC_OFF64_T */
363 /* this handles option instances, for communication between subroutines */
365 const struct optdesc
*desc
;
366 union integral value
;
367 union integral value2
;
368 union integral value3
;
371 extern const char *PIPESEP
;
372 extern xiofile_t
*sock
[XIO_MAXSOCK
];
374 extern int num_child
;
376 /* return values of xioopensingle */
378 #define STAT_WARNING 1
380 #define STAT_NOACTION 3 /* by retropt_* when option not applied */
381 #define STAT_RETRYNOW -1 /* only after timeouts useful ? */
382 #define STAT_RETRYLATER -2 /* address cannot be opened, but user might
383 change something in the filesystem etc. to
384 make this process succeed later. */
385 #define STAT_NORETRY -3 /* address syntax error, not implemented etc;
386 not even by external changes correctable */
388 extern int xioinitialize(void);
389 extern int xioinitialize2(void);
390 extern pid_t
xio_fork(bool subchild
, int level
);
391 extern int xio_forked_inchild(void);
392 extern int xiosetopt(char what
, const char *arg
);
393 extern int xioinqopt(char what
, char *arg
, size_t n
);
394 extern xiofile_t
*xioopen(const char *args
, int flags
);
395 extern int xioopensingle(char *addr
, struct single
*xfd
, int xioflags
);
396 extern int xioopenhelp(FILE *of
, int level
);
398 /* must be outside function for use by childdied handler */
399 extern xiofile_t
*sock1
, *sock2
;
400 extern pid_t diedunknown1
; /* child died before it is registered */
401 extern pid_t diedunknown2
;
402 extern pid_t diedunknown3
;
403 extern pid_t diedunknown4
;
405 extern int xiosetsigchild(xiofile_t
*xfd
, int (*callback
)(struct single
*));
406 extern int xiosetchilddied(void);
407 extern int xio_opt_signal(pid_t pid
, int signum
);
408 extern void childdied(int signum
);
410 extern ssize_t
xioread(xiofile_t
*sock1
, void *buff
, size_t bufsiz
);
411 extern ssize_t
xiopending(xiofile_t
*sock1
);
412 extern ssize_t
xiowrite(xiofile_t
*sock1
, const void *buff
, size_t bufsiz
);
413 extern int xioshutdown(xiofile_t
*sock
, int how
);
415 extern int xioclose(xiofile_t
*sock
);
416 extern void xioexit(void);
418 extern int (*xiohook_newchild
)(void); /* xio calls this function from a new child process */
420 #endif /* !defined(__xio_h_included) */