File:  [ELWIX - Embedded LightWeight unIX -] / libaitrpc / inc / aitrpc.h
Revision 1.1.1.1.2.1: download - view: text, annotated - select for diffs - revision graph
Fri Jun 18 13:36:01 2010 UTC (14 years ago) by misho
Branches: rpc1_0
Diff to: branchpoint 1.1.1.1: preferred, unified
added shutdown builtin
added mutex control for exit
fix client side

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>