File:  [ELWIX - Embedded LightWeight unIX -] / libaitrpc / src / builtin.c
Revision 1.3.2.3: download - view: text, annotated - select for diffs - revision graph
Wed Aug 31 17:11:58 2011 UTC (12 years, 11 months ago) by misho
Branches: rpc1_3
Diff to: branchpoint 1.3: preferred, unified
totaly reworked all RPC!!!!!!
changed type of core elements to array_t!!!!

*** NOT TESTED *****!!!

    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.3.2.3 2011/08/31 17:11:58 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: /* builtin RPC server functions */
   50: 
   51: int
   52: rpcServerClients(rpc_func_t *call, int ic, array_t *iv)
   53: {
   54: 	rpc_srv_t *srv;
   55: 	array_t *vals;
   56: 	ait_val_t *v;
   57: 	rpc_cli_t *cli;
   58: 	register int i;
   59: 	const char *str = NULL;
   60: 	char wrk[INET6_ADDRSTRLEN];
   61: 	struct sockaddr_in *s;
   62: 	struct sockaddr_in6 *s6;
   63: 	struct sockaddr_un *su;
   64: 
   65: 	RPC_CALLBACK_CHECK_INPUT(call);
   66: 	if (!call->func_parent)
   67: 		return -1;
   68: 	else
   69: 		srv = call->func_parent;
   70: 
   71: 	if (!(vals = rpc_srv_returnVars(call, srv->srv_numcli)))
   72: 		return -1;
   73: 
   74: 	for (i = 0, cli = srv->srv_clients; i < srv->srv_numcli; i++, cli++) {
   75: 		v = io_array(vals, i, ait_val_t*);
   76: 		if (!cli->cli_sa.sa_family) {
   77: 			AIT_SET_STR(v, "");
   78: 			continue;
   79: 		}
   80: 
   81: 		switch (cli->cli_sa.sa_family) {
   82: 			case AF_INET:
   83: 				s = (struct sockaddr_in*) &cli->cli_sa;
   84: 				str = inet_ntop(cli->cli_sa.sa_family, &s->sin_addr, wrk, sizeof wrk);
   85: 				break;
   86: 			case AF_INET6:
   87: 				s6 = (struct sockaddr_in6*) &cli->cli_sa;
   88: 				str = inet_ntop(cli->cli_sa.sa_family, &s6->sin6_addr, wrk, sizeof wrk);
   89: 				break;
   90: 			case AF_LOCAL:
   91: 				su = (struct sockaddr_un*) &cli->cli_sa;
   92: 				str = su->sun_path;
   93: 				break;
   94: 		}
   95: 		if (str)
   96: 			AIT_SET_STR(v, (char*) str);
   97: 		else
   98: 			AIT_SET_STR(v, "0.0.0.0");
   99: 	}
  100: 
  101: 	return 0;
  102: }
  103: 
  104: int
  105: rpcServerCalls(rpc_func_t *call, int ic, array_t *iv)
  106: {
  107: 	rpc_srv_t *srv;
  108: 	array_t *vals;
  109: 	ait_val_t *v;
  110: 	rpc_func_t *f;
  111: 	register int i;
  112: 	char str[MAXPATHLEN];
  113: 
  114: 	RPC_CALLBACK_CHECK_INPUT(call);
  115: 	if (!call->func_parent)
  116: 		return -1;
  117: 	else
  118: 		srv = call->func_parent;
  119: 
  120: 	for (i = 0, f = srv->srv_funcs; f; i++, f = f->func_next);
  121: 	if (!(vals = rpc_srv_returnVars(call, i)))
  122: 		return -1;
  123: 
  124: 	for (f = srv->srv_funcs, i = 0; f; f = f->func_next) {
  125: 		if (*f->func_name) {
  126: 			memset(str, 0, sizeof str);
  127: 			snprintf(str, sizeof str, "/%s/%s()", f->func_file, f->func_name);
  128: 			v = io_array(vals, i++, ait_val_t*);
  129: 			AIT_SET_STR(v, str);
  130: 		}
  131: 	}
  132: 
  133: 	return 0;
  134: }
  135: 
  136: int
  137: rpcServerSessions(rpc_func_t *call, int ic, array_t *iv)
  138: {
  139: 	rpc_srv_t *srv;
  140: 	array_t *vals;
  141: 	ait_val_t *v;
  142: 
  143: 	RPC_CALLBACK_CHECK_INPUT(call);
  144: 	if (!call->func_parent)
  145: 		return -1;
  146: 	else
  147: 		srv = call->func_parent;
  148: 
  149: 	if (!(vals = rpc_srv_returnVars(call, 4)))
  150: 		return -1;
  151: 
  152: 	v = io_array(vals, 0, ait_val_t*);
  153: 	AIT_SET_U8(v, srv->srv_session.sess_version);
  154: 	v = io_array(vals, 1, ait_val_t*);
  155: 	AIT_SET_U32(v, srv->srv_session.sess_program);
  156: 	v = io_array(vals, 2, ait_val_t*);
  157: 	AIT_SET_U32(v, srv->srv_session.sess_process);
  158: 	v = io_array(vals, 3, ait_val_t*);
  159: 	AIT_SET_I32(v, srv->srv_numcli);
  160: 
  161: 	return 0;
  162: }
  163: 
  164: int
  165: rpcServerShutdown(rpc_func_t *call, int ic, array_t *iv)
  166: {
  167: 	rpc_srv_t *srv;
  168: 
  169: 	RPC_CALLBACK_CHECK_INPUT(call);
  170: 	if (!call->func_parent)
  171: 		return -1;
  172: 	else
  173: 		srv = call->func_parent;
  174: 
  175: 	pthread_mutex_lock(&srv->srv_mtx);
  176: 	srv->srv_kill = kill;
  177: 	pthread_mutex_unlock(&srv->srv_mtx);
  178: 
  179: 	return 0;
  180: }
  181: 
  182: // ----------------------------------------------------
  183: 
  184: int
  185: rpcBLOBServerShutdown(rpc_func_t *call, int ic, array_t *iv)
  186: {
  187: 	rpc_srv_t *srv;
  188: 
  189: 	RPC_CALLBACK_CHECK_INPUT(call);
  190: 	if (!call->func_parent)
  191: 		return -1;
  192: 	else
  193: 		srv = call->func_parent;
  194: 
  195: 	pthread_mutex_lock(&srv->srv_blob.mtx);
  196: 	srv->srv_blob.state = kill;
  197: 	pthread_mutex_unlock(&srv->srv_blob.mtx);
  198: 
  199: 	return 0;
  200: }
  201: 
  202: int
  203: rpcBLOBServerVars(rpc_func_t *call, int ic, array_t *iv)
  204: {
  205: 	rpc_srv_t *srv;
  206: 	array_t *vals;
  207: 	ait_val_t *v;
  208: 	rpc_blob_t *b;
  209: 	register int i;
  210: 
  211: 	RPC_CALLBACK_CHECK_INPUT(call);
  212: 	if (!call->func_parent)
  213: 		return -1;
  214: 	else
  215: 		srv = call->func_parent;
  216: 
  217: 	pthread_mutex_lock(&srv->srv_blob.mtx);
  218: 	for (i = 0, b = srv->srv_blob.blobs; b; i++, b = b->blob_next);
  219: 	if (!(vals = rpc_srv_returnVars(call, i))) {
  220: 		pthread_mutex_unlock(&srv->srv_blob.mtx);
  221: 		return 0;
  222: 	}
  223: 
  224: 	for (b = srv->srv_blob.blobs, i = 0; b; i++, b = b->blob_next) {
  225: 		v = io_array(vals, i, ait_val_t*);
  226: 		AIT_SET_U32(v, b->blob_var);
  227: 	}
  228: 	pthread_mutex_unlock(&srv->srv_blob.mtx);
  229: 
  230: 	return 0;
  231: }
  232: 
  233: int
  234: rpcBLOBServerState(rpc_func_t *call, int ic, array_t *iv)
  235: {
  236: 	rpc_srv_t *srv;
  237: 
  238: 	RPC_CALLBACK_CHK_ARGS(call, ic);
  239: 	if (!call->func_parent)
  240: 		return -1;
  241: 	else
  242: 		srv = call->func_parent;
  243: 
  244: 	if (AIT_TYPE(io_array(iv, 0, ait_val_t*)) != i32)
  245: 		return -1;
  246: 
  247: 	srv->srv_blob.state = AIT_GET_I32(io_array(iv, 0, ait_val_t*));
  248: 	return 0;
  249: }
  250: 
  251: int
  252: rpcBLOBServerClients(rpc_func_t *call, int ic, array_t *iv)
  253: {
  254: 	rpc_srv_t *srv;
  255: 	array_t *vals;
  256: 	ait_val_t *v;
  257: 	rpc_cli_t *cli;
  258: 	register int i;
  259: 	const char *str = NULL;
  260: 	char wrk[INET6_ADDRSTRLEN];
  261: 	struct sockaddr_in *s;
  262: 	struct sockaddr_in6 *s6;
  263: 	struct sockaddr_un *su;
  264: 
  265: 	RPC_CALLBACK_CHECK_INPUT(call);
  266: 	if (!call->func_parent)
  267: 		return -1;
  268: 	else
  269: 		srv = call->func_parent;
  270: 
  271: 	if (!(vals = rpc_srv_returnVars(call, srv->srv_numcli)))
  272: 		return -1;
  273: 
  274: 	for (i = 0, cli = srv->srv_blob.clients; i < srv->srv_numcli; i++, cli++) {
  275: 		v = io_array(vals, i, ait_val_t*);
  276: 
  277: 		if (!cli->cli_sa.sa_family) {
  278: 			AIT_SET_STR(v, "");
  279: 			continue;
  280: 		}
  281: 
  282: 		switch (cli->cli_sa.sa_family) {
  283: 			case AF_INET:
  284: 				s = (struct sockaddr_in*) &cli->cli_sa;
  285: 				str = inet_ntop(cli->cli_sa.sa_family, &s->sin_addr, wrk, sizeof wrk);
  286: 				break;
  287: 			case AF_INET6:
  288: 				s6 = (struct sockaddr_in6*) &cli->cli_sa;
  289: 				str = inet_ntop(cli->cli_sa.sa_family, &s6->sin6_addr, wrk, sizeof wrk);
  290: 				break;
  291: 			case AF_LOCAL:
  292: 				su = (struct sockaddr_un*) &cli->cli_sa;
  293: 				str = su->sun_path;
  294: 				break;
  295: 		}
  296: 		if (str)
  297: 			AIT_SET_STR(v, (char*) str);
  298: 		else
  299: 			AIT_SET_STR(v, "0.0.0.0");
  300: 	}
  301: 
  302: 	return 0;
  303: }

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