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