1: /*************************************************************************
2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
3: * by Michael Pounov <misho@openbsd-bg.org>
4: *
5: * $Author: misho $
6: * $Id: cli.c,v 1.5.2.4 2011/10/31 14:45:26 misho Exp $
7: *
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: */
46: #include "global.h"
47:
48:
49: /*
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;
59: io_sockaddr_t sa;
60: int n;
61:
62: if (!rpccli ||
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)) {
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:
78: memcpy(&sa, &rpccli->cli_sa, sizeof sa);
79: switch (rpccli->cli_sa.sa.sa_family) {
80: case AF_INET:
81: sa.sin.sin_port = htons(Port);
82: break;
83: case AF_INET6:
84: sa.sin6.sin6_port = htons(Port);
85: break;
86: case AF_LOCAL:
87: strlcat(sa.sun.sun_path, ".blob", sizeof sa.sun.sun_path);
88: break;
89: }
90: memcpy(&cli->cli_sa, &sa, sizeof sa);
91:
92: /* connect to BLOB server */
93: cli->cli_sock = socket(cli->cli_sa.sa.sa_family, SOCK_STREAM, 0);
94: if (cli->cli_sock == -1) {
95: LOGERR;
96: free(cli);
97: return NULL;
98: }
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: }
112: if (connect(cli->cli_sock, &cli->cli_sa.sa, sizeof cli->cli_sa) == -1) {
113: LOGERR;
114: close(cli->cli_sock);
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: /*
145: * rpc_cli_openClient() Connect to RPC Server
146: * @ProgID = ProgramID for RPC session request
147: * @ProcID = ProcessID for RPC session request
148: * @netBuf = Network buffer length, if =0 == BUFSIZ (also meaning max RPC packet)
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 *
155: rpc_cli_openClient(u_int ProgID, u_int ProcID, int netBuf, u_short family, const char *csHost, u_short Port)
156: {
157: rpc_cli_t *cli = NULL;
158: struct hostent *host = NULL;
159: io_sockaddr_t sa;
160: int n;
161:
162: if (!csHost || (family != AF_INET && family != AF_INET6 && family != AF_LOCAL)) {
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;
168: if (!netBuf)
169: netBuf = BUFSIZ;
170: if (csHost && family != AF_LOCAL) {
171: host = gethostbyname2(csHost, family);
172: if (!host) {
173: rpc_SetErr(h_errno, "Error:: %s\n", hstrerror(h_errno));
174: return NULL;
175: }
176: }
177: memset(&sa, 0, sizeof sa);
178: sa.sa.sa_family = family;
179: switch (family) {
180: case AF_INET:
181: sa.sin.sin_len = sizeof(struct sockaddr_in);
182: sa.sin.sin_port = htons(Port);
183: if (csHost)
184: memcpy(&sa.sin.sin_addr, host->h_addr, host->h_length);
185: break;
186: case AF_INET6:
187: sa.sin6.sin6_len = sizeof(struct sockaddr_in6);
188: sa.sin6.sin6_port = htons(Port);
189: if (csHost)
190: memcpy(&sa.sin6.sin6_addr, host->h_addr, host->h_length);
191: break;
192: case AF_LOCAL:
193: sa.sun.sun_len = sizeof(struct sockaddr_un);
194: if (csHost)
195: strlcpy(sa.sun.sun_path, csHost, sizeof sa.sun.sun_path);
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));
208:
209: cli->cli_netbuf = netBuf;
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:
221: memcpy(&cli->cli_sa, &sa, sizeof cli->cli_sa);
222:
223: /* connect to RPC server */
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: }
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: }
246: if (connect(cli->cli_sock, &cli->cli_sa.sa, sizeof cli->cli_sa) == -1) {
247: LOGERR;
248: close(cli->cli_sock);
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
285: * @in_vars = IN RPC call array of rpc values
286: * @out_vars = OUT returned array of rpc values, must be free after use with rpc_cli_freeVals()
287: * return: -1 error or != -1 ok result
288: */
289: int
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)
292: {
293: fd_set fds;
294: u_char *buf, str[MAXPATHLEN + UCHAR_MAX + 1];
295: struct tagRPCCall *rpc;
296: struct tagRPCRet *rrpc = NULL;
297: int ret = 0, Limit = 0;
298: struct timeval tv = { DEF_RPC_TIMEOUT, 0 };
299: uint16_t tag;
300: uint32_t hash;
301:
302: if (!cli || !csFunc) {
303: rpc_SetErr(EINVAL, "Error:: Can`t execute call because parameter is null or invalid!\n");
304: return -1;
305: }
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:
316: /* build RPC call string for hash */
317: memset(str, 0, sizeof str);
318: if (csModule)
319: strlcpy((char*) str, csModule, sizeof str);
320: strlcat((char*) str, "__", sizeof str);
321: strlcat((char*) str, csFunc, sizeof str);
322:
323: /* prepare RPC call */
324: rpc = (struct tagRPCCall*) buf;
325: rpc_addPktSession(&rpc->call_session, cli->cli_parent);
326: rpc->call_argc = htons(in_vars ? io_arraySize(in_vars) : 0);
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));
329: Limit = sizeof(struct tagRPCCall);
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);
337: return -7;
338: } else
339: Limit += ret;
340: }
341:
342: if ((ret = send(cli->cli_sock, buf, Limit, 0)) == -1) {
343: LOGERR;
344: free(buf);
345: return -1;
346: }
347: if (ret != Limit) {
348: rpc_SetErr(EPROCUNAVAIL, "Error:: in send RPC request, should be send %d bytes, really is %d\n",
349: Limit, ret);
350: free(buf);
351: return -9;
352: }
353:
354: /* reply from RPC server */
355: FD_ZERO(&fds);
356: FD_SET(cli->cli_sock, &fds);
357: if ((ret = select(cli->cli_sock + 1, &fds, NULL, NULL, &tv)) < 1) {
358: if (ret)
359: LOGERR;
360: else
361: rpc_SetErr(ETIMEDOUT, "Error:: timeout, no return from RPC server?\n");
362:
363: free(buf);
364: return -1;
365: }
366: memset(buf, 0, cli->cli_netbuf);
367: if ((ret = recv(cli->cli_sock, buf, cli->cli_netbuf, 0)) == -1) {
368: LOGERR;
369: free(buf);
370: return -3;
371: }
372: if (!ret) { /* receive EOF! */
373: free(buf);
374: return 0;
375: }
376: if (ret < sizeof(struct tagRPCRet)) {
377: rpc_SetErr(ERPCMISMATCH, "Error:: too short RPC packet ...\n");
378: free(buf);
379: return -4;
380: } else
381: rrpc = (struct tagRPCRet*) buf;
382: /* check RPC packet session info */
383: if (rpc_chkPktSession(&rrpc->ret_session, cli->cli_parent)) {
384: rpc_SetErr(ERPCMISMATCH, "Error:: get invalid RPC session ...\n");
385: free(buf);
386: return -5;
387: } else
388: Limit = sizeof(struct tagRPCRet);
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: }
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)));
399: free(buf);
400: return -6;
401: }
402: if (ntohs(rrpc->ret_argc) * sizeof(ait_val_t) > cli->cli_netbuf - Limit) {
403: rpc_SetErr(EMSGSIZE, "Error:: reply RPC packet is too long ...\n");
404: free(buf);
405: return -7;
406: }
407:
408: /* RPC is OK! Go de-marshaling variables ... */
409: if (ntohs(rrpc->ret_argc)) {
410: *out_vars = io_buffer2vals(buf + Limit, cli->cli_netbuf - Limit,
411: ntohs(rrpc->ret_argc), 0);
412: if (!*out_vars) {
413: free(buf);
414: return -1;
415: }
416: }
417:
418: ret = ntohl(rrpc->ret_retcode);
419: free(buf);
420: return ret;
421: }
422:
423: /*
424: * rpc_cli_freeVals() Free ait_val_t array returned from RPC call
425: * @vars = Variable array
426: * return: none
427: */
428: inline void
429: rpc_cli_freeVals(array_t ** __restrict vars)
430: {
431: register int i;
432:
433: if (!vars || !*vars)
434: return;
435:
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: }
473:
474: return arr;
475: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>