Annotation of libaitio/inc/aitio.h, revision 1.31.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.31.2.2! misho       6: * $Id: aitio.h,v 1.31.2.1 2013/06/24 11:09: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.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>
                     62: #include <netinet/in.h>
1.7       misho      63: 
                     64: 
1.21      misho      65: #ifndef STRSIZ
                     66: #define STRSIZ         256
                     67: #endif
                     68: 
1.15      misho      69: 
1.1       misho      70: // io_GetErrno() Get error code of last operation
1.30      misho      71: int io_GetErrno();
1.1       misho      72: // io_GetError() Get error text of last operation
1.30      misho      73: const char *io_GetError();
1.1       misho      74: 
                     75: 
1.12      misho      76: /*
1.17      misho      77:  * ioPromptRead() - Read data from input h[0] with prompt to output h[1]
1.16      misho      78:  *
1.1       misho      79:  * @h = file handles h[0] = input, h[1] = output, if NULL use stdin, stdout
                     80:  * @csPrompt = Prompt before input, may be NULL
                     81:  * @psData = Readed data
                     82:  * @dataLen = Length of data
                     83:  * return: 0 EOF; -1 error:: can`t read; >0 count of readed chars
                     84: */
                     85: int ioPromptRead(int *h, const char *csPrompt, char * __restrict psData, int dataLen);
                     86: /*
1.17      misho      87:  * ioPromptPassword() - Read password from input h[0] with prompt to output h[1]
1.16      misho      88:  *
1.1       misho      89:  * @h = file handles h[0] = input, h[1] = output, if NULL use stdin, stdout
                     90:  * @csPrompt = Prompt before input, may be NULL
                     91:  * @psPass = Readed password
                     92:  * @passLen = Length of password
                     93:  * @confirm = Confirm password, 0 - get password, !=0 Ask for confirmation
                     94:  * return: 0 EOF; -1 error:: can`t read; >0 count of readed chars
                     95: */
                     96: int ioPromptPassword(int *h, const char *csPrompt, char * __restrict psPass, int passLen, int confirm);
                     97: 
1.10      misho      98: 
                     99: /*
1.17      misho     100:  * ioMkDir() - Function for racursive directory creation and validation
1.16      misho     101:  *
1.5       misho     102:  * @csDir = Full directory path
                    103:  * @mode = Mode for directory creation if missing dir
                    104:  * return: -1 error, 0 directory path exist, >0 created missing dirs
                    105: */
                    106: int ioMkDir(const char *csDir, int mode);
                    107: 
1.6       misho     108: /*
1.17      misho     109:  * ioWatchDirLoop() - Function for watching changes in directory and fire callback
1.16      misho     110:  *
1.6       misho     111:  * @csDir = Full directory path
                    112:  * @callback = Callback if raise event! nOp -1 delete, 0 change/move, 1 create
                    113:  * return: -1 error, !=-1 ok, number of total signaled events
                    114: */
                    115: int ioWatchDirLoop(const char *csDir, int (*callback)(const char *csName, int nOp));
                    116: 
1.5       misho     117: 
1.24      misho     118: #ifdef AIO_OPS
                    119: /*
                    120:  * io_aiobulk() - AIO bulk R/W function
                    121:  *
                    122:  * @mode = Bulk wait mode
                    123:  * @acbs = List of aiocb structures
                    124:  * @nacb = Number of aiocb in list
                    125:  * @sig = Event for completed operations, may be =NULL
                    126:  * return: -1 error or 0 ok
                    127:  */
1.30      misho     128: int io_aiobulk(int mode, struct aiocb ** __restrict acbs, int nacb, 
1.24      misho     129:                struct sigevent *sig);
                    130: #endif
                    131: /*
                    132:  * io_rreadv() - Raw VFS bulk read function
                    133:  *
                    134:  * @fd = File handle
                    135:  * @bufs = Read buffers
                    136:  * @nbufs = Number of read buffers
                    137:  * @offset = Read from position, if -1 read nbytes from current position
                    138:  * @update = Update file handle position !0
                    139:  * return: -1 error or !=-1 readed bytes
                    140:  */
                    141: int io_rreadv(int fd, struct iovec * __restrict bufs, int nbufs, off_t offset, 
                    142:                int update);
                    143: /*
                    144:  * io_rwritev() - Raw VFS bulk write function
                    145:  *
                    146:  * @fd = File handle
                    147:  * @bufs = Write buffers
                    148:  * @nbufs = Number of write buffers
                    149:  * @offset = Write to position, if -1 write nbytes to current position
                    150:  * @update = Update file handle position !0
                    151:  * return: -1 error or !=-1 written bytes
                    152:  */
                    153: int io_rwritev(int fd, struct iovec * __restrict bufs, int nbufs, off_t offset, 
                    154:                int update);
1.5       misho     155: /*
1.17      misho     156:  * io_rread() - Raw VFS read function
1.16      misho     157:  *
1.5       misho     158:  * @fd = File handle
                    159:  * @buf = Read buffer
                    160:  * @nbytes = Read buffer size
                    161:  * @offset = Read from position, if -1 read nbytes from current position
                    162:  * @update = Update file handle position !0
                    163:  * return: -1 error or !=-1 readed bytes
                    164:  */
1.30      misho     165: int io_rread(int fd, void * __restrict buf, size_t nbytes, off_t offset, 
1.24      misho     166:                int update);
1.5       misho     167: /*
1.17      misho     168:  * io_rwrite() - Raw VFS write function
1.16      misho     169:  *
1.5       misho     170:  * @fd = File handle
                    171:  * @buf = Write buffer
                    172:  * @nbytes = Write bytes from buffer
                    173:  * @offset = Write at position, if -1 write nbytes from current position
                    174:  * @update = Update file handle position !0
1.24      misho     175:  * return: -1 error or !=-1 written bytes
1.5       misho     176:  */
1.30      misho     177: int io_rwrite(int fd, void * __restrict buf, size_t nbytes, off_t offset, 
1.24      misho     178:                int update);
1.5       misho     179: 
                    180: /* Disk I/O helper macros */
                    181: #define io_read(f, b, n) io_rread(f, b, n, -1, 1)
                    182: #define io_write(f, b, n) io_rwrite(f, b, n, -1, 1)
                    183: 
                    184: 
1.7       misho     185: /* Crypto framework */
                    186: 
                    187: /*
1.17      misho     188:  * ioCipher() - Cipher wrapper for all supported crypto algorythms
1.16      misho     189:  *
1.7       misho     190:  * @pInput = input buffer
                    191:  * @inLen = input buffer len
1.29      misho     192:  * @ppOutput = output allocated buffe, must be e_free after use
1.7       misho     193:  * @Cipher = cipher engine, like EVP_bf_cbc() or etc...
                    194:  * @pKey = key
                    195:  * @pIV = IV, salt (8 bytes)
                    196:  * @nMode = Mode 0 - decrypting or 1 - encrypting
                    197:  * return: 0 not present data or error!; >0 number of processed and returned bytes into ppOutput
                    198: */
                    199: int ioCipher(unsigned char *pInput, int inLen, unsigned char **ppOutput, const EVP_CIPHER *Cipher, 
                    200:                unsigned char *pKey, unsigned char *pIV, int nMode);
                    201: 
                    202: /*
1.17      misho     203:  * io_Blowfish() - Blowfish cipher algorythm, work with ASCII hex strings
1.16      misho     204:  *
1.7       misho     205:  * @pInput = input buffer
                    206:  * @inLen = input buffer len
1.29      misho     207:  * @ppOutput = output allocated buffe, must be e_free after use
1.7       misho     208:  * @pKey = key
                    209:  * @pIV = IV, salt (8 bytes)
                    210:  * @nMode = Mode 0 - decrypting or 1 - encrypting
                    211:  * return: 0 not present data or error!; >0 number of processed and returned bytes into ppOutput
                    212: */
1.8       misho     213: int io_Blowfish(unsigned char *pInput, int inLen, unsigned char **ppOutput, 
                    214:                unsigned char *pKey, unsigned char *pIV, int nMode);
1.12      misho     215: /*
1.17      misho     216:  * io_ctr_AES() - Encrypt/Decrypt stream cipher CTR_AES
1.16      misho     217:  *
1.12      misho     218:  * @pInput = Input buffer with ASCII
                    219:  * @inLen = Input buffer data length
1.29      misho     220:  * @ppOutput = Output buffer with cipher data, must be e_free after use
1.12      misho     221:  * @pKey = Key
                    222:  * @IV = IVector/Nonce/Counter, Warning: IV must be variable, because we write there!!!
                    223:  * return: -1 error or >-1 how many cipher blocks proceeded
                    224:  */
                    225: int io_ctr_AES(unsigned char *pInput, int inLen, unsigned char **ppOutput, 
                    226:                unsigned char *pKey, unsigned char IV[AES_BLOCK_SIZE]);
                    227: 
                    228: 
                    229: /*
1.17      misho     230:  * ioAllocPTY() - Allocate new PTY and TTY
1.16      misho     231:  *
1.12      misho     232:  * @ptyfd = master fd, pty
                    233:  * @ttyfd = slave fd, tty
                    234:  * @name = tty device name if not null
                    235:  * @namesiz = name length, must be above 63 bytes.
                    236:  * @term = termios for terminal
                    237:  * @winz = winsize for terminal
                    238:  * return: -1 error or 0 ok
                    239:  */
1.30      misho     240: int ioAllocPTY(int *ptyfd, int *ttyfd, char * __restrict name, int namesiz, 
1.12      misho     241:                struct termios * __restrict term, struct winsize * __restrict winz);
                    242: /*
1.17      misho     243:  * ioFreePTY() - Release PTY and TTY device
1.16      misho     244:  *
1.12      misho     245:  * @ptyfd = master fd, pty (==-1 skip closing pty)
                    246:  * @ttyname = tty filename
                    247:  * return: none
                    248:  */
1.30      misho     249: void ioFreePTY(int ptyfd, const char *ttyname);
1.12      misho     250: /*
1.17      misho     251:  * ioChgWinPTY() - Change window size of PTY
1.16      misho     252:  *
1.12      misho     253:  * @ptyfd = master fd, pty
                    254:  * @row = row
                    255:  * @col = col
                    256:  * @xpxl = x pixels
                    257:  * @ypxl = y pixels
                    258:  * return: -1 error or 0 ok
                    259:  */
1.30      misho     260: int ioChgWinPTY(int ptyfd, unsigned short row, unsigned short col, 
1.12      misho     261:                unsigned short xpxl, unsigned short ypxl);
                    262: /*
1.17      misho     263:  * ioSetOwnerTTY() - Set owner to TTY
1.16      misho     264:  *
1.12      misho     265:  * @ttyname = tty filename
                    266:  * @UID = uid
                    267:  * @GID = gid
                    268:  * return: -1 error or 0 ok
                    269:  */
                    270: int ioSetOwnerTTY(const char *ttyname, uid_t UID, gid_t GID);
                    271: /*
1.17      misho     272:  * ioSetSidTTY() - Makes the process's controlling TTY and sets it to sane modes.
1.16      misho     273:  *
1.12      misho     274:  * @ttyfd = slave fd, tty
                    275:  * @ttyname = tty filename
                    276:  * return: -1 error or 0 ok
                    277:  */
                    278: int ioSetSidTTY(int *ttyfd, const char *ttyname);
                    279: /*
1.17      misho     280:  * ioSetRAWMode() - Enter into RAW mode
1.16      misho     281:  *
1.12      misho     282:  * @fd = tty fd
                    283:  * @otio = saved old termios for later restore if !=NULL
                    284:  * return: -1 error or 0 ok
                    285:  */
1.30      misho     286: int ioSetRAWMode(int fd, struct termios *otio);
1.12      misho     287: /*
1.17      misho     288:  * ioRestoreMode() - Restore termios to tty fd
1.16      misho     289:  *
1.12      misho     290:  * @fd = tty fd
                    291:  * @tio = termios structure for restore
                    292:  * return: -1 error or 0 ok
                    293:  */
1.30      misho     294: int ioRestoreMode(int fd, struct termios tio);
1.12      misho     295: /*
1.17      misho     296:  * ioForkPTY() - Fork new process with session leader and new TTY
1.16      misho     297:  *
1.12      misho     298:  * @ptyfd = master fd, pty
                    299:  * @name = tty device name if not null
                    300:  * @namesiz = name length, must be above 63 bytes.
                    301:  * @term = termios for terminal
                    302:  * @winz = winsize for terminal
                    303:  * @otio = old termios structure for restore
                    304:  * return: -1 error, 0 child process or >0 parent: pid of child
                    305:  */
                    306: pid_t ioForkPTY(int *ptyfd, char * __restrict name, int namesiz, struct termios * __restrict term, 
                    307:                struct winsize * __restrict winz, struct termios * __restrict otio);
                    308: 
                    309: /*
1.17      misho     310:  * ioCreatePIDFile() - Create PID file
1.16      misho     311:  *
1.12      misho     312:  * @csName = PID filename
                    313:  * @ifExists = !=0 if filename exists return error
                    314:  * return: -1 error or 0 ok
                    315:  */
1.30      misho     316: int ioCreatePIDFile(const char *csName, int ifExists);
1.7       misho     317: 
1.13      misho     318: /*
1.17      misho     319:  * ioSendFile() - AITNET sendfile() userland implementation, not dependant from OS
1.16      misho     320:  *
1.13      misho     321:  * @s = socket
                    322:  * @csFile = file for send
                    323:  * @sendLen = bytes to send, if 0 send all data
                    324:  * @offset = start file offset
                    325:  * @sndbuf = SO_SNDBUF value, if 0 use default
                    326:  * return: 0 error, >0 ok, sended bytes
                    327:  */
                    328: size_t ioSendFile(int s, const char *csFile, size_t sendLen, off_t offset, int sndbuf);
                    329: /*
1.17      misho     330:  * ioRecvFile() - Receive file from socket, fastest (zero-copy) way
1.16      misho     331:  *
1.13      misho     332:  * @s = socket
                    333:  * @csFile = file for receive
                    334:  * @recvLen = receive bytes
                    335:  * @over = overwrite file if exists with mode like 0644
                    336:  * @rcvbuf = SO_RCVBUF value, if 0 use default
                    337:  * return: 0 error, >0 ok, received bytes
                    338:  */
                    339: size_t ioRecvFile(int s, const char *csFile, size_t recvLen, int over, int rcvbuf);
                    340: 
1.31      misho     341: /*
                    342:  * ioRealFileName() - Get real file name
                    343:  *
                    344:  * @fname = filename
                    345:  * return: =NULL error or !=NULL real filename, should be free with e_free()
                    346:  */
                    347: char *ioRealFileName(const char *fname);
1.7       misho     348: 
1.14      misho     349: /* Buffered file access over memory block */
                    350: 
                    351: /*
1.17      misho     352:  * io_fmemopen() - File buffered stream operations over memory block
1.14      misho     353:  *
                    354:  * @base = Base address of memory block, if =NULL Infinit length(auto-grow)
                    355:  * @basesize = Size of memory block
                    356:  * return: NULL error or !=NULL Opened file resource
                    357:  */
                    358: FILE *io_fmemopen(void ** __restrict base, off_t basesize);
                    359: /*
1.17      misho     360:  * io_fmapopen() - File buffered stream operations over MMAP block
1.14      misho     361:  *
                    362:  * @csFile = Filename for MMAP, if =NULL private MMAP block
                    363:  * @mode = File open mode
                    364:  * @perm = If file not exists will be created with this access permissions
                    365:  * @prot = MMAP protection
                    366:  * @flags = MMAP mode flags
                    367:  * @offset = Map from file offset, if csFile==NULL then this is size of MMAP private block
                    368:  * return: NULL error or !=NULL Opened file resource
                    369:  */
                    370: FILE *io_fmapopen(const char *csFile, int mode, int perm, int prot, int flags, off_t offset);
                    371: /*
1.17      misho     372:  * io_fd2buf() - Convert open file handle to buffered file I/O
1.14      misho     373:  *
                    374:  * @fd = File handle
                    375:  * @mode = Permissions for new buffered file I/O
                    376:  * return: NULL error or open buffered file
                    377:  */
1.30      misho     378: FILE *io_fd2buf(int fd, const char *mode);
1.14      misho     379: /*
1.17      misho     380:  * io_dumbFile() - Create empry or dumb file with fixed size
1.14      misho     381:  *
                    382:  * @csFile = Filename for create
                    383:  * @mode = File access permissions
                    384:  * @size = File size
                    385:  * return: -1 error or open file handle
                    386:  */
                    387: int io_dumbFile(const char *csFile, int mode, off_t size);
                    388: 
                    389: 
1.31.2.1  misho     390: /*
                    391:  * io_get1stiface() - Get first interface of host
                    392:  *
                    393:  * @szIface = interface string buffer
                    394:  * @iflen = size of interface buffer
                    395:  * return: -1 error or 0 ok
                    396:  */
                    397: int io_get1stiface(char *szIface, int iflen);
                    398: /*
                    399:  * io_etherOpen() - Open BPF interface to device
                    400:  *
                    401:  * @csIface = interface name
                    402:  * @flags = open flags
1.31.2.2! misho     403:  * @whdr = with complete headers
        !           404:  * @buflen = buffer length
        !           405:  * @zcbuf = zero copy buffer, if BPF supports it and isn't NULL
1.31.2.1  misho     406:  * return: -1 error or >-1 bpf handle
                    407:  */
1.31.2.2! misho     408: int io_etherOpen(const char *csIface, int flags, int whdr, 
        !           409:                unsigned int buflen, void **zcbuf);
1.31.2.1  misho     410: /*
                    411:  * io_etherClose() - Close BPF interface
                    412:  *
                    413:  * @eth = bpf handle
1.31.2.2! misho     414:  * @zcbuf = zero copy buffer, if BPF supports it and isn't NULL
1.31.2.1  misho     415:  * return: none
                    416:  */
1.31.2.2! misho     417: void io_etherClose(int eth, void **zcbuf);
1.31.2.1  misho     418: 
                    419: 
1.1       misho     420: #endif

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