File:  [ELWIX - Embedded LightWeight unIX -] / libaitio / inc / aitio.h
Revision 1.32: download - view: text, annotated - select for diffs - revision graph
Wed Jun 26 22:48:53 2013 UTC (11 years ago) by misho
Branches: MAIN
CVS tags: io5_5, IO5_4, HEAD
version 5.4

    1: /*************************************************************************
    2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
    3: *  by Michael Pounov <misho@elwix.org>
    4: *
    5: * $Author: misho $
    6: * $Id: aitio.h,v 1.32 2013/06/26 22:48:53 misho Exp $
    7: *
    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: 
   15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
   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: */
   46: #ifndef __AITIO_H
   47: #define __AITIO_H
   48: 
   49: 
   50: #define COMPAT_43TTY
   51: 
   52: #include <assert.h>
   53: #include <syslog.h>
   54: #include <openssl/evp.h>
   55: #include <openssl/aes.h>
   56: #include <sys/tty.h>
   57: #include <sys/ioctl_compat.h>
   58: #include <sys/socket.h>
   59: #include <sys/un.h>
   60: #include <sys/uio.h>
   61: #include <net/if_dl.h>
   62: #include <netinet/in.h>
   63: 
   64: 
   65: #ifndef STRSIZ
   66: #define STRSIZ		256
   67: #endif
   68: 
   69: 
   70: // io_GetErrno() Get error code of last operation
   71: int io_GetErrno();
   72: // io_GetError() Get error text of last operation
   73: const char *io_GetError();
   74: 
   75: 
   76: /*
   77:  * ioPromptRead() - Read data from input h[0] with prompt to output h[1]
   78:  *
   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: /*
   87:  * ioPromptPassword() - Read password from input h[0] with prompt to output h[1]
   88:  *
   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: 
   98: 
   99: /*
  100:  * ioMkDir() - Function for racursive directory creation and validation
  101:  *
  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: 
  108: /*
  109:  * ioWatchDirLoop() - Function for watching changes in directory and fire callback
  110:  *
  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: 
  117: 
  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:  */
  128: int io_aiobulk(int mode, struct aiocb ** __restrict acbs, int nacb, 
  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);
  155: /*
  156:  * io_rread() - Raw VFS read function
  157:  *
  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:  */
  165: int io_rread(int fd, void * __restrict buf, size_t nbytes, off_t offset, 
  166: 		int update);
  167: /*
  168:  * io_rwrite() - Raw VFS write function
  169:  *
  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
  175:  * return: -1 error or !=-1 written bytes
  176:  */
  177: int io_rwrite(int fd, void * __restrict buf, size_t nbytes, off_t offset, 
  178: 		int update);
  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: 
  185: /* Crypto framework */
  186: 
  187: /*
  188:  * ioCipher() - Cipher wrapper for all supported crypto algorythms
  189:  *
  190:  * @pInput = input buffer
  191:  * @inLen = input buffer len
  192:  * @ppOutput = output allocated buffe, must be e_free after use
  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: /*
  203:  * io_Blowfish() - Blowfish cipher algorythm, work with ASCII hex strings
  204:  *
  205:  * @pInput = input buffer
  206:  * @inLen = input buffer len
  207:  * @ppOutput = output allocated buffe, must be e_free after use
  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: */
  213: int io_Blowfish(unsigned char *pInput, int inLen, unsigned char **ppOutput, 
  214: 		unsigned char *pKey, unsigned char *pIV, int nMode);
  215: /*
  216:  * io_ctr_AES() - Encrypt/Decrypt stream cipher CTR_AES
  217:  *
  218:  * @pInput = Input buffer with ASCII
  219:  * @inLen = Input buffer data length
  220:  * @ppOutput = Output buffer with cipher data, must be e_free after use
  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: /*
  230:  * ioAllocPTY() - Allocate new PTY and TTY
  231:  *
  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:  */
  240: int ioAllocPTY(int *ptyfd, int *ttyfd, char * __restrict name, int namesiz, 
  241: 		struct termios * __restrict term, struct winsize * __restrict winz);
  242: /*
  243:  * ioFreePTY() - Release PTY and TTY device
  244:  *
  245:  * @ptyfd = master fd, pty (==-1 skip closing pty)
  246:  * @ttyname = tty filename
  247:  * return: none
  248:  */
  249: void ioFreePTY(int ptyfd, const char *ttyname);
  250: /*
  251:  * ioChgWinPTY() - Change window size of PTY
  252:  *
  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:  */
  260: int ioChgWinPTY(int ptyfd, unsigned short row, unsigned short col, 
  261: 		unsigned short xpxl, unsigned short ypxl);
  262: /*
  263:  * ioSetOwnerTTY() - Set owner to TTY
  264:  *
  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: /*
  272:  * ioSetSidTTY() - Makes the process's controlling TTY and sets it to sane modes.
  273:  *
  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: /*
  280:  * ioSetRAWMode() - Enter into RAW mode
  281:  *
  282:  * @fd = tty fd
  283:  * @otio = saved old termios for later restore if !=NULL
  284:  * return: -1 error or 0 ok
  285:  */
  286: int ioSetRAWMode(int fd, struct termios *otio);
  287: /*
  288:  * ioRestoreMode() - Restore termios to tty fd
  289:  *
  290:  * @fd = tty fd
  291:  * @tio = termios structure for restore
  292:  * return: -1 error or 0 ok
  293:  */
  294: int ioRestoreMode(int fd, struct termios tio);
  295: /*
  296:  * ioForkPTY() - Fork new process with session leader and new TTY
  297:  *
  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: /*
  310:  * ioCreatePIDFile() - Create PID file
  311:  *
  312:  * @csName = PID filename
  313:  * @ifExists = !=0 if filename exists return error
  314:  * return: -1 error or 0 ok
  315:  */
  316: int ioCreatePIDFile(const char *csName, int ifExists);
  317: 
  318: /*
  319:  * ioSendFile() - AITNET sendfile() userland implementation, not dependant from OS
  320:  *
  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: /*
  330:  * ioRecvFile() - Receive file from socket, fastest (zero-copy) way
  331:  *
  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: 
  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);
  348: 
  349: /* Buffered file access over memory block */
  350: 
  351: /*
  352:  * io_fmemopen() - File buffered stream operations over memory block
  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: /*
  360:  * io_fmapopen() - File buffered stream operations over MMAP block
  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: /*
  372:  * io_fd2buf() - Convert open file handle to buffered file I/O
  373:  *
  374:  * @fd = File handle
  375:  * @mode = Permissions for new buffered file I/O
  376:  * return: NULL error or open buffered file
  377:  */
  378: FILE *io_fd2buf(int fd, const char *mode);
  379: /*
  380:  * io_dumbFile() - Create empry or dumb file with fixed size
  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: 
  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
  403:  * @whdr = with complete headers
  404:  * @buflen = buffer length
  405:  * @zcbuf = zero copy buffer, if BPF supports it and isn't NULL
  406:  * return: -1 error or >-1 bpf handle
  407:  */
  408: int io_etherOpen(const char *csIface, int flags, int whdr, 
  409: 		unsigned int *buflen, void **zcbuf);
  410: /*
  411:  * io_etherClose() - Close BPF interface
  412:  *
  413:  * @eth = bpf handle
  414:  * @zcbuf = zero copy buffer, if BPF supports it and isn't NULL
  415:  * return: none
  416:  */
  417: void io_etherClose(int eth, void **zcbuf);
  418: 
  419: /*
  420:  * io_etherSend() - Send packet to bpf
  421:  *
  422:  * @eth = bpf handle
  423:  * @buf = buffer
  424:  * @buflen = buffer length
  425:  * return: -1 error or !=-1 written bytes
  426:  */
  427: ssize_t io_etherSend(int eth, const void *buf, size_t buflen);
  428: /*
  429:  * io_etherRecv() - Receive packet from bpf
  430:  *
  431:  * @eth = bpf handle
  432:  * @buf = buffer
  433:  * @buflen = buffer length
  434:  * @zcbuf = zero copy buffer, if BPF supports it and isn't NULL
  435:  * return: -1 error or !=-1 readed bytes
  436:  */
  437: ssize_t io_etherRecv(int eth, void * __restrict buf, 
  438: 		size_t buflen, void * __restrict zcbuf);
  439: 
  440: 
  441: #endif

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