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

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.42.2.1! misho       6: * $Id: aitio.h,v 1.42 2016/08/18 09:06:31 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.42      misho      15: Copyright 2004 - 2016
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.42.2.1! misho      54: #include <openssl/crypto.h>
1.12      misho      55: #include <openssl/aes.h>
1.42.2.1! misho      56: #include <openssl/evp.h>
        !            57: #if OPENSSL_VERSION_NUMBER >= 0x10100000L
        !            58:        #include <openssl/modes.h>
        !            59: #endif
1.13      misho      60: #include <sys/socket.h>
                     61: #include <sys/un.h>
1.24      misho      62: #include <sys/uio.h>
1.42      misho      63: #ifndef __linux__
                     64: #include <sys/tty.h>
                     65: #include <sys/ioctl_compat.h>
1.13      misho      66: #include <net/if_dl.h>
1.33      misho      67: #include <net/bpf.h>
1.42      misho      68: #endif
1.13      misho      69: #include <netinet/in.h>
1.34      misho      70: #include <elwix.h>
1.37      misho      71: #include <aitsched.h>
1.7       misho      72: 
1.42.2.1! misho      73: 
        !            74: #ifndef evp_cipher_ctx_st
        !            75: struct evp_cipher_ctx_st {
        !            76:     const EVP_CIPHER *cipher;
        !            77:     ENGINE *engine;             /* functional reference if 'cipher' is
        !            78:                                  * ENGINE-provided */
        !            79:     int encrypt;                /* encrypt or decrypt */
        !            80:     int buf_len;                /* number we have left */
        !            81:     unsigned char oiv[EVP_MAX_IV_LENGTH]; /* original iv */
        !            82:     unsigned char iv[EVP_MAX_IV_LENGTH]; /* working iv */
        !            83:     unsigned char buf[EVP_MAX_BLOCK_LENGTH]; /* saved partial block */
        !            84:     int num;                    /* used by cfb/ofb/ctr mode */
        !            85:     /* FIXME: Should this even exist? It appears unused */
        !            86:     void *app_data;             /* application stuff */
        !            87:     int key_len;                /* May change for variable length cipher */
        !            88:     unsigned long flags;        /* Various flags */
        !            89:     void *cipher_data;          /* per EVP data */
        !            90:     int final_used;
        !            91:     int block_mask;
        !            92:     unsigned char final[EVP_MAX_BLOCK_LENGTH]; /* possible final block */
        !            93: } /* EVP_CIPHER_CTX */ ;
        !            94: #endif
1.7       misho      95: 
1.21      misho      96: #ifndef STRSIZ
1.34      misho      97: #define STRSIZ                 256
1.21      misho      98: #endif
                     99: 
1.34      misho     100: #define IO_SOCK_ROLE_CLIENT    0
                    101: #define IO_SOCK_ROLE_SERVER    1
                    102: 
1.36      misho     103: #define IO_ETHER_FILTER_PROMISC        0
                    104: #define IO_ETHER_FILTER_NOTREAD        -1
                    105: #define IO_ETHER_FILTER_READ   1
                    106: #define IO_ETHER_FILTER_WRITE  2
                    107: 
1.34      misho     108: 
1.39      misho     109: typedef struct {
                    110:        unsigned int                    prog_inin;      /* init progs */
                    111:        unsigned int                    prog_maxn;      /* max progs */
                    112:        unsigned int                    prog_cnum;      /* current progs */
                    113:        char                            prog_name[PATH_MAX];
                    114: 
                    115:        pthread_mutex_t                 prog_mtx;
                    116:        array_t                         *prog_fds;
                    117:        unsigned int                    *prog_used;
                    118: } prog_t;
                    119: 
1.35      misho     120: typedef struct tagCliSock sock_cli_t;
                    121: typedef void *(*sock_cb_t)(sock_cli_t*);
                    122: struct tagCliSock {
                    123:        void                            *cli_parent;
                    124:        int                             cli_fd;
1.39      misho     125:        intptr_t                        cli_pty;
1.35      misho     126:        sockaddr_t                      cli_addr;
                    127:        char                            cli_name[64];
1.37      misho     128:        char                            cli_cmdline[PATH_MAX];
                    129:        pid_t                           cli_pid;
1.35      misho     130: 
1.37      misho     131:        sched_task_func_t               cli_func;
1.35      misho     132: 
1.37      misho     133:        ait_val_t                       cli_buf[2];
1.35      misho     134: 
                    135:        TAILQ_ENTRY(tagCliSock)         cli_node;
                    136: };
1.37      misho     137: 
1.34      misho     138: typedef struct {
1.35      misho     139:        int                             sock_role;
                    140:        int                             sock_backq;
                    141:        int                             sock_type;
                    142:        int                             sock_proto;
                    143:        int                             sock_fd;
1.37      misho     144:        struct timespec                 sock_timeout;
1.35      misho     145:        sockaddr_t                      sock_addr;
                    146:        sockaddr_t                      sock_peer;
1.34      misho     147: 
1.35      misho     148:        ait_val_t                       sock_buf;
1.39      misho     149:        prog_t                          *sock_prog;
1.34      misho     150: 
1.37      misho     151:        volatile intptr_t               sock_kill;
                    152:        sched_root_task_t               *sock_root;
                    153: 
1.35      misho     154:        pthread_mutex_t                 sock_mtx;
                    155:        TAILQ_HEAD(, tagCliSock)        sock_cli;
1.34      misho     156: } sock_t;
                    157: 
1.15      misho     158: 
1.1       misho     159: // io_GetErrno() Get error code of last operation
1.30      misho     160: int io_GetErrno();
1.1       misho     161: // io_GetError() Get error text of last operation
1.30      misho     162: const char *io_GetError();
1.1       misho     163: 
                    164: 
1.12      misho     165: /*
1.39      misho     166:  * io_progInit() - Init program pool
                    167:  *
                    168:  * @progName = program name for execution
                    169:  * @initNum = initial started programs
                    170:  * @maxNum = maximum started programs
                    171:  * return: NULL error or !=NULL allocated pool (must destroied with io_progDestroy())
                    172:  */
                    173: prog_t *io_progInit(const char *progName, 
                    174:                unsigned int initNum, unsigned int maxNum);
                    175: /*
                    176:  * io_progOpen() - Execute number of program(s)
                    177:  *
                    178:  * @prg = program pool
                    179:  * @execNum = execute program(s) (0 max)
                    180:  * return: -1 error, >0 executed programs
                    181:  */
                    182: int io_progOpen(prog_t * __restrict prg, unsigned int execNum);
                    183: /*
                    184:  * io_progOpen2() - Start program from pool on first unused slot
                    185:  *
                    186:  * @prg = program pool
                    187:  * return: -1 error, >-1 reside at slot
                    188:  */
                    189: int io_progOpen2(prog_t * __restrict prg);
                    190: /*
                    191:  * io_progGrow() - Execute to number of programs in pool
                    192:  *
                    193:  * @prg = program pool
                    194:  * @toNum = execute to number of programs (0 max)
                    195:  * return: 0 error or nothing to do, 
                    196:  *     >0 executed programs and abs(<0) executed programs with logged error
                    197:  */
                    198: int io_progGrow(prog_t * __restrict prg, unsigned int toNum);
                    199: /*
                    200:  * io_progVacuum() - Vacuum pool to running number of programs
                    201:  *
                    202:  * @prg = program pool
                    203:  * @toNum = vacuum to number of programs (0 to init number)
                    204:  * return: 0 error or >0 closed programs
                    205:  */
                    206: int io_progVacuum(prog_t * __restrict prg, unsigned int toNum);
                    207: /*
                    208:  * io_progCloseAt() - Close program at pool of certain position
                    209:  *
                    210:  * @prg = program pool
                    211:  * @idx = index at pool
                    212:  * return: 0 error or !=0 closed program
                    213:  */
                    214: int io_progCloseAt(prog_t * __restrict prg, unsigned int idx);
                    215: /*
                    216:  * io_progCloseOf() - Close program at pool with certain handle
                    217:  *
                    218:  * @prg = program pool
                    219:  * @h = handle of program
                    220:  * return: 0 error, >0 closed programs
                    221:  */
                    222: #ifdef POPEN_STREAM
                    223: int io_progCloseOf(prog_t * __restrict prg, FILE *h);
                    224: #else
                    225: int io_progCloseOf(prog_t * __restrict prg, int h);
                    226: #endif
                    227: /*
                    228:  * io_progClose() - Close all programs in pool
                    229:  *
                    230:  * @prg = program pool
                    231:  * @closeNum = close program(s) (0 all)
                    232:  * return: 0 error, >0 closed programs
                    233:  */
                    234: int io_progClose(prog_t * __restrict prg, unsigned int closeNum);
                    235: /*
                    236:  * io_progDestroy() - Destroy entire program pool
                    237:  *
                    238:  * @pprg = program pool
                    239:  * return: none
                    240:  */
                    241: void io_progDestroy(prog_t ** __restrict pprg);
                    242: 
                    243: /*
                    244:  * io_progCheck() - Check exit status of program pool
                    245:  *
                    246:  * @prg = program pool
                    247:  * @re = resurrect program to init number
                    248:  * return: -1 error or >-1 exited programs
                    249:  */
                    250: int io_progCheck(prog_t * __restrict prg, int re);
                    251: 
                    252: /*
                    253:  * io_progAttach() - Attach to open program
                    254:  *
                    255:  * @prg = program pool
                    256:  * @newOne = Execute new one program after attach
                    257:  * return: NULL error or !=NULL attached program handle
                    258:  */
                    259: #ifdef POPEN_STREAM
                    260: FILE *io_progAttach(prog_t * __restrict prg, int newOne);
                    261: #else
                    262: int io_progAttach(prog_t * __restrict prg, int newOne);
                    263: #endif
                    264: /*
                    265:  * io_progDetach() - Detch from open program
                    266:  *
                    267:  * @prg= program pool
                    268:  * @pfd = attached program handle
                    269:  * return: none
                    270:  */
                    271: #ifdef POPEN_STREAM
                    272: void io_progDetach(prog_t * __restrict prg, FILE *pfd);
                    273: #else
                    274: void io_progDetach(prog_t * __restrict prg, int pfd);
                    275: #endif
                    276: 
                    277: 
                    278: /*
1.34      misho     279:  * ioInitSocket() - Init socket and allocate resources
                    280:  *
                    281:  * @role = Socket role
                    282:  * @type = Socket type
                    283:  * @proto = Socket protocol
                    284:  * @addr = Bind to address
                    285:  * @port = Bind to port
                    286:  * @buflen = Socket buffer, optional if =0 == BUFSIZ
                    287:  * return: NULL error or !=NULL created socket
                    288:  */
                    289: sock_t *ioInitSocket(int role, int type, int proto, 
                    290:                const char *addr, unsigned short port, size_t buflen);
                    291: /*
                    292:  * ioCloseSocket() - Close socket and free resources
                    293:  *
                    294:  * @s = Socket
                    295:  * return: none
                    296:  */
                    297: void ioCloseSocket(sock_t ** __restrict s);
1.37      misho     298: #define ioKillSocket(x)                (assert((x)), (x)->sock_kill = 1)
                    299: /*
                    300:  * ioCloseClient() - Close client socket
                    301:  *
                    302:  * @c = Client socket
                    303:  * return: 0 ok or !=0 error
                    304:  */
                    305: int ioCloseClient(sock_cli_t * __restrict c);
1.34      misho     306: /*
1.39      misho     307:  * ioSetupProg2Socket() - Setup program pool to socket server
                    308:  *
                    309:  * @s = Socket
                    310:  * @p = Program pool
                    311:  * return: -1 error or 0 ok
                    312:  */
                    313: int ioSetupProg2Socket(sock_t * __restrict s, prog_t * __restrict p);
                    314: /*
1.34      misho     315:  * ioUpSocket() - Setup socket for use
                    316:  *
                    317:  * @s = Socket
                    318:  * @arg = Server role = listen backlog queue and Client role = peer address
1.37      misho     319:  * @timeout = Socket timeout in ms (default -1 infinit)
1.34      misho     320:  * return: -1 error or 0 ok
                    321:  */
1.37      misho     322: int ioUpSocket(sock_t * __restrict s, void *arg, int timeout);
1.35      misho     323: /*
1.37      misho     324:  * ioUpdTimerSocket() - Update timeout of socket
                    325:  *
                    326:  * @c = Client socket
                    327:  * return:  none
                    328:  */
1.38      misho     329: void ioUpdTimerSocket(sock_cli_t * __restrict c);
1.37      misho     330: /*
                    331:  * ioLoopSocket() - Start socket scheduler
                    332:  *
                    333:  * @s = Socket
                    334:  * @rcb = Read callback
                    335:  * return: -1 error or return result from scheduler
                    336:  */
                    337: int ioLoopSocket(sock_t * __restrict s, sched_task_func_t rcb);
                    338: /*
                    339:  * ioBridgeProg2Socket() - Start socket scheduler and bridge program to socket
1.35      misho     340:  *
                    341:  * @s = Socket
1.37      misho     342:  * @prgname = Program name
                    343:  * return: 0 ok or !=0 error
1.35      misho     344:  */
1.37      misho     345: int ioBridgeProg2Socket(sock_t * __restrict s, const char *prgname);
1.34      misho     346: 
1.39      misho     347: 
1.34      misho     348: /*
1.17      misho     349:  * ioPromptRead() - Read data from input h[0] with prompt to output h[1]
1.16      misho     350:  *
1.1       misho     351:  * @h = file handles h[0] = input, h[1] = output, if NULL use stdin, stdout
                    352:  * @csPrompt = Prompt before input, may be NULL
                    353:  * @psData = Readed data
                    354:  * @dataLen = Length of data
                    355:  * return: 0 EOF; -1 error:: can`t read; >0 count of readed chars
                    356: */
1.37      misho     357: int ioPromptRead(int *h, const char *csPrompt, 
                    358:                char * __restrict psData, int dataLen);
1.1       misho     359: /*
1.17      misho     360:  * ioPromptPassword() - Read password from input h[0] with prompt to output h[1]
1.16      misho     361:  *
1.1       misho     362:  * @h = file handles h[0] = input, h[1] = output, if NULL use stdin, stdout
                    363:  * @csPrompt = Prompt before input, may be NULL
                    364:  * @psPass = Readed password
                    365:  * @passLen = Length of password
                    366:  * @confirm = Confirm password, 0 - get password, !=0 Ask for confirmation
                    367:  * return: 0 EOF; -1 error:: can`t read; >0 count of readed chars
                    368: */
1.37      misho     369: int ioPromptPassword(int *h, const char *csPrompt, 
                    370:                char * __restrict psPass, int passLen, int confirm);
1.1       misho     371: 
1.10      misho     372: 
                    373: /*
1.17      misho     374:  * ioMkDir() - Function for racursive directory creation and validation
1.16      misho     375:  *
1.5       misho     376:  * @csDir = Full directory path
                    377:  * @mode = Mode for directory creation if missing dir
                    378:  * return: -1 error, 0 directory path exist, >0 created missing dirs
                    379: */
                    380: int ioMkDir(const char *csDir, int mode);
                    381: 
1.6       misho     382: /*
1.17      misho     383:  * ioWatchDirLoop() - Function for watching changes in directory and fire callback
1.16      misho     384:  *
1.6       misho     385:  * @csDir = Full directory path
                    386:  * @callback = Callback if raise event! nOp -1 delete, 0 change/move, 1 create
                    387:  * return: -1 error, !=-1 ok, number of total signaled events
                    388: */
                    389: int ioWatchDirLoop(const char *csDir, int (*callback)(const char *csName, int nOp));
                    390: 
1.5       misho     391: 
1.24      misho     392: #ifdef AIO_OPS
                    393: /*
                    394:  * io_aiobulk() - AIO bulk R/W function
                    395:  *
                    396:  * @mode = Bulk wait mode
                    397:  * @acbs = List of aiocb structures
                    398:  * @nacb = Number of aiocb in list
                    399:  * @sig = Event for completed operations, may be =NULL
                    400:  * return: -1 error or 0 ok
                    401:  */
1.30      misho     402: int io_aiobulk(int mode, struct aiocb ** __restrict acbs, int nacb, 
1.24      misho     403:                struct sigevent *sig);
                    404: #endif
                    405: /*
                    406:  * io_rreadv() - Raw VFS bulk read function
                    407:  *
                    408:  * @fd = File handle
                    409:  * @bufs = Read buffers
                    410:  * @nbufs = Number of read buffers
                    411:  * @offset = Read from position, if -1 read nbytes from current position
                    412:  * @update = Update file handle position !0
                    413:  * return: -1 error or !=-1 readed bytes
                    414:  */
                    415: int io_rreadv(int fd, struct iovec * __restrict bufs, int nbufs, off_t offset, 
                    416:                int update);
                    417: /*
                    418:  * io_rwritev() - Raw VFS bulk write function
                    419:  *
                    420:  * @fd = File handle
                    421:  * @bufs = Write buffers
                    422:  * @nbufs = Number of write buffers
                    423:  * @offset = Write to position, if -1 write nbytes to current position
                    424:  * @update = Update file handle position !0
                    425:  * return: -1 error or !=-1 written bytes
                    426:  */
                    427: int io_rwritev(int fd, struct iovec * __restrict bufs, int nbufs, off_t offset, 
                    428:                int update);
1.5       misho     429: /*
1.17      misho     430:  * io_rread() - Raw VFS read function
1.16      misho     431:  *
1.5       misho     432:  * @fd = File handle
                    433:  * @buf = Read buffer
                    434:  * @nbytes = Read buffer size
                    435:  * @offset = Read from position, if -1 read nbytes from current position
                    436:  * @update = Update file handle position !0
                    437:  * return: -1 error or !=-1 readed bytes
                    438:  */
1.30      misho     439: int io_rread(int fd, void * __restrict buf, size_t nbytes, off_t offset, 
1.24      misho     440:                int update);
1.5       misho     441: /*
1.17      misho     442:  * io_rwrite() - Raw VFS write function
1.16      misho     443:  *
1.5       misho     444:  * @fd = File handle
                    445:  * @buf = Write buffer
                    446:  * @nbytes = Write bytes from buffer
                    447:  * @offset = Write at position, if -1 write nbytes from current position
                    448:  * @update = Update file handle position !0
1.24      misho     449:  * return: -1 error or !=-1 written bytes
1.5       misho     450:  */
1.30      misho     451: int io_rwrite(int fd, void * __restrict buf, size_t nbytes, off_t offset, 
1.24      misho     452:                int update);
1.5       misho     453: 
                    454: /* Disk I/O helper macros */
                    455: #define io_read(f, b, n) io_rread(f, b, n, -1, 1)
                    456: #define io_write(f, b, n) io_rwrite(f, b, n, -1, 1)
                    457: 
                    458: 
1.7       misho     459: /* Crypto framework */
                    460: 
                    461: /*
1.17      misho     462:  * ioCipher() - Cipher wrapper for all supported crypto algorythms
1.16      misho     463:  *
1.7       misho     464:  * @pInput = input buffer
                    465:  * @inLen = input buffer len
1.29      misho     466:  * @ppOutput = output allocated buffe, must be e_free after use
1.7       misho     467:  * @Cipher = cipher engine, like EVP_bf_cbc() or etc...
                    468:  * @pKey = key
                    469:  * @pIV = IV, salt (8 bytes)
                    470:  * @nMode = Mode 0 - decrypting or 1 - encrypting
                    471:  * return: 0 not present data or error!; >0 number of processed and returned bytes into ppOutput
                    472: */
                    473: int ioCipher(unsigned char *pInput, int inLen, unsigned char **ppOutput, const EVP_CIPHER *Cipher, 
                    474:                unsigned char *pKey, unsigned char *pIV, int nMode);
                    475: 
                    476: /*
1.17      misho     477:  * io_Blowfish() - Blowfish cipher algorythm, work with ASCII hex strings
1.16      misho     478:  *
1.7       misho     479:  * @pInput = input buffer
                    480:  * @inLen = input buffer len
1.29      misho     481:  * @ppOutput = output allocated buffe, must be e_free after use
1.7       misho     482:  * @pKey = key
                    483:  * @pIV = IV, salt (8 bytes)
                    484:  * @nMode = Mode 0 - decrypting or 1 - encrypting
                    485:  * return: 0 not present data or error!; >0 number of processed and returned bytes into ppOutput
                    486: */
1.8       misho     487: int io_Blowfish(unsigned char *pInput, int inLen, unsigned char **ppOutput, 
                    488:                unsigned char *pKey, unsigned char *pIV, int nMode);
1.12      misho     489: /*
1.17      misho     490:  * io_ctr_AES() - Encrypt/Decrypt stream cipher CTR_AES
1.16      misho     491:  *
1.12      misho     492:  * @pInput = Input buffer with ASCII
                    493:  * @inLen = Input buffer data length
1.29      misho     494:  * @ppOutput = Output buffer with cipher data, must be e_free after use
1.12      misho     495:  * @pKey = Key
                    496:  * @IV = IVector/Nonce/Counter, Warning: IV must be variable, because we write there!!!
                    497:  * return: -1 error or >-1 how many cipher blocks proceeded
                    498:  */
                    499: int io_ctr_AES(unsigned char *pInput, int inLen, unsigned char **ppOutput, 
                    500:                unsigned char *pKey, unsigned char IV[AES_BLOCK_SIZE]);
                    501: 
                    502: 
                    503: /*
1.17      misho     504:  * ioAllocPTY() - Allocate new PTY and TTY
1.16      misho     505:  *
1.12      misho     506:  * @ptyfd = master fd, pty
                    507:  * @ttyfd = slave fd, tty
                    508:  * @name = tty device name if not null
                    509:  * @namesiz = name length, must be above 63 bytes.
                    510:  * @term = termios for terminal
                    511:  * @winz = winsize for terminal
                    512:  * return: -1 error or 0 ok
                    513:  */
1.30      misho     514: int ioAllocPTY(int *ptyfd, int *ttyfd, char * __restrict name, int namesiz, 
1.12      misho     515:                struct termios * __restrict term, struct winsize * __restrict winz);
                    516: /*
1.17      misho     517:  * ioFreePTY() - Release PTY and TTY device
1.16      misho     518:  *
1.12      misho     519:  * @ptyfd = master fd, pty (==-1 skip closing pty)
                    520:  * @ttyname = tty filename
                    521:  * return: none
                    522:  */
1.30      misho     523: void ioFreePTY(int ptyfd, const char *ttyname);
1.12      misho     524: /*
1.17      misho     525:  * ioChgWinPTY() - Change window size of PTY
1.16      misho     526:  *
1.12      misho     527:  * @ptyfd = master fd, pty
                    528:  * @row = row
                    529:  * @col = col
                    530:  * @xpxl = x pixels
                    531:  * @ypxl = y pixels
                    532:  * return: -1 error or 0 ok
                    533:  */
1.30      misho     534: int ioChgWinPTY(int ptyfd, unsigned short row, unsigned short col, 
1.12      misho     535:                unsigned short xpxl, unsigned short ypxl);
                    536: /*
1.17      misho     537:  * ioSetOwnerTTY() - Set owner to TTY
1.16      misho     538:  *
1.12      misho     539:  * @ttyname = tty filename
                    540:  * @UID = uid
                    541:  * @GID = gid
                    542:  * return: -1 error or 0 ok
                    543:  */
                    544: int ioSetOwnerTTY(const char *ttyname, uid_t UID, gid_t GID);
                    545: /*
1.17      misho     546:  * ioSetSidTTY() - Makes the process's controlling TTY and sets it to sane modes.
1.16      misho     547:  *
1.12      misho     548:  * @ttyfd = slave fd, tty
                    549:  * @ttyname = tty filename
                    550:  * return: -1 error or 0 ok
                    551:  */
                    552: int ioSetSidTTY(int *ttyfd, const char *ttyname);
                    553: /*
1.17      misho     554:  * ioSetRAWMode() - Enter into RAW mode
1.16      misho     555:  *
1.12      misho     556:  * @fd = tty fd
                    557:  * @otio = saved old termios for later restore if !=NULL
                    558:  * return: -1 error or 0 ok
                    559:  */
1.30      misho     560: int ioSetRAWMode(int fd, struct termios *otio);
1.12      misho     561: /*
1.17      misho     562:  * ioRestoreMode() - Restore termios to tty fd
1.16      misho     563:  *
1.12      misho     564:  * @fd = tty fd
                    565:  * @tio = termios structure for restore
                    566:  * return: -1 error or 0 ok
                    567:  */
1.30      misho     568: int ioRestoreMode(int fd, struct termios tio);
1.12      misho     569: /*
1.17      misho     570:  * ioForkPTY() - Fork new process with session leader and new TTY
1.16      misho     571:  *
1.12      misho     572:  * @ptyfd = master fd, pty
                    573:  * @name = tty device name if not null
                    574:  * @namesiz = name length, must be above 63 bytes.
                    575:  * @term = termios for terminal
                    576:  * @winz = winsize for terminal
                    577:  * @otio = old termios structure for restore
                    578:  * return: -1 error, 0 child process or >0 parent: pid of child
                    579:  */
                    580: pid_t ioForkPTY(int *ptyfd, char * __restrict name, int namesiz, struct termios * __restrict term, 
                    581:                struct winsize * __restrict winz, struct termios * __restrict otio);
                    582: 
                    583: /*
1.17      misho     584:  * ioCreatePIDFile() - Create PID file
1.16      misho     585:  *
1.12      misho     586:  * @csName = PID filename
                    587:  * @ifExists = !=0 if filename exists return error
                    588:  * return: -1 error or 0 ok
                    589:  */
1.30      misho     590: int ioCreatePIDFile(const char *csName, int ifExists);
1.7       misho     591: 
1.13      misho     592: /*
1.17      misho     593:  * ioSendFile() - AITNET sendfile() userland implementation, not dependant from OS
1.16      misho     594:  *
1.13      misho     595:  * @s = socket
                    596:  * @csFile = file for send
                    597:  * @sendLen = bytes to send, if 0 send all data
                    598:  * @offset = start file offset
                    599:  * @sndbuf = SO_SNDBUF value, if 0 use default
                    600:  * return: 0 error, >0 ok, sended bytes
                    601:  */
                    602: size_t ioSendFile(int s, const char *csFile, size_t sendLen, off_t offset, int sndbuf);
                    603: /*
1.17      misho     604:  * ioRecvFile() - Receive file from socket, fastest (zero-copy) way
1.16      misho     605:  *
1.13      misho     606:  * @s = socket
                    607:  * @csFile = file for receive
                    608:  * @recvLen = receive bytes
                    609:  * @over = overwrite file if exists with mode like 0644
                    610:  * @rcvbuf = SO_RCVBUF value, if 0 use default
                    611:  * return: 0 error, >0 ok, received bytes
                    612:  */
                    613: size_t ioRecvFile(int s, const char *csFile, size_t recvLen, int over, int rcvbuf);
                    614: 
1.31      misho     615: /*
                    616:  * ioRealFileName() - Get real file name
                    617:  *
                    618:  * @fname = filename
                    619:  * return: =NULL error or !=NULL real filename, should be free with e_free()
                    620:  */
                    621: char *ioRealFileName(const char *fname);
1.7       misho     622: 
1.14      misho     623: /* Buffered file access over memory block */
                    624: 
                    625: /*
1.17      misho     626:  * io_fmemopen() - File buffered stream operations over memory block
1.14      misho     627:  *
                    628:  * @base = Base address of memory block, if =NULL Infinit length(auto-grow)
                    629:  * @basesize = Size of memory block
                    630:  * return: NULL error or !=NULL Opened file resource
                    631:  */
                    632: FILE *io_fmemopen(void ** __restrict base, off_t basesize);
                    633: /*
1.17      misho     634:  * io_fmapopen() - File buffered stream operations over MMAP block
1.14      misho     635:  *
                    636:  * @csFile = Filename for MMAP, if =NULL private MMAP block
                    637:  * @mode = File open mode
                    638:  * @perm = If file not exists will be created with this access permissions
                    639:  * @prot = MMAP protection
                    640:  * @flags = MMAP mode flags
                    641:  * @offset = Map from file offset, if csFile==NULL then this is size of MMAP private block
                    642:  * return: NULL error or !=NULL Opened file resource
                    643:  */
                    644: FILE *io_fmapopen(const char *csFile, int mode, int perm, int prot, int flags, off_t offset);
                    645: /*
1.17      misho     646:  * io_fd2buf() - Convert open file handle to buffered file I/O
1.14      misho     647:  *
                    648:  * @fd = File handle
                    649:  * @mode = Permissions for new buffered file I/O
                    650:  * return: NULL error or open buffered file
                    651:  */
1.30      misho     652: FILE *io_fd2buf(int fd, const char *mode);
1.14      misho     653: /*
1.17      misho     654:  * io_dumbFile() - Create empry or dumb file with fixed size
1.14      misho     655:  *
                    656:  * @csFile = Filename for create
                    657:  * @mode = File access permissions
                    658:  * @size = File size
                    659:  * return: -1 error or open file handle
                    660:  */
                    661: int io_dumbFile(const char *csFile, int mode, off_t size);
1.42      misho     662: #define io_emptyFile io_dumbFile
1.14      misho     663: 
                    664: 
1.32      misho     665: /*
                    666:  * io_etherOpen() - Open BPF interface to device
                    667:  *
                    668:  * @csIface = interface name
                    669:  * @flags = open flags
                    670:  * @whdr = with complete headers
1.42      misho     671:  * @wdlt = with data link type, on Linux is protocol number
1.32      misho     672:  * @buflen = buffer length
                    673:  * @zcbuf = zero copy buffer, if BPF supports it and isn't NULL
                    674:  * return: -1 error or >-1 bpf handle
                    675:  */
1.40      misho     676: int io_etherOpen(const char *csIface, int flags, unsigned int whdr, 
                    677:                unsigned int wdlt, unsigned int *buflen, void **zcbuf);
1.32      misho     678: /*
                    679:  * io_etherClose() - Close BPF interface
                    680:  *
                    681:  * @eth = bpf handle
                    682:  * @zcbuf = zero copy buffer, if BPF supports it and isn't NULL
                    683:  * return: none
                    684:  */
                    685: void io_etherClose(int eth, void **zcbuf);
                    686: 
1.42      misho     687: #ifndef __linux__
1.32      misho     688: /*
1.36      misho     689:  * io_etherFilter() - BPF filter routine
                    690:  *
                    691:  * @eth = bpf handle
                    692:  * @io = filter direction 
                    693:  *  (IO_ETHER_FILTER_PROMISC|IO_ETHER_FILTER_NOTREAD|IO_ETHER_FILTER_READ|IO_ETHER_FILTER_WRITE)
                    694:  * @insn = BPF filter instruction array
                    695:  * @insnlen = Length of BPF filter instruction array
                    696:  * return: -1 error or 0 ok
                    697:  */
                    698: int io_etherFilter(int eth, int io, struct bpf_insn * __restrict insn, size_t insnlen);
1.42      misho     699: #endif
1.36      misho     700: 
                    701: /*
1.32      misho     702:  * io_etherSend() - Send packet to bpf
                    703:  *
                    704:  * @eth = bpf handle
                    705:  * @buf = buffer
                    706:  * @buflen = buffer length
                    707:  * return: -1 error or !=-1 written bytes
                    708:  */
                    709: ssize_t io_etherSend(int eth, const void *buf, size_t buflen);
                    710: /*
                    711:  * io_etherRecv() - Receive packet from bpf
                    712:  *
                    713:  * @eth = bpf handle
                    714:  * @buf = buffer
                    715:  * @buflen = buffer length
                    716:  * @zcbuf = zero copy buffer, if BPF supports it and isn't NULL
                    717:  * return: -1 error or !=-1 readed bytes
                    718:  */
                    719: ssize_t io_etherRecv(int eth, void * __restrict buf, 
                    720:                size_t buflen, void * __restrict zcbuf);
                    721: 
                    722: 
1.1       misho     723: #endif

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