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

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

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