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

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

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