File:  [ELWIX - Embedded LightWeight unIX -] / libaitio / inc / aitio.h
Revision 1.23: download - view: text, annotated - select for diffs - revision graph
Wed Aug 1 00:37:07 2012 UTC (11 years, 10 months ago) by misho
Branches: MAIN
CVS tags: io3_5, IO3_4, HEAD
version 3.4

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

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