File:  [ELWIX - Embedded LightWeight unIX -] / libaitrpc / src / builtin.c
Revision 1.7.2.6: download - view: text, annotated - select for diffs - revision graph
Thu May 17 07:53:03 2012 UTC (12 years, 2 months ago) by misho
Branches: rpc3_3
Diff to: branchpoint 1.7: preferred, unified
add rpc_func helper macros

    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: builtin.c,v 1.7.2.6 2012/05/17 07:53:03 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, 2012
   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: /* builtin RPC server functions */
   50: 
   51: static int
   52: rpcServerClients(rpc_func_t *call, int ic, array_t *iv)
   53: {
   54: 	rpc_srv_t *srv;
   55: 	rpc_cli_t *cli;
   56: 	register int i;
   57: 	int len;
   58: 	const char *str = NULL;
   59: 	char *val;
   60: 	ait_val_t v;
   61: 
   62: 	RPC_CALLBACK_CHECK_INPUT(call);
   63: 	srv = RPC_FUNC_SERVER(call);
   64: 
   65: 	len = io_arraySize(srv->srv_clients) * STRSIZ;
   66: 	if (!(val = malloc(len))) {
   67: 		LOGERR;
   68: 		return -1;
   69: 	} else
   70: 		memset(val, 0, len);
   71: 
   72: 	for (i = 0; i < io_arraySize(srv->srv_clients); i++) {
   73: 		cli = io_array(srv->srv_clients, i, rpc_cli_t*);
   74: 		if (!cli)
   75: 			continue;
   76: 
   77: 		str = io_n2addr(&cli->cli_sa, &v);
   78: 		if (str)
   79: 			strlcat(val, (char*) str, len);
   80: 		else
   81: 			strlcat(val, "0.0.0.0", len);
   82: 		strlcat(val, " ", len);
   83: 		AIT_FREE_VAL(&v);
   84: 	}
   85: 
   86: 	AIT_SET_STR(io_array(RPC_FUNC_RETVARS(call), 0, ait_val_t*), val);
   87: 	free(val);
   88: 	return 0;
   89: }
   90: 
   91: static int
   92: rpcServerCalls(rpc_func_t *call, int ic, array_t *iv)
   93: {
   94: 	rpc_srv_t *srv;
   95: 	rpc_func_t *f;
   96: 	register int i = 0;
   97: 	int len;
   98: 	char *val, str[MAXPATHLEN];
   99: 
  100: 	RPC_CALLBACK_CHECK_INPUT(call);
  101: 	srv = RPC_FUNC_SERVER(call);
  102: 
  103: 	TAILQ_FOREACH(f, &srv->srv_funcs, func_node)
  104: 		i++;
  105: 	len = i * STRSIZ;
  106: 
  107: 	if (!(val = malloc(len))) {
  108: 		LOGERR;
  109: 		return -1;
  110: 	} else
  111: 		memset(val, 0, len);
  112: 
  113: 	TAILQ_FOREACH(f, &srv->srv_funcs, func_node)
  114: 		if (AIT_ADDR(&f->func_name)) {
  115: 			memset(str, 0, sizeof str);
  116: 			snprintf(str, sizeof str, "/%hu/0x%p(%d); ", AIT_KEY(&f->func_name), 
  117: 					AIT_ADDR(&f->func_name), io_arraySize(RPC_FUNC_RETVARS(f)));
  118: 			strlcat(val, str, len);
  119: 		}
  120: 
  121: 	AIT_SET_STR(io_array(RPC_FUNC_RETVARS(call), 0, ait_val_t*), val);
  122: 	free(val);
  123: 	return 0;
  124: }
  125: 
  126: static int
  127: rpcServerSessions(rpc_func_t *call, int ic, array_t *iv)
  128: {
  129: 	rpc_srv_t *srv;
  130: 	ait_val_t *v;
  131: 
  132: 	RPC_CALLBACK_CHECK_INPUT(call);
  133: 	srv = RPC_FUNC_SERVER(call);
  134: 
  135: 	v = io_array(RPC_FUNC_RETVARS(call), 0, ait_val_t*);
  136: 	AIT_SET_U8(v, srv->srv_session.sess_version);
  137: 	v = io_array(RPC_FUNC_RETVARS(call), 1, ait_val_t*);
  138: 	AIT_SET_U32(v, srv->srv_session.sess_program);
  139: 	v = io_array(RPC_FUNC_RETVARS(call), 2, ait_val_t*);
  140: 	AIT_SET_U8(v, srv->srv_session.sess_process);
  141: 	v = io_array(RPC_FUNC_RETVARS(call), 3, ait_val_t*);
  142: 	AIT_SET_I32(v, io_arraySize(srv->srv_clients));
  143: 
  144: 	return 0;
  145: }
  146: 
  147: static int
  148: rpcServerShutdown(rpc_func_t *call, int ic, array_t *iv)
  149: {
  150: 	rpc_srv_t *srv;
  151: 
  152: 	RPC_CALLBACK_CHECK_INPUT(call);
  153: 	srv = RPC_FUNC_SERVER(call);
  154: 
  155: 	srv->srv_kill = 1;
  156: 
  157: 	return 0;
  158: }
  159: 
  160: static int
  161: rpcServerPing(rpc_func_t *call, int ic, array_t *iv)
  162: {
  163: 	rpc_srv_t *srv;
  164: 
  165: 	srv = RPC_FUNC_SERVER(call);
  166: 
  167: 	return 0;
  168: }
  169: 
  170: /* ---------------------------------------------------- */
  171: 
  172: static int
  173: rpcBLOBServerShutdown(rpc_func_t *call, int ic, array_t *iv)
  174: {
  175: 	rpc_srv_t *srv;
  176: 
  177: 	RPC_CALLBACK_CHECK_INPUT(call);
  178: 	srv = RPC_FUNC_SERVER(call);
  179: 
  180: 	srv->srv_blob.kill = 1;
  181: 
  182: 	return 0;
  183: }
  184: 
  185: static int
  186: rpcBLOBServerVars(rpc_func_t *call, int ic, array_t *iv)
  187: {
  188: 	rpc_srv_t *srv;
  189: 	rpc_blob_t *b;
  190: 	register int i = 0;
  191: 	char *val, str[64];
  192: 	int len;
  193: 
  194: 	RPC_CALLBACK_CHECK_INPUT(call);
  195: 	srv = RPC_FUNC_SERVER(call);
  196: 
  197: 	if (srv->srv_blob.kill) {
  198: 		AIT_SET_STR(io_array(RPC_FUNC_RETVARS(call), 0, ait_val_t*), "BLOB Server is killed");
  199: 		return 1;
  200: 	}
  201: 
  202: 	TAILQ_FOREACH(b, &srv->srv_blob.blobs, blob_node)
  203: 		i++;
  204: 	len = i * sizeof str;
  205: 
  206: 	if (!len) {
  207: 		AIT_SET_STR(io_array(RPC_FUNC_RETVARS(call), 0, ait_val_t*), "");
  208: 		return 0;
  209: 	}
  210: 
  211: 	if (!(val = malloc(len))) {
  212: 		LOGERR;
  213: 		return -1;
  214: 	} else
  215: 		memset(val, 0, len);
  216: 
  217: 	TAILQ_FOREACH(b, &srv->srv_blob.blobs, blob_node) {
  218: 		memset(str, 0, sizeof str);
  219: 		snprintf(str, sizeof str, "0x%0X(%lu)=%p ", b->blob_var, (u_long) b->blob_len, b->blob_data);
  220: 		strlcat(val, str, len);
  221: 	}
  222: 
  223: 	AIT_SET_STR(io_array(RPC_FUNC_RETVARS(call), 0, ait_val_t*), val);
  224: 	free(val);
  225: 	return 0;
  226: }
  227: 
  228: static int
  229: rpcBLOBServerClients(rpc_func_t *call, int ic, array_t *iv)
  230: {
  231: 	rpc_srv_t *srv;
  232: 	rpc_cli_t *cli;
  233: 	register int i;
  234: 	int len;
  235: 	const char *str = NULL;
  236: 	char *val;
  237: 	ait_val_t v;
  238: 
  239: 	RPC_CALLBACK_CHECK_INPUT(call);
  240: 	srv = RPC_FUNC_SERVER(call);
  241: 
  242: 	if (srv->srv_blob.kill) {
  243: 		AIT_SET_STR(io_array(RPC_FUNC_RETVARS(call), 0, ait_val_t*), "BLOB Server is killed");
  244: 		return 1;
  245: 	}
  246: 
  247: 	len = io_arraySize(srv->srv_blob.clients) * STRSIZ;
  248: 	if (!(val = malloc(len))) {
  249: 		LOGERR;
  250: 		return -1;
  251: 	} else
  252: 		memset(val, 0, len);
  253: 
  254: 	for (i = 0; i < io_arraySize(srv->srv_clients); i++) {
  255: 		cli = io_array(srv->srv_blob.clients, i, rpc_cli_t*);
  256: 		if (!cli)
  257: 			continue;
  258: 
  259: 		str = io_n2addr(&cli->cli_sa, &v);
  260: 		if (str)
  261: 			strlcat(val, (char*) str, len);
  262: 		else
  263: 			strlcat(val, "0.0.0.0", len);
  264: 		strlcat(val, " ", len);
  265: 		AIT_FREE_VAL(&v);
  266: 	}
  267: 
  268: 	AIT_SET_STR(io_array(RPC_FUNC_RETVARS(call), 0, ait_val_t*), val);
  269: 	free(val);
  270: 	return 0;
  271: }
  272: 
  273: /* ----------------------------------------------------------------- */
  274: 
  275: /*
  276:  * rpc_register_srvServices() - Register internal service functions
  277:  *
  278:  * @srv = RPC server instance
  279:  * return: -1 error or 0 ok
  280:  */
  281: int
  282: rpc_register_srvServices(rpc_srv_t * __restrict srv)
  283: {
  284: 	if (!srv)
  285: 		return -1;
  286: 
  287: 	if (rpc_srv_registerCall(srv, CALL_SRVSHUTDOWN, rpcServerShutdown, 0) < 1)
  288: 		return -1;
  289: 	if (rpc_srv_registerCall(srv, CALL_SRVCLIENTS, rpcServerClients, 1) < 1)
  290: 		return -1;
  291: 	if (rpc_srv_registerCall(srv, CALL_SRVSESSIONS, rpcServerSessions, 4) < 1)
  292: 		return -1;
  293: 	if (rpc_srv_registerCall(srv, CALL_SRVCALLS, rpcServerCalls, 1) < 1)
  294: 		return -1;
  295: 
  296: 	return 0;
  297: }
  298: 
  299: /*
  300:  * rpc_register_blobServices() - Register internal service functions
  301:  *
  302:  * @srv = RPC server instance
  303:  * return: -1 error or 0 ok
  304:  */
  305: int
  306: rpc_register_blobServices(rpc_srv_t * __restrict srv)
  307: {
  308: 	if (!srv)
  309: 		return -1;
  310: 
  311: 	if (rpc_srv_registerCall(srv, CALL_BLOBSHUTDOWN, rpcBLOBServerShutdown, 0) < 1)
  312: 		return -1;
  313: 	if (rpc_srv_registerCall(srv, CALL_BLOBCLIENTS, rpcBLOBServerClients, 1) < 1)
  314: 		return -1;
  315: 	if (rpc_srv_registerCall(srv, CALL_BLOBVARS, rpcBLOBServerVars, 1) < 1)
  316: 		return -1;
  317: 
  318: 	return 0;
  319: }

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