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

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.34    ! misho       6: * $Id: aitio.h,v 1.33.2.2 2013/08/12 23:25:35 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.29      misho      15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
1.8       misho      16:        by Michael Pounov <misho@elwix.org>.  All rights reserved.
                     17: 
                     18: Redistribution and use in source and binary forms, with or without
                     19: modification, are permitted provided that the following conditions
                     20: are met:
                     21: 1. Redistributions of source code must retain the above copyright
                     22:    notice, this list of conditions and the following disclaimer.
                     23: 2. Redistributions in binary form must reproduce the above copyright
                     24:    notice, this list of conditions and the following disclaimer in the
                     25:    documentation and/or other materials provided with the distribution.
                     26: 3. All advertising materials mentioning features or use of this software
                     27:    must display the following acknowledgement:
                     28: This product includes software developed by Michael Pounov <misho@elwix.org>
                     29: ELWIX - Embedded LightWeight unIX and its contributors.
                     30: 4. Neither the name of AITNET nor the names of its contributors
                     31:    may be used to endorse or promote products derived from this software
                     32:    without specific prior written permission.
                     33: 
                     34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
                     35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     44: SUCH DAMAGE.
                     45: */
1.1       misho      46: #ifndef __AITIO_H
                     47: #define __AITIO_H
                     48: 
                     49: 
1.12      misho      50: #define COMPAT_43TTY
                     51: 
1.8       misho      52: #include <assert.h>
1.24      misho      53: #include <syslog.h>
1.7       misho      54: #include <openssl/evp.h>
1.12      misho      55: #include <openssl/aes.h>
                     56: #include <sys/tty.h>
                     57: #include <sys/ioctl_compat.h>
1.13      misho      58: #include <sys/socket.h>
                     59: #include <sys/un.h>
1.24      misho      60: #include <sys/uio.h>
1.13      misho      61: #include <net/if_dl.h>
1.33      misho      62: #include <net/bpf.h>
1.13      misho      63: #include <netinet/in.h>
1.34    ! misho      64: #include <elwix.h>
1.7       misho      65: 
                     66: 
1.21      misho      67: #ifndef STRSIZ
1.34    ! misho      68: #define STRSIZ                 256
1.21      misho      69: #endif
                     70: 
1.34    ! misho      71: #define IO_SOCK_ROLE_CLIENT    0
        !            72: #define IO_SOCK_ROLE_SERVER    1
        !            73: 
        !            74: 
        !            75: typedef struct {
        !            76:        int             sock_role;
        !            77:        int             sock_backq;
        !            78:        int             sock_type;
        !            79:        int             sock_proto;
        !            80:        int             sock_fd;
        !            81:        sockaddr_t      sock_addr;
        !            82: 
        !            83:        sockaddr_t      sock_peer;
        !            84: 
        !            85:        ait_val_t       sock_buf;
        !            86: } sock_t;
        !            87: 
1.15      misho      88: 
1.1       misho      89: // io_GetErrno() Get error code of last operation
1.30      misho      90: int io_GetErrno();
1.1       misho      91: // io_GetError() Get error text of last operation
1.30      misho      92: const char *io_GetError();
1.1       misho      93: 
                     94: 
1.12      misho      95: /*
1.34    ! misho      96:  * ioInitSocket() - Init socket and allocate resources
        !            97:  *
        !            98:  * @role = Socket role
        !            99:  * @type = Socket type
        !           100:  * @proto = Socket protocol
        !           101:  * @addr = Bind to address
        !           102:  * @port = Bind to port
        !           103:  * @buflen = Socket buffer, optional if =0 == BUFSIZ
        !           104:  * return: NULL error or !=NULL created socket
        !           105:  */
        !           106: sock_t *ioInitSocket(int role, int type, int proto, 
        !           107:                const char *addr, unsigned short port, size_t buflen);
        !           108: /*
        !           109:  * ioCloseSocket() - Close socket and free resources
        !           110:  *
        !           111:  * @s = Socket
        !           112:  * return: none
        !           113:  */
        !           114: void ioCloseSocket(sock_t ** __restrict s);
        !           115: /*
        !           116:  * ioUpSocket() - Setup socket for use
        !           117:  *
        !           118:  * @s = Socket
        !           119:  * @arg = Server role = listen backlog queue and Client role = peer address
        !           120:  * return: -1 error or 0 ok
        !           121:  */
        !           122: int ioUpSocket(sock_t * __restrict s, void *arg);
        !           123: 
        !           124: /*
1.17      misho     125:  * ioPromptRead() - Read data from input h[0] with prompt to output h[1]
1.16      misho     126:  *
1.1       misho     127:  * @h = file handles h[0] = input, h[1] = output, if NULL use stdin, stdout
                    128:  * @csPrompt = Prompt before input, may be NULL
                    129:  * @psData = Readed data
                    130:  * @dataLen = Length of data
                    131:  * return: 0 EOF; -1 error:: can`t read; >0 count of readed chars
                    132: */
                    133: int ioPromptRead(int *h, const char *csPrompt, char * __restrict psData, int dataLen);
                    134: /*
1.17      misho     135:  * ioPromptPassword() - Read password from input h[0] with prompt to output h[1]
1.16      misho     136:  *
1.1       misho     137:  * @h = file handles h[0] = input, h[1] = output, if NULL use stdin, stdout
                    138:  * @csPrompt = Prompt before input, may be NULL
                    139:  * @psPass = Readed password
                    140:  * @passLen = Length of password
                    141:  * @confirm = Confirm password, 0 - get password, !=0 Ask for confirmation
                    142:  * return: 0 EOF; -1 error:: can`t read; >0 count of readed chars
                    143: */
                    144: int ioPromptPassword(int *h, const char *csPrompt, char * __restrict psPass, int passLen, int confirm);
                    145: 
1.10      misho     146: 
                    147: /*
1.17      misho     148:  * ioMkDir() - Function for racursive directory creation and validation
1.16      misho     149:  *
1.5       misho     150:  * @csDir = Full directory path
                    151:  * @mode = Mode for directory creation if missing dir
                    152:  * return: -1 error, 0 directory path exist, >0 created missing dirs
                    153: */
                    154: int ioMkDir(const char *csDir, int mode);
                    155: 
1.6       misho     156: /*
1.17      misho     157:  * ioWatchDirLoop() - Function for watching changes in directory and fire callback
1.16      misho     158:  *
1.6       misho     159:  * @csDir = Full directory path
                    160:  * @callback = Callback if raise event! nOp -1 delete, 0 change/move, 1 create
                    161:  * return: -1 error, !=-1 ok, number of total signaled events
                    162: */
                    163: int ioWatchDirLoop(const char *csDir, int (*callback)(const char *csName, int nOp));
                    164: 
1.5       misho     165: 
1.24      misho     166: #ifdef AIO_OPS
                    167: /*
                    168:  * io_aiobulk() - AIO bulk R/W function
                    169:  *
                    170:  * @mode = Bulk wait mode
                    171:  * @acbs = List of aiocb structures
                    172:  * @nacb = Number of aiocb in list
                    173:  * @sig = Event for completed operations, may be =NULL
                    174:  * return: -1 error or 0 ok
                    175:  */
1.30      misho     176: int io_aiobulk(int mode, struct aiocb ** __restrict acbs, int nacb, 
1.24      misho     177:                struct sigevent *sig);
                    178: #endif
                    179: /*
                    180:  * io_rreadv() - Raw VFS bulk read function
                    181:  *
                    182:  * @fd = File handle
                    183:  * @bufs = Read buffers
                    184:  * @nbufs = Number of read buffers
                    185:  * @offset = Read from position, if -1 read nbytes from current position
                    186:  * @update = Update file handle position !0
                    187:  * return: -1 error or !=-1 readed bytes
                    188:  */
                    189: int io_rreadv(int fd, struct iovec * __restrict bufs, int nbufs, off_t offset, 
                    190:                int update);
                    191: /*
                    192:  * io_rwritev() - Raw VFS bulk write function
                    193:  *
                    194:  * @fd = File handle
                    195:  * @bufs = Write buffers
                    196:  * @nbufs = Number of write buffers
                    197:  * @offset = Write to position, if -1 write nbytes to current position
                    198:  * @update = Update file handle position !0
                    199:  * return: -1 error or !=-1 written bytes
                    200:  */
                    201: int io_rwritev(int fd, struct iovec * __restrict bufs, int nbufs, off_t offset, 
                    202:                int update);
1.5       misho     203: /*
1.17      misho     204:  * io_rread() - Raw VFS read function
1.16      misho     205:  *
1.5       misho     206:  * @fd = File handle
                    207:  * @buf = Read buffer
                    208:  * @nbytes = Read buffer size
                    209:  * @offset = Read from position, if -1 read nbytes from current position
                    210:  * @update = Update file handle position !0
                    211:  * return: -1 error or !=-1 readed bytes
                    212:  */
1.30      misho     213: int io_rread(int fd, void * __restrict buf, size_t nbytes, off_t offset, 
1.24      misho     214:                int update);
1.5       misho     215: /*
1.17      misho     216:  * io_rwrite() - Raw VFS write function
1.16      misho     217:  *
1.5       misho     218:  * @fd = File handle
                    219:  * @buf = Write buffer
                    220:  * @nbytes = Write bytes from buffer
                    221:  * @offset = Write at position, if -1 write nbytes from current position
                    222:  * @update = Update file handle position !0
1.24      misho     223:  * return: -1 error or !=-1 written bytes
1.5       misho     224:  */
1.30      misho     225: int io_rwrite(int fd, void * __restrict buf, size_t nbytes, off_t offset, 
1.24      misho     226:                int update);
1.5       misho     227: 
                    228: /* Disk I/O helper macros */
                    229: #define io_read(f, b, n) io_rread(f, b, n, -1, 1)
                    230: #define io_write(f, b, n) io_rwrite(f, b, n, -1, 1)
                    231: 
                    232: 
1.7       misho     233: /* Crypto framework */
                    234: 
                    235: /*
1.17      misho     236:  * ioCipher() - Cipher wrapper for all supported crypto algorythms
1.16      misho     237:  *
1.7       misho     238:  * @pInput = input buffer
                    239:  * @inLen = input buffer len
1.29      misho     240:  * @ppOutput = output allocated buffe, must be e_free after use
1.7       misho     241:  * @Cipher = cipher engine, like EVP_bf_cbc() or etc...
                    242:  * @pKey = key
                    243:  * @pIV = IV, salt (8 bytes)
                    244:  * @nMode = Mode 0 - decrypting or 1 - encrypting
                    245:  * return: 0 not present data or error!; >0 number of processed and returned bytes into ppOutput
                    246: */
                    247: int ioCipher(unsigned char *pInput, int inLen, unsigned char **ppOutput, const EVP_CIPHER *Cipher, 
                    248:                unsigned char *pKey, unsigned char *pIV, int nMode);
                    249: 
                    250: /*
1.17      misho     251:  * io_Blowfish() - Blowfish cipher algorythm, work with ASCII hex strings
1.16      misho     252:  *
1.7       misho     253:  * @pInput = input buffer
                    254:  * @inLen = input buffer len
1.29      misho     255:  * @ppOutput = output allocated buffe, must be e_free after use
1.7       misho     256:  * @pKey = key
                    257:  * @pIV = IV, salt (8 bytes)
                    258:  * @nMode = Mode 0 - decrypting or 1 - encrypting
                    259:  * return: 0 not present data or error!; >0 number of processed and returned bytes into ppOutput
                    260: */
1.8       misho     261: int io_Blowfish(unsigned char *pInput, int inLen, unsigned char **ppOutput, 
                    262:                unsigned char *pKey, unsigned char *pIV, int nMode);
1.12      misho     263: /*
1.17      misho     264:  * io_ctr_AES() - Encrypt/Decrypt stream cipher CTR_AES
1.16      misho     265:  *
1.12      misho     266:  * @pInput = Input buffer with ASCII
                    267:  * @inLen = Input buffer data length
1.29      misho     268:  * @ppOutput = Output buffer with cipher data, must be e_free after use
1.12      misho     269:  * @pKey = Key
                    270:  * @IV = IVector/Nonce/Counter, Warning: IV must be variable, because we write there!!!
                    271:  * return: -1 error or >-1 how many cipher blocks proceeded
                    272:  */
                    273: int io_ctr_AES(unsigned char *pInput, int inLen, unsigned char **ppOutput, 
                    274:                unsigned char *pKey, unsigned char IV[AES_BLOCK_SIZE]);
                    275: 
                    276: 
                    277: /*
1.17      misho     278:  * ioAllocPTY() - Allocate new PTY and TTY
1.16      misho     279:  *
1.12      misho     280:  * @ptyfd = master fd, pty
                    281:  * @ttyfd = slave fd, tty
                    282:  * @name = tty device name if not null
                    283:  * @namesiz = name length, must be above 63 bytes.
                    284:  * @term = termios for terminal
                    285:  * @winz = winsize for terminal
                    286:  * return: -1 error or 0 ok
                    287:  */
1.30      misho     288: int ioAllocPTY(int *ptyfd, int *ttyfd, char * __restrict name, int namesiz, 
1.12      misho     289:                struct termios * __restrict term, struct winsize * __restrict winz);
                    290: /*
1.17      misho     291:  * ioFreePTY() - Release PTY and TTY device
1.16      misho     292:  *
1.12      misho     293:  * @ptyfd = master fd, pty (==-1 skip closing pty)
                    294:  * @ttyname = tty filename
                    295:  * return: none
                    296:  */
1.30      misho     297: void ioFreePTY(int ptyfd, const char *ttyname);
1.12      misho     298: /*
1.17      misho     299:  * ioChgWinPTY() - Change window size of PTY
1.16      misho     300:  *
1.12      misho     301:  * @ptyfd = master fd, pty
                    302:  * @row = row
                    303:  * @col = col
                    304:  * @xpxl = x pixels
                    305:  * @ypxl = y pixels
                    306:  * return: -1 error or 0 ok
                    307:  */
1.30      misho     308: int ioChgWinPTY(int ptyfd, unsigned short row, unsigned short col, 
1.12      misho     309:                unsigned short xpxl, unsigned short ypxl);
                    310: /*
1.17      misho     311:  * ioSetOwnerTTY() - Set owner to TTY
1.16      misho     312:  *
1.12      misho     313:  * @ttyname = tty filename
                    314:  * @UID = uid
                    315:  * @GID = gid
                    316:  * return: -1 error or 0 ok
                    317:  */
                    318: int ioSetOwnerTTY(const char *ttyname, uid_t UID, gid_t GID);
                    319: /*
1.17      misho     320:  * ioSetSidTTY() - Makes the process's controlling TTY and sets it to sane modes.
1.16      misho     321:  *
1.12      misho     322:  * @ttyfd = slave fd, tty
                    323:  * @ttyname = tty filename
                    324:  * return: -1 error or 0 ok
                    325:  */
                    326: int ioSetSidTTY(int *ttyfd, const char *ttyname);
                    327: /*
1.17      misho     328:  * ioSetRAWMode() - Enter into RAW mode
1.16      misho     329:  *
1.12      misho     330:  * @fd = tty fd
                    331:  * @otio = saved old termios for later restore if !=NULL
                    332:  * return: -1 error or 0 ok
                    333:  */
1.30      misho     334: int ioSetRAWMode(int fd, struct termios *otio);
1.12      misho     335: /*
1.17      misho     336:  * ioRestoreMode() - Restore termios to tty fd
1.16      misho     337:  *
1.12      misho     338:  * @fd = tty fd
                    339:  * @tio = termios structure for restore
                    340:  * return: -1 error or 0 ok
                    341:  */
1.30      misho     342: int ioRestoreMode(int fd, struct termios tio);
1.12      misho     343: /*
1.17      misho     344:  * ioForkPTY() - Fork new process with session leader and new TTY
1.16      misho     345:  *
1.12      misho     346:  * @ptyfd = master fd, pty
                    347:  * @name = tty device name if not null
                    348:  * @namesiz = name length, must be above 63 bytes.
                    349:  * @term = termios for terminal
                    350:  * @winz = winsize for terminal
                    351:  * @otio = old termios structure for restore
                    352:  * return: -1 error, 0 child process or >0 parent: pid of child
                    353:  */
                    354: pid_t ioForkPTY(int *ptyfd, char * __restrict name, int namesiz, struct termios * __restrict term, 
                    355:                struct winsize * __restrict winz, struct termios * __restrict otio);
                    356: 
                    357: /*
1.17      misho     358:  * ioCreatePIDFile() - Create PID file
1.16      misho     359:  *
1.12      misho     360:  * @csName = PID filename
                    361:  * @ifExists = !=0 if filename exists return error
                    362:  * return: -1 error or 0 ok
                    363:  */
1.30      misho     364: int ioCreatePIDFile(const char *csName, int ifExists);
1.7       misho     365: 
1.13      misho     366: /*
1.17      misho     367:  * ioSendFile() - AITNET sendfile() userland implementation, not dependant from OS
1.16      misho     368:  *
1.13      misho     369:  * @s = socket
                    370:  * @csFile = file for send
                    371:  * @sendLen = bytes to send, if 0 send all data
                    372:  * @offset = start file offset
                    373:  * @sndbuf = SO_SNDBUF value, if 0 use default
                    374:  * return: 0 error, >0 ok, sended bytes
                    375:  */
                    376: size_t ioSendFile(int s, const char *csFile, size_t sendLen, off_t offset, int sndbuf);
                    377: /*
1.17      misho     378:  * ioRecvFile() - Receive file from socket, fastest (zero-copy) way
1.16      misho     379:  *
1.13      misho     380:  * @s = socket
                    381:  * @csFile = file for receive
                    382:  * @recvLen = receive bytes
                    383:  * @over = overwrite file if exists with mode like 0644
                    384:  * @rcvbuf = SO_RCVBUF value, if 0 use default
                    385:  * return: 0 error, >0 ok, received bytes
                    386:  */
                    387: size_t ioRecvFile(int s, const char *csFile, size_t recvLen, int over, int rcvbuf);
                    388: 
1.31      misho     389: /*
                    390:  * ioRealFileName() - Get real file name
                    391:  *
                    392:  * @fname = filename
                    393:  * return: =NULL error or !=NULL real filename, should be free with e_free()
                    394:  */
                    395: char *ioRealFileName(const char *fname);
1.7       misho     396: 
1.14      misho     397: /* Buffered file access over memory block */
                    398: 
                    399: /*
1.17      misho     400:  * io_fmemopen() - File buffered stream operations over memory block
1.14      misho     401:  *
                    402:  * @base = Base address of memory block, if =NULL Infinit length(auto-grow)
                    403:  * @basesize = Size of memory block
                    404:  * return: NULL error or !=NULL Opened file resource
                    405:  */
                    406: FILE *io_fmemopen(void ** __restrict base, off_t basesize);
                    407: /*
1.17      misho     408:  * io_fmapopen() - File buffered stream operations over MMAP block
1.14      misho     409:  *
                    410:  * @csFile = Filename for MMAP, if =NULL private MMAP block
                    411:  * @mode = File open mode
                    412:  * @perm = If file not exists will be created with this access permissions
                    413:  * @prot = MMAP protection
                    414:  * @flags = MMAP mode flags
                    415:  * @offset = Map from file offset, if csFile==NULL then this is size of MMAP private block
                    416:  * return: NULL error or !=NULL Opened file resource
                    417:  */
                    418: FILE *io_fmapopen(const char *csFile, int mode, int perm, int prot, int flags, off_t offset);
                    419: /*
1.17      misho     420:  * io_fd2buf() - Convert open file handle to buffered file I/O
1.14      misho     421:  *
                    422:  * @fd = File handle
                    423:  * @mode = Permissions for new buffered file I/O
                    424:  * return: NULL error or open buffered file
                    425:  */
1.30      misho     426: FILE *io_fd2buf(int fd, const char *mode);
1.14      misho     427: /*
1.17      misho     428:  * io_dumbFile() - Create empry or dumb file with fixed size
1.14      misho     429:  *
                    430:  * @csFile = Filename for create
                    431:  * @mode = File access permissions
                    432:  * @size = File size
                    433:  * return: -1 error or open file handle
                    434:  */
                    435: int io_dumbFile(const char *csFile, int mode, off_t size);
                    436: 
                    437: 
1.32      misho     438: /*
                    439:  * io_get1stiface() - Get first interface of host
                    440:  *
                    441:  * @szIface = interface string buffer
                    442:  * @iflen = size of interface buffer
                    443:  * return: -1 error or 0 ok
                    444:  */
                    445: int io_get1stiface(char *szIface, int iflen);
                    446: /*
                    447:  * io_etherOpen() - Open BPF interface to device
                    448:  *
                    449:  * @csIface = interface name
                    450:  * @flags = open flags
                    451:  * @whdr = with complete headers
1.33      misho     452:  * @wdlt = with data link type
1.32      misho     453:  * @buflen = buffer length
                    454:  * @zcbuf = zero copy buffer, if BPF supports it and isn't NULL
                    455:  * return: -1 error or >-1 bpf handle
                    456:  */
1.33      misho     457: int io_etherOpen(const char *csIface, int flags, int whdr, int wdlt, 
1.32      misho     458:                unsigned int *buflen, void **zcbuf);
                    459: /*
                    460:  * io_etherClose() - Close BPF interface
                    461:  *
                    462:  * @eth = bpf handle
                    463:  * @zcbuf = zero copy buffer, if BPF supports it and isn't NULL
                    464:  * return: none
                    465:  */
                    466: void io_etherClose(int eth, void **zcbuf);
                    467: 
                    468: /*
                    469:  * io_etherSend() - Send packet to bpf
                    470:  *
                    471:  * @eth = bpf handle
                    472:  * @buf = buffer
                    473:  * @buflen = buffer length
                    474:  * return: -1 error or !=-1 written bytes
                    475:  */
                    476: ssize_t io_etherSend(int eth, const void *buf, size_t buflen);
                    477: /*
                    478:  * io_etherRecv() - Receive packet from bpf
                    479:  *
                    480:  * @eth = bpf handle
                    481:  * @buf = buffer
                    482:  * @buflen = buffer length
                    483:  * @zcbuf = zero copy buffer, if BPF supports it and isn't NULL
                    484:  * return: -1 error or !=-1 readed bytes
                    485:  */
                    486: ssize_t io_etherRecv(int eth, void * __restrict buf, 
                    487:                size_t buflen, void * __restrict zcbuf);
                    488: 
                    489: 
1.1       misho     490: #endif

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