--- libaitrpc/src/builtin.c 2010/07/08 09:18:51 1.1.1.1.2.6 +++ libaitrpc/src/builtin.c 2012/05/18 15:24:33 1.7.2.9 @@ -3,234 +3,330 @@ * by Michael Pounov * * $Author: misho $ -* $Id: builtin.c,v 1.1.1.1.2.6 2010/07/08 09:18:51 misho Exp $ +* $Id: builtin.c,v 1.7.2.9 2012/05/18 15:24:33 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, 2012 + 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_func_t *call, int ic, rpc_val_t *iv) +static int +rpcServerClients(rpc_cli_t *cli, struct tagRPCCall *rpc, array_t *iv) { rpc_srv_t *srv; - rpc_val_t *v, *vals; - rpc_cli_t *cli; + rpc_cli_t *c; register int i; - const char *str; - char wrk[INET6_ADDRSTRLEN]; - struct sockaddr_in *s; - struct sockaddr_in6 *s6; + int len; + const char *str = NULL; + char *val; + ait_val_t v; - RPC_CALLBACK_CHECK_INPUT(call); - if (!call->func_parent) - return -1; - else - srv = call->func_parent; + RPC_CALLBACK_CHECK_INPUT(cli); + srv = RPC_SRV_SERVER(cli); - if (!(vals = rpc_srv_retValsCall(call, srv->srv_numcli))) + len = io_arraySize(srv->srv_clients) * STRSIZ; + if (!(val = malloc(len))) { + LOGERR; return -1; + } else + memset(val, 0, len); - 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++, ""); + for (i = 0; i < io_arraySize(srv->srv_clients); i++) { + c = io_array(srv->srv_clients, i, rpc_cli_t*); + if (!c) 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); - } + str = io_n2addr(&c->cli_sa, &v); if (str) - RPC_SET_STR(v++, (char*) str); + strlcat(val, (char*) str, len); else - RPC_SET_STR(v++, "0.0.0.0"); + strlcat(val, "0.0.0.0", len); + strlcat(val, " ", len); + AIT_FREE_VAL(&v); } + /* return values */ + AIT_SET_STR(io_getVars(&RPC_RETVARS(cli), 0), val); + free(val); return 0; } -int -rpcServerCalls(rpc_func_t *call, int ic, rpc_val_t *iv) +static int +rpcServerCalls(rpc_cli_t *cli, struct tagRPCCall *rpc, array_t *iv) { rpc_srv_t *srv; - rpc_val_t *v, *vals; rpc_func_t *f; - register int i; - char str[MAXPATHLEN]; + register int i = 0; + int len; + char *val, str[MAXPATHLEN]; - RPC_CALLBACK_CHECK_INPUT(call); - if (!call->func_parent) - return -1; - else - srv = call->func_parent; + RPC_CALLBACK_CHECK_INPUT(cli); + srv = RPC_SRV_SERVER(cli); - for (i = 0, f = srv->srv_funcs; f; i++, f = f->func_next); - if (!(vals = rpc_srv_retValsCall(call, i))) + TAILQ_FOREACH(f, &srv->srv_funcs, func_node) + i++; + len = i * STRSIZ; + + if (!(val = malloc(len))) { + LOGERR; return -1; + } else + memset(val, 0, len); - for (f = srv->srv_funcs, v = vals; f; f = f->func_next) { - if (*f->func_name) { - memset(str, 0, MAXPATHLEN); - snprintf(str, MAXPATHLEN, "/%s/%s()", f->func_file, f->func_name); - RPC_SET_STR(v++, str); + TAILQ_FOREACH(f, &srv->srv_funcs, func_node) + if (AIT_ADDR(&f->func_name)) { + memset(str, 0, sizeof str); + snprintf(str, sizeof str, "/%hu/0x%p; ", AIT_KEY(&f->func_name), + AIT_ADDR(&f->func_name)); + strlcat(val, str, len); } - } + /* return variables */ + AIT_SET_STR(io_getVars(&RPC_RETVARS(cli), 0), val); + free(val); return 0; } -int -rpcServerSessions(rpc_func_t *call, int ic, rpc_val_t *iv) +static int +rpcServerSessions(rpc_cli_t *cli, struct tagRPCCall *rpc, array_t *iv) { rpc_srv_t *srv; - rpc_val_t *vals; - RPC_CALLBACK_CHECK_INPUT(call); - if (!call->func_parent) - return -1; - else - srv = call->func_parent; + RPC_CALLBACK_CHECK_INPUT(cli); + srv = RPC_SRV_SERVER(cli); - if (!(vals = rpc_srv_retValsCall(call, 4))) - return -1; + AIT_SET_I32(io_getVars(&RPC_RETVARS(cli), 3), io_arraySize(srv->srv_clients)); + AIT_SET_U8(io_getVars(&RPC_RETVARS(cli), 0), srv->srv_session.sess_version); + AIT_SET_U32(io_getVars(&RPC_RETVARS(cli), 1), srv->srv_session.sess_program); + AIT_SET_U8(io_getVars(&RPC_RETVARS(cli), 2), srv->srv_session.sess_process); - RPC_SET_U8(&vals[0], srv->srv_session.sess_version); - RPC_SET_U32(&vals[1], srv->srv_session.sess_program); - RPC_SET_U32(&vals[2], srv->srv_session.sess_process); - RPC_SET_I32(&vals[3], srv->srv_numcli); - return 0; } -int -rpcServerShutdown(rpc_func_t *call, int ic, rpc_val_t *iv) +static int +rpcServerShutdown(rpc_cli_t *cli, struct tagRPCCall *rpc, array_t *iv) { rpc_srv_t *srv; - RPC_CALLBACK_CHECK_INPUT(call); - if (!call->func_parent) - return -1; - else - srv = call->func_parent; + RPC_CALLBACK_CHECK_INPUT(cli); + srv = RPC_SRV_SERVER(cli); - pthread_mutex_lock(&srv->srv_mtx); - rpc_Kill = 1; - pthread_mutex_unlock(&srv->srv_mtx); + srv->srv_kill = 1; return 0; } -// ---------------------------------------------------- +static int +rpcServerPing(rpc_cli_t *cli, struct tagRPCCall *rpc, array_t *iv) +{ + if (rpc->call_req.flags == RPC_REPLY) + AIT_SET_U16(io_getVars(&RPC_RETVARS(cli), 0), ntohs(rpc->call_seq)); + return 0; +} -int -rpcBLOBServerShutdown(rpc_func_t *call, int ic, rpc_val_t *iv) +/* ---------------------------------------------------- */ + +static int +rpcBLOBServerShutdown(rpc_cli_t *cli, struct tagRPCCall *rpc, array_t *iv) { rpc_srv_t *srv; - RPC_CALLBACK_CHECK_INPUT(call); - if (!call->func_parent) - return -1; - else - srv = call->func_parent; + RPC_CALLBACK_CHECK_INPUT(cli); + srv = RPC_SRV_SERVER(cli); - pthread_mutex_lock(&srv->srv_blob.mtx); - blob_Kill = 1; - pthread_mutex_unlock(&srv->srv_blob.mtx); + srv->srv_blob.kill = 1; return 0; } -int -rpcBLOBServerVars(rpc_func_t *call, int ic, rpc_val_t *iv) +static int +rpcBLOBServerVars(rpc_cli_t *cli, struct tagRPCCall *rpc, array_t *iv) { rpc_srv_t *srv; - rpc_val_t *v, *vals; rpc_blob_t *b; - register int i; + register int i = 0; + char *val, str[64]; + int len; - RPC_CALLBACK_CHECK_INPUT(call); - if (!call->func_parent) - return -1; - else - srv = call->func_parent; + RPC_CALLBACK_CHECK_INPUT(cli); + srv = RPC_SRV_SERVER(cli); - 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_retValsCall(call, i))) { - pthread_mutex_unlock(&srv->srv_blob.mtx); - return -1; + if (srv->srv_blob.kill) { + AIT_SET_STR(io_getVars(&RPC_RETVARS(cli), 0), "BLOB Server is killed"); + return 1; } - 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); + TAILQ_FOREACH(b, &srv->srv_blob.blobs, blob_node) + i++; + len = i * sizeof str; + if (!len) { + AIT_SET_STR(io_getVars(&RPC_RETVARS(cli), 0), ""); + return 0; + } + + if (!(val = malloc(len))) { + LOGERR; + return -1; + } else + memset(val, 0, len); + + TAILQ_FOREACH(b, &srv->srv_blob.blobs, blob_node) { + memset(str, 0, sizeof str); + snprintf(str, sizeof str, "0x%0X(%lu)=%p ", b->blob_var, (u_long) b->blob_len, b->blob_data); + strlcat(val, str, len); + } + + AIT_SET_STR(io_getVars(&RPC_RETVARS(cli), 0), val); + free(val); return 0; } -int -rpcBLOBServerState(rpc_func_t *call, int ic, rpc_val_t *iv) +static int +rpcBLOBServerClients(rpc_cli_t *cli, struct tagRPCCall *rpc, array_t *iv) { rpc_srv_t *srv; + rpc_cli_t *c; + register int i; + int len; + const char *str = NULL; + char *val; + ait_val_t v; - RPC_CALLBACK_CHECK_INPUT(call); - RPC_CALLBACK_CHK_RETARGS(call, ic); - if (!call->func_parent) + RPC_CALLBACK_CHECK_INPUT(cli); + srv = RPC_SRV_SERVER(cli); + + if (srv->srv_blob.kill) { + AIT_SET_STR(io_getVars(&RPC_RETVARS(cli), 0), "BLOB Server is killed"); + return 1; + } + + len = io_arraySize(srv->srv_blob.clients) * STRSIZ; + if (!(val = malloc(len))) { + LOGERR; return -1; - else - srv = call->func_parent; + } else + memset(val, 0, len); - if (iv[0].val_type != i32) + for (i = 0; i < io_arraySize(srv->srv_clients); i++) { + c = io_array(srv->srv_blob.clients, i, rpc_cli_t*); + if (!c) + continue; + + str = io_n2addr(&c->cli_sa, &v); + if (str) + strlcat(val, (char*) str, len); + else + strlcat(val, "0.0.0.0", len); + strlcat(val, " ", len); + AIT_FREE_VAL(&v); + } + + AIT_SET_STR(io_getVars(&RPC_RETVARS(cli), 0), val); + free(val); + return 0; +} + +/* ----------------------------------------------------------------- */ + +/* + * rpc_register_srvPing() - Register ping service function + * + * @srv = RPC server instance + * return: -1 error or 0 ok + */ +inline int +rpc_register_srvPing(rpc_srv_t * __restrict srv) +{ + if (!srv) return -1; - srv->srv_blob.state = RPC_GET_I32(&iv[0]); + if (rpc_srv_registerCall(srv, CALL_SRVPING, rpcServerPing) < 1) + return -1; + return 0; } +/* + * rpc_register_srvServices() - Register internal service functions + * + * @srv = RPC server instance + * return: -1 error or 0 ok + */ int -rpcBLOBServerClients(rpc_func_t *call, int ic, rpc_val_t *iv) +rpc_register_srvServices(rpc_srv_t * __restrict srv) { - rpc_srv_t *srv; - rpc_val_t *v, *vals; - rpc_cli_t *cli; - register int i; - const char *str; - char wrk[INET6_ADDRSTRLEN]; - struct sockaddr_in *s; - struct sockaddr_in6 *s6; - - RPC_CALLBACK_CHECK_INPUT(call); - if (!call->func_parent) + if (!srv) return -1; - else - srv = call->func_parent; - if (!(vals = rpc_srv_retValsCall(call, srv->srv_numcli))) + if (rpc_srv_registerCall(srv, CALL_SRVSHUTDOWN, rpcServerShutdown) < 1) return -1; + if (rpc_srv_registerCall(srv, CALL_SRVCLIENTS, rpcServerClients) < 1) + return -1; + if (rpc_srv_registerCall(srv, CALL_SRVSESSIONS, rpcServerSessions) < 1) + return -1; + if (rpc_srv_registerCall(srv, CALL_SRVCALLS, rpcServerCalls) < 1) + 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; - } + return 0; +} - 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); - } - if (str) - RPC_SET_STR(v++, (char*) str); - else - RPC_SET_STR(v++, "0.0.0.0"); - } +/* + * rpc_register_blobServices() - Register internal service functions + * + * @srv = RPC server instance + * return: -1 error or 0 ok + */ +int +rpc_register_blobServices(rpc_srv_t * __restrict srv) +{ + if (!srv) + return -1; + + if (rpc_srv_registerCall(srv, CALL_BLOBSHUTDOWN, rpcBLOBServerShutdown) < 1) + return -1; + if (rpc_srv_registerCall(srv, CALL_BLOBCLIENTS, rpcBLOBServerClients) < 1) + return -1; + if (rpc_srv_registerCall(srv, CALL_BLOBVARS, rpcBLOBServerVars) < 1) + return -1; return 0; }