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