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