File:  [ELWIX - Embedded LightWeight unIX -] / libaitrpc / inc / aitrpc.h
Revision 1.1.1.1.2.3: download - view: text, annotated - select for diffs - revision graph
Wed Jun 23 07:47:25 2010 UTC (14 years ago) by misho
Branches: rpc1_0
Diff to: branchpoint 1.1.1.1: preferred, unified
remove type array and add blob

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

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