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