Annotation of libaitio/inc/aitio.h, revision 1.12.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.12.2.3! misho 6: * $Id: aitio.h,v 1.12.2.2 2011/10/31 15:05:13 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:
15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
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.7 misho 53: #include <openssl/evp.h>
1.12 misho 54: #include <openssl/aes.h>
55: #include <sys/tty.h>
56: #include <sys/ioctl_compat.h>
1.12.2.1 misho 57: #include <sys/socket.h>
58: #include <sys/un.h>
59: #include <net/if_dl.h>
60: #include <netinet/in.h>
1.7 misho 61:
62:
1.8 misho 63: #define VACUUM_LEFT 1
64: #define VACUUM_BETWEEN 2
65:
1.12 misho 66: /* AIT arrays */
1.8 misho 67:
1.9 misho 68: typedef void ** sarr_seg_t;
69: typedef struct _tagSplitArray {
70: int sarr_num;
71: int sarr_seg;
72: int sarr_siz;
73: sarr_seg_t *sarr_data;
74: } sarr_t;
75:
1.8 misho 76: typedef struct _tagArray {
77: int arr_num;
78: void **arr_data;
79: } array_t;
80:
1.12 misho 81: /* URL staff ... */
82:
1.2 misho 83: typedef struct _tagURLItem {
84: int vallen;
85: char *value;
86: } url_Item_t;
87:
88: struct tagIOURL {
89: unsigned char url_line[BUFSIZ];
90:
91: url_Item_t url_tech;
92: url_Item_t url_user;
93: url_Item_t url_pass;
94: url_Item_t url_host;
95: url_Item_t url_port;
96: url_Item_t url_path;
97: url_Item_t url_args;
98:
99: char *url_reserved;
100: };
101:
1.6 misho 102: struct tagReqXML {
103: unsigned char xml_line[BUFSIZ];
104:
105: url_Item_t xml_namespace;
106: union {
107: url_Item_t container;
108: url_Item_t path;
109: } xml_node;
110: url_Item_t xml_data;
111: url_Item_t xml_attribute;
112: url_Item_t xml_value;
113: };
114:
1.12 misho 115: /* AIT RPC variables and managment */
116:
117: typedef enum {
118: empty, /* empty -> variable is not set */
119: buffer, string, blob, /* buffer -> uint8_t*; string -> int8_t*; blob -> void*(+socket); */
120: f32, f64, /* float -> f32; double -> f64 */
121: u8, u16, u32, u64, /* unsigned integers ... */
122: i8, i16, i32, i64, /* integers ... */
123: } ait_type_t;
124:
125: typedef struct {
126: uint8_t val_type;
127: uint32_t val_len;
128: union {
129: uint64_t net;
130:
131: uint8_t *buffer;
132: int8_t *string;
133: uint32_t blob;
134: float f32;
135: double f64;
136: uint8_t u8;
137: uint16_t u16;
138: uint32_t u32;
139: uint64_t u64;
140: int8_t i8;
141: int16_t i16;
142: int32_t i32;
143: int64_t i64;
144: } val;
145: } __packed ait_val_t;
146:
147: #define AIT_TYPE(_vl) ((ait_type_t) (_vl)->val_type)
148: #define AIT_LEN(_vl) (_vl)->val_len
149: #define AIT_BLOB_CHUNKS(_vl, _n) (assert((_vl)), AIT_LEN((_vl)) / _n + (AIT_LEN((_vl)) % _n) ? 1 : 0)
150: #define AIT_ISEMPTY(_vl) (assert((_vl)), AIT_TYPE((_vl)) == empty)
151:
152: #define AIT_GET_BUF(_vl) (assert((_vl)), assert(AIT_TYPE((_vl)) == buffer), (_vl)->val.buffer)
153: #define AIT_GET_STR(_vl) (assert((_vl)), assert(AIT_TYPE((_vl)) == string), (_vl)->val.string)
154: #define AIT_GET_BLOB(_vl) (assert((_vl)), assert(AIT_TYPE((_vl)) == blob), (_vl)->val.blob)
155: #define AIT_GET_U8(_vl) (assert((_vl)), assert(AIT_TYPE((_vl)) == u8), (_vl)->val.u8)
156: #define AIT_GET_U16(_vl) (assert((_vl)), assert(AIT_TYPE((_vl)) == u16), (_vl)->val.u16)
157: #define AIT_GET_U32(_vl) (assert((_vl)), assert(AIT_TYPE((_vl)) == u32), (_vl)->val.u32)
158: #define AIT_GET_U64(_vl) (assert((_vl)), assert(AIT_TYPE((_vl)) == u64), (_vl)->val.u64)
159: #define AIT_GET_I8(_vl) (assert((_vl)), assert(AIT_TYPE((_vl)) == i8), (_vl)->val.i8)
160: #define AIT_GET_I16(_vl) (assert((_vl)), assert(AIT_TYPE((_vl)) == i16), (_vl)->val.i16)
161: #define AIT_GET_I32(_vl) (assert((_vl)), assert(AIT_TYPE((_vl)) == i32), (_vl)->val.i32)
162: #define AIT_GET_I64(_vl) (assert((_vl)), assert(AIT_TYPE((_vl)) == i64), (_vl)->val.i64)
163: #define AIT_GET_F32(_vl) (assert((_vl)), assert(AIT_TYPE((_vl)) == f32), (_vl)->val.f32)
164: #define AIT_GET_F64(_vl) (assert((_vl)), assert(AIT_TYPE((_vl)) == f64), (_vl)->val.f64)
165:
166: #define AIT_SET_BUF(_vl, _v, _len) do { ait_val_t *__val = (_vl); assert(__val); assert((_v)); \
167: __val->val.buffer = malloc(_len); \
168: if (__val->val.buffer) { \
169: __val->val_type = buffer; AIT_LEN(__val) = _len; \
170: memcpy(__val->val.buffer, (_v), _len); \
171: } } while (0)
172: #define AIT_SET_STR(_vl, _v) do { ait_val_t *__val = (_vl); assert(__val); assert((_v)); \
173: __val->val.string = (int8_t*) strdup((_v)); \
174: if (__val->val.string) { \
175: __val->val_type = string; \
176: AIT_LEN(__val) = strlen((_v)) + 1; \
177: } } while (0)
178: #define AIT_SET_BLOB(_vl, _n, _len) do { ait_val_t *__val = (_vl); assert(__val); \
179: __val->val_type = blob; AIT_LEN(__val) = _len; \
180: __val->val.blob = _n; } while (0)
181: #define AIT_SET_BLOB2(_vl, _bv) do { ait_val_t *__val = (_vl); assert(__val); assert((_bv)); \
182: __val->val_type = blob; AIT_LEN(__val) = (_bv)->blob_len; \
183: __val->val.blob = (_bv)->blob_var; } while (0)
184: #define AIT_NEW_BLOB(_vl, _len) AIT_SET_BLOB((_vl), 0, _len)
185:
186: #define AIT_SET_U8(_vl, _n) do { ait_val_t *__val = (_vl); assert(__val); \
187: __val->val_type = u8; __val->val.u8 = _n; \
188: AIT_LEN(__val) = sizeof(uint8_t); } while (0)
189: #define AIT_SET_U16(_vl, _n) do { ait_val_t *__val = (_vl); assert(__val); \
190: __val->val_type = u16; __val->val.u16 = _n; \
191: AIT_LEN(__val) = sizeof(uint16_t); } while (0)
192: #define AIT_SET_U32(_vl, _n) do { ait_val_t *__val = (_vl); assert(__val); \
193: __val->val_type = u32; __val->val.u32 = _n; \
194: AIT_LEN(__val) = sizeof(uint32_t); } while (0)
195: #define AIT_SET_U64(_vl, _n) do { ait_val_t *__val = (_vl); assert(__val); \
196: __val->val_type = u64; __val->val.u64 = _n; \
197: AIT_LEN(__val) = sizeof(uint64_t); } while (0)
198: #define AIT_SET_I8(_vl, _n) do { ait_val_t *__val = (_vl); assert(__val); \
199: __val->val_type = i8; __val->val.i8 = _n; \
200: AIT_LEN(__val) = sizeof(int8_t); } while (0)
201: #define AIT_SET_I16(_vl, _n) do { ait_val_t *__val = (_vl); assert(__val); \
202: __val->val_type = i16; __val->val.i16 = _n; \
203: AIT_LEN(__val) = sizeof(int16_t); } while (0)
204: #define AIT_SET_I32(_vl, _n) do { ait_val_t *__val = (_vl); assert(__val); \
205: __val->val_type = i32; __val->val.i32 = _n; \
206: AIT_LEN(__val) = sizeof(int32_t); } while (0)
207: #define AIT_SET_I64(_vl, _n) do { ait_val_t *__val = (_vl); assert(__val); \
208: __val->val_type = i64; __val->val.i64 = _n; \
209: AIT_LEN(__val) = sizeof(int64_t); } while (0)
210: #define AIT_SET_F32(_vl, _n) do { ait_val_t *__val = (_vl); assert(__val); \
211: __val->val_type = f32; __val->val.f32 = _n; \
212: AIT_LEN(__val) = sizeof(float); } while (0)
213: #define AIT_SET_F64(_vl, _n) do { ait_val_t *__val = (_vl); assert(__val); \
214: __val->val_type = f64; __val->val.f64 = _n; \
215: AIT_LEN(__val) = sizeof(double); } while (0)
216:
217: /* if attribute zeroCopy is set not execute free() */
218: #define AIT_FREE_VAL(_vl) do { ait_val_t *__val = (_vl); assert(__val); \
219: switch (__val->val_type) { \
220: case buffer: \
221: if (__val->val.buffer) { \
222: free(__val->val.buffer); \
223: __val->val.buffer = NULL; \
224: } \
225: break; \
226: case string: \
227: if (__val->val.string) { \
228: free(__val->val.string); \
229: __val->val.string = NULL; \
230: } \
231: break; \
232: default: \
233: break; \
234: } \
235: __val->val_type = empty; \
236: AIT_LEN(__val) = 0; \
237: } while (0)
238:
239: struct io_ether_addr {
240: u_int8_t ether_addr_octet[6];
241: };
1.12.2.2 misho 242: typedef struct io_ether_addr io_ether_addr_t;
1.12 misho 243:
1.12.2.1 misho 244: typedef union {
245: struct sockaddr_storage ss;
246: struct sockaddr sa;
247: struct sockaddr_un sun;
248: struct sockaddr_in sin;
249: struct sockaddr_in6 sin6;
250: struct sockaddr_dl sdl;
251: } io_sockaddr_t;
252:
1.3 misho 253:
1.1 misho 254: // io_GetErrno() Get error code of last operation
255: inline int io_GetErrno();
256: // io_GetError() Get error text of last operation
257: inline const char *io_GetError();
258:
259:
260: /*
1.12 misho 261: * io_ether_ntoa() Convert ethernet address to string
262: * @n = ethernet address structure, like struct ether_addr
263: * @a = string
264: * @len = string length
265: * return: NULL error or !=NULL string a
266: */
267: inline char *io_ether_ntoa(const struct io_ether_addr *n, char * __restrict a, int len);
268: /*
269: * io_ether_aton() Convert string to ethernet address
270: * @a = string
271: * @e = ethernet address structure, like struct ether_addr
272: * return: NULL error or !=NULL ethernet address structure
273: */
274: inline struct io_ether_addr *io_ether_aton(const char *a, struct io_ether_addr *e);
275:
276: /*
277: * io_vals2buffer() Marshaling data from array with variables to buffer
278: * @buf = Buffer
279: * @buflen = Size of buffer
280: * @vars = Variable array
281: * return: -1 error, 0 nothing done or >0 size of marshaled data
282: */
283: int io_vals2buffer(unsigned char *buf, int buflen, array_t *vars);
284: /*
285: * io_buffer2vals() De-marshaling data from buffer to array with variables
286: * @buf = Buffer
287: * @buflen = Size of buffer
288: * @vnum = Number of variables into buffer
289: * @zcpy = Zero-copy for variables, if !=0 don't use io_arrayFree() for free variables and
290: *DON'T MODIFY OR DESTROY BUFFER*. =0 call io_arrayFree() before io_arrayDestroy()
291: * return: =NULL error, !=NULL allocated variable array, after use must free with io_arrayDestroy()
292: */
293: array_t *io_buffer2vals(unsigned char *buf, int buflen, int vnum, int zcpy);
294: /*
295: * io_vals2map() Marshaling data from array with variables to memory map
296: * @buf = Buffer
297: * @buflen = Size of buffer
298: * @vars = Variable array
299: * return: -1 error, 0 nothing done or >0 size of marshaled data
300: */
301: int io_vals2map(u_char *buf, int buflen, array_t *vars);
302: /*
303: * io_map2vals() De-marshaling data from memory map to array with variables
304: * @buf = Buffer
305: * @buflen = Size of buffer
306: * @vnum = Number of variables into buffer
307: * @zcpy = Zero-copy for variables, if !=0 don't use io_arrayFree() for free variables and
308: *DON'T MODIFY OR DESTROY BUFFER*. =0 call io_arrayFree() before io_arrayDestroy()
309: * return: =NULL error, !=NULL allocated variable array, after use must free with io_arrayDestroy()
310: */
311: array_t *io_map2vals(u_char *buf, int buflen, int vnum, int zcpy);
312:
313: /*
1.1 misho 314: * ioPromptRead() Read data from input h[0] with prompt to output h[1]
315: * @h = file handles h[0] = input, h[1] = output, if NULL use stdin, stdout
316: * @csPrompt = Prompt before input, may be NULL
317: * @psData = Readed data
318: * @dataLen = Length of data
319: * return: 0 EOF; -1 error:: can`t read; >0 count of readed chars
320: */
321: int ioPromptRead(int *h, const char *csPrompt, char * __restrict psData, int dataLen);
322: /*
323: * ioPromptPassword() Read password from input h[0] with prompt to output h[1]
324: * @h = file handles h[0] = input, h[1] = output, if NULL use stdin, stdout
325: * @csPrompt = Prompt before input, may be NULL
326: * @psPass = Readed password
327: * @passLen = Length of password
328: * @confirm = Confirm password, 0 - get password, !=0 Ask for confirmation
329: * return: 0 EOF; -1 error:: can`t read; >0 count of readed chars
330: */
331: int ioPromptPassword(int *h, const char *csPrompt, char * __restrict psPass, int passLen, int confirm);
332:
333: /*
334: * ioRegexVerify() Function for verify data match in regex expression
335: * @csRegex = Regulare expression pattern
336: * @csData = Data for check and verify
337: * @startPos = Return start positions
338: * @endPos = Return end positions
339: * return: NULL not match or error; !=NULL begin of matched data
340: */
341: const char *ioRegexVerify(const char *csRegex, const char *csData, int *startPos, int *endPos);
342: /*
343: * ioRegexGet() Function for get data match in regex expression
344: * @csRegex = Regulare expression pattern
345: * @csData = Data from get
346: * @psString = Returned string if match
347: * @strLen = Length of string
348: * return: 0 not match; >0 count of returned chars
349: */
350: int ioRegexGet(const char *csRegex, const char *csData, char * __restrict psString, int strLen);
351: /*
352: * ioRegexReplace() Function for replace data match in regex expression with newdata
353: * @csRegex = Regulare expression pattern
354: * @csData = Source data
355: * @csNew = Data for replace
356: * return: NULL not match or error; !=NULL allocated new string, must be free after use!
357: */
358: char *ioRegexReplace(const char *csRegex, const char *csData, const char *csNew);
359:
1.2 misho 360: /*
1.6 misho 361: * ioVarAst() Function for evaluate string like asterisk variable "{text[:[-]#[:#]]}"
362: * @csString = Input string
363: * return: NULL error, !=NULL Allocated new string evaluated from input string, must be free()
364: */
365: char *ioVarAst(const char *csString);
366:
367: /*
1.2 misho 368: * io_Path2File() Parse and make path/filename pair
369: * @csArgs = Input argument line
370: * @psPath = Output Path, if ==NULL path not returned
371: * @pathLen = Size of path array
372: * @psFile = Output File
373: * @fileLen = Size of file array
374: * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items
375: */
376: inline int io_Path2File(const char * __restrict csArgs, char * __restrict psPath, int pathLen,
377: char * __restrict psFile, int fileLen);
378:
379: /*
1.4 misho 380: * io_UnquotStr() Remove quots from input text string
381: * @psLine = Text string
382: * return: 0 nothing to do; 1 successful unquoted string
383: */
1.5 misho 384: inline int io_UnquotStr(unsigned char * __restrict psLine);
1.4 misho 385: /*
386: * io_LTrimStr() Remove left whitespaces from text string
387: * @psLine = Text string
388: * return: 0 nothing to do; !=0 Removed bytes
389: */
1.5 misho 390: inline int io_LTrimStr(unsigned char * __restrict psLine);
1.4 misho 391: /*
392: * io_RTrimStr() Remove right whitespaces from text string
393: * @psLine = Text string
394: * return: 0 nothing to do; !=0 Removed bytes
395: */
1.5 misho 396: inline int io_RTrimStr(unsigned char * __restrict psLine);
1.4 misho 397: /*
398: * io_TrimStr() Remove left and right whitespaces from text string
399: * @psLine = Text string
400: * return: 0 nothing to do; !=0 Removed bytes
401: */
1.5 misho 402: inline int io_TrimStr(unsigned char * __restrict psLine);
1.4 misho 403: /*
1.5 misho 404: * io_Ch2Hex() Convert from Char string to Hex string
1.4 misho 405: * @psLine = Text string
406: * @lineLen = Length of Text string
1.5 misho 407: * return: NULL nothing to do or error; !=0 Allocated new converted data without term\0 (must be free)
1.4 misho 408: */
1.5 misho 409: inline unsigned char *io_Ch2Hex(unsigned char *psLine, int lineLen);
1.4 misho 410: /*
1.5 misho 411: * io_Hex2Ch() Convert from Hex string to Char string
1.4 misho 412: * @psLine = Text string
413: * @lineLen = Length of Text string
414: * return: NULL nothing to do or error; !=0 Allocated new converted string(must be free)
415: */
1.5 misho 416: inline char *io_Hex2Ch(unsigned char *psLine, int lineLen);
1.4 misho 417:
418: /*
1.8 misho 419: * io_arrayInit() - Create and initialize dynamic array
420: * @numItems = Number of Items
421: * return: NULL error, != NULL allocated memory for array
422: */
423: inline array_t *io_arrayInit(int numItems);
424: /*
1.10 misho 425: * io_arrayDestroy() - Free and destroy dynamic array
1.8 misho 426: * @parr = Array
427: * return: none
428: */
429: inline void io_arrayDestroy(array_t ** __restrict parr);
430: /*
1.10 misho 431: * io_arrayFree() - Free all data in dynamic array items
1.8 misho 432: * (WARNING! If assign static array dont use this!!!)
433: * @arr = Array
434: * return: none
435: */
436: inline void io_arrayFree(array_t * __restrict arr);
437: /*
1.10 misho 438: * io_arrayFrom() - Create and fill array from array with pointers
1.11 misho 439: * @pargv = Array with pointers
1.10 misho 440: * @argc = Number of Items, if 0 walk through argv and stop when reach NULL item
1.11 misho 441: * return: NULL error, != NULL allocated new array
1.10 misho 442: */
1.11 misho 443: inline array_t *io_arrayFrom(const char *** __restrict pargv, int argc);
1.10 misho 444: /*
445: * io_arrayTo() - Create and fill array with pointers from dynamic array
446: * @arr = Array
1.11 misho 447: * return: NULL error, != NULL allocated memory for array, NULL terminated
1.10 misho 448: */
449: inline char **io_arrayTo(array_t * __restrict arr);
450: /*
1.8 misho 451: * io_arrayLen() - Get last used element in dynamic array (array Length)
452: * @arr = Array
453: * return: -1 error, 0 empty or >0 position of last used element
454: */
455: inline int io_arrayLen(array_t * __restrict arr);
456: /*
1.10 misho 457: * io_arrayConcat() Concat source array to destination array
458: * @dest = Destination array
459: * @src = Source array
460: * return: -1 error; >0 new count of destination array
461: */
462: int io_arrayConcat(array_t * __restrict dest, array_t * __restrict src);
463: /*
1.12 misho 464: * io_arrayCopy() Copy source array to destination array
465: * @dest = Destination array, after use free with io_arrayDestroy()
466: * @src = Source array
467: * return: -1 error; >0 count of destination array
468: */
469: int io_arrayCopy(array_t ** __restrict dest, array_t * __restrict src);
470: /*
1.8 misho 471: * io_arrayGrow() - Grow/Shrink dynamic array, Use with care when it shrink!!!
472: * @arr = Array
473: * @newNumItems = Number of Items
1.12 misho 474: * @freeShrink = Free elements before shrink array
1.8 misho 475: * return: -1 error, 0 ok
476: */
1.12 misho 477: int io_arrayGrow(array_t * __restrict arr, int newNumItems, int freeShrink);
1.8 misho 478: /*
479: * io_arrayVacuum() - Vacuum dynamic array, empty elements will be deleted
480: * @arr = Array
481: * @fromWhere = 1 begin, 2 ALL empty elements
482: * return: -1 error, 0 ok
483: */
484: int io_arrayVacuum(array_t * __restrict arr, int fromWhere);
485:
1.12 misho 486: #define io_arraySize(_arr) ((_arr) ? (_arr)->arr_num : 0)
487: #define io_arrayZero(_arr) (assert((_arr)), memset((_arr)->arr_data, 0, \
488: io_arraySize((_arr)) * sizeof(void*)))
1.8 misho 489:
1.11 misho 490: #define io_arrayGet(_arr, _d) (assert((_arr) && (_arr)->arr_num > _d), *((_arr)->arr_data + _d))
491: #define io_array(_arr, _d, _type) (assert((_arr) && (_arr)->arr_num > _d), \
492: ((_type) *((_arr)->arr_data + _d)))
1.8 misho 493: #define io_arraySet(_arr, _d, _ptr) do { \
1.11 misho 494: assert((_arr) && (_arr)->arr_num > _d); \
495: *((_arr)->arr_data + _d) = (void*) (_ptr); \
1.8 misho 496: } while (0)
1.11 misho 497: #define io_arrayDel(_arr, _d, _fri) do { \
498: assert((_arr) && (_arr)->arr_num > _d); \
499: if (_fri) \
500: free(*((_arr)->arr_data + _d)); \
501: *((_arr)->arr_data + _d) = NULL; \
1.9 misho 502: } while (0)
1.8 misho 503:
504: /*
505: * io_arrayPush() - Push element into dynamic array like stack manner, place at first empty position
506: * @arr = Array
507: * @data = Element, if set NULL return only first empty position
508: * return: -1 not found empty position, array is full!, >-1 return position of stored element into array
509: */
510: inline int io_arrayPush(array_t * __restrict arr, void **data);
511: /*
512: * io_arrayPop() - Pop element from dynamic array like stack manner, last used position
513: * @arr = Array
1.11 misho 514: * @data = Element, if set NULL return only last used position
1.8 misho 515: * @delAfter = Delete after Pop element, !=0 delete element from array after return data
516: * return: -1 not found used position, array is empty!, >-1 return element position
517: */
518: inline int io_arrayPop(array_t * __restrict arr, void ** __restrict data, int delAfter);
519:
520: /*
521: * io_argsNum() Parse and calculate number of arguments
522: * @csArgs = Input arguments line
523: * @csDelim = Delimiter(s) for separate
524: * return: 0 error format; -1 error:: can`t read; >0 ok, number of items
525: */
526: inline int io_argsNum(const char *csArgs, const char *csDelim);
527:
528: /*
529: * io_arrayMake() Parse and make array from arguments ... (input string will be modified!!!
530: * and output array must be free with io_arrayDestroy() after use!)
531: * @psArgs = Input arguments line, after execute string is modified!!!
532: * @nargs = Maximum requested count of arguments from input string psArgs, if 0 all psArgs
533: * @csDelim = Delimiter(s) for separate
534: * @parr = Output array of arguments ... (must be free with io_arrayDestroy() after use!)
535: * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items
536: */
537: int io_arrayMake(char * __restrict psArgs, int nargs, const char *csDelim,
538: array_t ** __restrict parr);
539:
540: /*
541: * io_MakeAV() Parse and make attribute/value pair
542: * @csArgs = Input argument line
543: * @csDelim = Delimiter for separate
544: * @psAttr = Output Attribute
545: * @attrLen = Size of attribute array
546: * @psValue = Output Value, if ==NULL this element not present value or not wanted for return
547: * @valLen = Size of value array
548: * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items
549: */
550: int io_MakeAV(const char * __restrict csArgs, const char *csDelim,
551: char * __restrict psAttr, int attrLen, char * __restrict psValue, int valLen);
552:
1.9 misho 553: /*
554: * io_sarrInit() - Create and initialize dynamic split-order array
555: * @numItems = Number of Items
556: * @segLen = Length of segment
557: * return: NULL error, != NULL allocated memory for array
558: */
559: inline sarr_t *io_sarrInit(int numItems, int segLen);
560: /*
561: * io_sarrDestroy() - Free all data in dynamic split-order array and Destroy array
562: * @parr = Array
563: * return: none
564: */
565: inline void io_sarrDestroy(sarr_t ** __restrict parr);
566: /*
567: * io_sarrGrow() - Grow/Shrink dynamic split-order array, Use with care when it shrink!!!
568: * @arr = Array
569: * @newNumItems = Number of Items
570: * return: -1 error, 0 ok
571: */
572: int io_sarrGrow(sarr_t * __restrict arr, int newNumItems);
573: /*
574: * io_sarrVacuum() - Vacuum dynamic split-order array, empty segments will be freed
575: * @arr = Array
576: * return: -1 error, >-1 freed segments
577: */
578: inline int io_sarrVacuum(sarr_t * __restrict arr);
1.12 misho 579: #define io_sarrSize(_arr) ((_arr) ? (_arr)->sarr_num : 0)
580: #define io_sarrSeg(_arr) (assert((_arr)), (_arr)->sarr_seg)
581: /*
582: * io_sarrCopy() Copy source split array to destination split array
583: * @dest = Destination split array, after use free with io_sarrDestroy()
584: * @src = Source split array
585: * return: -1 error; >0 count of destination split array
586: */
587: int io_sarrCopy(sarr_t ** __restrict dest, sarr_t * __restrict src);
1.9 misho 588: /*
589: * io_sarrGet() - Get element from dynamic split-order array
590: * @arr = Array
591: * @idx = Index (warning 1st element is at position 1)
592: * return: NULL not found, !=NULL element
593: */
594: inline void *io_sarrGet(sarr_t * __restrict arr, unsigned int idx);
595: /*
596: * io_sarrGet2() - Always get element from dynamic split-order array
597: * Function automatic grow array. Good use for Hash tables!
598: * @arr = Array
599: * @idx = Index (warning 1st element is at position 1)
600: * return: NULL not found, !=NULL element
601: */
602: void *io_sarrGet2(sarr_t * __restrict arr, unsigned int idx);
603: /*
604: * io_sarrSet() - Set element to dynamic split-order array
605: * @arr = Array
606: * @idx = Index (warning 1st element is at position 1)
607: * @data = Value
608: * return: NULL error or empty, !=NULL old value in element
609: */
610: inline void *io_sarrSet(sarr_t * __restrict arr, unsigned int idx, void *data);
1.11 misho 611: #define io_sarrDel(_arr, _idx) io_sarrSet((_arr), _idx, NULL)
612: #define io_sarr(_arr, _idx, _type) (_type)io_sarrGet((_arr), _idx)
613: /*
614: * io_sarr2array() - Convert from split-order array to dynamic array
615: * @sa = split array
616: * @sarrFree = after convert split array !=0 will be destroyed sarray
617: * return: NULL error or != NULL new array
618: */
619: array_t *io_sarr2array(sarr_t ** __restrict sa, int sarrFree);
620: /*
621: * io_array2sarr() - Convert from dynamic array to split-order array
622: * @a = array
623: * @segLen = Length of segment
624: * @arrFree = after convert array !=0 will be destroyed
625: * return: NULL error or != NULL new sarr
626: */
627: sarr_t *io_array2sarr(array_t ** __restrict a, int segLen, int arrFree);
1.8 misho 628:
629: /*
1.10 misho 630: * io_CopyEnv() Copy environment to new environment array;
631: * @oldenv = Environment array
632: * return: NULL error; !=NULL Allocated new environment array(must be free)
633: */
634: char **io_CopyEnv(const char **oldenv);
635: /*
636: * io_ExecArgs() Build exec arguments from other array
637: * @psProg = Program name for execute
638: * @oldarg = Arguments array
639: * return: NULL error; !=NULL Allocated execution array(must be free)
640: */
641: char **io_ExecArgs(const char *psProg, const char **oldarg);
642: /*
643: * io_FreeNullTerm() Free dynamic allocated null terminated array with strings
644: * @arr = Pointer to array for free
645: * return: none
646: */
647: inline void io_FreeNullTerm(char *** __restrict arr);
648:
649: /*
1.2 misho 650: * ioURLGet() Parse and get data from input URL
651: * @csURL = Input URL line
652: * @url = Output parsed URL
653: * return: 0 error format not find tech:// and return URL like path;
654: -1 error:: can`t read; >0 ok, up bits for known elements
655: */
656: int ioURLGet(const char *csURL, struct tagIOURL *url);
657: /*
658: * ioURLGetValue() Get value from parsed URL
659: * @url = Input parsed URL
660: * @csAttr = Attribute for search
661: * @psValue = Return value of attribute, if ==NULL only check for existence of attribute
662: * @valLen = Size of psValue array
663: * return: 0 error attribute not find; -1 error:: can`t read; >0 ok, find at position
664: */
665: int ioURLGetValue(struct tagIOURL *url, const char *csAttr, char * __restrict psValue, int valLen);
666: /*
667: * ioURLGetFile() Get file from parsed URL
668: * @url = Input parsed URL
669: * @psValue = Return filename, if not specified file in url path, replace with /
670: * @valLen = Size of psValue array
671: * return: -1 error:: can`t read; 0 ok
672: */
673: int ioURLGetFile(struct tagIOURL *url, char * __restrict psValue, int valLen);
674:
1.3 misho 675:
676: /*
1.6 misho 677: * ioXMLGet() Parse and get data from input XML request string [ns:]container[|attribute[=value]][?data]
678: * @csXML = Input XML request line
679: * @xml = Output parsed XML request
680: * return: 0 error format incorrect, -1 error:: can`t read; >0 ok readed elements bits
681: */
682: int ioXMLGet(const char *csXML, struct tagReqXML *xml);
683:
684:
685: /*
1.5 misho 686: * ioMkDir() Function for racursive directory creation and validation
687: * @csDir = Full directory path
688: * @mode = Mode for directory creation if missing dir
689: * return: -1 error, 0 directory path exist, >0 created missing dirs
690: */
691: int ioMkDir(const char *csDir, int mode);
692:
1.6 misho 693: /*
694: * ioWatchDirLoop() Function for watching changes in directory and fire callback
695: * @csDir = Full directory path
696: * @callback = Callback if raise event! nOp -1 delete, 0 change/move, 1 create
697: * return: -1 error, !=-1 ok, number of total signaled events
698: */
699: int ioWatchDirLoop(const char *csDir, int (*callback)(const char *csName, int nOp));
700:
1.5 misho 701:
702: /*
703: * io_rread() Raw VFS read function
704: * @fd = File handle
705: * @buf = Read buffer
706: * @nbytes = Read buffer size
707: * @offset = Read from position, if -1 read nbytes from current position
708: * @update = Update file handle position !0
709: * return: -1 error or !=-1 readed bytes
710: */
711: inline int io_rread(int fd, void * __restrict buf, size_t nbytes, off_t offset, int update);
712: /*
713: * io_rwrite() Raw VFS write function
714: * @fd = File handle
715: * @buf = Write buffer
716: * @nbytes = Write bytes from buffer
717: * @offset = Write at position, if -1 write nbytes from current position
718: * @update = Update file handle position !0
719: * return: -1 error or !=-1 writed bytes
720: */
721: inline int io_rwrite(int fd, void * __restrict buf, size_t nbytes, off_t offset, int update);
722:
723: /* Disk I/O helper macros */
724: #define io_read(f, b, n) io_rread(f, b, n, -1, 1)
725: #define io_write(f, b, n) io_rwrite(f, b, n, -1, 1)
726:
727:
728: /* Debug helper macros */
729: extern int io_Debug;
730:
731: #define io_initDebug(x) io_Debug = (x);
732: #define io_addDebug io_Debug++
733: #define ioDEBUG(x, fmt, ...) do { \
734: assert((fmt)); \
735: char str[STRSIZ] = { 0 }; \
1.11 misho 736: snprintf(str, sizeof str, (fmt), ##__VA_ARGS__); \
1.5 misho 737: if ((x) <= io_Debug) \
738: syslog(LOG_DEBUG, "ioDebug(%d):%s(%d): %s\n", \
739: (x), __func__, __LINE__, str); \
740: } while (0)
741:
742: #define ioERROR(x, fmt, ...) do { \
743: assert((fmt)); \
744: char str[STRSIZ] = { 0 }; \
1.11 misho 745: snprintf(str, sizeof str, (fmt), ##__VA_ARGS__); \
1.5 misho 746: syslog(LOG_ERR, "ioError():%s(%d): #%d - %s\n", \
747: __func__, __LINE__, (x), str); \
748: } while (0)
749: #define io_sysERROR(x) do { \
750: if (x > 0 || errno) \
751: syslog(LOG_ERR, "ioError(sys):%s(%d): #%d - %s\n", \
752: __func__, __LINE__, x > 0 ? x : errno, \
753: strerror(x > 0 ? x : errno)); \
754: } while (0)
755: #define io_aitERROR(ait) do { \
756: if (ait##_GetErrno()) \
757: syslog(LOG_ERR, "ioError(ait):%s(%d): #%d - %s\n", \
758: __func__, __LINE__, ait##_GetErrno(), \
759: ait##_GetError()); \
760: } while (0)
1.3 misho 761:
1.1 misho 762:
1.7 misho 763: /* Crypto framework */
764:
765: /*
766: * ioCipher() Cipher wrapper for all supported crypto algorythms
767: * @pInput = input buffer
768: * @inLen = input buffer len
769: * @ppOutput = output allocated buffe, must be free after use
770: * @Cipher = cipher engine, like EVP_bf_cbc() or etc...
771: * @pKey = key
772: * @pIV = IV, salt (8 bytes)
773: * @nMode = Mode 0 - decrypting or 1 - encrypting
774: * return: 0 not present data or error!; >0 number of processed and returned bytes into ppOutput
775: */
776: int ioCipher(unsigned char *pInput, int inLen, unsigned char **ppOutput, const EVP_CIPHER *Cipher,
777: unsigned char *pKey, unsigned char *pIV, int nMode);
778:
779: /*
780: * io_Blowfish() Blowfish cipher algorythm, work with ASCII hex strings
781: * @pInput = input buffer
782: * @inLen = input buffer len
783: * @ppOutput = output allocated buffe, must be free after use
784: * @pKey = key
785: * @pIV = IV, salt (8 bytes)
786: * @nMode = Mode 0 - decrypting or 1 - encrypting
787: * return: 0 not present data or error!; >0 number of processed and returned bytes into ppOutput
788: */
1.8 misho 789: int io_Blowfish(unsigned char *pInput, int inLen, unsigned char **ppOutput,
790: unsigned char *pKey, unsigned char *pIV, int nMode);
1.12 misho 791: /*
792: * io_ctr_AES() Encrypt/Decrypt stream cipher CTR_AES
793: * @pInput = Input buffer with ASCII
794: * @inLen = Input buffer data length
795: * @ppOutput = Output buffer with cipher data, must be free after use
796: * @pKey = Key
797: * @IV = IVector/Nonce/Counter, Warning: IV must be variable, because we write there!!!
798: * return: -1 error or >-1 how many cipher blocks proceeded
799: */
800: int io_ctr_AES(unsigned char *pInput, int inLen, unsigned char **ppOutput,
801: unsigned char *pKey, unsigned char IV[AES_BLOCK_SIZE]);
802:
803:
804: /*
805: * ioAllocPTY() Allocate new PTY and TTY
806: * @ptyfd = master fd, pty
807: * @ttyfd = slave fd, tty
808: * @name = tty device name if not null
809: * @namesiz = name length, must be above 63 bytes.
810: * @term = termios for terminal
811: * @winz = winsize for terminal
812: * return: -1 error or 0 ok
813: */
814: inline int ioAllocPTY(int *ptyfd, int *ttyfd, char * __restrict name, int namesiz,
815: struct termios * __restrict term, struct winsize * __restrict winz);
816: /*
817: * ioFreePTY() Release PTY and TTY device
818: * @ptyfd = master fd, pty (==-1 skip closing pty)
819: * @ttyname = tty filename
820: * return: none
821: */
822: inline void ioFreePTY(int ptyfd, const char *ttyname);
823: /*
824: * ioChgWinPTY() Change window size of PTY
825: * @ptyfd = master fd, pty
826: * @row = row
827: * @col = col
828: * @xpxl = x pixels
829: * @ypxl = y pixels
830: * return: -1 error or 0 ok
831: */
832: inline int ioChgWinPTY(int ptyfd, unsigned short row, unsigned short col,
833: unsigned short xpxl, unsigned short ypxl);
834: /*
835: * ioSetOwnerTTY() Set owner to TTY
836: * @ttyname = tty filename
837: * @UID = uid
838: * @GID = gid
839: * return: -1 error or 0 ok
840: */
841: int ioSetOwnerTTY(const char *ttyname, uid_t UID, gid_t GID);
842: /*
843: * ioSetSidTTY() Makes the process's controlling TTY and sets it to sane modes.
844: * @ttyfd = slave fd, tty
845: * @ttyname = tty filename
846: * return: -1 error or 0 ok
847: */
848: int ioSetSidTTY(int *ttyfd, const char *ttyname);
849: /*
850: * ioSetRAWMode() Enter into RAW mode
851: * @fd = tty fd
852: * @otio = saved old termios for later restore if !=NULL
853: * return: -1 error or 0 ok
854: */
855: inline int ioSetRAWMode(int fd, struct termios *otio);
856: /*
857: * ioRestoreMode() Restore termios to tty fd
858: * @fd = tty fd
859: * @tio = termios structure for restore
860: * return: -1 error or 0 ok
861: */
862: inline int ioRestoreMode(int fd, struct termios tio);
863: /*
864: * ioForkPTY() Fork new process with session leader and new TTY
865: * @ptyfd = master fd, pty
866: * @name = tty device name if not null
867: * @namesiz = name length, must be above 63 bytes.
868: * @term = termios for terminal
869: * @winz = winsize for terminal
870: * @otio = old termios structure for restore
871: * return: -1 error, 0 child process or >0 parent: pid of child
872: */
873: pid_t ioForkPTY(int *ptyfd, char * __restrict name, int namesiz, struct termios * __restrict term,
874: struct winsize * __restrict winz, struct termios * __restrict otio);
875:
876: /*
877: * ioCreatePIDFile() Create PID file
878: * @csName = PID filename
879: * @ifExists = !=0 if filename exists return error
880: * return: -1 error or 0 ok
881: */
882: inline int ioCreatePIDFile(const char *csName, int ifExists);
1.7 misho 883:
1.12.2.3! misho 884: /*
! 885: * ioSendFile() AITNET sendfile() userland implementation, not dependant from OS
! 886: * @s = socket
! 887: * @csFile = file for send
! 888: * @sendLen = bytes to send, if 0 send all data
! 889: * @offset = start file offset
! 890: * return: 0 error, >0 ok, sended bytes
! 891: */
! 892: size_t ioSendFile(int s, const char *csFile, size_t sendLen, off_t offset);
! 893: /*
! 894: * ioRecvFile() Receive file from socket, fastest (zero-copy) way
! 895: * @s = socket
! 896: * @csFile = file for receive
! 897: * @recvLen = receive bytes
! 898: * @over = overwrite file if exists with mode like 0644
! 899: * return: 0 error, >0 ok, received bytes
! 900: */
! 901: size_t ioRecvFile(int s, const char *csFile, size_t recvLen, int over);
! 902:
1.7 misho 903:
1.1 misho 904: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>