--- libaitrpc/src/cli.c 2013/05/30 09:22:01 1.16 +++ libaitrpc/src/cli.c 2013/08/22 15:31:09 1.17 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: cli.c,v 1.16 2013/05/30 09:22:01 misho Exp $ +* $Id: cli.c,v 1.17 2013/08/22 15:31:09 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -172,14 +172,6 @@ rpc_cli_openClient(u_char InstID, int netBuf, const ch else netBuf = E_ALIGN(netBuf, 2); /* align netBuf length */ -#ifdef HAVE_SRANDOMDEV - srandomdev(); -#else - time_t tim; - - srandom((time(&tim) ^ getpid())); -#endif - cli = e_malloc(sizeof(rpc_cli_t)); if (!cli) { LOGERR; @@ -330,10 +322,11 @@ int rpc_pkt_Receive(int sock, int type, sockaddr_t * __restrict sa, ait_val_t * __restrict pkt) { struct pollfd pfd; - int ret, len = 0; + int ret, estlen, blen = sizeof(struct tagRPCCall), len = 0; u_char *buf; sockaddr_t sa2; socklen_t salen; + struct tagRPCCall *rpc; if (!pkt) { rpc_SetErr(EINVAL, "Invalid argument(s)!"); @@ -360,13 +353,19 @@ rpc_pkt_Receive(int sock, int type, sockaddr_t * __res return -1; } - memset(buf, 0, AIT_LEN(pkt)); - if (type == SOCK_STREAM) - ret = recv(sock, buf, AIT_LEN(pkt), 0); - else { +payload: + if (type == SOCK_STREAM) { + memset(buf, 0, blen); + ret = recv(sock, buf, blen, 0); + /* handling case for reply without payload */ + if (!ret && buf != AIT_GET_BUF(pkt)) + return (buf - AIT_GET_BUF(pkt)); + } else { + blen = AIT_LEN(pkt); + memset(buf, 0, blen); memset(&sa2, 0, sizeof sa2); salen = sa2.ss.ss_len = sizeof(sockaddr_t); - ret = recvfrom(sock, buf, AIT_LEN(pkt), 0, &sa2.sa, &salen); + ret = recvfrom(sock, buf, blen, 0, &sa2.sa, &salen); } if (ret < 1) { if (ret) { @@ -379,13 +378,33 @@ rpc_pkt_Receive(int sock, int type, sockaddr_t * __res } /* check for response from known address */ - if (type == SOCK_DGRAM) + if (type == SOCK_DGRAM) { if (e_addrcmp(sa, &sa2, 42)) { rpc_SetErr(ERPCMISMATCH, "Received RPC response from unknown address"); continue; } + } else { + /* 1st read for RPC header */ + if (buf == AIT_GET_BUF(pkt)) { + if (ret < sizeof(struct tagRPCCall)) { + rpc_SetErr(ERPCMISMATCH, "Short RPC packet %d bytes", ret); + return -1; + } + + /* calc estimated length */ + rpc = (struct tagRPCCall*) buf; + estlen = ntohl(rpc->call_len); + if (estlen > AIT_LEN(pkt)) + AIT_RE_BUF(pkt, estlen); + buf = AIT_GET_BUF(pkt) + blen; + blen = estlen - blen; + goto payload; + } else + ret += sizeof(struct tagRPCCall); + } } while (0); + if (ret < sizeof(struct tagRPCCall)) { rpc_SetErr(ERPCMISMATCH, "Short RPC packet %d bytes", ret); return -1; @@ -410,20 +429,24 @@ rpc_pkt_Request(ait_val_t * __restrict pkt, rpc_sess_t array_t * __restrict vars, int noreply, int nocrc) { struct tagRPCCall *rpc; - int ret = 0, len = sizeof(struct tagRPCCall); + int ret = 0, estlen, len = sizeof(struct tagRPCCall); u_char *buf; if (!pkt || !sess) { rpc_SetErr(EINVAL, "Invalid argument(s)!"); return -1; - } else - buf = AIT_GET_BUF(pkt); + } + /* calc estimated length */ + estlen = ait_resideVars(vars) + len; + if (estlen > AIT_LEN(pkt)) + AIT_RE_BUF(pkt, estlen); + buf = AIT_GET_BUF(pkt); + /* prepare RPC call */ rpc = (struct tagRPCCall*) buf; rpc_addPktSession(&rpc->call_session, sess); rpc->call_tag = htons(tag); - rpc->call_seq = htons(random() % USHRT_MAX); if (!vars) rpc->call_argc = 0; else @@ -443,7 +466,7 @@ rpc_pkt_Request(ait_val_t * __restrict pkt, rpc_sess_t } /* total packet length */ - rpc->call_len = htons(len); + rpc->call_len = htonl(len); if (!nocrc) { /* calculate CRC */ @@ -484,7 +507,7 @@ rpc_pkt_Replay(ait_val_t * __restrict pkt, rpc_sess_t /* calculate CRC */ crc = ntohs(rpc->call_crc); rpc->call_crc ^= rpc->call_crc; - if (crc != crcFletcher16((u_short*) buf, ntohs(rpc->call_len) / 2)) { + if (crc != crcFletcher16((u_short*) buf, ntohl(rpc->call_len) / 2)) { rpc_SetErr(ERPCMISMATCH, "Bad CRC RPC packet"); return -1; } @@ -510,7 +533,7 @@ rpc_pkt_Replay(ait_val_t * __restrict pkt, rpc_sess_t rpc_SetErr(EMSGSIZE, "Reply RPC packet not enough buffer space ..."); return -1; } - if (len > ntohs(rpc->call_len) - sizeof(struct tagRPCCall)) { + if (len > ntohl(rpc->call_len) - sizeof(struct tagRPCCall)) { rpc_SetErr(EMSGSIZE, "Reply RPC packet is too short ..."); return -1; }