File:  [ELWIX - Embedded LightWeight unIX -] / libaitrpc / src / builtin.c
Revision 1.7.2.4: download - view: text, annotated - select for diffs - revision graph
Wed May 16 13:52:59 2012 UTC (12 years, 1 month ago) by misho
Branches: rpc3_3
Diff to: branchpoint 1.7: preferred, unified
move 2 apis to builtin

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

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