--- libaitrpc/src/blob.c 2012/03/15 01:55:33 1.5 +++ libaitrpc/src/blob.c 2012/05/19 00:29:51 1.8 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: blob.c,v 1.5 2012/03/15 01:55:33 misho Exp $ +* $Id: blob.c,v 1.8 2012/05/19 00:29:51 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -47,7 +47,7 @@ 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 @@ -61,13 +61,6 @@ 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; @@ -81,13 +74,12 @@ 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)); if (!blob) { @@ -166,9 +158,7 @@ rpc_srv_blobMap(rpc_srv_t * __restrict srv, rpc_blob_t inline void rpc_srv_blobUnmap(rpc_blob_t * __restrict blob) { - if (!blob || !blob->blob_data) - rpc_SetErr(EINVAL, "Invalid arguments"); - else { + if (blob && blob->blob_data) { munmap(blob->blob_data, blob->blob_len); blob->blob_data = NULL; } @@ -189,9 +179,7 @@ rpc_srv_blobFree(rpc_srv_t * __restrict srv, rpc_blob_ if (!blob) { rpc_SetErr(EINVAL, "Invalid argument BLOB"); return -1; - } - - if (blob->blob_data) + } else rpc_srv_blobUnmap(blob); memset(szFName, 0, sizeof szFName); @@ -204,7 +192,7 @@ rpc_srv_blobFree(rpc_srv_t * __restrict srv, rpc_blob_ return 0; } -// ------------------------------------------------------------ +/* ------------------------------------------------------------ */ /* * rpc_srv_sendBLOB() - Send mapped BLOB to client @@ -225,7 +213,7 @@ rpc_srv_sendBLOB(rpc_cli_t * __restrict cli, rpc_blob_ } 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; @@ -247,21 +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, "Invalid arguments"); return -1; - } else - tv.tv_sec = ((rpc_sess_t*) cli->cli_parent)->sess_timeout; + } + 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; } @@ -275,7 +264,7 @@ rpc_srv_recvBLOB(rpc_cli_t * __restrict cli, rpc_blob_ return ret; } -// ------------------------------------------------------------ +/* ------------------------------------------------------------ */ /* * rpc_cli_sendBLOB() - Send BLOB to server @@ -291,14 +280,13 @@ 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, "Invalid arguments"); return -1; } else - tv.tv_sec = ((rpc_sess_t*) cli->cli_parent)->sess_timeout; + memset(&hdr, 0, sizeof hdr); rpc_addPktSession(&hdr.hdr_session, cli->cli_parent); hdr.hdr_cmd = set; @@ -307,31 +295,37 @@ rpc_cli_sendBLOB(rpc_cli_t * __restrict cli, ait_val_t 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)); + hdr.hdr_crc = htons(crcFletcher16((u_short*) &hdr, sizeof hdr / 2)); /* send SET request */ - if (send(cli->cli_sock, &hdr, sizeof hdr, 0) == -1) { + 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; } /* wait for reply */ - FD_ZERO(&fds); - FD_SET(cli->cli_sock, &fds); - switch (select(cli->cli_sock + 1, &fds, NULL, NULL, &tv)) { - case -1: + 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, "Timeout reached! Server not responde"); - return 1; + else + rpc_SetErr(ETIMEDOUT, "Timeout reached! Server not respond"); + return -1; } if (recv(cli->cli_sock, &hdr, sizeof hdr, 0) == -1) { LOGERR; @@ -340,7 +334,7 @@ rpc_cli_sendBLOB(rpc_cli_t * __restrict cli, ait_val_t /* 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)) { + if (ret != crcFletcher16((u_short*) &hdr, sizeof hdr / 2)) { rpc_SetErr(ERPCMISMATCH, "Bad CRC BLOB packet"); return 1; } @@ -351,7 +345,7 @@ rpc_cli_sendBLOB(rpc_cli_t * __restrict cli, ait_val_t return 1; } - var->val.blob = ntohl(hdr.hdr_var); + AIT_SET_BLOB(var, ntohl(hdr.hdr_var), ntohl(hdr.hdr_len)); } return hdr.hdr_cmd == error; @@ -370,22 +364,22 @@ 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, "Invalid arguments"); return -1; - } else - tv.tv_sec = ((rpc_sess_t*) cli->cli_parent)->sess_timeout; + } *data = malloc(AIT_LEN(var)); if (!*data) { LOGERR; return -1; - } else + } else { + memset(&hdr, 0, sizeof hdr); memset(*data, 0, AIT_LEN(var)); + } rpc_addPktSession(&hdr.hdr_session, cli->cli_parent); hdr.hdr_cmd = get; @@ -394,9 +388,18 @@ rpc_cli_recvBLOB(rpc_cli_t * __restrict cli, ait_val_t 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)); + 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; + free(*data); + *data = NULL; + return -1; + } if (send(cli->cli_sock, &hdr, sizeof hdr, 0) == -1) { LOGERR; free(*data); @@ -405,11 +408,10 @@ rpc_cli_recvBLOB(rpc_cli_t * __restrict cli, ait_val_t } /* 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); *data = NULL; @@ -425,19 +427,15 @@ rpc_cli_recvBLOB(rpc_cli_t * __restrict cli, ait_val_t } /* wait for reply */ - FD_ZERO(&fds); - FD_SET(cli->cli_sock, &fds); - switch (select(cli->cli_sock + 1, &fds, NULL, NULL, &tv)) { - case -1: + 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, "Timeout reached! Server not responde"); - free(*data); - *data = NULL; - return 1; + else + rpc_SetErr(ETIMEDOUT, "Timeout reached! Server not respond"); + free(*data); + *data = NULL; + return 1; } if (recv(cli->cli_sock, &hdr, sizeof hdr, 0) == -1) { LOGERR; @@ -448,7 +446,7 @@ rpc_cli_recvBLOB(rpc_cli_t * __restrict cli, ait_val_t /* 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)) { + if (ret != crcFletcher16((u_short*) &hdr, sizeof hdr / 2)) { rpc_SetErr(ERPCMISMATCH, "Bad CRC BLOB packet"); free(*data); *data = NULL; @@ -477,15 +475,14 @@ 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, "Invalid arguments"); return -1; } else - tv.tv_sec = ((rpc_sess_t*) cli->cli_parent)->sess_timeout; + memset(&hdr, 0, sizeof hdr); rpc_addPktSession(&hdr.hdr_session, cli->cli_parent); hdr.hdr_cmd = unset; @@ -494,24 +491,30 @@ rpc_cli_delBLOB(rpc_cli_t * __restrict cli, ait_val_t 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)); + hdr.hdr_crc = htons(crcFletcher16((u_short*) &hdr, sizeof hdr / 2)); /* send UNSET request */ - if (send(cli->cli_sock, &hdr, sizeof hdr, 0) == -1) { + 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 */ - FD_ZERO(&fds); - FD_SET(cli->cli_sock, &fds); - switch (select(cli->cli_sock + 1, &fds, NULL, NULL, &tv)) { - case -1: + 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, "Timeout reached! Server not responde"); - return 1; + else + rpc_SetErr(ETIMEDOUT, "Timeout reached! Server not respond"); + return 1; } if (recv(cli->cli_sock, &hdr, sizeof hdr, 0) == -1) { LOGERR; @@ -520,7 +523,7 @@ rpc_cli_delBLOB(rpc_cli_t * __restrict cli, ait_val_t /* 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)) { + if (ret != crcFletcher16((u_short*) &hdr, sizeof hdr / 2)) { rpc_SetErr(ERPCMISMATCH, "Bad CRC BLOB packet"); return 1; }