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