File:  [ELWIX - Embedded LightWeight unIX -] / libaitio / inc / aitio.h
Revision 1.34.2.1: download - view: text, annotated - select for diffs - revision graph
Mon Aug 26 23:30:27 2013 UTC (10 years, 10 months ago) by misho
Branches: io5_7
Diff to: branchpoint 1.34: preferred, unified
add new api call

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

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