--- libaitrpc/src/builtin.c 2011/08/19 14:13:31 1.2.4.4 +++ libaitrpc/src/builtin.c 2012/05/11 08:49:43 1.6.4.2 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: builtin.c,v 1.2.4.4 2011/08/19 14:13:31 misho Exp $ +* $Id: builtin.c,v 1.6.4.2 2012/05/11 08:49:43 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -12,7 +12,7 @@ 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 +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 @@ -49,17 +49,15 @@ SUCH DAMAGE. /* builtin RPC server functions */ int -rpcServerClients(rpc_func_t *call, int ic, rpc_val_t *iv) +rpcServerClients(rpc_func_t *call, int ic, array_t *iv) { rpc_srv_t *srv; - rpc_val_t *v, *vals; + array_t *vals; rpc_cli_t *cli; register int i; + int len; const char *str = NULL; - char wrk[INET6_ADDRSTRLEN]; - struct sockaddr_in *s; - struct sockaddr_in6 *s6; - struct sockaddr_un *su; + char *val, wrk[INET6_ADDRSTRLEN]; RPC_CALLBACK_CHECK_INPUT(call); if (!call->func_parent) @@ -67,46 +65,51 @@ rpcServerClients(rpc_func_t *call, int ic, rpc_val_t * else srv = call->func_parent; - if (!(vals = rpc_srv_returnVars(call, srv->srv_numcli))) + rpc_srv_getVars(call, &vals); + + len = srv->srv_numcli * 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, cli = srv->srv_clients; i < srv->srv_numcli; i++, cli++) { + if (!cli->cli_sa.sa.sa_family) continue; - } - switch (cli->cli_sa.sa_family) { + switch (cli->cli_sa.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); + str = inet_ntop(cli->cli_sa.sa.sa_family, &cli->cli_sa.sin.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); + str = inet_ntop(cli->cli_sa.sa.sa_family, &cli->cli_sa.sin6.sin6_addr, wrk, sizeof wrk); break; case AF_LOCAL: - su = (struct sockaddr_un*) &cli->cli_sa; - str = su->sun_path; + str = cli->cli_sa.sun.sun_path; break; } 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_SET_STR(io_array(vals, 0, ait_val_t*), val); + free(val); return 0; } int -rpcServerCalls(rpc_func_t *call, int ic, rpc_val_t *iv) +rpcServerCalls(rpc_func_t *call, int ic, array_t *iv) { rpc_srv_t *srv; - rpc_val_t *v, *vals; + array_t *vals; rpc_func_t *f; register int i; - char str[MAXPATHLEN]; + int len; + char *val, str[MAXPATHLEN]; RPC_CALLBACK_CHECK_INPUT(call); if (!call->func_parent) @@ -115,25 +118,40 @@ rpcServerCalls(rpc_func_t *call, int ic, rpc_val_t *iv srv = call->func_parent; for (i = 0, f = srv->srv_funcs; f; i++, f = f->func_next); - if (!(vals = rpc_srv_returnVars(call, i))) + len = i * STRSIZ; + + rpc_srv_getVars(call, &vals); + + 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) { + for (f = srv->srv_funcs, i = 0; f; f = f->func_next) { + if (*AIT_GET_STR(&f->func_name)) { memset(str, 0, sizeof str); - snprintf(str, sizeof str, "/%s/%s()", f->func_file, f->func_name); - RPC_SET_STR(v++, str); + if (AIT_ISEMPTY(&f->func_file)) + snprintf(str, sizeof str, "/*/%s(%d); ", + AIT_GET_STR(&f->func_name), io_arraySize(f->func_vars)); + else + snprintf(str, sizeof str, "/%s/%s(%d); ", AIT_GET_STR(&f->func_file), + AIT_GET_STR(&f->func_name), io_arraySize(f->func_vars)); + strlcat(val, str, len); } } + AIT_SET_STR(io_array(vals, 0, ait_val_t*), val); + free(val); return 0; } int -rpcServerSessions(rpc_func_t *call, int ic, rpc_val_t *iv) +rpcServerSessions(rpc_func_t *call, int ic, array_t *iv) { rpc_srv_t *srv; - rpc_val_t *vals; + array_t *vals; + ait_val_t *v; RPC_CALLBACK_CHECK_INPUT(call); if (!call->func_parent) @@ -141,19 +159,22 @@ rpcServerSessions(rpc_func_t *call, int ic, rpc_val_t else srv = call->func_parent; - if (!(vals = rpc_srv_returnVars(call, 4))) - return -1; + rpc_srv_getVars(call, &vals); - 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); + v = io_array(vals, 0, ait_val_t*); + AIT_SET_U8(v, srv->srv_session.sess_version); + v = io_array(vals, 1, ait_val_t*); + AIT_SET_U32(v, srv->srv_session.sess_program); + v = io_array(vals, 2, ait_val_t*); + AIT_SET_U32(v, srv->srv_session.sess_process); + v = io_array(vals, 3, ait_val_t*); + AIT_SET_I32(v, srv->srv_numcli); return 0; } int -rpcServerShutdown(rpc_func_t *call, int ic, rpc_val_t *iv) +rpcServerShutdown(rpc_func_t *call, int ic, array_t *iv) { rpc_srv_t *srv; @@ -163,9 +184,7 @@ rpcServerShutdown(rpc_func_t *call, int ic, rpc_val_t else srv = call->func_parent; - pthread_mutex_lock(&srv->srv_mtx); srv->srv_kill = kill; - pthread_mutex_unlock(&srv->srv_mtx); return 0; } @@ -173,7 +192,7 @@ rpcServerShutdown(rpc_func_t *call, int ic, rpc_val_t // ---------------------------------------------------- int -rpcBLOBServerShutdown(rpc_func_t *call, int ic, rpc_val_t *iv) +rpcBLOBServerShutdown(rpc_func_t *call, int ic, array_t *iv) { rpc_srv_t *srv; @@ -183,20 +202,20 @@ rpcBLOBServerShutdown(rpc_func_t *call, int ic, rpc_va 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) +rpcBLOBServerVars(rpc_func_t *call, int ic, array_t *iv) { rpc_srv_t *srv; - rpc_val_t *v, *vals; + array_t *vals; rpc_blob_t *b; register int i; + char *val, str[64]; + int len; RPC_CALLBACK_CHECK_INPUT(call); if (!call->func_parent) @@ -204,22 +223,44 @@ rpcBLOBServerVars(rpc_func_t *call, int ic, rpc_val_t else srv = call->func_parent; - pthread_mutex_lock(&srv->srv_blob.mtx); + rpc_srv_getVars(call, &vals); + + if (srv->srv_blob.state == kill) { + AIT_SET_STR(io_array(vals, 0, ait_val_t*), "BLOB Server is killed"); + return 1; + } + if (srv->srv_blob.state == disable) { + AIT_SET_STR(io_array(vals, 0, ait_val_t*), "BLOB Server is disabled"); + return 1; + } + 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); + len = i * sizeof str; + + if (!len) { + AIT_SET_STR(io_array(vals, 0, ait_val_t*), ""); 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); + if (!(val = malloc(len))) { + LOGERR; + return -1; + } else + memset(val, 0, len); + for (b = srv->srv_blob.blobs, i = 0; b; i++, b = b->blob_next) { + 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_array(vals, 0, ait_val_t*), val); + free(val); return 0; } int -rpcBLOBServerState(rpc_func_t *call, int ic, rpc_val_t *iv) +rpcBLOBServerState(rpc_func_t *call, int ic, array_t *iv) { rpc_srv_t *srv; @@ -229,25 +270,23 @@ rpcBLOBServerState(rpc_func_t *call, int ic, rpc_val_t else srv = call->func_parent; - if (iv[0].val_type != i32) + if (AIT_TYPE(io_array(iv, 0, ait_val_t*)) != i32) return -1; - srv->srv_blob.state = RPC_GET_I32(&iv[0]); + srv->srv_blob.state = AIT_GET_I32(io_array(iv, 0, ait_val_t*)); return 0; } int -rpcBLOBServerClients(rpc_func_t *call, int ic, rpc_val_t *iv) +rpcBLOBServerClients(rpc_func_t *call, int ic, array_t *iv) { rpc_srv_t *srv; - rpc_val_t *v, *vals; + array_t *vals; rpc_cli_t *cli; register int i; + int len; const char *str = NULL; - char wrk[INET6_ADDRSTRLEN]; - struct sockaddr_in *s; - struct sockaddr_in6 *s6; - struct sockaddr_un *su; + char *val, wrk[INET6_ADDRSTRLEN]; RPC_CALLBACK_CHECK_INPUT(call); if (!call->func_parent) @@ -255,34 +294,47 @@ rpcBLOBServerClients(rpc_func_t *call, int ic, rpc_val else srv = call->func_parent; - if (!(vals = rpc_srv_returnVars(call, srv->srv_numcli))) + rpc_srv_getVars(call, &vals); + + if (srv->srv_blob.state == kill) { + AIT_SET_STR(io_array(vals, 0, ait_val_t*), "BLOB Server is killed"); + return 1; + } + if (srv->srv_blob.state == disable) { + AIT_SET_STR(io_array(vals, 0, ait_val_t*), "BLOB Server is disabled"); + return 1; + } + + len = srv->srv_numcli * STRSIZ; + if (!(val = malloc(len))) { + LOGERR; return -1; + } else + memset(val, 0, len); - 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++, ""); + for (i = 0, cli = srv->srv_blob.clients; i < srv->srv_numcli; i++, cli++) { + if (!cli->cli_sa.sa.sa_family) continue; - } - switch (cli->cli_sa.sa_family) { + switch (cli->cli_sa.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); + str = inet_ntop(cli->cli_sa.sa.sa_family, &cli->cli_sa.sin.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); + str = inet_ntop(cli->cli_sa.sa.sa_family, &cli->cli_sa.sin6.sin6_addr, wrk, sizeof wrk); break; case AF_LOCAL: - su = (struct sockaddr_un*) &cli->cli_sa; - str = su->sun_path; + str = cli->cli_sa.sun.sun_path; break; } 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_SET_STR(io_array(vals, 0, ait_val_t*), val); + free(val); return 0; }