File:  [ELWIX - Embedded LightWeight unIX -] / libaitio / inc / aitio.h
Revision 1.26: download - view: text, annotated - select for diffs - revision graph
Wed Sep 19 15:19:53 2012 UTC (11 years, 9 months ago) by misho
Branches: MAIN
CVS tags: io3_9, io3_8, IO3_8, IO3_7, HEAD
version 3.7

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

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