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