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

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

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