Annotation of libaitrpc/src/cli.c, revision 1.29

1.1       misho       1: /*************************************************************************
                      2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
                      3: *  by Michael Pounov <misho@openbsd-bg.org>
                      4: *
                      5: * $Author: misho $
1.29    ! misho       6: * $Id: cli.c,v 1.28.2.1 2024/03/19 15:17:16 misho Exp $
1.1       misho       7: *
1.2       misho       8: **************************************************************************
                      9: The ELWIX and AITNET software is distributed under the following
                     10: terms:
                     11: 
                     12: All of the documentation and software included in the ELWIX and AITNET
                     13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
                     14: 
1.29    ! misho      15: Copyright 2004 - 2024
1.2       misho      16:        by Michael Pounov <misho@elwix.org>.  All rights reserved.
                     17: 
                     18: Redistribution and use in source and binary forms, with or without
                     19: modification, are permitted provided that the following conditions
                     20: are met:
                     21: 1. Redistributions of source code must retain the above copyright
                     22:    notice, this list of conditions and the following disclaimer.
                     23: 2. Redistributions in binary form must reproduce the above copyright
                     24:    notice, this list of conditions and the following disclaimer in the
                     25:    documentation and/or other materials provided with the distribution.
                     26: 3. All advertising materials mentioning features or use of this software
                     27:    must display the following acknowledgement:
                     28: This product includes software developed by Michael Pounov <misho@elwix.org>
                     29: ELWIX - Embedded LightWeight unIX and its contributors.
                     30: 4. Neither the name of AITNET nor the names of its contributors
                     31:    may be used to endorse or promote products derived from this software
                     32:    without specific prior written permission.
                     33: 
                     34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
                     35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     44: SUCH DAMAGE.
                     45: */
1.1       misho      46: #include "global.h"
                     47: 
                     48: 
                     49: /*
1.7       misho      50:  * rpc_cli_openBLOBClient() - Connect to BLOB Server
                     51:  *
1.2       misho      52:  * @rpccli = RPC Client session
                     53:  * @Port = Port for bind server, if Port == 0 default port is selected
                     54:  * return: NULL == error or !=NULL connection to BLOB server established
                     55:  */
                     56: rpc_cli_t *
                     57: rpc_cli_openBLOBClient(rpc_cli_t * __restrict rpccli, u_short Port)
                     58: {
                     59:        rpc_cli_t *cli = NULL;
1.13      misho      60:        int n;
1.4       misho      61: 
1.10      misho      62:        if (!rpccli) {
1.7       misho      63:                rpc_SetErr(EINVAL, "Invalid parameters can`t connect to BLOB server");
1.2       misho      64:                return NULL;
                     65:        }
                     66: 
1.14      misho      67:        cli = e_malloc(sizeof(rpc_cli_t));
1.2       misho      68:        if (!cli) {
                     69:                LOGERR;
                     70:                return NULL;
                     71:        } else
                     72:                memcpy(cli, rpccli, sizeof(rpc_cli_t));
                     73: 
1.27      misho      74:        memcpy(&cli->cli_sa, &rpccli->cli_sa, sizeof cli->cli_sa);
1.10      misho      75:        switch (cli->cli_sa.sa.sa_family) {
1.4       misho      76:                case AF_INET:
1.10      misho      77:                        cli->cli_sa.sin.sin_port = 
                     78:                                htons(Port ? Port : ntohs(cli->cli_sa.sin.sin_port) + 1);
1.4       misho      79:                        break;
                     80:                case AF_INET6:
1.10      misho      81:                        cli->cli_sa.sin6.sin6_port = 
                     82:                                htons(Port ? Port : ntohs(cli->cli_sa.sin6.sin6_port) + 1);
1.4       misho      83:                        break;
                     84:                case AF_LOCAL:
1.10      misho      85:                        strlcat(cli->cli_sa.sun.sun_path, ".blob", sizeof cli->cli_sa.sun.sun_path);
1.4       misho      86:                        break;
1.10      misho      87:                default:
                     88:                        rpc_SetErr(EINVAL, "Invalid socket type %d", cli->cli_sa.sa.sa_family);
                     89:                        return NULL;
1.2       misho      90:        }
1.10      misho      91: 
                     92:        AIT_COPY_VAL(&cli->cli_buf, &rpccli->cli_buf);
1.13      misho      93:        n = AIT_LEN(&cli->cli_buf);
1.2       misho      94: 
1.4       misho      95:        /* connect to BLOB server */
1.6       misho      96:        cli->cli_sock = socket(cli->cli_sa.sa.sa_family, SOCK_STREAM, 0);
1.2       misho      97:        if (cli->cli_sock == -1) {
                     98:                LOGERR;
1.14      misho      99:                e_free(cli);
1.2       misho     100:                return NULL;
                    101:        }
1.13      misho     102:        if (setsockopt(cli->cli_sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof n) == -1) {
                    103:                LOGERR;
                    104:                close(cli->cli_sock);
1.14      misho     105:                e_free(cli);
1.13      misho     106:                return NULL;
                    107:        }
                    108:        if (setsockopt(cli->cli_sock, SOL_SOCKET, SO_RCVBUF, &n, sizeof n) == -1) {
                    109:                LOGERR;
                    110:                close(cli->cli_sock);
1.14      misho     111:                e_free(cli);
1.13      misho     112:                return NULL;
                    113:        }
1.27      misho     114:        if (connect(cli->cli_sock, &cli->cli_sa.sa, e_addrlen(&cli->cli_sa)) == -1) {
1.2       misho     115:                LOGERR;
1.5       misho     116:                close(cli->cli_sock);
1.14      misho     117:                e_free(cli);
1.2       misho     118:                return NULL;
1.10      misho     119:        } else
                    120:                fcntl(cli->cli_sock, F_SETFL, fcntl(cli->cli_sock, F_GETFL) | O_NONBLOCK);
1.2       misho     121: 
                    122:        return cli;
                    123: }
                    124: 
                    125: /*
1.7       misho     126:  * rpc_cli_closeBLOBClient() - Close connection to BLOB server and free resources
                    127:  *
1.2       misho     128:  * @cli = BLOB Client session
                    129:  * return: none
                    130:  */
                    131: void
1.10      misho     132: rpc_cli_closeBLOBClient(rpc_cli_t ** __restrict cli)
1.2       misho     133: {
1.10      misho     134:        if (!cli || !*cli)
1.2       misho     135:                return;
                    136: 
1.10      misho     137:        shutdown((*cli)->cli_sock, SHUT_RDWR);
                    138:        close((*cli)->cli_sock);
                    139: 
                    140:        AIT_FREE_VAL(&(*cli)->cli_buf);
1.2       misho     141: 
1.14      misho     142:        e_free(*cli);
1.10      misho     143:        *cli = NULL;
1.2       misho     144: }
                    145: 
1.10      misho     146: /* -------------------------------------------------------------- */
1.2       misho     147: 
                    148: /*
1.7       misho     149:  * rpc_cli_openClient() - Connect to RPC Server
                    150:  *
1.15      misho     151:  * @InstID = InstID for RPC session request
1.10      misho     152:  * @netBuf = Network buffer length (min:512 bytes), if =0 == BUFSIZ (also meaning max RPC packet)
1.1       misho     153:  * @csHost = Host name or IP address for bind server
                    154:  * @Port = Port for bind server, if Port == 0 default port is selected
1.13      misho     155:  * @proto = Protocol, if == 0 choose SOCK_STREAM
1.1       misho     156:  * return: NULL == error or !=NULL connection to RPC server established
                    157:  */
                    158: rpc_cli_t *
1.15      misho     159: rpc_cli_openClient(u_char InstID, int netBuf, const char *csHost, u_short Port, int proto)
1.1       misho     160: {
1.26      misho     161:        int n = 1;
1.1       misho     162:        rpc_cli_t *cli = NULL;
1.14      misho     163:        sockaddr_t sa = E_SOCKADDR_INIT;
1.27      misho     164:        socklen_t salen;
1.1       misho     165: 
1.25      misho     166:        if (proto < 0 || proto > SOCK_RAW) {
                    167:                rpc_SetErr(EINVAL, "Invalid parameters can`t open RPC client");
                    168:                return NULL;
                    169:        }
                    170: 
                    171:        srandom(time(NULL) ^ getpid());
                    172: 
                    173:        if (!Port && proto < SOCK_RAW)
                    174:                Port = RPC_DEFPORT;
1.27      misho     175:        if (!(salen = e_gethostbyname(csHost, Port, &sa)))
1.1       misho     176:                return NULL;
1.13      misho     177:        if (!proto)
                    178:                proto = SOCK_STREAM;
1.10      misho     179:        if (netBuf < RPC_MIN_BUFSIZ)
1.5       misho     180:                netBuf = BUFSIZ;
1.10      misho     181:        else
1.14      misho     182:                netBuf = E_ALIGN(netBuf, 2);    /* align netBuf length */
1.10      misho     183: 
1.14      misho     184:        cli = e_malloc(sizeof(rpc_cli_t));
1.1       misho     185:        if (!cli) {
                    186:                LOGERR;
                    187:                return NULL;
                    188:        } else
                    189:                memset(cli, 0, sizeof(rpc_cli_t));
1.5       misho     190: 
1.10      misho     191:        /* build session */
1.14      misho     192:        cli->cli_parent = e_malloc(sizeof(rpc_sess_t));
1.1       misho     193:        if (!cli->cli_parent) {
                    194:                LOGERR;
1.14      misho     195:                e_free(cli);
1.1       misho     196:                return NULL;
                    197:        } else {
                    198:                ((rpc_sess_t*) cli->cli_parent)->sess_version = RPC_VERSION;
1.15      misho     199:                ((rpc_sess_t*) cli->cli_parent)->sess_instance = InstID;
1.1       misho     200:        }
                    201: 
1.13      misho     202:        cli->cli_id = proto;
1.6       misho     203:        memcpy(&cli->cli_sa, &sa, sizeof cli->cli_sa);
1.14      misho     204:        AIT_SET_BUFSIZ(&cli->cli_buf, 0, netBuf);
1.4       misho     205: 
                    206:        /* connect to RPC server */
1.25      misho     207:        cli->cli_sock = socket(cli->cli_sa.sa.sa_family, cli->cli_id, 
                    208:                        cli->cli_id == SOCK_RAW ? IPPROTO_ERPC : 0);
1.1       misho     209:        if (cli->cli_sock == -1) {
                    210:                LOGERR;
1.13      misho     211:                goto err;
                    212:        }
                    213:        if (setsockopt(cli->cli_sock, SOL_SOCKET, SO_SNDBUF, 
                    214:                                &netBuf, sizeof netBuf) == -1) {
                    215:                LOGERR;
                    216:                goto err;
1.5       misho     217:        }
1.13      misho     218:        if (setsockopt(cli->cli_sock, SOL_SOCKET, SO_RCVBUF, 
                    219:                                &netBuf, sizeof netBuf) == -1) {
1.1       misho     220:                LOGERR;
1.13      misho     221:                goto err;
                    222:        }
1.26      misho     223:        if (cli->cli_id == SOCK_STREAM) {
                    224:                setsockopt(cli->cli_sock, IPPROTO_TCP, TCP_NODELAY, &n, sizeof n);
1.27      misho     225:                if (connect(cli->cli_sock, &cli->cli_sa.sa, salen) == -1) {
1.13      misho     226:                        LOGERR;
                    227:                        goto err;
                    228:                }
1.26      misho     229:        }
                    230:        if (cli->cli_id == SOCK_DGRAM) {
                    231:                sockaddr_t sa2;
                    232: 
1.27      misho     233:                if (!(salen = e_gethostbyname(csHost, 0, &sa2)))
1.26      misho     234:                        goto err;
1.27      misho     235:                if (bind(cli->cli_sock, &sa2.sa, salen) == -1) {
1.26      misho     236:                        LOGERR;
                    237:                        goto err;
                    238:                }
                    239:        }
1.1       misho     240: 
1.13      misho     241:        fcntl(cli->cli_sock, F_SETFL, fcntl(cli->cli_sock, F_GETFL) | O_NONBLOCK);
1.1       misho     242:        return cli;
1.13      misho     243: err:
                    244:        AIT_FREE_VAL(&cli->cli_buf);
                    245:        if (cli->cli_sock > 2)
                    246:                close(cli->cli_sock);
1.14      misho     247:        e_free(cli->cli_parent);
                    248:        e_free(cli);
1.13      misho     249:        return NULL;
1.1       misho     250: }
                    251: 
                    252: /*
1.21      misho     253:  * rpc_cli_reconnectClient() - Reconnecting client to RPC server
                    254:  *
                    255:  * @cli = RPC Client session
                    256:  * return: -1 error or 0 ok
                    257:  */
                    258: int
                    259: rpc_cli_reconnectClient(rpc_cli_t * __restrict cli)
                    260: {
                    261:        int netBuf;
                    262: 
                    263:        if (!cli)
                    264:                return -1;
                    265:        else
                    266:                netBuf = AIT_LEN(&cli->cli_buf);
                    267: 
1.26      misho     268:        if (cli->cli_id == SOCK_STREAM)
                    269:                shutdown(cli->cli_sock, SHUT_RDWR);
                    270:        if (cli->cli_id == SOCK_DGRAM && cli->cli_sa.sa.sa_family == AF_LOCAL) {
                    271:                sockaddr_t sa2 = E_SOCKADDR_INIT;
1.27      misho     272:                socklen_t salen = sizeof sa2;
1.26      misho     273: 
1.27      misho     274: #ifndef __linux__
                    275:                sa2.sa.sa_len = salen;
                    276: #endif
1.26      misho     277:                if (!getsockname(cli->cli_sock, &sa2.sa, &salen))
                    278:                        unlink(sa2.sun.sun_path);
                    279:        }
1.21      misho     280:        close(cli->cli_sock);
                    281: 
1.25      misho     282:        srandom(time(NULL) ^ getpid());
                    283: 
1.21      misho     284:        cli->cli_sock = socket(cli->cli_sa.sa.sa_family, cli->cli_id, 0);
                    285:        if (cli->cli_sock == -1) {
                    286:                LOGERR;
                    287:                return -1;
                    288:        }
                    289:        if (setsockopt(cli->cli_sock, SOL_SOCKET, SO_SNDBUF, 
                    290:                                &netBuf, sizeof netBuf) == -1) {
                    291:                LOGERR;
                    292:                close(cli->cli_sock);
                    293:                return -1;
                    294:        }
                    295:        if (setsockopt(cli->cli_sock, SOL_SOCKET, SO_RCVBUF, 
                    296:                                &netBuf, sizeof netBuf) == -1) {
                    297:                LOGERR;
                    298:                close(cli->cli_sock);
                    299:                return -1;
                    300:        }
                    301:        if (cli->cli_id == SOCK_STREAM)
1.27      misho     302:                if (connect(cli->cli_sock, &cli->cli_sa.sa, e_addrlen(&cli->cli_sa)) == -1) {
1.21      misho     303:                        LOGERR;
                    304:                        close(cli->cli_sock);
                    305:                        return -1;
                    306:                }
                    307: 
                    308:        fcntl(cli->cli_sock, F_SETFL, fcntl(cli->cli_sock, F_GETFL) | O_NONBLOCK);
                    309:        return 0;
                    310: }
                    311: 
                    312: /*
1.7       misho     313:  * rpc_cli_closeClient() - Close connection to RPC server and free resources
                    314:  *
1.1       misho     315:  * @cli = RPC Client session
                    316:  * return: none
                    317:  */
                    318: void
1.10      misho     319: rpc_cli_closeClient(rpc_cli_t ** __restrict cli)
1.1       misho     320: {
1.23      misho     321:        if (!cli || !*cli || (*cli)->cli_id == SOCK_BPF)
1.1       misho     322:                return;
                    323: 
1.13      misho     324:        if ((*cli)->cli_id == SOCK_STREAM)
                    325:                shutdown((*cli)->cli_sock, SHUT_RDWR);
1.26      misho     326:        if ((*cli)->cli_id == SOCK_DGRAM && (*cli)->cli_sa.sa.sa_family == AF_LOCAL) {
                    327:                sockaddr_t sa2 = E_SOCKADDR_INIT;
1.27      misho     328:                socklen_t salen = sizeof sa2;
1.26      misho     329: 
1.27      misho     330: #ifndef __linux__
                    331:                sa2.sa.sa_len = salen;
                    332: #endif
1.26      misho     333:                if (!getsockname((*cli)->cli_sock, &sa2.sa, &salen))
                    334:                        unlink(sa2.sun.sun_path);
                    335:        }
1.10      misho     336:        close((*cli)->cli_sock);
                    337: 
                    338:        AIT_FREE_VAL(&(*cli)->cli_buf);
                    339: 
                    340:        if ((*cli)->cli_parent)
1.14      misho     341:                e_free((*cli)->cli_parent);
1.1       misho     342: 
1.14      misho     343:        e_free(*cli);
1.10      misho     344:        *cli = NULL;
1.1       misho     345: }
                    346: 
                    347: /*
1.14      misho     348:  * rpc_pkt_Send() - Send RPC packet
1.7       misho     349:  *
1.14      misho     350:  * @sock = Socket
                    351:  * @type = Type of socket
                    352:  * @sa = Server address
                    353:  * @pkt = RPC packet
                    354:  * @len = Length of packet
1.21      misho     355:  * return: -1 error, 0  EOF or >0 sended bytes
1.1       misho     356:  */
                    357: int
1.14      misho     358: rpc_pkt_Send(int sock, int type, sockaddr_t * __restrict sa, ait_val_t * __restrict pkt, int len)
1.1       misho     359: {
1.14      misho     360:        if (!pkt) {
                    361:                rpc_SetErr(EINVAL, "Invalid argument(s)!");
1.1       misho     362:                return -1;
1.25      misho     363:        }
1.7       misho     364: 
1.25      misho     365:        return rpc_Write(sock, type, MSG_NOSIGNAL, sa, pkt, len);
1.14      misho     366: }
                    367: 
                    368: /*
                    369:  * rpc_pkt_Receive() - Receive RPC packet
                    370:  *
                    371:  * @sock = Socket
                    372:  * @type = Type of socket
                    373:  * @sa = Server address
                    374:  * @pkt = RPC packet
1.25      misho     375:  * @seq = Signed packet with seq.no
1.21      misho     376:  * return: -1 error, 0 EOF or >0 received bytes
1.14      misho     377:  */
                    378: int
1.25      misho     379: rpc_pkt_Receive(int sock, int type, sockaddr_t * __restrict sa, ait_val_t * __restrict pkt, int seq)
1.14      misho     380: {
1.25      misho     381:        int ret;
1.17      misho     382:        struct tagRPCCall *rpc;
1.14      misho     383: 
                    384:        if (!pkt) {
                    385:                rpc_SetErr(EINVAL, "Invalid argument(s)!");
                    386:                return -1;
1.20      misho     387:        }
1.10      misho     388: 
                    389:        /* reply from RPC server */
                    390:        do {
1.25      misho     391:                if (type == SOCK_STREAM || type == SOCK_EXT)
                    392:                        ret = rpc_Read(sock, type, 0, NULL, pkt);
                    393:                else
                    394:                        ret = rpc_Read(sock, type, 0, sa, pkt);
                    395: 
                    396:                if (ret > 0) {
                    397:                        rpc = (struct tagRPCCall*) AIT_GET_BUF(pkt);
1.26      misho     398:                        if (ret < ntohl(rpc->call_len))
                    399:                                continue;
1.25      misho     400: 
                    401:                        /* check for loop request */
                    402:                        if (!(rpc->call_io & RPC_ACK))
                    403:                                continue;
                    404:                        /* check for signed request */
                    405:                        if (seq != ntohl(rpc->call_seq))
                    406:                                continue;
1.17      misho     407:                }
1.28      misho     408:        } while (ret < 0);
1.8       misho     409: 
1.14      misho     410:        return ret;
                    411: }
                    412: 
                    413: /*
                    414:  * rpc_pkt_Request() - Build RPC Request packet
                    415:  *
                    416:  * @pkt = Packet buffer
                    417:  * @sess = RPC session info
                    418:  * @tag = Function tag for execution
                    419:  * @vars = Function argument array of values, may be NULL
1.29    ! misho     420:  * @noreply = >0 We not want RPC reply, -1 IPC request with reply
1.15      misho     421:  * @nocrc = Without CRC calculation
1.25      misho     422:  * @seq = Sign packet with seq.no
1.14      misho     423:  * return: -1 error or != -1 prepared bytes into packet
                    424:  */
                    425: int
                    426: rpc_pkt_Request(ait_val_t * __restrict pkt, rpc_sess_t * __restrict sess, u_short tag, 
1.25      misho     427:                array_t * __restrict vars, int noreply, int nocrc, int seq)
1.14      misho     428: {
                    429:        struct tagRPCCall *rpc;
1.17      misho     430:        int ret = 0, estlen, len = sizeof(struct tagRPCCall);
1.14      misho     431:        u_char *buf;
                    432: 
                    433:        if (!pkt || !sess) {
                    434:                rpc_SetErr(EINVAL, "Invalid argument(s)!");
                    435:                return -1;
1.17      misho     436:        }
                    437: 
                    438:        /* calc estimated length */
                    439:        estlen = ait_resideVars(vars) + len;
1.26      misho     440:        if (estlen > AIT_LEN(pkt)) {
                    441:                rpc_SetErr(EMSGSIZE, "Message too long");
                    442:                return -1;
                    443:        }
1.17      misho     444:        buf = AIT_GET_BUF(pkt);
1.14      misho     445: 
                    446:        /* prepare RPC call */
                    447:        rpc = (struct tagRPCCall*) buf;
                    448:        rpc_addPktSession(&rpc->call_session, sess);
                    449:        rpc->call_tag = htons(tag);
                    450:        if (!vars)
                    451:                rpc->call_argc = 0;
                    452:        else
1.24      misho     453:                rpc->call_argc = (u_char) array_Size(vars);
1.14      misho     454: 
                    455:        /* set reply */
1.29    ! misho     456:        rpc->call_req.flags = (uint64_t) htonl((noreply > 0) ? RPC_NOREPLY : RPC_REPLY);
1.14      misho     457: 
                    458:        if (array_Size(vars)) {
                    459:                /* marshaling variables */
                    460:                ret = ait_vars2buffer(buf + len, AIT_LEN(pkt) - len, vars);
                    461:                if (ret == -1) {
                    462:                        rpc_SetErr(EBADRPC, "Failed to prepare RPC packet values");
                    463:                        return -1;
                    464:                } else
                    465:                        len += ret;
                    466:        }
                    467: 
                    468:        /* total packet length */
1.17      misho     469:        rpc->call_len = htonl(len);
1.29    ! misho     470:        rpc->call_io = (noreply == -1) ? RPC_IPC : RPC_REQ;
1.14      misho     471: 
1.25      misho     472:        /* sign packet */
                    473:        rpc->call_seq = htonl(seq);
                    474: 
1.15      misho     475:        if (!nocrc) {
                    476:                /* calculate CRC */
                    477:                rpc->call_crc ^= rpc->call_crc;
                    478:                rpc->call_crc = htons(crcFletcher16((u_short*) buf, len / 2));
                    479:        }
1.14      misho     480: 
                    481:        return len;
                    482: }
                    483: 
                    484: /*
                    485:  * rpc_pkt_Replay() - Decode RPC Replay packet
                    486:  *
                    487:  * @pkt = Packet buffer
1.23      misho     488:  * @sess = RPC session info, if =NULL don't check session
1.25      misho     489:  * @tag = Function tag, if =CALL_TAG_MAX don't check tag
1.14      misho     490:  * @vars = Function argument array of values, may be NULL
1.15      misho     491:  * @nocrc = Without CRC calculation
1.14      misho     492:  * return: -1 error or != -1 return value from function
                    493:  */
                    494: int
                    495: rpc_pkt_Replay(ait_val_t * __restrict pkt, rpc_sess_t * __restrict sess, u_short tag, 
1.15      misho     496:                array_t ** __restrict vars, int nocrc)
1.14      misho     497: {
                    498:        struct tagRPCCall *rpc;
                    499:        int len;
                    500:        u_char *buf;
                    501:        uint16_t crc;
                    502: 
1.23      misho     503:        if (!pkt) {
1.14      misho     504:                rpc_SetErr(EINVAL, "Invalid argument(s)!");
                    505:                return -1;
                    506:        } else
                    507:                buf = AIT_GET_BUF(pkt);
                    508: 
                    509:        rpc = (struct tagRPCCall*) buf;
1.15      misho     510:        if (!nocrc) {
                    511:                /* calculate CRC */
                    512:                crc = ntohs(rpc->call_crc);
                    513:                rpc->call_crc ^= rpc->call_crc;
1.17      misho     514:                if (crc != crcFletcher16((u_short*) buf, ntohl(rpc->call_len) / 2)) {
1.15      misho     515:                        rpc_SetErr(ERPCMISMATCH, "Bad CRC RPC packet");
                    516:                        return -1;
                    517:                }
1.8       misho     518:        }
                    519: 
1.3       misho     520:        /* check RPC packet session info */
1.23      misho     521:        if (sess && rpc_chkPktSession(&rpc->call_session, sess)) {
1.10      misho     522:                rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session");
1.8       misho     523:                return -1;
1.14      misho     524:        }
1.25      misho     525:        if (tag != CALL_TAG_MAX && ntohs(rpc->call_tag) != tag) {
1.10      misho     526:                rpc_SetErr(ERPCMISMATCH, "Get wrong RPC reply");
1.8       misho     527:                return -1;
1.6       misho     528:        }
1.10      misho     529:        if (ntohl(rpc->call_rep.eno) && ntohl(rpc->call_rep.ret) == -1) {
1.8       misho     530:                rpc_SetErr(ntohl(rpc->call_rep.eno), "Server side: retcode=%d #%d %s", 
1.7       misho     531:                                ntohl(rpc->call_rep.ret), ntohl(rpc->call_rep.eno), 
                    532:                                strerror(ntohl(rpc->call_rep.eno)));
1.8       misho     533:                return -1;
1.1       misho     534:        }
1.24      misho     535:        len = rpc->call_argc * sizeof(ait_val_t);
1.14      misho     536:        if (len > AIT_LEN(pkt) - sizeof(struct tagRPCCall)) {
                    537:                rpc_SetErr(EMSGSIZE, "Reply RPC packet not enough buffer space ...");
                    538:                return -1;
                    539:        }
1.17      misho     540:        if (len > ntohl(rpc->call_len) - sizeof(struct tagRPCCall)) {
1.14      misho     541:                rpc_SetErr(EMSGSIZE, "Reply RPC packet is too short ...");
1.8       misho     542:                return -1;
1.5       misho     543:        }
                    544: 
                    545:        /* RPC is OK! Go de-marshaling variables ... */
1.24      misho     546:        if (vars && rpc->call_argc) {
1.12      misho     547: #ifdef CLI_RES_ZCOPY
1.14      misho     548:                *vars = ait_buffer2vars(buf + sizeof(struct tagRPCCall), len, 
1.24      misho     549:                                rpc->call_argc, 42);
1.12      misho     550: #else
1.14      misho     551:                *vars = ait_buffer2vars(buf + sizeof(struct tagRPCCall), len, 
1.24      misho     552:                                rpc->call_argc, 0);
1.12      misho     553: #endif
1.14      misho     554:                if (!*vars) {
                    555:                        rpc_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
1.1       misho     556:                        return -1;
1.5       misho     557:                }
1.1       misho     558:        }
                    559: 
1.14      misho     560:        return ntohl(rpc->call_rep.ret);
                    561: }
                    562: 
                    563: /*
                    564:  * rpc_cli_execCall() - Execute RPC call
                    565:  *
                    566:  * @cli = RPC Client session
1.29    ! misho     567:  * @noreply = >0 We not want RPC reply, -1 IPC request with reply
1.14      misho     568:  * @tag = Function tag for execution
                    569:  * @in_vars = IN function argument array of values, may be NULL
                    570:  * @out_vars = OUT returned array of rpc values, if !=NULL must be free after use with ait_freeVars()
1.21      misho     571:  * return: -1 error, 0 ok result or 1 closed rpc connection
1.14      misho     572:  */
                    573: int
                    574: rpc_cli_execCall(rpc_cli_t *cli, int noreply, u_short tag, 
                    575:                array_t * __restrict in_vars, array_t ** __restrict out_vars)
                    576: {
1.15      misho     577:        int type = 0, wlen;
1.25      misho     578:        u_int seq = 0;
1.14      misho     579: 
                    580:        if (!cli) {
                    581:                rpc_SetErr(EINVAL, "Can`t execute call because parameter is null or invalid!");
                    582:                return -1;
1.15      misho     583:        } else {
1.24      misho     584:                if (cli->cli_id == SOCK_STREAM || cli->cli_id == SOCK_EXT)
1.15      misho     585:                        type = cli->cli_id;
1.25      misho     586:                else
                    587:                        seq = random(); /* sign package */
1.15      misho     588:        }
1.14      misho     589:        if (out_vars)
                    590:                *out_vars = NULL;
                    591: 
1.26      misho     592:        if ((wlen = rpc_pkt_Request(&cli->cli_buf, cli->cli_parent, tag, 
                    593:                                        in_vars, noreply, type, seq)) == -1)
1.14      misho     594:                return -1;
                    595: 
1.26      misho     596:        if ((wlen = rpc_pkt_Send(cli->cli_sock, cli->cli_id, &cli->cli_sa, 
                    597:                                        &cli->cli_buf, wlen)) == -1)
1.14      misho     598:                return -1;
1.29    ! misho     599:        if (!wlen)              /* closed rpc connection */
1.21      misho     600:                return 1;
1.14      misho     601: 
1.29    ! misho     602:        if (noreply > 0)        /* we not want reply */
1.14      misho     603:                return 0;
                    604: 
1.26      misho     605:        if ((wlen = rpc_pkt_Receive(cli->cli_sock, cli->cli_id, &cli->cli_sa, 
                    606:                                        &cli->cli_buf, seq)) == -1)
1.14      misho     607:                return -1;
1.28      misho     608:        if (!wlen) {    /* closed rpc connection */
                    609:                rpc_SetErr(ECONNRESET, "Closed connection");
                    610:                return -1;
                    611:        }
1.14      misho     612: 
1.26      misho     613:        if ((wlen = rpc_pkt_Replay(&cli->cli_buf, cli->cli_parent, tag, 
                    614:                                        out_vars, type)) == -1)
1.14      misho     615:                return -1;
                    616: 
1.28      misho     617:        return wlen;
1.10      misho     618: }
                    619: 
                    620: /*
1.12      misho     621:  * rpc_cli_freeCall() - Free resouce allocated by RPC call
                    622:  *
                    623:  * @out_vars = Returned array with variables from RPC call
                    624:  * return: none
                    625:  */
1.16      misho     626: void
1.12      misho     627: rpc_cli_freeCall(array_t ** __restrict out_vars)
                    628: {
                    629: #ifdef CLI_RES_ZCOPY
1.14      misho     630:        array_Destroy(out_vars);
1.12      misho     631: #else
1.14      misho     632:        ait_freeVars(out_vars);
1.12      misho     633: #endif
                    634: }
                    635: 
                    636: /*
1.10      misho     637:  * rpc_cli_ping() - Ping RPC server
                    638:  *
                    639:  * @cli = connected client
                    640:  * return: -1 error or !=-1 ping seq id
                    641:  */
1.16      misho     642: int
1.10      misho     643: rpc_cli_ping(rpc_cli_t *cli)
                    644: {
                    645:        int ret = 0;
                    646:        array_t *arr = NULL;
                    647: 
                    648:        if (!cli)
                    649:                return -1;
                    650: 
                    651:        if (rpc_cli_execCall(cli, RPC_REPLY, CALL_SRVPING, NULL, &arr))
                    652:                return -1;
                    653:        else
1.14      misho     654:                ret = AIT_GET_U16(array(arr, 0, ait_val_t*));
1.12      misho     655:        rpc_cli_freeCall(&arr);
1.10      misho     656: 
1.5       misho     657:        return ret;
1.1       misho     658: }
1.23      misho     659: 
                    660: 
                    661: /*
                    662:  * rpc_cli_openClient2() - Connect to layer2 RPC Server
                    663:  *
                    664:  * @InstID = InstID for RPC session request
                    665:  * @netBuf = Network buffer length (min:512 bytes), if =0 == BUFSIZ (also meaning max RPC packet)
                    666:  * @csIface = Interface name for bind client, if NULL first interface on host
                    667:  * @csHost = Host ethernet address
                    668:  * return: NULL == error or !=NULL connection to RPC server established
                    669:  */
                    670: rpc_cli_t *
                    671: rpc_cli_openClient2(u_char InstID, int netBuf, const char *csIface, const char *csHost)
                    672: {
1.27      misho     673: #ifndef __linux__
1.23      misho     674:        rpc_cli_t *cli = NULL;
                    675:        sockaddr_t sa = E_SOCKADDR_INIT, sal = E_SOCKADDR_INIT;
                    676:        char szIface[64], szStr[STRSIZ];
                    677:        register int i;
                    678:        struct ifreq ifr;
                    679:        int n = 1;
                    680:        struct bpf_insn insns[] = {
                    681:                BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12),
                    682:                BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, RPC_DEFPORT, 0, 1),
                    683:                BPF_STMT(BPF_RET + BPF_K, -1),
                    684:                BPF_STMT(BPF_RET + BPF_K, 0),
                    685:        };
                    686:        struct bpf_program fcode = { 
                    687:                .bf_len = sizeof(insns) / sizeof(struct bpf_insn), 
                    688:                        .bf_insns = insns
                    689:        };
                    690: 
                    691:        if (!csHost || !e_getlinkbyname(csHost, &sa))
                    692:                return NULL;
                    693:        if (!csIface) {
                    694:                if (e_get1stiface(szIface, sizeof szIface))
                    695:                        return NULL;
                    696:        } else
                    697:                strlcpy(szIface, csIface, sizeof szIface);
                    698:        if (!e_getifacebyname(szIface, &sal))
                    699:                return NULL;
                    700: 
                    701:        cli = e_malloc(sizeof(rpc_cli_t));
                    702:        if (!cli) {
                    703:                LOGERR;
                    704:                return NULL;
                    705:        } else
                    706:                memset(cli, 0, sizeof(rpc_cli_t));
                    707: 
                    708:        /* build session */
                    709:        cli->cli_parent = e_malloc(sizeof(rpc_sess_t));
                    710:        if (!cli->cli_parent) {
                    711:                LOGERR;
                    712:                e_free(cli);
                    713:                return NULL;
                    714:        } else {
                    715:                ((rpc_sess_t*) cli->cli_parent)->sess_version = RPC_VERSION;
                    716:                ((rpc_sess_t*) cli->cli_parent)->sess_instance = InstID;
                    717:        }
                    718: 
                    719:        cli->cli_id = SOCK_BPF;
                    720:        memcpy(&cli->cli_sa, &sa, sizeof cli->cli_sa);
                    721: 
                    722:        /* connect to RPC server */
                    723:        for (i = 0; i < 10; i++) {
                    724:                memset(szStr, 0, sizeof szStr);
                    725:                snprintf(szStr, sizeof szStr, "/dev/bpf%d", i);
                    726:                cli->cli_sock = open(szStr, O_RDWR);
                    727:                if (cli->cli_sock > STDERR_FILENO)
                    728:                        break;
                    729:        }
                    730:        if (cli->cli_sock < 3) {
                    731:                LOGERR;
                    732:                e_free(cli->cli_parent);
                    733:                e_free(cli);
                    734:                return NULL;
                    735:        }
                    736: 
                    737:        if (ioctl(cli->cli_sock, BIOCIMMEDIATE, &n) == -1) {
                    738:                LOGERR;
                    739:                goto err;
                    740:        }
                    741:        if (ioctl(cli->cli_sock, BIOCSETF, &fcode) == -1) {
                    742:                LOGERR;
                    743:                goto err;
                    744:        }
                    745:        n = (netBuf < RPC_MIN_BUFSIZ) ? getpagesize() : E_ALIGN(netBuf, 2);
                    746:        if (ioctl(cli->cli_sock, BIOCSBLEN, &n) == -1) {
                    747:                LOGERR;
                    748:                goto err;
                    749:        } else
                    750:                AIT_SET_BUFSIZ(&cli->cli_buf, 0, n);
                    751: 
                    752:        memset(&ifr, 0, sizeof ifr);
                    753:        strlcpy(ifr.ifr_name, szIface, sizeof ifr.ifr_name);
                    754:        if (ioctl(cli->cli_sock, BIOCSETIF, &ifr) == -1) {
                    755:                LOGERR;
                    756:                goto err;
                    757:        } else
                    758:                fcntl(cli->cli_sock, F_SETFL, 
                    759:                                fcntl(cli->cli_sock, F_GETFL) | O_NONBLOCK);
                    760: 
                    761:        return cli;
                    762: err:
                    763:        AIT_FREE_VAL(&cli->cli_buf);
                    764:        if (cli->cli_sock > 2)
                    765:                close(cli->cli_sock);
                    766:        e_free(cli->cli_parent);
                    767:        e_free(cli);
1.27      misho     768: #else
                    769:        rpc_SetErr(ENOTSUP, "Feature isn't supported on Linux!");
                    770: #endif
1.23      misho     771:        return NULL;
                    772: }
                    773: 
                    774: /*
                    775:  * rpc_cli_closeClient2() - Close layer2 connection to RPC server and free resources
                    776:  *
                    777:  * @cli = RPC Client session
                    778:  * return: none
                    779:  */
                    780: void
                    781: rpc_cli_closeClient2(rpc_cli_t ** __restrict cli)
                    782: {
                    783:        if (!cli || !*cli || (*cli)->cli_id != SOCK_BPF)
                    784:                return;
                    785: 
                    786:        close((*cli)->cli_sock);
                    787: 
                    788:        AIT_FREE_VAL(&(*cli)->cli_buf);
                    789: 
                    790:        if ((*cli)->cli_parent)
                    791:                e_free((*cli)->cli_parent);
                    792: 
                    793:        e_free(*cli);
                    794:        *cli = NULL;
                    795: }
1.24      misho     796: 
                    797: /*
                    798:  * rpc_cli_openClientExt() - Connect to pipe RPC Server
                    799:  *
                    800:  * @InstID = InstID for RPC session request
                    801:  * @netBuf = Network buffer length (min:512 bytes), if =0 == BUFSIZ (also meaning max RPC packet)
                    802:  * @fd = File descriptor
                    803:  * return: NULL == error or !=NULL connection to RPC server established
                    804:  */
                    805: rpc_cli_t *
                    806: rpc_cli_openClientExt(u_char InstID, int netBuf, int fd)
                    807: {
                    808:        rpc_cli_t *cli = NULL;
                    809:        int n;
                    810: 
                    811:        cli = e_malloc(sizeof(rpc_cli_t));
                    812:        if (!cli) {
                    813:                LOGERR;
                    814:                return NULL;
                    815:        } else
                    816:                memset(cli, 0, sizeof(rpc_cli_t));
                    817: 
                    818:        /* build session */
                    819:        cli->cli_parent = e_malloc(sizeof(rpc_sess_t));
                    820:        if (!cli->cli_parent) {
                    821:                LOGERR;
                    822:                e_free(cli);
                    823:                return NULL;
                    824:        } else {
                    825:                ((rpc_sess_t*) cli->cli_parent)->sess_version = RPC_VERSION;
                    826:                ((rpc_sess_t*) cli->cli_parent)->sess_instance = InstID;
                    827:        }
                    828: 
                    829:        cli->cli_id = SOCK_EXT;
                    830:        cli->cli_sock = fd;
                    831: 
                    832:        n = (netBuf < RPC_MIN_BUFSIZ) ? getpagesize() : E_ALIGN(netBuf, 2);
                    833:        AIT_SET_BUFSIZ(&cli->cli_buf, 0, n);
                    834: 
                    835:        fcntl(cli->cli_sock, F_SETFL, 
                    836:                        fcntl(cli->cli_sock, F_GETFL) | O_NONBLOCK);
                    837: 
                    838:        return cli;
                    839: }
                    840: 
                    841: /*
                    842:  * rpc_cli_closeClientExt() - Close pipe connection to RPC server and free resources
                    843:  *
                    844:  * @cli = RPC Client session
                    845:  * return: none
                    846:  */
                    847: void
                    848: rpc_cli_closeClientExt(rpc_cli_t ** __restrict cli)
                    849: {
                    850:        if (!cli || !*cli || (*cli)->cli_id != SOCK_EXT)
                    851:                return;
                    852: 
                    853:        AIT_FREE_VAL(&(*cli)->cli_buf);
                    854: 
                    855:        if ((*cli)->cli_parent)
                    856:                e_free((*cli)->cli_parent);
                    857: 
                    858:        e_free(*cli);
                    859:        *cli = NULL;
                    860: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>