--- libaitrpc/src/builtin.c 2011/11/03 12:52:36 1.4.2.2 +++ libaitrpc/src/builtin.c 2012/05/16 16:36:10 1.7.2.5 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: builtin.c,v 1.4.2.2 2011/11/03 12:52:36 misho Exp $ +* $Id: builtin.c,v 1.7.2.5 2012/05/16 16:36:10 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 @@ -48,16 +48,16 @@ SUCH DAMAGE. /* builtin RPC server functions */ -int +static int rpcServerClients(rpc_func_t *call, int ic, array_t *iv) { rpc_srv_t *srv; - array_t *vals; rpc_cli_t *cli; register int i; int len; const char *str = NULL; - char *val, wrk[INET6_ADDRSTRLEN]; + char *val; + ait_val_t v; RPC_CALLBACK_CHECK_INPUT(call); if (!call->func_parent) @@ -65,49 +65,38 @@ rpcServerClients(rpc_func_t *call, int ic, array_t *iv else srv = call->func_parent; - rpc_srv_getVars(call, &vals); - - len = srv->srv_numcli * STRSIZ; + 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; i < srv->srv_numcli; i++, cli++) { - if (!cli->cli_sa.sa.sa_family) + for (i = 0; i < io_arraySize(srv->srv_clients); i++) { + cli = io_array(srv->srv_clients, i, rpc_cli_t*); + if (!cli) continue; - switch (cli->cli_sa.sa.sa_family) { - case AF_INET: - str = inet_ntop(cli->cli_sa.sa.sa_family, &cli->cli_sa.sin.sin_addr, wrk, sizeof wrk); - break; - case AF_INET6: - str = inet_ntop(cli->cli_sa.sa.sa_family, &cli->cli_sa.sin6.sin6_addr, wrk, sizeof wrk); - break; - case AF_LOCAL: - str = cli->cli_sa.sun.sun_path; - break; - } + str = io_n2addr(&cli->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_array(vals, 0, ait_val_t*), val); + AIT_SET_STR(io_array(call->func_vars, 0, ait_val_t*), val); free(val); return 0; } -int +static int rpcServerCalls(rpc_func_t *call, int ic, array_t *iv) { rpc_srv_t *srv; - array_t *vals; rpc_func_t *f; - register int i; + register int i = 0; int len; char *val, str[MAXPATHLEN]; @@ -117,37 +106,33 @@ rpcServerCalls(rpc_func_t *call, int ic, array_t *iv) else srv = call->func_parent; - for (i = 0, f = srv->srv_funcs; f; i++, f = f->func_next); + TAILQ_FOREACH(f, &srv->srv_funcs, func_node) + 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, i = 0; f; f = f->func_next) { - if (*f->func_name) { + TAILQ_FOREACH(f, &srv->srv_funcs, func_node) + if (AIT_ADDR(&f->func_name)) { memset(str, 0, sizeof str); - snprintf(str, sizeof str, "/%s/%s(%d); ", - *f->func_file ? f->func_file : "*", - f->func_name, io_arraySize(f->func_vars)); + snprintf(str, sizeof str, "/%hu/0x%p(%d); ", AIT_KEY(&f->func_name), + AIT_ADDR(&f->func_name), io_arraySize(f->func_vars)); strlcat(val, str, len); } - } - AIT_SET_STR(io_array(vals, 0, ait_val_t*), val); + AIT_SET_STR(io_array(call->func_vars, 0, ait_val_t*), val); free(val); return 0; } -int +static int rpcServerSessions(rpc_func_t *call, int ic, array_t *iv) { rpc_srv_t *srv; - array_t *vals; ait_val_t *v; RPC_CALLBACK_CHECK_INPUT(call); @@ -156,21 +141,19 @@ rpcServerSessions(rpc_func_t *call, int ic, array_t *i else srv = call->func_parent; - rpc_srv_getVars(call, &vals); - - v = io_array(vals, 0, ait_val_t*); + v = io_array(call->func_vars, 0, ait_val_t*); AIT_SET_U8(v, srv->srv_session.sess_version); - v = io_array(vals, 1, ait_val_t*); + v = io_array(call->func_vars, 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); + v = io_array(call->func_vars, 2, ait_val_t*); + AIT_SET_U8(v, srv->srv_session.sess_process); + v = io_array(call->func_vars, 3, ait_val_t*); + AIT_SET_I32(v, io_arraySize(srv->srv_clients)); return 0; } -int +static int rpcServerShutdown(rpc_func_t *call, int ic, array_t *iv) { rpc_srv_t *srv; @@ -181,16 +164,14 @@ rpcServerShutdown(rpc_func_t *call, int ic, array_t *i else srv = call->func_parent; - pthread_mutex_lock(&srv->srv_mtx); - srv->srv_kill = kill; - pthread_mutex_unlock(&srv->srv_mtx); + srv->srv_kill = 1; return 0; } -// ---------------------------------------------------- +/* ---------------------------------------------------- */ -int +static int rpcBLOBServerShutdown(rpc_func_t *call, int ic, array_t *iv) { rpc_srv_t *srv; @@ -201,20 +182,17 @@ rpcBLOBServerShutdown(rpc_func_t *call, int ic, array_ else srv = call->func_parent; - pthread_mutex_lock(&srv->srv_blob.mtx); - srv->srv_blob.state = kill; - pthread_mutex_unlock(&srv->srv_blob.mtx); + srv->srv_blob.kill = 1; return 0; } -int +static int rpcBLOBServerVars(rpc_func_t *call, int ic, array_t *iv) { rpc_srv_t *srv; - array_t *vals; rpc_blob_t *b; - register int i; + register int i = 0; char *val, str[64]; int len; @@ -224,76 +202,47 @@ rpcBLOBServerVars(rpc_func_t *call, int ic, array_t *i else srv = call->func_parent; - 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"); + if (srv->srv_blob.kill) { + AIT_SET_STR(io_array(call->func_vars, 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; - } - pthread_mutex_lock(&srv->srv_blob.mtx); - for (i = 0, b = srv->srv_blob.blobs; b; i++, b = b->blob_next); + TAILQ_FOREACH(b, &srv->srv_blob.blobs, blob_node) + i++; len = i * sizeof str; if (!len) { - AIT_SET_STR(io_array(vals, 0, ait_val_t*), ""); - pthread_mutex_unlock(&srv->srv_blob.mtx); + AIT_SET_STR(io_array(call->func_vars, 0, ait_val_t*), ""); return 0; } if (!(val = malloc(len))) { LOGERR; - pthread_mutex_unlock(&srv->srv_blob.mtx); return -1; } else memset(val, 0, len); - for (b = srv->srv_blob.blobs, i = 0; b; i++, b = b->blob_next) { + 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_array(vals, 0, ait_val_t*), val); + AIT_SET_STR(io_array(call->func_vars, 0, ait_val_t*), val); free(val); - pthread_mutex_unlock(&srv->srv_blob.mtx); return 0; } -int -rpcBLOBServerState(rpc_func_t *call, int ic, array_t *iv) -{ - rpc_srv_t *srv; - - RPC_CALLBACK_CHK_ARGS(call, ic); - if (!call->func_parent) - return -1; - else - srv = call->func_parent; - - if (AIT_TYPE(io_array(iv, 0, ait_val_t*)) != i32) - return -1; - - pthread_mutex_lock(&srv->srv_mtx); - srv->srv_blob.state = AIT_GET_I32(io_array(iv, 0, ait_val_t*)); - pthread_mutex_unlock(&srv->srv_mtx); - return 0; -} - -int +static int rpcBLOBServerClients(rpc_func_t *call, int ic, array_t *iv) { rpc_srv_t *srv; - array_t *vals; rpc_cli_t *cli; register int i; int len; const char *str = NULL; - char *val, wrk[INET6_ADDRSTRLEN]; + char *val; + ait_val_t v; RPC_CALLBACK_CHECK_INPUT(call); if (!call->func_parent) @@ -301,47 +250,81 @@ rpcBLOBServerClients(rpc_func_t *call, int ic, array_t else srv = call->func_parent; - 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"); + if (srv->srv_blob.kill) { + AIT_SET_STR(io_array(call->func_vars, 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; + len = io_arraySize(srv->srv_blob.clients) * STRSIZ; if (!(val = malloc(len))) { LOGERR; return -1; } else memset(val, 0, len); - for (i = 0, cli = srv->srv_blob.clients; i < srv->srv_numcli; i++, cli++) { - if (!cli->cli_sa.sa.sa_family) + for (i = 0; i < io_arraySize(srv->srv_clients); i++) { + cli = io_array(srv->srv_blob.clients, i, rpc_cli_t*); + if (!cli) continue; - switch (cli->cli_sa.sa.sa_family) { - case AF_INET: - str = inet_ntop(cli->cli_sa.sa.sa_family, &cli->cli_sa.sin.sin_addr, wrk, sizeof wrk); - break; - case AF_INET6: - str = inet_ntop(cli->cli_sa.sa.sa_family, &cli->cli_sa.sin6.sin6_addr, wrk, sizeof wrk); - break; - case AF_LOCAL: - str = cli->cli_sa.sun.sun_path; - break; - } + str = io_n2addr(&cli->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_array(vals, 0, ait_val_t*), val); + AIT_SET_STR(io_array(call->func_vars, 0, ait_val_t*), val); free(val); + return 0; +} + +/* ----------------------------------------------------------------- */ + +/* + * rpc_register_srvServices() - Register internal service functions + * + * @srv = RPC server instance + * return: -1 error or 0 ok + */ +int +rpc_register_srvServices(rpc_srv_t * __restrict srv) +{ + if (!srv) + return -1; + + if (rpc_srv_registerCall(srv, CALL_SRVSHUTDOWN, rpcServerShutdown, 0) < 1) + return -1; + if (rpc_srv_registerCall(srv, CALL_SRVCLIENTS, rpcServerClients, 1) < 1) + return -1; + if (rpc_srv_registerCall(srv, CALL_SRVSESSIONS, rpcServerSessions, 4) < 1) + return -1; + if (rpc_srv_registerCall(srv, CALL_SRVCALLS, rpcServerCalls, 1) < 1) + return -1; + + return 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, 0) < 1) + return -1; + if (rpc_srv_registerCall(srv, CALL_BLOBCLIENTS, rpcBLOBServerClients, 1) < 1) + return -1; + if (rpc_srv_registerCall(srv, CALL_BLOBVARS, rpcBLOBServerVars, 1) < 1) + return -1; + return 0; }