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

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

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