Diff for /libaitrpc/src/blobcli.c between versions 1.1 and 1.2

version 1.1, 2015/07/02 17:52:53 version 1.2, 2015/07/02 22:28:15
Line 0 Line 1
   /*************************************************************************
   * (C) 2015 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
   *  by Michael Pounov <misho@openbsd-bg.org>
   *
   * $Author$
   * $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 - 2015
           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"
   
   
   /*
    * rpc_cli_sendBLOB() - Send BLOB to server
    *
    * @cli = Client instance
    * @var = BLOB variable
    * @data = BLOB data
    * @tout = BLOB live on server timeout in seconds, if =0 default timeout
    * return: -1 error, 0 ok, 1 remote error
    */
   int
   rpc_cli_sendBLOB(rpc_cli_t * __restrict cli, ait_val_t * __restrict var, 
                   void * __restrict data, int tout)
   {
           int ret, len;
           uint8_t *pos;
           struct tagBLOBHdr hdr;
           struct pollfd pfd;
   
           if (!cli || !var || !data) {
                   rpc_SetErr(EINVAL, "Invalid arguments");
                   return -1;
           } else
                   memset(&hdr, 0, sizeof hdr);
   
           rpc_addPktSession(&hdr.hdr_session, cli->cli_parent);
           hdr.hdr_cmd = set;
           hdr.hdr_var = 0;
           hdr.hdr_ret = tout;
           hdr.hdr_len = htonl(AIT_LEN(var));
   
           /* send SET request */
           pfd.fd = cli->cli_sock;
           pfd.events = POLLOUT;
           if ((ret = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) == -1 || 
                           pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
                   LOGERR;
                   return -1;
           }
           if (send(cli->cli_sock, &hdr, sizeof hdr, MSG_NOSIGNAL) == -1) {
                   LOGERR;
                   return -1;
           }
   
           /* send BLOB to server */
           for (ret = AIT_LEN(var), pos = data; ret > 0; ret -= len, pos += len)
                   if ((len = send(cli->cli_sock, pos, ret, MSG_NOSIGNAL)) == -1) {
                           LOGERR;
                           return -1;
                   }
   
           /* wait for reply */
           pfd.events = POLLIN | POLLPRI;
           if ((ret = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) < 1 || 
                           pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
                   if (ret)
                           LOGERR;
                   else
                           rpc_SetErr(ETIMEDOUT, "Timeout reached! Server not respond");
                   return -1;
           }
           if (recv(cli->cli_sock, &hdr, sizeof hdr, 0) == -1) {
                   LOGERR;
                   return 1;
           }
   
           if (hdr.hdr_cmd != error) {
                   AIT_SET_BLOB(var, ntohl(hdr.hdr_var), ntohl(hdr.hdr_len));
   
                   if (ntohl(hdr.hdr_len) != AIT_LEN(var)) {
                           rpc_cli_delBLOB(cli, var);
                           AIT_NEW_BLOB(var, ntohl(hdr.hdr_len));
   
                           rpc_SetErr(ECANCELED, "Bad return length packet");
                           return 1;
                   }
           }
   
           return hdr.hdr_cmd == error;
   }
   
   /*
    * rpc_cli_recvBLOB() - Receive BLOB from server
    *
    * @cli = Client instance
    * @var = BLOB variable
    * @data = BLOB data, must be e_free after use!
    * return: -1 error, 0 ok, 1 remote error
    */
   int
   rpc_cli_recvBLOB(rpc_cli_t * __restrict cli, ait_val_t * __restrict var, void ** __restrict data)
   {
           int ret, len;
           uint8_t *pos;
           struct pollfd pfd;
           struct tagBLOBHdr hdr;
   
           if (!cli || !var || !data) {
                   rpc_SetErr(EINVAL, "Invalid arguments");
                   return -1;
           }
   
           *data = e_malloc(AIT_LEN(var));
           if (!*data) {
                   LOGERR;
                   return -1;
           } else {
                   memset(&hdr, 0, sizeof hdr);
                   memset(*data, 0, AIT_LEN(var));
           }
   
           rpc_addPktSession(&hdr.hdr_session, cli->cli_parent);
           hdr.hdr_cmd = get;
           hdr.hdr_var = htonl((uint32_t) AIT_GET_BLOB(var));
           hdr.hdr_ret = 0;
           hdr.hdr_len = 0;
   
           /* send GET request */
           pfd.fd = cli->cli_sock;
           pfd.events = POLLOUT;
           if ((ret = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) == -1 || 
                           pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
                   LOGERR;
                   e_free(*data);
                   *data = NULL;
                   return -1;
           }
           if (send(cli->cli_sock, &hdr, sizeof hdr, 0) == -1) {
                   LOGERR;
                   e_free(*data);
                   *data = NULL;
                   return -1;
           }
   
           /* receive BLOB from server */
           pfd.events = POLLIN | POLLPRI;
           for (ret = AIT_LEN(var), pos = *data; ret > 0; ret -= len, pos += len) {
                   if ((len = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) < 1 || 
                                   pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
                           LOGERR;
                           e_free(*data);
                           *data = NULL;
                           return -1;
                   }
   
                   if ((len = recv(cli->cli_sock, pos, ret, 0)) == -1) {
                           LOGERR;
                           e_free(*data);
                           *data = NULL;
                           return -1;
                   }
           }
   
           /* wait for reply */
           if ((len = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) < 1 || 
                           pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
                   if (len)
                           LOGERR;
                   else
                           rpc_SetErr(ETIMEDOUT, "Timeout reached! Server not respond");
                   e_free(*data);
                   *data = NULL;
                   return 1;
           }
           if (recv(cli->cli_sock, &hdr, sizeof hdr, 0) == -1) {
                   LOGERR;
                   e_free(*data);
                   *data = NULL;
                   return 1;
           }
           if (hdr.hdr_cmd != error) {
                   if (ntohl(hdr.hdr_len) != AIT_LEN(var)) {
                           rpc_SetErr(ECANCELED, "Bad return length packet");
                           e_free(*data);
                           *data = NULL;
                           return 1;
                   }
           }
   
           return hdr.hdr_cmd == error;
   }
   
   /*
    * rpc_cli_delBLOB() - Delete BLOB from server
    *
    * @cli = Client instance
    * @var = BLOB variable
    * return: -1 error, 0 ok, 1 remote error
    */
   int
   rpc_cli_delBLOB(rpc_cli_t * __restrict cli, ait_val_t * __restrict var)
   {
           struct tagBLOBHdr hdr;
           struct pollfd pfd;
           int ret;
   
           if (!cli || !var) {
                   rpc_SetErr(EINVAL, "Invalid arguments");
                   return -1;
           } else
                   memset(&hdr, 0, sizeof hdr);
   
           rpc_addPktSession(&hdr.hdr_session, cli->cli_parent);
           hdr.hdr_cmd = unset;
           hdr.hdr_var = htonl((uint32_t) AIT_GET_BLOB(var));
           hdr.hdr_ret = 0;
           hdr.hdr_len = 0;
   
           /* send UNSET request */
           pfd.fd = cli->cli_sock;
           pfd.events = POLLOUT;
           if ((ret = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) == -1 || 
                           pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
                   LOGERR;
                   return -1;
           }
           if (send(cli->cli_sock, &hdr, sizeof hdr, MSG_NOSIGNAL) == -1) {
                   LOGERR;
                   return -1;
           }
   
           /* wait for reply */
           pfd.events = POLLIN | POLLPRI;
           if ((ret = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) < 1 || 
                           pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
                   if (ret)
                           LOGERR;
                   else
                           rpc_SetErr(ETIMEDOUT, "Timeout reached! Server not respond");
                   return 1;
           }
           if (recv(cli->cli_sock, &hdr, sizeof hdr, 0) == -1) {
                   LOGERR;
                   return 1;
           }
   
           return hdr.hdr_cmd == error;
   }
   
   /*
    * rpc_cli_getBLOB() - Receive BLOB from server and Delete after that
    *
    * @cli = Client instance
    * @var = BLOB variable
    * @data = BLOB data, must be e_free after use!
    * return: -1 error, 0 ok, 1 remote error
    */
   int
   rpc_cli_getBLOB(rpc_cli_t * __restrict cli, ait_val_t * __restrict var, void ** __restrict data)
   {
           int ret;
   
           ret = rpc_cli_recvBLOB(cli, var, data);
           ret |= rpc_cli_delBLOB(cli, var) > 0 ? 2 : 0;
   
           return ret;
   }

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


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