File:  [ELWIX - Embedded LightWeight unIX -] / libaitio / inc / aitio.h
Revision 1.39: download - view: text, annotated - select for diffs - revision graph
Sun Dec 15 22:57:20 2013 UTC (10 years, 6 months ago) by misho
Branches: MAIN
CVS tags: io6_9, io6_8, IO6_8, IO6_7, HEAD
version 6.7

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

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