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