Annotation of libaitrpc/src/cli.c, revision 1.5.2.6
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.5.2.6 ! misho 6: * $Id: cli.c,v 1.5.2.5 2011/10/31 14:58:33 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:
15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
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.2 misho 50: * rpc_cli_openBLOBClient() Connect to BLOB Server
51: * @rpccli = RPC Client session
52: * @Port = Port for bind server, if Port == 0 default port is selected
53: * return: NULL == error or !=NULL connection to BLOB server established
54: */
55: rpc_cli_t *
56: rpc_cli_openBLOBClient(rpc_cli_t * __restrict rpccli, u_short Port)
57: {
58: rpc_cli_t *cli = NULL;
1.5.2.4 misho 59: io_sockaddr_t sa;
1.5 misho 60: int n;
1.4 misho 61:
62: if (!rpccli ||
1.5.2.4 misho 63: (rpccli->cli_sa.sa.sa_family != AF_INET && rpccli->cli_sa.sa.sa_family != AF_INET6 &&
64: rpccli->cli_sa.sa.sa_family != AF_LOCAL)) {
1.2 misho 65: rpc_SetErr(EINVAL, "Error:: Invalid parameters can`t connect to BLOB server ...\n");
66: return NULL;
67: }
68:
69: cli = malloc(sizeof(rpc_cli_t));
70: if (!cli) {
71: LOGERR;
72: return NULL;
73: } else
74: memcpy(cli, rpccli, sizeof(rpc_cli_t));
75:
1.4 misho 76: memcpy(&sa, &rpccli->cli_sa, sizeof sa);
1.5.2.6 ! misho 77: switch (sa.sa.sa_family) {
1.4 misho 78: case AF_INET:
1.5.2.6 ! misho 79: sa.sin.sin_port = htons(Port ? Port : ntohs(sa.sin.sin_port) + 1);
1.4 misho 80: break;
81: case AF_INET6:
1.5.2.6 ! misho 82: sa.sin6.sin6_port = htons(Port ? Port : ntohs(sa.sin6.sin6_port) + 1);
1.4 misho 83: break;
84: case AF_LOCAL:
1.5.2.4 misho 85: strlcat(sa.sun.sun_path, ".blob", sizeof sa.sun.sun_path);
1.4 misho 86: break;
1.2 misho 87: }
1.5.2.4 misho 88: memcpy(&cli->cli_sa, &sa, sizeof sa);
1.2 misho 89:
1.4 misho 90: /* connect to BLOB server */
1.5.2.4 misho 91: cli->cli_sock = socket(cli->cli_sa.sa.sa_family, SOCK_STREAM, 0);
1.2 misho 92: if (cli->cli_sock == -1) {
93: LOGERR;
94: free(cli);
95: return NULL;
96: }
1.5 misho 97: n = cli->cli_netbuf;
98: if (setsockopt(cli->cli_sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof n) == -1) {
99: LOGERR;
100: close(cli->cli_sock);
101: free(cli);
102: return NULL;
103: }
104: if (setsockopt(cli->cli_sock, SOL_SOCKET, SO_RCVBUF, &n, sizeof n) == -1) {
105: LOGERR;
106: close(cli->cli_sock);
107: free(cli);
108: return NULL;
109: }
1.5.2.5 misho 110: if (connect(cli->cli_sock, &cli->cli_sa.sa, cli->cli_sa.sa.sa_len) == -1) {
1.2 misho 111: LOGERR;
1.5 misho 112: close(cli->cli_sock);
1.2 misho 113: free(cli);
114: return NULL;
115: }
116:
117: return cli;
118: }
119:
120: /*
121: * rpc_cli_closeBLOBClient() Close connection to BLOB server and free resources
122: * @cli = BLOB Client session
123: * return: none
124: */
125: void
126: rpc_cli_closeBLOBClient(rpc_cli_t * __restrict cli)
127: {
128: if (!cli) {
129: rpc_SetErr(EINVAL, "Error:: Can`t close connection because parameter is null!\n");
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.1 misho 143: * rpc_cli_openClient() Connect to RPC Server
144: * @ProgID = ProgramID for RPC session request
145: * @ProcID = ProcessID for RPC session request
1.5 misho 146: * @netBuf = Network buffer length, if =0 == BUFSIZ (also meaning max RPC packet)
1.1 misho 147: * @family = Family socket type, AF_INET or AF_INET6
148: * @csHost = Host name or IP address for bind server
149: * @Port = Port for bind server, if Port == 0 default port is selected
150: * return: NULL == error or !=NULL connection to RPC server established
151: */
152: rpc_cli_t *
1.5 misho 153: rpc_cli_openClient(u_int ProgID, u_int ProcID, int netBuf, u_short family, const char *csHost, u_short Port)
1.1 misho 154: {
155: rpc_cli_t *cli = NULL;
156: struct hostent *host = NULL;
1.5.2.4 misho 157: io_sockaddr_t sa;
1.5 misho 158: int n;
1.1 misho 159:
1.4 misho 160: if (!csHost || (family != AF_INET && family != AF_INET6 && family != AF_LOCAL)) {
1.1 misho 161: rpc_SetErr(EINVAL, "Error:: Invalid parameters can`t connect to RPC server ...\n");
162: return NULL;
163: }
164: if (!Port)
165: Port = RPC_DEFPORT;
1.5 misho 166: if (!netBuf)
167: netBuf = BUFSIZ;
1.4 misho 168: if (csHost && family != AF_LOCAL) {
1.1 misho 169: host = gethostbyname2(csHost, family);
170: if (!host) {
171: rpc_SetErr(h_errno, "Error:: %s\n", hstrerror(h_errno));
172: return NULL;
173: }
174: }
1.4 misho 175: memset(&sa, 0, sizeof sa);
1.5.2.4 misho 176: sa.sa.sa_family = family;
1.1 misho 177: switch (family) {
178: case AF_INET:
1.5.2.4 misho 179: sa.sin.sin_len = sizeof(struct sockaddr_in);
180: sa.sin.sin_port = htons(Port);
1.1 misho 181: if (csHost)
1.5.2.4 misho 182: memcpy(&sa.sin.sin_addr, host->h_addr, host->h_length);
1.1 misho 183: break;
184: case AF_INET6:
1.5.2.4 misho 185: sa.sin6.sin6_len = sizeof(struct sockaddr_in6);
186: sa.sin6.sin6_port = htons(Port);
1.1 misho 187: if (csHost)
1.5.2.4 misho 188: memcpy(&sa.sin6.sin6_addr, host->h_addr, host->h_length);
1.4 misho 189: break;
190: case AF_LOCAL:
1.5.2.4 misho 191: sa.sun.sun_len = sizeof(struct sockaddr_un);
1.4 misho 192: if (csHost)
1.5.2.4 misho 193: strlcpy(sa.sun.sun_path, csHost, sizeof sa.sun.sun_path);
1.1 misho 194: break;
195: default:
196: rpc_SetErr(EINVAL, "Error:: Invalid parameters can`t connect to RPC server ...\n");
197: return NULL;
198: }
199:
200: cli = malloc(sizeof(rpc_cli_t));
201: if (!cli) {
202: LOGERR;
203: return NULL;
204: } else
205: memset(cli, 0, sizeof(rpc_cli_t));
1.5 misho 206:
207: cli->cli_netbuf = netBuf;
1.1 misho 208: cli->cli_parent = malloc(sizeof(rpc_sess_t));
209: if (!cli->cli_parent) {
210: LOGERR;
211: free(cli);
212: return NULL;
213: } else {
214: ((rpc_sess_t*) cli->cli_parent)->sess_version = RPC_VERSION;
215: ((rpc_sess_t*) cli->cli_parent)->sess_program = ProgID;
216: ((rpc_sess_t*) cli->cli_parent)->sess_process = ProcID;
217: }
218:
1.5.2.4 misho 219: memcpy(&cli->cli_sa, &sa, sizeof cli->cli_sa);
1.4 misho 220:
221: /* connect to RPC server */
1.1 misho 222: cli->cli_sock = socket(family, SOCK_STREAM, 0);
223: if (cli->cli_sock == -1) {
224: LOGERR;
225: free(cli->cli_parent);
226: free(cli);
227: return NULL;
228: }
1.5 misho 229: n = cli->cli_netbuf;
230: if (setsockopt(cli->cli_sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof n) == -1) {
231: LOGERR;
232: close(cli->cli_sock);
233: free(cli->cli_parent);
234: free(cli);
235: return NULL;
236: }
237: if (setsockopt(cli->cli_sock, SOL_SOCKET, SO_RCVBUF, &n, sizeof n) == -1) {
238: LOGERR;
239: close(cli->cli_sock);
240: free(cli->cli_parent);
241: free(cli);
242: return NULL;
243: }
1.5.2.5 misho 244: if (connect(cli->cli_sock, &cli->cli_sa.sa, cli->cli_sa.sa.sa_len) == -1) {
1.1 misho 245: LOGERR;
1.5 misho 246: close(cli->cli_sock);
1.1 misho 247: free(cli->cli_parent);
248: free(cli);
249: return NULL;
250: }
251:
252: return cli;
253: }
254:
255: /*
256: * rpc_cli_closeClient() Close connection to RPC server and free resources
257: * @cli = RPC Client session
258: * return: none
259: */
260: void
261: rpc_cli_closeClient(rpc_cli_t * __restrict cli)
262: {
263: if (!cli) {
264: rpc_SetErr(EINVAL, "Error:: Can`t close connection because parameter is null!\n");
265: return;
266: }
267:
268: shutdown(cli->cli_sock, SHUT_RDWR);
269: close(cli->cli_sock);
270:
271: if (cli->cli_parent)
272: free(cli->cli_parent);
273: free(cli);
274: cli = NULL;
275: }
276:
277:
278: /*
279: * rpc_cli_execCall() Execute RPC call
280: * @cli = RPC Client session
281: * @csModule = Module name, if NULL self binary
282: * @csFunc = Function name for execute
1.4 misho 283: * @in_vars = IN RPC call array of rpc values
1.5 misho 284: * @out_vars = OUT returned array of rpc values, must be free after use with rpc_cli_freeVals()
1.1 misho 285: * return: -1 error or != -1 ok result
286: */
287: int
1.5 misho 288: rpc_cli_execCall(rpc_cli_t *cli, const char *csModule, const char *csFunc,
289: array_t * __restrict in_vars, array_t ** __restrict out_vars)
1.1 misho 290: {
291: fd_set fds;
1.5 misho 292: u_char *buf, str[MAXPATHLEN + UCHAR_MAX + 1];
293: struct tagRPCCall *rpc;
1.3 misho 294: struct tagRPCRet *rrpc = NULL;
1.1 misho 295: int ret = 0, Limit = 0;
1.2 misho 296: struct timeval tv = { DEF_RPC_TIMEOUT, 0 };
1.5.2.3 misho 297: uint16_t tag;
298: uint32_t hash;
1.1 misho 299:
1.5 misho 300: if (!cli || !csFunc) {
1.1 misho 301: rpc_SetErr(EINVAL, "Error:: Can`t execute call because parameter is null or invalid!\n");
302: return -1;
303: }
1.5 misho 304: if (out_vars)
305: *out_vars = NULL;
306:
307: buf = malloc(cli->cli_netbuf);
308: if (!buf) {
309: LOGERR;
310: return -1;
311: } else
312: memset(buf, 0, cli->cli_netbuf);
313:
1.4 misho 314: /* build RPC call string for hash */
1.5 misho 315: memset(str, 0, sizeof str);
1.1 misho 316: if (csModule)
1.4 misho 317: strlcpy((char*) str, csModule, sizeof str);
318: strlcat((char*) str, "__", sizeof str);
319: strlcat((char*) str, csFunc, sizeof str);
1.1 misho 320:
1.4 misho 321: /* prepare RPC call */
1.5 misho 322: rpc = (struct tagRPCCall*) buf;
1.5.2.1 misho 323: rpc_addPktSession(&rpc->call_session, cli->cli_parent);
1.5.2.6 ! misho 324: rpc->call_argc = htons(io_arraySize(in_vars));
1.5.2.3 misho 325: rpc->call_tag = tag = htons(crcFletcher16((u_short*) str, sizeof str / 2));
326: rpc->call_hash = hash = htonl(hash_fnv((char*) str, sizeof str));
1.1 misho 327: Limit = sizeof(struct tagRPCCall);
1.5 misho 328:
1.5.2.6 ! misho 329: if (io_arraySize(in_vars)) {
1.5 misho 330: /* marshaling variables */
331: ret = io_vals2buffer(buf + Limit, cli->cli_netbuf - Limit, in_vars);
332: if (ret == -1) {
333: rpc_SetErr(EBADRPC, "Error:: in prepare RPC packet values (-7) ...\n");
334: free(buf);
1.3 misho 335: return -7;
336: } else
1.5 misho 337: Limit += ret;
1.1 misho 338: }
1.4 misho 339:
1.1 misho 340: if ((ret = send(cli->cli_sock, buf, Limit, 0)) == -1) {
341: LOGERR;
1.5 misho 342: free(buf);
1.1 misho 343: return -1;
344: }
345: if (ret != Limit) {
1.5 misho 346: rpc_SetErr(EPROCUNAVAIL, "Error:: in send RPC request, should be send %d bytes, really is %d\n",
1.1 misho 347: Limit, ret);
1.5 misho 348: free(buf);
1.1 misho 349: return -9;
350: }
351:
1.5 misho 352: /* reply from RPC server */
353: FD_ZERO(&fds);
1.1 misho 354: FD_SET(cli->cli_sock, &fds);
355: if ((ret = select(cli->cli_sock + 1, &fds, NULL, NULL, &tv)) < 1) {
1.4 misho 356: if (ret)
1.1 misho 357: LOGERR;
1.4 misho 358: else
1.1 misho 359: rpc_SetErr(ETIMEDOUT, "Error:: timeout, no return from RPC server?\n");
1.4 misho 360:
1.5 misho 361: free(buf);
1.1 misho 362: return -1;
363: }
1.5 misho 364: memset(buf, 0, cli->cli_netbuf);
365: if ((ret = recv(cli->cli_sock, buf, cli->cli_netbuf, 0)) == -1) {
1.1 misho 366: LOGERR;
1.5 misho 367: free(buf);
1.1 misho 368: return -3;
369: }
1.5 misho 370: if (!ret) { /* receive EOF! */
371: free(buf);
1.1 misho 372: return 0;
1.5 misho 373: }
1.2 misho 374: if (ret < sizeof(struct tagRPCRet)) {
1.5 misho 375: rpc_SetErr(ERPCMISMATCH, "Error:: too short RPC packet ...\n");
376: free(buf);
1.1 misho 377: return -4;
378: } else
379: rrpc = (struct tagRPCRet*) buf;
1.3 misho 380: /* check RPC packet session info */
1.5.2.1 misho 381: if (rpc_chkPktSession(&rrpc->ret_session, cli->cli_parent)) {
1.5 misho 382: rpc_SetErr(ERPCMISMATCH, "Error:: get invalid RPC session ...\n");
383: free(buf);
1.1 misho 384: return -5;
1.2 misho 385: } else
386: Limit = sizeof(struct tagRPCRet);
1.5.2.3 misho 387: if (rrpc->ret_tag != tag || rrpc->ret_hash != hash) {
388: rpc_SetErr(ERPCMISMATCH, "Error:: get wrong RPC reply ...\n");
389: free(buf);
390: return -5;
391: }
1.5.2.2 misho 392: if (ntohl(rrpc->ret_retcode) < 0 && ntohl(rrpc->ret_errno)) {
393: rpc_SetErr(ntohl(rrpc->ret_errno), "Error::Server side: retcode=%d #%d %s\n",
394: ntohl(rrpc->ret_retcode), ntohl(rrpc->ret_errno),
395: strerror(ntohl(rrpc->ret_errno)));
1.5 misho 396: free(buf);
1.1 misho 397: return -6;
398: }
1.5.2.2 misho 399: if (ntohs(rrpc->ret_argc) * sizeof(ait_val_t) > cli->cli_netbuf - Limit) {
1.4 misho 400: rpc_SetErr(EMSGSIZE, "Error:: reply RPC packet is too long ...\n");
1.5 misho 401: free(buf);
1.2 misho 402: return -7;
1.5 misho 403: }
404:
405: /* RPC is OK! Go de-marshaling variables ... */
1.5.2.2 misho 406: if (ntohs(rrpc->ret_argc)) {
407: *out_vars = io_buffer2vals(buf + Limit, cli->cli_netbuf - Limit,
408: ntohs(rrpc->ret_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.5.2.2 misho 415: ret = ntohl(rrpc->ret_retcode);
1.5 misho 416: free(buf);
417: return ret;
1.1 misho 418: }
419:
420: /*
1.5.2.6 ! misho 421: * rpc_cli_freeVars() Free ait_val_t array returned from RPC call
1.5 misho 422: * @vars = Variable array
1.1 misho 423: * return: none
424: */
425: inline void
1.5.2.6 ! misho 426: rpc_cli_freeVars(array_t ** __restrict vars)
1.1 misho 427: {
428: register int i;
429:
1.5 misho 430: if (!vars || !*vars)
1.1 misho 431: return;
432:
1.5 misho 433: for (i = 0; i < io_arraySize(*vars); i++)
434: if (io_arrayGet(*vars, i))
435: AIT_FREE_VAL(io_array(*vars, i, ait_val_t*));
436:
437: io_arrayFree(*vars);
438: io_arrayDestroy(vars);
439: }
440:
441: /*
1.5.2.6 ! misho 442: * rpc_cli_allocVars() Allocate ait_val_t array for RPC call
1.5 misho 443: * @args = Number of arguments
444: * return: =NULL error or !=NULL allocated array
445: */
446: inline array_t *
1.5.2.6 ! misho 447: rpc_cli_allocVars(u_short args)
1.5 misho 448: {
449: array_t *arr;
450: register int i;
451: ait_val_t *v;
452:
453: if (!args)
454: return NULL;
455:
456: if (!(arr = io_arrayInit(args)))
457: return NULL;
458:
459: for (i = 0; i < io_arraySize(arr); i++) {
460: v = malloc(sizeof(ait_val_t));
461: if (!v) {
462: LOGERR;
1.5.2.6 ! misho 463: rpc_cli_freeVars(&arr);
1.5 misho 464: return NULL;
465: } else {
466: memset(v, 0, sizeof(ait_val_t));
467: io_arraySet(arr, i, v);
468: }
469: }
1.1 misho 470:
1.5 misho 471: return arr;
1.1 misho 472: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>