--- libaitrpc/src/builtin.c 2010/06/18 13:36:01 1.1.1.1.2.1 +++ libaitrpc/src/builtin.c 2011/08/19 09:20:46 1.2.4.3 @@ -3,72 +3,120 @@ * by Michael Pounov * * $Author: misho $ -* $Id: builtin.c,v 1.1.1.1.2.1 2010/06/18 13:36:01 misho Exp $ +* $Id: builtin.c,v 1.2.4.3 2011/08/19 09:20:46 misho Exp $ * -*************************************************************************/ +************************************************************************** +The ELWIX and AITNET software is distributed under the following +terms: + +All of the documentation and software included in the ELWIX and AITNET +Releases is copyrighted by ELWIX - Sofia/Bulgaria + +Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 + by Michael Pounov . All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. +3. All advertising materials mentioning features or use of this software + must display the following acknowledgement: +This product includes software developed by Michael Pounov +ELWIX - Embedded LightWeight unIX and its contributors. +4. Neither the name of AITNET nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +SUCH DAMAGE. +*/ #include "global.h" /* builtin RPC server functions */ int -rpcServerClients(rpc_srv_t * __restrict srv, rpc_func_t *call, int ic, rpc_val_t *iv) +rpcServerClients(rpc_func_t *call, int ic, rpc_val_t *iv) { - rpc_val_t *v, *vals = NULL; + rpc_srv_t *srv; + rpc_val_t *v, *vals; rpc_cli_t *cli; register int i; - const char *str; + const char *str = NULL; char wrk[INET6_ADDRSTRLEN]; struct sockaddr_in *s; struct sockaddr_in6 *s6; + struct sockaddr_un *su; - RPC_CALLBACK_CHECK_INPUT(srv, call); - - if (rpc_srv_declValsCall(call, srv->srv_numcli) == -1) + RPC_CALLBACK_CHECK_INPUT(call); + if (!call->func_parent) return -1; else - rpc_srv_delValsCall(call); - if (rpc_srv_getValsCall(call, &vals) == -1) + srv = call->func_parent; + + if (!(vals = rpc_srv_returnVars(call, srv->srv_numcli))) return -1; - for (i = 0, cli = srv->srv_clients, v = vals; i < srv->srv_numcli; cli++) { - if (!cli->cli_sa.sa_family) + for (i = 0, cli = srv->srv_clients, v = vals; i < srv->srv_numcli; i++, cli++) { + if (!cli->cli_sa.sa_family) { + RPC_SET_STR(v++, ""); continue; + } - if (AF_INET == cli->cli_sa.sa_family) { - s = (struct sockaddr_in*) &cli->cli_sa; - str = inet_ntop(cli->cli_sa.sa_family, &s->sin_addr, wrk, sizeof wrk); - } else { - s6 = (struct sockaddr_in6*) &cli->cli_sa; - str = inet_ntop(cli->cli_sa.sa_family, &s6->sin6_addr, wrk, sizeof wrk); + switch (cli->cli_sa.sa_family) { + case AF_INET: + s = (struct sockaddr_in*) &cli->cli_sa; + str = inet_ntop(cli->cli_sa.sa_family, &s->sin_addr, wrk, sizeof wrk); + break; + case AF_INET6: + s6 = (struct sockaddr_in6*) &cli->cli_sa; + str = inet_ntop(cli->cli_sa.sa_family, &s6->sin6_addr, wrk, sizeof wrk); + break; + case AF_LOCAL: + su = (struct sockaddr_un*) &cli->cli_sa; + str = su->sun_path; + break; } - if (str) { + if (str) RPC_SET_STR(v++, (char*) str); - i++; - } + else + RPC_SET_STR(v++, "0.0.0.0"); } return 0; } int -rpcServerCalls(rpc_srv_t * __restrict srv, rpc_func_t *call, int ic, rpc_val_t *iv) +rpcServerCalls(rpc_func_t *call, int ic, rpc_val_t *iv) { - rpc_val_t *v, *vals = NULL; + rpc_srv_t *srv; + rpc_val_t *v, *vals; rpc_func_t *f; register int i; char str[MAXPATHLEN]; - RPC_CALLBACK_CHECK_INPUT(srv, call); + RPC_CALLBACK_CHECK_INPUT(call); + if (!call->func_parent) + return -1; + else + srv = call->func_parent; for (i = 0, f = srv->srv_funcs; f; i++, f = f->func_next); - - if (rpc_srv_declValsCall(call, i) == -1) + if (!(vals = rpc_srv_returnVars(call, i))) return -1; - else - rpc_srv_delValsCall(call); - if (rpc_srv_getValsCall(call, &vals) == -1) - return -1; for (f = srv->srv_funcs, v = vals; f; f = f->func_next) { if (*f->func_name) { @@ -82,17 +130,18 @@ rpcServerCalls(rpc_srv_t * __restrict srv, rpc_func_t } int -rpcServerSessions(rpc_srv_t * __restrict srv, rpc_func_t *call, int ic, rpc_val_t *iv) +rpcServerSessions(rpc_func_t *call, int ic, rpc_val_t *iv) { - rpc_val_t *vals = NULL; + rpc_srv_t *srv; + rpc_val_t *vals; - RPC_CALLBACK_CHECK_INPUT(srv, call); - - if (rpc_srv_declValsCall(call, 4) == -1) + RPC_CALLBACK_CHECK_INPUT(call); + if (!call->func_parent) return -1; else - rpc_srv_delValsCall(call); - if (rpc_srv_getValsCall(call, &vals) == -1) + srv = call->func_parent; + + if (!(vals = rpc_srv_returnVars(call, 4))) return -1; RPC_SET_U8(&vals[0], srv->srv_session.sess_version); @@ -104,13 +153,136 @@ rpcServerSessions(rpc_srv_t * __restrict srv, rpc_func } int -rpcServerShutdown(rpc_srv_t * __restrict srv, rpc_func_t *call, int ic, rpc_val_t *iv) +rpcServerShutdown(rpc_func_t *call, int ic, rpc_val_t *iv) { - RPC_CALLBACK_CHECK_INPUT(srv, call); + rpc_srv_t *srv; - pthread_mutex_lock(&rpc_mtx); - rpc_Kill = 1; - pthread_mutex_unlock(&rpc_mtx); + RPC_CALLBACK_CHECK_INPUT(call); + if (!call->func_parent) + return -1; + else + srv = call->func_parent; + + pthread_mutex_lock(&srv->srv_mtx); + srv->srv_kill = kill; + pthread_mutex_unlock(&srv->srv_mtx); + + return 0; +} + +// ---------------------------------------------------- + +int +rpcBLOBServerShutdown(rpc_func_t *call, int ic, rpc_val_t *iv) +{ + rpc_srv_t *srv; + + RPC_CALLBACK_CHECK_INPUT(call); + if (!call->func_parent) + return -1; + else + srv = call->func_parent; + + pthread_mutex_lock(&srv->srv_blob.mtx); + srv->srv_blob.state = kill; + pthread_mutex_unlock(&srv->srv_blob.mtx); + + return 0; +} + +int +rpcBLOBServerVars(rpc_func_t *call, int ic, rpc_val_t *iv) +{ + rpc_srv_t *srv; + rpc_val_t *v, *vals; + rpc_blob_t *b; + register int i; + + RPC_CALLBACK_CHECK_INPUT(call); + if (!call->func_parent) + return -1; + else + srv = call->func_parent; + + pthread_mutex_lock(&srv->srv_blob.mtx); + for (i = 0, b = srv->srv_blob.blobs; b; i++, b = b->blob_next); + if (!(vals = rpc_srv_returnVars(call, i))) { + pthread_mutex_unlock(&srv->srv_blob.mtx); + return 0; + } + + for (b = srv->srv_blob.blobs, v = vals; b; b = b->blob_next) + RPC_SET_U32(v++, b->blob_var); + pthread_mutex_unlock(&srv->srv_blob.mtx); + + return 0; +} + +int +rpcBLOBServerState(rpc_func_t *call, int ic, rpc_val_t *iv) +{ + rpc_srv_t *srv; + + RPC_CALLBACK_CHK_ARGS(call, ic); + if (!call->func_parent) + return -1; + else + srv = call->func_parent; + + if (iv[0].val_type != i32) + return -1; + + srv->srv_blob.state = RPC_GET_I32(&iv[0]); + return 0; +} + +int +rpcBLOBServerClients(rpc_func_t *call, int ic, rpc_val_t *iv) +{ + rpc_srv_t *srv; + rpc_val_t *v, *vals; + rpc_cli_t *cli; + register int i; + const char *str = NULL; + char wrk[INET6_ADDRSTRLEN]; + struct sockaddr_in *s; + struct sockaddr_in6 *s6; + struct sockaddr_un *su; + + RPC_CALLBACK_CHECK_INPUT(call); + if (!call->func_parent) + return -1; + else + srv = call->func_parent; + + if (!(vals = rpc_srv_returnVars(call, srv->srv_numcli))) + return -1; + + for (i = 0, cli = srv->srv_blob.clients, v = vals; i < srv->srv_numcli; i++, cli++) { + if (!cli->cli_sa.sa_family) { + RPC_SET_STR(v++, ""); + continue; + } + + switch (cli->cli_sa.sa_family) { + case AF_INET: + s = (struct sockaddr_in*) &cli->cli_sa; + str = inet_ntop(cli->cli_sa.sa_family, &s->sin_addr, wrk, sizeof wrk); + break; + case AF_INET6: + s6 = (struct sockaddr_in6*) &cli->cli_sa; + str = inet_ntop(cli->cli_sa.sa_family, &s6->sin6_addr, wrk, sizeof wrk); + break; + case AF_LOCAL: + su = (struct sockaddr_un*) &cli->cli_sa; + str = su->sun_path; + break; + } + if (str) + RPC_SET_STR(v++, (char*) str); + else + RPC_SET_STR(v++, "0.0.0.0"); + } return 0; }