Diff for /libaitrpc/src/cli.c between versions 1.1 and 1.12

version 1.1, 2010/06/18 01:48:06 version 1.12, 2012/11/13 09:22:10
Line 5 Line 5
 * $Author$  * $Author$
 * $Id$  * $Id$
 *  *
*************************************************************************/**************************************************************************
 The ELWIX and AITNET software is distributed under the following
 terms:
 
 All of the documentation and software included in the ELWIX and AITNET
 Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
 
 Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
         by Michael Pounov <misho@elwix.org>.  All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:
 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.
 3. All advertising materials mentioning features or use of this software
    must display the following acknowledgement:
 This product includes software developed by Michael Pounov <misho@elwix.org>
 ELWIX - Embedded LightWeight unIX and its contributors.
 4. Neither the name of AITNET nor the names of its contributors
    may be used to endorse or promote products derived from this software
    without specific prior written permission.
 
 THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 SUCH DAMAGE.
 */
 #include "global.h"  #include "global.h"
   
   
 /*  /*
 * rpc_cli_openClient() Connect to RPC Server * rpc_cli_openBLOBClient() - Connect to BLOB Server
 * @ProgID = ProgramID for RPC session request *
 * @ProcID = ProcessID for RPC session request * @rpccli = RPC Client session
 * @family = Family socket type, AF_INET or AF_INET6 
 * @csHost = Host name or IP address for bind server 
  * @Port = Port for bind server, if Port == 0 default port is selected   * @Port = Port for bind server, if Port == 0 default port is selected
 * return: NULL == error or !=NULL connection to RPC server established * return: NULL == error or !=NULL connection to BLOB server established
  */   */
 rpc_cli_t *  rpc_cli_t *
rpc_cli_openClient(u_int ProgID, u_int ProcID, u_short family, const char *csHost, u_short Port)rpc_cli_openBLOBClient(rpc_cli_t * __restrict rpccli, u_short Port)
 {  {
         rpc_cli_t *cli = NULL;          rpc_cli_t *cli = NULL;
         struct hostent *host = NULL;  
         struct sockaddr_in sin;  
         struct sockaddr_in6 sin6;  
   
        if (!csHost || (family != AF_INET && family != AF_INET6)) {        if (!rpccli) {
                rpc_SetErr(EINVAL, "Error:: Invalid parameters can`t connect to RPC server ...\n");                rpc_SetErr(EINVAL, "Invalid parameters can`t connect to BLOB server");
                 return NULL;                  return NULL;
         }          }
        if (!Port)
                Port = RPC_DEFPORT;        cli = io_malloc(sizeof(rpc_cli_t));
        if (csHost) {        if (!cli) {
                host = gethostbyname2(csHost, family);                LOGERR;
                if (!host) {                return NULL;
                        rpc_SetErr(h_errno, "Error:: %s\n", hstrerror(h_errno));        } else
                        return NULL;                memcpy(cli, rpccli, sizeof(rpc_cli_t));
                }
        }        memcpy(&cli->cli_sa, &rpccli->cli_sa, sizeof(io_sockaddr_t));
        switch (family) {        switch (cli->cli_sa.sa.sa_family) {
                 case AF_INET:                  case AF_INET:
                        memset(&sin, 0, sizeof sin);                        cli->cli_sa.sin.sin_port = 
                        sin.sin_len = sizeof sin;                                htons(Port ? Port : ntohs(cli->cli_sa.sin.sin_port) + 1);
                        sin.sin_family = family; 
                        sin.sin_port = htons(Port); 
                        if (csHost) 
                                memcpy(&sin.sin_addr, host->h_addr, host->h_length); 
                         break;                          break;
                 case AF_INET6:                  case AF_INET6:
                        memset(&sin6, 0, sizeof sin6);                        cli->cli_sa.sin6.sin6_port = 
                        sin6.sin6_len = sizeof sin6;                                htons(Port ? Port : ntohs(cli->cli_sa.sin6.sin6_port) + 1);
                        sin6.sin6_family = family; 
                        sin6.sin6_port = htons(Port); 
                        if (csHost) 
                                memcpy(&sin6.sin6_addr, host->h_addr, host->h_length); 
                         break;                          break;
                   case AF_LOCAL:
                           strlcat(cli->cli_sa.sun.sun_path, ".blob", sizeof cli->cli_sa.sun.sun_path);
                           break;
                 default:                  default:
                        rpc_SetErr(EINVAL, "Error:: Invalid parameters can`t connect to RPC server ...\n");                        rpc_SetErr(EINVAL, "Invalid socket type %d", cli->cli_sa.sa.sa_family);
                         return NULL;                          return NULL;
         }          }
   
        cli = malloc(sizeof(rpc_cli_t));        AIT_COPY_VAL(&cli->cli_buf, &rpccli->cli_buf);
 
         /* connect to BLOB server */
         cli->cli_sock = socket(cli->cli_sa.sa.sa_family, SOCK_STREAM, 0);
         if (cli->cli_sock == -1) {
                 LOGERR;
                 io_free(cli);
                 return NULL;
         }
         if (connect(cli->cli_sock, &cli->cli_sa.sa, cli->cli_sa.sa.sa_len) == -1) {
                 LOGERR;
                 close(cli->cli_sock);
                 io_free(cli);
                 return NULL;
         } else
                 fcntl(cli->cli_sock, F_SETFL, fcntl(cli->cli_sock, F_GETFL) | O_NONBLOCK);
 
         return cli;
 }
 
 /*
  * rpc_cli_closeBLOBClient() - Close connection to BLOB server and free resources
  *
  * @cli = BLOB Client session
  * return: none
  */
 void
 rpc_cli_closeBLOBClient(rpc_cli_t ** __restrict cli)
 {
         if (!cli || !*cli)
                 return;
 
         shutdown((*cli)->cli_sock, SHUT_RDWR);
         close((*cli)->cli_sock);
 
         AIT_FREE_VAL(&(*cli)->cli_buf);
 
         io_free(*cli);
         *cli = NULL;
 }
 
 /* -------------------------------------------------------------- */
 
 /*
  * rpc_cli_openClient() - Connect to RPC Server
  *
  * @ProgID = ProgramID for RPC session request
  * @ProcID = ProcessID for RPC session request
  * @netBuf = Network buffer length (min:512 bytes), if =0 == BUFSIZ (also meaning max RPC packet)
  * @csHost = Host name or IP address for bind server
  * @Port = Port for bind server, if Port == 0 default port is selected
  * return: NULL == error or !=NULL connection to RPC server established
  */
 rpc_cli_t *
 rpc_cli_openClient(u_int ProgID, u_char ProcID, int netBuf, const char *csHost, u_short Port)
 {
         rpc_cli_t *cli = NULL;
         io_sockaddr_t sa = IO_SOCKADDR_INIT;
 
         if (!io_gethostbyname(csHost, Port, &sa))
                 return NULL;
         if (!Port)
                 Port = RPC_DEFPORT;
         if (netBuf < RPC_MIN_BUFSIZ)
                 netBuf = BUFSIZ;
         else
                 netBuf = io_align(netBuf, 2);   /* align netBuf length */
 
 #ifdef HAVE_SRANDOMDEV
         srandomdev();
 #else
         time_t tim;
 
         srandom((time(&tim) ^ getpid()));
 #endif
 
         cli = io_malloc(sizeof(rpc_cli_t));
         if (!cli) {          if (!cli) {
                 LOGERR;                  LOGERR;
                 return NULL;                  return NULL;
         } else          } else
                 memset(cli, 0, sizeof(rpc_cli_t));                  memset(cli, 0, sizeof(rpc_cli_t));
        cli->cli_parent = malloc(sizeof(rpc_sess_t));
         /* build session */
         cli->cli_parent = io_malloc(sizeof(rpc_sess_t));
         if (!cli->cli_parent) {          if (!cli->cli_parent) {
                 LOGERR;                  LOGERR;
                free(cli);                io_free(cli);
                 return NULL;                  return NULL;
         } else {          } else {
                 ((rpc_sess_t*) cli->cli_parent)->sess_version = RPC_VERSION;                  ((rpc_sess_t*) cli->cli_parent)->sess_version = RPC_VERSION;
Line 78  rpc_cli_openClient(u_int ProgID, u_int ProcID, u_short Line 183  rpc_cli_openClient(u_int ProgID, u_int ProcID, u_short
                 ((rpc_sess_t*) cli->cli_parent)->sess_process = ProcID;                  ((rpc_sess_t*) cli->cli_parent)->sess_process = ProcID;
         }          }
   
        if (family == AF_INET)        memcpy(&cli->cli_sa, &sa, sizeof cli->cli_sa);
                memcpy(&cli->cli_sa, &sin, sizeof cli->cli_sa);        AIT_SET_BUF2(&cli->cli_buf, 0, netBuf);
        else
                memcpy(&cli->cli_sa, &sin6, sizeof cli->cli_sa);        /* connect to RPC server */
        cli->cli_sock = socket(family, SOCK_STREAM, 0);        cli->cli_sock = socket(cli->cli_sa.sa.sa_family, SOCK_STREAM, 0);
         if (cli->cli_sock == -1) {          if (cli->cli_sock == -1) {
                 LOGERR;                  LOGERR;
                free(cli->cli_parent);                AIT_FREE_VAL(&cli->cli_buf);
                free(cli);                io_free(cli->cli_parent);
                 io_free(cli);
                 return NULL;                  return NULL;
         }          }
        if (connect(cli->cli_sock, &cli->cli_sa, sizeof cli->cli_sa) == -1) {        if (connect(cli->cli_sock, &cli->cli_sa.sa, cli->cli_sa.sa.sa_len) == -1) {
                 LOGERR;                  LOGERR;
                free(cli->cli_parent);                AIT_FREE_VAL(&cli->cli_buf);
                free(cli);                close(cli->cli_sock);
                 io_free(cli->cli_parent);
                 io_free(cli);
                 return NULL;                  return NULL;
        }        } else
                 fcntl(cli->cli_sock, F_SETFL, fcntl(cli->cli_sock, F_GETFL) | O_NONBLOCK);
   
         return cli;          return cli;
 }  }
   
 /*  /*
 * rpc_cli_closeClient() Close connection to RPC server and free resources * rpc_cli_closeClient() - Close connection to RPC server and free resources
  *
  * @cli = RPC Client session   * @cli = RPC Client session
  * return: none   * return: none
  */   */
 void  void
rpc_cli_closeClient(rpc_cli_t * __restrict cli)rpc_cli_closeClient(rpc_cli_t ** __restrict cli)
 {  {
        if (!cli) {        if (!cli || !*cli)
                rpc_SetErr(EINVAL, "Error:: Can`t close connection because parameter is null!\n"); 
                 return;                  return;
         }  
   
        shutdown(cli->cli_sock, SHUT_RDWR);        shutdown((*cli)->cli_sock, SHUT_RDWR);
        close(cli->cli_sock);        close((*cli)->cli_sock);
   
        if (cli->cli_parent)        AIT_FREE_VAL(&(*cli)->cli_buf);
                free(cli->cli_parent);
        free(cli);        if ((*cli)->cli_parent)
        cli = NULL;                io_free((*cli)->cli_parent);
 
         io_free(*cli);
         *cli = NULL;
 }  }
   
   
 /*  /*
 * rpc_cli_execCall() Execute RPC call * rpc_cli_execCall() - Execute RPC call
  *
  * @cli = RPC Client session   * @cli = RPC Client session
 * @csModule = Module name, if NULL self binary * @noreply = We not want RPC reply
 * @csFunc = Function name for execute * @tag = Function tag for execution
 * @in_argc = IN count of arguments * @in_vars = IN RPC call array of rpc values, may be NULL
 * @in_vals = IN RPC call array of rpc values * @out_vars = OUT returned array of rpc values, if !=NULL must be free after use with io_freeVars()
 * @out_argc = OUT returned count of arguments 
 * @out_vals = OUT returned array of rpc values, must be free after use (see rpc_cli_freeVals()) 
  * return: -1 error or != -1 ok result   * return: -1 error or != -1 ok result
  */   */
 int  int
rpc_cli_execCall(rpc_cli_t *cli, const char *csModule, const char *csFunc, int in_argc, rpc_cli_execCall(rpc_cli_t *cli, int noreply, u_short tag, 
                rpc_val_t * __restrict in_vals, int *out_argc, rpc_val_t ** __restrict out_vals)                array_t * __restrict in_vars, array_t ** __restrict out_vars)
 {  {
        fd_set fds;        struct tagRPCCall *rpc;
        u_char buf[BUFSIZ], str[MAXPATHLEN + UCHAR_MAX + 1], *data;        int ret = 0, wlen = sizeof(struct tagRPCCall);
        struct tagRPCCall *rpc = (struct tagRPCCall*) buf;        uint16_t crc;
        struct tagRPCRet *rrpc;        u_char *buf;
        int ret = 0, Limit = 0;        struct pollfd pfd;
        register int i; 
        rpc_val_t *v; 
        struct timeval tv = { DEF_CLI_TIMEOUT, 0 }; 
   
        FD_ZERO(&fds);        if (!cli) {
        memset(buf, 0, BUFSIZ);                rpc_SetErr(EINVAL, "Can`t execute call because parameter is null or invalid!");
        memset(str, 0, MAXPATHLEN + UCHAR_MAX + 1); 
        if (!cli || !csFunc || (in_argc && !in_vals)) { 
                rpc_SetErr(EINVAL, "Error:: Can`t execute call because parameter is null or invalid!\n"); 
                 return -1;                  return -1;
           } else
                   buf = AIT_GET_BUF(&cli->cli_buf);
           if (out_vars)
                   *out_vars = NULL;
   
           /* prepare RPC call */
           rpc = (struct tagRPCCall*) buf;
           rpc_addPktSession(&rpc->call_session, cli->cli_parent);
           rpc->call_argc = htons(io_arraySize(in_vars));
           rpc->call_tag = htons(tag);
           rpc->call_seq = htons(random() % USHRT_MAX);
   
           /* set reply */
           rpc->call_req.flags = noreply ? RPC_NOREPLY : RPC_REPLY;
   
           if (io_arraySize(in_vars)) {
                   /* marshaling variables */
                   ret = io_vars2buffer(buf + wlen, AIT_LEN(&cli->cli_buf) - wlen, in_vars);
                   if (ret == -1) {
                           rpc_SetErr(EBADRPC, "Failed to prepare RPC packet values");
                           return -1;
                   } else
                           wlen += ret;
         }          }
         if (csModule)  
                 strlcpy((char*) str, csModule, MAXPATHLEN + UCHAR_MAX + 1);  
         strlcat((char*) str, "__", MAXPATHLEN + UCHAR_MAX + 1);  
         strlcat((char*) str, csFunc, MAXPATHLEN + UCHAR_MAX + 1);  
   
        memcpy(&rpc->call_session, cli->cli_parent, sizeof rpc->call_session);        /* total packet length */
        rpc->call_argc = in_argc;        rpc->call_len = htons(wlen);
        rpc->call_tag = crcFletcher16((u_short*) str, (MAXPATHLEN + UCHAR_MAX + 1) / 2); 
        rpc->call_hash = hash_fnv((char*) str, MAXPATHLEN + UCHAR_MAX + 1); 
        Limit = sizeof(struct tagRPCCall); 
        if (in_argc) { 
                v = (rpc_val_t*) (buf + sizeof(struct tagRPCCall)); 
                memcpy(v, in_vals, in_argc * sizeof(rpc_val_t)); 
                Limit += in_argc * sizeof(rpc_val_t); 
                data = (u_char*) v + in_argc * sizeof(rpc_val_t); 
                for (i = 0; i < in_argc; i++) { 
                        switch (in_vals[i].val_type) { 
                                case buffer: 
                                        if (Limit + in_vals[i].val_len > BUFSIZ) { 
                                                ret = -7; 
                                                break; 
                                        } 
   
                                        memcpy(data, in_vals[i].val.buffer, in_vals[i].val_len);        /* calculate CRC */
                                        v[i].val.buffer = (uint8_t*) ((void*) data - (void*) v);        rpc->call_crc ^= rpc->call_crc;
                                        data += in_vals[i].val_len;        rpc->call_crc = htons(crcFletcher16((u_short*) buf, wlen / 2));
                                        Limit += in_vals[i].val_len; 
                                        break; 
                                case string: 
                                        if (Limit + in_vals[i].val_len + 1 > BUFSIZ) { 
                                                ret = -7; 
                                                break; 
                                        } 
   
                                        memcpy(data, in_vals[i].val.string, in_vals[i].val_len);        pfd.fd = cli->cli_sock;
                                        v[i].val.string = (int8_t*) ((void*) data - (void*) v);        pfd.events = POLLOUT;
                                        data += in_vals[i].val_len;        if ((ret = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) < 1 || 
                                        Limit += in_vals[i].val_len;                        pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
                                        break;                if (ret)
                                case array:                        LOGERR;
                                        if (Limit + in_vals[i].val_len > BUFSIZ) {                else
                                                ret = -7;                        rpc_SetErr(ETIMEDOUT, "Timeout, can't send to RPC server");
                                                break;                return -1;
                                        }        }
         do {
                 if ((ret = send(cli->cli_sock, buf, wlen, MSG_NOSIGNAL)) == -1) {
                         if (errno == EAGAIN)
                                 continue;
                         LOGERR;
                         return -1;
                 } else if (ret != wlen) {
                         rpc_SetErr(EPROCUNAVAIL, "RPC request, should be send %d bytes, "
                                         "really sended %d bytes", wlen, ret);
                         return -1;
                 }
         } while (0);
   
                                        memcpy(data, in_vals[i].val.array, in_vals[i].val_len);        if (noreply)    /* we not want reply */
                                        v[i].val.array = (int8_t**) ((void*) data - (void*) v);                return 0;
                                        data += in_vals[i].val_len;
                                        Limit += in_vals[i].val_len;        wlen = 0;
                                        break;        /* reply from RPC server */
                                default:        pfd.events = POLLIN | POLLPRI;
                                        break;        do {
                 if ((ret = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) < 1 || 
                                 pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
                         if (ret)
                                 LOGERR;
                         else {
                                 if (wlen++ < 7)
                                         continue;
                                 else
                                         rpc_SetErr(ETIMEDOUT, "Timeout, no answer from RPC server");
                         }                          }
                           return -1;
                 }                  }
                if (ret < 0) {        } while (0);
                        rpc_SetErr(EMSGSIZE, "Error:: in prepare RPC packet (-7) ...\n");        do {
                        return ret;                memset(buf, 0, AIT_LEN(&cli->cli_buf));
                 if ((ret = recv(cli->cli_sock, buf, AIT_LEN(&cli->cli_buf), 0)) < 1) {
                         if (ret) {
                                 if (errno == EAGAIN)
                                         continue;
                                 else
                                         LOGERR;
                         }
                         return -1;
                 }                  }
        }        } while (0);
        if ((ret = send(cli->cli_sock, buf, Limit, 0)) == -1) {        if (ret < sizeof(struct tagRPCCall)) {
                LOGERR;                rpc_SetErr(ERPCMISMATCH, "Short RPC packet %d bytes", ret);
                 return -1;                  return -1;
         }          }
         if (ret != Limit) {  
                 rpc_SetErr(EBADMSG, "Error:: in send RPC request, should be send %d bytes, really is %d\n",   
                                 Limit, ret);  
                 return -9;  
         }  
   
        FD_SET(cli->cli_sock, &fds);        /* calculate CRC */
        if ((ret = select(cli->cli_sock + 1, &fds, NULL, NULL, &tv)) < 1) {        crc = ntohs(rpc->call_crc);
                if (ret) {        rpc->call_crc ^= rpc->call_crc;
                        LOGERR;        if (crc != crcFletcher16((u_short*) buf, ntohs(rpc->call_len) / 2)) {
                } else                rpc_SetErr(ERPCMISMATCH, "Bad CRC RPC packet");
                        rpc_SetErr(ETIMEDOUT, "Error:: timeout, no return from RPC server?\n"); 
                 return -1;                  return -1;
         }          }
        memset(buf, 0, BUFSIZ);
        if ((ret = recv(cli->cli_sock, buf, BUFSIZ, 0)) == -1) {        /* check RPC packet session info */
                LOGERR;        if (rpc_chkPktSession(&rpc->call_session, cli->cli_parent)) {
                return -3;                rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session");
        }                return -1;
        if (!ret)       // receive EOF 
                return 0; 
        if (ret < sizeof(struct tagRPCCall)) { 
                rpc_SetErr(EMSGSIZE, "Error:: too short RPC packet ...\n"); 
                return -4; 
         } else          } else
                rrpc = (struct tagRPCRet*) buf;                wlen = sizeof(struct tagRPCCall);
        // check RPC packet session info        if (ntohs(rpc->call_tag) != tag) {
        if (memcmp(&rrpc->ret_session, cli->cli_parent, sizeof rrpc->ret_session)) {                rpc_SetErr(ERPCMISMATCH, "Get wrong RPC reply");
                rpc_SetErr(EINVAL, "Error:: get invalid RPC session ...\n");                return -1;
                return -5; 
         }          }
        if (rrpc->ret_retcode < 0 && rrpc->ret_errno) {        if (ntohl(rpc->call_rep.eno) && ntohl(rpc->call_rep.ret) == -1) {
                rpc_SetErr(rrpc->ret_errno, "Error::Server side: %s\n", strerror(rrpc->ret_errno));                rpc_SetErr(ntohl(rpc->call_rep.eno), "Server side: retcode=%d #%d %s", 
                return -6;                                ntohl(rpc->call_rep.ret), ntohl(rpc->call_rep.eno), 
                                 strerror(ntohl(rpc->call_rep.eno)));
                 return -1;
         }          }
        // RPC is OK! Go decapsulate variables ...        if (ntohs(rpc->call_argc) * sizeof(ait_val_t) > AIT_LEN(&cli->cli_buf) - wlen) {
        if (rrpc->ret_argc) {                rpc_SetErr(EMSGSIZE, "Reply RPC packet is too long ...");
                *out_argc = rrpc->ret_argc;                return -1;
                *out_vals = calloc(rrpc->ret_argc, sizeof(rpc_val_t));        }
                if (!*out_vals) {
                        LOGERR;        /* RPC is OK! Go de-marshaling variables ... */
                        *out_argc = 0;        if (out_vars && ntohs(rpc->call_argc)) {
 #ifdef CLI_RES_ZCOPY
                 *out_vars = io_buffer2vars(buf + wlen, AIT_LEN(&cli->cli_buf) - wlen, 
                                 ntohs(rpc->call_argc), 42);
 #else
                 *out_vars = io_buffer2vars(buf + wlen, AIT_LEN(&cli->cli_buf) - wlen, 
                                 ntohs(rpc->call_argc), 0);
 #endif
                 if (!*out_vars) {
                         rpc_SetErr(io_GetErrno(), "%s", io_GetError());
                         return -1;                          return -1;
                } else                }
                        Limit = rrpc->ret_argc * sizeof(rpc_val_t); 
                memcpy(*out_vals, buf + sizeof(struct tagRPCRet), Limit); 
                // RPC received variables types OK! 
                data = (u_char*) buf + sizeof(struct tagRPCRet) + Limit; 
                for (i = 0; i < rrpc->ret_argc; i++) 
                        switch ((*out_vals)[i].val_type) { 
                                case buffer: 
                                        (*out_vals)[i].val.buffer = malloc((*out_vals)[i].val_len); 
                                        if (!(*out_vals)[i].val.buffer) { 
                                                rpc_SetErr(errno, "Error:: in prepare RPC reply ...\n"); 
                                                free(*out_vals); 
                                                *out_vals = NULL; 
                                                *out_argc = 0; 
                                                return -1; 
                                        } else 
                                                memcpy((*out_vals)[i].val.buffer, data, (*out_vals)[i].val_len); 
                                        data += (*out_vals)[i].val_len; 
                                        break; 
                                case string: 
                                        (*out_vals)[i].val.string = (int8_t*) strdup((char*) data); 
                                        if (!(*out_vals)[i].val.string) { 
                                                rpc_SetErr(errno, "Error:: in prepare RPC reply ...\n"); 
                                                free(*out_vals); 
                                                *out_vals = NULL; 
                                                *out_argc = 0; 
                                                return -1; 
                                        } 
                                        data += (*out_vals)[i].val_len + 1; 
                                        break; 
                                case array: 
                                        (*out_vals)[i].val.array = malloc((*out_vals)[i].val_len); 
                                        if (!(*out_vals)[i].val.array) { 
                                                rpc_SetErr(errno, "Error:: in prepare RPC reply ...\n"); 
                                                free(*out_vals); 
                                                *out_vals = NULL; 
                                                *out_argc = 0; 
                                                return -1; 
                                        } else 
                                                memcpy((*out_vals)[i].val.array, data, (*out_vals)[i].val_len); 
                                        data += (*out_vals)[i].val_len; 
                                        break; 
                                default: 
                                        break; 
                        } 
        } else { 
                *out_argc = 0; 
                *out_vals = NULL; 
         }          }
   
        return rrpc->ret_retcode;        ret = ntohl(rpc->call_rep.ret);
         return ret;
 }  }
   
 /*  /*
 * rpc_cli_freeVals() Free rpc_val_t array returned from RPC call * rpc_cli_freeCall() - Free resouce allocated by RPC call
 * @args = Number of arguments in array *
 * @vals = Value elements * @out_vars = Returned array with variables from RPC call
  * return: none   * return: none
  */   */
 inline void  inline void
rpc_cli_freeVals(int args, rpc_val_t *vals)rpc_cli_freeCall(array_t ** __restrict out_vars)
 {  {
        register int i;#ifdef CLI_RES_ZCOPY
         io_arrayDestroy(out_vars);
 #else
         io_freeVars(out_vars);
 #endif
 }
   
        if (!vals)/*
                return; * rpc_cli_ping() - Ping RPC server
  *
  * @cli = connected client
  * return: -1 error or !=-1 ping seq id
  */
 inline int
 rpc_cli_ping(rpc_cli_t *cli)
 {
         int ret = 0;
         array_t *arr = NULL;
   
        for (i = 0; i < args; i++)        if (!cli)
                RPC_FREE_VAL(&vals[i]);                return -1;
   
        free(vals);        if (rpc_cli_execCall(cli, RPC_REPLY, CALL_SRVPING, NULL, &arr))
        vals = NULL;                return -1;
         else
                 ret = AIT_GET_U16(io_array(arr, 0, ait_val_t*));
         rpc_cli_freeCall(&arr);
 
         return ret;
 }  }

Removed from v.1.1  
changed lines
  Added in v.1.12


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>