Annotation of libaitrpc/src/cli.c, revision 1.4.2.2
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.2 ! misho 6: * $Id: cli.c,v 1.4.2.1 2011/08/29 23:26:56 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
304: * @in_argc = IN count of arguments
1.4 misho 305: * @in_vars = IN RPC call array of rpc values
1.1 misho 306: * @out_argc = OUT returned count of arguments
1.4 misho 307: * @out_vars = OUT returned array of rpc values, must be free after use (see rpc_cli_freeVals())
1.1 misho 308: * return: -1 error or != -1 ok result
309: */
310: int
311: rpc_cli_execCall(rpc_cli_t *cli, const char *csModule, const char *csFunc, int in_argc,
1.4.2.1 misho 312: ait_val_t * __restrict in_vars, int *out_argc, ait_val_t ** __restrict out_vars)
1.1 misho 313: {
314: fd_set fds;
315: u_char buf[BUFSIZ], str[MAXPATHLEN + UCHAR_MAX + 1], *data;
316: struct tagRPCCall *rpc = (struct tagRPCCall*) buf;
1.3 misho 317: struct tagRPCRet *rrpc = NULL;
1.1 misho 318: int ret = 0, Limit = 0;
319: register int i;
1.4.2.1 misho 320: ait_val_t *v;
1.2 misho 321: struct timeval tv = { DEF_RPC_TIMEOUT, 0 };
1.1 misho 322:
323: FD_ZERO(&fds);
1.4 misho 324: memset(buf, 0, sizeof buf);
325: memset(str, 0, sizeof str);
326: if (!cli || !csFunc || (in_argc && !in_vars)) {
1.1 misho 327: rpc_SetErr(EINVAL, "Error:: Can`t execute call because parameter is null or invalid!\n");
328: return -1;
329: }
1.4 misho 330: /* build RPC call string for hash */
1.1 misho 331: if (csModule)
1.4 misho 332: strlcpy((char*) str, csModule, sizeof str);
333: strlcat((char*) str, "__", sizeof str);
334: strlcat((char*) str, csFunc, sizeof str);
1.1 misho 335:
1.4 misho 336: /* prepare RPC call */
1.1 misho 337: memcpy(&rpc->call_session, cli->cli_parent, sizeof rpc->call_session);
338: rpc->call_argc = in_argc;
1.4 misho 339: rpc->call_tag = crcFletcher16((u_short*) str, sizeof str / 2);
340: rpc->call_hash = hash_fnv((char*) str, sizeof str);
1.1 misho 341: Limit = sizeof(struct tagRPCCall);
342: if (in_argc) {
1.4.2.1 misho 343: v = (ait_val_t*) (buf + sizeof(struct tagRPCCall));
344: if (in_argc * sizeof(ait_val_t) > sizeof buf - Limit) {
1.3 misho 345: rpc_SetErr(EMSGSIZE, "Error:: in prepare RPC packet values (-7) ...\n");
346: return -7;
347: } else
1.4.2.1 misho 348: Limit += in_argc * sizeof(ait_val_t);
349: memcpy(v, in_vars, in_argc * sizeof(ait_val_t));
350: data = (u_char*) v + in_argc * sizeof(ait_val_t);
1.4 misho 351: for (i = 0; i < in_argc && !ret; i++) {
352: switch (in_vars[i].val_type) {
1.1 misho 353: case buffer:
1.4 misho 354: if (Limit + in_vars[i].val_len > sizeof buf) {
1.1 misho 355: ret = -7;
356: break;
357: }
358:
1.4 misho 359: memcpy(data, in_vars[i].val.buffer, in_vars[i].val_len);
1.1 misho 360: v[i].val.buffer = (uint8_t*) ((void*) data - (void*) v);
1.4 misho 361: data += in_vars[i].val_len;
362: Limit += in_vars[i].val_len;
1.1 misho 363: break;
364: case string:
1.4 misho 365: if (Limit + in_vars[i].val_len > sizeof buf) {
1.1 misho 366: ret = -7;
367: break;
368: }
369:
1.4 misho 370: memcpy(data, in_vars[i].val.string, in_vars[i].val_len);
1.1 misho 371: v[i].val.string = (int8_t*) ((void*) data - (void*) v);
1.4 misho 372: data += in_vars[i].val_len;
373: Limit += in_vars[i].val_len;
1.1 misho 374: break;
375: default:
376: break;
377: }
378: }
379: if (ret < 0) {
380: rpc_SetErr(EMSGSIZE, "Error:: in prepare RPC packet (-7) ...\n");
381: return ret;
382: }
383: }
1.4 misho 384:
1.1 misho 385: if ((ret = send(cli->cli_sock, buf, Limit, 0)) == -1) {
386: LOGERR;
387: return -1;
388: }
389: if (ret != Limit) {
1.2 misho 390: rpc_SetErr(ECANCELED, "Error:: in send RPC request, should be send %d bytes, really is %d\n",
1.1 misho 391: Limit, ret);
392: return -9;
393: }
394:
395: FD_SET(cli->cli_sock, &fds);
396: if ((ret = select(cli->cli_sock + 1, &fds, NULL, NULL, &tv)) < 1) {
1.4 misho 397: if (ret)
1.1 misho 398: LOGERR;
1.4 misho 399: else
1.1 misho 400: rpc_SetErr(ETIMEDOUT, "Error:: timeout, no return from RPC server?\n");
1.4 misho 401:
1.1 misho 402: return -1;
403: }
1.4 misho 404: memset(buf, 0, sizeof buf);
405: if ((ret = recv(cli->cli_sock, buf, sizeof buf, 0)) == -1) {
1.1 misho 406: LOGERR;
407: return -3;
408: }
1.3 misho 409: if (!ret) /* receive EOF! */
1.1 misho 410: return 0;
1.2 misho 411: if (ret < sizeof(struct tagRPCRet)) {
1.1 misho 412: rpc_SetErr(EMSGSIZE, "Error:: too short RPC packet ...\n");
413: return -4;
414: } else
415: rrpc = (struct tagRPCRet*) buf;
1.3 misho 416: /* check RPC packet session info */
1.1 misho 417: if (memcmp(&rrpc->ret_session, cli->cli_parent, sizeof rrpc->ret_session)) {
418: rpc_SetErr(EINVAL, "Error:: get invalid RPC session ...\n");
419: return -5;
1.2 misho 420: } else
421: Limit = sizeof(struct tagRPCRet);
1.1 misho 422: if (rrpc->ret_retcode < 0 && rrpc->ret_errno) {
1.2 misho 423: rpc_SetErr(rrpc->ret_errno, "Error::Server side: %d %s\n",
424: rrpc->ret_retcode, strerror(rrpc->ret_errno));
1.1 misho 425: return -6;
426: }
1.4.2.1 misho 427: if (rrpc->ret_argc * sizeof(ait_val_t) > sizeof buf - Limit) {
1.4 misho 428: rpc_SetErr(EMSGSIZE, "Error:: reply RPC packet is too long ...\n");
1.2 misho 429: return -7;
430: } else
1.4.2.1 misho 431: Limit += rrpc->ret_argc * sizeof(ait_val_t);
1.3 misho 432: /* RPC is OK! Go decapsulate variables ... */
1.1 misho 433: if (rrpc->ret_argc) {
434: *out_argc = rrpc->ret_argc;
1.4.2.1 misho 435: *out_vars = calloc(rrpc->ret_argc, sizeof(ait_val_t));
1.4 misho 436: if (!*out_vars) {
1.1 misho 437: LOGERR;
438: *out_argc = 0;
439: return -1;
440: } else
1.4 misho 441: memcpy(*out_vars, buf + sizeof(struct tagRPCRet), Limit - sizeof(struct tagRPCRet));
1.3 misho 442: /* RPC received variables types OK! */
1.2 misho 443: data = (u_char*) buf + Limit;
1.1 misho 444: for (i = 0; i < rrpc->ret_argc; i++)
1.4 misho 445: switch ((*out_vars)[i].val_type) {
1.1 misho 446: case buffer:
1.4 misho 447: if ((*out_vars)[i].val_len > sizeof buf - Limit) {
448: rpc_SetErr(EMSGSIZE, "Error:: Too long RPC packet ...\n");
449: free(*out_vars);
450: *out_vars = NULL;
1.2 misho 451: *out_argc = 0;
452: return -7;
453: } else
1.4 misho 454: Limit += (*out_vars)[i].val_len;
1.2 misho 455:
1.4 misho 456: (*out_vars)[i].val.buffer = malloc((*out_vars)[i].val_len);
457: if (!(*out_vars)[i].val.buffer) {
1.1 misho 458: rpc_SetErr(errno, "Error:: in prepare RPC reply ...\n");
1.4 misho 459: free(*out_vars);
460: *out_vars = NULL;
1.1 misho 461: *out_argc = 0;
462: return -1;
463: } else
1.4 misho 464: memcpy((*out_vars)[i].val.buffer, data, (*out_vars)[i].val_len);
465: data += (*out_vars)[i].val_len;
1.1 misho 466: break;
467: case string:
1.4 misho 468: if ((*out_vars)[i].val_len > sizeof buf - Limit) {
469: rpc_SetErr(EMSGSIZE, "Error:: Too long RPC packet ...\n");
470: free(*out_vars);
471: *out_vars = NULL;
1.2 misho 472: *out_argc = 0;
473: return -7;
474: } else
1.4 misho 475: Limit += (*out_vars)[i].val_len;
1.2 misho 476:
1.4 misho 477: (*out_vars)[i].val.string = malloc((*out_vars)[i].val_len);
478: if (!(*out_vars)[i].val.string) {
1.1 misho 479: rpc_SetErr(errno, "Error:: in prepare RPC reply ...\n");
1.4 misho 480: free(*out_vars);
481: *out_vars = NULL;
1.1 misho 482: *out_argc = 0;
483: return -1;
1.3 misho 484: } else
1.4 misho 485: memcpy((*out_vars)[i].val.string, data, (*out_vars)[i].val_len);
486: data += (*out_vars)[i].val_len;
1.1 misho 487: break;
488: default:
489: break;
490: }
491: } else {
1.2 misho 492: if (out_argc)
493: *out_argc = 0;
1.4 misho 494: if (out_vars)
495: *out_vars = NULL;
1.1 misho 496: }
497:
498: return rrpc->ret_retcode;
499: }
500:
501: /*
1.4.2.1 misho 502: * rpc_cli_freeVals() Free ait_val_t array returned from RPC call
1.1 misho 503: * @args = Number of arguments in array
1.4 misho 504: * @vars = Value elements
1.1 misho 505: * return: none
506: */
507: inline void
1.4.2.1 misho 508: rpc_cli_freeVals(int args, ait_val_t * __restrict vars)
1.1 misho 509: {
510: register int i;
511:
1.4 misho 512: if (!vars)
1.1 misho 513: return;
514:
515: for (i = 0; i < args; i++)
1.4.2.1 misho 516: AIT_FREE_VAL(&vars[i]);
1.1 misho 517:
1.4 misho 518: free(vars);
519: vars = NULL;
1.1 misho 520: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>