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