Annotation of libaitrpc/src/cli.c, revision 1.1.1.1.2.9
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.1.1.1.2.9! misho 6: * $Id: cli.c,v 1.1.1.1.2.8 2011/03/15 16:59:44 misho Exp $
1.1 misho 7: *
1.1.1.1.2.9! 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.1.1.1.2.4 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.1.1.1.2.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));
279: memcpy(v, in_vals, in_argc * sizeof(rpc_val_t));
280: Limit += in_argc * sizeof(rpc_val_t);
281: data = (u_char*) v + in_argc * sizeof(rpc_val_t);
282: for (i = 0; i < in_argc; i++) {
283: switch (in_vals[i].val_type) {
284: case buffer:
285: if (Limit + in_vals[i].val_len > BUFSIZ) {
286: ret = -7;
287: break;
288: }
289:
290: memcpy(data, in_vals[i].val.buffer, in_vals[i].val_len);
291: v[i].val.buffer = (uint8_t*) ((void*) data - (void*) v);
292: data += in_vals[i].val_len;
293: Limit += in_vals[i].val_len;
294: break;
295: case string:
296: if (Limit + in_vals[i].val_len + 1 > BUFSIZ) {
297: ret = -7;
298: break;
299: }
300:
301: memcpy(data, in_vals[i].val.string, in_vals[i].val_len);
302: v[i].val.string = (int8_t*) ((void*) data - (void*) v);
1.1.1.1.2.6 misho 303: data += in_vals[i].val_len + 1;
304: Limit += in_vals[i].val_len + 1;
1.1 misho 305: break;
306: default:
307: break;
308: }
309: }
310: if (ret < 0) {
311: rpc_SetErr(EMSGSIZE, "Error:: in prepare RPC packet (-7) ...\n");
312: return ret;
313: }
314: }
315: if ((ret = send(cli->cli_sock, buf, Limit, 0)) == -1) {
316: LOGERR;
317: return -1;
318: }
319: if (ret != Limit) {
1.1.1.1.2.7 misho 320: rpc_SetErr(ECANCELED, "Error:: in send RPC request, should be send %d bytes, really is %d\n",
1.1 misho 321: Limit, ret);
322: return -9;
323: }
324:
325: FD_SET(cli->cli_sock, &fds);
326: if ((ret = select(cli->cli_sock + 1, &fds, NULL, NULL, &tv)) < 1) {
327: if (ret) {
328: LOGERR;
329: } else
330: rpc_SetErr(ETIMEDOUT, "Error:: timeout, no return from RPC server?\n");
331: return -1;
332: }
333: memset(buf, 0, BUFSIZ);
334: if ((ret = recv(cli->cli_sock, buf, BUFSIZ, 0)) == -1) {
335: LOGERR;
336: return -3;
337: }
338: if (!ret) // receive EOF
339: return 0;
1.1.1.1.2.6 misho 340: if (ret < sizeof(struct tagRPCRet)) {
1.1 misho 341: rpc_SetErr(EMSGSIZE, "Error:: too short RPC packet ...\n");
342: return -4;
343: } else
344: rrpc = (struct tagRPCRet*) buf;
345: // check RPC packet session info
346: if (memcmp(&rrpc->ret_session, cli->cli_parent, sizeof rrpc->ret_session)) {
347: rpc_SetErr(EINVAL, "Error:: get invalid RPC session ...\n");
348: return -5;
1.1.1.1.2.6 misho 349: } else
350: Limit = sizeof(struct tagRPCRet);
1.1 misho 351: if (rrpc->ret_retcode < 0 && rrpc->ret_errno) {
1.1.1.1.2.5 misho 352: rpc_SetErr(rrpc->ret_errno, "Error::Server side: %d %s\n",
353: rrpc->ret_retcode, strerror(rrpc->ret_errno));
1.1 misho 354: return -6;
355: }
1.1.1.1.2.6 misho 356: if (rrpc->ret_argc * sizeof(rpc_val_t) > BUFSIZ - Limit) {
357: rpc_SetErr(EMSGSIZE, "Error:: reply RPC packet is too big ...\n");
358: return -7;
359: } else
360: Limit += rrpc->ret_argc * sizeof(rpc_val_t);
1.1 misho 361: // RPC is OK! Go decapsulate variables ...
362: if (rrpc->ret_argc) {
363: *out_argc = rrpc->ret_argc;
364: *out_vals = calloc(rrpc->ret_argc, sizeof(rpc_val_t));
365: if (!*out_vals) {
366: LOGERR;
367: *out_argc = 0;
368: return -1;
369: } else
1.1.1.1.2.6 misho 370: memcpy(*out_vals, buf + sizeof(struct tagRPCRet), Limit - sizeof(struct tagRPCRet));
1.1 misho 371: // RPC received variables types OK!
1.1.1.1.2.6 misho 372: data = (u_char*) buf + Limit;
1.1 misho 373: for (i = 0; i < rrpc->ret_argc; i++)
374: switch ((*out_vals)[i].val_type) {
375: case buffer:
1.1.1.1.2.6 misho 376: if ((*out_vals)[i].val_len > BUFSIZ - Limit) {
377: rpc_SetErr(EMSGSIZE, "Error:: Too big RPC packet ...\n");
378: free(*out_vals);
379: *out_vals = NULL;
380: *out_argc = 0;
381: return -7;
382: } else
383: Limit += (*out_vals)[i].val_len;
384:
1.1 misho 385: (*out_vals)[i].val.buffer = malloc((*out_vals)[i].val_len);
386: if (!(*out_vals)[i].val.buffer) {
387: rpc_SetErr(errno, "Error:: in prepare RPC reply ...\n");
388: free(*out_vals);
389: *out_vals = NULL;
390: *out_argc = 0;
391: return -1;
392: } else
393: memcpy((*out_vals)[i].val.buffer, data, (*out_vals)[i].val_len);
394: data += (*out_vals)[i].val_len;
395: break;
396: case string:
1.1.1.1.2.6 misho 397: if ((*out_vals)[i].val_len + 1 > BUFSIZ - Limit) {
398: rpc_SetErr(EMSGSIZE, "Error:: Too big RPC packet ...\n");
399: free(*out_vals);
400: *out_vals = NULL;
401: *out_argc = 0;
402: return -7;
403: } else
404: Limit += (*out_vals)[i].val_len + 1;
405:
1.1 misho 406: (*out_vals)[i].val.string = (int8_t*) strdup((char*) data);
407: if (!(*out_vals)[i].val.string) {
408: rpc_SetErr(errno, "Error:: in prepare RPC reply ...\n");
409: free(*out_vals);
410: *out_vals = NULL;
411: *out_argc = 0;
412: return -1;
1.1.1.1.2.8 misho 413: } else {
414: /* sanity check ... preserve null byte */
415: (*out_vals)[i].val.string[(*out_vals)[i].val_len] = 0;
1.1 misho 416: }
417: data += (*out_vals)[i].val_len + 1;
418: break;
419: default:
420: break;
421: }
422: } else {
1.1.1.1.2.1 misho 423: if (out_argc)
424: *out_argc = 0;
425: if (out_vals)
426: *out_vals = NULL;
1.1 misho 427: }
428:
429: return rrpc->ret_retcode;
430: }
431:
432: /*
433: * rpc_cli_freeVals() Free rpc_val_t array returned from RPC call
434: * @args = Number of arguments in array
435: * @vals = Value elements
436: * return: none
437: */
438: inline void
439: rpc_cli_freeVals(int args, rpc_val_t *vals)
440: {
441: register int i;
442:
443: if (!vals)
444: return;
445:
446: for (i = 0; i < args; i++)
447: RPC_FREE_VAL(&vals[i]);
448:
449: free(vals);
450: vals = NULL;
451: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>