File:  [ELWIX - Embedded LightWeight unIX -] / libaitrpc / src / builtin.c
Revision 1.4.2.1: download - view: text, annotated - select for diffs - revision graph
Mon Oct 31 14:45:26 2011 UTC (12 years, 8 months ago) by misho
Branches: rpc2_0
Diff to: branchpoint 1.4: preferred, unified
add new io_sockaddr_t and migrate sockaddr to new sockaddr type

    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.4.2.1 2011/10/31 14:45: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: /* 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: 	rpc_cli_t *cli;
   57: 	register int i;
   58: 	int len;
   59: 	const char *str = NULL;
   60: 	char *val, wrk[INET6_ADDRSTRLEN];
   61: 
   62: 	RPC_CALLBACK_CHECK_INPUT(call);
   63: 	if (!call->func_parent)
   64: 		return -1;
   65: 	else
   66: 		srv = call->func_parent;
   67: 
   68: 	rpc_srv_getVars(call, &vals);
   69: 
   70: 	len = srv->srv_numcli * STRSIZ;
   71: 	if (!(val = malloc(len))) {
   72: 		LOGERR;
   73: 		return -1;
   74: 	} else
   75: 		memset(val, 0, len);
   76: 
   77: 	for (i = 0, cli = srv->srv_clients; i < srv->srv_numcli; i++, cli++) {
   78: 		if (!cli->cli_sa.sa.sa_family)
   79: 			continue;
   80: 
   81: 		switch (cli->cli_sa.sa.sa_family) {
   82: 			case AF_INET:
   83: 				str = inet_ntop(cli->cli_sa.sa.sa_family, &cli->cli_sa.sin.sin_addr, wrk, sizeof wrk);
   84: 				break;
   85: 			case AF_INET6:
   86: 				str = inet_ntop(cli->cli_sa.sa.sa_family, &cli->cli_sa.sin6.sin6_addr, wrk, sizeof wrk);
   87: 				break;
   88: 			case AF_LOCAL:
   89: 				str = cli->cli_sa.sun.sun_path;
   90: 				break;
   91: 		}
   92: 		if (str)
   93: 			strlcat(val, (char*) str, len);
   94: 		else
   95: 			strlcat(val, "0.0.0.0", len);
   96: 		strlcat(val, " ", len);
   97: 	}
   98: 
   99: 	AIT_SET_STR(io_array(vals, 0, ait_val_t*), val);
  100: 	free(val);
  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: 	rpc_func_t *f;
  110: 	register int i;
  111: 	int len;
  112: 	char *val, 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: 	len = i * STRSIZ;
  122: 
  123: 	rpc_srv_getVars(call, &vals);
  124: 
  125: 	if (!(val = malloc(len))) {
  126: 		LOGERR;
  127: 		return -1;
  128: 	} else
  129: 		memset(val, 0, len);
  130: 
  131: 	for (f = srv->srv_funcs, i = 0; f; f = f->func_next) {
  132: 		if (*f->func_name) {
  133: 			memset(str, 0, sizeof str);
  134: 			snprintf(str, sizeof str, "/%s/%s(%d); ", 
  135: 					f->func_file, f->func_name, io_arraySize(f->func_vars));
  136: 			strlcat(val, str, len);
  137: 		}
  138: 	}
  139: 
  140: 	AIT_SET_STR(io_array(vals, 0, ait_val_t*), val);
  141: 	free(val);
  142: 	return 0;
  143: }
  144: 
  145: int
  146: rpcServerSessions(rpc_func_t *call, int ic, array_t *iv)
  147: {
  148: 	rpc_srv_t *srv;
  149: 	array_t *vals;
  150: 	ait_val_t *v;
  151: 
  152: 	RPC_CALLBACK_CHECK_INPUT(call);
  153: 	if (!call->func_parent)
  154: 		return -1;
  155: 	else
  156: 		srv = call->func_parent;
  157: 
  158: 	rpc_srv_getVars(call, &vals);
  159: 
  160: 	v = io_array(vals, 0, ait_val_t*);
  161: 	AIT_SET_U8(v, srv->srv_session.sess_version);
  162: 	v = io_array(vals, 1, ait_val_t*);
  163: 	AIT_SET_U32(v, srv->srv_session.sess_program);
  164: 	v = io_array(vals, 2, ait_val_t*);
  165: 	AIT_SET_U32(v, srv->srv_session.sess_process);
  166: 	v = io_array(vals, 3, ait_val_t*);
  167: 	AIT_SET_I32(v, srv->srv_numcli);
  168: 
  169: 	return 0;
  170: }
  171: 
  172: int
  173: rpcServerShutdown(rpc_func_t *call, int ic, array_t *iv)
  174: {
  175: 	rpc_srv_t *srv;
  176: 
  177: 	RPC_CALLBACK_CHECK_INPUT(call);
  178: 	if (!call->func_parent)
  179: 		return -1;
  180: 	else
  181: 		srv = call->func_parent;
  182: 
  183: 	pthread_mutex_lock(&srv->srv_mtx);
  184: 	srv->srv_kill = kill;
  185: 	pthread_mutex_unlock(&srv->srv_mtx);
  186: 
  187: 	return 0;
  188: }
  189: 
  190: // ----------------------------------------------------
  191: 
  192: int
  193: rpcBLOBServerShutdown(rpc_func_t *call, int ic, array_t *iv)
  194: {
  195: 	rpc_srv_t *srv;
  196: 
  197: 	RPC_CALLBACK_CHECK_INPUT(call);
  198: 	if (!call->func_parent)
  199: 		return -1;
  200: 	else
  201: 		srv = call->func_parent;
  202: 
  203: 	pthread_mutex_lock(&srv->srv_blob.mtx);
  204: 	srv->srv_blob.state = kill;
  205: 	pthread_mutex_unlock(&srv->srv_blob.mtx);
  206: 
  207: 	return 0;
  208: }
  209: 
  210: int
  211: rpcBLOBServerVars(rpc_func_t *call, int ic, array_t *iv)
  212: {
  213: 	rpc_srv_t *srv;
  214: 	array_t *vals;
  215: 	rpc_blob_t *b;
  216: 	register int i;
  217: 	char *val, str[64];
  218: 	int len;
  219: 
  220: 	RPC_CALLBACK_CHECK_INPUT(call);
  221: 	if (!call->func_parent)
  222: 		return -1;
  223: 	else
  224: 		srv = call->func_parent;
  225: 
  226: 	rpc_srv_getVars(call, &vals);
  227: 
  228: 	if (srv->srv_blob.state == kill) {
  229: 		AIT_SET_STR(io_array(vals, 0, ait_val_t*), "BLOB Server is killed");
  230: 		return 1;
  231: 	}
  232: 	if (srv->srv_blob.state == disable) {
  233: 		AIT_SET_STR(io_array(vals, 0, ait_val_t*), "BLOB Server is disabled");
  234: 		return 1;
  235: 	}
  236: 
  237: 	pthread_mutex_lock(&srv->srv_blob.mtx);
  238: 	for (i = 0, b = srv->srv_blob.blobs; b; i++, b = b->blob_next);
  239: 	len = i * sizeof str;
  240: 
  241: 	if (!len) {
  242: 		AIT_SET_STR(io_array(vals, 0, ait_val_t*), "");
  243: 		pthread_mutex_unlock(&srv->srv_blob.mtx);
  244: 		return 0;
  245: 	}
  246: 
  247: 	if (!(val = malloc(len))) {
  248: 		LOGERR;
  249: 		pthread_mutex_unlock(&srv->srv_blob.mtx);
  250: 		return -1;
  251: 	} else
  252: 		memset(val, 0, len);
  253: 
  254: 	for (b = srv->srv_blob.blobs, i = 0; b; i++, b = b->blob_next) {
  255: 		memset(str, 0, sizeof str);
  256: 		snprintf(str, sizeof str, "0x%0X(%lu)=%p ", b->blob_var, (u_long) b->blob_len, b->blob_data);
  257: 		strlcat(val, str, len);
  258: 	}
  259: 
  260: 	AIT_SET_STR(io_array(vals, 0, ait_val_t*), val);
  261: 	free(val);
  262: 	pthread_mutex_unlock(&srv->srv_blob.mtx);
  263: 	return 0;
  264: }
  265: 
  266: int
  267: rpcBLOBServerState(rpc_func_t *call, int ic, array_t *iv)
  268: {
  269: 	rpc_srv_t *srv;
  270: 
  271: 	RPC_CALLBACK_CHK_ARGS(call, ic);
  272: 	if (!call->func_parent)
  273: 		return -1;
  274: 	else
  275: 		srv = call->func_parent;
  276: 
  277: 	if (AIT_TYPE(io_array(iv, 0, ait_val_t*)) != i32)
  278: 		return -1;
  279: 
  280: 	pthread_mutex_lock(&srv->srv_mtx);
  281: 	srv->srv_blob.state = AIT_GET_I32(io_array(iv, 0, ait_val_t*));
  282: 	pthread_mutex_unlock(&srv->srv_mtx);
  283: 	return 0;
  284: }
  285: 
  286: int
  287: rpcBLOBServerClients(rpc_func_t *call, int ic, array_t *iv)
  288: {
  289: 	rpc_srv_t *srv;
  290: 	array_t *vals;
  291: 	rpc_cli_t *cli;
  292: 	register int i;
  293: 	int len;
  294: 	const char *str = NULL;
  295: 	char *val, wrk[INET6_ADDRSTRLEN];
  296: 
  297: 	RPC_CALLBACK_CHECK_INPUT(call);
  298: 	if (!call->func_parent)
  299: 		return -1;
  300: 	else
  301: 		srv = call->func_parent;
  302: 
  303: 	rpc_srv_getVars(call, &vals);
  304: 
  305: 	if (srv->srv_blob.state == kill) {
  306: 		AIT_SET_STR(io_array(vals, 0, ait_val_t*), "BLOB Server is killed");
  307: 		return 1;
  308: 	}
  309: 	if (srv->srv_blob.state == disable) {
  310: 		AIT_SET_STR(io_array(vals, 0, ait_val_t*), "BLOB Server is disabled");
  311: 		return 1;
  312: 	}
  313: 
  314: 	len = srv->srv_numcli * STRSIZ;
  315: 	if (!(val = malloc(len))) {
  316: 		LOGERR;
  317: 		return -1;
  318: 	} else
  319: 		memset(val, 0, len);
  320: 
  321: 	for (i = 0, cli = srv->srv_blob.clients; i < srv->srv_numcli; i++, cli++) {
  322: 		if (!cli->cli_sa.sa.sa_family)
  323: 			continue;
  324: 
  325: 		switch (cli->cli_sa.sa.sa_family) {
  326: 			case AF_INET:
  327: 				str = inet_ntop(cli->cli_sa.sa.sa_family, &cli->cli_sa.sin.sin_addr, wrk, sizeof wrk);
  328: 				break;
  329: 			case AF_INET6:
  330: 				str = inet_ntop(cli->cli_sa.sa.sa_family, &cli->cli_sa.sin6.sin6_addr, wrk, sizeof wrk);
  331: 				break;
  332: 			case AF_LOCAL:
  333: 				str = cli->cli_sa.sun.sun_path;
  334: 				break;
  335: 		}
  336: 		if (str)
  337: 			strlcat(val, (char*) str, len);
  338: 		else
  339: 			strlcat(val, "0.0.0.0", len);
  340: 		strlcat(val, " ", len);
  341: 	}
  342: 
  343: 	AIT_SET_STR(io_array(vals, 0, ait_val_t*), val);
  344: 	free(val);
  345: 	return 0;
  346: }

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