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

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

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