File:  [ELWIX - Embedded LightWeight unIX -] / libaitio / inc / aitio.h
Revision 1.42: download - view: text, annotated - select for diffs - revision graph
Thu Aug 18 09:06:31 2016 UTC (7 years, 10 months ago) by misho
Branches: MAIN
CVS tags: io7_4, IO7_3, HEAD
version 7.3

    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.42 2016/08/18 09:06:31 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 - 2016
   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/socket.h>
   57: #include <sys/un.h>
   58: #include <sys/uio.h>
   59: #ifndef __linux__
   60: #include <sys/tty.h>
   61: #include <sys/ioctl_compat.h>
   62: #include <net/if_dl.h>
   63: #include <net/bpf.h>
   64: #endif
   65: #include <netinet/in.h>
   66: #include <elwix.h>
   67: #include <aitsched.h>
   68: 
   69: 
   70: #ifndef STRSIZ
   71: #define STRSIZ			256
   72: #endif
   73: 
   74: #define IO_SOCK_ROLE_CLIENT	0
   75: #define IO_SOCK_ROLE_SERVER	1
   76: 
   77: #define IO_ETHER_FILTER_PROMISC	0
   78: #define IO_ETHER_FILTER_NOTREAD	-1
   79: #define IO_ETHER_FILTER_READ	1
   80: #define IO_ETHER_FILTER_WRITE	2
   81: 
   82: 
   83: typedef struct {
   84: 	unsigned int			prog_inin;	/* init progs */
   85: 	unsigned int			prog_maxn;	/* max progs */
   86: 	unsigned int			prog_cnum;	/* current progs */
   87: 	char				prog_name[PATH_MAX];
   88: 
   89: 	pthread_mutex_t			prog_mtx;
   90: 	array_t				*prog_fds;
   91: 	unsigned int			*prog_used;
   92: } prog_t;
   93: 
   94: typedef struct tagCliSock sock_cli_t;
   95: typedef void *(*sock_cb_t)(sock_cli_t*);
   96: struct tagCliSock {
   97: 	void				*cli_parent;
   98: 	int				cli_fd;
   99: 	intptr_t			cli_pty;
  100: 	sockaddr_t			cli_addr;
  101: 	char				cli_name[64];
  102: 	char				cli_cmdline[PATH_MAX];
  103: 	pid_t				cli_pid;
  104: 
  105: 	sched_task_func_t		cli_func;
  106: 
  107: 	ait_val_t			cli_buf[2];
  108: 
  109: 	TAILQ_ENTRY(tagCliSock)		cli_node;
  110: };
  111: 
  112: typedef struct {
  113: 	int				sock_role;
  114: 	int				sock_backq;
  115: 	int				sock_type;
  116: 	int				sock_proto;
  117: 	int 				sock_fd;
  118: 	struct timespec			sock_timeout;
  119: 	sockaddr_t			sock_addr;
  120: 	sockaddr_t			sock_peer;
  121: 
  122: 	ait_val_t			sock_buf;
  123: 	prog_t				*sock_prog;
  124: 
  125: 	volatile intptr_t		sock_kill;
  126: 	sched_root_task_t		*sock_root;
  127: 
  128: 	pthread_mutex_t			sock_mtx;
  129: 	TAILQ_HEAD(, tagCliSock)	sock_cli;
  130: } sock_t;
  131: 
  132: 
  133: // io_GetErrno() Get error code of last operation
  134: int io_GetErrno();
  135: // io_GetError() Get error text of last operation
  136: const char *io_GetError();
  137: 
  138: 
  139: /*
  140:  * io_progInit() - Init program pool
  141:  *
  142:  * @progName = program name for execution
  143:  * @initNum = initial started programs
  144:  * @maxNum = maximum started programs
  145:  * return: NULL error or !=NULL allocated pool (must destroied with io_progDestroy())
  146:  */
  147: prog_t *io_progInit(const char *progName, 
  148: 		unsigned int initNum, unsigned int maxNum);
  149: /*
  150:  * io_progOpen() - Execute number of program(s)
  151:  *
  152:  * @prg = program pool
  153:  * @execNum = execute program(s) (0 max)
  154:  * return: -1 error, >0 executed programs
  155:  */
  156: int io_progOpen(prog_t * __restrict prg, unsigned int execNum);
  157: /*
  158:  * io_progOpen2() - Start program from pool on first unused slot
  159:  *
  160:  * @prg = program pool
  161:  * return: -1 error, >-1 reside at slot
  162:  */
  163: int io_progOpen2(prog_t * __restrict prg);
  164: /*
  165:  * io_progGrow() - Execute to number of programs in pool
  166:  *
  167:  * @prg = program pool
  168:  * @toNum = execute to number of programs (0 max)
  169:  * return: 0 error or nothing to do, 
  170:  * 	>0 executed programs and abs(<0) executed programs with logged error
  171:  */
  172: int io_progGrow(prog_t * __restrict prg, unsigned int toNum);
  173: /*
  174:  * io_progVacuum() - Vacuum pool to running number of programs
  175:  *
  176:  * @prg = program pool
  177:  * @toNum = vacuum to number of programs (0 to init number)
  178:  * return: 0 error or >0 closed programs
  179:  */
  180: int io_progVacuum(prog_t * __restrict prg, unsigned int toNum);
  181: /*
  182:  * io_progCloseAt() - Close program at pool of certain position
  183:  *
  184:  * @prg = program pool
  185:  * @idx = index at pool
  186:  * return: 0 error or !=0 closed program
  187:  */
  188: int io_progCloseAt(prog_t * __restrict prg, unsigned int idx);
  189: /*
  190:  * io_progCloseOf() - Close program at pool with certain handle
  191:  *
  192:  * @prg = program pool
  193:  * @h = handle of program
  194:  * return: 0 error, >0 closed programs
  195:  */
  196: #ifdef POPEN_STREAM
  197: int io_progCloseOf(prog_t * __restrict prg, FILE *h);
  198: #else
  199: int io_progCloseOf(prog_t * __restrict prg, int h);
  200: #endif
  201: /*
  202:  * io_progClose() - Close all programs in pool
  203:  *
  204:  * @prg = program pool
  205:  * @closeNum = close program(s) (0 all)
  206:  * return: 0 error, >0 closed programs
  207:  */
  208: int io_progClose(prog_t * __restrict prg, unsigned int closeNum);
  209: /*
  210:  * io_progDestroy() - Destroy entire program pool
  211:  *
  212:  * @pprg = program pool
  213:  * return: none
  214:  */
  215: void io_progDestroy(prog_t ** __restrict pprg);
  216: 
  217: /*
  218:  * io_progCheck() - Check exit status of program pool
  219:  *
  220:  * @prg = program pool
  221:  * @re = resurrect program to init number
  222:  * return: -1 error or >-1 exited programs
  223:  */
  224: int io_progCheck(prog_t * __restrict prg, int re);
  225: 
  226: /*
  227:  * io_progAttach() - Attach to open program
  228:  *
  229:  * @prg = program pool
  230:  * @newOne = Execute new one program after attach
  231:  * return: NULL error or !=NULL attached program handle
  232:  */
  233: #ifdef POPEN_STREAM
  234: FILE *io_progAttach(prog_t * __restrict prg, int newOne);
  235: #else
  236: int io_progAttach(prog_t * __restrict prg, int newOne);
  237: #endif
  238: /*
  239:  * io_progDetach() - Detch from open program
  240:  *
  241:  * @prg= program pool
  242:  * @pfd = attached program handle
  243:  * return: none
  244:  */
  245: #ifdef POPEN_STREAM
  246: void io_progDetach(prog_t * __restrict prg, FILE *pfd);
  247: #else
  248: void io_progDetach(prog_t * __restrict prg, int pfd);
  249: #endif
  250: 
  251: 
  252: /*
  253:  * ioInitSocket() - Init socket and allocate resources
  254:  *
  255:  * @role = Socket role
  256:  * @type = Socket type
  257:  * @proto = Socket protocol
  258:  * @addr = Bind to address
  259:  * @port = Bind to port
  260:  * @buflen = Socket buffer, optional if =0 == BUFSIZ
  261:  * return: NULL error or !=NULL created socket
  262:  */
  263: sock_t *ioInitSocket(int role, int type, int proto, 
  264: 		const char *addr, unsigned short port, size_t buflen);
  265: /*
  266:  * ioCloseSocket() - Close socket and free resources
  267:  *
  268:  * @s = Socket
  269:  * return: none
  270:  */
  271: void ioCloseSocket(sock_t ** __restrict s);
  272: #define ioKillSocket(x)		(assert((x)), (x)->sock_kill = 1)
  273: /*
  274:  * ioCloseClient() - Close client socket
  275:  *
  276:  * @c = Client socket
  277:  * return: 0 ok or !=0 error
  278:  */
  279: int ioCloseClient(sock_cli_t * __restrict c);
  280: /*
  281:  * ioSetupProg2Socket() - Setup program pool to socket server
  282:  *
  283:  * @s = Socket
  284:  * @p = Program pool
  285:  * return: -1 error or 0 ok
  286:  */
  287: int ioSetupProg2Socket(sock_t * __restrict s, prog_t * __restrict p);
  288: /*
  289:  * ioUpSocket() - Setup socket for use
  290:  *
  291:  * @s = Socket
  292:  * @arg = Server role = listen backlog queue and Client role = peer address
  293:  * @timeout = Socket timeout in ms (default -1 infinit)
  294:  * return: -1 error or 0 ok
  295:  */
  296: int ioUpSocket(sock_t * __restrict s, void *arg, int timeout);
  297: /*
  298:  * ioUpdTimerSocket() - Update timeout of socket
  299:  *
  300:  * @c = Client socket
  301:  * return:  none
  302:  */
  303: void ioUpdTimerSocket(sock_cli_t * __restrict c);
  304: /*
  305:  * ioLoopSocket() - Start socket scheduler
  306:  *
  307:  * @s = Socket
  308:  * @rcb = Read callback
  309:  * return: -1 error or return result from scheduler
  310:  */
  311: int ioLoopSocket(sock_t * __restrict s, sched_task_func_t rcb);
  312: /*
  313:  * ioBridgeProg2Socket() - Start socket scheduler and bridge program to socket
  314:  *
  315:  * @s = Socket
  316:  * @prgname = Program name
  317:  * return: 0 ok or !=0 error
  318:  */
  319: int ioBridgeProg2Socket(sock_t * __restrict s, const char *prgname);
  320: 
  321: 
  322: /*
  323:  * ioPromptRead() - Read data from input h[0] with prompt to output h[1]
  324:  *
  325:  * @h = file handles h[0] = input, h[1] = output, if NULL use stdin, stdout
  326:  * @csPrompt = Prompt before input, may be NULL
  327:  * @psData = Readed data
  328:  * @dataLen = Length of data
  329:  * return: 0 EOF; -1 error:: can`t read; >0 count of readed chars
  330: */
  331: int ioPromptRead(int *h, const char *csPrompt, 
  332: 		char * __restrict psData, int dataLen);
  333: /*
  334:  * ioPromptPassword() - Read password from input h[0] with prompt to output h[1]
  335:  *
  336:  * @h = file handles h[0] = input, h[1] = output, if NULL use stdin, stdout
  337:  * @csPrompt = Prompt before input, may be NULL
  338:  * @psPass = Readed password
  339:  * @passLen = Length of password
  340:  * @confirm = Confirm password, 0 - get password, !=0 Ask for confirmation
  341:  * return: 0 EOF; -1 error:: can`t read; >0 count of readed chars
  342: */
  343: int ioPromptPassword(int *h, const char *csPrompt, 
  344: 		char * __restrict psPass, int passLen, int confirm);
  345: 
  346: 
  347: /*
  348:  * ioMkDir() - Function for racursive directory creation and validation
  349:  *
  350:  * @csDir = Full directory path
  351:  * @mode = Mode for directory creation if missing dir
  352:  * return: -1 error, 0 directory path exist, >0 created missing dirs
  353: */
  354: int ioMkDir(const char *csDir, int mode);
  355: 
  356: /*
  357:  * ioWatchDirLoop() - Function for watching changes in directory and fire callback
  358:  *
  359:  * @csDir = Full directory path
  360:  * @callback = Callback if raise event! nOp -1 delete, 0 change/move, 1 create
  361:  * return: -1 error, !=-1 ok, number of total signaled events
  362: */
  363: int ioWatchDirLoop(const char *csDir, int (*callback)(const char *csName, int nOp));
  364: 
  365: 
  366: #ifdef AIO_OPS
  367: /*
  368:  * io_aiobulk() - AIO bulk R/W function
  369:  *
  370:  * @mode = Bulk wait mode
  371:  * @acbs = List of aiocb structures
  372:  * @nacb = Number of aiocb in list
  373:  * @sig = Event for completed operations, may be =NULL
  374:  * return: -1 error or 0 ok
  375:  */
  376: int io_aiobulk(int mode, struct aiocb ** __restrict acbs, int nacb, 
  377: 		struct sigevent *sig);
  378: #endif
  379: /*
  380:  * io_rreadv() - Raw VFS bulk read function
  381:  *
  382:  * @fd = File handle
  383:  * @bufs = Read buffers
  384:  * @nbufs = Number of read buffers
  385:  * @offset = Read from position, if -1 read nbytes from current position
  386:  * @update = Update file handle position !0
  387:  * return: -1 error or !=-1 readed bytes
  388:  */
  389: int io_rreadv(int fd, struct iovec * __restrict bufs, int nbufs, off_t offset, 
  390: 		int update);
  391: /*
  392:  * io_rwritev() - Raw VFS bulk write function
  393:  *
  394:  * @fd = File handle
  395:  * @bufs = Write buffers
  396:  * @nbufs = Number of write buffers
  397:  * @offset = Write to position, if -1 write nbytes to current position
  398:  * @update = Update file handle position !0
  399:  * return: -1 error or !=-1 written bytes
  400:  */
  401: int io_rwritev(int fd, struct iovec * __restrict bufs, int nbufs, off_t offset, 
  402: 		int update);
  403: /*
  404:  * io_rread() - Raw VFS read function
  405:  *
  406:  * @fd = File handle
  407:  * @buf = Read buffer
  408:  * @nbytes = Read buffer size
  409:  * @offset = Read from position, if -1 read nbytes from current position
  410:  * @update = Update file handle position !0
  411:  * return: -1 error or !=-1 readed bytes
  412:  */
  413: int io_rread(int fd, void * __restrict buf, size_t nbytes, off_t offset, 
  414: 		int update);
  415: /*
  416:  * io_rwrite() - Raw VFS write function
  417:  *
  418:  * @fd = File handle
  419:  * @buf = Write buffer
  420:  * @nbytes = Write bytes from buffer
  421:  * @offset = Write at position, if -1 write nbytes from current position
  422:  * @update = Update file handle position !0
  423:  * return: -1 error or !=-1 written bytes
  424:  */
  425: int io_rwrite(int fd, void * __restrict buf, size_t nbytes, off_t offset, 
  426: 		int update);
  427: 
  428: /* Disk I/O helper macros */
  429: #define io_read(f, b, n) io_rread(f, b, n, -1, 1)
  430: #define io_write(f, b, n) io_rwrite(f, b, n, -1, 1)
  431: 
  432: 
  433: /* Crypto framework */
  434: 
  435: /*
  436:  * ioCipher() - Cipher wrapper for all supported crypto algorythms
  437:  *
  438:  * @pInput = input buffer
  439:  * @inLen = input buffer len
  440:  * @ppOutput = output allocated buffe, must be e_free after use
  441:  * @Cipher = cipher engine, like EVP_bf_cbc() or etc...
  442:  * @pKey = key
  443:  * @pIV = IV, salt (8 bytes)
  444:  * @nMode = Mode 0 - decrypting or 1 - encrypting
  445:  * return: 0 not present data or error!; >0 number of processed and returned bytes into ppOutput
  446: */
  447: int ioCipher(unsigned char *pInput, int inLen, unsigned char **ppOutput, const EVP_CIPHER *Cipher, 
  448: 		unsigned char *pKey, unsigned char *pIV, int nMode);
  449: 
  450: /*
  451:  * io_Blowfish() - Blowfish cipher algorythm, work with ASCII hex strings
  452:  *
  453:  * @pInput = input buffer
  454:  * @inLen = input buffer len
  455:  * @ppOutput = output allocated buffe, must be e_free after use
  456:  * @pKey = key
  457:  * @pIV = IV, salt (8 bytes)
  458:  * @nMode = Mode 0 - decrypting or 1 - encrypting
  459:  * return: 0 not present data or error!; >0 number of processed and returned bytes into ppOutput
  460: */
  461: int io_Blowfish(unsigned char *pInput, int inLen, unsigned char **ppOutput, 
  462: 		unsigned char *pKey, unsigned char *pIV, int nMode);
  463: /*
  464:  * io_ctr_AES() - Encrypt/Decrypt stream cipher CTR_AES
  465:  *
  466:  * @pInput = Input buffer with ASCII
  467:  * @inLen = Input buffer data length
  468:  * @ppOutput = Output buffer with cipher data, must be e_free after use
  469:  * @pKey = Key
  470:  * @IV = IVector/Nonce/Counter, Warning: IV must be variable, because we write there!!!
  471:  * return: -1 error or >-1 how many cipher blocks proceeded
  472:  */
  473: int io_ctr_AES(unsigned char *pInput, int inLen, unsigned char **ppOutput, 
  474: 		unsigned char *pKey, unsigned char IV[AES_BLOCK_SIZE]);
  475: 
  476: 
  477: /*
  478:  * ioAllocPTY() - Allocate new PTY and TTY
  479:  *
  480:  * @ptyfd = master fd, pty
  481:  * @ttyfd = slave fd, tty
  482:  * @name = tty device name if not null
  483:  * @namesiz = name length, must be above 63 bytes.
  484:  * @term = termios for terminal
  485:  * @winz = winsize for terminal
  486:  * return: -1 error or 0 ok
  487:  */
  488: int ioAllocPTY(int *ptyfd, int *ttyfd, char * __restrict name, int namesiz, 
  489: 		struct termios * __restrict term, struct winsize * __restrict winz);
  490: /*
  491:  * ioFreePTY() - Release PTY and TTY device
  492:  *
  493:  * @ptyfd = master fd, pty (==-1 skip closing pty)
  494:  * @ttyname = tty filename
  495:  * return: none
  496:  */
  497: void ioFreePTY(int ptyfd, const char *ttyname);
  498: /*
  499:  * ioChgWinPTY() - Change window size of PTY
  500:  *
  501:  * @ptyfd = master fd, pty
  502:  * @row = row
  503:  * @col = col
  504:  * @xpxl = x pixels
  505:  * @ypxl = y pixels
  506:  * return: -1 error or 0 ok
  507:  */
  508: int ioChgWinPTY(int ptyfd, unsigned short row, unsigned short col, 
  509: 		unsigned short xpxl, unsigned short ypxl);
  510: /*
  511:  * ioSetOwnerTTY() - Set owner to TTY
  512:  *
  513:  * @ttyname = tty filename
  514:  * @UID = uid
  515:  * @GID = gid
  516:  * return: -1 error or 0 ok
  517:  */
  518: int ioSetOwnerTTY(const char *ttyname, uid_t UID, gid_t GID);
  519: /*
  520:  * ioSetSidTTY() - Makes the process's controlling TTY and sets it to sane modes.
  521:  *
  522:  * @ttyfd = slave fd, tty
  523:  * @ttyname = tty filename
  524:  * return: -1 error or 0 ok
  525:  */
  526: int ioSetSidTTY(int *ttyfd, const char *ttyname);
  527: /*
  528:  * ioSetRAWMode() - Enter into RAW mode
  529:  *
  530:  * @fd = tty fd
  531:  * @otio = saved old termios for later restore if !=NULL
  532:  * return: -1 error or 0 ok
  533:  */
  534: int ioSetRAWMode(int fd, struct termios *otio);
  535: /*
  536:  * ioRestoreMode() - Restore termios to tty fd
  537:  *
  538:  * @fd = tty fd
  539:  * @tio = termios structure for restore
  540:  * return: -1 error or 0 ok
  541:  */
  542: int ioRestoreMode(int fd, struct termios tio);
  543: /*
  544:  * ioForkPTY() - Fork new process with session leader and new TTY
  545:  *
  546:  * @ptyfd = master fd, pty
  547:  * @name = tty device name if not null
  548:  * @namesiz = name length, must be above 63 bytes.
  549:  * @term = termios for terminal
  550:  * @winz = winsize for terminal
  551:  * @otio = old termios structure for restore
  552:  * return: -1 error, 0 child process or >0 parent: pid of child
  553:  */
  554: pid_t ioForkPTY(int *ptyfd, char * __restrict name, int namesiz, struct termios * __restrict term, 
  555: 		struct winsize * __restrict winz, struct termios * __restrict otio);
  556: 
  557: /*
  558:  * ioCreatePIDFile() - Create PID file
  559:  *
  560:  * @csName = PID filename
  561:  * @ifExists = !=0 if filename exists return error
  562:  * return: -1 error or 0 ok
  563:  */
  564: int ioCreatePIDFile(const char *csName, int ifExists);
  565: 
  566: /*
  567:  * ioSendFile() - AITNET sendfile() userland implementation, not dependant from OS
  568:  *
  569:  * @s = socket
  570:  * @csFile = file for send
  571:  * @sendLen = bytes to send, if 0 send all data
  572:  * @offset = start file offset
  573:  * @sndbuf = SO_SNDBUF value, if 0 use default
  574:  * return: 0 error, >0 ok, sended bytes
  575:  */
  576: size_t ioSendFile(int s, const char *csFile, size_t sendLen, off_t offset, int sndbuf);
  577: /*
  578:  * ioRecvFile() - Receive file from socket, fastest (zero-copy) way
  579:  *
  580:  * @s = socket
  581:  * @csFile = file for receive
  582:  * @recvLen = receive bytes
  583:  * @over = overwrite file if exists with mode like 0644
  584:  * @rcvbuf = SO_RCVBUF value, if 0 use default
  585:  * return: 0 error, >0 ok, received bytes
  586:  */
  587: size_t ioRecvFile(int s, const char *csFile, size_t recvLen, int over, int rcvbuf);
  588: 
  589: /*
  590:  * ioRealFileName() - Get real file name
  591:  *
  592:  * @fname = filename
  593:  * return: =NULL error or !=NULL real filename, should be free with e_free()
  594:  */
  595: char *ioRealFileName(const char *fname);
  596: 
  597: /* Buffered file access over memory block */
  598: 
  599: /*
  600:  * io_fmemopen() - File buffered stream operations over memory block
  601:  *
  602:  * @base = Base address of memory block, if =NULL Infinit length(auto-grow)
  603:  * @basesize = Size of memory block
  604:  * return: NULL error or !=NULL Opened file resource
  605:  */
  606: FILE *io_fmemopen(void ** __restrict base, off_t basesize);
  607: /*
  608:  * io_fmapopen() - File buffered stream operations over MMAP block
  609:  *
  610:  * @csFile = Filename for MMAP, if =NULL private MMAP block
  611:  * @mode = File open mode
  612:  * @perm = If file not exists will be created with this access permissions
  613:  * @prot = MMAP protection
  614:  * @flags = MMAP mode flags
  615:  * @offset = Map from file offset, if csFile==NULL then this is size of MMAP private block
  616:  * return: NULL error or !=NULL Opened file resource
  617:  */
  618: FILE *io_fmapopen(const char *csFile, int mode, int perm, int prot, int flags, off_t offset);
  619: /*
  620:  * io_fd2buf() - Convert open file handle to buffered file I/O
  621:  *
  622:  * @fd = File handle
  623:  * @mode = Permissions for new buffered file I/O
  624:  * return: NULL error or open buffered file
  625:  */
  626: FILE *io_fd2buf(int fd, const char *mode);
  627: /*
  628:  * io_dumbFile() - Create empry or dumb file with fixed size
  629:  *
  630:  * @csFile = Filename for create
  631:  * @mode = File access permissions
  632:  * @size = File size
  633:  * return: -1 error or open file handle
  634:  */
  635: int io_dumbFile(const char *csFile, int mode, off_t size);
  636: #define io_emptyFile io_dumbFile
  637: 
  638: 
  639: /*
  640:  * io_etherOpen() - Open BPF interface to device
  641:  *
  642:  * @csIface = interface name
  643:  * @flags = open flags
  644:  * @whdr = with complete headers
  645:  * @wdlt = with data link type, on Linux is protocol number
  646:  * @buflen = buffer length
  647:  * @zcbuf = zero copy buffer, if BPF supports it and isn't NULL
  648:  * return: -1 error or >-1 bpf handle
  649:  */
  650: int io_etherOpen(const char *csIface, int flags, unsigned int whdr, 
  651: 		unsigned int wdlt, unsigned int *buflen, void **zcbuf);
  652: /*
  653:  * io_etherClose() - Close BPF interface
  654:  *
  655:  * @eth = bpf handle
  656:  * @zcbuf = zero copy buffer, if BPF supports it and isn't NULL
  657:  * return: none
  658:  */
  659: void io_etherClose(int eth, void **zcbuf);
  660: 
  661: #ifndef __linux__
  662: /*
  663:  * io_etherFilter() - BPF filter routine
  664:  *
  665:  * @eth = bpf handle
  666:  * @io = filter direction 
  667:  *  (IO_ETHER_FILTER_PROMISC|IO_ETHER_FILTER_NOTREAD|IO_ETHER_FILTER_READ|IO_ETHER_FILTER_WRITE)
  668:  * @insn = BPF filter instruction array
  669:  * @insnlen = Length of BPF filter instruction array
  670:  * return: -1 error or 0 ok
  671:  */
  672: int io_etherFilter(int eth, int io, struct bpf_insn * __restrict insn, size_t insnlen);
  673: #endif
  674: 
  675: /*
  676:  * io_etherSend() - Send packet to bpf
  677:  *
  678:  * @eth = bpf handle
  679:  * @buf = buffer
  680:  * @buflen = buffer length
  681:  * return: -1 error or !=-1 written bytes
  682:  */
  683: ssize_t io_etherSend(int eth, const void *buf, size_t buflen);
  684: /*
  685:  * io_etherRecv() - Receive packet from bpf
  686:  *
  687:  * @eth = bpf handle
  688:  * @buf = buffer
  689:  * @buflen = buffer length
  690:  * @zcbuf = zero copy buffer, if BPF supports it and isn't NULL
  691:  * return: -1 error or !=-1 readed bytes
  692:  */
  693: ssize_t io_etherRecv(int eth, void * __restrict buf, 
  694: 		size_t buflen, void * __restrict zcbuf);
  695: 
  696: 
  697: #endif

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