Diff for /libaitrpc/src/blob.c between versions 1.4 and 1.4.4.5

version 1.4, 2011/09/07 07:24:21 version 1.4.4.5, 2012/03/15 01:24:34
Line 12  terms: Line 12  terms:
 All of the documentation and software included in the ELWIX and AITNET  All of the documentation and software included in the ELWIX and AITNET
 Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>  Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
   
Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
         by Michael Pounov <misho@elwix.org>.  All rights reserved.          by Michael Pounov <misho@elwix.org>.  All rights reserved.
   
 Redistribution and use in source and binary forms, with or without  Redistribution and use in source and binary forms, with or without
Line 47  SUCH DAMAGE. Line 47  SUCH DAMAGE.
   
   
 /*  /*
 * rpc_srv_blobCreate() Create map blob to memory region and return object * rpc_srv_blobCreate() - Create map blob to memory region and return object
  *
  * @srv = RPC Server instance   * @srv = RPC Server instance
  * @len = BLOB length object   * @len = BLOB length object
  * return: NULL error or !=NULL allocated BLOB object   * return: NULL error or !=NULL allocated BLOB object
Line 71  again: Line 72  again:
         rnd = random() % UINT_MAX;          rnd = random() % UINT_MAX;
   
         memset(szFName, 0, sizeof szFName);          memset(szFName, 0, sizeof szFName);
        snprintf(szFName, sizeof szFName, BLOB_FILE, srv->srv_blob.dir, rnd);        snprintf(szFName, sizeof szFName, BLOB_FILE, AIT_GET_STR(&srv->srv_blob.dir), rnd);
         f = open(szFName, O_CREAT | O_EXCL | O_RDWR, 0600);          f = open(szFName, O_CREAT | O_EXCL | O_RDWR, 0600);
         if (f == -1) {          if (f == -1) {
                 if (errno == EEXIST)                  if (errno == EEXIST)
Line 112  again: Line 113  again:
 }  }
   
 /*  /*
 * rpc_srv_blobMap() Map blob to memory region  * rpc_srv_blobMap() - Map blob to memory region 
  *
  * @srv = RPC Server instance   * @srv = RPC Server instance
  * @blob = Map to this BLOB element   * @blob = Map to this BLOB element
  * return: -1 error or 0 ok   * return: -1 error or 0 ok
Line 124  rpc_srv_blobMap(rpc_srv_t * __restrict srv, rpc_blob_t Line 126  rpc_srv_blobMap(rpc_srv_t * __restrict srv, rpc_blob_t
         char szFName[MAXPATHLEN];          char szFName[MAXPATHLEN];
   
         if (!blob) {          if (!blob) {
                rpc_SetErr(EINVAL, "Error:: invalid arguments ...\n");                rpc_SetErr(EINVAL, "Invalid argument BLOB");
                 return -1;                  return -1;
         }          }
         if (blob->blob_data) {          if (blob->blob_data) {
                rpc_SetErr(EPERM, "Error:: already mmapped object found!\n");                rpc_SetErr(EPERM, "Already mmapped object found!");
                 return -1;                  return -1;
         }          }
   
         memset(szFName, 0, sizeof szFName);          memset(szFName, 0, sizeof szFName);
        snprintf(szFName, sizeof szFName, BLOB_FILE, srv->srv_blob.dir, blob->blob_var);        snprintf(szFName, sizeof szFName, BLOB_FILE, AIT_GET_STR(&srv->srv_blob.dir), blob->blob_var);
         f = open(szFName, O_RDWR);          f = open(szFName, O_RDWR);
         if (f == -1) {          if (f == -1) {
                 LOGERR;                  LOGERR;
Line 156  rpc_srv_blobMap(rpc_srv_t * __restrict srv, rpc_blob_t Line 158  rpc_srv_blobMap(rpc_srv_t * __restrict srv, rpc_blob_t
 }  }
   
 /*  /*
 * rpc_srv_blobUnmap() Unmap blob memory region  * rpc_srv_blobUnmap() - Unmap blob memory region 
  *
  * @blob = Mapped BLOB element   * @blob = Mapped BLOB element
  * return: none   * return: none
  */   */
Line 164  inline void Line 167  inline void
 rpc_srv_blobUnmap(rpc_blob_t * __restrict blob)  rpc_srv_blobUnmap(rpc_blob_t * __restrict blob)
 {  {
         if (!blob || !blob->blob_data)          if (!blob || !blob->blob_data)
                rpc_SetErr(EINVAL, "Error:: invalid arguments ...\n");                rpc_SetErr(EINVAL, "Invalid arguments");
         else {          else {
                 munmap(blob->blob_data, blob->blob_len);                  munmap(blob->blob_data, blob->blob_len);
                 blob->blob_data = NULL;                  blob->blob_data = NULL;
Line 172  rpc_srv_blobUnmap(rpc_blob_t * __restrict blob) Line 175  rpc_srv_blobUnmap(rpc_blob_t * __restrict blob)
 }  }
   
 /*  /*
 * rpc_srv_blobFree() Free blob from disk & memory * rpc_srv_blobFree() - Free blob from disk & memory
  *
  * @srv = RPC Server instance   * @srv = RPC Server instance
  * @blob = Mapped BLOB element   * @blob = Mapped BLOB element
  * return: -1 error or 0 ok   * return: -1 error or 0 ok
Line 183  rpc_srv_blobFree(rpc_srv_t * __restrict srv, rpc_blob_ Line 187  rpc_srv_blobFree(rpc_srv_t * __restrict srv, rpc_blob_
         char szFName[MAXPATHLEN];          char szFName[MAXPATHLEN];
   
         if (!blob) {          if (!blob) {
                rpc_SetErr(EINVAL, "Error:: invalid arguments ...\n");                rpc_SetErr(EINVAL, "Invalid argument BLOB");
                 return -1;                  return -1;
         }          }
   
Line 191  rpc_srv_blobFree(rpc_srv_t * __restrict srv, rpc_blob_ Line 195  rpc_srv_blobFree(rpc_srv_t * __restrict srv, rpc_blob_
                 rpc_srv_blobUnmap(blob);                  rpc_srv_blobUnmap(blob);
   
         memset(szFName, 0, sizeof szFName);          memset(szFName, 0, sizeof szFName);
        snprintf(szFName, sizeof szFName, BLOB_FILE, srv->srv_blob.dir, blob->blob_var);        snprintf(szFName, sizeof szFName, BLOB_FILE, AIT_GET_STR(&srv->srv_blob.dir), blob->blob_var);
         if (unlink(szFName) == -1) {          if (unlink(szFName) == -1) {
                 LOGERR;                  LOGERR;
                 return -1;                  return -1;
Line 203  rpc_srv_blobFree(rpc_srv_t * __restrict srv, rpc_blob_ Line 207  rpc_srv_blobFree(rpc_srv_t * __restrict srv, rpc_blob_
 // ------------------------------------------------------------  // ------------------------------------------------------------
   
 /*  /*
 * rpc_srv_sendBLOB() Send mapped BLOB to client * rpc_srv_sendBLOB() - Send mapped BLOB to client
  *
  * @cli = Client instance   * @cli = Client instance
  * @blob = Mapped BLOB element   * @blob = Mapped BLOB element
  * return: -1 error, 0 ok   * return: -1 error, 0 ok
Line 215  rpc_srv_sendBLOB(rpc_cli_t * __restrict cli, rpc_blob_ Line 220  rpc_srv_sendBLOB(rpc_cli_t * __restrict cli, rpc_blob_
         uint8_t *pos;          uint8_t *pos;
   
         if (!cli || !blob || !blob->blob_data) {          if (!cli || !blob || !blob->blob_data) {
                rpc_SetErr(EINVAL, "Error:: invalid arguments ...\n");                rpc_SetErr(EINVAL, "Invalid arguments");
                 return -1;                  return -1;
         }          }
   
Line 231  rpc_srv_sendBLOB(rpc_cli_t * __restrict cli, rpc_blob_ Line 236  rpc_srv_sendBLOB(rpc_cli_t * __restrict cli, rpc_blob_
 }  }
   
 /*  /*
 * rpc_srv_recvBLOB() Receive BLOB from client * rpc_srv_recvBLOB() - Receive BLOB from client
  *
  * @cli = Client instance   * @cli = Client instance
  * @blob = Mapped BLOB element   * @blob = Mapped BLOB element
  * return: -1 error, 0 ok, >0 unreceived data from client, may be error?   * return: -1 error, 0 ok, >0 unreceived data from client, may be error?
Line 245  rpc_srv_recvBLOB(rpc_cli_t * __restrict cli, rpc_blob_ Line 251  rpc_srv_recvBLOB(rpc_cli_t * __restrict cli, rpc_blob_
         struct timeval tv = { DEF_RPC_TIMEOUT, 0 };          struct timeval tv = { DEF_RPC_TIMEOUT, 0 };
   
         if (!cli || !blob || !blob->blob_data) {          if (!cli || !blob || !blob->blob_data) {
                rpc_SetErr(EINVAL, "Error:: invalid arguments ...\n");                rpc_SetErr(EINVAL, "Invalid arguments");
                 return -1;                  return -1;
        }        } else
                 tv.tv_sec = ((rpc_sess_t*) cli->cli_parent)->sess_timeout;
   
         for (ret = blob->blob_len, pos = blob->blob_data; ret > 0; ret -= len, pos += len) {          for (ret = blob->blob_len, pos = blob->blob_data; ret > 0; ret -= len, pos += len) {
                 FD_ZERO(&fds);                  FD_ZERO(&fds);
Line 271  rpc_srv_recvBLOB(rpc_cli_t * __restrict cli, rpc_blob_ Line 278  rpc_srv_recvBLOB(rpc_cli_t * __restrict cli, rpc_blob_
 // ------------------------------------------------------------  // ------------------------------------------------------------
   
 /*  /*
 * rpc_cli_sendBLOB() Send BLOB to server * rpc_cli_sendBLOB() - Send BLOB to server
  *
  * @cli = Client instance   * @cli = Client instance
  * @var = BLOB variable   * @var = BLOB variable
  * @data = BLOB data   * @data = BLOB data
Line 287  rpc_cli_sendBLOB(rpc_cli_t * __restrict cli, ait_val_t Line 295  rpc_cli_sendBLOB(rpc_cli_t * __restrict cli, ait_val_t
         struct timeval tv = { DEF_RPC_TIMEOUT, 0 };          struct timeval tv = { DEF_RPC_TIMEOUT, 0 };
   
         if (!cli || !var || !data) {          if (!cli || !var || !data) {
                rpc_SetErr(EINVAL, "Error:: invalid arguments ...\n");                rpc_SetErr(EINVAL, "Invalid arguments");
                 return -1;                  return -1;
        }        } else
                 tv.tv_sec = ((rpc_sess_t*) cli->cli_parent)->sess_timeout;
   
        memcpy(&hdr.hdr_session, cli->cli_parent, sizeof(rpc_sess_t));        rpc_addPktSession(&hdr.hdr_session, cli->cli_parent);
         hdr.hdr_cmd = set;          hdr.hdr_cmd = set;
         hdr.hdr_var = 0;          hdr.hdr_var = 0;
         hdr.hdr_ret = 0;          hdr.hdr_ret = 0;
        hdr.hdr_len = AIT_LEN(var);        hdr.hdr_len = htonl(AIT_LEN(var));
         /* calculate CRC */
         hdr.hdr_crc ^= hdr.hdr_crc;
         hdr.hdr_crc = htons(crcFletcher16((u_short*) &hdr, io_align(sizeof hdr, 1) / 2));
 
         /* send SET request */
         if (send(cli->cli_sock, &hdr, sizeof hdr, 0) == -1) {          if (send(cli->cli_sock, &hdr, sizeof hdr, 0) == -1) {
                 LOGERR;                  LOGERR;
                 return -1;                  return -1;
Line 308  rpc_cli_sendBLOB(rpc_cli_t * __restrict cli, ait_val_t Line 322  rpc_cli_sendBLOB(rpc_cli_t * __restrict cli, ait_val_t
                         return -1;                          return -1;
                 }                  }
   
           /* wait for reply */
         FD_ZERO(&fds);          FD_ZERO(&fds);
         FD_SET(cli->cli_sock, &fds);          FD_SET(cli->cli_sock, &fds);
         switch (select(cli->cli_sock + 1, &fds, NULL, NULL, &tv)) {          switch (select(cli->cli_sock + 1, &fds, NULL, NULL, &tv)) {
                 case -1:                  case -1:
                         LOGERR;                          LOGERR;
                        return -1;                        return 1;
                 case 0:                  case 0:
                        rpc_SetErr(ETIMEDOUT, "Error:: Timeout reached! Server not responde ...\n");                        rpc_SetErr(ETIMEDOUT, "Timeout reached! Server not responde");
                        return -1;                        return 1;
         }          }
         if (recv(cli->cli_sock, &hdr, sizeof hdr, 0) == -1) {          if (recv(cli->cli_sock, &hdr, sizeof hdr, 0) == -1) {
                 LOGERR;                  LOGERR;
                return -1;                return 1;
         }          }
           /* check CRC */
           ret = ntohs(hdr.hdr_crc);
           hdr.hdr_crc ^= hdr.hdr_crc;
           if (ret != crcFletcher16((u_short*) &hdr, io_align(sizeof hdr, 1) / 2)) {
                   rpc_SetErr(ERPCMISMATCH, "Bad CRC BLOB packet");
                   return 1;
           }
   
         if (hdr.hdr_cmd != error) {          if (hdr.hdr_cmd != error) {
                if (hdr.hdr_len != AIT_LEN(var)) {                if (ntohl(hdr.hdr_len) != AIT_LEN(var)) {
                        rpc_SetErr(ECANCELED, "Error:: Bad return length packet ...\n");                        rpc_SetErr(ECANCELED, "Bad return length packet");
                        return -1;                        return 1;
                 }                  }
   
                var->val.blob = hdr.hdr_var;                var->val.blob = ntohl(hdr.hdr_var);
         }          }
   
         return hdr.hdr_cmd == error;          return hdr.hdr_cmd == error;
 }  }
   
 /*  /*
 * rpc_cli_recvBLOB() Receive BLOB from server * rpc_cli_recvBLOB() - Receive BLOB from server
  *
  * @cli = Client instance   * @cli = Client instance
  * @var = BLOB variable   * @var = BLOB variable
  * @data = BLOB data, must be free after use!   * @data = BLOB data, must be free after use!
Line 351  rpc_cli_recvBLOB(rpc_cli_t * __restrict cli, ait_val_t Line 375  rpc_cli_recvBLOB(rpc_cli_t * __restrict cli, ait_val_t
         struct tagBLOBHdr hdr;          struct tagBLOBHdr hdr;
   
         if (!cli || !var || !data) {          if (!cli || !var || !data) {
                rpc_SetErr(EINVAL, "Error:: invalid arguments ...\n");                rpc_SetErr(EINVAL, "Invalid arguments");
                 return -1;                  return -1;
        }        } else
                 tv.tv_sec = ((rpc_sess_t*) cli->cli_parent)->sess_timeout;
   
         *data = malloc(AIT_LEN(var));          *data = malloc(AIT_LEN(var));
         if (!*data) {          if (!*data) {
Line 362  rpc_cli_recvBLOB(rpc_cli_t * __restrict cli, ait_val_t Line 387  rpc_cli_recvBLOB(rpc_cli_t * __restrict cli, ait_val_t
         } else          } else
                 memset(*data, 0, AIT_LEN(var));                  memset(*data, 0, AIT_LEN(var));
   
        memcpy(&hdr.hdr_session, cli->cli_parent, sizeof(rpc_sess_t));        rpc_addPktSession(&hdr.hdr_session, cli->cli_parent);
         hdr.hdr_cmd = get;          hdr.hdr_cmd = get;
        hdr.hdr_var = (uint32_t) AIT_GET_BLOB(var);        hdr.hdr_var = htonl((uint32_t) AIT_GET_BLOB(var));
         hdr.hdr_ret = 0;          hdr.hdr_ret = 0;
         hdr.hdr_len = 0;          hdr.hdr_len = 0;
           /* calculate CRC */
           hdr.hdr_crc ^= hdr.hdr_crc;
           hdr.hdr_crc = htons(crcFletcher16((u_short*) &hdr, io_align(sizeof hdr, 1) / 2));
   
           /* send GET request */
         if (send(cli->cli_sock, &hdr, sizeof hdr, 0) == -1) {          if (send(cli->cli_sock, &hdr, sizeof hdr, 0) == -1) {
                 LOGERR;                  LOGERR;
                 free(*data);                  free(*data);
Line 394  rpc_cli_recvBLOB(rpc_cli_t * __restrict cli, ait_val_t Line 424  rpc_cli_recvBLOB(rpc_cli_t * __restrict cli, ait_val_t
                 }                  }
         }          }
   
           /* wait for reply */
         FD_ZERO(&fds);          FD_ZERO(&fds);
         FD_SET(cli->cli_sock, &fds);          FD_SET(cli->cli_sock, &fds);
         switch (select(cli->cli_sock + 1, &fds, NULL, NULL, &tv)) {          switch (select(cli->cli_sock + 1, &fds, NULL, NULL, &tv)) {
Line 401  rpc_cli_recvBLOB(rpc_cli_t * __restrict cli, ait_val_t Line 432  rpc_cli_recvBLOB(rpc_cli_t * __restrict cli, ait_val_t
                         LOGERR;                          LOGERR;
                         free(*data);                          free(*data);
                         *data = NULL;                          *data = NULL;
                        return -1;                        return 1;
                 case 0:                  case 0:
                        rpc_SetErr(ETIMEDOUT, "Error:: Timeout reached! Server not responde ...\n");                        rpc_SetErr(ETIMEDOUT, "Timeout reached! Server not responde");
                         free(*data);                          free(*data);
                         *data = NULL;                          *data = NULL;
                        return -1;                        return 1;
         }          }
         if (recv(cli->cli_sock, &hdr, sizeof hdr, 0) == -1) {          if (recv(cli->cli_sock, &hdr, sizeof hdr, 0) == -1) {
                 LOGERR;                  LOGERR;
                 free(*data);                  free(*data);
                 *data = NULL;                  *data = NULL;
                return -1;                return 1;
         }          }
           /* check CRC */
           ret = ntohs(hdr.hdr_crc);
           hdr.hdr_crc ^= hdr.hdr_crc;
           if (ret != crcFletcher16((u_short*) &hdr, io_align(sizeof hdr, 1) / 2)) {
                   rpc_SetErr(ERPCMISMATCH, "Bad CRC BLOB packet");
                   free(*data);
                   *data = NULL;
                   return 1;
           }
         if (hdr.hdr_cmd != error) {          if (hdr.hdr_cmd != error) {
                if (hdr.hdr_len != AIT_LEN(var)) {                if (ntohl(hdr.hdr_len) != AIT_LEN(var)) {
                        rpc_SetErr(ECANCELED, "Error:: Bad return length packet ...\n");                        rpc_SetErr(ECANCELED, "Bad return length packet");
                         free(*data);                          free(*data);
                         *data = NULL;                          *data = NULL;
                        return -1;                        return 1;
                 }                  }
         }          }
   
Line 427  rpc_cli_recvBLOB(rpc_cli_t * __restrict cli, ait_val_t Line 467  rpc_cli_recvBLOB(rpc_cli_t * __restrict cli, ait_val_t
 }  }
   
 /*  /*
 * rpc_cli_delBLOB() Delete BLOB from server * rpc_cli_delBLOB() - Delete BLOB from server
  *
  * @cli = Client instance   * @cli = Client instance
  * @var = BLOB variable   * @var = BLOB variable
  * return: -1 error, 0 ok, 1 remote error   * return: -1 error, 0 ok, 1 remote error
Line 438  rpc_cli_delBLOB(rpc_cli_t * __restrict cli, ait_val_t  Line 479  rpc_cli_delBLOB(rpc_cli_t * __restrict cli, ait_val_t 
         struct tagBLOBHdr hdr;          struct tagBLOBHdr hdr;
         fd_set fds;          fd_set fds;
         struct timeval tv = { DEF_RPC_TIMEOUT, 0 };          struct timeval tv = { DEF_RPC_TIMEOUT, 0 };
           int ret;
   
         if (!cli || !var) {          if (!cli || !var) {
                rpc_SetErr(EINVAL, "Error:: invalid arguments ...\n");                rpc_SetErr(EINVAL, "Invalid arguments");
                 return -1;                  return -1;
        }        } else
                 tv.tv_sec = ((rpc_sess_t*) cli->cli_parent)->sess_timeout;
   
        memcpy(&hdr.hdr_session, cli->cli_parent, sizeof(rpc_sess_t));        rpc_addPktSession(&hdr.hdr_session, cli->cli_parent);
         hdr.hdr_cmd = unset;          hdr.hdr_cmd = unset;
        hdr.hdr_var = (uint32_t) AIT_GET_BLOB(var);        hdr.hdr_var = htonl((uint32_t) AIT_GET_BLOB(var));
         hdr.hdr_ret = 0;          hdr.hdr_ret = 0;
         hdr.hdr_len = 0;          hdr.hdr_len = 0;
           /* calculate CRC */
           hdr.hdr_crc ^= hdr.hdr_crc;
           hdr.hdr_crc = htons(crcFletcher16((u_short*) &hdr, io_align(sizeof hdr, 1) / 2));
   
           /* send UNSET request */
         if (send(cli->cli_sock, &hdr, sizeof hdr, 0) == -1) {          if (send(cli->cli_sock, &hdr, sizeof hdr, 0) == -1) {
                 LOGERR;                  LOGERR;
                 return -1;                  return -1;
         }          }
   
           /* wait for reply */
         FD_ZERO(&fds);          FD_ZERO(&fds);
         FD_SET(cli->cli_sock, &fds);          FD_SET(cli->cli_sock, &fds);
         switch (select(cli->cli_sock + 1, &fds, NULL, NULL, &tv)) {          switch (select(cli->cli_sock + 1, &fds, NULL, NULL, &tv)) {
                 case -1:                  case -1:
                         LOGERR;                          LOGERR;
                        return -1;                        return 1;
                 case 0:                  case 0:
                        rpc_SetErr(ETIMEDOUT, "Error:: Timeout reached! Server not responde ...\n");                        rpc_SetErr(ETIMEDOUT, "Timeout reached! Server not responde");
                        return -1;                        return 1;
         }          }
         if (recv(cli->cli_sock, &hdr, sizeof hdr, 0) == -1) {          if (recv(cli->cli_sock, &hdr, sizeof hdr, 0) == -1) {
                 LOGERR;                  LOGERR;
                return -1;                return 1;
         }          }
           /* check CRC */
           ret = ntohs(hdr.hdr_crc);
           hdr.hdr_crc ^= hdr.hdr_crc;
           if (ret != crcFletcher16((u_short*) &hdr, io_align(sizeof hdr, 1) / 2)) {
                   rpc_SetErr(ERPCMISMATCH, "Bad CRC BLOB packet");
                   return 1;
           }
   
         return hdr.hdr_cmd == error;          return hdr.hdr_cmd == error;
 }  }
   
 /*  /*
 * rpc_cli_getBLOB() Receive BLOB from server and Delete after that * rpc_cli_getBLOB() - Receive BLOB from server and Delete after that
  *
  * @cli = Client instance   * @cli = Client instance
  * @var = BLOB variable   * @var = BLOB variable
  * @data = BLOB data, must be free after use!   * @data = BLOB data, must be free after use!

Removed from v.1.4  
changed lines
  Added in v.1.4.4.5


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