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