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

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.9.2.1 ! misho       6: * $Id: cli.c,v 1.9 2012/05/14 08:39:05 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.7       misho      15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
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.6       misho      60:        io_sockaddr_t sa;
1.5       misho      61:        int n;
1.4       misho      62: 
                     63:        if (!rpccli || 
1.6       misho      64:                        (rpccli->cli_sa.sa.sa_family != AF_INET && rpccli->cli_sa.sa.sa_family != AF_INET6 && 
                     65:                         rpccli->cli_sa.sa.sa_family != AF_LOCAL)) {
1.7       misho      66:                rpc_SetErr(EINVAL, "Invalid parameters can`t connect to BLOB server");
1.2       misho      67:                return NULL;
                     68:        }
                     69: 
                     70:        cli = malloc(sizeof(rpc_cli_t));
                     71:        if (!cli) {
                     72:                LOGERR;
                     73:                return NULL;
                     74:        } else
                     75:                memcpy(cli, rpccli, sizeof(rpc_cli_t));
                     76: 
1.4       misho      77:        memcpy(&sa, &rpccli->cli_sa, sizeof sa);
1.6       misho      78:        switch (sa.sa.sa_family) {
1.4       misho      79:                case AF_INET:
1.6       misho      80:                        sa.sin.sin_port = htons(Port ? Port : ntohs(sa.sin.sin_port) + 1);
1.4       misho      81:                        break;
                     82:                case AF_INET6:
1.6       misho      83:                        sa.sin6.sin6_port = htons(Port ? Port : ntohs(sa.sin6.sin6_port) + 1);
1.4       misho      84:                        break;
                     85:                case AF_LOCAL:
1.6       misho      86:                        strlcat(sa.sun.sun_path, ".blob", sizeof sa.sun.sun_path);
1.4       misho      87:                        break;
1.2       misho      88:        }
1.6       misho      89:        memcpy(&cli->cli_sa, &sa, sizeof sa);
1.2       misho      90: 
1.4       misho      91:        /* connect to BLOB server */
1.6       misho      92:        cli->cli_sock = socket(cli->cli_sa.sa.sa_family, SOCK_STREAM, 0);
1.2       misho      93:        if (cli->cli_sock == -1) {
                     94:                LOGERR;
                     95:                free(cli);
                     96:                return NULL;
                     97:        }
1.5       misho      98:        n = cli->cli_netbuf;
                     99:        if (setsockopt(cli->cli_sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof n) == -1) {
                    100:                LOGERR;
                    101:                close(cli->cli_sock);
                    102:                free(cli);
                    103:                return NULL;
                    104:        }
                    105:        if (setsockopt(cli->cli_sock, SOL_SOCKET, SO_RCVBUF, &n, sizeof n) == -1) {
                    106:                LOGERR;
                    107:                close(cli->cli_sock);
                    108:                free(cli);
                    109:                return NULL;
                    110:        }
1.6       misho     111:        if (connect(cli->cli_sock, &cli->cli_sa.sa, cli->cli_sa.sa.sa_len) == -1) {
1.2       misho     112:                LOGERR;
1.5       misho     113:                close(cli->cli_sock);
1.2       misho     114:                free(cli);
                    115:                return NULL;
                    116:        }
                    117: 
1.9.2.1 ! misho     118:        cli->cli_kill = enable;
1.2       misho     119:        return cli;
                    120: }
                    121: 
                    122: /*
1.7       misho     123:  * rpc_cli_closeBLOBClient() - Close connection to BLOB server and free resources
                    124:  *
1.2       misho     125:  * @cli = BLOB Client session
                    126:  * return: none
                    127:  */
                    128: void
                    129: rpc_cli_closeBLOBClient(rpc_cli_t * __restrict cli)
                    130: {
                    131:        if (!cli) {
1.7       misho     132:                rpc_SetErr(EINVAL, "Can`t close connection because parameter is null!");
1.2       misho     133:                return;
                    134:        }
1.9.2.1 ! misho     135:        cli->cli_kill = disable;
1.2       misho     136: 
                    137:        shutdown(cli->cli_sock, SHUT_RDWR);
                    138:        close(cli->cli_sock);
                    139: 
                    140:        free(cli);
                    141:        cli = NULL;
                    142: }
                    143: 
                    144: // --------------------------------------------------------------
                    145: 
                    146: /*
1.7       misho     147:  * rpc_cli_openClient() - Connect to RPC Server
                    148:  *
1.1       misho     149:  * @ProgID = ProgramID for RPC session request
                    150:  * @ProcID = ProcessID for RPC session request
1.5       misho     151:  * @netBuf = Network buffer length, if =0 == BUFSIZ (also meaning max RPC packet)
1.7       misho     152:  * @Timeout = RPC timeout in seconds, if =0 set default RPC timeout
1.1       misho     153:  * @family = Family socket type, AF_INET or AF_INET6
                    154:  * @csHost = Host name or IP address for bind server
                    155:  * @Port = Port for bind server, if Port == 0 default port is selected
                    156:  * return: NULL == error or !=NULL connection to RPC server established
                    157:  */
                    158: rpc_cli_t *
1.7       misho     159: rpc_cli_openClient(u_int ProgID, u_int ProcID, int netBuf, u_char Timeout, 
                    160:                u_short family, const char *csHost, u_short Port)
1.1       misho     161: {
                    162:        rpc_cli_t *cli = NULL;
1.6       misho     163:        io_sockaddr_t sa;
1.5       misho     164:        int n;
1.1       misho     165: 
1.9.2.1 ! misho     166:        if (!io_gethostbyname(csHost, Port, &sa))
1.1       misho     167:                return NULL;
                    168:        if (!Port)
                    169:                Port = RPC_DEFPORT;
1.5       misho     170:        if (!netBuf)
                    171:                netBuf = BUFSIZ;
1.7       misho     172:        if (!Timeout)
                    173:                Timeout = DEF_RPC_TIMEOUT;
1.1       misho     174: 
                    175:        cli = malloc(sizeof(rpc_cli_t));
                    176:        if (!cli) {
                    177:                LOGERR;
                    178:                return NULL;
                    179:        } else
                    180:                memset(cli, 0, sizeof(rpc_cli_t));
1.5       misho     181: 
                    182:        cli->cli_netbuf = netBuf;
1.1       misho     183:        cli->cli_parent = malloc(sizeof(rpc_sess_t));
                    184:        if (!cli->cli_parent) {
                    185:                LOGERR;
                    186:                free(cli);
                    187:                return NULL;
                    188:        } else {
                    189:                ((rpc_sess_t*) cli->cli_parent)->sess_version = RPC_VERSION;
1.7       misho     190:                ((rpc_sess_t*) cli->cli_parent)->sess_timeout = Timeout;
1.1       misho     191:                ((rpc_sess_t*) cli->cli_parent)->sess_program = ProgID;
                    192:                ((rpc_sess_t*) cli->cli_parent)->sess_process = ProcID;
                    193:        }
                    194: 
1.6       misho     195:        memcpy(&cli->cli_sa, &sa, sizeof cli->cli_sa);
1.4       misho     196: 
                    197:        /* connect to RPC server */
1.9.2.1 ! misho     198:        cli->cli_sock = socket(cli->cli_sa.sa.sa_family, SOCK_STREAM, 0);
1.1       misho     199:        if (cli->cli_sock == -1) {
                    200:                LOGERR;
                    201:                free(cli->cli_parent);
                    202:                free(cli);
                    203:                return NULL;
                    204:        }
1.5       misho     205:        n = cli->cli_netbuf;
                    206:        if (setsockopt(cli->cli_sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof n) == -1) {
                    207:                LOGERR;
                    208:                close(cli->cli_sock);
                    209:                free(cli->cli_parent);
                    210:                free(cli);
                    211:                return NULL;
                    212:        }
                    213:        if (setsockopt(cli->cli_sock, SOL_SOCKET, SO_RCVBUF, &n, sizeof n) == -1) {
                    214:                LOGERR;
                    215:                close(cli->cli_sock);
                    216:                free(cli->cli_parent);
                    217:                free(cli);
                    218:                return NULL;
                    219:        }
1.6       misho     220:        if (connect(cli->cli_sock, &cli->cli_sa.sa, cli->cli_sa.sa.sa_len) == -1) {
1.1       misho     221:                LOGERR;
1.5       misho     222:                close(cli->cli_sock);
1.1       misho     223:                free(cli->cli_parent);
                    224:                free(cli);
                    225:                return NULL;
                    226:        }
                    227: 
                    228:        return cli;
                    229: }
                    230: 
                    231: /*
1.7       misho     232:  * rpc_cli_closeClient() - Close connection to RPC server and free resources
                    233:  *
1.1       misho     234:  * @cli = RPC Client session
                    235:  * return: none
                    236:  */
                    237: void
                    238: rpc_cli_closeClient(rpc_cli_t * __restrict cli)
                    239: {
                    240:        if (!cli) {
1.7       misho     241:                rpc_SetErr(EINVAL, "Can`t close connection because parameter is null!");
1.1       misho     242:                return;
                    243:        }
                    244: 
                    245:        shutdown(cli->cli_sock, SHUT_RDWR);
                    246:        close(cli->cli_sock);
                    247: 
                    248:        if (cli->cli_parent)
                    249:                free(cli->cli_parent);
                    250:        free(cli);
                    251:        cli = NULL;
                    252: }
                    253: 
                    254: 
                    255: /*
1.7       misho     256:  * rpc_cli_execCall() - Execute RPC call
                    257:  *
1.1       misho     258:  * @cli = RPC Client session
1.8       misho     259:  * @noreply = We not want RPC reply
1.1       misho     260:  * @csModule = Module name, if NULL self binary
                    261:  * @csFunc = Function name for execute
1.4       misho     262:  * @in_vars = IN RPC call array of rpc values
1.5       misho     263:  * @out_vars = OUT returned array of rpc values, must be free after use with rpc_cli_freeVals()
1.1       misho     264:  * return: -1 error or != -1 ok result
                    265:  */
                    266: int
1.8       misho     267: rpc_cli_execCall(rpc_cli_t *cli, int noreply, const char *csModule, const char *csFunc, 
1.5       misho     268:                array_t * __restrict in_vars, array_t ** __restrict out_vars)
1.1       misho     269: {
1.7       misho     270:        u_char *buf;
                    271:        rpc_func_t func;
1.5       misho     272:        struct tagRPCCall *rpc;
1.7       misho     273:        int ret = 0, wlen = sizeof(struct tagRPCCall);
1.8       misho     274:        uint16_t tag, crc;
1.6       misho     275:        uint32_t hash;
1.9       misho     276:        struct pollfd pfd;
1.1       misho     277: 
1.5       misho     278:        if (!cli || !csFunc) {
1.7       misho     279:                rpc_SetErr(EINVAL, "Can`t execute call because parameter is null or invalid!");
1.1       misho     280:                return -1;
                    281:        }
1.5       misho     282:        if (out_vars)
                    283:                *out_vars = NULL;
                    284: 
                    285:        buf = malloc(cli->cli_netbuf);
                    286:        if (!buf) {
                    287:                LOGERR;
                    288:                return -1;
                    289:        } else
                    290:                memset(buf, 0, cli->cli_netbuf);
                    291: 
1.7       misho     292:        /* calculate hashes */
                    293:        if (rpc_calcHashes(&func, csModule, csFunc) == -1)
                    294:                return -1;
                    295:        else {
                    296:                tag = htons(func.func_tag);
                    297:                hash = htonl(func.func_hash);
                    298:                AIT_FREE_VAL(&func.func_name);
                    299:                AIT_FREE_VAL(&func.func_file);
                    300:        }
1.1       misho     301: 
1.4       misho     302:        /* prepare RPC call */
1.5       misho     303:        rpc = (struct tagRPCCall*) buf;
1.6       misho     304:        rpc_addPktSession(&rpc->call_session, cli->cli_parent);
                    305:        rpc->call_argc = htons(io_arraySize(in_vars));
1.7       misho     306:        rpc->call_tag = tag;
                    307:        rpc->call_hash = hash;
1.5       misho     308: 
1.8       misho     309:        /* set reply */
                    310:        rpc->call_req.flags = noreply ? RPC_NOREPLY : RPC_REPLY;
                    311: 
1.6       misho     312:        if (io_arraySize(in_vars)) {
1.5       misho     313:                /* marshaling variables */
1.7       misho     314:                ret = io_vars2buffer(buf + wlen, cli->cli_netbuf - wlen, in_vars);
1.5       misho     315:                if (ret == -1) {
1.7       misho     316:                        rpc_SetErr(EBADRPC, "Failed to prepare RPC packet values");
1.5       misho     317:                        free(buf);
1.7       misho     318:                        return -1;
1.3       misho     319:                } else
1.7       misho     320:                        wlen += ret;
1.1       misho     321:        }
1.4       misho     322: 
1.8       misho     323:        rpc->call_len = htons(wlen);
                    324: 
1.7       misho     325:        /* calculate CRC */
                    326:        rpc->call_crc ^= rpc->call_crc;
1.8       misho     327:        rpc->call_crc = htons(crcFletcher16((u_short*) buf, wlen / 2));
1.7       misho     328: 
                    329:        if ((ret = send(cli->cli_sock, buf, wlen, 0)) == -1) {
1.1       misho     330:                LOGERR;
1.5       misho     331:                free(buf);
1.1       misho     332:                return -1;
1.7       misho     333:        } else if (ret != wlen) {
                    334:                rpc_SetErr(EPROCUNAVAIL, "RPC request, should be send %d bytes, "
                    335:                                "really sended %d bytes", wlen, ret);
1.5       misho     336:                free(buf);
1.7       misho     337:                return -1;
1.1       misho     338:        }
                    339: 
1.8       misho     340:        if (noreply) {
                    341:                /* we not want reply */
                    342:                free(buf);
                    343:                return 0;
                    344:        }
                    345: 
1.5       misho     346:        /* reply from RPC server */
1.9       misho     347:        pfd.fd = cli->cli_sock;
                    348:        pfd.events = POLLIN | POLLPRI;
                    349:        if ((ret = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) == -1 || 
                    350:                        pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
1.4       misho     351:                if (ret)
1.1       misho     352:                        LOGERR;
1.4       misho     353:                else
1.8       misho     354:                        rpc_SetErr(ETIMEDOUT, "Timeout, no return from RPC server?");
1.4       misho     355: 
1.5       misho     356:                free(buf);
1.1       misho     357:                return -1;
                    358:        }
1.5       misho     359:        memset(buf, 0, cli->cli_netbuf);
                    360:        if ((ret = recv(cli->cli_sock, buf, cli->cli_netbuf, 0)) == -1) {
1.1       misho     361:                LOGERR;
1.5       misho     362:                free(buf);
1.8       misho     363:                return -1;
1.1       misho     364:        }
1.5       misho     365:        if (!ret) {     /* receive EOF! */
                    366:                free(buf);
1.1       misho     367:                return 0;
1.5       misho     368:        }
1.7       misho     369:        if (ret < sizeof(struct tagRPCCall)) {
1.8       misho     370:                rpc_SetErr(ERPCMISMATCH, "Too short RPC packet ...");
1.5       misho     371:                free(buf);
1.8       misho     372:                return -1;
1.7       misho     373:        }
1.8       misho     374: 
                    375:        /* calculate CRC */
                    376:        crc = ntohs(rpc->call_crc);
                    377:        rpc->call_crc ^= rpc->call_crc;
                    378:        if (crc != crcFletcher16((u_short*) buf, ret / 2)) {
                    379:                rpc_SetErr(ERPCMISMATCH, "Bad CRC RPC packet");
                    380:                free(buf);
                    381:                return -1;
                    382:        }
                    383: 
1.3       misho     384:        /* check RPC packet session info */
1.7       misho     385:        if (rpc_chkPktSession(&rpc->call_session, cli->cli_parent)) {
1.8       misho     386:                rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session ...");
1.5       misho     387:                free(buf);
1.8       misho     388:                return -1;
1.2       misho     389:        } else
1.7       misho     390:                wlen = sizeof(struct tagRPCCall);
                    391:        if (rpc->call_tag != tag || rpc->call_hash != hash) {
1.8       misho     392:                rpc_SetErr(ERPCMISMATCH, "Get wrong RPC reply ...");
1.6       misho     393:                free(buf);
1.8       misho     394:                return -1;
1.6       misho     395:        }
1.7       misho     396:        if (ntohl(rpc->call_rep.ret) < 0 && ntohl(rpc->call_rep.eno)) {
1.8       misho     397:                rpc_SetErr(ntohl(rpc->call_rep.eno), "Server side: retcode=%d #%d %s", 
1.7       misho     398:                                ntohl(rpc->call_rep.ret), ntohl(rpc->call_rep.eno), 
                    399:                                strerror(ntohl(rpc->call_rep.eno)));
1.5       misho     400:                free(buf);
1.8       misho     401:                return -1;
1.1       misho     402:        }
1.7       misho     403:        if (ntohs(rpc->call_argc) * sizeof(ait_val_t) > cli->cli_netbuf - wlen) {
1.8       misho     404:                rpc_SetErr(EMSGSIZE, "Reply RPC packet is too long ...");
1.5       misho     405:                free(buf);
1.8       misho     406:                return -1;
1.5       misho     407:        }
                    408: 
                    409:        /* RPC is OK! Go de-marshaling variables ... */
1.7       misho     410:        if (ntohs(rpc->call_argc)) {
                    411:                *out_vars = io_buffer2vars(buf + wlen, cli->cli_netbuf - wlen, 
                    412:                                ntohs(rpc->call_argc), 0);
1.4       misho     413:                if (!*out_vars) {
1.5       misho     414:                        free(buf);
1.1       misho     415:                        return -1;
1.5       misho     416:                }
1.1       misho     417:        }
                    418: 
1.7       misho     419:        ret = ntohl(rpc->call_rep.ret);
1.5       misho     420:        free(buf);
                    421:        return ret;
1.1       misho     422: }

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