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

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.24.2.4! misho       6: * $Id: cli.c,v 1.24.2.3 2015/05/18 14:50:12 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.24      misho      15: Copyright 2004 - 2015
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.14      misho      74:        memcpy(&cli->cli_sa, &rpccli->cli_sa, sizeof(sockaddr_t));
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.6       misho     114:        if (connect(cli->cli_sock, &cli->cli_sa.sa, cli->cli_sa.sa.sa_len) == -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: {
                    161:        rpc_cli_t *cli = NULL;
1.14      misho     162:        sockaddr_t sa = E_SOCKADDR_INIT;
1.1       misho     163: 
1.24.2.2  misho     164:        if (proto < 0 || proto > SOCK_RAW) {
                    165:                rpc_SetErr(EINVAL, "Invalid parameters can`t open RPC client");
                    166:                return NULL;
                    167:        }
                    168: 
1.24.2.3  misho     169:        srandom(time(NULL) ^ getpid());
                    170: 
                    171:        if (!Port && proto < SOCK_RAW)
                    172:                Port = RPC_DEFPORT;
1.14      misho     173:        if (!e_gethostbyname(csHost, Port, &sa))
1.1       misho     174:                return NULL;
1.13      misho     175:        if (!proto)
                    176:                proto = SOCK_STREAM;
1.10      misho     177:        if (netBuf < RPC_MIN_BUFSIZ)
1.5       misho     178:                netBuf = BUFSIZ;
1.10      misho     179:        else
1.14      misho     180:                netBuf = E_ALIGN(netBuf, 2);    /* align netBuf length */
1.10      misho     181: 
1.14      misho     182:        cli = e_malloc(sizeof(rpc_cli_t));
1.1       misho     183:        if (!cli) {
                    184:                LOGERR;
                    185:                return NULL;
                    186:        } else
                    187:                memset(cli, 0, sizeof(rpc_cli_t));
1.5       misho     188: 
1.10      misho     189:        /* build session */
1.14      misho     190:        cli->cli_parent = e_malloc(sizeof(rpc_sess_t));
1.1       misho     191:        if (!cli->cli_parent) {
                    192:                LOGERR;
1.14      misho     193:                e_free(cli);
1.1       misho     194:                return NULL;
                    195:        } else {
                    196:                ((rpc_sess_t*) cli->cli_parent)->sess_version = RPC_VERSION;
1.15      misho     197:                ((rpc_sess_t*) cli->cli_parent)->sess_instance = InstID;
1.1       misho     198:        }
                    199: 
1.13      misho     200:        cli->cli_id = proto;
1.6       misho     201:        memcpy(&cli->cli_sa, &sa, sizeof cli->cli_sa);
1.14      misho     202:        AIT_SET_BUFSIZ(&cli->cli_buf, 0, netBuf);
1.4       misho     203: 
                    204:        /* connect to RPC server */
1.24.2.2  misho     205:        cli->cli_sock = socket(cli->cli_sa.sa.sa_family, cli->cli_id, 
                    206:                        cli->cli_id == SOCK_RAW ? IPPROTO_ERPC : 0);
1.1       misho     207:        if (cli->cli_sock == -1) {
                    208:                LOGERR;
1.13      misho     209:                goto err;
                    210:        }
                    211:        if (setsockopt(cli->cli_sock, SOL_SOCKET, SO_SNDBUF, 
                    212:                                &netBuf, sizeof netBuf) == -1) {
                    213:                LOGERR;
                    214:                goto err;
1.5       misho     215:        }
1.13      misho     216:        if (setsockopt(cli->cli_sock, SOL_SOCKET, SO_RCVBUF, 
                    217:                                &netBuf, sizeof netBuf) == -1) {
1.1       misho     218:                LOGERR;
1.13      misho     219:                goto err;
                    220:        }
                    221:        if (cli->cli_id == SOCK_STREAM)
                    222:                if (connect(cli->cli_sock, &cli->cli_sa.sa, cli->cli_sa.sa.sa_len) == -1) {
                    223:                        LOGERR;
                    224:                        goto err;
                    225:                }
1.1       misho     226: 
1.13      misho     227:        fcntl(cli->cli_sock, F_SETFL, fcntl(cli->cli_sock, F_GETFL) | O_NONBLOCK);
1.1       misho     228:        return cli;
1.13      misho     229: err:
                    230:        AIT_FREE_VAL(&cli->cli_buf);
                    231:        if (cli->cli_sock > 2)
                    232:                close(cli->cli_sock);
1.14      misho     233:        e_free(cli->cli_parent);
                    234:        e_free(cli);
1.13      misho     235:        return NULL;
1.1       misho     236: }
                    237: 
                    238: /*
1.21      misho     239:  * rpc_cli_reconnectClient() - Reconnecting client to RPC server
                    240:  *
                    241:  * @cli = RPC Client session
                    242:  * return: -1 error or 0 ok
                    243:  */
                    244: int
                    245: rpc_cli_reconnectClient(rpc_cli_t * __restrict cli)
                    246: {
                    247:        int netBuf;
                    248: 
                    249:        if (!cli)
                    250:                return -1;
                    251:        else
                    252:                netBuf = AIT_LEN(&cli->cli_buf);
                    253: 
                    254:        close(cli->cli_sock);
                    255: 
1.24.2.3  misho     256:        srandom(time(NULL) ^ getpid());
                    257: 
1.21      misho     258:        cli->cli_sock = socket(cli->cli_sa.sa.sa_family, cli->cli_id, 0);
                    259:        if (cli->cli_sock == -1) {
                    260:                LOGERR;
                    261:                return -1;
                    262:        }
                    263:        if (setsockopt(cli->cli_sock, SOL_SOCKET, SO_SNDBUF, 
                    264:                                &netBuf, sizeof netBuf) == -1) {
                    265:                LOGERR;
                    266:                close(cli->cli_sock);
                    267:                return -1;
                    268:        }
                    269:        if (setsockopt(cli->cli_sock, SOL_SOCKET, SO_RCVBUF, 
                    270:                                &netBuf, sizeof netBuf) == -1) {
                    271:                LOGERR;
                    272:                close(cli->cli_sock);
                    273:                return -1;
                    274:        }
                    275:        if (cli->cli_id == SOCK_STREAM)
                    276:                if (connect(cli->cli_sock, &cli->cli_sa.sa, cli->cli_sa.sa.sa_len) == -1) {
                    277:                        LOGERR;
                    278:                        close(cli->cli_sock);
                    279:                        return -1;
                    280:                }
                    281: 
                    282:        fcntl(cli->cli_sock, F_SETFL, fcntl(cli->cli_sock, F_GETFL) | O_NONBLOCK);
                    283:        return 0;
                    284: }
                    285: 
                    286: /*
1.7       misho     287:  * rpc_cli_closeClient() - Close connection to RPC server and free resources
                    288:  *
1.1       misho     289:  * @cli = RPC Client session
                    290:  * return: none
                    291:  */
                    292: void
1.10      misho     293: rpc_cli_closeClient(rpc_cli_t ** __restrict cli)
1.1       misho     294: {
1.23      misho     295:        if (!cli || !*cli || (*cli)->cli_id == SOCK_BPF)
1.1       misho     296:                return;
                    297: 
1.13      misho     298:        if ((*cli)->cli_id == SOCK_STREAM)
                    299:                shutdown((*cli)->cli_sock, SHUT_RDWR);
1.10      misho     300:        close((*cli)->cli_sock);
                    301: 
                    302:        AIT_FREE_VAL(&(*cli)->cli_buf);
                    303: 
                    304:        if ((*cli)->cli_parent)
1.14      misho     305:                e_free((*cli)->cli_parent);
1.1       misho     306: 
1.14      misho     307:        e_free(*cli);
1.10      misho     308:        *cli = NULL;
1.1       misho     309: }
                    310: 
                    311: /*
1.14      misho     312:  * rpc_pkt_Send() - Send RPC packet
1.7       misho     313:  *
1.14      misho     314:  * @sock = Socket
                    315:  * @type = Type of socket
                    316:  * @sa = Server address
                    317:  * @pkt = RPC packet
                    318:  * @len = Length of packet
1.21      misho     319:  * return: -1 error, 0  EOF or >0 sended bytes
1.1       misho     320:  */
                    321: int
1.14      misho     322: rpc_pkt_Send(int sock, int type, sockaddr_t * __restrict sa, ait_val_t * __restrict pkt, int len)
1.1       misho     323: {
1.14      misho     324:        if (!pkt) {
                    325:                rpc_SetErr(EINVAL, "Invalid argument(s)!");
1.1       misho     326:                return -1;
1.24.2.3  misho     327:        }
1.7       misho     328: 
1.24.2.3  misho     329:        return rpc_Write(sock, type, MSG_NOSIGNAL, sa, pkt, len);
1.14      misho     330: }
                    331: 
                    332: /*
                    333:  * rpc_pkt_Receive() - Receive RPC packet
                    334:  *
                    335:  * @sock = Socket
                    336:  * @type = Type of socket
                    337:  * @sa = Server address
                    338:  * @pkt = RPC packet
1.24.2.3  misho     339:  * @seq = Signed packet with seq.no
1.21      misho     340:  * return: -1 error, 0 EOF or >0 received bytes
1.14      misho     341:  */
                    342: int
1.24.2.3  misho     343: rpc_pkt_Receive(int sock, int type, sockaddr_t * __restrict sa, ait_val_t * __restrict pkt, int seq)
1.14      misho     344: {
1.24.2.3  misho     345:        int ret;
1.17      misho     346:        struct tagRPCCall *rpc;
1.14      misho     347: 
                    348:        if (!pkt) {
                    349:                rpc_SetErr(EINVAL, "Invalid argument(s)!");
                    350:                return -1;
1.20      misho     351:        }
1.10      misho     352: 
                    353:        /* reply from RPC server */
                    354:        do {
1.24.2.3  misho     355:                if (type == SOCK_STREAM || type == SOCK_EXT)
                    356:                        ret = rpc_Read(sock, type, 0, NULL, pkt);
                    357:                else
                    358:                        ret = rpc_Read(sock, type, 0, sa, pkt);
                    359: 
                    360:                if (ret > 0) {
                    361:                        rpc = (struct tagRPCCall*) AIT_GET_BUF(pkt);
                    362: 
                    363:                        /* check for loop request */
                    364:                        if (!(rpc->call_io & RPC_ACK))
                    365:                                continue;
                    366:                        /* check for signed request */
                    367:                        if (seq != ntohl(rpc->call_seq))
                    368:                                continue;
1.17      misho     369:                }
1.24.2.3  misho     370:        } while (ret < 1);
1.8       misho     371: 
1.14      misho     372:        return ret;
                    373: }
                    374: 
                    375: /*
                    376:  * rpc_pkt_Request() - Build RPC Request packet
                    377:  *
                    378:  * @pkt = Packet buffer
                    379:  * @sess = RPC session info
                    380:  * @tag = Function tag for execution
                    381:  * @vars = Function argument array of values, may be NULL
                    382:  * @noreply = We not want RPC reply
1.15      misho     383:  * @nocrc = Without CRC calculation
1.24.2.3  misho     384:  * @seq = Sign packet with seq.no
1.14      misho     385:  * return: -1 error or != -1 prepared bytes into packet
                    386:  */
                    387: int
                    388: rpc_pkt_Request(ait_val_t * __restrict pkt, rpc_sess_t * __restrict sess, u_short tag, 
1.24.2.3  misho     389:                array_t * __restrict vars, int noreply, int nocrc, int seq)
1.14      misho     390: {
                    391:        struct tagRPCCall *rpc;
1.17      misho     392:        int ret = 0, estlen, len = sizeof(struct tagRPCCall);
1.14      misho     393:        u_char *buf;
                    394: 
                    395:        if (!pkt || !sess) {
                    396:                rpc_SetErr(EINVAL, "Invalid argument(s)!");
                    397:                return -1;
1.17      misho     398:        }
                    399: 
                    400:        /* calc estimated length */
                    401:        estlen = ait_resideVars(vars) + len;
                    402:        if (estlen > AIT_LEN(pkt))
                    403:                AIT_RE_BUF(pkt, estlen);
                    404:        buf = AIT_GET_BUF(pkt);
1.14      misho     405: 
                    406:        /* prepare RPC call */
                    407:        rpc = (struct tagRPCCall*) buf;
                    408:        rpc_addPktSession(&rpc->call_session, sess);
                    409:        rpc->call_tag = htons(tag);
                    410:        if (!vars)
                    411:                rpc->call_argc = 0;
                    412:        else
1.24      misho     413:                rpc->call_argc = (u_char) array_Size(vars);
1.14      misho     414: 
                    415:        /* set reply */
1.19      misho     416:        rpc->call_req.flags = (uint64_t) htonl(noreply ? RPC_NOREPLY : RPC_REPLY);
1.14      misho     417: 
                    418:        if (array_Size(vars)) {
                    419:                /* marshaling variables */
                    420:                ret = ait_vars2buffer(buf + len, AIT_LEN(pkt) - len, vars);
                    421:                if (ret == -1) {
                    422:                        rpc_SetErr(EBADRPC, "Failed to prepare RPC packet values");
                    423:                        return -1;
                    424:                } else
                    425:                        len += ret;
                    426:        }
                    427: 
                    428:        /* total packet length */
1.17      misho     429:        rpc->call_len = htonl(len);
1.24      misho     430:        rpc->call_io = RPC_REQ;
1.14      misho     431: 
1.24.2.3  misho     432:        /* sign packet */
                    433:        rpc->call_seq = htonl(seq);
                    434: 
1.15      misho     435:        if (!nocrc) {
                    436:                /* calculate CRC */
                    437:                rpc->call_crc ^= rpc->call_crc;
                    438:                rpc->call_crc = htons(crcFletcher16((u_short*) buf, len / 2));
                    439:        }
1.14      misho     440: 
                    441:        return len;
                    442: }
                    443: 
                    444: /*
                    445:  * rpc_pkt_Replay() - Decode RPC Replay packet
                    446:  *
                    447:  * @pkt = Packet buffer
1.23      misho     448:  * @sess = RPC session info, if =NULL don't check session
1.24.2.1  misho     449:  * @tag = Function tag, if =CALL_TAG_MAX don't check tag
1.14      misho     450:  * @vars = Function argument array of values, may be NULL
1.15      misho     451:  * @nocrc = Without CRC calculation
1.14      misho     452:  * return: -1 error or != -1 return value from function
                    453:  */
                    454: int
                    455: rpc_pkt_Replay(ait_val_t * __restrict pkt, rpc_sess_t * __restrict sess, u_short tag, 
1.15      misho     456:                array_t ** __restrict vars, int nocrc)
1.14      misho     457: {
                    458:        struct tagRPCCall *rpc;
                    459:        int len;
                    460:        u_char *buf;
                    461:        uint16_t crc;
                    462: 
1.23      misho     463:        if (!pkt) {
1.14      misho     464:                rpc_SetErr(EINVAL, "Invalid argument(s)!");
                    465:                return -1;
                    466:        } else
                    467:                buf = AIT_GET_BUF(pkt);
                    468: 
                    469:        rpc = (struct tagRPCCall*) buf;
1.15      misho     470:        if (!nocrc) {
                    471:                /* calculate CRC */
                    472:                crc = ntohs(rpc->call_crc);
                    473:                rpc->call_crc ^= rpc->call_crc;
1.17      misho     474:                if (crc != crcFletcher16((u_short*) buf, ntohl(rpc->call_len) / 2)) {
1.15      misho     475:                        rpc_SetErr(ERPCMISMATCH, "Bad CRC RPC packet");
                    476:                        return -1;
                    477:                }
1.8       misho     478:        }
                    479: 
1.3       misho     480:        /* check RPC packet session info */
1.23      misho     481:        if (sess && rpc_chkPktSession(&rpc->call_session, sess)) {
1.10      misho     482:                rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session");
1.8       misho     483:                return -1;
1.14      misho     484:        }
1.24.2.1  misho     485:        if (tag != CALL_TAG_MAX && ntohs(rpc->call_tag) != tag) {
1.10      misho     486:                rpc_SetErr(ERPCMISMATCH, "Get wrong RPC reply");
1.8       misho     487:                return -1;
1.6       misho     488:        }
1.10      misho     489:        if (ntohl(rpc->call_rep.eno) && ntohl(rpc->call_rep.ret) == -1) {
1.8       misho     490:                rpc_SetErr(ntohl(rpc->call_rep.eno), "Server side: retcode=%d #%d %s", 
1.7       misho     491:                                ntohl(rpc->call_rep.ret), ntohl(rpc->call_rep.eno), 
                    492:                                strerror(ntohl(rpc->call_rep.eno)));
1.8       misho     493:                return -1;
1.1       misho     494:        }
1.24      misho     495:        len = rpc->call_argc * sizeof(ait_val_t);
1.14      misho     496:        if (len > AIT_LEN(pkt) - sizeof(struct tagRPCCall)) {
                    497:                rpc_SetErr(EMSGSIZE, "Reply RPC packet not enough buffer space ...");
                    498:                return -1;
                    499:        }
1.17      misho     500:        if (len > ntohl(rpc->call_len) - sizeof(struct tagRPCCall)) {
1.14      misho     501:                rpc_SetErr(EMSGSIZE, "Reply RPC packet is too short ...");
1.8       misho     502:                return -1;
1.5       misho     503:        }
                    504: 
                    505:        /* RPC is OK! Go de-marshaling variables ... */
1.24      misho     506:        if (vars && rpc->call_argc) {
1.12      misho     507: #ifdef CLI_RES_ZCOPY
1.14      misho     508:                *vars = ait_buffer2vars(buf + sizeof(struct tagRPCCall), len, 
1.24      misho     509:                                rpc->call_argc, 42);
1.12      misho     510: #else
1.14      misho     511:                *vars = ait_buffer2vars(buf + sizeof(struct tagRPCCall), len, 
1.24      misho     512:                                rpc->call_argc, 0);
1.12      misho     513: #endif
1.14      misho     514:                if (!*vars) {
                    515:                        rpc_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
1.1       misho     516:                        return -1;
1.5       misho     517:                }
1.1       misho     518:        }
                    519: 
1.14      misho     520:        return ntohl(rpc->call_rep.ret);
                    521: }
                    522: 
                    523: /*
                    524:  * rpc_cli_execCall() - Execute RPC call
                    525:  *
                    526:  * @cli = RPC Client session
                    527:  * @noreply = We not want RPC reply
                    528:  * @tag = Function tag for execution
                    529:  * @in_vars = IN function argument array of values, may be NULL
                    530:  * @out_vars = OUT returned array of rpc values, if !=NULL must be free after use with ait_freeVars()
1.21      misho     531:  * return: -1 error, 0 ok result or 1 closed rpc connection
1.14      misho     532:  */
                    533: int
                    534: rpc_cli_execCall(rpc_cli_t *cli, int noreply, u_short tag, 
                    535:                array_t * __restrict in_vars, array_t ** __restrict out_vars)
                    536: {
1.15      misho     537:        int type = 0, wlen;
1.14      misho     538:        u_char *buf;
1.24.2.3  misho     539:        u_int seq = 0;
1.14      misho     540: 
                    541:        if (!cli) {
                    542:                rpc_SetErr(EINVAL, "Can`t execute call because parameter is null or invalid!");
                    543:                return -1;
1.15      misho     544:        } else {
1.24      misho     545:                if (cli->cli_id == SOCK_STREAM || cli->cli_id == SOCK_EXT)
1.15      misho     546:                        type = cli->cli_id;
1.24.2.3  misho     547:                else
                    548:                        seq = random(); /* sign package */
1.14      misho     549:                buf = AIT_GET_BUF(&cli->cli_buf);
1.15      misho     550:        }
1.14      misho     551:        if (out_vars)
                    552:                *out_vars = NULL;
                    553: 
1.24.2.3  misho     554:        if ((wlen = rpc_pkt_Request(&cli->cli_buf, cli->cli_parent, tag, in_vars, noreply, type, seq)) == -1)
1.14      misho     555:                return -1;
                    556: 
1.21      misho     557:        if ((wlen = rpc_pkt_Send(cli->cli_sock, cli->cli_id, &cli->cli_sa, &cli->cli_buf, wlen)) == -1)
1.14      misho     558:                return -1;
1.21      misho     559:        if (!wlen)      /* closed rpc connection */
                    560:                return 1;
1.14      misho     561: 
                    562:        if (noreply)    /* we not want reply */
                    563:                return 0;
                    564: 
1.24.2.3  misho     565:        if ((wlen = rpc_pkt_Receive(cli->cli_sock, cli->cli_id, &cli->cli_sa, &cli->cli_buf, seq)) == -1)
1.14      misho     566:                return -1;
1.21      misho     567:        if (!wlen)      /* closed rpc connection */
                    568:                return 1;
1.14      misho     569: 
1.15      misho     570:        if ((wlen = rpc_pkt_Replay(&cli->cli_buf, cli->cli_parent, tag, out_vars, type)) == -1)
1.14      misho     571:                return -1;
                    572: 
1.15      misho     573:        return 0;
1.10      misho     574: }
                    575: 
                    576: /*
1.12      misho     577:  * rpc_cli_freeCall() - Free resouce allocated by RPC call
                    578:  *
                    579:  * @out_vars = Returned array with variables from RPC call
                    580:  * return: none
                    581:  */
1.16      misho     582: void
1.12      misho     583: rpc_cli_freeCall(array_t ** __restrict out_vars)
                    584: {
                    585: #ifdef CLI_RES_ZCOPY
1.14      misho     586:        array_Destroy(out_vars);
1.12      misho     587: #else
1.14      misho     588:        ait_freeVars(out_vars);
1.12      misho     589: #endif
                    590: }
                    591: 
                    592: /*
1.10      misho     593:  * rpc_cli_ping() - Ping RPC server
                    594:  *
                    595:  * @cli = connected client
                    596:  * return: -1 error or !=-1 ping seq id
                    597:  */
1.16      misho     598: int
1.10      misho     599: rpc_cli_ping(rpc_cli_t *cli)
                    600: {
                    601:        int ret = 0;
                    602:        array_t *arr = NULL;
                    603: 
                    604:        if (!cli)
                    605:                return -1;
                    606: 
                    607:        if (rpc_cli_execCall(cli, RPC_REPLY, CALL_SRVPING, NULL, &arr))
                    608:                return -1;
                    609:        else
1.14      misho     610:                ret = AIT_GET_U16(array(arr, 0, ait_val_t*));
1.12      misho     611:        rpc_cli_freeCall(&arr);
1.10      misho     612: 
1.5       misho     613:        return ret;
1.1       misho     614: }
1.23      misho     615: 
                    616: 
                    617: /*
                    618:  * rpc_cli_openClient2() - Connect to layer2 RPC Server
                    619:  *
                    620:  * @InstID = InstID for RPC session request
                    621:  * @netBuf = Network buffer length (min:512 bytes), if =0 == BUFSIZ (also meaning max RPC packet)
                    622:  * @csIface = Interface name for bind client, if NULL first interface on host
                    623:  * @csHost = Host ethernet address
                    624:  * return: NULL == error or !=NULL connection to RPC server established
                    625:  */
                    626: rpc_cli_t *
                    627: rpc_cli_openClient2(u_char InstID, int netBuf, const char *csIface, const char *csHost)
                    628: {
                    629:        rpc_cli_t *cli = NULL;
                    630:        sockaddr_t sa = E_SOCKADDR_INIT, sal = E_SOCKADDR_INIT;
                    631:        char szIface[64], szStr[STRSIZ];
                    632:        register int i;
                    633:        struct ifreq ifr;
                    634:        int n = 1;
                    635:        struct bpf_insn insns[] = {
                    636:                BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12),
                    637:                BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, RPC_DEFPORT, 0, 1),
                    638:                BPF_STMT(BPF_RET + BPF_K, -1),
                    639:                BPF_STMT(BPF_RET + BPF_K, 0),
                    640:        };
                    641:        struct bpf_program fcode = { 
                    642:                .bf_len = sizeof(insns) / sizeof(struct bpf_insn), 
                    643:                        .bf_insns = insns
                    644:        };
                    645: 
                    646:        if (!csHost || !e_getlinkbyname(csHost, &sa))
                    647:                return NULL;
                    648:        if (!csIface) {
                    649:                if (e_get1stiface(szIface, sizeof szIface))
                    650:                        return NULL;
                    651:        } else
                    652:                strlcpy(szIface, csIface, sizeof szIface);
                    653:        if (!e_getifacebyname(szIface, &sal))
                    654:                return NULL;
                    655: 
                    656:        cli = e_malloc(sizeof(rpc_cli_t));
                    657:        if (!cli) {
                    658:                LOGERR;
                    659:                return NULL;
                    660:        } else
                    661:                memset(cli, 0, sizeof(rpc_cli_t));
                    662: 
                    663:        /* build session */
                    664:        cli->cli_parent = e_malloc(sizeof(rpc_sess_t));
                    665:        if (!cli->cli_parent) {
                    666:                LOGERR;
                    667:                e_free(cli);
                    668:                return NULL;
                    669:        } else {
                    670:                ((rpc_sess_t*) cli->cli_parent)->sess_version = RPC_VERSION;
                    671:                ((rpc_sess_t*) cli->cli_parent)->sess_instance = InstID;
                    672:        }
                    673: 
                    674:        cli->cli_id = SOCK_BPF;
                    675:        memcpy(&cli->cli_sa, &sa, sizeof cli->cli_sa);
                    676: 
                    677:        /* connect to RPC server */
                    678:        for (i = 0; i < 10; i++) {
                    679:                memset(szStr, 0, sizeof szStr);
                    680:                snprintf(szStr, sizeof szStr, "/dev/bpf%d", i);
                    681:                cli->cli_sock = open(szStr, O_RDWR);
                    682:                if (cli->cli_sock > STDERR_FILENO)
                    683:                        break;
                    684:        }
                    685:        if (cli->cli_sock < 3) {
                    686:                LOGERR;
                    687:                e_free(cli->cli_parent);
                    688:                e_free(cli);
                    689:                return NULL;
                    690:        }
                    691: 
                    692:        if (ioctl(cli->cli_sock, BIOCIMMEDIATE, &n) == -1) {
                    693:                LOGERR;
                    694:                goto err;
                    695:        }
                    696:        if (ioctl(cli->cli_sock, BIOCSETF, &fcode) == -1) {
                    697:                LOGERR;
                    698:                goto err;
                    699:        }
                    700:        n = (netBuf < RPC_MIN_BUFSIZ) ? getpagesize() : E_ALIGN(netBuf, 2);
                    701:        if (ioctl(cli->cli_sock, BIOCSBLEN, &n) == -1) {
                    702:                LOGERR;
                    703:                goto err;
                    704:        } else
                    705:                AIT_SET_BUFSIZ(&cli->cli_buf, 0, n);
                    706: 
                    707:        memset(&ifr, 0, sizeof ifr);
                    708:        strlcpy(ifr.ifr_name, szIface, sizeof ifr.ifr_name);
                    709:        if (ioctl(cli->cli_sock, BIOCSETIF, &ifr) == -1) {
                    710:                LOGERR;
                    711:                goto err;
                    712:        } else
                    713:                fcntl(cli->cli_sock, F_SETFL, 
                    714:                                fcntl(cli->cli_sock, F_GETFL) | O_NONBLOCK);
                    715: 
                    716:        return cli;
                    717: err:
                    718:        AIT_FREE_VAL(&cli->cli_buf);
                    719:        if (cli->cli_sock > 2)
                    720:                close(cli->cli_sock);
                    721:        e_free(cli->cli_parent);
                    722:        e_free(cli);
                    723:        return NULL;
                    724: }
                    725: 
                    726: /*
                    727:  * rpc_cli_closeClient2() - Close layer2 connection to RPC server and free resources
                    728:  *
                    729:  * @cli = RPC Client session
                    730:  * return: none
                    731:  */
                    732: void
                    733: rpc_cli_closeClient2(rpc_cli_t ** __restrict cli)
                    734: {
                    735:        if (!cli || !*cli || (*cli)->cli_id != SOCK_BPF)
                    736:                return;
                    737: 
                    738:        close((*cli)->cli_sock);
                    739: 
                    740:        AIT_FREE_VAL(&(*cli)->cli_buf);
                    741: 
                    742:        if ((*cli)->cli_parent)
                    743:                e_free((*cli)->cli_parent);
                    744: 
                    745:        e_free(*cli);
                    746:        *cli = NULL;
                    747: }
1.24      misho     748: 
                    749: /*
                    750:  * rpc_cli_openClientExt() - Connect to pipe RPC Server
                    751:  *
                    752:  * @InstID = InstID for RPC session request
                    753:  * @netBuf = Network buffer length (min:512 bytes), if =0 == BUFSIZ (also meaning max RPC packet)
                    754:  * @fd = File descriptor
                    755:  * return: NULL == error or !=NULL connection to RPC server established
                    756:  */
                    757: rpc_cli_t *
                    758: rpc_cli_openClientExt(u_char InstID, int netBuf, int fd)
                    759: {
                    760:        rpc_cli_t *cli = NULL;
                    761:        int n;
                    762: 
                    763:        cli = e_malloc(sizeof(rpc_cli_t));
                    764:        if (!cli) {
                    765:                LOGERR;
                    766:                return NULL;
                    767:        } else
                    768:                memset(cli, 0, sizeof(rpc_cli_t));
                    769: 
                    770:        /* build session */
                    771:        cli->cli_parent = e_malloc(sizeof(rpc_sess_t));
                    772:        if (!cli->cli_parent) {
                    773:                LOGERR;
                    774:                e_free(cli);
                    775:                return NULL;
                    776:        } else {
                    777:                ((rpc_sess_t*) cli->cli_parent)->sess_version = RPC_VERSION;
                    778:                ((rpc_sess_t*) cli->cli_parent)->sess_instance = InstID;
                    779:        }
                    780: 
                    781:        cli->cli_id = SOCK_EXT;
                    782:        cli->cli_sock = fd;
                    783: 
                    784:        n = (netBuf < RPC_MIN_BUFSIZ) ? getpagesize() : E_ALIGN(netBuf, 2);
                    785:        AIT_SET_BUFSIZ(&cli->cli_buf, 0, n);
                    786: 
                    787:        fcntl(cli->cli_sock, F_SETFL, 
                    788:                        fcntl(cli->cli_sock, F_GETFL) | O_NONBLOCK);
                    789: 
                    790:        return cli;
                    791: }
                    792: 
                    793: /*
                    794:  * rpc_cli_closeClientExt() - Close pipe connection to RPC server and free resources
                    795:  *
                    796:  * @cli = RPC Client session
                    797:  * return: none
                    798:  */
                    799: void
                    800: rpc_cli_closeClientExt(rpc_cli_t ** __restrict cli)
                    801: {
                    802:        if (!cli || !*cli || (*cli)->cli_id != SOCK_EXT)
                    803:                return;
                    804: 
                    805:        AIT_FREE_VAL(&(*cli)->cli_buf);
                    806: 
                    807:        if ((*cli)->cli_parent)
                    808:                e_free((*cli)->cli_parent);
                    809: 
                    810:        e_free(*cli);
                    811:        *cli = NULL;
                    812: }

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