File:  [ELWIX - Embedded LightWeight unIX -] / libaitrpc / inc / aitrpc.h
Revision 1.1.1.1.2.6: download - view: text, annotated - select for diffs - revision graph
Wed Jun 23 15:07:15 2010 UTC (14 years, 3 months ago) by misho
Branches: rpc1_0
Diff to: branchpoint 1.1.1.1: preferred, unified
new file

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

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