--- libaitrpc/src/blob.c 2011/09/03 12:58:49 1.3.2.2 +++ libaitrpc/src/blob.c 2012/11/13 09:22:10 1.10 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: blob.c,v 1.3.2.2 2011/09/03 12:58:49 misho Exp $ +* $Id: blob.c,v 1.10 2012/11/13 09:22:10 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, 2005, 2006, 2007, 2008, 2009, 2010, 2011 +Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -47,7 +47,8 @@ SUCH DAMAGE. /* - * rpc_srv_blobCreate() Create map blob to memory region and return object + * rpc_srv_blobCreate() - Create and map blob to memory region and return object + * * @srv = RPC Server instance * @len = BLOB length object * return: NULL error or !=NULL allocated BLOB object @@ -60,18 +61,11 @@ rpc_srv_blobCreate(rpc_srv_t * __restrict srv, int len int f; u_int rnd; -#ifdef HAVE_SRANDOMDEV - srandomdev(); -#else - time_t tim; - - srandom((time(&tim) ^ getpid())); -#endif again: rnd = random() % UINT_MAX; memset(szFName, 0, sizeof szFName); - snprintf(szFName, sizeof szFName, BLOB_FILE, srv->srv_blob.dir, rnd); + snprintf(szFName, sizeof szFName, BLOB_FILE, AIT_GET_STRZ(&srv->srv_blob.dir), rnd); f = open(szFName, O_CREAT | O_EXCL | O_RDWR, 0600); if (f == -1) { if (errno == EEXIST) @@ -80,15 +74,14 @@ again: LOGERR; return NULL; } - if (lseek(f, len - 1, SEEK_SET) == -1) { + if (ftruncate(f, len) == -1) { LOGERR; close(f); unlink(szFName); return NULL; - } else - write(f, "", 1); + } - blob = malloc(sizeof(rpc_blob_t)); + blob = io_malloc(sizeof(rpc_blob_t)); if (!blob) { LOGERR; close(f); @@ -99,7 +92,7 @@ again: blob->blob_data = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, f, 0); if (blob->blob_data == MAP_FAILED) { LOGERR; - free(blob); + io_free(blob); close(f); unlink(szFName); return NULL; @@ -112,7 +105,8 @@ again: } /* - * rpc_srv_blobMap() Map blob to memory region + * rpc_srv_blobMap() - Map blob to memory region + * * @srv = RPC Server instance * @blob = Map to this BLOB element * return: -1 error or 0 ok @@ -124,16 +118,16 @@ rpc_srv_blobMap(rpc_srv_t * __restrict srv, rpc_blob_t char szFName[MAXPATHLEN]; if (!blob) { - rpc_SetErr(EINVAL, "Error:: invalid arguments ...\n"); + rpc_SetErr(EINVAL, "Invalid argument BLOB"); return -1; } if (blob->blob_data) { - rpc_SetErr(EPERM, "Error:: already mmapped object found!\n"); + rpc_SetErr(EPERM, "Already mmapped object found!"); return -1; } 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_STRZ(&srv->srv_blob.dir), blob->blob_var); f = open(szFName, O_RDWR); if (f == -1) { LOGERR; @@ -156,23 +150,23 @@ 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 * return: none */ inline void rpc_srv_blobUnmap(rpc_blob_t * __restrict blob) { - if (!blob || !blob->blob_data) - rpc_SetErr(EINVAL, "Error:: invalid arguments ...\n"); - else { + if (blob && blob->blob_data) { munmap(blob->blob_data, blob->blob_len); blob->blob_data = NULL; } } /* - * rpc_srv_blobFree() Free blob from disk & memory + * rpc_srv_blobFree() - Free blob from disk & memory + * * @srv = RPC Server instance * @blob = Mapped BLOB element * return: -1 error or 0 ok @@ -183,15 +177,13 @@ rpc_srv_blobFree(rpc_srv_t * __restrict srv, rpc_blob_ char szFName[MAXPATHLEN]; if (!blob) { - rpc_SetErr(EINVAL, "Error:: invalid arguments ...\n"); + rpc_SetErr(EINVAL, "Invalid argument BLOB"); return -1; - } - - if (blob->blob_data) + } else rpc_srv_blobUnmap(blob); 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_STRZ(&srv->srv_blob.dir), blob->blob_var); if (unlink(szFName) == -1) { LOGERR; return -1; @@ -200,10 +192,11 @@ rpc_srv_blobFree(rpc_srv_t * __restrict srv, rpc_blob_ return 0; } -// ------------------------------------------------------------ +/* ------------------------------------------------------------ */ /* - * rpc_srv_sendBLOB() Send mapped BLOB to client + * rpc_srv_sendBLOB() - Send mapped BLOB to client + * * @cli = Client instance * @blob = Mapped BLOB element * return: -1 error, 0 ok @@ -215,12 +208,12 @@ rpc_srv_sendBLOB(rpc_cli_t * __restrict cli, rpc_blob_ uint8_t *pos; if (!cli || !blob || !blob->blob_data) { - rpc_SetErr(EINVAL, "Error:: invalid arguments ...\n"); + rpc_SetErr(EINVAL, "Invalid arguments"); return -1; } for (ret = blob->blob_len, pos = blob->blob_data; ret > 0; ret -= len, pos += len) { - len = send(cli->cli_sock, pos, ret, 0); + len = send(cli->cli_sock, pos, ret, MSG_NOSIGNAL); if (len == -1) { LOGERR; return -1; @@ -231,7 +224,8 @@ 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 * @blob = Mapped BLOB element * return: -1 error, 0 ok, >0 unreceived data from client, may be error? @@ -241,20 +235,22 @@ rpc_srv_recvBLOB(rpc_cli_t * __restrict cli, rpc_blob_ { int ret, len; uint8_t *pos; - fd_set fds; - struct timeval tv = { DEF_RPC_TIMEOUT, 0 }; + struct pollfd pfd; if (!cli || !blob || !blob->blob_data) { - rpc_SetErr(EINVAL, "Error:: invalid arguments ...\n"); + rpc_SetErr(EINVAL, "Invalid arguments"); return -1; } + pfd.fd = cli->cli_sock; + pfd.events = POLLIN | POLLPRI; for (ret = blob->blob_len, pos = blob->blob_data; ret > 0; ret -= len, pos += len) { - FD_ZERO(&fds); - FD_SET(cli->cli_sock, &fds); - len = select(cli->cli_sock + 1, &fds, NULL, NULL, &tv); - if (len < 1) { - LOGERR; + 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"); return -1; } @@ -268,10 +264,11 @@ rpc_srv_recvBLOB(rpc_cli_t * __restrict cli, rpc_blob_ return ret; } -// ------------------------------------------------------------ +/* ------------------------------------------------------------ */ /* - * rpc_cli_sendBLOB() Send BLOB to server + * rpc_cli_sendBLOB() - Send BLOB to server + * * @cli = Client instance * @var = BLOB variable * @data = BLOB data @@ -283,62 +280,83 @@ rpc_cli_sendBLOB(rpc_cli_t * __restrict cli, ait_val_t int ret, len; uint8_t *pos; struct tagBLOBHdr hdr; - fd_set fds; - struct timeval tv = { DEF_RPC_TIMEOUT, 0 }; + struct pollfd pfd; if (!cli || !var || !data) { - rpc_SetErr(EINVAL, "Error:: invalid arguments ...\n"); + rpc_SetErr(EINVAL, "Invalid arguments"); return -1; - } + } else + memset(&hdr, 0, sizeof hdr); - 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_var = 0; hdr.hdr_ret = 0; - hdr.hdr_len = AIT_LEN(var); - if (send(cli->cli_sock, &hdr, sizeof hdr, 0) == -1) { + hdr.hdr_len = htonl(AIT_LEN(var)); + /* calculate CRC */ + hdr.hdr_crc ^= hdr.hdr_crc; + hdr.hdr_crc = htons(crcFletcher16((u_short*) &hdr, sizeof hdr / 2)); + + /* 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, 0)) == -1) { + if ((len = send(cli->cli_sock, pos, ret, MSG_NOSIGNAL)) == -1) { LOGERR; return -1; } - FD_ZERO(&fds); - FD_SET(cli->cli_sock, &fds); - switch (select(cli->cli_sock + 1, &fds, NULL, NULL, &tv)) { - case -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; - return -1; - case 0: - rpc_SetErr(ETIMEDOUT, "Error:: Timeout reached! Server not responde ...\n"); - return -1; + 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 1; } + /* check CRC */ + ret = ntohs(hdr.hdr_crc); + hdr.hdr_crc ^= hdr.hdr_crc; + if (ret != crcFletcher16((u_short*) &hdr, sizeof hdr / 2)) { + rpc_SetErr(ERPCMISMATCH, "Bad CRC BLOB packet"); + return 1; + } + if (hdr.hdr_cmd != error) { - if (hdr.hdr_len != AIT_LEN(var)) { - rpc_SetErr(ECANCELED, "Error:: Bad return length packet ...\n"); - return -1; + if (ntohl(hdr.hdr_len) != AIT_LEN(var)) { + rpc_SetErr(ECANCELED, "Bad return length packet"); + return 1; } - var->val.blob = hdr.hdr_var; + AIT_SET_BLOB(var, ntohl(hdr.hdr_var), ntohl(hdr.hdr_len)); } return hdr.hdr_cmd == error; } /* - * rpc_cli_recvBLOB() Receive BLOB from server + * rpc_cli_recvBLOB() - Receive BLOB from server + * * @cli = Client instance * @var = BLOB variable - * @data = BLOB data, must be free after use! + * @data = BLOB data, must be io_free after use! * return: -1 error, 0 ok, 1 remote error */ int @@ -346,80 +364,100 @@ rpc_cli_recvBLOB(rpc_cli_t * __restrict cli, ait_val_t { int ret, len; uint8_t *pos; - fd_set fds; - struct timeval tv = { DEF_RPC_TIMEOUT, 0 }; + struct pollfd pfd; struct tagBLOBHdr hdr; if (!cli || !var || !data) { - rpc_SetErr(EINVAL, "Error:: invalid arguments ...\n"); + rpc_SetErr(EINVAL, "Invalid arguments"); return -1; } - *data = malloc(AIT_LEN(var)); + *data = io_malloc(AIT_LEN(var)); if (!*data) { LOGERR; return -1; - } else + } else { + memset(&hdr, 0, sizeof hdr); 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_var = (uint32_t) AIT_GET_BLOB(var); + hdr.hdr_var = htonl((uint32_t) AIT_GET_BLOB(var)); hdr.hdr_ret = 0; hdr.hdr_len = 0; + /* calculate CRC */ + hdr.hdr_crc ^= hdr.hdr_crc; + hdr.hdr_crc = htons(crcFletcher16((u_short*) &hdr, sizeof hdr / 2)); + + /* 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; + io_free(*data); + *data = NULL; + return -1; + } if (send(cli->cli_sock, &hdr, sizeof hdr, 0) == -1) { LOGERR; - free(*data); + io_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) { - FD_ZERO(&fds); - FD_SET(cli->cli_sock, &fds); - len = select(cli->cli_sock + 1, &fds, NULL, NULL, &tv); - if (len < 1) { + if ((len = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) < 1 || + pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) { LOGERR; - free(*data); + io_free(*data); *data = NULL; return -1; } if ((len = recv(cli->cli_sock, pos, ret, 0)) == -1) { LOGERR; - free(*data); + io_free(*data); *data = NULL; return -1; } } - FD_ZERO(&fds); - FD_SET(cli->cli_sock, &fds); - switch (select(cli->cli_sock + 1, &fds, NULL, NULL, &tv)) { - case -1: + /* wait for reply */ + if ((len = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) < 1 || + pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) { + if (len) LOGERR; - free(*data); - *data = NULL; - return -1; - case 0: - rpc_SetErr(ETIMEDOUT, "Error:: Timeout reached! Server not responde ...\n"); - free(*data); - *data = NULL; - return -1; + else + rpc_SetErr(ETIMEDOUT, "Timeout reached! Server not respond"); + io_free(*data); + *data = NULL; + return 1; } if (recv(cli->cli_sock, &hdr, sizeof hdr, 0) == -1) { LOGERR; - free(*data); + io_free(*data); *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, sizeof hdr / 2)) { + rpc_SetErr(ERPCMISMATCH, "Bad CRC BLOB packet"); + io_free(*data); + *data = NULL; + return 1; + } if (hdr.hdr_cmd != error) { - if (hdr.hdr_len != AIT_LEN(var)) { - rpc_SetErr(ECANCELED, "Error:: Bad return length packet ...\n"); - free(*data); + if (ntohl(hdr.hdr_len) != AIT_LEN(var)) { + rpc_SetErr(ECANCELED, "Bad return length packet"); + io_free(*data); *data = NULL; - return -1; + return 1; } } @@ -427,7 +465,8 @@ 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 * @var = BLOB variable * return: -1 error, 0 ok, 1 remote error @@ -436,47 +475,68 @@ int rpc_cli_delBLOB(rpc_cli_t * __restrict cli, ait_val_t * __restrict var) { struct tagBLOBHdr hdr; - fd_set fds; - struct timeval tv = { DEF_RPC_TIMEOUT, 0 }; + struct pollfd pfd; + int ret; if (!cli || !var) { - rpc_SetErr(EINVAL, "Error:: invalid arguments ...\n"); + rpc_SetErr(EINVAL, "Invalid arguments"); return -1; - } + } else + memset(&hdr, 0, sizeof hdr); - 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_var = (uint32_t) AIT_GET_BLOB(var); + hdr.hdr_var = htonl((uint32_t) AIT_GET_BLOB(var)); hdr.hdr_ret = 0; hdr.hdr_len = 0; - if (send(cli->cli_sock, &hdr, sizeof hdr, 0) == -1) { + /* calculate CRC */ + hdr.hdr_crc ^= hdr.hdr_crc; + hdr.hdr_crc = htons(crcFletcher16((u_short*) &hdr, sizeof hdr / 2)); + + /* 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; + } - FD_ZERO(&fds); - FD_SET(cli->cli_sock, &fds); - switch (select(cli->cli_sock + 1, &fds, NULL, NULL, &tv)) { - case -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; - return -1; - case 0: - rpc_SetErr(ETIMEDOUT, "Error:: Timeout reached! Server not responde ...\n"); - return -1; + 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 1; } + /* check CRC */ + ret = ntohs(hdr.hdr_crc); + hdr.hdr_crc ^= hdr.hdr_crc; + if (ret != crcFletcher16((u_short*) &hdr, sizeof hdr / 2)) { + rpc_SetErr(ERPCMISMATCH, "Bad CRC BLOB packet"); + return 1; + } 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 * @var = BLOB variable - * @data = BLOB data, must be free after use! + * @data = BLOB data, must be io_free after use! * return: -1 error, 0 ok, 1 remote error */ inline int