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