--- libaitrpc/src/srv.c 2014/12/18 00:28:47 1.23.6.5 +++ libaitrpc/src/srv.c 2015/01/15 20:37:14 1.24.2.2 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: srv.c,v 1.23.6.5 2014/12/18 00:28:47 misho Exp $ +* $Id: srv.c,v 1.24.2.2 2015/01/15 20:37:14 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 - 2014 +Copyright 2004 - 2015 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -63,12 +63,15 @@ static void *txUDPPacket(sched_task_t *); static void *rxBPFPacket(sched_task_t *); static void *txBPFPacket(sched_task_t *); -static sched_task_func_t cbProto[SOCK_BPF + 1][4] = { +/* SOCK_PIPE */ + +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 */ { NULL, NULL, NULL, NULL }, /* SOCK_RAW */ - { rxBPFPacket, freeClient, rxBPFPacket, txBPFPacket } /* SOCK_BPF */ + { rxBPFPacket, freeClient, rxBPFPacket, txBPFPacket }, /* SOCK_BPF */ + { NULL, NULL, NULL, NULL } /* SOCK_PIPE */ }; /* Global Signal Argument when kqueue support disabled */ @@ -645,9 +648,10 @@ 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, estlen, wlen = sizeof(struct tagRPCCall); + int ret, len, wlen = sizeof(struct tagRPCCall); struct timespec ts = { DEF_RPC_TIMEOUT, 0 }; - struct pollfd pfd; + struct ether_header *eh; + ait_val_t b = AIT_VAL_INIT; schedCancelby(TASK_ROOT(task), taskTIMER, CRITERIA_DATA, TASK_ARG(task), NULL); schedTimer(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT], @@ -662,9 +666,9 @@ txBPFPacket(sched_task_t *task) 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); + 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; @@ -673,7 +677,7 @@ txBPFPacket(sched_task_t *task) ret = ait_vars2buffer(buf + wlen, AIT_LEN(&c->cli_buf) - wlen, RPC_RETVARS(c)); /* Free return values */ - ait_freeVars(&c->cli_vars); + ait_freeVars(&RPC_RETVARS(c)); if (ret == -1) { rpc_SetErr(EBADRPC, "Prepare RPC packet failed"); rpc->call_argc ^= rpc->call_argc; @@ -691,27 +695,19 @@ txBPFPacket(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 = write(TASK_FD(task), buf, MIN(wlen, s->srv_netbuf)); - if (ret == -1) { - /* close connection */ - schedEvent(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT], - TASK_ARG(task), 0, NULL, 0); - return NULL; - } + AIT_SET_BUF(&b, NULL, MIN(wlen, s->srv_netbuf) + 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)); + + ret = write(TASK_FD(task), AIT_GET_BUF(&b), AIT_LEN(&b)); + AIT_FREE_VAL(&b); + 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; @@ -736,7 +732,7 @@ rxBPFPacket(sched_task_t *task) rlen = read(TASK_FD(task), AIT_GET_BUF(&b), AIT_LEN(&b)); h = (struct bpf_hdr*) AIT_GET_BUF(&b); rlen -= h->bh_hdrlen; - if (rlen < h->bh_caplen || h->bh_caplen != h->bh_datalen || + if (rlen < h->bh_datalen || h->bh_caplen != h->bh_datalen || rlen < ETHER_HDR_LEN + sizeof(struct tagRPCCall)) { rpc_SetErr(ERPCMISMATCH, "Short RPC packet"); goto end; @@ -799,7 +795,7 @@ rxBPFPacket(sched_task_t *task) /* send RPC reply */ if (!noreply) - schedWrite(TASK_ROOT(task), cbProto[srv->srv_proto][CB_TXPACKET], + schedEvent(TASK_ROOT(task), cbProto[srv->srv_proto][CB_TXPACKET], c, TASK_FD(task), rpc, len); end: AIT_FREE_VAL(&b); @@ -1249,7 +1245,7 @@ rpc_srv_initServer(u_char InstID, int concurentClients rpc_srv_t *srv = NULL; sockaddr_t sa = E_SOCKADDR_INIT; - if (!concurentClients || (proto < 0 || proto > SOCK_DGRAM)) { + if (!concurentClients || (proto < 0 || proto > SOCK_RAW)) { rpc_SetErr(EINVAL, "Invalid parameters can`t init RPC server"); return NULL; }