1: /*************************************************************************
2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
3: * by Michael Pounov <misho@openbsd-bg.org>
4: *
5: * $Author: misho $
6: * $Id: aitrpc.h,v 1.11 2012/11/13 09:22:10 misho Exp $
7: *
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, 2012
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: */
46: #ifndef __AITRPC_H
47: #define __AITRPC_H
48:
49:
50: #include <assert.h>
51: #include <stdlib.h>
52: #include <string.h>
53: #include <errno.h>
54: #include <sys/types.h>
55: #include <sys/param.h>
56: #if !defined(__NetBSD__)
57: #include <sys/limits.h>
58: #endif
59: #include <sys/socket.h>
60: #include <sys/queue.h>
61: #include <aitio.h>
62: #include <aitsched.h>
63:
64:
65: #define RPC_VERSION 4
66: #define RPC_DEFPORT 2611
67:
68: /* RPC call request flags */
69:
70: #define RPC_REPLY 0x0
71: #define RPC_NOREPLY 0x1
72:
73: /* RPC builtin registed calls */
74:
75: #define CALL_TAG_MAX 65535
76:
77: #define CALL_SRVPING 65534
78:
79: #define CALL_SRVSHUTDOWN 65533
80: #define CALL_SRVCLIENTS 65532
81: #define CALL_SRVCALLS 65531
82: #define CALL_SRVSESSIONS 65530
83:
84: #define CALL_BLOBSHUTDOWN 65529
85: #define CALL_BLOBCLIENTS 65528
86: #define CALL_BLOBVARS 65527
87:
88:
89: /* RPC types */
90:
91: typedef enum {
92: ok, error, no, /* for blob reply */
93: get, set, unset /* for blob request */
94: } cmd_type_t;
95:
96:
97: #define RPC_CALLBACK_CHECK_INPUT(x) do { \
98: assert((x)); \
99: if (!(x)) { \
100: rpc_SetErr(EINVAL, \
101: "Invalid callback parameters ..."); \
102: return -1; \
103: } \
104: } while (0)
105:
106:
107: /* RPC session identification */
108:
109: typedef struct {
110: uint8_t sess_version;
111: uint32_t sess_program;
112: uint8_t sess_process;
113: } __packed rpc_sess_t; /* size == 6 bytes */
114:
115:
116: /* Server managment RPC functions ... */
117:
118: /* Network RPC packet - Client request */
119:
120: struct tagRPCCall {
121: rpc_sess_t call_session;
122:
123: uint16_t call_seq;
124: uint16_t call_len;
125: uint16_t call_crc;
126:
127: union {
128: struct {
129: uint64_t flags;
130: } call_req;
131: struct {
132: int32_t ret;
133: int32_t eno;
134: } call_rep;
135: };
136:
137: uint16_t call_tag;
138: uint16_t call_argc;
139: ait_val_t call_argv[0];
140: } __packed; /* size == 24 bytes */
141: #define RPC_CHK_NOREPLY(x) ((x)->call_req.flags & RPC_NOREPLY)
142:
143: /* Network BLOB packet - Header */
144:
145: struct tagBLOBHdr {
146: rpc_sess_t hdr_session;
147: uint8_t hdr_cmd;
148: uint32_t hdr_var;
149: uint32_t hdr_len;
150: uint32_t hdr_ret;
151: uint16_t hdr_crc;
152: uint8_t hdr_pad;
153: } __packed; /* size == 22 bytes */
154:
155: /* Network RPC client & server elements */
156:
157: /* RPC function registration element! */
158: typedef struct tagRPCFunc {
159: ait_val_t func_name;
160:
161: void *func_parent;
162:
163: SLIST_ENTRY(tagRPCFunc) func_next;
164: AVL_ENTRY(tagRPCFunc) func_node;
165: } rpc_func_t;
166: #define RPC_FUNC_SERVER(x) ((rpc_srv_t*) (x)->func_parent)
167:
168: /* Tree root node */
169: typedef struct tagRPCFuncs {
170: pthread_mutex_t mtx;
171:
172: struct tagRPCFunc *slh_first;
173: struct tagRPCFunc *avlh_root;
174: } rpc_funcs_t;
175: #define RPC_FUNCS_LOCK(x) pthread_mutex_lock(&(x)->mtx)
176: #define RPC_FUNCS_UNLOCK(x) pthread_mutex_unlock(&(x)->mtx)
177: #define RPC_FUNCS_ISEMPTY(x) AVL_EMPTY((x))
178:
179:
180: /* BLOB register element */
181: typedef struct tagBLOB {
182: uint32_t blob_var; /* BLOB id */
183:
184: size_t blob_len; /* size of allocated BLOB data */
185: void *blob_data; /* mapped BLOB data */
186:
187: TAILQ_ENTRY(tagBLOB) blob_node;
188: } rpc_blob_t;
189:
190:
191: typedef struct {
192: int cli_id; /* slot id */
193: int cli_sock; /* socket fd */
194: io_sockaddr_t cli_sa; /* host address */
195: ait_val_t cli_buf; /* network buffer */
196:
197: array_t *cli_vars; /* function return variables */
198:
199: void *cli_parent; /* pointer to parent rpc_srv_t for server or to rpc_sess_t for client */
200: } rpc_cli_t;
201: #define RPC_RETVARS(x) ((x)->cli_vars)
202: #define RPC_SRV_SERVER(x) ((rpc_srv_t*) (x)->cli_parent)
203: #define RPC_CLI_SESSION(x) ((rpc_sess_t*) (x)->cli_parent)
204:
205: typedef struct {
206: rpc_sess_t srv_session; /* RPC session registration info */
207: int srv_netbuf; /* size of network buffer */
208:
209: pthread_t srv_tid; /* RPC exec pthread */
210: sched_root_task_t *srv_root; /* RPC server scheduler */
211: intptr_t srv_kill; /* Scheduler condition variable */
212:
213: rpc_cli_t srv_server; /* RPC server socket */
214: array_t *srv_clients; /* connected rpc client sockets */
215:
216: rpc_funcs_t srv_funcs; /* RPC functions */
217:
218: struct {
219: pthread_t tid; /* BLOB exec pthread */
220: sched_root_task_t *root; /* BLOB server scheduler */
221: intptr_t kill; /* BLOB server state: ==0 disable | !=0 enable */
222:
223: ait_val_t dir; /* BLOB states directory */
224:
225: rpc_cli_t server; /* BLOB server socket */
226: array_t *clients; /* connected blob client sockets */
227:
228: TAILQ_HEAD(, tagBLOB) blobs; /* registered blob variables list */
229: } srv_blob;
230: } rpc_srv_t;
231:
232:
233: /*
234: * (*rpc_callback_t)() - Callback type definition for RPC call in server process
235: *
236: * @arg1 = RPC client
237: * @arg2 = RPC packet header
238: * @arg3 = input array with values from RPC call execution request
239: * return: -1 error or >-1 success execution
240: */
241: typedef int (*rpc_callback_t)(rpc_cli_t *, struct tagRPCCall *, array_t *);
242:
243:
244: /* ----------------------------------------------------------------------- */
245:
246: /* Error support functions */
247:
248: // rpc_GetErrno() Get error code of last operation
249: inline int rpc_GetErrno();
250: // rpc_GetError() Get error text of last operation
251: inline const char *rpc_GetError();
252:
253:
254: /*
255: * rpc_chkPktSession() - Check RPC session
256: *
257: * @p = packet session
258: * @s = active session
259: * return: -1, 1, 2, 3 are errors or 0 ok
260: */
261: inline int rpc_chkPktSession(rpc_sess_t *p, rpc_sess_t *s);
262: /*
263: * rpc_addPktSession() - Prepare session into network format
264: *
265: * @p = packet session
266: * @s = host session
267: * return: -1 error or 0 ok
268: */
269: inline int rpc_addPktSession(rpc_sess_t *p, rpc_sess_t *s);
270: /*
271: * rpc_register_srvPing() - Register ping service function
272: *
273: * @srv = RPC server instance
274: * return: -1 error or 0 ok
275: */
276: inline int rpc_register_srvPing(rpc_srv_t * __restrict srv);
277: /*
278: * rpc_register_srvServices() - Register internal service functions
279: *
280: * @srv = RPC server instance
281: * return: -1 error or 0 ok
282: */
283: int rpc_register_srvServices(rpc_srv_t * __restrict srv);
284: /*
285: * rpc_register_blobServices() - Register internal service functions
286: *
287: * @srv = RPC server instance
288: * return: -1 error or 0 ok
289: */
290: int rpc_register_blobServices(rpc_srv_t * __restrict srv);
291:
292:
293: /* RPC Server side functions */
294:
295: /*
296: * rpc_srv_initServer() - Init & create RPC Server
297: *
298: * @regProgID = ProgramID for authentication & recognition
299: * @regProcID = ProcessID for authentication & recognition
300: * @concurentClients = Concurent clients at same time to this server
301: * @netBuf = Network buffer length (min:512 bytes), if =0 == BUFSIZ (also meaning max RPC packet)
302: * @csHost = Host name or address for bind server, if NULL any address
303: * @Port = Port for bind server, if Port == 0 default port is selected
304: * return: NULL == error or !=NULL bind and created RPC server instance
305: */
306: rpc_srv_t *rpc_srv_initServer(unsigned int regProgID, unsigned char regProcID,
307: int concurentClients, int netBuf,
308: const char *csHost, unsigned short Port);
309: /*
310: * rpc_srv_endServer() - Destroy RPC server, close all opened sockets and free resources
311: *
312: * @psrv = RPC Server instance
313: * return: none
314: */
315: inline void rpc_srv_endServer(rpc_srv_t ** __restrict psrv);
316: /*
317: * rpc_srv_loopServer() - Execute Main server loop and wait for clients requests
318: *
319: * @srv = RPC Server instance
320: * return: -1 error or 0 ok, infinite loop ...
321: */
322: int rpc_srv_loopServer(rpc_srv_t * __restrict srv);
323: #define rpc_srv_execServer(_srv, _sync) do { assert((_srv)); \
324: if (!(_srv)->srv_kill) { \
325: pthread_create(&(_srv)->srv_tid, NULL, (void*(*)(void*)) \
326: rpc_srv_loopServer, (_srv)); \
327: if ((_sync)) \
328: pthread_join((_srv)->srv_tid, (void**) (_sync)); \
329: else \
330: pthread_detach((_srv)->srv_tid); \
331: } } while (0)
332:
333: /*
334: * rpc_srv_initBLOBServer() - Init & create BLOB Server
335: *
336: * @srv = RPC server instance
337: * @Port = Port for bind server, if Port == 0 default port is selected
338: * @diskDir = Disk place for BLOB file objects
339: * return: -1 == error or 0 bind and created BLOB server instance
340: */
341: int rpc_srv_initBLOBServer(rpc_srv_t * __restrict srv, unsigned short Port, const char *diskDir);
342: /*
343: * rpc_srv_endBLOBServer() - Destroy BLOB server, close all opened sockets and free resources
344: *
345: * @srv = RPC Server instance
346: * return: none
347: */
348: inline void rpc_srv_endBLOBServer(rpc_srv_t * __restrict srv);
349: /*
350: * rpc_srv_loopBLOB() - Execute Main BLOB server loop and wait for clients requests
351: *
352: * @srv = RPC Server instance
353: * return: -1 error or 0 ok, infinite loop ...
354: */
355: int rpc_srv_loopBLOBServer(rpc_srv_t * __restrict srv);
356: #define rpc_srv_execBLOBServer(_srv) do { assert((_srv)); \
357: if (!(_srv)->srv_kill && !(_srv)->srv_blob.kill) { \
358: pthread_create(&(_srv)->srv_blob.tid, NULL, \
359: (void*(*)(void*)) \
360: rpc_srv_loopBLOBServer, (_srv)); \
361: pthread_detach((_srv)->srv_blob.tid); \
362: } \
363: } while (0)
364:
365: /*
366: * rpc_srv_registerCall() - Register call to RPC server
367: *
368: * @srv = RPC Server instance
369: * @tag = Function tag
370: * @funcaddr = Function address
371: * return: -1 error, 0 already registered tag or 1 register ok
372: */
373: int rpc_srv_registerCall(rpc_srv_t * __restrict srv, unsigned short tag, void *funcaddr);
374: /*
375: * rpc_srv_unregisterCall() - Unregister call from RPC server
376: *
377: * @srv = RPC Server instance
378: * @tag = Function tag
379: * return: -1 error, 0 not found call, 1 unregister ok
380: */
381: int rpc_srv_unregisterCall(rpc_srv_t * __restrict srv, unsigned short tag);
382: /*
383: * rpc_srv_getCall() - Get registered call from RPC server
384: *
385: * @srv = RPC Server instance
386: * @tag = tag for function
387: * return: NULL not found call, !=NULL return call
388: */
389: inline rpc_func_t *rpc_srv_getCall(rpc_srv_t * __restrict srv, uint16_t tag);
390: /*
391: * rpc_srv_execCall() Execute registered call from RPC server
392: *
393: * @cli = RPC client
394: * @rpc = IN RPC call structure
395: * @funcname = Execute RPC function
396: * @args = IN RPC calling arguments from RPC client
397: * return: -1 error, !=-1 ok
398: */
399: int rpc_srv_execCall(rpc_cli_t * __restrict cli, struct tagRPCCall * __restrict rpc,
400: ait_val_t funcname, array_t * __restrict args);
401:
402:
403: /*
404: * rpc_srv_blobCreate() - Create and map blob to memory region and return object
405: *
406: * @srv = RPC Server instance
407: * @len = BLOB length object
408: * return: NULL error or !=NULL allocated BLOB object
409: */
410: inline rpc_blob_t *rpc_srv_blobCreate(rpc_srv_t * __restrict srv, int len);
411: /*
412: * rpc_srv_blobMap() - Map blob to memory region
413: *
414: * @srv = RPC Server instance
415: * @blob = Map to this BLOB element
416: * return: -1 error or 0 ok
417: */
418: inline int rpc_srv_blobMap(rpc_srv_t * __restrict srv, rpc_blob_t * __restrict blob);
419: /*
420: * rpc_srv_blobUnmap() - Unmap blob memory region
421: *
422: * @blob = Mapped BLOB element
423: * return: none
424: */
425: inline void rpc_srv_blobUnmap(rpc_blob_t * __restrict blob);
426: /*
427: * rpc_srv_blobFree() - Free blob from disk & memory
428: *
429: * @srv = RPC Server instance
430: * @blob = Mapped BLOB element
431: * return: -1 error or 0 ok
432: */
433: inline int rpc_srv_blobFree(rpc_srv_t * __restrict srv, rpc_blob_t * __restrict blob);
434:
435: /*
436: * rpc_srv_registerBLOB() - Register new BLOB to server
437: *
438: * @srv = RPC Server instance
439: * @len = BLOB length
440: * return: NULL error or new registered BLOB
441: */
442: rpc_blob_t *rpc_srv_registerBLOB(rpc_srv_t * __restrict srv, size_t len);
443: /*
444: * rpc_srv_unregisterBLOB() - Unregister BLOB from server
445: *
446: * @srv = RPC Server instance
447: * @var = BLOB Variable for unregister
448: * return: -1 error, 0 not found call, 1 unregister ok
449: */
450: int rpc_srv_unregisterBLOB(rpc_srv_t * __restrict srv, uint32_t var);
451: /*
452: * rpc_srv_getBLOB() - Get registered BLOB
453: *
454: * @srv = RPC Server instance
455: * @var = hash for variable
456: * return: NULL not found, !=NULL return blob var
457: */
458: inline rpc_blob_t *rpc_srv_getBLOB(rpc_srv_t * __restrict srv, uint32_t var);
459:
460: /*
461: * rpc_srv_sendBLOB() - Send mapped BLOB to client
462: *
463: * @cli = Client instance
464: * @blob = Mapped BLOB element
465: * return: -1 error, 0 ok
466: */
467: int rpc_srv_sendBLOB(rpc_cli_t * __restrict cli, rpc_blob_t * __restrict blob);
468: /*
469: * rpc_srv_recvBLOB() - Receive BLOB from client
470: *
471: * @cli = Client instance
472: * @blob = Mapped BLOB element
473: * return: -1 error, 0 ok, >0 unreceived data from client, may be error?
474: */
475: int rpc_srv_recvBLOB(rpc_cli_t * __restrict cli, rpc_blob_t * __restrict blob);
476:
477: /* CLIENT part of functions */
478:
479: /*
480: * rpc_cli_sendBLOB() - Send BLOB to server
481: *
482: * @cli = Client instance
483: * @var = BLOB variable
484: * @data = BLOB data
485: * return: -1 error, 0 ok, 1 remote error
486: */
487: int rpc_cli_sendBLOB(rpc_cli_t * __restrict cli, ait_val_t * __restrict var, void * __restrict data);
488: /*
489: * rpc_cli_recvBLOB() - Receive BLOB from server
490: *
491: * @cli = Client instance
492: * @var = BLOB variable
493: * @data = BLOB data, must be io_free after use!
494: * return: -1 error, 0 ok, 1 remote error
495: */
496: int rpc_cli_recvBLOB(rpc_cli_t * __restrict cli, ait_val_t * __restrict var, void ** __restrict data);
497: /*
498: * rpc_cli_delBLOB() - Delete BLOB from server
499: *
500: * @cli = Client instance
501: * @var = BLOB variable
502: * return: -1 error, 0 ok, 1 remote error
503: */
504: int rpc_cli_delBLOB(rpc_cli_t * __restrict cli, ait_val_t * __restrict var);
505: /*
506: * rpc_cli_getBLOB() - Receive BLOB from server and Delete after that.
507: *
508: * @cli = Client instance
509: * @var = BLOB variable
510: * @data = BLOB data, must be io_free after use!
511: * return: -1 error, 0 ok, >0 remote error
512: */
513: inline int rpc_cli_getBLOB(rpc_cli_t * __restrict cli, ait_val_t * __restrict var,
514: void ** __restrict data);
515:
516:
517: /* RPC Client side functions */
518:
519: /*
520: * rpc_cli_openClient() - Connect to RPC Server
521: *
522: * @ProgID = ProgramID for RPC session request
523: * @ProcID = ProcessID for RPC session request
524: * @netBuf = Network buffer length (min:512 bytes), if =0 == BUFSIZ (also meaning max RPC packet)
525: * @csHost = Host name or IP address for bind server
526: * @Port = Port for bind server, if Port == 0 default port is selected
527: * return: NULL == error or !=NULL connection to RPC server established
528: */
529: rpc_cli_t *rpc_cli_openClient(unsigned int ProgID, unsigned char ProcID, int netBuf,
530: const char *csHost, unsigned short Port);
531: /*
532: * rpc_cli_closeClient() - Close connection to RPC server and free resources
533: *
534: * @cli = RPC Client session
535: * return: none
536: */
537: void rpc_cli_closeClient(rpc_cli_t ** __restrict cli);
538: /*
539: * rpc_cli_execCall() - Execute RPC call
540: *
541: * @cli = RPC Client session
542: * @noreply = We not want RPC reply
543: * @tag = Function tag for execution
544: * @in_vars = IN RPC call array of rpc values, may be NULL
545: * @out_vars = OUT returned array of rpc values, if !=NULL must be free after use with io_freeVars()
546: * return: -1 error or != -1 ok result
547: */
548: int rpc_cli_execCall(rpc_cli_t *cli, int noreply, unsigned short tag,
549: array_t * __restrict in_vars, array_t ** __restrict out_vars);
550: /*
551: * rpc_cli_freeCall() - Free resouce allocated by RPC call
552: *
553: * @out_vars = Returned array with variables from RPC call
554: * return: none
555: */
556: inline void rpc_cli_freeCall(array_t ** __restrict out_vars);
557: /*
558: * rpc_cli_ping() - Ping RPC server
559: *
560: * @cli = connected client
561: * return: -1 error or !=-1 ping seq id
562: */
563: inline int rpc_cli_ping(rpc_cli_t *cli);
564:
565:
566: /*
567: * rpc_cli_openBLOBClient() - Connect to BLOB Server
568: *
569: * @rpccli = RPC Client session
570: * @Port = Port for bind server, if Port == 0 default port is selected
571: * return: NULL == error or !=NULL connection to BLOB server established
572: */
573: rpc_cli_t *rpc_cli_openBLOBClient(rpc_cli_t * __restrict rpccli, unsigned short Port);
574: /*
575: * rpc_cli_closeBLOBClient() - Close connection to BLOB server and free resources
576: *
577: * @cli = BLOB Client session
578: * return: none
579: */
580: void rpc_cli_closeBLOBClient(rpc_cli_t ** __restrict cli);
581:
582:
583: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>