Annotation of libaitrpc/src/cli.c, revision 1.3.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.3.2.2 ! misho 6: * $Id: cli.c,v 1.3.2.1 2011/08/19 09:20:47 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 *
1.3.2.2 ! misho 56: rpc_cli_openBLOBClient(rpc_cli_t * __restrict rpccli, u_short Port)
1.2 misho 57: {
58: rpc_cli_t *cli = NULL;
1.3.2.1 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.3.2.1 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.3.2.1 misho 96: /* connect to BLOB server */
1.3.2.2 ! misho 97: cli->cli_sock = socket(cli->cli_sa.sa_family, SOCK_STREAM, 0);
1.2 misho 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 *
1.3.2.2 ! misho 144: rpc_cli_openClient(u_int ProgID, u_int ProcID, u_short family, const char *csHost, u_short Port)
1.1 misho 145: {
146: rpc_cli_t *cli = NULL;
147: struct hostent *host = NULL;
1.3.2.1 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.3.2.1 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.3.2.1 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.3.2.1 misho 166: memset(&sa, 0, sizeof sa);
167: sa.sa_family = family;
1.1 misho 168: switch (family) {
169: case AF_INET:
1.3.2.1 misho 170: sin->sin_len = sizeof(struct sockaddr_in);
171: sin->sin_port = htons(Port);
1.1 misho 172: if (csHost)
1.3.2.1 misho 173: memcpy(&sin->sin_addr, host->h_addr, host->h_length);
1.1 misho 174: break;
175: case AF_INET6:
1.3.2.1 misho 176: sin6->sin6_len = sizeof(struct sockaddr_in6);
177: sin6->sin6_port = htons(Port);
178: if (csHost)
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);
1.1 misho 183: if (csHost)
1.3.2.1 misho 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.3.2.1 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.3.2.2 ! misho 221: cli->cli_sock = socket(family, SOCK_STREAM, 0);
1.1 misho 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
267: * @in_vals = IN RPC call array of rpc values
268: * @out_argc = OUT returned count of arguments
269: * @out_vals = OUT returned array of rpc values, must be free after use (see rpc_cli_freeVals())
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,
274: rpc_val_t * __restrict in_vals, int *out_argc, rpc_val_t ** __restrict out_vals)
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;
282: rpc_val_t *v;
1.2 misho 283: struct timeval tv = { DEF_RPC_TIMEOUT, 0 };
1.1 misho 284:
285: FD_ZERO(&fds);
286: memset(buf, 0, BUFSIZ);
287: memset(str, 0, MAXPATHLEN + UCHAR_MAX + 1);
288: if (!cli || !csFunc || (in_argc && !in_vals)) {
289: rpc_SetErr(EINVAL, "Error:: Can`t execute call because parameter is null or invalid!\n");
290: return -1;
291: }
292: if (csModule)
293: strlcpy((char*) str, csModule, MAXPATHLEN + UCHAR_MAX + 1);
294: strlcat((char*) str, "__", MAXPATHLEN + UCHAR_MAX + 1);
295: strlcat((char*) str, csFunc, MAXPATHLEN + UCHAR_MAX + 1);
296:
297: memcpy(&rpc->call_session, cli->cli_parent, sizeof rpc->call_session);
298: rpc->call_argc = in_argc;
299: rpc->call_tag = crcFletcher16((u_short*) str, (MAXPATHLEN + UCHAR_MAX + 1) / 2);
300: rpc->call_hash = hash_fnv((char*) str, MAXPATHLEN + UCHAR_MAX + 1);
301: Limit = sizeof(struct tagRPCCall);
302: if (in_argc) {
303: v = (rpc_val_t*) (buf + sizeof(struct tagRPCCall));
1.3 misho 304: if (in_argc * sizeof(rpc_val_t) > BUFSIZ - Limit) {
305: rpc_SetErr(EMSGSIZE, "Error:: in prepare RPC packet values (-7) ...\n");
306: return -7;
307: } else
308: Limit += in_argc * sizeof(rpc_val_t);
1.1 misho 309: memcpy(v, in_vals, in_argc * sizeof(rpc_val_t));
310: data = (u_char*) v + in_argc * sizeof(rpc_val_t);
311: for (i = 0; i < in_argc; i++) {
312: switch (in_vals[i].val_type) {
313: case buffer:
314: if (Limit + in_vals[i].val_len > BUFSIZ) {
315: ret = -7;
316: break;
317: }
318:
319: memcpy(data, in_vals[i].val.buffer, in_vals[i].val_len);
320: v[i].val.buffer = (uint8_t*) ((void*) data - (void*) v);
321: data += in_vals[i].val_len;
322: Limit += in_vals[i].val_len;
323: break;
324: case string:
1.3 misho 325: if (Limit + in_vals[i].val_len > BUFSIZ) {
1.1 misho 326: ret = -7;
327: break;
328: }
329:
330: memcpy(data, in_vals[i].val.string, in_vals[i].val_len);
331: v[i].val.string = (int8_t*) ((void*) data - (void*) v);
1.3 misho 332: data += in_vals[i].val_len;
333: Limit += in_vals[i].val_len;
1.1 misho 334: break;
335: default:
336: break;
337: }
338: }
339: if (ret < 0) {
340: rpc_SetErr(EMSGSIZE, "Error:: in prepare RPC packet (-7) ...\n");
341: return ret;
342: }
343: }
344: if ((ret = send(cli->cli_sock, buf, Limit, 0)) == -1) {
345: LOGERR;
346: return -1;
347: }
348: if (ret != Limit) {
1.2 misho 349: rpc_SetErr(ECANCELED, "Error:: in send RPC request, should be send %d bytes, really is %d\n",
1.1 misho 350: Limit, ret);
351: return -9;
352: }
353:
354: FD_SET(cli->cli_sock, &fds);
355: if ((ret = select(cli->cli_sock + 1, &fds, NULL, NULL, &tv)) < 1) {
356: if (ret) {
357: LOGERR;
358: } else
359: rpc_SetErr(ETIMEDOUT, "Error:: timeout, no return from RPC server?\n");
360: return -1;
361: }
362: memset(buf, 0, BUFSIZ);
363: if ((ret = recv(cli->cli_sock, buf, BUFSIZ, 0)) == -1) {
364: LOGERR;
365: return -3;
366: }
1.3 misho 367: if (!ret) /* receive EOF! */
1.1 misho 368: return 0;
1.2 misho 369: if (ret < sizeof(struct tagRPCRet)) {
1.1 misho 370: rpc_SetErr(EMSGSIZE, "Error:: too short RPC packet ...\n");
371: return -4;
372: } else
373: rrpc = (struct tagRPCRet*) buf;
1.3 misho 374: /* check RPC packet session info */
1.1 misho 375: if (memcmp(&rrpc->ret_session, cli->cli_parent, sizeof rrpc->ret_session)) {
376: rpc_SetErr(EINVAL, "Error:: get invalid RPC session ...\n");
377: return -5;
1.2 misho 378: } else
379: Limit = sizeof(struct tagRPCRet);
1.1 misho 380: if (rrpc->ret_retcode < 0 && rrpc->ret_errno) {
1.2 misho 381: rpc_SetErr(rrpc->ret_errno, "Error::Server side: %d %s\n",
382: rrpc->ret_retcode, strerror(rrpc->ret_errno));
1.1 misho 383: return -6;
384: }
1.2 misho 385: if (rrpc->ret_argc * sizeof(rpc_val_t) > BUFSIZ - Limit) {
386: rpc_SetErr(EMSGSIZE, "Error:: reply RPC packet is too big ...\n");
387: return -7;
388: } else
389: Limit += rrpc->ret_argc * sizeof(rpc_val_t);
1.3 misho 390: /* RPC is OK! Go decapsulate variables ... */
1.1 misho 391: if (rrpc->ret_argc) {
392: *out_argc = rrpc->ret_argc;
393: *out_vals = calloc(rrpc->ret_argc, sizeof(rpc_val_t));
394: if (!*out_vals) {
395: LOGERR;
396: *out_argc = 0;
397: return -1;
398: } else
1.2 misho 399: memcpy(*out_vals, buf + sizeof(struct tagRPCRet), Limit - sizeof(struct tagRPCRet));
1.3 misho 400: /* RPC received variables types OK! */
1.2 misho 401: data = (u_char*) buf + Limit;
1.1 misho 402: for (i = 0; i < rrpc->ret_argc; i++)
403: switch ((*out_vals)[i].val_type) {
404: case buffer:
1.2 misho 405: if ((*out_vals)[i].val_len > BUFSIZ - Limit) {
406: rpc_SetErr(EMSGSIZE, "Error:: Too big RPC packet ...\n");
407: free(*out_vals);
408: *out_vals = NULL;
409: *out_argc = 0;
410: return -7;
411: } else
412: Limit += (*out_vals)[i].val_len;
413:
1.1 misho 414: (*out_vals)[i].val.buffer = malloc((*out_vals)[i].val_len);
415: if (!(*out_vals)[i].val.buffer) {
416: rpc_SetErr(errno, "Error:: in prepare RPC reply ...\n");
417: free(*out_vals);
418: *out_vals = NULL;
419: *out_argc = 0;
420: return -1;
421: } else
422: memcpy((*out_vals)[i].val.buffer, data, (*out_vals)[i].val_len);
423: data += (*out_vals)[i].val_len;
424: break;
425: case string:
1.3 misho 426: if ((*out_vals)[i].val_len > BUFSIZ - Limit) {
1.2 misho 427: rpc_SetErr(EMSGSIZE, "Error:: Too big RPC packet ...\n");
428: free(*out_vals);
429: *out_vals = NULL;
430: *out_argc = 0;
431: return -7;
432: } else
1.3 misho 433: Limit += (*out_vals)[i].val_len;
1.2 misho 434:
1.3 misho 435: (*out_vals)[i].val.string = malloc((*out_vals)[i].val_len);
1.1 misho 436: if (!(*out_vals)[i].val.string) {
437: rpc_SetErr(errno, "Error:: in prepare RPC reply ...\n");
438: free(*out_vals);
439: *out_vals = NULL;
440: *out_argc = 0;
441: return -1;
1.3 misho 442: } else
443: memcpy((*out_vals)[i].val.string, data, (*out_vals)[i].val_len);
444: data += (*out_vals)[i].val_len;
1.1 misho 445: break;
446: default:
447: break;
448: }
449: } else {
1.2 misho 450: if (out_argc)
451: *out_argc = 0;
452: if (out_vals)
453: *out_vals = NULL;
1.1 misho 454: }
455:
456: return rrpc->ret_retcode;
457: }
458:
459: /*
460: * rpc_cli_freeVals() Free rpc_val_t array returned from RPC call
461: * @args = Number of arguments in array
462: * @vals = Value elements
463: * return: none
464: */
465: inline void
466: rpc_cli_freeVals(int args, rpc_val_t *vals)
467: {
468: register int i;
469:
470: if (!vals)
471: return;
472:
473: for (i = 0; i < args; i++)
474: RPC_FREE_VAL(&vals[i]);
475:
476: free(vals);
477: vals = NULL;
478: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>