/* source: xio.h */ /* Copyright Gerhard Rieger 2001-2008 */ /* Published under the GNU General Public License V.2, see file COPYING */ #ifndef __xio_h_included #define __xio_h_included 1 #if 1 /*!*/ #include "mytypes.h" #include "sysutils.h" #endif #define XIO_MAXSOCK 2 /* Linux 2.2.10 */ #define HAVE_STRUCT_LINGER 1 #define LINETERM_RAW 0 #define LINETERM_CR 1 #define LINETERM_CRNL 2 union xioaddr_desc; struct opt; /* the flags argument of xioopen */ #define XIO_RDONLY O_RDONLY /* asserted to be 0 */ #define XIO_WRONLY O_WRONLY /* asserted to be 1 */ #define XIO_RDWR O_RDWR /* asserted to be 2 */ #define XIO_ACCMODE O_ACCMODE /* must be 3 */ /* 3 is undefined */ #define XIO_MAYFORK 4 /* address is allowed to fork the program (fork), especially with listen and connect addresses */ #define XIO_MAYCHILD 8 /* address is allowed to fork off a child that exec's another program or calls system() */ #define XIO_MAYEXEC 16 /* address is allowed to exec a prog (exec+nofork) */ #define XIO_MAYCONVERT 32 /* address is allowed to perform modifications on the stream data, e.g. SSL, READLINE; CRLF */ #define XIO_MAYCHAIN 64 /* address is allowed to consist of a chain of subaddresses that are handled by socat subprocesses */ #define XIO_EMBEDDED 256 /* address is nonterminal */ #define XIO_MAYALL INT_MAX /* all features enabled */ /* the status flags of xiofile_t */ #define XIO_DOESFORK XIO_MAYFORK #define XIO_DOESCHILD XIO_MAYCHILD #define XIO_DOESEXEC XIO_MAYEXEC #define XIO_DOESCONVERT XIO_MAYCONVERT #define XIO_DOESCHAIN XIO_MAYCHAIN /* sometimes we use a set of allowed direction(s), a bit pattern */ #define XIOBIT_RDONLY (1<common.flags+1)&2) #define XIO_READABLE(s) (((s)->common.flags+1)&1) #define XIO_RDSTREAM(s) (((s)->tag==XIO_TAG_DUAL)?(s)->dual.stream[0]:&(s)->stream) #define XIO_WRSTREAM(s) (((s)->tag==XIO_TAG_DUAL)?(s)->dual.stream[1]:&(s)->stream) #define XIO_GETRDFD(s) (((s)->tag==XIO_TAG_DUAL)?(s)->dual.stream[0]->fd1:(s)->stream.fd1) #define _XIO_GETWRFD(s) (((s)->fdtype==FDTYPE_DOUBLE)?(s)->fd2:(s)->fd1) #define XIO_GETWRFD(s) (((s)->tag==XIO_TAG_DUAL)?_XIO_GETWRFD((s)->dual.stream[1]):_XIO_GETWRFD(&(s)->stream)) #define XIO_EOF(s) (XIO_RDSTREAM(s)->eof && !XIO_RDSTREAM(s)->ignoreeof) typedef unsigned long flags_t; union integral { int u_bool; uint8_t u_byte; gid_t u_gidt; int u_int; long u_long; #if HAVE_TYPE_LONGLONG long long u_longlong; #endif double u_double; mode_t u_modet; short u_short; size_t u_sizet; char *u_string; uid_t u_uidt; unsigned int u_uint; unsigned long u_ulong; unsigned short u_ushort; uint16_t u_2bytes; void *u_ptr; flags_t u_flag; struct { uint8_t *b_data; size_t b_len; } u_bin; struct timeval u_timeval; #if HAVE_STRUCT_LINGER struct linger u_linger; #endif /* HAVE_STRUCT_LINGER */ #if HAVE_STRUCT_TIMESPEC struct timespec u_timespec; #endif /* HAVE_STRUCT_TIMESPEC */ #if HAVE_STRUCT_IP_MREQ || HAVE_STRUCT_IP_MREQN struct { char *multiaddr; char *param2; /* address, interface */ #if HAVE_STRUCT_IP_MREQN char ifindex[IF_NAMESIZE+1]; #endif } u_ip_mreq; #endif #if WITH_IP4 struct in_addr u_ip4addr; #endif } ; /* some aliases */ #define u_off u_long /* please report when this causes problems */ #if defined(HAVE_BASIC_OFF64_T) && HAVE_BASIC_OFF64_T # if HAVE_BASIC_OFF64_T==5 # define u_off64 u_long # elif HAVE_BASIC_OFF64_T==7 # define u_off64 u_longlong # else # error "unexpected size of off64_t, please report this as bug" # endif #endif /* defined(HAVE_BASIC_OFF64_T) && HAVE_BASIC_OFF64_T */ /* this handles option instances, for communication between subroutines */ struct opt { const struct optdesc *desc; union integral value; union integral value2; union integral value3; } ; /* with threading, the arguments indirectly passed to xioengine() */ struct threadarg_struct { xiofile_t *xfd1; xiofile_t *xfd2; } ; extern const char *PIPESEP; extern xiofile_t *sock[XIO_MAXSOCK]; /* return values of xioopensingle */ #define STAT_OK 0 #define STAT_WARNING 1 #define STAT_EXIT 2 #define STAT_NOACTION 3 /* by retropt_* when option not applied */ #define STAT_RETRYNOW -1 /* only after timeouts useful ? */ #define STAT_RETRYLATER -2 /* address cannot be opened, but user might change something in the filesystem etc. to make this process succeed later. */ #define STAT_NORETRY -3 /* address syntax error, not implemented etc; not even by external changes correctable */ extern int xioinitialize(int xioflags); extern int xioinitialize2(void); extern pid_t xio_fork(bool subchild, int level); extern int xio_forked_inchild(void); extern int xiosetopt(char what, const char *arg); extern int xioinqopt(char what, char *arg, size_t n); extern xiofile_t *xioopen(const char *args, int xioflags); extern xiofile_t *xioopenx(const char *addr, int xioflags, int infd, int outfd); extern int xiosocketpair2(xiofile_t **xfd1p, xiofile_t **xfd2p, int how, ...); extern int xioopensingle(char *addr, xiosingle_t *fd, int xioflags); extern int xioopenhelp(FILE *of, int level); /* must be outside function for use by childdied handler */ extern xiofile_t *xioallocfd(void); extern void xiofreefd(xiofile_t *xfd); extern int xiosetsigchild(xiofile_t *xfd, int (*callback)(struct single *)); extern int xiosetchilddied(void); extern int xio_opt_signal(pid_t pid, int signum); extern void *xioengine(void *thread_arg); extern int _socat(xiofile_t *xfd1, xiofile_t *xfd2); extern ssize_t xioread(xiofile_t *sock1, void *buff, size_t bufsiz); extern ssize_t xiopending(xiofile_t *sock1); extern ssize_t xiowrite(xiofile_t *sock1, const void *buff, size_t bufsiz); extern int xiotransfer(xiofile_t *inpipe, xiofile_t *outpipe, unsigned char **buff, size_t bufsiz, bool righttoleft); extern int xioshutdown(xiofile_t *sock, int how); extern int xioclose(xiofile_t *sock); extern void xioexit(void); extern int (*xiohook_newchild)(void); /* xio calls this function from a new child process */ extern int socat_sigchild(struct single *file); extern xioopts_t xioopts, *xioparams; #endif /* !defined(__xio_h_included) */