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