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

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

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