File:  [ELWIX - Embedded LightWeight unIX -] / libaitio / inc / aitio.h
Revision 1.36: download - view: text, annotated - select for diffs - revision graph
Mon Oct 21 21:12:41 2013 UTC (10 years, 8 months ago) by misho
Branches: MAIN
CVS tags: io6_0, IO5_9, HEAD
version 5.9

    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.36 2013/10/21 21:12:41 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: #include <elwix.h>
   65: 
   66: 
   67: #ifndef STRSIZ
   68: #define STRSIZ			256
   69: #endif
   70: 
   71: #define IO_SOCK_ROLE_CLIENT	0
   72: #define IO_SOCK_ROLE_SERVER	1
   73: 
   74: #define IO_ETHER_FILTER_PROMISC	0
   75: #define IO_ETHER_FILTER_NOTREAD	-1
   76: #define IO_ETHER_FILTER_READ	1
   77: #define IO_ETHER_FILTER_WRITE	2
   78: 
   79: 
   80: typedef struct tagCliSock sock_cli_t;
   81: typedef void *(*sock_cb_t)(sock_cli_t*);
   82: struct tagCliSock {
   83: 	void				*cli_parent;
   84: 	pthread_t			cli_tid;
   85: 	int				cli_fd;
   86: 	int				cli_pty;
   87: 	sockaddr_t			cli_addr;
   88: 	char				cli_name[64];
   89: 
   90: 	sock_cb_t			cli_func;
   91: 	void				*cli_arg;
   92: 
   93: 	ait_val_t			cli_buf;
   94: 
   95: 	TAILQ_ENTRY(tagCliSock)		cli_node;
   96: };
   97: typedef struct {
   98: 	int				sock_role;
   99: 	int				sock_backq;
  100: 	int				sock_type;
  101: 	int				sock_proto;
  102: 	int 				sock_fd;
  103: 	sockaddr_t			sock_addr;
  104: 	sockaddr_t			sock_peer;
  105: 
  106: 	ait_val_t			sock_buf;
  107: 
  108: 	pthread_mutex_t			sock_mtx;
  109: 	TAILQ_HEAD(, tagCliSock)	sock_cli;
  110: } sock_t;
  111: 
  112: 
  113: // io_GetErrno() Get error code of last operation
  114: int io_GetErrno();
  115: // io_GetError() Get error text of last operation
  116: const char *io_GetError();
  117: 
  118: 
  119: /*
  120:  * ioInitSocket() - Init socket and allocate resources
  121:  *
  122:  * @role = Socket role
  123:  * @type = Socket type
  124:  * @proto = Socket protocol
  125:  * @addr = Bind to address
  126:  * @port = Bind to port
  127:  * @buflen = Socket buffer, optional if =0 == BUFSIZ
  128:  * return: NULL error or !=NULL created socket
  129:  */
  130: sock_t *ioInitSocket(int role, int type, int proto, 
  131: 		const char *addr, unsigned short port, size_t buflen);
  132: /*
  133:  * ioCloseSocket() - Close socket and free resources
  134:  *
  135:  * @s = Socket
  136:  * return: none
  137:  */
  138: void ioCloseSocket(sock_t ** __restrict s);
  139: /*
  140:  * ioUpSocket() - Setup socket for use
  141:  *
  142:  * @s = Socket
  143:  * @arg = Server role = listen backlog queue and Client role = peer address
  144:  * return: -1 error or 0 ok
  145:  */
  146: int ioUpSocket(sock_t * __restrict s, void *arg);
  147: /*
  148:  * ioAcceptSocket() - Accept clients
  149:  *
  150:  * @s = Socket
  151:  * @f = callback function for client handling
  152:  * @arg = optional argument for callback function
  153:  * return: -1 error or 0 ok
  154:  */
  155: int ioAcceptSocket(sock_t * __restrict s, sock_cb_t f, void *arg);
  156: 
  157: /*
  158:  * ioPromptRead() - Read data from input h[0] with prompt to output h[1]
  159:  *
  160:  * @h = file handles h[0] = input, h[1] = output, if NULL use stdin, stdout
  161:  * @csPrompt = Prompt before input, may be NULL
  162:  * @psData = Readed data
  163:  * @dataLen = Length of data
  164:  * return: 0 EOF; -1 error:: can`t read; >0 count of readed chars
  165: */
  166: int ioPromptRead(int *h, const char *csPrompt, char * __restrict psData, int dataLen);
  167: /*
  168:  * ioPromptPassword() - Read password from input h[0] with prompt to output h[1]
  169:  *
  170:  * @h = file handles h[0] = input, h[1] = output, if NULL use stdin, stdout
  171:  * @csPrompt = Prompt before input, may be NULL
  172:  * @psPass = Readed password
  173:  * @passLen = Length of password
  174:  * @confirm = Confirm password, 0 - get password, !=0 Ask for confirmation
  175:  * return: 0 EOF; -1 error:: can`t read; >0 count of readed chars
  176: */
  177: int ioPromptPassword(int *h, const char *csPrompt, char * __restrict psPass, int passLen, int confirm);
  178: 
  179: 
  180: /*
  181:  * ioMkDir() - Function for racursive directory creation and validation
  182:  *
  183:  * @csDir = Full directory path
  184:  * @mode = Mode for directory creation if missing dir
  185:  * return: -1 error, 0 directory path exist, >0 created missing dirs
  186: */
  187: int ioMkDir(const char *csDir, int mode);
  188: 
  189: /*
  190:  * ioWatchDirLoop() - Function for watching changes in directory and fire callback
  191:  *
  192:  * @csDir = Full directory path
  193:  * @callback = Callback if raise event! nOp -1 delete, 0 change/move, 1 create
  194:  * return: -1 error, !=-1 ok, number of total signaled events
  195: */
  196: int ioWatchDirLoop(const char *csDir, int (*callback)(const char *csName, int nOp));
  197: 
  198: 
  199: #ifdef AIO_OPS
  200: /*
  201:  * io_aiobulk() - AIO bulk R/W function
  202:  *
  203:  * @mode = Bulk wait mode
  204:  * @acbs = List of aiocb structures
  205:  * @nacb = Number of aiocb in list
  206:  * @sig = Event for completed operations, may be =NULL
  207:  * return: -1 error or 0 ok
  208:  */
  209: int io_aiobulk(int mode, struct aiocb ** __restrict acbs, int nacb, 
  210: 		struct sigevent *sig);
  211: #endif
  212: /*
  213:  * io_rreadv() - Raw VFS bulk read function
  214:  *
  215:  * @fd = File handle
  216:  * @bufs = Read buffers
  217:  * @nbufs = Number of read buffers
  218:  * @offset = Read from position, if -1 read nbytes from current position
  219:  * @update = Update file handle position !0
  220:  * return: -1 error or !=-1 readed bytes
  221:  */
  222: int io_rreadv(int fd, struct iovec * __restrict bufs, int nbufs, off_t offset, 
  223: 		int update);
  224: /*
  225:  * io_rwritev() - Raw VFS bulk write function
  226:  *
  227:  * @fd = File handle
  228:  * @bufs = Write buffers
  229:  * @nbufs = Number of write buffers
  230:  * @offset = Write to position, if -1 write nbytes to current position
  231:  * @update = Update file handle position !0
  232:  * return: -1 error or !=-1 written bytes
  233:  */
  234: int io_rwritev(int fd, struct iovec * __restrict bufs, int nbufs, off_t offset, 
  235: 		int update);
  236: /*
  237:  * io_rread() - Raw VFS read function
  238:  *
  239:  * @fd = File handle
  240:  * @buf = Read buffer
  241:  * @nbytes = Read buffer size
  242:  * @offset = Read from position, if -1 read nbytes from current position
  243:  * @update = Update file handle position !0
  244:  * return: -1 error or !=-1 readed bytes
  245:  */
  246: int io_rread(int fd, void * __restrict buf, size_t nbytes, off_t offset, 
  247: 		int update);
  248: /*
  249:  * io_rwrite() - Raw VFS write function
  250:  *
  251:  * @fd = File handle
  252:  * @buf = Write buffer
  253:  * @nbytes = Write bytes from buffer
  254:  * @offset = Write at position, if -1 write nbytes from current position
  255:  * @update = Update file handle position !0
  256:  * return: -1 error or !=-1 written bytes
  257:  */
  258: int io_rwrite(int fd, void * __restrict buf, size_t nbytes, off_t offset, 
  259: 		int update);
  260: 
  261: /* Disk I/O helper macros */
  262: #define io_read(f, b, n) io_rread(f, b, n, -1, 1)
  263: #define io_write(f, b, n) io_rwrite(f, b, n, -1, 1)
  264: 
  265: 
  266: /* Crypto framework */
  267: 
  268: /*
  269:  * ioCipher() - Cipher wrapper for all supported crypto algorythms
  270:  *
  271:  * @pInput = input buffer
  272:  * @inLen = input buffer len
  273:  * @ppOutput = output allocated buffe, must be e_free after use
  274:  * @Cipher = cipher engine, like EVP_bf_cbc() or etc...
  275:  * @pKey = key
  276:  * @pIV = IV, salt (8 bytes)
  277:  * @nMode = Mode 0 - decrypting or 1 - encrypting
  278:  * return: 0 not present data or error!; >0 number of processed and returned bytes into ppOutput
  279: */
  280: int ioCipher(unsigned char *pInput, int inLen, unsigned char **ppOutput, const EVP_CIPHER *Cipher, 
  281: 		unsigned char *pKey, unsigned char *pIV, int nMode);
  282: 
  283: /*
  284:  * io_Blowfish() - Blowfish cipher algorythm, work with ASCII hex strings
  285:  *
  286:  * @pInput = input buffer
  287:  * @inLen = input buffer len
  288:  * @ppOutput = output allocated buffe, must be e_free after use
  289:  * @pKey = key
  290:  * @pIV = IV, salt (8 bytes)
  291:  * @nMode = Mode 0 - decrypting or 1 - encrypting
  292:  * return: 0 not present data or error!; >0 number of processed and returned bytes into ppOutput
  293: */
  294: int io_Blowfish(unsigned char *pInput, int inLen, unsigned char **ppOutput, 
  295: 		unsigned char *pKey, unsigned char *pIV, int nMode);
  296: /*
  297:  * io_ctr_AES() - Encrypt/Decrypt stream cipher CTR_AES
  298:  *
  299:  * @pInput = Input buffer with ASCII
  300:  * @inLen = Input buffer data length
  301:  * @ppOutput = Output buffer with cipher data, must be e_free after use
  302:  * @pKey = Key
  303:  * @IV = IVector/Nonce/Counter, Warning: IV must be variable, because we write there!!!
  304:  * return: -1 error or >-1 how many cipher blocks proceeded
  305:  */
  306: int io_ctr_AES(unsigned char *pInput, int inLen, unsigned char **ppOutput, 
  307: 		unsigned char *pKey, unsigned char IV[AES_BLOCK_SIZE]);
  308: 
  309: 
  310: /*
  311:  * ioAllocPTY() - Allocate new PTY and TTY
  312:  *
  313:  * @ptyfd = master fd, pty
  314:  * @ttyfd = slave fd, tty
  315:  * @name = tty device name if not null
  316:  * @namesiz = name length, must be above 63 bytes.
  317:  * @term = termios for terminal
  318:  * @winz = winsize for terminal
  319:  * return: -1 error or 0 ok
  320:  */
  321: int ioAllocPTY(int *ptyfd, int *ttyfd, char * __restrict name, int namesiz, 
  322: 		struct termios * __restrict term, struct winsize * __restrict winz);
  323: /*
  324:  * ioFreePTY() - Release PTY and TTY device
  325:  *
  326:  * @ptyfd = master fd, pty (==-1 skip closing pty)
  327:  * @ttyname = tty filename
  328:  * return: none
  329:  */
  330: void ioFreePTY(int ptyfd, const char *ttyname);
  331: /*
  332:  * ioChgWinPTY() - Change window size of PTY
  333:  *
  334:  * @ptyfd = master fd, pty
  335:  * @row = row
  336:  * @col = col
  337:  * @xpxl = x pixels
  338:  * @ypxl = y pixels
  339:  * return: -1 error or 0 ok
  340:  */
  341: int ioChgWinPTY(int ptyfd, unsigned short row, unsigned short col, 
  342: 		unsigned short xpxl, unsigned short ypxl);
  343: /*
  344:  * ioSetOwnerTTY() - Set owner to TTY
  345:  *
  346:  * @ttyname = tty filename
  347:  * @UID = uid
  348:  * @GID = gid
  349:  * return: -1 error or 0 ok
  350:  */
  351: int ioSetOwnerTTY(const char *ttyname, uid_t UID, gid_t GID);
  352: /*
  353:  * ioSetSidTTY() - Makes the process's controlling TTY and sets it to sane modes.
  354:  *
  355:  * @ttyfd = slave fd, tty
  356:  * @ttyname = tty filename
  357:  * return: -1 error or 0 ok
  358:  */
  359: int ioSetSidTTY(int *ttyfd, const char *ttyname);
  360: /*
  361:  * ioSetRAWMode() - Enter into RAW mode
  362:  *
  363:  * @fd = tty fd
  364:  * @otio = saved old termios for later restore if !=NULL
  365:  * return: -1 error or 0 ok
  366:  */
  367: int ioSetRAWMode(int fd, struct termios *otio);
  368: /*
  369:  * ioRestoreMode() - Restore termios to tty fd
  370:  *
  371:  * @fd = tty fd
  372:  * @tio = termios structure for restore
  373:  * return: -1 error or 0 ok
  374:  */
  375: int ioRestoreMode(int fd, struct termios tio);
  376: /*
  377:  * ioForkPTY() - Fork new process with session leader and new TTY
  378:  *
  379:  * @ptyfd = master fd, pty
  380:  * @name = tty device name if not null
  381:  * @namesiz = name length, must be above 63 bytes.
  382:  * @term = termios for terminal
  383:  * @winz = winsize for terminal
  384:  * @otio = old termios structure for restore
  385:  * return: -1 error, 0 child process or >0 parent: pid of child
  386:  */
  387: pid_t ioForkPTY(int *ptyfd, char * __restrict name, int namesiz, struct termios * __restrict term, 
  388: 		struct winsize * __restrict winz, struct termios * __restrict otio);
  389: 
  390: /*
  391:  * ioCreatePIDFile() - Create PID file
  392:  *
  393:  * @csName = PID filename
  394:  * @ifExists = !=0 if filename exists return error
  395:  * return: -1 error or 0 ok
  396:  */
  397: int ioCreatePIDFile(const char *csName, int ifExists);
  398: 
  399: /*
  400:  * ioSendFile() - AITNET sendfile() userland implementation, not dependant from OS
  401:  *
  402:  * @s = socket
  403:  * @csFile = file for send
  404:  * @sendLen = bytes to send, if 0 send all data
  405:  * @offset = start file offset
  406:  * @sndbuf = SO_SNDBUF value, if 0 use default
  407:  * return: 0 error, >0 ok, sended bytes
  408:  */
  409: size_t ioSendFile(int s, const char *csFile, size_t sendLen, off_t offset, int sndbuf);
  410: /*
  411:  * ioRecvFile() - Receive file from socket, fastest (zero-copy) way
  412:  *
  413:  * @s = socket
  414:  * @csFile = file for receive
  415:  * @recvLen = receive bytes
  416:  * @over = overwrite file if exists with mode like 0644
  417:  * @rcvbuf = SO_RCVBUF value, if 0 use default
  418:  * return: 0 error, >0 ok, received bytes
  419:  */
  420: size_t ioRecvFile(int s, const char *csFile, size_t recvLen, int over, int rcvbuf);
  421: 
  422: /*
  423:  * ioRealFileName() - Get real file name
  424:  *
  425:  * @fname = filename
  426:  * return: =NULL error or !=NULL real filename, should be free with e_free()
  427:  */
  428: char *ioRealFileName(const char *fname);
  429: 
  430: /* Buffered file access over memory block */
  431: 
  432: /*
  433:  * io_fmemopen() - File buffered stream operations over memory block
  434:  *
  435:  * @base = Base address of memory block, if =NULL Infinit length(auto-grow)
  436:  * @basesize = Size of memory block
  437:  * return: NULL error or !=NULL Opened file resource
  438:  */
  439: FILE *io_fmemopen(void ** __restrict base, off_t basesize);
  440: /*
  441:  * io_fmapopen() - File buffered stream operations over MMAP block
  442:  *
  443:  * @csFile = Filename for MMAP, if =NULL private MMAP block
  444:  * @mode = File open mode
  445:  * @perm = If file not exists will be created with this access permissions
  446:  * @prot = MMAP protection
  447:  * @flags = MMAP mode flags
  448:  * @offset = Map from file offset, if csFile==NULL then this is size of MMAP private block
  449:  * return: NULL error or !=NULL Opened file resource
  450:  */
  451: FILE *io_fmapopen(const char *csFile, int mode, int perm, int prot, int flags, off_t offset);
  452: /*
  453:  * io_fd2buf() - Convert open file handle to buffered file I/O
  454:  *
  455:  * @fd = File handle
  456:  * @mode = Permissions for new buffered file I/O
  457:  * return: NULL error or open buffered file
  458:  */
  459: FILE *io_fd2buf(int fd, const char *mode);
  460: /*
  461:  * io_dumbFile() - Create empry or dumb file with fixed size
  462:  *
  463:  * @csFile = Filename for create
  464:  * @mode = File access permissions
  465:  * @size = File size
  466:  * return: -1 error or open file handle
  467:  */
  468: int io_dumbFile(const char *csFile, int mode, off_t size);
  469: 
  470: 
  471: /*
  472:  * io_get1stiface() - Get first interface of host
  473:  *
  474:  * @szIface = interface string buffer
  475:  * @iflen = size of interface buffer
  476:  * return: -1 error or 0 ok
  477:  */
  478: int io_get1stiface(char *szIface, int iflen);
  479: /*
  480:  * io_etherOpen() - Open BPF interface to device
  481:  *
  482:  * @csIface = interface name
  483:  * @flags = open flags
  484:  * @whdr = with complete headers
  485:  * @wdlt = with data link type
  486:  * @buflen = buffer length
  487:  * @zcbuf = zero copy buffer, if BPF supports it and isn't NULL
  488:  * return: -1 error or >-1 bpf handle
  489:  */
  490: int io_etherOpen(const char *csIface, int flags, int whdr, int wdlt, 
  491: 		unsigned int *buflen, void **zcbuf);
  492: /*
  493:  * io_etherClose() - Close BPF interface
  494:  *
  495:  * @eth = bpf handle
  496:  * @zcbuf = zero copy buffer, if BPF supports it and isn't NULL
  497:  * return: none
  498:  */
  499: void io_etherClose(int eth, void **zcbuf);
  500: 
  501: /*
  502:  * io_etherFilter() - BPF filter routine
  503:  *
  504:  * @eth = bpf handle
  505:  * @io = filter direction 
  506:  *  (IO_ETHER_FILTER_PROMISC|IO_ETHER_FILTER_NOTREAD|IO_ETHER_FILTER_READ|IO_ETHER_FILTER_WRITE)
  507:  * @insn = BPF filter instruction array
  508:  * @insnlen = Length of BPF filter instruction array
  509:  * return: -1 error or 0 ok
  510:  */
  511: int io_etherFilter(int eth, int io, struct bpf_insn * __restrict insn, size_t insnlen);
  512: 
  513: /*
  514:  * io_etherSend() - Send packet to bpf
  515:  *
  516:  * @eth = bpf handle
  517:  * @buf = buffer
  518:  * @buflen = buffer length
  519:  * return: -1 error or !=-1 written bytes
  520:  */
  521: ssize_t io_etherSend(int eth, const void *buf, size_t buflen);
  522: /*
  523:  * io_etherRecv() - Receive packet from bpf
  524:  *
  525:  * @eth = bpf handle
  526:  * @buf = buffer
  527:  * @buflen = buffer length
  528:  * @zcbuf = zero copy buffer, if BPF supports it and isn't NULL
  529:  * return: -1 error or !=-1 readed bytes
  530:  */
  531: ssize_t io_etherRecv(int eth, void * __restrict buf, 
  532: 		size_t buflen, void * __restrict zcbuf);
  533: 
  534: 
  535: #endif

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