--- libaitrpc/src/srv.c 2015/06/24 22:44:24 1.26.2.3 +++ libaitrpc/src/srv.c 2015/06/29 17:05:27 1.26.2.7 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: srv.c,v 1.26.2.3 2015/06/24 22:44:24 misho Exp $ +* $Id: srv.c,v 1.26.2.7 2015/06/29 17:05:27 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -183,7 +183,6 @@ txPacket(sched_task_t *task) u_char *buf = AIT_GET_BUF(&c->cli_buf); struct tagRPCCall *rpc = (struct tagRPCCall*) buf; int ret, estlen, wlen = sizeof(struct tagRPCCall); - struct pollfd pfd; #ifdef TCP_SESSION_TIMEOUT struct timespec ts = { DEF_RPC_TIMEOUT, 0 }; @@ -212,8 +211,6 @@ txPacket(sched_task_t *task) /* Go Encapsulate variables */ ret = ait_vars2buffer(buf + wlen, AIT_LEN(&c->cli_buf) - wlen, RPC_RETVARS(c)); - /* Free return values */ - ait_freeVars(&c->cli_vars); if (ret == -1) { rpc_SetErr(EBADRPC, "Prepare RPC packet failed"); @@ -225,6 +222,9 @@ txPacket(sched_task_t *task) } } + /* Free return values */ + ait_freeVars(&c->cli_vars); + rpc->call_len = htonl(wlen); rpc->call_io = RPC_ACK; @@ -235,27 +235,12 @@ txPacket(sched_task_t *task) #endif /* send reply */ - pfd.fd = TASK_FD(task); - pfd.events = POLLOUT; - for (; wlen > 0; wlen -= ret, buf += ret) { - if ((ret = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) < 1 || - pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) { - if (ret) - LOGERR; - else - rpc_SetErr(ETIMEDOUT, "Timeout reached! Client not respond"); - /* close connection */ - schedEvent(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT], - TASK_ARG(task), 0, NULL, 0); - return NULL; - } - ret = send(TASK_FD(task), buf, MIN(wlen, s->srv_netbuf), MSG_NOSIGNAL); - if (ret == -1) { - /* close connection */ - schedEvent(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT], - TASK_ARG(task), 0, NULL, 0); - return NULL; - } + ret = send(TASK_FD(task), buf, MIN(wlen, s->srv_netbuf), MSG_NOSIGNAL); + if (ret == -1) { + /* close connection */ + schedEvent(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT], + TASK_ARG(task), 0, NULL, 0); + return NULL; } return NULL; @@ -303,6 +288,7 @@ execCall(sched_task_t *task) rpc->call_rep.eno = RPC_ERROR(rpc_Errno); } rpc->call_argc ^= rpc->call_argc; + ait_freeVars(&c->cli_vars); } else { rpc->call_rep.eno ^= rpc->call_rep.eno; if (argc) { @@ -476,7 +462,6 @@ txUDPPacket(sched_task_t *task) struct tagRPCCall *rpc = (struct tagRPCCall*) buf; int ret, estlen, wlen = sizeof(struct tagRPCCall); struct timespec ts = { DEF_RPC_TIMEOUT, 0 }; - struct pollfd pfd; schedCancelby(TASK_ROOT(task), taskTIMER, CRITERIA_DATA, TASK_ARG(task), NULL); schedTimer(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT], @@ -501,8 +486,6 @@ txUDPPacket(sched_task_t *task) /* Go Encapsulate variables */ ret = ait_vars2buffer(buf + wlen, AIT_LEN(&c->cli_buf) - wlen, RPC_RETVARS(c)); - /* Free return values */ - ait_freeVars(&c->cli_vars); if (ret == -1) { rpc_SetErr(EBADRPC, "Prepare RPC packet failed"); rpc->call_argc ^= rpc->call_argc; @@ -513,6 +496,9 @@ txUDPPacket(sched_task_t *task) } } + /* Free return values */ + ait_freeVars(&c->cli_vars); + rpc->call_len = htonl(wlen); rpc->call_io = RPC_ACK; @@ -521,28 +507,13 @@ txUDPPacket(sched_task_t *task) rpc->call_crc = htons(crcFletcher16((u_short*) buf, wlen / 2)); /* send reply */ - pfd.fd = TASK_FD(task); - pfd.events = POLLOUT; - for (; wlen > 0; wlen -= ret, buf += ret) { - if ((ret = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) < 1 || - pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) { - if (ret) - LOGERR; - else - rpc_SetErr(ETIMEDOUT, "Timeout reached! Client not respond"); - /* close connection */ - schedEvent(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT], - TASK_ARG(task), 0, NULL, 0); - return NULL; - } - ret = sendto(TASK_FD(task), buf, MIN(wlen, s->srv_netbuf), MSG_NOSIGNAL, - &c->cli_sa.sa, c->cli_sa.sa.sa_len); - if (ret == -1) { - /* close connection */ - schedEvent(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT], - TASK_ARG(task), 0, NULL, 0); - return NULL; - } + ret = sendto(TASK_FD(task), buf, MIN(wlen, s->srv_netbuf), MSG_NOSIGNAL, + &c->cli_sa.sa, c->cli_sa.sa.sa_len); + if (ret == -1) { + /* close connection */ + schedEvent(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT], + TASK_ARG(task), 0, NULL, 0); + return NULL; } return NULL; @@ -553,35 +524,39 @@ rxUDPPacket(sched_task_t *task) { rpc_srv_t *srv = TASK_ARG(task); rpc_cli_t *c = NULL; - int len, rlen, noreply, estlen; + int len, rlen, noreply; u_short crc; - u_char *buf, b[sizeof(struct tagRPCCall)]; - struct tagRPCCall *rpc = (struct tagRPCCall*) b; + struct tagRPCCall *rpc; sockaddr_t sa; socklen_t salen; struct timespec ts = { DEF_RPC_TIMEOUT, 0 }; - struct pollfd pfd; + ait_val_t b = AIT_VAL_INIT; /* receive connect packet */ + AIT_SET_BUF(&b, NULL, srv->srv_netbuf); salen = sa.ss.ss_len = sizeof(sockaddr_t); - rlen = recvfrom(TASK_FD(task), b, sizeof b, MSG_PEEK, &sa.sa, &salen); - if (rlen < sizeof(struct tagRPCCall)) { + rlen = recvfrom(TASK_FD(task), AIT_GET_BUF(&b), AIT_LEN(&b), 0, &sa.sa, &salen); + rpc = (struct tagRPCCall*) AIT_GET_BUF(&b); + if (rlen < sizeof(struct tagRPCCall) || rlen < ntohl(rpc->call_len)) { rpc_SetErr(ERPCMISMATCH, "Short RPC packet"); goto end; } + /* skip loop packet */ + if (rpc->call_io & RPC_ACK) + goto end; + c = _allocClient(srv, &sa); if (!c) { EVERBOSE(1, "RPC client quota exceeded! Connection will be shutdown!\n"); usleep(2000); /* blocked client delay */ goto end; } else { - estlen = ntohl(rpc->call_len); - if (estlen > AIT_LEN(&c->cli_buf)) - AIT_RE_BUF(&c->cli_buf, estlen); + len = ntohl(rpc->call_len); + if (len > AIT_LEN(&c->cli_buf)) + AIT_RE_BUF(&c->cli_buf, len); + memcpy(AIT_GET_BUF(&c->cli_buf), AIT_GET_BUF(&b), len); rpc = (struct tagRPCCall*) AIT_GET_BUF(&c->cli_buf); - buf = AIT_GET_BUF(&c->cli_buf); - len = estlen; c->cli_sock = TASK_FD(task); memcpy(&c->cli_sa, &sa, sizeof c->cli_sa); @@ -592,38 +567,6 @@ rxUDPPacket(sched_task_t *task) c, ts, c, 0); } - /* get next part of packet */ - memset(buf, 0, len); - pfd.fd = TASK_FD(task); - pfd.events = POLLIN | POLLPRI; - for (; len > 0; len -= rlen, buf += rlen) { - if ((rlen = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) < 1 || - pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) { - if (rlen) - LOGERR; - else - rpc_SetErr(ETIMEDOUT, "Timeout reached! Client not respond"); - schedEvent(TASK_ROOT(task), cbProto[srv->srv_proto][CB_CLOSECLIENT], - c, 0, NULL, 0); - goto end; - } - salen = sa.ss.ss_len = sizeof(sockaddr_t); - rlen = recvfrom(TASK_FD(task), buf, len, 0, &sa.sa, &salen); - if (rlen == -1) { - /* close connection */ - schedEvent(TASK_ROOT(task), cbProto[srv->srv_proto][CB_CLOSECLIENT], - c, 0, NULL, 0); - goto end; - } - if (e_addrcmp(&c->cli_sa, &sa, 42)) - rlen ^= rlen; /* skip if arrive from different address */ - } - len = estlen; - - /* skip loop packet */ - if (rpc->call_io & RPC_ACK) - goto end; - /* check integrity of packet */ crc = ntohs(rpc->call_crc); rpc->call_crc ^= rpc->call_crc; @@ -654,6 +597,7 @@ rxUDPPacket(sched_task_t *task) schedWrite(TASK_ROOT(task), cbProto[srv->srv_proto][CB_TXPACKET], c, TASK_FD(task), rpc, len); end: + AIT_FREE_VAL(&b); schedReadSelf(task); return NULL; } @@ -693,8 +637,6 @@ txRAWPacket(sched_task_t *task) /* Go Encapsulate variables */ ret = ait_vars2buffer(buf + wlen, AIT_LEN(&c->cli_buf) - wlen, RPC_RETVARS(c)); - /* Free return values */ - ait_freeVars(&c->cli_vars); if (ret == -1) { rpc_SetErr(EBADRPC, "Prepare RPC packet failed"); rpc->call_argc ^= rpc->call_argc; @@ -705,6 +647,9 @@ txRAWPacket(sched_task_t *task) } } + /* Free return values */ + ait_freeVars(&c->cli_vars); + rpc->call_len = htonl(wlen); rpc->call_io = RPC_ACK; @@ -764,7 +709,7 @@ rxRAWPacket(sched_task_t *task) (AIT_GET_BUF(&b) + sizeof(struct ip6_hdr)); } } - if (rlen < sizeof(struct tagRPCCall)) { + if (rlen < sizeof(struct tagRPCCall) || rlen < ntohl(rpc->call_len)) { rpc_SetErr(ERPCMISMATCH, "Short RPC packet"); goto end; } @@ -866,8 +811,6 @@ txBPFPacket(sched_task_t *task) /* Go Encapsulate variables */ ret = ait_vars2buffer(buf + wlen, AIT_LEN(&c->cli_buf) - wlen, RPC_RETVARS(c)); - /* Free return values */ - ait_freeVars(&RPC_RETVARS(c)); if (ret == -1) { rpc_SetErr(EBADRPC, "Prepare RPC packet failed"); rpc->call_argc ^= rpc->call_argc; @@ -878,6 +821,9 @@ txBPFPacket(sched_task_t *task) } } + /* Free return values */ + ait_freeVars(&RPC_RETVARS(c)); + rpc->call_len = htonl(wlen); rpc->call_io = RPC_ACK; @@ -1036,8 +982,6 @@ txEXTPacket(sched_task_t *task) /* Go Encapsulate variables */ ret = ait_vars2buffer(buf + wlen, AIT_LEN(&c->cli_buf) - wlen, RPC_RETVARS(c)); - /* Free return values */ - ait_freeVars(&RPC_RETVARS(c)); if (ret == -1) { rpc_SetErr(EBADRPC, "Prepare RPC packet failed"); rpc->call_argc ^= rpc->call_argc; @@ -1048,6 +992,9 @@ txEXTPacket(sched_task_t *task) } } + /* Free return values */ + ait_freeVars(&RPC_RETVARS(c)); + rpc->call_len = htonl(wlen); rpc->call_io = RPC_ACK; @@ -1078,17 +1025,16 @@ rxEXTPacket(sched_task_t *task) /* receive connect packet */ AIT_SET_BUF(&b, NULL, srv->srv_netbuf); rlen = read(TASK_FD(task), AIT_GET_BUF(&b), AIT_LEN(&b)); - if (rlen < sizeof(struct tagRPCCall)) { + rpc = (struct tagRPCCall*) AIT_GET_BUF(&b); + if (rlen < sizeof(struct tagRPCCall) || rlen < ntohl(rpc->call_len)) { rpc_SetErr(ERPCMISMATCH, "Short RPC packet"); goto end; - } else { - rpc = (struct tagRPCCall*) AIT_GET_BUF(&b); - - /* skip loop packet */ - if (rpc->call_io & RPC_ACK) - goto end; } + /* skip loop packet */ + if (rpc->call_io & RPC_ACK) + goto end; + c = _allocClient(srv, &sa); if (!c) { EVERBOSE(1, "RPC client quota exceeded! Connection will be shutdown!\n"); @@ -1478,6 +1424,9 @@ rpc_srv_endBLOBServer(rpc_srv_t * __restrict srv) srv->srv_blob.kill = 1; schedEnd(&srv->srv_blob.root); + + if (srv->srv_blob.server.cli_sa.sa.sa_family == AF_LOCAL) + unlink(srv->srv_blob.server.cli_sa.sun.sun_path); } /* @@ -1700,6 +1649,9 @@ rpc_srv_endServer(rpc_srv_t ** __restrict psrv) sleep(RPC_SCHED_POLLING); schedEnd(&(*psrv)->srv_root); + + if ((*psrv)->srv_server.cli_sa.sa.sa_family == AF_LOCAL) + unlink((*psrv)->srv_server.cli_sa.sun.sun_path); pthread_mutex_destroy(&(*psrv)->srv_funcs.mtx); e_free(*psrv);