Annotation of libaitio/inc/aitio.h, revision 1.24

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

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