Annotation of libaitrpc/inc/aitrpc.h, revision 1.1.1.1.2.5
1.1 misho 1: /*************************************************************************
2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
3: * by Michael Pounov <misho@openbsd-bg.org>
4: *
5: * $Author: misho $
1.1.1.1.2.5! misho 6: * $Id: aitrpc.h,v 1.1.1.1.2.4 2010/06/23 07:50:01 misho Exp $
1.1 misho 7: *
8: *************************************************************************/
9: #ifndef __AITRPC_H
10: #define __AITRPC_H
11:
12:
13: #include <assert.h>
14: #include <stdlib.h>
15: #include <string.h>
16: #include <sys/types.h>
17: #include <sys/param.h>
18: #include <sys/limits.h>
19: #include <sys/socket.h>
20:
21:
22: #define STRSIZ 256
23:
24: #define RPC_VERSION 1
25: #define RPC_DEFPORT 2611
26:
27:
28: /* RPC builtin registed calls */
29:
1.1.1.1.2.5! misho 30: #define CALL_BLOBSHUTDOWN "rpcBLOBServerShutdown"
! 31: #define CALL_BLOBCLIENTS "rpcBLOBServerClients"
! 32: #define CALL_BLOBVARS "rpcBLOBServerVars"
! 33:
1.1.1.1.2.1 misho 34: #define CALL_SRVSHUTDOWN "rpcServerShutdown"
1.1 misho 35: #define CALL_SRVCLIENTS "rpcServerClients"
36: #define CALL_SRVCALLS "rpcServerCalls"
37: #define CALL_SRVSESSIONS "rpcServerSessions"
38:
39:
40: /* RPC types */
41:
42: typedef enum {
43: empty, // empty -> variable is not set
1.1.1.1.2.3 misho 44: buffer, string, blob, // buffer -> uint8_t*; string -> int8_t*; blob -> void*(+socket);
1.1 misho 45: size, offset, datetime, // size -> size_t; offset -> off_t; datetime -> time_t;
46: real, bigreal, // real -> float; bigreal -> double;
47: u8, u16, u32, u64, // unsigned integers ...
48: i8, i16, i32, i64 // integers ...
49: } rpc_type_t;
50:
51: /* RPC value */
52:
53: typedef struct {
54: rpc_type_t val_type;
55: size_t val_len;
56: union {
57: uint8_t *buffer;
58: int8_t *string;
1.1.1.1.2.3 misho 59: void *blob;
1.1 misho 60: size_t size;
61: off_t offset;
62: time_t datetime;
63: float real;
64: double bigreal;
65: uint8_t u8;
66: uint16_t u16;
67: uint32_t u32;
68: uint64_t u64;
69: int8_t i8;
70: int16_t i16;
71: int32_t i32;
72: int64_t i64;
73: } val;
74: } __packed rpc_val_t;
75:
76: #define RPC_TYPE_VAL(vl) ((vl)->val_type)
77: #define RPC_LEN_VAL(vl) ((vl)->val_len)
1.1.1.1.2.3 misho 78: #define RPC_BLOB_CHUNKS(vl, n) ((vl)->val_len / (n) + ((vl)->val_len % (n)) ? 1 : 0)
1.1 misho 79: #define RPC_EMPTY_VAL(vl) ((vl)->val_type == empty)
80:
81: #define RPC_GET_BUF(vl) (assert((vl)->val_type == buffer), (vl)->val.buffer)
82: #define RPC_GET_STR(vl) (assert((vl)->val_type == string), (vl)->val.string)
1.1.1.1.2.3 misho 83: #define RPC_GET_BLOB(vl) (assert((vl)->val_type == blob), (vl)->val.blob)
1.1 misho 84: #define RPC_GET_SIZE(vl) (assert((vl)->val_type == size), (vl)->val.size)
85: #define RPC_GET_OFF(vl) (assert((vl)->val_type == offset), (vl)->val.offset)
86: #define RPC_GET_TIME(vl) (assert((vl)->val_type == datetime), (vl)->val.datetime)
87: #define RPC_GET_REAL(vl) (assert((vl)->val_type == real), (vl)->val.real)
88: #define RPC_GET_BREAL(vl) (assert((vl)->val_type == bigreal), (vl)->val.bigreal)
89: #define RPC_GET_U8(vl) (assert((vl)->val_type == u8), (vl)->val.u8)
90: #define RPC_GET_U16(vl) (assert((vl)->val_type == u16), (vl)->val.u16)
91: #define RPC_GET_U32(vl) (assert((vl)->val_type == u32), (vl)->val.u32)
92: #define RPC_GET_U64(vl) (assert((vl)->val_type == u64), (vl)->val.u64)
93: #define RPC_GET_I8(vl) (assert((vl)->val_type == i8), (vl)->val.i8)
94: #define RPC_GET_I16(vl) (assert((vl)->val_type == i16), (vl)->val.i16)
95: #define RPC_GET_I32(vl) (assert((vl)->val_type == i32), (vl)->val.i32)
96: #define RPC_GET_I64(vl) (assert((vl)->val_type == i64), (vl)->val.i64)
97:
98: #define RPC_SET_BUF(vl, v, l) do { rpc_val_t *val = (vl); assert(val); val->val.buffer = malloc(l); \
99: if (val->val.buffer) { \
100: val->val_type = buffer; val->val_len = l; \
101: memcpy(val->val.buffer, v, l); \
102: } } while (0)
103: #define RPC_SET_STR(vl, v) do { rpc_val_t *val = (vl); assert(val); val->val.string = (int8_t*) strdup(v); \
104: if (val->val.string) { \
105: val->val_type = string; val->val_len = strlen(v) + 1; \
106: } } while (0)
1.1.1.1.2.3 misho 107: #define RPC_SET_BLOB(vl, v, l) do { rpc_val_t *val = (vl); assert(val); val->val.blob = realloc(val->val.blob, l); \
108: if (val->val.blob) { \
109: val->val_type = blob; val->val_len = l; \
110: memcpy(val->val.blob, v, l); \
1.1 misho 111: } } while (0)
112: #define RPC_SET_SIZE(vl, v) do { rpc_val_t *val = (vl); assert(val); val->val_type = size; val->val.size = v; \
113: val->val_len = sizeof(size_t); } while (0)
114: #define RPC_SET_OFF(vl, v) do { rpc_val_t *val = (vl); assert(val); val->val_type = offset; val->val.offset = v; \
115: val->val_len = sizeof(off_t); } while (0)
116: #define RPC_SET_TIME(vl, v) do { rpc_val_t *val = (vl); assert(val); val->val_type = datetime; val->val.datetime = v; \
117: val->val_len = sizeof(time_t); } while (0)
118: #define RPC_SET_REAL(vl, v) do { rpc_val_t *val = (vl); assert(val); val->val_type = real; val->val.real = v; \
119: val->val_len = sizeof(float); } while (0)
120: #define RPC_SET_BREAL(vl, v) do { rpc_val_t *val = (vl); assert(val); val->val_type = bigreal; val->val.bigreal = v; \
121: val->val_len = sizeof(double); } while (0)
122: #define RPC_SET_U8(vl, v) do { rpc_val_t *val = (vl); assert(val); val->val_type = u8; val->val.u8 = v; \
123: val->val_len = sizeof(uint8_t); } while (0)
124: #define RPC_SET_U16(vl, v) do { rpc_val_t *val = (vl); assert(val); val->val_type = u16; val->val.u16 = v; \
125: val->val_len = sizeof(uint16_t); } while (0)
126: #define RPC_SET_U32(vl, v) do { rpc_val_t *val = (vl); assert(val); val->val_type = u32; val->val.u32 = v; \
127: val->val_len = sizeof(uint32_t); } while (0)
128: #define RPC_SET_U64(vl, v) do { rpc_val_t *val = (vl); assert(val); val->val_type = u64; val->val.u64 = v; \
129: val->val_len = sizeof(uint64_t); } while (0)
130: #define RPC_SET_I8(vl, v) do { rpc_val_t *val = (vl); assert(val); val->val_type = i8; val->val.i8 = v; \
131: val->val_len = sizeof(int8_t); } while (0)
132: #define RPC_SET_I16(vl, v) do { rpc_val_t *val = (vl); assert(val); val->val_type = i16; val->val.i16 = v; \
133: val->val_len = sizeof(int16_t); } while (0)
134: #define RPC_SET_I32(vl, v) do { rpc_val_t *val = (vl); assert(val); val->val_type = i32; val->val.i32 = v; \
135: val->val_len = sizeof(int32_t); } while (0)
136: #define RPC_SET_I64(vl, v) do { rpc_val_t *val = (vl); assert(val); val->val_type = i64; val->val.i64 = v; \
137: val->val_len = sizeof(int64_t); } while (0)
138:
139: #define RPC_FREE_VAL(vl) do { rpc_val_t *val = (vl); assert(val); \
140: if (val->val_type == buffer && val->val.buffer) { \
141: free(val->val.buffer); \
142: val->val.buffer = NULL; \
143: } \
144: if (val->val_type == string && val->val.string) { \
145: free(val->val.string); \
146: val->val.string = NULL; \
147: } \
1.1.1.1.2.4 misho 148: if (val->val_type == blob && val->val.blob) { \
149: free(val->val.blob); \
150: val->val.blob = NULL; \
1.1 misho 151: } \
152: val->val_type = val->val_len = 0; \
153: } while (0)
154:
155:
1.1.1.1.2.2 misho 156: #define RPC_CALLBACK_CHK_NUM_ARGS(f, n) do { \
1.1 misho 157: if (f->func_args != n) { \
158: rpc_SetErr(22, "Error:: different number of arguments!\n"); \
159: return -1; \
160: } \
1.1.1.1.2.1 misho 161: } while (0)
162: #define RPC_CALLBACK_CHECK_INPUT(s, f) do { \
163: if (!s || !f) { \
164: rpc_SetErr(22, "Error:: invalid callback parameters ...\n"); \
165: return -1; \
166: } \
167: } while (0)
1.1 misho 168:
169:
170: /* RPC session identification */
171:
172: typedef struct {
173: uint8_t sess_version;
174: uint32_t sess_program;
175: uint32_t sess_process;
176: } __packed rpc_sess_t;
177:
178:
179: /* Server managment RPC functions ... */
180:
181: // RPC function registration element!
182: typedef struct tagRPCFunc {
183: uint16_t func_tag;
184: uint32_t func_hash;
185: int8_t func_file[MAXPATHLEN];
186: int8_t func_name[UCHAR_MAX + 1];
187:
188: int8_t func_args;
189: rpc_val_t *func_vals;
190:
191: struct tagRPCFunc *func_next;
192: } rpc_func_t;
193:
194:
195: /* Network RPC packet - Client request */
196:
197: struct tagRPCCall {
198: rpc_sess_t call_session;
199: uint16_t call_tag;
200: uint32_t call_hash;
201: uint8_t call_argc;
202: } __packed;
203:
204: /* Network RPC packet - Server response */
205:
206: struct tagRPCRet {
207: rpc_sess_t ret_session;
208: uint16_t ret_tag;
209: uint32_t ret_hash;
210: int32_t ret_retcode;
211: int32_t ret_errno;
212: uint8_t ret_argc;
213: } __packed;
214:
215: /* Network RPC client & server elements */
216:
217: typedef struct {
218: struct sockaddr cli_sa; // host info
219: int cli_sock; // socket
220: pthread_t cli_tid; // TID of thread
221:
222: void *cli_parent; // pointer to parent rpc_srv_t for server or to rpc_sess_t for client
223: } rpc_cli_t;
224:
225:
1.1.1.1.2.5! misho 226: // BLOB registration element!
! 227: typedef struct tagBLOB {
! 228: rpc_cli_t *blob_cli; // from RPC client
! 229:
! 230: size_t blob_len; // size of allocated BLOB data
! 231: void *blob_data; // BLOB data
! 232:
! 233: struct tagBLOB *blob_next;
! 234: } rpc_blob_t;
! 235:
! 236: typedef struct {
1.1 misho 237: rpc_sess_t srv_session; // RPC session registration info
1.1.1.1.2.5! misho 238: int srv_numcli; // maximum concurent client connections
1.1 misho 239:
1.1.1.1.2.5! misho 240: rpc_cli_t srv_server; // RPC server socket
! 241: rpc_cli_t *srv_clients; // connected rpc client sockets
1.1 misho 242:
243: rpc_func_t *srv_funcs; // registered functions list
1.1.1.1.2.5! misho 244:
! 245: pthread_mutex_t srv_mtx;
! 246:
! 247: struct {
! 248: int state; // BLOB server state: ==0 disable | !=0 enable
! 249:
! 250: rpc_cli_t server; // BLOB server socket
! 251: rpc_cli_t *clients; // connected blob client sockets
! 252:
! 253: rpc_blob_t *blobs; // registered blob variables list
! 254:
! 255: pthread_mutex_t mtx;
! 256: } srv_blob;
1.1 misho 257: } rpc_srv_t;
258:
259:
260: typedef int (*rpc_callback_t)(void * const, rpc_func_t *, int, rpc_val_t *);
261:
262:
263: // -----------------------------------------------------------------------
264:
265: /* Error support functions */
266:
267: // cli_GetErrno() Get error code of last operation
268: inline int cli_GetErrno();
269: // cli_GetError() Get error text of last operation
270: inline const char *cli_GetError();
271:
272:
273: /* RPC Server side functions */
274:
275: /*
276: * rpc_srv_initServer() Init & create RPC Server
277: * @regProgID = ProgramID for authentication & recognition
278: * @regProcID = ProcessID for authentication & recognition
279: * @concurentClients = Concurent clients at same time to this server
280: * @family = Family socket type, AF_INET or AF_INET6
281: * @csHost = Host name or IP address for bind server, if NULL any address
282: * @Port = Port for bind server, if Port == 0 default port is selected
283: * return: NULL == error or !=NULL bind and created RPC server instance
284: */
285: rpc_srv_t *rpc_srv_initServer(u_int regProgID, u_int regProcID, int concurentClients,
286: u_short family, const char *csHost, u_short Port);
287: /*
288: * rpc_srv_endServer() Destroy RPC server, close all opened sockets and free resources
289: * @srv = RPC Server instance
290: * return: none
291: */
292: void rpc_srv_endServer(rpc_srv_t * __restrict srv);
293: /*
294: * rpc_srv_execServer() Execute Main server loop and wait for clients requests
295: * @srv = RPC Server instance
296: * return: -1 error or 0 ok, infinite loop ...
297: */
298: int rpc_srv_execServer(rpc_srv_t * __restrict srv);
299:
300: /*
1.1.1.1.2.5! misho 301: * rpc_srv_initBLOBServer() Init & create BLOB Server
! 302: * @Port = Port for bind server, if Port == 0 default port is selected
! 303: * return: -1 == error or 0 bind and created BLOB server instance
! 304: */
! 305: int rpc_srv_initBLOBServer(rpc_srv_t * __restrict srv, u_short Port);
! 306: /*
! 307: * rpc_srv_endBLOBServer() Destroy BLOB server, close all opened sockets and free resources
! 308: * @srv = RPC Server instance
! 309: * return: none
! 310: */
! 311: void rpc_srv_endBLOBServer(rpc_srv_t * __restrict srv);
! 312: /*
! 313: * rpc_srv_execBLOBServer() Execute Main BLOB server loop and wait for clients requests
! 314: * @srv = RPC Server instance
! 315: * return: -1 error or 0 ok, infinite loop ...
! 316: */
! 317: int rpc_srv_execBLOBServer(rpc_srv_t * __restrict srv);
! 318:
! 319: /*
1.1 misho 320: * rpc_srv_registerCall() Register call to RPC server
321: * @srv = RPC Server instance
322: * @csModule = Module name, if NULL self binary
323: * @csFunc = Function name
324: * @args = Number of function arguments
325: * return: -1 error or 0 register ok
326: */
327: int rpc_srv_registerCall(rpc_srv_t * __restrict srv, const char *csModule, const char *csFunc,
328: unsigned char args);
329: /*
330: * rpc_srv_unregisterCall() Unregister call from RPC server
331: * @srv = RPC Server instance
332: * @csModule = Module name, if NULL self binary
333: * @csFunc = Function name
334: * return: -1 error, 0 not found call, 1 unregister ok
335: */
336: int rpc_srv_unregisterCall(rpc_srv_t * __restrict srv, const char *csModule, const char *csFunc);
337: /*
338: * rpc_srv_getFunc() Get registered call from RPC server by Name
339: * @srv = RPC Server instance
340: * @csModule = Module name, if NULL self binary
341: * @csFunc = Function name
342: * return: NULL not found call, !=NULL return call
343: */
344: rpc_func_t *rpc_srv_getFunc(rpc_srv_t * __restrict srv, const char *csModule, const char *csFunc);
345: /*
346: * rpc_srv_getCall() Get registered call from RPC server
347: * @srv = RPC Server instance
348: * @tag = tag for function
349: * @hash = hash for function
350: * return: NULL not found call, !=NULL return call
351: */
352: inline rpc_func_t *rpc_srv_getCall(rpc_srv_t * __restrict srv, uint16_t tag, uint32_t hash);
353: /*
354: * rpc_srv_execCall() Execute registered call from RPC server
355: * @data = RPC const data
356: * @call = Register RPC call
357: * @rpc = IN RPC call structure
358: * @args = IN RPC call array of rpc values
359: * return: -1 error, !=-1 ok
360: */
361: int rpc_srv_execCall(void * const data, rpc_func_t * __restrict call,
362: struct tagRPCCall * __restrict rpc, rpc_val_t * __restrict args);
363:
364:
365: /*
366: * rpc_srv_declValsCall() Declare return variables for RPC call
367: * @call = RPC function call
368: * @return_vals = Number of return variables
369: * return: -1 error, !=-1 ok
370: */
371: inline int rpc_srv_declValsCall(rpc_func_t * __restrict call, int return_vals);
372: /*
373: * rpc_srv_freeValsCall() Free return variables for RPC call
374: * @call = RPC function call
375: * return: none
376: */
377: inline void rpc_srv_freeValsCall(rpc_func_t * __restrict call);
378: /*
379: * rpc_srv_copyValsCall() Copy return variables for RPC call to new variable
380: * @call = RPC function call
381: * @newvals = New allocated variables array, must be free after use
382: * return: -1 error, !=-1 Returned number of copied RPC variables
383: */
384: inline int rpc_srv_copyValsCall(rpc_func_t * __restrict call, rpc_val_t ** __restrict newvals);
385: /*
386: * rpc_srv_delValsCall() Clean values from return variables of RPC call
387: * @call = RPC function call
388: * return: -1 error, !=-1 Returned number of cleaned RPC variables
389: */
390: inline int rpc_srv_delValsCall(rpc_func_t * __restrict call);
391: /*
392: * rpc_srv_getValsCall() Get return variables for RPC call
393: * @call = RPC function call
394: * @vals = Returned variables, may be NULL
395: * return: -1 error, !=-1 Number of returned variables
396: */
397: inline int rpc_srv_getValsCall(rpc_func_t * __restrict call, rpc_val_t ** __restrict vals);
398:
399:
400: /* RPC Client side functions */
401:
402: /*
403: * rpc_cli_openClient() Connect to RPC Server
404: * @ProgID = ProgramID for RPC session request
405: * @ProcID = ProcessID for RPC session request
406: * @family = Family socket type, AF_INET or AF_INET6
407: * @csHost = Host name or IP address for bind server
408: * @Port = Port for bind server, if Port == 0 default port is selected
409: * return: NULL == error or !=NULL connection to RPC server established
410: */
411: rpc_cli_t *rpc_cli_openClient(u_int ProgID, u_int ProcID, u_short family,
412: const char *csHost, u_short Port);
413: /*
414: * rpc_cli_closeClient() Close connection to RPC server and free resources
415: * @cli = RPC Client session
416: * return: none
417: */
418: void rpc_cli_closeClient(rpc_cli_t * __restrict cli);
419: /*
420: * rpc_cli_execCall() Execute RPC call
421: * @cli = RPC Client session
422: * @csModule = Module name, if NULL self binary
423: * @csFunc = Function name for execute
424: * @in_argc = IN count of arguments
425: * @in_vals = IN RPC call array of rpc values
426: * @out_argc = OUT returned count of arguments
427: * @out_vals = OUT returned array of rpc values, must be free after use (see rpc_cli_freeVals())
428: * return: -1 error or != -1 ok result
429: */
430: int rpc_cli_execCall(rpc_cli_t *cli, const char *csModule, const char *csFunc, int in_argc,
431: rpc_val_t * __restrict in_vals, int *out_argc, rpc_val_t ** __restrict out_vals);
432: /*
433: * rpc_cli_freeVals() Free rpc_val_t array returned from RPC call
434: * @args = Number of arguments in array
435: * @vals = Value elements
436: * return: none
437: */
438: inline void rpc_cli_freeVals(int args, rpc_val_t *vals);
439:
440:
441: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>