File:  [ELWIX - Embedded LightWeight unIX -] / libaitio / inc / aitio.h
Revision 1.22.2.5: download - view: text, annotated - select for diffs - revision graph
Mon Jul 30 11:20:07 2012 UTC (11 years, 11 months ago) by misho
Branches: io3_4
Diff to: branchpoint 1.22: preferred, unified
fix output types

    1: /*************************************************************************
    2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
    3: *  by Michael Pounov <misho@elwix.org>
    4: *
    5: * $Author: misho $
    6: * $Id: aitio.h,v 1.22.2.5 2012/07/30 11:20:07 misho Exp $
    7: *
    8: **************************************************************************
    9: The ELWIX and AITNET software is distributed under the following
   10: terms:
   11: 
   12: All of the documentation and software included in the ELWIX and AITNET
   13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
   14: 
   15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
   16: 	by Michael Pounov <misho@elwix.org>.  All rights reserved.
   17: 
   18: Redistribution and use in source and binary forms, with or without
   19: modification, are permitted provided that the following conditions
   20: are met:
   21: 1. Redistributions of source code must retain the above copyright
   22:    notice, this list of conditions and the following disclaimer.
   23: 2. Redistributions in binary form must reproduce the above copyright
   24:    notice, this list of conditions and the following disclaimer in the
   25:    documentation and/or other materials provided with the distribution.
   26: 3. All advertising materials mentioning features or use of this software
   27:    must display the following acknowledgement:
   28: This product includes software developed by Michael Pounov <misho@elwix.org>
   29: ELWIX - Embedded LightWeight unIX and its contributors.
   30: 4. Neither the name of AITNET nor the names of its contributors
   31:    may be used to endorse or promote products derived from this software
   32:    without specific prior written permission.
   33: 
   34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
   35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   44: SUCH DAMAGE.
   45: */
   46: #ifndef __AITIO_H
   47: #define __AITIO_H
   48: 
   49: 
   50: #define COMPAT_43TTY
   51: 
   52: #include <assert.h>
   53: #include <openssl/evp.h>
   54: #include <openssl/aes.h>
   55: #include <sys/tty.h>
   56: #include <sys/ioctl_compat.h>
   57: #include <sys/socket.h>
   58: #include <sys/un.h>
   59: #include <net/if_dl.h>
   60: #include <netinet/in.h>
   61: #include <atree.h>
   62: #include <ampool.h>
   63: 
   64: 
   65: #ifndef STRSIZ
   66: #define STRSIZ		256
   67: #endif
   68: 
   69: #ifndef be16toh
   70: #define be16toh		betoh16
   71: #endif
   72: #ifndef be32toh
   73: #define be32toh		betoh32
   74: #endif
   75: #ifndef be64toh
   76: #define be64toh		betoh64
   77: #endif
   78: #ifndef le16toh
   79: #define le16toh		letoh16
   80: #endif
   81: #ifndef le32toh
   82: #define le32toh		letoh32
   83: #endif
   84: #ifndef le64toh
   85: #define le64toh		letoh64
   86: #endif
   87: 
   88: #define IO_SYSM		0
   89: #define IO_MPOOL	1
   90: 
   91: #define VACUUM_LEFT	1
   92: #define VACUUM_BETWEEN	2
   93: 
   94: /* AIT arrays */
   95: 
   96: typedef void ** sarr_seg_t;
   97: typedef struct _tagSplitArray {
   98: 	int		sarr_num;
   99: 	int		sarr_seg;
  100: 	int		sarr_siz;
  101: 	sarr_seg_t	*sarr_data;
  102: } sarr_t;
  103: 
  104: typedef struct _tagArray {
  105: 	int	arr_last;
  106: 	int	arr_num;
  107: 	void	**arr_data;
  108: } array_t;
  109: 
  110: /* AIT RPC variables and managment */
  111: 
  112: typedef enum {
  113: 	empty, ptr, data, 		/* empty -> variable is not set; ptr -> void*; data -> data after struct  */
  114: 	buffer, string, blob, 		/* buffer -> uint8_t*; string -> int8_t*; blob -> uint32_t blobID(+socket); */
  115: 	f32, f64, 			/* float -> f32; double -> f64 */
  116: 	u8, u16, u32, u64,		/* unsigned integers ... */
  117: 	i8, i16, i32, i64,		/* integers ... */
  118: } ait_type_t;
  119: 
  120: typedef struct {
  121: 	uint8_t		val_type;
  122: 	union {
  123: 		struct {
  124: 			uint8_t	val_in:1;
  125: 			uint8_t	val_be:1;
  126: 			uint8_t	val_le:1;
  127: 			uint8_t val_pad:5;
  128: 		};
  129: 		uint8_t		val_opt;
  130: 	};
  131: 	uint16_t	val_key;
  132: 	uint32_t	val_len;
  133: 	union {
  134: 		uint64_t	net;
  135: 
  136: 		void		*ptr;
  137: 		uint8_t		*buffer;
  138: 		int8_t		*string;
  139: 		uint32_t	blob;
  140: 		float		f32;
  141: 		double		f64;
  142: 		uint8_t		u8;
  143: 		uint16_t	u16;
  144: 		uint32_t	u32;
  145: 		uint64_t	u64;
  146: 		int8_t		i8;
  147: 		int16_t		i16;
  148: 		int32_t		i32;
  149: 		int64_t		i64;
  150: 	} val;
  151: 	uint8_t		val_data[0];
  152: } __packed ait_val_t;	/* sizeof 16 bytes */
  153: 
  154: #define AIT_TYPE(_vl)			((ait_type_t) (_vl)->val_type)
  155: #define AIT_LEN(_vl)			(_vl)->val_len
  156: #define AIT_KEY(_vl)			(_vl)->val_key
  157: #define AIT_RAW(_vl)			(_vl)->val.net
  158: #define AIT_ADDR(_vl)			(_vl)->val.ptr
  159: #define AIT_IN(_vl)			(_vl)->val_in
  160: #define AIT_BE(_vl)			(_vl)->val_be
  161: #define AIT_LE(_vl)			(_vl)->val_le
  162: #define AIT_BLOB_CHUNKS(_vl, _n)	(AIT_LEN((_vl)) / _n + (AIT_LEN((_vl)) % _n) ? 1 : 0)
  163: #define AIT_ISEMPTY(_vl)		(AIT_TYPE((_vl)) == empty)
  164: 
  165: #define AIT_GET_LIKE(_vl, _type)	((_type) AIT_ADDR((_vl)))
  166: 
  167: #define AIT_GET_PTR(_vl)		(assert(AIT_TYPE((_vl)) == ptr), (_vl)->val.ptr)
  168: #define AIT_GET_DATA(_vl)		(assert(AIT_TYPE((_vl)) == data), (_vl)->val_data)
  169: #define AIT_GET_BUF(_vl)		(assert(AIT_TYPE((_vl)) == buffer), (_vl)->val.buffer)
  170: #define AIT_GET_STR(_vl)		(assert(AIT_TYPE((_vl)) == string), (char*) (_vl)->val.string)
  171: #define AIT_GET_BLOB(_vl)		(assert(AIT_TYPE((_vl)) == blob), (_vl)->val.blob)
  172: #define AIT_GET_U8(_vl)			(assert(AIT_TYPE((_vl)) == u8), (_vl)->val.u8)
  173: #define AIT_GET_U16(_vl)		(assert(AIT_TYPE((_vl)) == u16), (_vl)->val.u16)
  174: #define AIT_GET_U32(_vl)		(assert(AIT_TYPE((_vl)) == u32), (_vl)->val.u32)
  175: #define AIT_GET_U64(_vl)		(assert(AIT_TYPE((_vl)) == u64), (_vl)->val.u64)
  176: #define AIT_GET_I8(_vl)			(assert(AIT_TYPE((_vl)) == i8), (_vl)->val.i8)
  177: #define AIT_GET_I16(_vl)		(assert(AIT_TYPE((_vl)) == i16), (_vl)->val.i16)
  178: #define AIT_GET_I32(_vl)		(assert(AIT_TYPE((_vl)) == i32), (_vl)->val.i32)
  179: #define AIT_GET_I64(_vl)		(assert(AIT_TYPE((_vl)) == i64), (_vl)->val.i64)
  180: #define AIT_GET_F32(_vl)		(assert(AIT_TYPE((_vl)) == f32), (_vl)->val.f32)
  181: #define AIT_GET_F64(_vl)		(assert(AIT_TYPE((_vl)) == f64), (_vl)->val.f64)
  182: 
  183: #define AIT_SET_DATA(_vl, _p, _len)	do { ait_val_t *__val = (_vl); \
  184: 						__val = io_realloc(__val, sizeof(ait_val_t) + _len); \
  185: 						if (__val) { \
  186: 							__val->val_type = data; AIT_LEN(__val) = _len; \
  187: 							if ((_p)) \
  188: 								memcpy(__val->val_data, (_p), _len); \
  189: 						} \
  190: 						(_vl) = __val; \
  191: 					} while (0);
  192: #define AIT_SET_PTR(_vl, _p, _len)	do { ait_val_t *__val = (_vl); assert(__val); \
  193: 						__val->val_type = ptr; __val->val.ptr = _p; \
  194: 						AIT_LEN(__val) = _len; } while (0)
  195: #define AIT_RE_BUF(_vl, _len)		do { ait_val_t *__val = (_vl); assert(__val); \
  196: 						void *__ptr = io_realloc(AIT_GET_BUF(__val), _len); \
  197: 						if (__ptr) { \
  198: 							__val->val.buffer = __ptr; AIT_LEN(__val) = _len; \
  199: 						} } while (0)
  200: #define AIT_SET_BUF2(_vl, _c, _len)	do { ait_val_t *__val = (_vl); assert(__val); \
  201: 						__val->val.buffer = io_malloc(_len); \
  202: 						if (__val->val.buffer) { \
  203: 							__val->val_type = buffer; AIT_LEN(__val) = _len; \
  204: 							memset(__val->val.buffer, _c, _len); \
  205: 						} } while (0)
  206: #define AIT_SET_BUF(_vl, _v, _len)	do { ait_val_t *__val = (_vl); assert(__val); \
  207: 						__val->val.buffer = io_malloc(_len); \
  208: 						if (__val->val.buffer) { \
  209: 							__val->val_type = buffer; AIT_LEN(__val) = _len; \
  210: 							if ((_v)) \
  211: 								memcpy(__val->val.buffer, (_v), _len); \
  212: 							else \
  213: 								memset(__val->val.buffer, 0, _len); \
  214: 						} } while (0)
  215: #define AIT_SET_STR(_vl, _v)		do { ait_val_t *__val = (_vl); assert(__val); \
  216: 						__val->val.string = (int8_t*) io_strdup((_v)); \
  217: 						if (__val->val.string) { \
  218: 							__val->val_type = string; \
  219: 							AIT_LEN(__val) = strlen((_v)) + 1; \
  220: 						} } while (0)
  221: #define AIT_SET_STRCAT(_vl, _v)		do { ait_val_t *__val = (_vl); assert(__val); \
  222: 						assert(AIT_TYPE(__val) == string); \
  223: 						void *__p = io_realloc(__val->val.string, \
  224: 								AIT_LEN(__val) + strlen((_v))); \
  225: 						if (__p) { \
  226: 							__val->val.string = __p; \
  227: 							AIT_LEN(__val) += strlen((_v)); \
  228: 							strlcat((char*) __val->val.string, (_v), \
  229: 									AIT_LEN(__val)); \
  230: 						} } while (0)
  231: #define AIT_SET_STRCPY(_vl, _v)		do { ait_val_t *__val = (_vl); assert(__val); \
  232: 						assert(AIT_TYPE(__val) == string); \
  233: 						void *__p = io_realloc(__val->val.string, \
  234: 								strlen((_v)) + 1); \
  235: 						if (__p) { \
  236: 							__val->val.string = __p; \
  237: 							AIT_LEN(__val) = strlen((_v)) + 1; \
  238: 							strlcpy((char*) __val->val.string, (_v), \
  239: 									AIT_LEN(__val)); \
  240: 						} } while (0)
  241: #define AIT_SET_BLOB(_vl, _n, _len)	do { ait_val_t *__val = (_vl); assert(__val); \
  242: 						__val->val_type = blob; __val->val.blob = _n; \
  243: 						AIT_LEN(__val) = _len; } while (0)
  244: #define AIT_SET_BLOB2(_vl, _bv)		do { ait_val_t *__val = (_vl); assert(__val); assert((_bv)); \
  245: 						__val->val_type = blob; AIT_LEN(__val) = \
  246: 							(_bv)->blob_len; \
  247: 						__val->val.blob = (_bv)->blob_var; } while (0)
  248: #define AIT_NEW_BLOB(_vl, _len)		AIT_SET_BLOB((_vl), 0, _len)
  249: 
  250: #define AIT_SET_U8(_vl, _n)		do { ait_val_t *__val = (_vl); assert(__val); \
  251: 						__val->val_type = u8; __val->val.u8 = _n; \
  252: 						AIT_LEN(__val) = sizeof(uint8_t); } while (0)
  253: #define AIT_SET_U16(_vl, _n)		do { ait_val_t *__val = (_vl); assert(__val); \
  254: 						__val->val_type = u16; __val->val.u16 = _n; \
  255: 						AIT_LEN(__val) = sizeof(uint16_t); } while (0)
  256: #define AIT_SET_U32(_vl, _n)		do { ait_val_t *__val = (_vl); assert(__val); \
  257: 						__val->val_type = u32; __val->val.u32 = _n; \
  258: 						AIT_LEN(__val) = sizeof(uint32_t); } while (0)
  259: #define AIT_SET_U64(_vl, _n)		do { ait_val_t *__val = (_vl); assert(__val); \
  260: 						__val->val_type = u64; __val->val.u64 = _n; \
  261: 						AIT_LEN(__val) = sizeof(uint64_t); } while (0)
  262: #define AIT_SET_I8(_vl, _n)		do { ait_val_t *__val = (_vl); assert(__val); \
  263: 						__val->val_type = i8; __val->val.i8 = _n; \
  264: 						AIT_LEN(__val) = sizeof(int8_t); } while (0)
  265: #define AIT_SET_I16(_vl, _n)		do { ait_val_t *__val = (_vl); assert(__val); \
  266: 						__val->val_type = i16; __val->val.i16 = _n; \
  267: 						AIT_LEN(__val) = sizeof(int16_t); } while (0)
  268: #define AIT_SET_I32(_vl, _n)		do { ait_val_t *__val = (_vl); assert(__val); \
  269: 						__val->val_type = i32; __val->val.i32 = _n; \
  270: 						AIT_LEN(__val) = sizeof(int32_t); } while (0)
  271: #define AIT_SET_I64(_vl, _n)		do { ait_val_t *__val = (_vl); assert(__val); \
  272: 						__val->val_type = i64; __val->val.i64 = _n; \
  273: 						AIT_LEN(__val) = sizeof(int64_t); } while (0)
  274: #define AIT_SET_F32(_vl, _n)		do { ait_val_t *__val = (_vl); assert(__val); \
  275: 						__val->val_type = f32; __val->val.f32 = _n; \
  276: 						AIT_LEN(__val) = sizeof(float); } while (0)
  277: #define AIT_SET_F64(_vl, _n)		do { ait_val_t *__val = (_vl); assert(__val); \
  278: 						__val->val_type = f64; __val->val.f64 = _n; \
  279: 						AIT_LEN(__val) = sizeof(double); } while (0)
  280: 
  281: #define AIT_COPY_VAL(_vl, _v)		do { memcpy((_vl), (_v), sizeof(ait_val_t)); \
  282: 						switch (AIT_TYPE((_vl))) { \
  283: 							case buffer: \
  284: 								AIT_SET_BUF((_vl), \
  285: 										AIT_GET_BUF((_v)), \
  286: 										AIT_LEN((_v))); \
  287: 								break; \
  288: 							case string: \
  289: 								AIT_SET_STR((_vl), \
  290: 										AIT_GET_STR((_v))); \
  291: 								break; \
  292: 							default: \
  293: 								break; \
  294: 						} \
  295: 					} while (0)
  296: #define AIT_COPY_VAL2(_vl, _v)		do { AIT_COPY_VAL((_vl), (_v)); \
  297: 						if (AIT_TYPE((_vl)) == data) \
  298: 							AIT_SET_DATA((_vl), AIT_GET_DATA((_v)), \
  299: 									AIT_LEN((_v))); \
  300: 					} while (0)
  301: 
  302: #define AIT_INIT_VAL(_vl)		(memset((_vl), 0, sizeof(ait_val_t)))
  303: #define AIT_INIT_VAL2(_vl, _t)		do { \
  304: 						AIT_INIT_VAL((_vl)); \
  305: 						(_vl)->val_type = _t; \
  306: 					} while (0)
  307: 					/* if attribute zeroCopy is set not execute io_free() */
  308: #define AIT_FREE_VAL(_vl)		do { ait_val_t *__val = (_vl); assert(__val); \
  309: 						switch (AIT_TYPE(__val)) { \
  310: 							case buffer: \
  311: 								if (!__val->val_in && \
  312: 										__val->val.buffer) \
  313: 									io_free(__val->val.buffer); \
  314: 								__val->val.buffer = NULL; \
  315: 								break; \
  316: 							case string: \
  317: 								if (!__val->val_in && \
  318: 										__val->val.string) \
  319: 									io_free(__val->val.string); \
  320: 								__val->val.string = NULL; \
  321: 								break; \
  322: 							default: \
  323: 								break; \
  324: 						} \
  325: 						__val->val_type = empty; \
  326: 						__val->val_opt ^= __val->val_opt; \
  327: 						AIT_LEN(__val) = 0; \
  328: 						AIT_KEY(__val) = 0; \
  329: 					} while (0)
  330: #define AIT_ZERO_VAL(_vl)		do { ait_val_t *__val = (_vl); assert(__val); \
  331: 						switch (AIT_TYPE(__val)) { \
  332: 							case buffer: \
  333: 							case string: \
  334: 								memset(__val->val.buffer, 0, \
  335: 										AIT_LEN(__val)); \
  336: 								break; \
  337: 							case data: \
  338: 								memset(__val->val_data, 0, \
  339: 										AIT_LEN(__val)); \
  340: 								break; \
  341: 							default: \
  342: 								__val->val.net = 0LL; \
  343: 								break; \
  344: 						} \
  345: 						AIT_KEY(__val) = 0; \
  346: 					} while (0)
  347: 
  348: struct io_ether_addr {
  349: 	u_int8_t ether_addr_octet[6];
  350: };
  351: typedef struct io_ether_addr io_ether_addr_t;
  352: 
  353: typedef union {
  354: 	struct sockaddr_storage	ss;
  355: 	struct sockaddr		sa;
  356: 	struct sockaddr_un	sun;
  357: 	struct sockaddr_in	sin;
  358: 	struct sockaddr_in6	sin6;
  359: 	struct sockaddr_dl	sdl;
  360: } io_sockaddr_t;
  361: #define IO_SOCKADDR_INIT	{ .ss = { 0 } }
  362: 
  363: 
  364: #define io_align(x, a)		(((x) + (a)) & ~(a))
  365: 
  366: 
  367: // io_GetErrno() Get error code of last operation
  368: inline int io_GetErrno();
  369: // io_GetError() Get error text of last operation
  370: inline const char *io_GetError();
  371: 
  372: 
  373: // io_mm_inuse() Check for memory management model
  374: inline int io_mm_inuse();
  375: 
  376: /*
  377:  * ioLibInit() - Init libaitio library memory management
  378:  *
  379:  * @mm = memory management (IO_SYSM or IO_MPOOL)
  380:  * @maxmem = memory limit
  381:  * return: -1 error or !=-1 used memory management model
  382:  */
  383: inline int ioLibInit(int mm, unsigned long maxmem);
  384: /*
  385:  * ioLibFini() - Finish libaitio library memory management
  386:  *
  387:  * return: none
  388:  */
  389: inline void ioLibFini();
  390: 
  391: /* memory management hooks */
  392: extern void *(*io_malloc)(size_t);
  393: extern void *(*io_calloc)(size_t, size_t);
  394: extern void *(*io_realloc)(void*, size_t);
  395: extern char *(*io_strdup)(const char*);
  396: extern void (*io_free)(void*);
  397: 
  398: 
  399: /*
  400:  * io_ether_ntoa() - Convert ethernet address to string
  401:  *
  402:  * @n = ethernet address structure, like struct ether_addr
  403:  * @a = string
  404:  * @len = string length
  405:  * return: NULL error or !=NULL string a
  406:  */
  407: inline char *io_ether_ntoa(const struct io_ether_addr *n, char * __restrict a, int len);
  408: /*
  409:  * io_ether_aton() - Convert string to ethernet address
  410:  *
  411:  * @a = string
  412:  * @e = ethernet address structure, like struct ether_addr
  413:  * return: NULL error or !=NULL ethernet address structure
  414:  */
  415: inline struct io_ether_addr *io_ether_aton(const char *a, struct io_ether_addr *e);
  416: /*
  417:  * io_n2port() - Extract port from network structure
  418:  *
  419:  * @addr = Address
  420:  * return: 0 not supported family type or port number
  421:  */
  422: inline u_short io_n2port(io_sockaddr_t * __restrict addr);
  423: /*
  424:  * io_n2addr() - Extract address from network structure
  425:  *
  426:  * @addr = Address
  427:  * @val = Value for store string address
  428:  * return: NULL error or !=NULL string address from val
  429:  */
  430: const char *io_n2addr(io_sockaddr_t * __restrict addr, ait_val_t * __restrict val);
  431: /*
  432:  * io_gethostbyname() - Get host and port and make network structure
  433:  *
  434:  * @psHost = Hostname
  435:  * @port = Port
  436:  * @addr = Network address structure
  437:  * return: NULL error or !=NULL network structure
  438:  */
  439: io_sockaddr_t *io_gethostbyname(const char *psHost, unsigned short port, 
  440: 		io_sockaddr_t * __restrict addr);
  441: 
  442: /*
  443:  * io_vars2buffer() - Marshaling data from array with variables to buffer
  444:  *
  445:  * @buf = Buffer
  446:  * @buflen = Size of buffer
  447:  * @vars = Variable array
  448:  * return: -1 error, 0 nothing done or >0 size of marshaled data
  449:  */
  450: inline int io_vars2buffer(unsigned char * __restrict buf, int buflen, 
  451: 		array_t * __restrict vars);
  452: /*
  453:  * io_buffer2vars() - De-marshaling data from buffer to array with variables
  454:  *
  455:  * @buf = Buffer
  456:  * @buflen = Size of buffer
  457:  * @vnum = Number of variables into buffer
  458:  * @zcpy = Zero-copy for variables, if !=0 don't use io_arrayFree() for free variables and 
  459:  		*DON'T MODIFY OR DESTROY BUFFER*. =0 call io_arrayFree() before io_arrayDestroy()
  460:  * return: =NULL error, !=NULL allocated variable array, after use must free with io_arrayDestroy()
  461:  */
  462: inline array_t *io_buffer2vars(unsigned char * __restrict buf, int buflen, int vnum, int zcpy);
  463: /*
  464:  * io_vars2map() - Marshaling data from array with variables to memory map
  465:  *
  466:  * @buf = Buffer
  467:  * @buflen = Size of buffer
  468:  * @vars = Variable array
  469:  * return: -1 error, 0 nothing done or >0 size of marshaled data
  470:  */
  471: inline int io_vars2map(unsigned char * __restrict buf, int buflen, 
  472: 		array_t * __restrict vars);
  473: /*
  474:  * io_map2vars() - De-marshaling data from memory map to array with variables
  475:  *
  476:  * @buf = Buffer
  477:  * @buflen = Size of buffer
  478:  * @vnum = Number of variables into buffer
  479:  * @zcpy = Zero-copy for variables, if !=0 don't use io_arrayFree() for free variables and 
  480:  		*DON'T MODIFY OR DESTROY BUFFER*. =0 call io_arrayFree() before io_arrayDestroy()
  481:  * return: =NULL error, !=NULL allocated variable array, after use must free with io_arrayDestroy()
  482:  */
  483: inline array_t *io_map2vars(unsigned char * __restrict buf, int buflen, int vnum, int zcpy);
  484: 
  485: /*
  486:  * io_allocVar() - Allocate memory for variable
  487:  *
  488:  * return: NULL error or new variable, after use free variable with io_freeVar()
  489:  */
  490: inline ait_val_t *io_allocVar(void);
  491: /*
  492:  * io_freeVar() - Free allocated memory for variable
  493:  *
  494:  * @val = Variable
  495:  * return: none
  496:  */
  497: inline void io_freeVar(ait_val_t ** __restrict val);
  498: /*
  499:  * io_allocVars() - Allocate ait_val_t array
  500:  *
  501:  * @varnum = Number of variables
  502:  * return: =NULL error or !=NULL allocated array
  503:  */
  504: inline array_t *io_allocVars(int varnum);
  505: /*
  506:  * io_clrVars() - Clear ait_val_t elements from array
  507:  *
  508:  * @vars = Variable array
  509:  * return: -1 error or size of array
  510:  */
  511: inline int io_clrVars(array_t * __restrict vars);
  512: /*
  513:  * io_freeVars() - Free ait_val_t array
  514:  *
  515:  * @vars = Variable array
  516:  * return: none
  517:  */
  518: inline void io_freeVars(array_t ** __restrict vars);
  519: /*
  520:  * io_getVars() - Get ait_val_t element from array and if not exists allocate it
  521:  *
  522:  * @vars = Variable array
  523:  * @n = index of variable into array
  524:  * return: NULL error or !=NULL ait_val_t element
  525:  */
  526: inline ait_val_t *io_getVars(array_t ** __restrict vars, int n);
  527: /*
  528:  * io_sortVarsByKey() - Sorting array with variables by key
  529:  *
  530:  * @vars = Variable array
  531:  * @order = Sort order. If =0 ascend or !=0 descend
  532:  * return: none
  533:  */
  534: inline void io_sortVarsByKey(array_t * __restrict vars, int order);
  535: /*
  536:  * io_sortVarsByVal() - Sorting array with variables by value
  537:  *
  538:  * @vars = Variable array
  539:  * @order = Sort order. If =0 ascend or !=0 descend
  540:  * @cmp = Custom compare function for sorting. If =NULL compare by value
  541:  * return: none
  542:  */
  543: inline void io_sortVarsByVal(array_t * __restrict vars, int order,  
  544: 		int (*cmp)(const void*, const void*));
  545: /*
  546:  * io_findKeyVars() - Find variable by key from array
  547:  *
  548:  * @vars = Variables
  549:  * @key = Search key
  550:  * return: NULL error or not found, !=NULL valid element
  551:  */
  552: ait_val_t *io_findKeyVars(array_t * __restrict vars, u_short key);
  553: 
  554: 
  555: /*
  556:  * ioPromptRead() - Read data from input h[0] with prompt to output h[1]
  557:  *
  558:  * @h = file handles h[0] = input, h[1] = output, if NULL use stdin, stdout
  559:  * @csPrompt = Prompt before input, may be NULL
  560:  * @psData = Readed data
  561:  * @dataLen = Length of data
  562:  * return: 0 EOF; -1 error:: can`t read; >0 count of readed chars
  563: */
  564: int ioPromptRead(int *h, const char *csPrompt, char * __restrict psData, int dataLen);
  565: /*
  566:  * ioPromptPassword() - Read password from input h[0] with prompt to output h[1]
  567:  *
  568:  * @h = file handles h[0] = input, h[1] = output, if NULL use stdin, stdout
  569:  * @csPrompt = Prompt before input, may be NULL
  570:  * @psPass = Readed password
  571:  * @passLen = Length of password
  572:  * @confirm = Confirm password, 0 - get password, !=0 Ask for confirmation
  573:  * return: 0 EOF; -1 error:: can`t read; >0 count of readed chars
  574: */
  575: int ioPromptPassword(int *h, const char *csPrompt, char * __restrict psPass, int passLen, int confirm);
  576: 
  577: /*
  578:  * ioRegexVerify() - Function for verify data match in regex expression
  579:  *
  580:  * @csRegex = Regulare expression pattern
  581:  * @csData = Data for check and verify
  582:  * @startPos = Return start positions
  583:  * @endPos = Return end positions
  584:  * return: NULL not match or error; !=NULL begin of matched data
  585: */
  586: const char *ioRegexVerify(const char *csRegex, const char *csData, int *startPos, int *endPos);
  587: /*
  588:  * ioRegexGet() - Function for get data match in regex expression
  589:  *
  590:  * @csRegex = Regulare expression pattern
  591:  * @csData = Data from get
  592:  * @psString = Returned string if match
  593:  * @strLen = Length of string
  594:  * return: 0 not match; >0 count of returned chars
  595: */
  596: int ioRegexGet(const char *csRegex, const char *csData, char * __restrict psString, int strLen);
  597: /*
  598:  * ioRegexReplace() - Function for replace data match in regex expression with newdata
  599:  *
  600:  * @csRegex = Regulare expression pattern
  601:  * @csData = Source data
  602:  * @csNew = Data for replace
  603:  * return: NULL not match or error; !=NULL allocated new string, must be io_free after use!
  604: */
  605: char *ioRegexReplace(const char *csRegex, const char *csData, const char *csNew);
  606: 
  607: /*
  608:  * ioStrAst() - Function for evaluate string like asterisk variable "{text[:[-]#[:#]]}"
  609:  *
  610:  * @csString = Input string
  611:  * return: NULL error, !=NULL Allocated new string evaluated from input string, must be io_free()
  612: */
  613: char *ioStrAst(const char *csString);
  614: 
  615: /*
  616:  * io_UnquotStr() - Remove quots from input text string 
  617:  *
  618:  * @psLine = Text string
  619:  * return: 0 nothing to do; 1 successful unquoted string
  620: */
  621: inline int io_UnquotStr(char * __restrict psLine);
  622: /*
  623:  * io_LTrimStr() - Remove left whitespaces from text string
  624:  *
  625:  * @psLine = Text string
  626:  * return: 0 nothing to do; !=0 Removed bytes
  627: */
  628: inline int io_LTrimStr(char * __restrict psLine);
  629: /*
  630:  * io_RTrimStr() - Remove right whitespaces from text string
  631:  *
  632:  * @psLine = Text string
  633:  * return: 0 nothing to do; !=0 Removed bytes
  634: */
  635: inline int io_RTrimStr(char * __restrict psLine);
  636: /*
  637:  * io_TrimStr() - Remove left and right whitespaces from text string
  638:  *
  639:  * @psLine = Text string
  640:  * return: 0 nothing to do; !=0 Removed bytes
  641: */
  642: inline int io_TrimStr(char * __restrict psLine);
  643: /*
  644:  * io_Ch2Hex() - Convert from Char string to Hex string
  645:  *
  646:  * @psLine = Text string
  647:  * @lineLen = Length of Text string
  648:  * return: NULL nothing to do or error; !=0 Allocated new converted data without term\0 (must be io_free)
  649: */
  650: inline unsigned char *io_Ch2Hex(unsigned char *psLine, int lineLen);
  651: /*
  652:  * io_Hex2Ch() - Convert from Hex string to Char string
  653:  *
  654:  * @psLine = Text string
  655:  * @lineLen = Length of Text string
  656:  * return: NULL nothing to do or error; !=0 Allocated new converted string(must be io_free)
  657: */
  658: inline char *io_Hex2Ch(unsigned char *psLine, int lineLen);
  659: 
  660: /*
  661:  * io_Path2File() - Parse and make path/filename pair
  662:  *
  663:  * @csArgs = Input argument line
  664:  * @psPath = Output Path, if ==NULL path not returned
  665:  * @pathLen = Size of path array
  666:  * @psFile = Output File
  667:  * @fileLen = Size of file array
  668:  * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items
  669:  */
  670: inline int
  671: io_Path2File(const char * __restrict csArgs, char * __restrict psPath, 
  672: 		int pathLen, char * __restrict psFile, int fileLen);
  673: 
  674: /*
  675:  * io_arrayInit() - Create and initialize dynamic array
  676:  *
  677:  * @numItems = Number of Items
  678:  * return: NULL error, != NULL allocated memory for array
  679:  */
  680: inline array_t *io_arrayInit(int numItems);
  681: /*
  682:  * io_arrayDestroy() - Free and destroy dynamic array
  683:  *
  684:  * @parr = Array
  685:  * return: none
  686:  */
  687: inline void io_arrayDestroy(array_t ** __restrict parr);
  688: /*
  689:  * io_arrayFree() - Free all data in dynamic array items
  690:  *	(WARNING! If assign static array dont use this!!!)
  691:  *
  692:  * @arr = Array
  693:  * return: none
  694:  */
  695: inline void io_arrayFree(array_t * __restrict arr);
  696: /*
  697:  * io_arrayFrom() - Create and fill array from array with pointers
  698:  *
  699:  * @pargv = Array with pointers
  700:  * @argc = Number of Items, if 0 walk through argv and stop when reach NULL item
  701:  * return: NULL error, != NULL allocated new array
  702:  */
  703: inline array_t *io_arrayFrom(const char *** __restrict pargv, int argc);
  704: /*
  705:  * io_arrayTo() - Create and fill array with pointers from dynamic array
  706:  *
  707:  * @arr = Array
  708:  * return: NULL error, != NULL allocated memory for array, NULL terminated
  709:  */
  710: inline char **io_arrayTo(array_t * __restrict arr);
  711: /*
  712:  * io_arrayLen() - Get last used element in dynamic array (array Length)
  713:  *
  714:  * @arr = Array
  715:  * return: -1 empty or >-1 position of last used element
  716:  */
  717: inline int io_arrayLen(array_t * __restrict arr);
  718: /*
  719:  * io_arrayConcat() Concat source array to destination array
  720:  *
  721:  * @dest = Destination array
  722:  * @src = Source array
  723:  * return: -1 error; >0 new count of destination array
  724:  */
  725: int io_arrayConcat(array_t * __restrict dest, array_t * __restrict src);
  726: /*
  727:  * io_arrayCopy() Copy source array to destination array
  728:  *
  729:  * @dest = Destination array, after use free with io_arrayDestroy()
  730:  * @src = Source array
  731:  * return: -1 error; >0 count of destination array
  732:  */
  733: int io_arrayCopy(array_t ** __restrict dest, array_t * __restrict src);
  734: /*
  735:  * io_arrayGrow() - Grow/Shrink dynamic array, Use with care when it shrink!!!
  736:  *
  737:  * @arr = Array
  738:  * @newNumItems = Number of Items
  739:  * @freeShrink = Free elements before shrink array
  740:  * return: -1 error, 0 ok
  741:  */
  742: int io_arrayGrow(array_t * __restrict arr, int newNumItems, int freeShrink);
  743: /*
  744:  * io_arrayVacuum() - Vacuum dynamic array, empty elements will be deleted
  745:  *
  746:  * @arr = Array
  747:  * @fromWhere = 1 begin, 2 ALL empty elements
  748:  * return: -1 error, 0 ok
  749:  */
  750: int io_arrayVacuum(array_t * __restrict arr, int fromWhere);
  751: 
  752: #define io_arraySize(_arr)		((_arr) ? (_arr)->arr_num : 0)
  753: #define io_arrayLast(_arr)		(io_arraySize((_arr)) ? (_arr)->arr_last : -1)
  754: #define io_arrayZero(_arr)		(assert((_arr)), memset((_arr)->arr_data, 0, \
  755: 						io_arraySize((_arr)) * sizeof(void*)))
  756: 
  757: #define io_arrayGet2(_arr, _d)		(assert((_arr) && (_arr)->arr_num > _d), ((_arr)->arr_data + _d))
  758: #define io_arrayGet(_arr, _d)		(assert((_arr) && (_arr)->arr_num > _d), (_arr)->arr_data[_d])
  759: #define io_array(_arr, _d, _type)	(assert((_arr) && (_arr)->arr_num > _d), \
  760: 						((_type) (_arr)->arr_data[_d]))
  761: #define io_arraySet(_arr, _d, _ptr)	do { \
  762: 						assert((_arr) && (_arr)->arr_num > _d); \
  763: 						if ((_arr)->arr_last < _d) \
  764: 							(_arr)->arr_last = _d; \
  765: 						(_arr)->arr_data[_d] = (void*) (_ptr); \
  766: 					} while (0)
  767: #define io_arrayDel(_arr, _d, _fri)	do { \
  768: 						assert((_arr) && (_arr)->arr_num > _d); \
  769: 						if (_fri && (_arr)->arr_data[_d]) \
  770: 							free((_arr)->arr_data[_d]); \
  771: 						(_arr)->arr_data[_d] = NULL; \
  772: 					} while (0)
  773: 
  774: /*
  775:  * io_arrayElem() - Always GET/PUT element into dynamic array, if not enough elements grow array
  776:  *
  777:  * @arr = Array
  778:  * @n = Position
  779:  * @data = Element, if set NULL GET element at position or !=NULL PUT element at position
  780:  * return: -1 error or !=-1 return element at position
  781:  */
  782: inline void *io_arrayElem(array_t * __restrict arr, int n, void *data);
  783: /*
  784:  * io_arrayPush() - Push element into dynamic array like stack manner, place at first empty position
  785:  *
  786:  * @arr = Array
  787:  * @data = Element, if set NULL return only first empty position
  788:  * @mayGrow = Array may to grow, if not enough room for new element
  789:  * return: -1 not found empty position, array is full!, >-1 return position of stored element into array
  790:  */
  791: inline int io_arrayPush(array_t * __restrict arr, void **data, int mayGrow);
  792: /*
  793:  * io_arrayPop() - Pop element from dynamic array like stack manner, last used position
  794:  *
  795:  * @arr = Array
  796:  * @data = Element, if set NULL return only last used position
  797:  * @delAfter = Delete after Pop element, !=0 delete element from array after return data
  798:  * return: -1 not found used position, array is empty!, >-1 return element position
  799:  */
  800: inline int io_arrayPop(array_t * __restrict arr, void ** __restrict data, int delAfter);
  801: 
  802: /*
  803:  * io_argsNum() - Parse and calculate number of arguments
  804:  *
  805:  * @csArgs = Input arguments line
  806:  * @csDelim = Delimiter(s) for separate
  807:  * return: 0 error format; -1 error:: can`t read; >0 ok, number of items
  808:  */
  809: inline int io_argsNum(const char *csArgs, const char *csDelim);
  810: 
  811: /*
  812:  * io_arrayMake() - Parse and make array from arguments ... (input string will be modified!!! 
  813:  *	and output array must be free with io_arrayDestroy() after use!)
  814:  *
  815:  * @psArgs = Input arguments line, after execute string is modified!!!
  816:  * @nargs = Maximum requested count of arguments from input string psArgs, if 0 all psArgs
  817:  * @csDelim = Delimiter(s) for separate
  818:  * @parr = Output array of arguments ... (must be io_free with io_arrayDestroy() after use!)
  819:  * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items
  820:  */
  821: int io_arrayMake(char * __restrict psArgs, int nargs, const char *csDelim, 
  822: 		array_t ** __restrict parr);
  823: 
  824: /*
  825:  * io_MakeAV() - Parse and make attribute/value pair
  826:  *
  827:  * @csArgs = Input argument line
  828:  * @csDelim = Delimiter for separate
  829:  * @psAttr = Output Attribute
  830:  * @attrLen = Size of attribute array
  831:  * @psValue = Output Value, if ==NULL this element not present value or not wanted for return
  832:  * @valLen = Size of value array
  833:  * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items
  834: */
  835: int io_MakeAV(const char * __restrict csArgs, const char *csDelim, 
  836: 		char * __restrict psAttr, int attrLen, char * __restrict psValue, int valLen);
  837: /*
  838:  * io_MakeAV2() Parse and make attribute/value pair over input string
  839:  *
  840:  * @csArgs = Input argument line, will be modified!
  841:  * @csDelim = Delimiter for separate
  842:  * @psAttr = Output Attribute
  843:  * @psValue = Output Value, if ==NULL this element not present value or not wanted for return
  844:  * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items
  845: */
  846: int io_MakeAV2(char * __restrict psArgs, const char *csDelim, 
  847: 		char ** __restrict psAttr, char ** __restrict psValue);
  848: 
  849: /*
  850:  * io_sarrInit() - Create and initialize dynamic split-order array
  851:  *
  852:  * @numItems = Number of Items
  853:  * @segLen = Length of segment
  854:  * return: NULL error, != NULL allocated memory for array
  855:  */
  856: inline sarr_t *io_sarrInit(int numItems, int segLen);
  857: /*
  858:  * io_sarrDestroy() - Free all data in dynamic split-order array and Destroy array
  859:  *
  860:  * @parr = Array
  861:  * return: none
  862:  */
  863: inline void io_sarrDestroy(sarr_t ** __restrict parr);
  864: /*
  865:  * io_sarrGrow() - Grow/Shrink dynamic split-order array, Use with care when it shrink!!!
  866:  *
  867:  * @arr = Array
  868:  * @newNumItems = Number of Items
  869:  * return: -1 error, 0 ok
  870:  */
  871: int io_sarrGrow(sarr_t * __restrict arr, int newNumItems);
  872: /*
  873:  * io_sarrVacuum() - Vacuum dynamic split-order array, empty segments will be freed
  874:  *
  875:  * @arr = Array
  876:  * return: -1 error, >-1 freed segments
  877:  */
  878: inline int io_sarrVacuum(sarr_t * __restrict arr);
  879: #define io_sarrSize(_arr)		((_arr) ? (_arr)->sarr_num : 0)
  880: #define io_sarrSeg(_arr)		(assert((_arr)), (_arr)->sarr_seg)
  881: /*
  882:  * io_sarrCopy() Copy source split array to destination split array
  883:  *
  884:  * @dest = Destination split array, after use free with io_sarrDestroy()
  885:  * @src = Source split array
  886:  * return: -1 error; >0 count of destination split array
  887:  */
  888: int io_sarrCopy(sarr_t ** __restrict dest, sarr_t * __restrict src);
  889: /*
  890:  * io_sarrGet() - Get element from dynamic split-order array
  891:  *
  892:  * @arr = Array
  893:  * @idx = Index (warning 1st element is at position 1)
  894:  * return: NULL not found, !=NULL element
  895:  */
  896: inline void *io_sarrGet(sarr_t * __restrict arr, unsigned int idx);
  897: /*
  898:  * io_sarrGet2() - Always get element from dynamic split-order array
  899:  *	Function automatic grow array. Good use for Hash tables! 
  900:  *
  901:  * @arr = Array
  902:  * @idx = Index (warning 1st element is at position 1)
  903:  * return: NULL not found, !=NULL element
  904:  */
  905: void *io_sarrGet2(sarr_t * __restrict arr, unsigned int idx);
  906: /*
  907:  * io_sarrSet() - Set element to dynamic split-order array
  908:  *
  909:  * @arr = Array
  910:  * @idx = Index (warning 1st element is at position 1)
  911:  * @data = Value
  912:  * return: NULL error or empty, !=NULL old value in element
  913:  */
  914: inline void *io_sarrSet(sarr_t * __restrict arr, unsigned int idx, void *data);
  915: #define io_sarrDel(_arr, _idx)		io_sarrSet((_arr), _idx, NULL)
  916: #define io_sarr(_arr, _idx, _type)	(_type)io_sarrGet((_arr), _idx)
  917: /*
  918:  * io_sarr2array() - Convert from split-order array to dynamic array
  919:  *
  920:  * @sa = split array
  921:  * @sarrFree = after convert split array !=0 will be destroyed sarray
  922:  * return: NULL error or != NULL new array
  923:  */
  924: array_t *io_sarr2array(sarr_t ** __restrict sa, int sarrFree);
  925: /*
  926:  * io_array2sarr() - Convert from dynamic array to split-order array
  927:  *
  928:  * @a = array
  929:  * @segLen = Length of segment
  930:  * @arrFree = after convert array !=0 will be destroyed
  931:  * return: NULL error or != NULL new sarr
  932:  */
  933: sarr_t *io_array2sarr(array_t ** __restrict a, int segLen, int arrFree);
  934: 
  935: /*
  936:  * io_CopyEnv() - Copy environment to new environment array;
  937:  *
  938:  * @oldenv = Environment array
  939:  * return: NULL error; !=NULL Allocated new environment array(must be io_free)
  940: */
  941: char **io_CopyEnv(const char **oldenv);
  942: /*
  943:  * io_ExecArgs() - Build exec arguments from other array
  944:  *
  945:  * @psProg = Program name for execute
  946:  * @oldarg = Arguments array
  947:  * return: NULL error; !=NULL Allocated execution array(must be io_free)
  948: */
  949: char **io_ExecArgs(const char *psProg, const char **oldarg);
  950: /*
  951:  * io_FreeNullTerm() - Free dynamic allocated null terminated array with strings
  952:  *
  953:  * @arr = Pointer to array for free
  954:  * return: none
  955: */
  956: inline void io_FreeNullTerm(char *** __restrict arr);
  957: 
  958: /*
  959:  * ioMkDir() - Function for racursive directory creation and validation
  960:  *
  961:  * @csDir = Full directory path
  962:  * @mode = Mode for directory creation if missing dir
  963:  * return: -1 error, 0 directory path exist, >0 created missing dirs
  964: */
  965: int ioMkDir(const char *csDir, int mode);
  966: 
  967: /*
  968:  * ioWatchDirLoop() - Function for watching changes in directory and fire callback
  969:  *
  970:  * @csDir = Full directory path
  971:  * @callback = Callback if raise event! nOp -1 delete, 0 change/move, 1 create
  972:  * return: -1 error, !=-1 ok, number of total signaled events
  973: */
  974: int ioWatchDirLoop(const char *csDir, int (*callback)(const char *csName, int nOp));
  975: 
  976: 
  977: /*
  978:  * io_rread() - Raw VFS read function
  979:  *
  980:  * @fd = File handle
  981:  * @buf = Read buffer
  982:  * @nbytes = Read buffer size
  983:  * @offset = Read from position, if -1 read nbytes from current position
  984:  * @update = Update file handle position !0
  985:  * return: -1 error or !=-1 readed bytes
  986:  */
  987: inline int io_rread(int fd, void * __restrict buf, size_t nbytes, off_t offset, int update);
  988: /*
  989:  * io_rwrite() - Raw VFS write function
  990:  *
  991:  * @fd = File handle
  992:  * @buf = Write buffer
  993:  * @nbytes = Write bytes from buffer
  994:  * @offset = Write at position, if -1 write nbytes from current position
  995:  * @update = Update file handle position !0
  996:  * return: -1 error or !=-1 writed bytes
  997:  */
  998: inline int io_rwrite(int fd, void * __restrict buf, size_t nbytes, off_t offset, int update);
  999: 
 1000: /* Disk I/O helper macros */
 1001: #define io_read(f, b, n) io_rread(f, b, n, -1, 1)
 1002: #define io_write(f, b, n) io_rwrite(f, b, n, -1, 1)
 1003: 
 1004: 
 1005: /* Debug helper macros */
 1006: extern int io_Debug;
 1007: 
 1008: #define io_enableDEBUG		int io_Debug
 1009: #define io_initDebug(x)		(io_Debug = (x))
 1010: #define io_incDebug		(io_Debug++)
 1011: #define io_decDebug		(io_Debug--)
 1012: 
 1013: /* Debug macros */
 1014: #define ioVERBOSE(x)		if ((x) <= io_Debug)
 1015: #define ioTRACE(x)		if ((x) <= io_Debug) \
 1016: 					   syslog(LOG_DEBUG, "I'm in %s(%d)", __func__, __LINE__)
 1017: #define ioDEBUG(x, fmt, ...)	do { \
 1018: 					assert((fmt)); \
 1019: 					char str[STRSIZ] = { 0 }; \
 1020: 					snprintf(str, sizeof str, (fmt), ##__VA_ARGS__); \
 1021: 					if ((x) <= io_Debug) \
 1022: 						syslog(LOG_DEBUG, "Debug(%d):%s(%d): %s\n", \
 1023: 								(x), __func__, __LINE__, str); \
 1024: 				} while (0)
 1025: 
 1026: /* Logger macro */
 1027: #define ioLOGGER(x, fmt, ...)	do { \
 1028: 					assert((fmt)); \
 1029: 					char str[STRSIZ] = { 0 }; \
 1030: 					snprintf(str, sizeof str, (fmt), ##__VA_ARGS__); \
 1031: 					syslog((x), "Logger:%s(%d): %s\n", \
 1032: 								__func__, __LINE__, str); \
 1033: 				} while (0)
 1034: 
 1035: /* Error state macros */
 1036: #define ioERROR(x, fmt, ...)	do { \
 1037: 					assert((fmt)); \
 1038: 					char str[STRSIZ] = { 0 }; \
 1039: 					snprintf(str, sizeof str, (fmt), ##__VA_ARGS__); \
 1040: 					syslog(LOG_ERR, "Error:%s(%d): #%d - %s\n", \
 1041: 							 __func__, __LINE__, (x), str); \
 1042: 				} while (0)
 1043: #define ioSYSERR(x)		do { \
 1044: 					if (x > 0 || errno) \
 1045: 						syslog(LOG_ERR, "Error(sys):%s(%d): #%d - %s\n", \
 1046: 								__func__, __LINE__, x > 0 ? x : errno, \
 1047: 								strerror(x > 0 ? x : errno)); \
 1048: 				} while (0)
 1049: #define ioLIBERR(ait)		do { \
 1050: 					if (ait##_GetErrno()) \
 1051: 						syslog(LOG_ERR, "Error(lib):%s(%d): #%d - %s\n", \
 1052: 								__func__, __LINE__, ait##_GetErrno(), \
 1053: 								ait##_GetError()); \
 1054: 				} while (0)
 1055: 
 1056: 
 1057: /* Crypto framework */
 1058: 
 1059: /*
 1060:  * ioCipher() - Cipher wrapper for all supported crypto algorythms
 1061:  *
 1062:  * @pInput = input buffer
 1063:  * @inLen = input buffer len
 1064:  * @ppOutput = output allocated buffe, must be io_free after use
 1065:  * @Cipher = cipher engine, like EVP_bf_cbc() or etc...
 1066:  * @pKey = key
 1067:  * @pIV = IV, salt (8 bytes)
 1068:  * @nMode = Mode 0 - decrypting or 1 - encrypting
 1069:  * return: 0 not present data or error!; >0 number of processed and returned bytes into ppOutput
 1070: */
 1071: int ioCipher(unsigned char *pInput, int inLen, unsigned char **ppOutput, const EVP_CIPHER *Cipher, 
 1072: 		unsigned char *pKey, unsigned char *pIV, int nMode);
 1073: 
 1074: /*
 1075:  * io_Blowfish() - Blowfish cipher algorythm, work with ASCII hex strings
 1076:  *
 1077:  * @pInput = input buffer
 1078:  * @inLen = input buffer len
 1079:  * @ppOutput = output allocated buffe, must be io_free after use
 1080:  * @pKey = key
 1081:  * @pIV = IV, salt (8 bytes)
 1082:  * @nMode = Mode 0 - decrypting or 1 - encrypting
 1083:  * return: 0 not present data or error!; >0 number of processed and returned bytes into ppOutput
 1084: */
 1085: int io_Blowfish(unsigned char *pInput, int inLen, unsigned char **ppOutput, 
 1086: 		unsigned char *pKey, unsigned char *pIV, int nMode);
 1087: /*
 1088:  * io_ctr_AES() - Encrypt/Decrypt stream cipher CTR_AES
 1089:  *
 1090:  * @pInput = Input buffer with ASCII
 1091:  * @inLen = Input buffer data length
 1092:  * @ppOutput = Output buffer with cipher data, must be io_free after use
 1093:  * @pKey = Key
 1094:  * @IV = IVector/Nonce/Counter, Warning: IV must be variable, because we write there!!!
 1095:  * return: -1 error or >-1 how many cipher blocks proceeded
 1096:  */
 1097: int io_ctr_AES(unsigned char *pInput, int inLen, unsigned char **ppOutput, 
 1098: 		unsigned char *pKey, unsigned char IV[AES_BLOCK_SIZE]);
 1099: 
 1100: 
 1101: /*
 1102:  * ioAllocPTY() - Allocate new PTY and TTY
 1103:  *
 1104:  * @ptyfd = master fd, pty
 1105:  * @ttyfd = slave fd, tty
 1106:  * @name = tty device name if not null
 1107:  * @namesiz = name length, must be above 63 bytes.
 1108:  * @term = termios for terminal
 1109:  * @winz = winsize for terminal
 1110:  * return: -1 error or 0 ok
 1111:  */
 1112: inline int ioAllocPTY(int *ptyfd, int *ttyfd, char * __restrict name, int namesiz, 
 1113: 		struct termios * __restrict term, struct winsize * __restrict winz);
 1114: /*
 1115:  * ioFreePTY() - Release PTY and TTY device
 1116:  *
 1117:  * @ptyfd = master fd, pty (==-1 skip closing pty)
 1118:  * @ttyname = tty filename
 1119:  * return: none
 1120:  */
 1121: inline void ioFreePTY(int ptyfd, const char *ttyname);
 1122: /*
 1123:  * ioChgWinPTY() - Change window size of PTY
 1124:  *
 1125:  * @ptyfd = master fd, pty
 1126:  * @row = row
 1127:  * @col = col
 1128:  * @xpxl = x pixels
 1129:  * @ypxl = y pixels
 1130:  * return: -1 error or 0 ok
 1131:  */
 1132: inline int ioChgWinPTY(int ptyfd, unsigned short row, unsigned short col, 
 1133: 		unsigned short xpxl, unsigned short ypxl);
 1134: /*
 1135:  * ioSetOwnerTTY() - Set owner to TTY
 1136:  *
 1137:  * @ttyname = tty filename
 1138:  * @UID = uid
 1139:  * @GID = gid
 1140:  * return: -1 error or 0 ok
 1141:  */
 1142: int ioSetOwnerTTY(const char *ttyname, uid_t UID, gid_t GID);
 1143: /*
 1144:  * ioSetSidTTY() - Makes the process's controlling TTY and sets it to sane modes.
 1145:  *
 1146:  * @ttyfd = slave fd, tty
 1147:  * @ttyname = tty filename
 1148:  * return: -1 error or 0 ok
 1149:  */
 1150: int ioSetSidTTY(int *ttyfd, const char *ttyname);
 1151: /*
 1152:  * ioSetRAWMode() - Enter into RAW mode
 1153:  *
 1154:  * @fd = tty fd
 1155:  * @otio = saved old termios for later restore if !=NULL
 1156:  * return: -1 error or 0 ok
 1157:  */
 1158: inline int ioSetRAWMode(int fd, struct termios *otio);
 1159: /*
 1160:  * ioRestoreMode() - Restore termios to tty fd
 1161:  *
 1162:  * @fd = tty fd
 1163:  * @tio = termios structure for restore
 1164:  * return: -1 error or 0 ok
 1165:  */
 1166: inline int ioRestoreMode(int fd, struct termios tio);
 1167: /*
 1168:  * ioForkPTY() - Fork new process with session leader and new TTY
 1169:  *
 1170:  * @ptyfd = master fd, pty
 1171:  * @name = tty device name if not null
 1172:  * @namesiz = name length, must be above 63 bytes.
 1173:  * @term = termios for terminal
 1174:  * @winz = winsize for terminal
 1175:  * @otio = old termios structure for restore
 1176:  * return: -1 error, 0 child process or >0 parent: pid of child
 1177:  */
 1178: pid_t ioForkPTY(int *ptyfd, char * __restrict name, int namesiz, struct termios * __restrict term, 
 1179: 		struct winsize * __restrict winz, struct termios * __restrict otio);
 1180: 
 1181: /*
 1182:  * ioCreatePIDFile() - Create PID file
 1183:  *
 1184:  * @csName = PID filename
 1185:  * @ifExists = !=0 if filename exists return error
 1186:  * return: -1 error or 0 ok
 1187:  */
 1188: inline int ioCreatePIDFile(const char *csName, int ifExists);
 1189: 
 1190: /*
 1191:  * ioSendFile() - AITNET sendfile() userland implementation, not dependant from OS
 1192:  *
 1193:  * @s = socket
 1194:  * @csFile = file for send
 1195:  * @sendLen = bytes to send, if 0 send all data
 1196:  * @offset = start file offset
 1197:  * @sndbuf = SO_SNDBUF value, if 0 use default
 1198:  * return: 0 error, >0 ok, sended bytes
 1199:  */
 1200: size_t ioSendFile(int s, const char *csFile, size_t sendLen, off_t offset, int sndbuf);
 1201: /*
 1202:  * ioRecvFile() - Receive file from socket, fastest (zero-copy) way
 1203:  *
 1204:  * @s = socket
 1205:  * @csFile = file for receive
 1206:  * @recvLen = receive bytes
 1207:  * @over = overwrite file if exists with mode like 0644
 1208:  * @rcvbuf = SO_RCVBUF value, if 0 use default
 1209:  * return: 0 error, >0 ok, received bytes
 1210:  */
 1211: size_t ioRecvFile(int s, const char *csFile, size_t recvLen, int over, int rcvbuf);
 1212: 
 1213: 
 1214: /* Buffered file access over memory block */
 1215: 
 1216: /*
 1217:  * io_fmemopen() - File buffered stream operations over memory block
 1218:  *
 1219:  * @base = Base address of memory block, if =NULL Infinit length(auto-grow)
 1220:  * @basesize = Size of memory block
 1221:  * return: NULL error or !=NULL Opened file resource
 1222:  */
 1223: FILE *io_fmemopen(void ** __restrict base, off_t basesize);
 1224: /*
 1225:  * io_fmapopen() - File buffered stream operations over MMAP block
 1226:  *
 1227:  * @csFile = Filename for MMAP, if =NULL private MMAP block
 1228:  * @mode = File open mode
 1229:  * @perm = If file not exists will be created with this access permissions
 1230:  * @prot = MMAP protection
 1231:  * @flags = MMAP mode flags
 1232:  * @offset = Map from file offset, if csFile==NULL then this is size of MMAP private block
 1233:  * return: NULL error or !=NULL Opened file resource
 1234:  */
 1235: FILE *io_fmapopen(const char *csFile, int mode, int perm, int prot, int flags, off_t offset);
 1236: /*
 1237:  * io_fd2buf() - Convert open file handle to buffered file I/O
 1238:  *
 1239:  * @fd = File handle
 1240:  * @mode = Permissions for new buffered file I/O
 1241:  * return: NULL error or open buffered file
 1242:  */
 1243: inline FILE *io_fd2buf(int fd, const char *mode);
 1244: /*
 1245:  * io_dumbFile() - Create empry or dumb file with fixed size
 1246:  *
 1247:  * @csFile = Filename for create
 1248:  * @mode = File access permissions
 1249:  * @size = File size
 1250:  * return: -1 error or open file handle
 1251:  */
 1252: int io_dumbFile(const char *csFile, int mode, off_t size);
 1253: 
 1254: 
 1255: #endif

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>