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.9.2.5 2012/05/16 14:08:52 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, 2012
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: *
52: * @rpccli = RPC Client session
53: * @Port = Port for bind server, if Port == 0 default port is selected
54: * return: NULL == error or !=NULL connection to BLOB server established
55: */
56: rpc_cli_t *
57: rpc_cli_openBLOBClient(rpc_cli_t * __restrict rpccli, u_short Port)
58: {
59: rpc_cli_t *cli = NULL;
60:
61: if (!rpccli) {
62: rpc_SetErr(EINVAL, "Invalid parameters can`t connect to BLOB server");
63: return NULL;
64: }
65:
66: cli = malloc(sizeof(rpc_cli_t));
67: if (!cli) {
68: LOGERR;
69: return NULL;
70: } else
71: memcpy(cli, rpccli, sizeof(rpc_cli_t));
72:
73: memcpy(&cli->cli_sa, &rpccli->cli_sa, sizeof(io_sockaddr_t));
74: switch (cli->cli_sa.sa.sa_family) {
75: case AF_INET:
76: cli->cli_sa.sin.sin_port =
77: htons(Port ? Port : ntohs(cli->cli_sa.sin.sin_port) + 1);
78: break;
79: case AF_INET6:
80: cli->cli_sa.sin6.sin6_port =
81: htons(Port ? Port : ntohs(cli->cli_sa.sin6.sin6_port) + 1);
82: break;
83: case AF_LOCAL:
84: strlcat(cli->cli_sa.sun.sun_path, ".blob", sizeof cli->cli_sa.sun.sun_path);
85: break;
86: default:
87: rpc_SetErr(EINVAL, "Invalid socket type %d", cli->cli_sa.sa.sa_family);
88: return NULL;
89: }
90:
91: /* connect to BLOB server */
92: cli->cli_sock = socket(cli->cli_sa.sa.sa_family, SOCK_STREAM, 0);
93: if (cli->cli_sock == -1) {
94: LOGERR;
95: free(cli);
96: return NULL;
97: }
98: if (connect(cli->cli_sock, &cli->cli_sa.sa, cli->cli_sa.sa.sa_len) == -1) {
99: LOGERR;
100: close(cli->cli_sock);
101: free(cli);
102: return NULL;
103: }
104:
105: return cli;
106: }
107:
108: /*
109: * rpc_cli_closeBLOBClient() - Close connection to BLOB server and free resources
110: *
111: * @cli = BLOB Client session
112: * return: none
113: */
114: void
115: rpc_cli_closeBLOBClient(rpc_cli_t ** __restrict cli)
116: {
117: if (!cli || !*cli)
118: return;
119:
120: shutdown((*cli)->cli_sock, SHUT_RDWR);
121: close((*cli)->cli_sock);
122:
123: AIT_FREE_VAL(&(*cli)->cli_buf);
124:
125: free(*cli);
126: *cli = NULL;
127: }
128:
129: /* -------------------------------------------------------------- */
130:
131: /*
132: * rpc_cli_openClient() - Connect to RPC Server
133: *
134: * @ProgID = ProgramID for RPC session request
135: * @ProcID = ProcessID for RPC session request
136: * @netBuf = Network buffer length, if =0 == BUFSIZ (also meaning max RPC packet)
137: * @csHost = Host name or IP address for bind server
138: * @Port = Port for bind server, if Port == 0 default port is selected
139: * return: NULL == error or !=NULL connection to RPC server established
140: */
141: rpc_cli_t *
142: rpc_cli_openClient(u_int ProgID, u_char ProcID, int netBuf, const char *csHost, u_short Port)
143: {
144: rpc_cli_t *cli = NULL;
145: io_sockaddr_t sa;
146:
147: if (!io_gethostbyname(csHost, Port, &sa))
148: return NULL;
149: if (!Port)
150: Port = RPC_DEFPORT;
151: if (!netBuf)
152: netBuf = BUFSIZ;
153:
154: #ifdef HAVE_SRANDOMDEV
155: srandomdev();
156: #else
157: time_t tim;
158:
159: srandom((time(&tim) ^ getpid()));
160: #endif
161:
162: cli = malloc(sizeof(rpc_cli_t));
163: if (!cli) {
164: LOGERR;
165: return NULL;
166: } else
167: memset(cli, 0, sizeof(rpc_cli_t));
168:
169: /* build session */
170: cli->cli_parent = malloc(sizeof(rpc_sess_t));
171: if (!cli->cli_parent) {
172: LOGERR;
173: free(cli);
174: return NULL;
175: } else {
176: ((rpc_sess_t*) cli->cli_parent)->sess_version = RPC_VERSION;
177: ((rpc_sess_t*) cli->cli_parent)->sess_program = ProgID;
178: ((rpc_sess_t*) cli->cli_parent)->sess_process = ProcID;
179: }
180:
181: memcpy(&cli->cli_sa, &sa, sizeof cli->cli_sa);
182: AIT_SET_BUF2(&cli->cli_buf, 0, netBuf);
183:
184: /* connect to RPC server */
185: cli->cli_sock = socket(cli->cli_sa.sa.sa_family, SOCK_STREAM, 0);
186: if (cli->cli_sock == -1) {
187: LOGERR;
188: AIT_FREE_VAL(&cli->cli_buf);
189: free(cli->cli_parent);
190: free(cli);
191: return NULL;
192: }
193: if (connect(cli->cli_sock, &cli->cli_sa.sa, cli->cli_sa.sa.sa_len) == -1) {
194: LOGERR;
195: AIT_FREE_VAL(&cli->cli_buf);
196: close(cli->cli_sock);
197: free(cli->cli_parent);
198: free(cli);
199: return NULL;
200: }
201:
202: return cli;
203: }
204:
205: /*
206: * rpc_cli_closeClient() - Close connection to RPC server and free resources
207: *
208: * @cli = RPC Client session
209: * return: none
210: */
211: void
212: rpc_cli_closeClient(rpc_cli_t ** __restrict cli)
213: {
214: if (!cli || !*cli)
215: return;
216:
217: shutdown((*cli)->cli_sock, SHUT_RDWR);
218: close((*cli)->cli_sock);
219:
220: AIT_FREE_VAL(&(*cli)->cli_buf);
221:
222: if ((*cli)->cli_parent)
223: free((*cli)->cli_parent);
224:
225: free(*cli);
226: *cli = NULL;
227: }
228:
229:
230: /*
231: * rpc_cli_execCall() - Execute RPC call
232: *
233: * @cli = RPC Client session
234: * @noreply = We not want RPC reply
235: * @tag = Function tag for execution
236: * @in_vars = IN RPC call array of rpc values
237: * @out_vars = OUT returned array of rpc values, must be free after use with rpc_cli_freeVals()
238: * return: -1 error or != -1 ok result
239: */
240: int
241: rpc_cli_execCall(rpc_cli_t *cli, int noreply, u_short tag,
242: array_t * __restrict in_vars, array_t ** __restrict out_vars)
243: {
244: struct tagRPCCall *rpc;
245: int ret = 0, wlen = sizeof(struct tagRPCCall);
246: uint16_t crc;
247: u_char *buf;
248: struct pollfd pfd;
249:
250: if (!cli) {
251: rpc_SetErr(EINVAL, "Can`t execute call because parameter is null or invalid!");
252: return -1;
253: } else
254: buf = AIT_GET_BUF(&cli->cli_buf);
255: if (out_vars)
256: *out_vars = NULL;
257:
258: /* prepare RPC call */
259: rpc = (struct tagRPCCall*) buf;
260: rpc_addPktSession(&rpc->call_session, cli->cli_parent);
261: rpc->call_argc = htons(io_arraySize(in_vars));
262: rpc->call_tag = htons(tag);
263:
264: /* set reply */
265: rpc->call_req.flags = noreply ? RPC_NOREPLY : RPC_REPLY;
266:
267: if (io_arraySize(in_vars)) {
268: /* marshaling variables */
269: ret = io_vars2buffer(buf + wlen, AIT_LEN(&cli->cli_buf) - wlen, in_vars);
270: if (ret == -1) {
271: rpc_SetErr(EBADRPC, "Failed to prepare RPC packet values");
272: return -1;
273: } else
274: wlen += ret;
275: }
276:
277: /* total packet length */
278: rpc->call_len = htons(wlen);
279:
280: /* calculate CRC */
281: rpc->call_crc ^= rpc->call_crc;
282: rpc->call_crc = htons(crcFletcher16((u_short*) buf, wlen / 2));
283:
284: if ((ret = send(cli->cli_sock, buf, wlen, MSG_NOSIGNAL)) == -1) {
285: LOGERR;
286: return -1;
287: } else if (ret != wlen) {
288: rpc_SetErr(EPROCUNAVAIL, "RPC request, should be send %d bytes, "
289: "really sended %d bytes", wlen, ret);
290: return -1;
291: }
292:
293: if (noreply) /* we not want reply */
294: return 0;
295:
296: /* reply from RPC server */
297: pfd.fd = cli->cli_sock;
298: pfd.events = POLLIN | POLLPRI;
299: if ((ret = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) == -1 ||
300: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
301: if (ret)
302: LOGERR;
303: else
304: rpc_SetErr(ETIMEDOUT, "Timeout, no answer from RPC server");
305:
306: return -1;
307: }
308: memset(buf, 0, AIT_LEN(&cli->cli_buf));
309: if ((ret = recv(cli->cli_sock, buf, AIT_LEN(&cli->cli_buf), 0)) < 1) {
310: if (ret)
311: LOGERR;
312: return -1;
313: }
314: if (ret < sizeof(struct tagRPCCall)) {
315: rpc_SetErr(ERPCMISMATCH, "Short RPC packet");
316: return -1;
317: }
318:
319: /* calculate CRC */
320: crc = ntohs(rpc->call_crc);
321: rpc->call_crc ^= rpc->call_crc;
322: if (crc != crcFletcher16((u_short*) buf, ret / 2)) {
323: rpc_SetErr(ERPCMISMATCH, "Bad CRC RPC packet");
324: return -1;
325: }
326:
327: /* check RPC packet session info */
328: if (rpc_chkPktSession(&rpc->call_session, cli->cli_parent)) {
329: rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session");
330: return -1;
331: } else
332: wlen = sizeof(struct tagRPCCall);
333: if (ntohs(rpc->call_tag) != tag) {
334: rpc_SetErr(ERPCMISMATCH, "Get wrong RPC reply");
335: return -1;
336: }
337: if (ntohl(rpc->call_rep.ret) < 0 && ntohl(rpc->call_rep.eno)) {
338: rpc_SetErr(ntohl(rpc->call_rep.eno), "Server side: retcode=%d #%d %s",
339: ntohl(rpc->call_rep.ret), ntohl(rpc->call_rep.eno),
340: strerror(ntohl(rpc->call_rep.eno)));
341: return -1;
342: }
343: if (ntohs(rpc->call_argc) * sizeof(ait_val_t) > AIT_LEN(&cli->cli_buf) - wlen) {
344: rpc_SetErr(EMSGSIZE, "Reply RPC packet is too long ...");
345: return -1;
346: }
347:
348: /* RPC is OK! Go de-marshaling variables ... */
349: if (ntohs(rpc->call_argc)) {
350: *out_vars = io_buffer2vars(buf + wlen, AIT_LEN(&cli->cli_buf) - wlen,
351: ntohs(rpc->call_argc), 0);
352: if (!*out_vars) {
353: return -1;
354: }
355: }
356:
357: ret = ntohl(rpc->call_rep.ret);
358: return ret;
359: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>