--- libaitrpc/src/srv.c 2012/05/17 11:01:30 1.9.2.20 +++ libaitrpc/src/srv.c 2012/05/17 15:18:48 1.9.2.24 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: srv.c,v 1.9.2.20 2012/05/17 11:01:30 misho Exp $ +* $Id: srv.c,v 1.9.2.24 2012/05/17 15:18:48 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -178,17 +178,20 @@ rxPacket(sched_task_t *task) { rpc_cli_t *c = TASK_ARG(task); rpc_srv_t *s = c->cli_parent; - u_char *buf = AIT_GET_BUF(&c->cli_buf); int len, rlen, noreply; - u_short crc, off = 0; + u_short crc, off = TASK_DATLEN(task); + u_char *buf = AIT_GET_BUF(&c->cli_buf); struct tagRPCCall *rpc; memset(buf, 0, AIT_LEN(&c->cli_buf)); - rlen = recv(TASK_FD(task), buf, AIT_LEN(&c->cli_buf), 0); + rlen = recv(TASK_FD(task), buf + off, AIT_LEN(&c->cli_buf) - off, 0); if (rlen < 1) { /* close connection */ schedEvent(TASK_ROOT(task), closeClient, c, 42, NULL, 0); return NULL; + } else { + rlen += off; /* add reminded bytes from previous rxPacket, if exists! */ + off = 0; /* process buffer from start offset == 0 */ } do { @@ -196,7 +199,9 @@ rxPacket(sched_task_t *task) if (rlen < sizeof(struct tagRPCCall)) { rpc_SetErr(ERPCMISMATCH, "Short RPC packet"); - schedReadSelf(task); + /* reminder received previous bytes ;) */ + schedRead(TASK_ROOT(task), TASK_FUNC(task), TASK_ARG(task), + TASK_FD(task), TASK_DATA(task), rlen); return NULL; } else rpc = (struct tagRPCCall*) (buf + off); @@ -204,17 +209,22 @@ rxPacket(sched_task_t *task) len = ntohs(rpc->call_len); rlen -= len; + /* check RPC packet lengths */ + if (rlen < 0 || len < sizeof(struct tagRPCCall)) { + rpc_SetErr(ERPCMISMATCH, "Broken RPC packet length"); + /* skip entire packet */ + break; + } + /* check integrity of packet */ crc = ntohs(rpc->call_crc); rpc->call_crc ^= rpc->call_crc; - if (crc != crcFletcher16((u_short*) (buf + off), len / 2)) { + if (crc != crcFletcher16((u_short*) rpc, len / 2)) { rpc_SetErr(ERPCMISMATCH, "Bad CRC RPC packet"); off += len; - if (rlen < 1) - break; - else - continue; + /* try next packet remaining into buffer */ + continue; } noreply = rpc->call_req.flags & RPC_NOREPLY; @@ -238,7 +248,7 @@ rxPacket(sched_task_t *task) } while (rlen > 0); /* lets get next packet */ - schedReadSelf(task); + schedRead(TASK_ROOT(task), TASK_FUNC(task), TASK_ARG(task), TASK_FD(task), TASK_DATA(task), 0); return NULL; } @@ -336,13 +346,12 @@ rxBLOB(sched_task_t *task) rpc_cli_t *c = TASK_ARG(task); rpc_srv_t *s = c->cli_parent; rpc_blob_t *b; - u_char *buf = AIT_GET_BUF(&c->cli_buf); - struct tagBLOBHdr *blob = (struct tagBLOBHdr *) buf; + struct tagBLOBHdr blob; int rlen; u_short crc; - memset(buf, 0, AIT_LEN(&c->cli_buf)); - rlen = recv(TASK_FD(task), buf, AIT_LEN(&c->cli_buf), 0); + memset(&blob, 0, sizeof blob); + rlen = recv(TASK_FD(task), &blob, sizeof blob, 0); if (rlen < 1) { /* close blob connection */ schedEvent(TASK_ROOT(task), closeBLOBClient, c, 42, NULL, 0); @@ -358,9 +367,9 @@ rxBLOB(sched_task_t *task) } /* check integrity of packet */ - crc = ntohs(blob->hdr_crc); - blob->hdr_crc ^= blob->hdr_crc; - if (crc != crcFletcher16((u_short*) buf, rlen / 2)) { + crc = ntohs(blob.hdr_crc); + blob.hdr_crc ^= blob.hdr_crc; + if (crc != crcFletcher16((u_short*) &blob, rlen / 2)) { rpc_SetErr(ERPCMISMATCH, "Bad CRC BLOB packet"); schedReadSelf(task); @@ -368,58 +377,59 @@ rxBLOB(sched_task_t *task) } /* check RPC packet session info */ - if ((crc = rpc_chkPktSession(&blob->hdr_session, &s->srv_session))) { + if ((crc = rpc_chkPktSession(&blob.hdr_session, &s->srv_session))) { rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session"); - blob->hdr_cmd = error; + blob.hdr_cmd = error; goto end; } /* Go to proceed packet ... */ - switch (blob->hdr_cmd) { + switch (blob.hdr_cmd) { case get: - if (!(b = rpc_srv_getBLOB(s, ntohl(blob->hdr_var)))) { - rpc_SetErr(EINVAL, "Var=%x not found", ntohl(blob->hdr_var)); - blob->hdr_cmd = no; - blob->hdr_ret = RPC_ERROR(-1); + if (!(b = rpc_srv_getBLOB(s, ntohl(blob.hdr_var)))) { + rpc_SetErr(EINVAL, "Var=%x not found", ntohl(blob.hdr_var)); + blob.hdr_cmd = no; + blob.hdr_ret = RPC_ERROR(-1); break; } else - blob->hdr_len = htonl(b->blob_len); + blob.hdr_len = htonl(b->blob_len); if (rpc_srv_blobMap(s, b) != -1) { /* deliver BLOB variable to client */ - blob->hdr_ret = htonl(rpc_srv_sendBLOB(c, b)); + blob.hdr_ret = htonl(rpc_srv_sendBLOB(c, b)); rpc_srv_blobUnmap(b); } else { - blob->hdr_cmd = error; - blob->hdr_ret = RPC_ERROR(-1); + blob.hdr_cmd = error; + blob.hdr_ret = RPC_ERROR(-1); } break; case set: - if ((b = rpc_srv_registerBLOB(s, ntohl(blob->hdr_len)))) { + if ((b = rpc_srv_registerBLOB(s, ntohl(blob.hdr_len)))) { /* set new BLOB variable for reply :) */ - blob->hdr_var = htonl(b->blob_var); + blob.hdr_var = htonl(b->blob_var); /* receive BLOB from client */ - blob->hdr_ret = htonl(rpc_srv_recvBLOB(c, b)); + blob.hdr_ret = htonl(rpc_srv_recvBLOB(c, b)); rpc_srv_blobUnmap(b); } else { - blob->hdr_cmd = error; - blob->hdr_ret = RPC_ERROR(-1); + blob.hdr_cmd = error; + blob.hdr_ret = RPC_ERROR(-1); } break; case unset: - if (rpc_srv_unregisterBLOB(s, blob->hdr_var) == -1) { - blob->hdr_cmd = error; - blob->hdr_ret = RPC_ERROR(-1); + if (rpc_srv_unregisterBLOB(s, blob.hdr_var) == -1) { + blob.hdr_cmd = error; + blob.hdr_ret = RPC_ERROR(-1); } break; default: - rpc_SetErr(EPROCUNAVAIL, "Unsupported BLOB command %d", blob->hdr_cmd); - blob->hdr_cmd = error; - blob->hdr_ret = RPC_ERROR(-1); + rpc_SetErr(EPROCUNAVAIL, "Unsupported BLOB command %d", blob.hdr_cmd); + blob.hdr_cmd = error; + blob.hdr_ret = RPC_ERROR(-1); } end: + memcpy(AIT_ADDR(&c->cli_buf), &blob, sizeof blob); schedWrite(TASK_ROOT(task), txBLOB, TASK_ARG(task), TASK_FD(task), NULL, 0); schedReadSelf(task); return NULL; @@ -592,8 +602,10 @@ rpc_srv_endBLOBServer(rpc_srv_t * __restrict srv) if (!srv) return; - pthread_cancel(srv->srv_blob.tid); - pthread_join(srv->srv_blob.tid, NULL); + if (srv->srv_blob.tid) { + pthread_cancel(srv->srv_blob.tid); + pthread_join(srv->srv_blob.tid, NULL); + } /* close all clients connections & server socket */ for (i = 0; i < io_arraySize(srv->srv_blob.clients); i++) { @@ -663,7 +675,7 @@ rpc_srv_loopBLOBServer(rpc_srv_t * __restrict srv) * @regProgID = ProgramID for authentication & recognition * @regProcID = ProcessID for authentication & recognition * @concurentClients = Concurent clients at same time to this server - * @netBuf = Network buffer length, if =0 == BUFSIZ (also meaning max RPC packet) + * @netBuf = Network buffer length (min:512 bytes), if =0 == BUFSIZ (also meaning max RPC packet) * @csHost = Host name or address for bind server, if NULL any address * @Port = Port for bind server, if Port == 0 default port is selected * return: NULL == error or !=NULL bind and created RPC server instance @@ -684,7 +696,7 @@ rpc_srv_initServer(u_int regProgID, u_char regProcID, return NULL; if (!Port) Port = RPC_DEFPORT; - if (!netBuf) + if (netBuf < RPC_MIN_BUFSIZ) netBuf = BUFSIZ; else netBuf = io_align(netBuf, 1); /* align netBuf length */