File:  [ELWIX - Embedded LightWeight unIX -] / libaitrpc / src / lists.c
Revision 1.4.2.3: download - view: text, annotated - select for diffs - revision graph
Thu Sep 1 11:39:26 2011 UTC (12 years, 10 months ago) by misho
Branches: rpc1_3
Diff to: branchpoint 1.4: preferred, unified
reworked all builtin command. because audit & changed main logic of returned vars

    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: lists.c,v 1.4.2.3 2011/09/01 11:39:26 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
   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: #include "global.h"
   47: 
   48: 
   49: /*
   50:  * rpc_srv_allocVars() Allocate array for call variables, 
   51: 				if already allocated memory for RPC call reallocate used space
   52:  * @call = RPC function call
   53:  * @varnum = Number of variables, if ==0 free previous allocated variables
   54:  * return: -1 error, !=-1 return varnum value
   55:  */
   56: inline int
   57: rpc_srv_allocVars(rpc_func_t * __restrict call, int varnum)
   58: {
   59: 	register int i;
   60: 	ait_val_t *v;
   61: 
   62: 	if (!call || varnum < 0) {
   63: 		rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t allocate variables for RPC call...\n");
   64: 		return -1;
   65: 	}
   66: 
   67: 	if (varnum) {
   68: 		call->func_vars = io_arrayInit(varnum);
   69: 		if (!call->func_vars)
   70: 			return -1;
   71: 
   72: 		/* allocate ait_val_t elements & add to array */
   73: 		for (i = 0; i < io_arraySize(call->func_vars); i++) {
   74: 			v = malloc(sizeof(ait_val_t));
   75: 			if (!v) {
   76: 				LOGERR;
   77: 				rpc_srv_destroyVars(call);
   78: 				return -1;
   79: 			} else {
   80: 				memset(v, 0, sizeof(ait_val_t));
   81: 				io_arraySet(call->func_vars, i, v);
   82: 			}
   83: 		}
   84: 	}
   85: 
   86: 	return io_arraySize(call->func_vars);
   87: }
   88: 
   89: /*
   90:  * rpc_srv_destroyVars() Destroy variables of array & array
   91:  * @call = RPC function call
   92:  * return: -1 error, !=-1 Returne remained variables
   93:  */
   94: inline int
   95: rpc_srv_destroyVars(rpc_func_t * __restrict call)
   96: {
   97: 	if (!call) {
   98: 		rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t delete variables ...\n");
   99: 		return -1;
  100: 	}
  101: 
  102: 	if (call->func_vars) {
  103: 		rpc_srv_freeVals(call);
  104: 
  105: 		io_arrayFree(call->func_vars);
  106: 		io_arrayDestroy(&call->func_vars);
  107: 	}
  108: 
  109: 	return io_arraySize(call->func_vars);
  110: }
  111: 
  112: /*
  113:  * rpc_srv_freeVals() Clean values from variables of array
  114:  * @call = RPC function call
  115:  * return: -1 error, !=-1 Returned number of cleaned variables
  116:  */
  117: inline int
  118: rpc_srv_freeVals(rpc_func_t * __restrict call)
  119: {
  120: 	register int i;
  121: 
  122: 	if (!call) {
  123: 		rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t delete variables ...\n");
  124: 		return -1;
  125: 	}
  126: 
  127: 	if (call->func_vars) {
  128: 		for (i = 0; i < io_arraySize(call->func_vars); i++)
  129: 			if (io_arrayGet(call->func_vars, i))
  130: 				AIT_FREE_VAL(io_array(call->func_vars, i, ait_val_t*));
  131: 	}
  132: 
  133: 	return io_arraySize(call->func_vars);
  134: }
  135: 
  136: /*
  137:  * rpc_srv_copyVars() Copy variables for RPC call to new variable array
  138:  * @call = RPC function call
  139:  * @newvars = New allocated variables array, must be free after use
  140:  * return: -1 error, !=-1 Returned number of copied RPC variables
  141:  */
  142: inline int
  143: rpc_srv_copyVars(rpc_func_t * __restrict call, array_t ** __restrict newvars)
  144: {
  145: 	if (!call || !newvars) {
  146: 		rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t copy variables to new array\n");
  147: 		return -1;
  148: 	}
  149: 
  150: 	if (io_arrayCopy(newvars, call->func_vars) == -1)
  151: 		return -1;
  152: 
  153: 	return io_arraySize(*newvars);
  154: }
  155: 
  156: /*
  157:  * rpc_srv_getVars() Get variables array for RPC call
  158:  * @call = RPC function call
  159:  * @vars = Returned variables array, may be NULL
  160:  * return: -1 error, !=-1 Number of returned variables
  161:  */
  162: inline int
  163: rpc_srv_getVars(rpc_func_t * __restrict call, array_t ** __restrict vars)
  164: {
  165: 	if (!call) {
  166: 		rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t get variables ...\n");
  167: 		return -1;
  168: 	}
  169: 
  170: 	if (vars)
  171: 		*vars = call->func_vars;
  172: 	return io_arraySize(call->func_vars);
  173: }
  174: 
  175: // ---------------------------------------------------------
  176: 
  177: /*
  178:  * rpc_srv_registerCall() Register call to RPC server
  179:  * @srv = RPC Server instance
  180:  * @csModule = Module name, if NULL self binary
  181:  * @csFunc = Function name
  182:  * @args = Number of return function arguments, use for restriction case!
  183:  * return: -1 error or 0 register ok
  184:  */
  185: int
  186: rpc_srv_registerCall(rpc_srv_t * __restrict srv, const char *csModule, const char *csFunc, u_short args)
  187: {
  188: 	rpc_func_t *func;
  189: 	u_char str[MAXPATHLEN + UCHAR_MAX + 1];
  190: 
  191: 	memset(str, 0, sizeof str);
  192: 	if (!srv || !csFunc) {
  193: 		rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t register function to RPC server ...\n");
  194: 		return -1;
  195: 	}
  196: 	if (!(func = malloc(sizeof(rpc_func_t)))) {
  197: 		LOGERR;
  198: 		return -1;
  199: 	} else {
  200: 		memset(func, 0, sizeof(rpc_func_t));
  201: 		strlcpy((char*) func->func_name, csFunc, sizeof func->func_name);
  202: 	}
  203: 	if (csModule) {
  204: 		strlcpy((char*) func->func_file, csModule, sizeof func->func_file);
  205: 		strlcpy((char*) str, csModule, sizeof str);
  206: 	}
  207: 	strlcat((char*) str, "__", sizeof str);
  208: 	strlcat((char*) str, csFunc, sizeof str);
  209: 
  210: 	func->func_tag = crcFletcher16((u_short*) str, sizeof str / 2);
  211: 	func->func_hash = hash_fnv((char*) str, sizeof str);
  212: 
  213: 	func->func_parent = srv;
  214: 
  215: 	if (args > 0 && rpc_srv_allocVars(func, args) == -1) {
  216: 		free(func);
  217: 		return -1;
  218: 	}
  219: 
  220: 	pthread_mutex_lock(&srv->srv_mtx);
  221: 	func->func_next = srv->srv_funcs;
  222: 	srv->srv_funcs = func;
  223: 	pthread_mutex_unlock(&srv->srv_mtx);
  224: 	return 0;
  225: }
  226: 
  227: /*
  228:  * rpc_srv_unregisterCall() Unregister call from RPC server
  229:  * @srv = RPC Server instance
  230:  * @csModule = Module name, if NULL self binary
  231:  * @csFunc = Function name
  232:  * return: -1 error, 0 not found call, 1 unregister ok
  233:  */
  234: int
  235: rpc_srv_unregisterCall(rpc_srv_t * __restrict srv, const char *csModule, const char *csFunc)
  236: {
  237: 	rpc_func_t func, *f, *curr;
  238: 	u_char str[MAXPATHLEN + UCHAR_MAX + 1];
  239: 
  240: 	memset(&func, 0, sizeof(rpc_func_t));
  241: 	memset(str, 0, sizeof str);
  242: 	if (!srv || !csFunc) {
  243: 		rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t unregister function from RPC server ...\n");
  244: 		return -1;
  245: 	} else
  246: 		strlcpy((char*) func.func_name, csFunc, sizeof func.func_name);
  247: 	if (csModule) {
  248: 		strlcpy((char*) func.func_file, csModule, sizeof func.func_file);
  249: 		strlcpy((char*) str, csModule, sizeof str);
  250: 	}
  251: 	strlcat((char*) str, "__", sizeof str);
  252: 	strlcat((char*) str, csFunc, sizeof str);
  253: 
  254: 	func.func_tag = crcFletcher16((u_short*) str, sizeof str / 2);
  255: 	func.func_hash = hash_fnv((char*) str, sizeof str);
  256: 
  257: 	f = rpc_srv_getCall(srv, func.func_tag, func.func_hash);
  258: 	if (!f)				/* not found element for unregister */
  259: 		return 0;
  260: 
  261: 	pthread_mutex_lock(&srv->srv_mtx);
  262: 	if (srv->srv_funcs == f) {	/* if is 1st element */
  263: 		srv->srv_funcs = srv->srv_funcs->func_next;
  264: 
  265: 		if (f->func_vars)
  266: 			rpc_srv_destroyVars(f);
  267: 		free(f);
  268: 	} else {
  269: 		for (curr = srv->srv_funcs; curr->func_next != f; curr = curr->func_next);
  270: 		curr->func_next = curr->func_next->func_next;
  271: 
  272: 		if (f->func_vars)
  273: 			rpc_srv_destroyVars(f);
  274: 		free(f);
  275: 	}
  276: 	pthread_mutex_unlock(&srv->srv_mtx);
  277: 
  278: 	return 1;
  279: }
  280: 
  281: /*
  282:  * rpc_srv_getCall() Get registered call from RPC server
  283:  * @srv = RPC Server instance
  284:  * @tag = tag for function
  285:  * @hash = hash for function
  286:  * return: NULL not found call, !=NULL return call
  287:  */
  288: inline rpc_func_t *
  289: rpc_srv_getCall(rpc_srv_t * __restrict srv, uint16_t tag, uint32_t hash)
  290: {
  291: 	rpc_func_t *f;
  292: 
  293: 	if (!srv) {
  294: 		rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t get function from RPC server ...\n");
  295: 		return NULL;
  296: 	}
  297: 
  298: 	for (f = srv->srv_funcs; f; f = f->func_next)
  299: 		if (f->func_tag == tag && f->func_hash == hash)
  300: 			break;
  301: 
  302: 	return f;
  303: }
  304: 
  305: /*
  306:  * rpc_srv_getFunc() Get registered call from RPC server by Name
  307:  * @srv = RPC Server instance
  308:  * @csModule = Module name, if NULL self binary
  309:  * @csFunc = Function name
  310:  * return: NULL not found call, !=NULL return call
  311:  */
  312: rpc_func_t *
  313: rpc_srv_getFunc(rpc_srv_t * __restrict srv, const char *csModule, const char *csFunc)
  314: {
  315: 	rpc_func_t func;
  316: 	u_char str[MAXPATHLEN + UCHAR_MAX + 1];
  317: 
  318: 	memset(&func, 0, sizeof(rpc_func_t));
  319: 	memset(str, 0, sizeof str);
  320: 	if (!srv || !csFunc) {
  321: 		rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t get function from RPC server ...\n");
  322: 		return NULL;
  323: 	} else
  324: 		strlcpy((char*) func.func_name, csFunc, sizeof func.func_name);
  325: 	if (csModule) {
  326: 		strlcpy((char*) func.func_file, csModule, sizeof func.func_file);
  327: 		strlcpy((char*) str, csModule, sizeof str);
  328: 	}
  329: 	strlcat((char*) str, "__", sizeof str);
  330: 	strlcat((char*) str, csFunc, sizeof str);
  331: 
  332: 	func.func_tag = crcFletcher16((u_short*) str, sizeof str / 2);
  333: 	func.func_hash = hash_fnv((char*) str, sizeof str);
  334: 
  335: 	return rpc_srv_getCall(srv, func.func_tag, func.func_hash);
  336: }
  337: 
  338: // ---------------------------------------------------------
  339: 
  340: /*
  341:  * rpc_srv_getBLOB() Get registered BLOB 
  342:  * @srv = RPC Server instance
  343:  * @var = hash for variable
  344:  * return: NULL not found, !=NULL return blob var
  345:  */
  346: inline rpc_blob_t *
  347: rpc_srv_getBLOB(rpc_srv_t * __restrict srv, uint32_t var)
  348: {
  349: 	rpc_blob_t *b;
  350: 
  351: 	if (!srv) {
  352: 		rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t get variable from BLOB server ...\n");
  353: 		return NULL;
  354: 	}
  355: 
  356: 	pthread_mutex_lock(&srv->srv_blob.mtx);
  357: 	for (b = srv->srv_blob.blobs; b; b = b->blob_next) {
  358: 		if (b->blob_var == var)
  359: 			break;
  360: 	}
  361: 	pthread_mutex_unlock(&srv->srv_blob.mtx);
  362: 
  363: 	return b;
  364: }
  365: 
  366: /*
  367:  * rpc_srv_registerBLOB() Register new BLOB to server
  368:  * @srv = RPC Server instance
  369:  * @len = BLOB length
  370:  * return: NULL error or new registered BLOB
  371:  */
  372: rpc_blob_t *
  373: rpc_srv_registerBLOB(rpc_srv_t * __restrict srv, size_t len)
  374: {
  375: 	rpc_blob_t *blob = NULL;
  376: 
  377: 	if (!srv || !len) {
  378: 		rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t register BLOB to server ...\n");
  379: 		return blob;
  380: 	}
  381: 
  382: 	blob = rpc_srv_blobCreate(srv, len);
  383: 	if (blob) {
  384: 		pthread_mutex_lock(&srv->srv_blob.mtx);
  385: 		blob->blob_next = srv->srv_blob.blobs;
  386: 		srv->srv_blob.blobs = blob;
  387: 		pthread_mutex_unlock(&srv->srv_blob.mtx);
  388: 	}
  389: 
  390: 	return blob;
  391: }
  392: 
  393: /*
  394:  * rpc_srv_unregisterBLOB() Unregister BLOB from server
  395:  * @srv = RPC Server instance
  396:  * @var = BLOB Variable for unregister
  397:  * return: -1 error, 0 not found call, 1 unregister ok
  398:  */
  399: int
  400: rpc_srv_unregisterBLOB(rpc_srv_t * __restrict srv, uint32_t var)
  401: {
  402: 	rpc_blob_t *b, *curr;
  403: 
  404: 	if (!srv) {
  405: 		rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t unregister BLOB from server ...\n");
  406: 		return -1;
  407: 	}
  408: 
  409: 	b = rpc_srv_getBLOB(srv, var);
  410: 	if (!b)				/* not found element for unregister */
  411: 		return 0;
  412: 
  413: 	pthread_mutex_lock(&srv->srv_blob.mtx);
  414: 	if (srv->srv_blob.blobs == b) {	/* if is 1st element */
  415: 		srv->srv_blob.blobs = srv->srv_blob.blobs->blob_next;
  416: 	} else {
  417: 		for (curr = srv->srv_blob.blobs; curr->blob_next != b; curr = curr->blob_next);
  418: 		curr->blob_next = curr->blob_next->blob_next;
  419: 	}
  420: 	rpc_srv_blobFree(srv, b);
  421: 	free(b);
  422: 	pthread_mutex_unlock(&srv->srv_blob.mtx);
  423: 
  424: 	return 1;
  425: }

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