File:  [ELWIX - Embedded LightWeight unIX -] / libaitrpc / inc / aitrpc.h
Revision 1.1.1.1.2.2: download - view: text, annotated - select for diffs - revision graph
Wed Jun 23 07:18:18 2010 UTC (14 years ago) by misho
Branches: rpc1_0
Diff to: branchpoint 1.1.1.1: preferred, unified
change array variable
macro

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

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