--- libaitrpc/src/cli.c 2015/01/15 01:42:37 1.23 +++ libaitrpc/src/cli.c 2015/01/21 21:17:05 1.24 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: cli.c,v 1.23 2015/01/15 01:42:37 misho Exp $ +* $Id: cli.c,v 1.24 2015/01/21 21:17:05 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 - 2014 +Copyright 2004 - 2015 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -350,7 +350,11 @@ rpc_pkt_Receive(int sock, int type, sockaddr_t * __res do { if (type == SOCK_STREAM) ret = rpc_Read(sock, type, !estlen ? MSG_PEEK : 0, NULL, buf, blen); - else if (type == SOCK_BPF) { + else if (type == SOCK_EXT) { + ret = rpc_Read(sock, type, 0, NULL, buf, blen); + if (estlen) /* hack for skip sanity checks */ + ret += sizeof(struct tagRPCCall); + } else if (type == SOCK_BPF) { ret = rpc_Read(sock, type, 0, sa, AIT_GET_BUF(pkt), AIT_LEN(pkt)); if (ret > 0) estlen = ret; @@ -359,21 +363,31 @@ rpc_pkt_Receive(int sock, int type, sockaddr_t * __res if (ret < 1) return ret; + /* 1st read for RPC header */ + if (ret < sizeof(struct tagRPCCall)) { + rpc_SetErr(ERPCMISMATCH, "Short RPC packet %d bytes", ret); + return -1; + } + + /* check for loop request */ + if (!estlen && !(rpc->call_io & RPC_ACK)) + continue; + /* check for response from known address */ if (!estlen) { - /* 1st read for RPC header */ - if (ret < sizeof(struct tagRPCCall)) { - rpc_SetErr(ERPCMISMATCH, "Short RPC packet %d bytes", ret); - return -1; - } - /* calc estimated length */ estlen = ntohl(rpc->call_len); if (estlen > AIT_LEN(pkt)) AIT_RE_BUF(pkt, estlen); + blen = estlen; buf = AIT_GET_BUF(pkt); + + if (type == SOCK_EXT) { + blen -= sizeof(struct tagRPCCall); + buf += sizeof(struct tagRPCCall); + } + rpc = (struct tagRPCCall*) buf; - blen = estlen; continue; } @@ -381,7 +395,7 @@ rpc_pkt_Receive(int sock, int type, sockaddr_t * __res break; } while (42); - if (ret < sizeof(struct tagRPCCall) || estlen != ret) { + if (estlen != ret) { rpc_SetErr(ERPCMISMATCH, "RPC packet mismatch estimate %d bytes, but received %d\n", estlen, ret); return -1; @@ -427,7 +441,7 @@ rpc_pkt_Request(ait_val_t * __restrict pkt, rpc_sess_t if (!vars) rpc->call_argc = 0; else - rpc->call_argc = htons(array_Size(vars)); + rpc->call_argc = (u_char) array_Size(vars); /* set reply */ rpc->call_req.flags = (uint64_t) htonl(noreply ? RPC_NOREPLY : RPC_REPLY); @@ -444,6 +458,7 @@ rpc_pkt_Request(ait_val_t * __restrict pkt, rpc_sess_t /* total packet length */ rpc->call_len = htonl(len); + rpc->call_io = RPC_REQ; if (!nocrc) { /* calculate CRC */ @@ -505,7 +520,7 @@ rpc_pkt_Replay(ait_val_t * __restrict pkt, rpc_sess_t strerror(ntohl(rpc->call_rep.eno))); return -1; } - len = ntohs(rpc->call_argc) * sizeof(ait_val_t); + len = rpc->call_argc * sizeof(ait_val_t); if (len > AIT_LEN(pkt) - sizeof(struct tagRPCCall)) { rpc_SetErr(EMSGSIZE, "Reply RPC packet not enough buffer space ..."); return -1; @@ -516,13 +531,13 @@ rpc_pkt_Replay(ait_val_t * __restrict pkt, rpc_sess_t } /* RPC is OK! Go de-marshaling variables ... */ - if (vars && ntohs(rpc->call_argc)) { + if (vars && rpc->call_argc) { #ifdef CLI_RES_ZCOPY *vars = ait_buffer2vars(buf + sizeof(struct tagRPCCall), len, - ntohs(rpc->call_argc), 42); + rpc->call_argc, 42); #else *vars = ait_buffer2vars(buf + sizeof(struct tagRPCCall), len, - ntohs(rpc->call_argc), 0); + rpc->call_argc, 0); #endif if (!*vars) { rpc_SetErr(elwix_GetErrno(), "%s", elwix_GetError()); @@ -554,7 +569,7 @@ rpc_cli_execCall(rpc_cli_t *cli, int noreply, u_short rpc_SetErr(EINVAL, "Can`t execute call because parameter is null or invalid!"); return -1; } else { - if (cli->cli_id == SOCK_STREAM) + if (cli->cli_id == SOCK_STREAM || cli->cli_id == SOCK_EXT) type = cli->cli_id; buf = AIT_GET_BUF(&cli->cli_buf); } @@ -746,6 +761,71 @@ rpc_cli_closeClient2(rpc_cli_t ** __restrict cli) return; close((*cli)->cli_sock); + + AIT_FREE_VAL(&(*cli)->cli_buf); + + if ((*cli)->cli_parent) + e_free((*cli)->cli_parent); + + e_free(*cli); + *cli = NULL; +} + +/* + * rpc_cli_openClientExt() - Connect to pipe RPC Server + * + * @InstID = InstID for RPC session request + * @netBuf = Network buffer length (min:512 bytes), if =0 == BUFSIZ (also meaning max RPC packet) + * @fd = File descriptor + * return: NULL == error or !=NULL connection to RPC server established + */ +rpc_cli_t * +rpc_cli_openClientExt(u_char InstID, int netBuf, int fd) +{ + rpc_cli_t *cli = NULL; + int n; + + cli = e_malloc(sizeof(rpc_cli_t)); + if (!cli) { + LOGERR; + return NULL; + } else + memset(cli, 0, sizeof(rpc_cli_t)); + + /* build session */ + cli->cli_parent = e_malloc(sizeof(rpc_sess_t)); + if (!cli->cli_parent) { + LOGERR; + e_free(cli); + return NULL; + } else { + ((rpc_sess_t*) cli->cli_parent)->sess_version = RPC_VERSION; + ((rpc_sess_t*) cli->cli_parent)->sess_instance = InstID; + } + + cli->cli_id = SOCK_EXT; + cli->cli_sock = fd; + + n = (netBuf < RPC_MIN_BUFSIZ) ? getpagesize() : E_ALIGN(netBuf, 2); + AIT_SET_BUFSIZ(&cli->cli_buf, 0, n); + + fcntl(cli->cli_sock, F_SETFL, + fcntl(cli->cli_sock, F_GETFL) | O_NONBLOCK); + + return cli; +} + +/* + * rpc_cli_closeClientExt() - Close pipe connection to RPC server and free resources + * + * @cli = RPC Client session + * return: none + */ +void +rpc_cli_closeClientExt(rpc_cli_t ** __restrict cli) +{ + if (!cli || !*cli || (*cli)->cli_id != SOCK_EXT) + return; AIT_FREE_VAL(&(*cli)->cli_buf);