--- libaitrpc/src/srv.c 2015/06/28 21:40:46 1.26.2.6 +++ libaitrpc/src/srv.c 2015/07/02 21:52:29 1.26.2.12 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: srv.c,v 1.26.2.6 2015/06/28 21:40:46 misho Exp $ +* $Id: srv.c,v 1.26.2.12 2015/07/02 21:52:29 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -70,19 +70,41 @@ static void *rxEXTPacket(sched_task_t *); static void *txEXTPacket(sched_task_t *); static sched_task_func_t cbProto[SOCK_MAX_SUPPORT][4] = { - { acceptClients, closeClient, rxPacket, txPacket }, /* SOCK_STREAM */ - { acceptClients, closeClient, rxPacket, txPacket }, /* SOCK_STREAM */ - { rxUDPPacket, freeClient, rxUDPPacket, txUDPPacket }, /* SOCK_DGRAM */ - { rxRAWPacket, freeClient, rxRAWPacket, txRAWPacket }, /* SOCK_RAW */ - { rxBPFPacket, freeClient, rxBPFPacket, txBPFPacket }, /* SOCK_BPF */ - { rxEXTPacket, freeClient, rxEXTPacket, txEXTPacket } /* SOCK_EXT */ + { acceptClients, closeClient, rxPacket, txPacket }, /* SOCK_STREAM */ + { acceptClients, closeClient, rxPacket, txPacket }, /* SOCK_STREAM */ + { rxUDPPacket, freeClient, NULL /*rxUDPPacket*/, txUDPPacket }, /* SOCK_DGRAM */ + { rxRAWPacket, freeClient, NULL /*rxRAWPacket*/, txRAWPacket }, /* SOCK_RAW */ + { rxBPFPacket, freeClient, NULL /*rxBPFPacket*/, txBPFPacket }, /* SOCK_BPF */ + { rxEXTPacket, freeClient, NULL /*rxEXTPacket*/, txEXTPacket } /* SOCK_EXT */ }; /* Global Signal Argument when kqueue support disabled */ static volatile uintptr_t _glSigArg = 0; +#pragma GCC visibility push(hidden) +static int +rpc_funcs_cmp(struct tagRPCFunc *a, struct tagRPCFunc *b) +{ + int ret; + + assert(a && b); + + ret = AIT_KEY(&a->func_name) - AIT_KEY(&b->func_name); + + if (ret < 0) + return -1; + else if (ret > 0) + return 1; + + return ret; +} + +AVL_GENERATE(tagRPCFuncs, tagRPCFunc, func_node, rpc_funcs_cmp); + +#pragma GCC visibility pop + void rpc_freeCli(rpc_cli_t * __restrict c) { @@ -158,7 +180,7 @@ freeClient(sched_task_t *task) { rpc_freeCli(TASK_ARG(task)); - return NULL; + taskExit(task, NULL); } static void * @@ -171,7 +193,7 @@ closeClient(sched_task_t *task) /* close client socket */ shutdown(sock, SHUT_RDWR); close(sock); - return NULL; + taskExit(task, NULL); } static void * @@ -182,8 +204,7 @@ txPacket(sched_task_t *task) rpc_func_t *f = NULL; 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; + int ret, wlen = sizeof(struct tagRPCCall); #ifdef TCP_SESSION_TIMEOUT struct timespec ts = { DEF_RPC_TIMEOUT, 0 }; @@ -198,16 +219,15 @@ txPacket(sched_task_t *task) rpc_SetErr(EPROGUNAVAIL, "Function not found at RPC server"); rpc->call_argc ^= rpc->call_argc; - rpc->call_rep.ret = RPC_ERROR(-1); - rpc->call_rep.eno = RPC_ERROR(rpc_Errno); - } else { - /* calc estimated length */ - estlen = ait_resideVars(RPC_RETVARS(c)) + wlen; - if (estlen > AIT_LEN(&c->cli_buf)) - AIT_RE_BUF(&c->cli_buf, estlen); - buf = AIT_GET_BUF(&c->cli_buf); - rpc = (struct tagRPCCall*) buf; + RPC_SET_RETURN(rpc, -1); + RPC_SET_ERRNO(rpc, rpc_Errno); + } else if (rpc_pktFreeSpace(c) > s->srv_netbuf) { + rpc_SetErr(EMSGSIZE, "Message too long"); + rpc->call_argc ^= rpc->call_argc; + RPC_SET_RETURN(rpc, -1); + RPC_SET_ERRNO(rpc, rpc_Errno); + } else { rpc->call_argc = (u_char) array_Size(RPC_RETVARS(c)); /* Go Encapsulate variables */ ret = ait_vars2buffer(buf + wlen, AIT_LEN(&c->cli_buf) - wlen, @@ -216,8 +236,8 @@ txPacket(sched_task_t *task) rpc_SetErr(EBADRPC, "Prepare RPC packet failed"); rpc->call_argc ^= rpc->call_argc; - rpc->call_rep.ret = RPC_ERROR(-1); - rpc->call_rep.eno = RPC_ERROR(rpc_Errno); + RPC_SET_RETURN(rpc, -1); + RPC_SET_ERRNO(rpc, rpc_Errno); } else wlen += ret; } @@ -236,30 +256,14 @@ 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, wlen, 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; + taskExit(task, NULL); } static void * @@ -281,9 +285,9 @@ execCall(sched_task_t *task) rpc_SetErr(ERPCMISMATCH, "#%d - %s", elwix_GetErrno(), elwix_GetError()); rpc->call_argc ^= rpc->call_argc; - rpc->call_rep.ret = RPC_ERROR(-1); - rpc->call_rep.eno = RPC_ERROR(rpc_Errno); - return NULL; + RPC_SET_RETURN(rpc, -1); + RPC_SET_ERRNO(rpc, rpc_Errno); + taskExit(task, NULL); } } else arr = NULL; @@ -292,25 +296,24 @@ execCall(sched_task_t *task) rpc_SetErr(EPROGUNAVAIL, "Function not found at RPC server"); rpc->call_argc ^= rpc->call_argc; - rpc->call_rep.ret = RPC_ERROR(-1); - rpc->call_rep.eno = RPC_ERROR(rpc_Errno); + RPC_SET_RETURN(rpc, -1); + RPC_SET_ERRNO(rpc, rpc_Errno); } else { /* if client doesn't want reply */ - argc = RPC_CHK_NOREPLY(rpc); - rpc->call_rep.ret = RPC_ERROR(rpc_srv_execCall(c, rpc, f->func_name, arr)); + RPC_SET_RETURN(rpc, rpc_srv_execCall(c, rpc, f->func_name, arr)); if (rpc->call_rep.ret == htonl(-1)) { if (!rpc->call_rep.eno) { LOGERR; - rpc->call_rep.eno = RPC_ERROR(rpc_Errno); + RPC_SET_ERRNO(rpc, rpc_Errno); } rpc->call_argc ^= rpc->call_argc; ait_freeVars(&c->cli_vars); } else { rpc->call_rep.eno ^= rpc->call_rep.eno; - if (argc) { + rpc->call_argc ^= rpc->call_argc; + if (TASK_VAL(task)) { /* without reply */ ait_freeVars(&c->cli_vars); - rpc->call_argc ^= rpc->call_argc; } else { /* reply */ rpc->call_argc = (u_char) array_Size(RPC_RETVARS(c)); @@ -319,7 +322,7 @@ execCall(sched_task_t *task) } array_Destroy(&arr); - return NULL; + taskExit(task, NULL); } static void * @@ -327,13 +330,13 @@ rxPacket(sched_task_t *task) { rpc_cli_t *c = TASK_ARG(task); rpc_srv_t *s = c->cli_parent; - int len, rlen, noreply, estlen; + int len, noreply = 0, rlen = AIT_LEN(&c->cli_buf); #if 0 u_short crc; #endif u_char *buf = AIT_GET_BUF(&c->cli_buf); + u_char b[sizeof(struct tagRPCCall)]; struct tagRPCCall *rpc = (struct tagRPCCall*) buf; - struct pollfd pfd; #ifdef TCP_SESSION_TIMEOUT struct timespec ts = { DEF_RPC_TIMEOUT, 0 }; @@ -342,51 +345,40 @@ rxPacket(sched_task_t *task) TASK_ARG(task), ts, TASK_ARG(task), 0); #endif - memset(buf, 0, sizeof(struct tagRPCCall)); - rlen = recv(TASK_FD(task), rpc, sizeof(struct tagRPCCall), MSG_PEEK); - if (rlen < sizeof(struct tagRPCCall)) { + /* prepare rx */ + len = recv(TASK_FD(task), b, sizeof b, MSG_PEEK); + if (len == sizeof b) + rlen = ntohl(((struct tagRPCCall*) b)->call_len); + + rlen = recv(TASK_FD(task), buf, rlen, 0); + if (rlen == -1) { /* close connection */ schedEvent(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT], TASK_ARG(task), 0, NULL, 0); - return NULL; - } else { - estlen = ntohl(rpc->call_len); - if (estlen > AIT_LEN(&c->cli_buf)) - AIT_RE_BUF(&c->cli_buf, estlen); - rpc = (struct tagRPCCall*) AIT_GET_BUF(&c->cli_buf); - buf = AIT_GET_BUF(&c->cli_buf); - len = estlen; + taskExit(task, NULL); } + if (rlen < sizeof(struct tagRPCCall)) { + rpc_SetErr(ERPCMISMATCH, "Short RPC packet"); - /* 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[s->srv_proto][CB_CLOSECLIENT], - TASK_ARG(task), 0, NULL, 0); - return NULL; - } - rlen = recv(TASK_FD(task), buf, len, 0); - if (rlen == -1) { - /* close connection */ - schedEvent(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT], - TASK_ARG(task), 0, NULL, 0); - return NULL; - } + rpc->call_argc ^= rpc->call_argc; + RPC_SET_RETURN(rpc, -1); + RPC_SET_ERRNO(rpc, rpc_Errno); + goto err; + } else + len = ntohl(rpc->call_len); + if (rlen < len || len > AIT_LEN(&c->cli_buf)) { + rpc_SetErr(ERPCMISMATCH, "Short RPC packet"); + + rpc->call_argc ^= rpc->call_argc; + RPC_SET_RETURN(rpc, -1); + RPC_SET_ERRNO(rpc, rpc_Errno); + goto err; } - len = estlen; /* skip loop packet */ if (rpc->call_io & RPC_ACK) { schedReadSelf(task); - return NULL; + taskExit(task, NULL); } #if 0 @@ -395,24 +387,29 @@ rxPacket(sched_task_t *task) rpc->call_crc ^= rpc->call_crc; if (crc != crcFletcher16((u_short*) rpc, len / 2)) { rpc_SetErr(ERPCMISMATCH, "Bad CRC RPC packet"); - return NULL; + + rpc->call_argc ^= rpc->call_argc; + RPC_SET_RETURN(rpc, -1); + RPC_SET_ERRNO(rpc, rpc_Errno); + goto err; } #endif - noreply = RPC_CHK_NOREPLY(rpc); - /* check RPC packet session info */ if (rpc_chkPktSession(&rpc->call_session, &s->srv_session)) { rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session"); rpc->call_argc ^= rpc->call_argc; - rpc->call_rep.ret = RPC_ERROR(-1); - rpc->call_rep.eno = RPC_ERROR(errno); - } else { - /* execute RPC call */ - schedEvent(TASK_ROOT(task), execCall, TASK_ARG(task), (int) noreply, rpc, len); + RPC_SET_RETURN(rpc, -1); + RPC_SET_ERRNO(rpc, rpc_Errno); + goto err; } + noreply = RPC_CHK_NOREPLY(rpc); + + /* execute RPC call */ + schedEvent(TASK_ROOT(task), execCall, TASK_ARG(task), (int) noreply, rpc, len); +err: /* send RPC reply */ if (!noreply) schedWrite(TASK_ROOT(task), cbProto[s->srv_proto][CB_TXPACKET], @@ -420,7 +417,7 @@ rxPacket(sched_task_t *task) /* lets get next packet */ schedReadSelf(task); - return NULL; + taskExit(task, NULL); } static void * @@ -464,7 +461,7 @@ acceptClients(sched_task_t *task) c->cli_sock, NULL, 0); end: schedReadSelf(task); - return NULL; + taskExit(task, NULL); } @@ -476,7 +473,7 @@ txUDPPacket(sched_task_t *task) rpc_func_t *f = NULL; u_char *buf = AIT_GET_BUF(&c->cli_buf); struct tagRPCCall *rpc = (struct tagRPCCall*) buf; - int ret, estlen, wlen = sizeof(struct tagRPCCall); + int ret, wlen = sizeof(struct tagRPCCall); struct timespec ts = { DEF_RPC_TIMEOUT, 0 }; schedCancelby(TASK_ROOT(task), taskTIMER, CRITERIA_DATA, TASK_ARG(task), NULL); @@ -487,26 +484,27 @@ txUDPPacket(sched_task_t *task) f = rpc_srv_getCall(s, ntohs(rpc->call_tag)); if (!f) { rpc_SetErr(EPROGUNAVAIL, "Function not found at RPC server"); + rpc->call_argc ^= rpc->call_argc; - rpc->call_rep.ret = RPC_ERROR(-1); - rpc->call_rep.eno = RPC_ERROR(rpc_Errno); - } else { - /* calc estimated length */ - estlen = ait_resideVars(RPC_RETVARS(c)) + wlen; - if (estlen > AIT_LEN(&c->cli_buf)) - AIT_RE_BUF(&c->cli_buf, estlen); - buf = AIT_GET_BUF(&c->cli_buf); - rpc = (struct tagRPCCall*) buf; + RPC_SET_RETURN(rpc, -1); + RPC_SET_ERRNO(rpc, rpc_Errno); + } else if (rpc_pktFreeSpace(c) > s->srv_netbuf) { + rpc_SetErr(EMSGSIZE, "Message too long"); + rpc->call_argc ^= rpc->call_argc; + RPC_SET_RETURN(rpc, -1); + RPC_SET_ERRNO(rpc, rpc_Errno); + } else { rpc->call_argc = (u_char) array_Size(RPC_RETVARS(c)); /* Go Encapsulate variables */ ret = ait_vars2buffer(buf + wlen, AIT_LEN(&c->cli_buf) - wlen, RPC_RETVARS(c)); if (ret == -1) { rpc_SetErr(EBADRPC, "Prepare RPC packet failed"); + rpc->call_argc ^= rpc->call_argc; - rpc->call_rep.ret = RPC_ERROR(-1); - rpc->call_rep.eno = RPC_ERROR(rpc_Errno); + RPC_SET_RETURN(rpc, -1); + RPC_SET_ERRNO(rpc, rpc_Errno); } else wlen += ret; } @@ -523,16 +521,15 @@ txUDPPacket(sched_task_t *task) rpc->call_crc = htons(crcFletcher16((u_short*) buf, wlen / 2)); /* send reply */ - ret = sendto(TASK_FD(task), buf, MIN(wlen, s->srv_netbuf), MSG_NOSIGNAL, + ret = sendto(TASK_FD(task), buf, wlen, 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; + taskExit(task, NULL); } static void * @@ -540,7 +537,7 @@ rxUDPPacket(sched_task_t *task) { rpc_srv_t *srv = TASK_ARG(task); rpc_cli_t *c = NULL; - int len, rlen, noreply; + int len, noreply = 0, rlen; u_short crc; struct tagRPCCall *rpc; sockaddr_t sa; @@ -553,24 +550,33 @@ rxUDPPacket(sched_task_t *task) salen = sa.ss.ss_len = sizeof(sockaddr_t); 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"); + if (rlen < sizeof(struct tagRPCCall)) goto end; - } + else + len = ntohl(rpc->call_len); + if (rlen < len || len > srv->srv_netbuf) + goto end; /* 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; + if (crc != crcFletcher16((u_short*) rpc, len / 2)) + goto end; + + /* check RPC packet session info */ + if (rpc_chkPktSession(&rpc->call_session, &srv->srv_session)) + 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 { - 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); @@ -583,31 +589,11 @@ rxUDPPacket(sched_task_t *task) c, ts, c, 0); } - /* check integrity of packet */ - crc = ntohs(rpc->call_crc); - rpc->call_crc ^= rpc->call_crc; - if (crc != crcFletcher16((u_short*) rpc, len / 2)) { - rpc_SetErr(ERPCMISMATCH, "Bad CRC RPC packet"); - /* close connection */ - schedEvent(TASK_ROOT(task), cbProto[srv->srv_proto][CB_CLOSECLIENT], - c, 0, NULL, 0); - goto end; - } - noreply = RPC_CHK_NOREPLY(rpc); - /* check RPC packet session info */ - if (rpc_chkPktSession(&rpc->call_session, &srv->srv_session)) { - rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session"); + /* execute RPC call */ + schedEvent(TASK_ROOT(task), execCall, c, (int) noreply, rpc, len); - rpc->call_argc ^= rpc->call_argc; - rpc->call_rep.ret = RPC_ERROR(-1); - rpc->call_rep.eno = RPC_ERROR(errno); - } else { - /* execute RPC call */ - schedEvent(TASK_ROOT(task), execCall, c, (int) noreply, rpc, len); - } - /* send RPC reply */ if (!noreply) schedWrite(TASK_ROOT(task), cbProto[srv->srv_proto][CB_TXPACKET], @@ -615,7 +601,7 @@ rxUDPPacket(sched_task_t *task) end: AIT_FREE_VAL(&b); schedReadSelf(task); - return NULL; + taskExit(task, NULL); } @@ -627,7 +613,7 @@ txRAWPacket(sched_task_t *task) rpc_func_t *f = NULL; u_char *buf = AIT_GET_BUF(&c->cli_buf); struct tagRPCCall *rpc = (struct tagRPCCall*) buf; - int ret, estlen, wlen = sizeof(struct tagRPCCall); + int ret, wlen = sizeof(struct tagRPCCall); struct timespec ts = { DEF_RPC_TIMEOUT, 0 }; schedCancelby(TASK_ROOT(task), taskTIMER, CRITERIA_DATA, TASK_ARG(task), NULL); @@ -638,26 +624,27 @@ txRAWPacket(sched_task_t *task) f = rpc_srv_getCall(s, ntohs(rpc->call_tag)); if (!f) { rpc_SetErr(EPROGUNAVAIL, "Function not found at RPC server"); + rpc->call_argc ^= rpc->call_argc; - rpc->call_rep.ret = RPC_ERROR(-1); - rpc->call_rep.eno = RPC_ERROR(rpc_Errno); - } else { - /* calc estimated length */ - estlen = ait_resideVars(RPC_RETVARS(c)) + wlen; - if (estlen > AIT_LEN(&c->cli_buf)) - AIT_RE_BUF(&c->cli_buf, estlen); - buf = AIT_GET_BUF(&c->cli_buf); - rpc = (struct tagRPCCall*) buf; + RPC_SET_RETURN(rpc, -1); + RPC_SET_ERRNO(rpc, rpc_Errno); + } else if (rpc_pktFreeSpace(c) > s->srv_netbuf) { + rpc_SetErr(EMSGSIZE, "Message too long"); + rpc->call_argc ^= rpc->call_argc; + RPC_SET_RETURN(rpc, -1); + RPC_SET_ERRNO(rpc, rpc_Errno); + } else { rpc->call_argc = (u_char) array_Size(RPC_RETVARS(c)); /* Go Encapsulate variables */ ret = ait_vars2buffer(buf + wlen, AIT_LEN(&c->cli_buf) - wlen, RPC_RETVARS(c)); if (ret == -1) { rpc_SetErr(EBADRPC, "Prepare RPC packet failed"); + rpc->call_argc ^= rpc->call_argc; - rpc->call_rep.ret = RPC_ERROR(-1); - rpc->call_rep.eno = RPC_ERROR(rpc_Errno); + RPC_SET_RETURN(rpc, -1); + RPC_SET_ERRNO(rpc, rpc_Errno); } else wlen += ret; } @@ -674,16 +661,15 @@ txRAWPacket(sched_task_t *task) rpc->call_crc = htons(crcFletcher16((u_short*) buf, wlen / 2)); /* send reply */ - ret = sendto(TASK_FD(task), buf, MIN(wlen, s->srv_netbuf), MSG_NOSIGNAL, + ret = sendto(TASK_FD(task), buf, wlen, 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; + taskExit(task, NULL); } static void * @@ -691,7 +677,7 @@ rxRAWPacket(sched_task_t *task) { rpc_srv_t *srv = TASK_ARG(task); rpc_cli_t *c = NULL; - int len, rlen, noreply; + int len, noreply = 0, rlen; u_short crc; struct tagRPCCall *rpc; sockaddr_t sa; @@ -710,8 +696,7 @@ rxRAWPacket(sched_task_t *task) goto end; else { rlen -= sizeof(struct ip); - rpc = (struct tagRPCCall*) - (AIT_GET_BUF(&b) + sizeof(struct ip)); + rpc = (struct tagRPCCall*) (h + 1); } } else { struct ip6_hdr *h; @@ -721,28 +706,36 @@ rxRAWPacket(sched_task_t *task) goto end; else { rlen -= sizeof(struct ip6_hdr); - rpc = (struct tagRPCCall*) - (AIT_GET_BUF(&b) + sizeof(struct ip6_hdr)); + rpc = (struct tagRPCCall*) (h + 1); } } - if (rlen < sizeof(struct tagRPCCall) || rlen < ntohl(rpc->call_len)) { - rpc_SetErr(ERPCMISMATCH, "Short RPC packet"); + if (rlen < sizeof(struct tagRPCCall)) goto end; - } + else + len = ntohl(rpc->call_len); + if (rlen < len || len > srv->srv_netbuf) + goto end; /* 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; + if (crc != crcFletcher16((u_short*) rpc, len / 2)) + goto end; + + /* check RPC packet session info */ + if (rpc_chkPktSession(&rpc->call_session, &srv->srv_session)) + 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 { - 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), rpc, len); rpc = (struct tagRPCCall*) AIT_GET_BUF(&c->cli_buf); @@ -755,31 +748,11 @@ rxRAWPacket(sched_task_t *task) c, ts, c, 0); } - /* check integrity of packet */ - crc = ntohs(rpc->call_crc); - rpc->call_crc ^= rpc->call_crc; - if (crc != crcFletcher16((u_short*) rpc, len / 2)) { - rpc_SetErr(ERPCMISMATCH, "Bad CRC RPC packet"); - /* close connection */ - schedEvent(TASK_ROOT(task), cbProto[srv->srv_proto][CB_CLOSECLIENT], - c, 0, NULL, 0); - goto end; - } - noreply = RPC_CHK_NOREPLY(rpc); - /* check RPC packet session info */ - if (rpc_chkPktSession(&rpc->call_session, &srv->srv_session)) { - rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session"); + /* execute RPC call */ + schedEvent(TASK_ROOT(task), execCall, c, (int) noreply, rpc, len); - rpc->call_argc ^= rpc->call_argc; - rpc->call_rep.ret = RPC_ERROR(-1); - rpc->call_rep.eno = RPC_ERROR(errno); - } else { - /* execute RPC call */ - schedEvent(TASK_ROOT(task), execCall, c, (int) noreply, rpc, len); - } - /* send RPC reply */ if (!noreply) schedWrite(TASK_ROOT(task), cbProto[srv->srv_proto][CB_TXPACKET], @@ -787,7 +760,7 @@ rxRAWPacket(sched_task_t *task) end: AIT_FREE_VAL(&b); schedReadSelf(task); - return NULL; + taskExit(task, NULL); } @@ -799,7 +772,7 @@ txBPFPacket(sched_task_t *task) rpc_func_t *f = NULL; u_char *buf = AIT_GET_BUF(&c->cli_buf); struct tagRPCCall *rpc = (struct tagRPCCall*) buf; - int ret, len, wlen = sizeof(struct tagRPCCall); + int ret, wlen = sizeof(struct tagRPCCall); struct timespec ts = { DEF_RPC_TIMEOUT, 0 }; struct ether_header *eh; ait_val_t b = AIT_VAL_INIT; @@ -812,26 +785,27 @@ txBPFPacket(sched_task_t *task) f = rpc_srv_getCall(s, ntohs(rpc->call_tag)); if (!f) { rpc_SetErr(EPROGUNAVAIL, "Function not found at RPC server"); + rpc->call_argc ^= rpc->call_argc; - rpc->call_rep.ret = RPC_ERROR(-1); - rpc->call_rep.eno = RPC_ERROR(rpc_Errno); - } else { - /* calc estimated length */ - len = ait_resideVars(RPC_RETVARS(c)) + wlen; - if (len > AIT_LEN(&c->cli_buf)) - AIT_RE_BUF(&c->cli_buf, len); - buf = AIT_GET_BUF(&c->cli_buf); - rpc = (struct tagRPCCall*) buf; + RPC_SET_RETURN(rpc, -1); + RPC_SET_ERRNO(rpc, rpc_Errno); + } else if (rpc_pktFreeSpace(c) > s->srv_netbuf) { + rpc_SetErr(EMSGSIZE, "Message too long"); + rpc->call_argc ^= rpc->call_argc; + RPC_SET_RETURN(rpc, -1); + RPC_SET_ERRNO(rpc, rpc_Errno); + } else { rpc->call_argc = (u_char) array_Size(RPC_RETVARS(c)); /* Go Encapsulate variables */ ret = ait_vars2buffer(buf + wlen, AIT_LEN(&c->cli_buf) - wlen, RPC_RETVARS(c)); if (ret == -1) { rpc_SetErr(EBADRPC, "Prepare RPC packet failed"); + rpc->call_argc ^= rpc->call_argc; - rpc->call_rep.ret = RPC_ERROR(-1); - rpc->call_rep.eno = RPC_ERROR(rpc_Errno); + RPC_SET_RETURN(rpc, -1); + RPC_SET_ERRNO(rpc, rpc_Errno); } else wlen += ret; } @@ -848,11 +822,11 @@ txBPFPacket(sched_task_t *task) rpc->call_crc = htons(crcFletcher16((u_short*) buf, wlen / 2)); /* send reply */ - AIT_SET_BUF(&b, NULL, MIN(wlen, s->srv_netbuf) + ETHER_HDR_LEN); + AIT_SET_BUF(&b, NULL, wlen + ETHER_HDR_LEN); eh = (struct ether_header*) AIT_GET_BUF(&b); memcpy(eh->ether_dhost, LLADDR(&c->cli_sa.sdl), ETHER_ADDR_LEN); eh->ether_type = htons(RPC_DEFPORT); - memcpy(eh + 1, buf, MIN(wlen, s->srv_netbuf)); + memcpy(eh + 1, buf, wlen); ret = write(TASK_FD(task), AIT_GET_BUF(&b), AIT_LEN(&b)); AIT_FREE_VAL(&b); @@ -860,10 +834,9 @@ txBPFPacket(sched_task_t *task) /* close connection */ schedEvent(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT], TASK_ARG(task), 0, NULL, 0); - return NULL; } - return NULL; + taskExit(task, NULL); } static void * @@ -886,36 +859,48 @@ rxBPFPacket(sched_task_t *task) h = (struct bpf_hdr*) AIT_GET_BUF(&b); rlen -= h->bh_hdrlen; if (rlen < h->bh_datalen || h->bh_caplen != h->bh_datalen || - rlen < ETHER_HDR_LEN + sizeof(struct tagRPCCall)) { - rpc_SetErr(ERPCMISMATCH, "Short RPC packet"); + rlen < ETHER_HDR_LEN + sizeof(struct tagRPCCall)) goto end; - } else { + else { rlen = h->bh_caplen; eh = (struct ether_header*) (AIT_GET_BUF(&b) + h->bh_hdrlen); rlen -= ETHER_HDR_LEN; rpc = (struct tagRPCCall*) (eh + 1); -#if 0 - /* skip loop packet */ - if (rpc->call_io & RPC_ACK) - goto end; -#endif - if (eh->ether_type != ntohs(RPC_DEFPORT)) goto end; else e_getlinkbymac((const ether_addr_t*) eh->ether_shost, &sa); } + if (rlen < sizeof(struct tagRPCCall)) + goto end; + else + len = ntohl(rpc->call_len); + if (rlen < len || len > srv->srv_netbuf) + goto end; +#ifdef CHECK_ETHACK + /* skip loop packet */ + if (rpc->call_io & RPC_ACK) + goto end; +#endif + + /* check integrity of packet */ + crc = ntohs(rpc->call_crc); + rpc->call_crc ^= rpc->call_crc; + if (crc != crcFletcher16((u_short*) rpc, len / 2)) + goto end; + + /* check RPC packet session info */ + if (rpc_chkPktSession(&rpc->call_session, &srv->srv_session)) + 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 { - 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), rpc, len); rpc = (struct tagRPCCall*) AIT_GET_BUF(&c->cli_buf); @@ -928,31 +913,11 @@ rxBPFPacket(sched_task_t *task) c, ts, c, 0); } - /* check integrity of packet */ - crc = ntohs(rpc->call_crc); - rpc->call_crc ^= rpc->call_crc; - if (crc != crcFletcher16((u_short*) rpc, len / 2)) { - rpc_SetErr(ERPCMISMATCH, "Bad CRC RPC packet"); - /* close connection */ - schedEvent(TASK_ROOT(task), cbProto[srv->srv_proto][CB_CLOSECLIENT], - c, 0, NULL, 0); - goto end; - } - noreply = RPC_CHK_NOREPLY(rpc); - /* check RPC packet session info */ - if (rpc_chkPktSession(&rpc->call_session, &srv->srv_session)) { - rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session"); + /* execute RPC call */ + schedEvent(TASK_ROOT(task), execCall, c, (int) noreply, rpc, len); - rpc->call_argc ^= rpc->call_argc; - rpc->call_rep.ret = RPC_ERROR(-1); - rpc->call_rep.eno = RPC_ERROR(errno); - } else { - /* execute RPC call */ - schedEvent(TASK_ROOT(task), execCall, c, (int) noreply, rpc, len); - } - /* send RPC reply */ if (!noreply) schedEvent(TASK_ROOT(task), cbProto[srv->srv_proto][CB_TXPACKET], @@ -960,7 +925,7 @@ rxBPFPacket(sched_task_t *task) end: AIT_FREE_VAL(&b); schedReadSelf(task); - return NULL; + taskExit(task, NULL); } @@ -972,7 +937,7 @@ txEXTPacket(sched_task_t *task) rpc_func_t *f = NULL; u_char *buf = AIT_GET_BUF(&c->cli_buf); struct tagRPCCall *rpc = (struct tagRPCCall*) buf; - int ret, len, wlen = sizeof(struct tagRPCCall); + int ret, wlen = sizeof(struct tagRPCCall); struct timespec ts = { DEF_RPC_TIMEOUT, 0 }; schedCancelby(TASK_ROOT(task), taskTIMER, CRITERIA_DATA, TASK_ARG(task), NULL); @@ -983,26 +948,27 @@ txEXTPacket(sched_task_t *task) f = rpc_srv_getCall(s, ntohs(rpc->call_tag)); if (!f) { rpc_SetErr(EPROGUNAVAIL, "Function not found at RPC server"); + rpc->call_argc ^= rpc->call_argc; - rpc->call_rep.ret = RPC_ERROR(-1); - rpc->call_rep.eno = RPC_ERROR(rpc_Errno); - } else { - /* calc estimated length */ - len = ait_resideVars(RPC_RETVARS(c)) + wlen; - if (len > AIT_LEN(&c->cli_buf)) - AIT_RE_BUF(&c->cli_buf, len); - buf = AIT_GET_BUF(&c->cli_buf); - rpc = (struct tagRPCCall*) buf; + RPC_SET_RETURN(rpc, -1); + RPC_SET_ERRNO(rpc, rpc_Errno); + } else if (rpc_pktFreeSpace(c) > s->srv_netbuf) { + rpc_SetErr(EMSGSIZE, "Message too long"); + rpc->call_argc ^= rpc->call_argc; + RPC_SET_RETURN(rpc, -1); + RPC_SET_ERRNO(rpc, rpc_Errno); + } else { rpc->call_argc = (u_char) array_Size(RPC_RETVARS(c)); /* Go Encapsulate variables */ ret = ait_vars2buffer(buf + wlen, AIT_LEN(&c->cli_buf) - wlen, RPC_RETVARS(c)); if (ret == -1) { rpc_SetErr(EBADRPC, "Prepare RPC packet failed"); + rpc->call_argc ^= rpc->call_argc; - rpc->call_rep.ret = RPC_ERROR(-1); - rpc->call_rep.eno = RPC_ERROR(rpc_Errno); + RPC_SET_RETURN(rpc, -1); + RPC_SET_ERRNO(rpc, rpc_Errno); } else wlen += ret; } @@ -1015,15 +981,14 @@ txEXTPacket(sched_task_t *task) rpc->call_io = RPC_ACK; /* send reply */ - ret = write(TASK_FD(task), buf, MIN(wlen, s->srv_netbuf)); + ret = write(TASK_FD(task), buf, wlen); 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; + taskExit(task, NULL); } static void * @@ -1031,36 +996,39 @@ rxEXTPacket(sched_task_t *task) { rpc_srv_t *srv = TASK_ARG(task); rpc_cli_t *c = NULL; - int len, rlen, noreply; + int len, noreply = 0, rlen = AIT_LEN(&c->cli_buf); struct tagRPCCall *rpc; struct timespec ts = { DEF_RPC_TIMEOUT, 0 }; - ait_val_t b = AIT_VAL_INIT; sockaddr_t sa; + ait_val_t b = AIT_VAL_INIT; memset(&sa, 0, sizeof sa); /* receive connect packet */ AIT_SET_BUF(&b, NULL, srv->srv_netbuf); rlen = read(TASK_FD(task), AIT_GET_BUF(&b), AIT_LEN(&b)); rpc = (struct tagRPCCall*) AIT_GET_BUF(&b); - if (rlen < sizeof(struct tagRPCCall) || rlen < ntohl(rpc->call_len)) { - rpc_SetErr(ERPCMISMATCH, "Short RPC packet"); + if (rlen < sizeof(struct tagRPCCall)) goto end; - } + else + len = ntohl(rpc->call_len); + if (rlen < len || len > srv->srv_netbuf) + goto end; /* skip loop packet */ if (rpc->call_io & RPC_ACK) goto end; + /* check RPC packet session info */ + if (rpc_chkPktSession(&rpc->call_session, &srv->srv_session)) + 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 { - 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), rpc, AIT_LEN(&c->cli_buf)); + memcpy(AIT_GET_BUF(&c->cli_buf), AIT_GET_BUF(&b), len); rpc = (struct tagRPCCall*) AIT_GET_BUF(&c->cli_buf); c->cli_sock = TASK_FD(task); @@ -1073,18 +1041,9 @@ rxEXTPacket(sched_task_t *task) noreply = RPC_CHK_NOREPLY(rpc); - /* check RPC packet session info */ - if (rpc_chkPktSession(&rpc->call_session, &srv->srv_session)) { - rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session"); + /* execute RPC call */ + schedEvent(TASK_ROOT(task), execCall, c, (int) noreply, rpc, len); - rpc->call_argc ^= rpc->call_argc; - rpc->call_rep.ret = RPC_ERROR(-1); - rpc->call_rep.eno = RPC_ERROR(errno); - } else { - /* execute RPC call */ - schedEvent(TASK_ROOT(task), execCall, c, (int) noreply, rpc, len); - } - /* send RPC reply */ if (!noreply) schedWrite(TASK_ROOT(task), cbProto[srv->srv_proto][CB_TXPACKET], @@ -1092,7 +1051,7 @@ rxEXTPacket(sched_task_t *task) end: AIT_FREE_VAL(&b); schedReadSelf(task); - return NULL; + taskExit(task, NULL); } /* ------------------------------------------------------ */ @@ -1123,7 +1082,7 @@ closeBLOBClient(sched_task_t *task) /* close client socket */ shutdown(sock, SHUT_RDWR); close(sock); - return NULL; + taskExit(task, NULL); } static void * @@ -1140,7 +1099,7 @@ txBLOB(sched_task_t *task) schedEvent(TASK_ROOT(task), closeBLOBClient, c, 42, NULL, 0); } - return NULL; + taskExit(task, NULL); } static void * @@ -1157,7 +1116,7 @@ rxBLOB(sched_task_t *task) if (rlen < 1) { /* close blob connection */ schedEvent(TASK_ROOT(task), closeBLOBClient, c, 42, NULL, 0); - return NULL; + taskExit(task, NULL); } /* check BLOB packet */ @@ -1165,7 +1124,7 @@ rxBLOB(sched_task_t *task) rpc_SetErr(ERPCMISMATCH, "Short BLOB packet"); schedReadSelf(task); - return NULL; + taskExit(task, NULL); } /* check RPC packet session info */ @@ -1181,7 +1140,7 @@ rxBLOB(sched_task_t *task) 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); + RPC_SET_BLOB_RET(&blob, -1); break; } else blob.hdr_len = htonl(b->blob_len); @@ -1192,7 +1151,7 @@ rxBLOB(sched_task_t *task) rpc_srv_blobUnmap(b); } else { blob.hdr_cmd = error; - blob.hdr_ret = RPC_ERROR(-1); + RPC_SET_BLOB_RET(&blob, -1); } break; case set: @@ -1206,26 +1165,26 @@ rxBLOB(sched_task_t *task) rpc_srv_blobUnmap(b); } else { blob.hdr_cmd = error; - blob.hdr_ret = RPC_ERROR(-1); + RPC_SET_BLOB_RET(&blob, -1); } break; case unset: if (rpc_srv_unregisterBLOB(s, ntohl(blob.hdr_var)) == -1) { blob.hdr_cmd = error; - blob.hdr_ret = RPC_ERROR(-1); + RPC_SET_BLOB_RET(&blob, -1); } break; default: rpc_SetErr(EPROCUNAVAIL, "Unsupported BLOB command %d", blob.hdr_cmd); blob.hdr_cmd = error; - blob.hdr_ret = RPC_ERROR(-1); + RPC_SET_BLOB_RET(&blob, -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; + taskExit(task, NULL); } static void * @@ -1253,7 +1212,7 @@ flushBLOB(sched_task_t *task) sigaction(SIGFBLOB, &sa, NULL); } - return NULL; + taskExit(task, NULL); } static void * @@ -1284,7 +1243,7 @@ acceptBLOBClients(sched_task_t *task) if (!c) { LOGERR; srv->srv_kill = srv->srv_blob.kill = 1; - return NULL; + taskExit(task, NULL); } else { memset(c, 0, sizeof(rpc_cli_t)); array_Set(srv->srv_blob.clients, i, c); @@ -1312,7 +1271,7 @@ acceptBLOBClients(sched_task_t *task) schedRead(TASK_ROOT(task), rxBLOB, c, c->cli_sock, NULL, 0); end: schedReadSelf(task); - return NULL; + taskExit(task, NULL); } /* ------------------------------------------------------ */ @@ -1617,6 +1576,8 @@ rpc_srv_initServer(u_char InstID, int concurentClients LOGERR; goto err; } + if (srv->srv_proto == SOCK_STREAM) + setsockopt(srv->srv_server.cli_sock, IPPROTO_TCP, TCP_NODELAY, &n, sizeof n); n = srv->srv_netbuf; if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof n) == -1) { LOGERR;