File:  [ELWIX - Embedded LightWeight unIX -] / libaitio / inc / aitio.h
Revision 1.33: download - view: text, annotated - select for diffs - revision graph
Tue Jul 9 00:35:35 2013 UTC (11 years ago) by misho
Branches: MAIN
CVS tags: io5_6, IO5_5, HEAD
version 5.5

    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.33 2013/07/09 00:35:35 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 <net/bpf.h>
   63: #include <netinet/in.h>
   64: 
   65: 
   66: #ifndef STRSIZ
   67: #define STRSIZ		256
   68: #endif
   69: 
   70: 
   71: // io_GetErrno() Get error code of last operation
   72: int io_GetErrno();
   73: // io_GetError() Get error text of last operation
   74: const char *io_GetError();
   75: 
   76: 
   77: /*
   78:  * ioPromptRead() - Read data from input h[0] with prompt to output h[1]
   79:  *
   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: /*
   88:  * ioPromptPassword() - Read password from input h[0] with prompt to output h[1]
   89:  *
   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: 
   99: 
  100: /*
  101:  * ioMkDir() - Function for racursive directory creation and validation
  102:  *
  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: 
  109: /*
  110:  * ioWatchDirLoop() - Function for watching changes in directory and fire callback
  111:  *
  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: 
  118: 
  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:  */
  129: int io_aiobulk(int mode, struct aiocb ** __restrict acbs, int nacb, 
  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);
  156: /*
  157:  * io_rread() - Raw VFS read function
  158:  *
  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:  */
  166: int io_rread(int fd, void * __restrict buf, size_t nbytes, off_t offset, 
  167: 		int update);
  168: /*
  169:  * io_rwrite() - Raw VFS write function
  170:  *
  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
  176:  * return: -1 error or !=-1 written bytes
  177:  */
  178: int io_rwrite(int fd, void * __restrict buf, size_t nbytes, off_t offset, 
  179: 		int update);
  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: 
  186: /* Crypto framework */
  187: 
  188: /*
  189:  * ioCipher() - Cipher wrapper for all supported crypto algorythms
  190:  *
  191:  * @pInput = input buffer
  192:  * @inLen = input buffer len
  193:  * @ppOutput = output allocated buffe, must be e_free after use
  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: /*
  204:  * io_Blowfish() - Blowfish cipher algorythm, work with ASCII hex strings
  205:  *
  206:  * @pInput = input buffer
  207:  * @inLen = input buffer len
  208:  * @ppOutput = output allocated buffe, must be e_free after use
  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: */
  214: int io_Blowfish(unsigned char *pInput, int inLen, unsigned char **ppOutput, 
  215: 		unsigned char *pKey, unsigned char *pIV, int nMode);
  216: /*
  217:  * io_ctr_AES() - Encrypt/Decrypt stream cipher CTR_AES
  218:  *
  219:  * @pInput = Input buffer with ASCII
  220:  * @inLen = Input buffer data length
  221:  * @ppOutput = Output buffer with cipher data, must be e_free after use
  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: /*
  231:  * ioAllocPTY() - Allocate new PTY and TTY
  232:  *
  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:  */
  241: int ioAllocPTY(int *ptyfd, int *ttyfd, char * __restrict name, int namesiz, 
  242: 		struct termios * __restrict term, struct winsize * __restrict winz);
  243: /*
  244:  * ioFreePTY() - Release PTY and TTY device
  245:  *
  246:  * @ptyfd = master fd, pty (==-1 skip closing pty)
  247:  * @ttyname = tty filename
  248:  * return: none
  249:  */
  250: void ioFreePTY(int ptyfd, const char *ttyname);
  251: /*
  252:  * ioChgWinPTY() - Change window size of PTY
  253:  *
  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:  */
  261: int ioChgWinPTY(int ptyfd, unsigned short row, unsigned short col, 
  262: 		unsigned short xpxl, unsigned short ypxl);
  263: /*
  264:  * ioSetOwnerTTY() - Set owner to TTY
  265:  *
  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: /*
  273:  * ioSetSidTTY() - Makes the process's controlling TTY and sets it to sane modes.
  274:  *
  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: /*
  281:  * ioSetRAWMode() - Enter into RAW mode
  282:  *
  283:  * @fd = tty fd
  284:  * @otio = saved old termios for later restore if !=NULL
  285:  * return: -1 error or 0 ok
  286:  */
  287: int ioSetRAWMode(int fd, struct termios *otio);
  288: /*
  289:  * ioRestoreMode() - Restore termios to tty fd
  290:  *
  291:  * @fd = tty fd
  292:  * @tio = termios structure for restore
  293:  * return: -1 error or 0 ok
  294:  */
  295: int ioRestoreMode(int fd, struct termios tio);
  296: /*
  297:  * ioForkPTY() - Fork new process with session leader and new TTY
  298:  *
  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: /*
  311:  * ioCreatePIDFile() - Create PID file
  312:  *
  313:  * @csName = PID filename
  314:  * @ifExists = !=0 if filename exists return error
  315:  * return: -1 error or 0 ok
  316:  */
  317: int ioCreatePIDFile(const char *csName, int ifExists);
  318: 
  319: /*
  320:  * ioSendFile() - AITNET sendfile() userland implementation, not dependant from OS
  321:  *
  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: /*
  331:  * ioRecvFile() - Receive file from socket, fastest (zero-copy) way
  332:  *
  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: 
  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);
  349: 
  350: /* Buffered file access over memory block */
  351: 
  352: /*
  353:  * io_fmemopen() - File buffered stream operations over memory block
  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: /*
  361:  * io_fmapopen() - File buffered stream operations over MMAP block
  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: /*
  373:  * io_fd2buf() - Convert open file handle to buffered file I/O
  374:  *
  375:  * @fd = File handle
  376:  * @mode = Permissions for new buffered file I/O
  377:  * return: NULL error or open buffered file
  378:  */
  379: FILE *io_fd2buf(int fd, const char *mode);
  380: /*
  381:  * io_dumbFile() - Create empry or dumb file with fixed size
  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: 
  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
  405:  * @wdlt = with data link type
  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:  */
  410: int io_etherOpen(const char *csIface, int flags, int whdr, int wdlt, 
  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: 
  443: #endif

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