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

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

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