--- libaitrpc/src/cli.c 2011/08/30 12:59:02 1.4.2.3 +++ libaitrpc/src/cli.c 2011/08/31 17:11:58 1.4.2.4 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: cli.c,v 1.4.2.3 2011/08/30 12:59:02 misho Exp $ +* $Id: cli.c,v 1.4.2.4 2011/08/31 17:11:58 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -301,29 +301,27 @@ rpc_cli_closeClient(rpc_cli_t * __restrict cli) * @cli = RPC Client session * @csModule = Module name, if NULL self binary * @csFunc = Function name for execute - * @in_argc = IN count of arguments * @in_vars = IN RPC call array of rpc values - * @out_argc = OUT returned count of arguments - * @out_vars = OUT returned array of rpc values, must be free after use (see rpc_cli_freeVals()) + * @out_vars = OUT returned array of rpc values, must be free after use with rpc_cli_freeVals() * return: -1 error or != -1 ok result */ int -rpc_cli_execCall(rpc_cli_t *cli, const char *csModule, const char *csFunc, int in_argc, - ait_val_t * __restrict in_vars, int *out_argc, ait_val_t ** __restrict out_vars) +rpc_cli_execCall(rpc_cli_t *cli, const char *csModule, const char *csFunc, + array_t * __restrict in_vars, array_t ** __restrict out_vars) { fd_set fds; - u_char *buf, str[MAXPATHLEN + UCHAR_MAX + 1], *data; + u_char *buf, str[MAXPATHLEN + UCHAR_MAX + 1]; struct tagRPCCall *rpc; struct tagRPCRet *rrpc = NULL; int ret = 0, Limit = 0; - register int i; - ait_val_t *v; struct timeval tv = { DEF_RPC_TIMEOUT, 0 }; - if (!cli || !csFunc || (in_argc && !in_vars)) { + if (!cli || !csFunc) { rpc_SetErr(EINVAL, "Error:: Can`t execute call because parameter is null or invalid!\n"); return -1; } + if (out_vars) + *out_vars = NULL; buf = malloc(cli->cli_netbuf); if (!buf) { @@ -342,57 +340,20 @@ rpc_cli_execCall(rpc_cli_t *cli, const char *csModule, /* prepare RPC call */ rpc = (struct tagRPCCall*) buf; memcpy(&rpc->call_session, cli->cli_parent, sizeof rpc->call_session); - rpc->call_argc = in_argc; + rpc->call_argc = in_vars ? io_arraySize(in_vars) : 0; rpc->call_tag = crcFletcher16((u_short*) str, sizeof str / 2); rpc->call_hash = hash_fnv((char*) str, sizeof str); Limit = sizeof(struct tagRPCCall); - if (in_argc) { + if (in_vars && io_arraySize(in_vars)) { /* marshaling variables */ - v = (ait_val_t*) (buf + sizeof(struct tagRPCCall)); - if (in_argc * sizeof(ait_val_t) > cli->cli_netbuf - Limit) { + ret = io_vals2buffer(buf + Limit, cli->cli_netbuf - Limit, in_vars); + if (ret == -1) { rpc_SetErr(EMSGSIZE, "Error:: in prepare RPC packet values (-7) ...\n"); free(buf); return -7; } else - Limit += in_argc * sizeof(ait_val_t); - memcpy(v, in_vars, in_argc * sizeof(ait_val_t)); - - /* adding additional data */ - data = (u_char*) v + in_argc * sizeof(ait_val_t); - for (i = 0; i < in_argc && !ret; i++) { - switch (AIT_TYPE(&in_vars[i])) { - case buffer: - if (Limit + AIT_LEN(&in_vars[i]) > cli->cli_netbuf) { - ret = -7; - break; - } - - memcpy(data, in_vars[i].val.buffer, AIT_LEN(&in_vars[i])); - v[i].val.buffer = (uint8_t*) ((void*) data - (void*) v); - data += AIT_LEN(&in_vars[i]); - Limit += AIT_LEN(&in_vars[i]); - break; - case string: - if (Limit + AIT_LEN(&in_vars[i]) > cli->cli_netbuf) { - ret = -7; - break; - } - - memcpy(data, in_vars[i].val.string, AIT_LEN(&in_vars[i])); - v[i].val.string = (int8_t*) ((void*) data - (void*) v); - data += AIT_LEN(&in_vars[i]); - Limit += AIT_LEN(&in_vars[i]); - break; - default: - break; - } - } - if (ret < 0) { - rpc_SetErr(EMSGSIZE, "Error:: in prepare RPC packet (-7) ...\n"); - free(buf); - return ret; - } + Limit += ret; } if ((ret = send(cli->cli_sock, buf, Limit, 0)) == -1) { @@ -452,78 +413,15 @@ rpc_cli_execCall(rpc_cli_t *cli, const char *csModule, rpc_SetErr(EMSGSIZE, "Error:: reply RPC packet is too long ...\n"); free(buf); return -7; - } else - Limit += rrpc->ret_argc * sizeof(ait_val_t); - /* RPC is OK! Go decapsulate variables ... */ + } + + /* RPC is OK! Go de-marshaling variables ... */ if (rrpc->ret_argc) { - *out_argc = rrpc->ret_argc; - *out_vars = calloc(rrpc->ret_argc, sizeof(ait_val_t)); + *out_vars = io_buffer2vals(buf + Limit, cli->cli_netbuf - Limit, rrpc->ret_argc, 0); if (!*out_vars) { - LOGERR; - *out_argc = 0; free(buf); return -1; - } else - memcpy(*out_vars, buf + sizeof(struct tagRPCRet), Limit - sizeof(struct tagRPCRet)); - - /* RPC received variables types OK! */ - data = (u_char*) buf + Limit; - for (i = 0; i < rrpc->ret_argc; i++) - switch (AIT_TYPE(&(*out_vars)[i])) { - case buffer: - if (AIT_LEN(&(*out_vars)[i]) > cli->cli_netbuf - Limit) { - rpc_SetErr(EMSGSIZE, "Error:: Too long RPC packet ...\n"); - free(*out_vars); - *out_vars = NULL; - *out_argc = 0; - free(buf); - return -7; - } else - Limit += AIT_LEN(&(*out_vars)[i]); - - (*out_vars)[i].val.buffer = malloc(AIT_LEN(&(*out_vars)[i])); - if (!(*out_vars)[i].val.buffer) { - rpc_SetErr(errno, "Error:: in prepare RPC reply ...\n"); - free(*out_vars); - *out_vars = NULL; - *out_argc = 0; - free(buf); - return -1; - } else - memcpy((*out_vars)[i].val.buffer, data, AIT_LEN(&(*out_vars)[i])); - data += AIT_LEN(&(*out_vars)[i]); - break; - case string: - if (AIT_LEN(&(*out_vars)[i]) > cli->cli_netbuf - Limit) { - rpc_SetErr(EMSGSIZE, "Error:: Too long RPC packet ...\n"); - free(*out_vars); - *out_vars = NULL; - *out_argc = 0; - free(buf); - return -7; - } else - Limit += AIT_LEN(&(*out_vars)[i]); - - (*out_vars)[i].val.string = malloc(AIT_LEN(&(*out_vars)[i])); - if (!(*out_vars)[i].val.string) { - rpc_SetErr(errno, "Error:: in prepare RPC reply ...\n"); - free(*out_vars); - *out_vars = NULL; - *out_argc = 0; - free(buf); - return -1; - } else - memcpy((*out_vars)[i].val.string, data, AIT_LEN(&(*out_vars)[i])); - data += AIT_LEN(&(*out_vars)[i]); - break; - default: - break; - } - } else { - if (out_argc) - *out_argc = 0; - if (out_vars) - *out_vars = NULL; + } } ret = rrpc->ret_retcode; @@ -533,21 +431,54 @@ rpc_cli_execCall(rpc_cli_t *cli, const char *csModule, /* * rpc_cli_freeVals() Free ait_val_t array returned from RPC call - * @args = Number of arguments in array - * @vars = Value elements + * @vars = Variable array * return: none */ inline void -rpc_cli_freeVals(int args, ait_val_t * __restrict vars) +rpc_cli_freeVals(array_t ** __restrict vars) { register int i; - if (!vars) + if (!vars || !*vars) return; - for (i = 0; i < args; i++) - AIT_FREE_VAL(&vars[i]); + for (i = 0; i < io_arraySize(*vars); i++) + if (io_arrayGet(*vars, i)) + AIT_FREE_VAL(io_array(*vars, i, ait_val_t*)); - free(vars); - vars = NULL; + io_arrayFree(*vars); + io_arrayDestroy(vars); +} + +/* + * rpc_cli_allocVals() Allocate ait_val_t array for RPC call + * @args = Number of arguments + * return: =NULL error or !=NULL allocated array + */ +inline array_t * +rpc_cli_allocVals(u_short args) +{ + array_t *arr; + register int i; + ait_val_t *v; + + if (!args) + return NULL; + + if (!(arr = io_arrayInit(args))) + return NULL; + + for (i = 0; i < io_arraySize(arr); i++) { + v = malloc(sizeof(ait_val_t)); + if (!v) { + LOGERR; + rpc_cli_freeVals(&arr); + return NULL; + } else { + memset(v, 0, sizeof(ait_val_t)); + io_arraySet(arr, i, v); + } + } + + return arr; }