--- libaitrpc/src/cli.c 2011/09/07 07:24:21 1.5 +++ libaitrpc/src/cli.c 2011/09/07 09:22:15 1.5.2.3 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: cli.c,v 1.5 2011/09/07 07:24:21 misho Exp $ +* $Id: cli.c,v 1.5.2.3 2011/09/07 09:22:15 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -314,6 +314,8 @@ rpc_cli_execCall(rpc_cli_t *cli, const char *csModule, struct tagRPCRet *rrpc = NULL; int ret = 0, Limit = 0; struct timeval tv = { DEF_RPC_TIMEOUT, 0 }; + uint16_t tag; + uint32_t hash; if (!cli || !csFunc) { rpc_SetErr(EINVAL, "Error:: Can`t execute call because parameter is null or invalid!\n"); @@ -338,10 +340,10 @@ 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_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); + rpc_addPktSession(&rpc->call_session, cli->cli_parent); + rpc->call_argc = htons(in_vars ? io_arraySize(in_vars) : 0); + rpc->call_tag = tag = htons(crcFletcher16((u_short*) str, sizeof str / 2)); + rpc->call_hash = hash = htonl(hash_fnv((char*) str, sizeof str)); Limit = sizeof(struct tagRPCCall); if (in_vars && io_arraySize(in_vars)) { @@ -396,34 +398,42 @@ rpc_cli_execCall(rpc_cli_t *cli, const char *csModule, } else rrpc = (struct tagRPCRet*) buf; /* check RPC packet session info */ - if (memcmp(&rrpc->ret_session, cli->cli_parent, sizeof rrpc->ret_session)) { + if (rpc_chkPktSession(&rrpc->ret_session, cli->cli_parent)) { rpc_SetErr(ERPCMISMATCH, "Error:: get invalid RPC session ...\n"); free(buf); return -5; } else Limit = sizeof(struct tagRPCRet); - if (rrpc->ret_retcode < 0 && rrpc->ret_errno) { - rpc_SetErr(rrpc->ret_errno, "Error::Server side: retcode=%d #%d %s\n", - rrpc->ret_retcode, rrpc->ret_errno, strerror(rrpc->ret_errno)); + if (rrpc->ret_tag != tag || rrpc->ret_hash != hash) { + printf("tag=%x/%x hash=%x/%x\n", rrpc->ret_tag, tag, rrpc->ret_hash, hash); + rpc_SetErr(ERPCMISMATCH, "Error:: get wrong RPC reply ...\n"); free(buf); + return -5; + } + if (ntohl(rrpc->ret_retcode) < 0 && ntohl(rrpc->ret_errno)) { + rpc_SetErr(ntohl(rrpc->ret_errno), "Error::Server side: retcode=%d #%d %s\n", + ntohl(rrpc->ret_retcode), ntohl(rrpc->ret_errno), + strerror(ntohl(rrpc->ret_errno))); + free(buf); return -6; } - if (rrpc->ret_argc * sizeof(ait_val_t) > cli->cli_netbuf - Limit) { + if (ntohs(rrpc->ret_argc) * sizeof(ait_val_t) > cli->cli_netbuf - Limit) { rpc_SetErr(EMSGSIZE, "Error:: reply RPC packet is too long ...\n"); free(buf); return -7; } /* RPC is OK! Go de-marshaling variables ... */ - if (rrpc->ret_argc) { - *out_vars = io_buffer2vals(buf + Limit, cli->cli_netbuf - Limit, rrpc->ret_argc, 0); + if (ntohs(rrpc->ret_argc)) { + *out_vars = io_buffer2vals(buf + Limit, cli->cli_netbuf - Limit, + ntohs(rrpc->ret_argc), 0); if (!*out_vars) { free(buf); return -1; } } - ret = rrpc->ret_retcode; + ret = ntohl(rrpc->ret_retcode); free(buf); return ret; }