Annotation of libaitrpc/src/cli.c, revision 1.12.2.2
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.12.2.2! misho 6: * $Id: cli.c,v 1.12.2.1 2012/11/19 12:43:21 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:
1.7 misho 15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
1.2 misho 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.7 misho 50: * rpc_cli_openBLOBClient() - Connect to BLOB Server
51: *
1.2 misho 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;
1.4 misho 60:
1.10 misho 61: if (!rpccli) {
1.7 misho 62: rpc_SetErr(EINVAL, "Invalid parameters can`t connect to BLOB server");
1.2 misho 63: return NULL;
64: }
65:
1.11 misho 66: cli = io_malloc(sizeof(rpc_cli_t));
1.2 misho 67: if (!cli) {
68: LOGERR;
69: return NULL;
70: } else
71: memcpy(cli, rpccli, sizeof(rpc_cli_t));
72:
1.10 misho 73: memcpy(&cli->cli_sa, &rpccli->cli_sa, sizeof(io_sockaddr_t));
74: switch (cli->cli_sa.sa.sa_family) {
1.4 misho 75: case AF_INET:
1.10 misho 76: cli->cli_sa.sin.sin_port =
77: htons(Port ? Port : ntohs(cli->cli_sa.sin.sin_port) + 1);
1.4 misho 78: break;
79: case AF_INET6:
1.10 misho 80: cli->cli_sa.sin6.sin6_port =
81: htons(Port ? Port : ntohs(cli->cli_sa.sin6.sin6_port) + 1);
1.4 misho 82: break;
83: case AF_LOCAL:
1.10 misho 84: strlcat(cli->cli_sa.sun.sun_path, ".blob", sizeof cli->cli_sa.sun.sun_path);
1.4 misho 85: break;
1.10 misho 86: default:
87: rpc_SetErr(EINVAL, "Invalid socket type %d", cli->cli_sa.sa.sa_family);
88: return NULL;
1.2 misho 89: }
1.10 misho 90:
91: AIT_COPY_VAL(&cli->cli_buf, &rpccli->cli_buf);
1.2 misho 92:
1.4 misho 93: /* connect to BLOB server */
1.6 misho 94: cli->cli_sock = socket(cli->cli_sa.sa.sa_family, SOCK_STREAM, 0);
1.2 misho 95: if (cli->cli_sock == -1) {
96: LOGERR;
1.11 misho 97: io_free(cli);
1.2 misho 98: return NULL;
99: }
1.6 misho 100: if (connect(cli->cli_sock, &cli->cli_sa.sa, cli->cli_sa.sa.sa_len) == -1) {
1.2 misho 101: LOGERR;
1.5 misho 102: close(cli->cli_sock);
1.11 misho 103: io_free(cli);
1.2 misho 104: return NULL;
1.10 misho 105: } else
106: fcntl(cli->cli_sock, F_SETFL, fcntl(cli->cli_sock, F_GETFL) | O_NONBLOCK);
1.2 misho 107:
108: return cli;
109: }
110:
111: /*
1.7 misho 112: * rpc_cli_closeBLOBClient() - Close connection to BLOB server and free resources
113: *
1.2 misho 114: * @cli = BLOB Client session
115: * return: none
116: */
117: void
1.10 misho 118: rpc_cli_closeBLOBClient(rpc_cli_t ** __restrict cli)
1.2 misho 119: {
1.10 misho 120: if (!cli || !*cli)
1.2 misho 121: return;
122:
1.10 misho 123: shutdown((*cli)->cli_sock, SHUT_RDWR);
124: close((*cli)->cli_sock);
125:
126: AIT_FREE_VAL(&(*cli)->cli_buf);
1.2 misho 127:
1.11 misho 128: io_free(*cli);
1.10 misho 129: *cli = NULL;
1.2 misho 130: }
131:
1.10 misho 132: /* -------------------------------------------------------------- */
1.2 misho 133:
134: /*
1.7 misho 135: * rpc_cli_openClient() - Connect to RPC Server
136: *
1.1 misho 137: * @ProgID = ProgramID for RPC session request
138: * @ProcID = ProcessID for RPC session request
1.10 misho 139: * @netBuf = Network buffer length (min:512 bytes), if =0 == BUFSIZ (also meaning max RPC packet)
1.1 misho 140: * @csHost = Host name or IP address for bind server
141: * @Port = Port for bind server, if Port == 0 default port is selected
1.12.2.1 misho 142: * @proto = Protocol, if == 0 choose SOCK_STREAM
1.1 misho 143: * return: NULL == error or !=NULL connection to RPC server established
144: */
145: rpc_cli_t *
1.12.2.1 misho 146: rpc_cli_openClient(u_int ProgID, u_char ProcID, int netBuf, const char *csHost,
147: u_short Port, int proto)
1.1 misho 148: {
149: rpc_cli_t *cli = NULL;
1.11 misho 150: io_sockaddr_t sa = IO_SOCKADDR_INIT;
1.1 misho 151:
1.10 misho 152: if (!io_gethostbyname(csHost, Port, &sa))
1.1 misho 153: return NULL;
154: if (!Port)
155: Port = RPC_DEFPORT;
1.12.2.1 misho 156: if (!proto)
157: proto = SOCK_STREAM;
1.10 misho 158: if (netBuf < RPC_MIN_BUFSIZ)
1.5 misho 159: netBuf = BUFSIZ;
1.10 misho 160: else
1.12 misho 161: netBuf = io_align(netBuf, 2); /* align netBuf length */
1.10 misho 162:
163: #ifdef HAVE_SRANDOMDEV
164: srandomdev();
165: #else
166: time_t tim;
167:
168: srandom((time(&tim) ^ getpid()));
169: #endif
1.1 misho 170:
1.11 misho 171: cli = io_malloc(sizeof(rpc_cli_t));
1.1 misho 172: if (!cli) {
173: LOGERR;
174: return NULL;
175: } else
176: memset(cli, 0, sizeof(rpc_cli_t));
1.5 misho 177:
1.10 misho 178: /* build session */
1.11 misho 179: cli->cli_parent = io_malloc(sizeof(rpc_sess_t));
1.1 misho 180: if (!cli->cli_parent) {
181: LOGERR;
1.11 misho 182: io_free(cli);
1.1 misho 183: return NULL;
184: } else {
185: ((rpc_sess_t*) cli->cli_parent)->sess_version = RPC_VERSION;
186: ((rpc_sess_t*) cli->cli_parent)->sess_program = ProgID;
187: ((rpc_sess_t*) cli->cli_parent)->sess_process = ProcID;
188: }
189:
1.12.2.1 misho 190: cli->cli_id = proto;
1.6 misho 191: memcpy(&cli->cli_sa, &sa, sizeof cli->cli_sa);
1.10 misho 192: AIT_SET_BUF2(&cli->cli_buf, 0, netBuf);
1.4 misho 193:
194: /* connect to RPC server */
1.12.2.1 misho 195: cli->cli_sock = socket(cli->cli_sa.sa.sa_family, cli->cli_id, 0);
1.1 misho 196: if (cli->cli_sock == -1) {
197: LOGERR;
1.10 misho 198: AIT_FREE_VAL(&cli->cli_buf);
1.11 misho 199: io_free(cli->cli_parent);
200: io_free(cli);
1.5 misho 201: return NULL;
202: }
1.12.2.1 misho 203: if (cli->cli_id == SOCK_STREAM)
204: if (connect(cli->cli_sock, &cli->cli_sa.sa, cli->cli_sa.sa.sa_len) == -1) {
205: LOGERR;
206: AIT_FREE_VAL(&cli->cli_buf);
207: close(cli->cli_sock);
208: io_free(cli->cli_parent);
209: io_free(cli);
210: return NULL;
211: }
1.1 misho 212:
1.12.2.1 misho 213: fcntl(cli->cli_sock, F_SETFL, fcntl(cli->cli_sock, F_GETFL) | O_NONBLOCK);
1.1 misho 214: return cli;
215: }
216:
217: /*
1.7 misho 218: * rpc_cli_closeClient() - Close connection to RPC server and free resources
219: *
1.1 misho 220: * @cli = RPC Client session
221: * return: none
222: */
223: void
1.10 misho 224: rpc_cli_closeClient(rpc_cli_t ** __restrict cli)
1.1 misho 225: {
1.10 misho 226: if (!cli || !*cli)
1.1 misho 227: return;
228:
1.12.2.1 misho 229: if ((*cli)->cli_id == SOCK_STREAM)
230: shutdown((*cli)->cli_sock, SHUT_RDWR);
1.10 misho 231: close((*cli)->cli_sock);
232:
233: AIT_FREE_VAL(&(*cli)->cli_buf);
234:
235: if ((*cli)->cli_parent)
1.11 misho 236: io_free((*cli)->cli_parent);
1.1 misho 237:
1.11 misho 238: io_free(*cli);
1.10 misho 239: *cli = NULL;
1.1 misho 240: }
241:
242:
243: /*
1.7 misho 244: * rpc_cli_execCall() - Execute RPC call
245: *
1.1 misho 246: * @cli = RPC Client session
1.8 misho 247: * @noreply = We not want RPC reply
1.10 misho 248: * @tag = Function tag for execution
249: * @in_vars = IN RPC call array of rpc values, may be NULL
250: * @out_vars = OUT returned array of rpc values, if !=NULL must be free after use with io_freeVars()
1.1 misho 251: * return: -1 error or != -1 ok result
252: */
253: int
1.10 misho 254: rpc_cli_execCall(rpc_cli_t *cli, int noreply, u_short tag,
1.5 misho 255: array_t * __restrict in_vars, array_t ** __restrict out_vars)
1.1 misho 256: {
1.5 misho 257: struct tagRPCCall *rpc;
1.7 misho 258: int ret = 0, wlen = sizeof(struct tagRPCCall);
1.10 misho 259: uint16_t crc;
260: u_char *buf;
1.9 misho 261: struct pollfd pfd;
1.12.2.1 misho 262: io_sockaddr_t sa;
263: socklen_t salen;
1.1 misho 264:
1.10 misho 265: if (!cli) {
1.7 misho 266: rpc_SetErr(EINVAL, "Can`t execute call because parameter is null or invalid!");
1.1 misho 267: return -1;
1.10 misho 268: } else
269: buf = AIT_GET_BUF(&cli->cli_buf);
1.5 misho 270: if (out_vars)
271: *out_vars = NULL;
272:
1.12.2.2! misho 273: memset(&sa, 0, sizeof sa);
! 274:
1.4 misho 275: /* prepare RPC call */
1.5 misho 276: rpc = (struct tagRPCCall*) buf;
1.6 misho 277: rpc_addPktSession(&rpc->call_session, cli->cli_parent);
278: rpc->call_argc = htons(io_arraySize(in_vars));
1.10 misho 279: rpc->call_tag = htons(tag);
280: rpc->call_seq = htons(random() % USHRT_MAX);
1.5 misho 281:
1.8 misho 282: /* set reply */
283: rpc->call_req.flags = noreply ? RPC_NOREPLY : RPC_REPLY;
284:
1.6 misho 285: if (io_arraySize(in_vars)) {
1.5 misho 286: /* marshaling variables */
1.10 misho 287: ret = io_vars2buffer(buf + wlen, AIT_LEN(&cli->cli_buf) - wlen, in_vars);
1.5 misho 288: if (ret == -1) {
1.7 misho 289: rpc_SetErr(EBADRPC, "Failed to prepare RPC packet values");
290: return -1;
1.3 misho 291: } else
1.7 misho 292: wlen += ret;
1.1 misho 293: }
1.4 misho 294:
1.10 misho 295: /* total packet length */
1.8 misho 296: rpc->call_len = htons(wlen);
297:
1.7 misho 298: /* calculate CRC */
299: rpc->call_crc ^= rpc->call_crc;
1.8 misho 300: rpc->call_crc = htons(crcFletcher16((u_short*) buf, wlen / 2));
1.7 misho 301:
1.9 misho 302: pfd.fd = cli->cli_sock;
1.10 misho 303: pfd.events = POLLOUT;
304: if ((ret = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) < 1 ||
1.9 misho 305: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
1.4 misho 306: if (ret)
1.1 misho 307: LOGERR;
1.4 misho 308: else
1.10 misho 309: rpc_SetErr(ETIMEDOUT, "Timeout, can't send to RPC server");
1.1 misho 310: return -1;
311: }
1.10 misho 312: do {
1.12.2.1 misho 313: if (cli->cli_id == SOCK_STREAM)
314: ret = send(cli->cli_sock, buf, wlen, MSG_NOSIGNAL);
315: else
316: ret = sendto(cli->cli_sock, buf, wlen, MSG_NOSIGNAL,
317: &cli->cli_sa.sa, cli->cli_sa.sa.sa_len);
318: if (ret == -1) {
1.10 misho 319: if (errno == EAGAIN)
320: continue;
321: LOGERR;
322: return -1;
323: } else if (ret != wlen) {
324: rpc_SetErr(EPROCUNAVAIL, "RPC request, should be send %d bytes, "
325: "really sended %d bytes", wlen, ret);
326: return -1;
327: }
328: } while (0);
329:
330: if (noreply) /* we not want reply */
1.1 misho 331: return 0;
1.10 misho 332:
333: wlen = 0;
334: /* reply from RPC server */
335: pfd.events = POLLIN | POLLPRI;
336: do {
337: if ((ret = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) < 1 ||
338: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
1.12.2.2! misho 339: if (ret) {
1.10 misho 340: LOGERR;
1.12.2.2! misho 341: } else {
1.10 misho 342: if (wlen++ < 7)
343: continue;
344: else
345: rpc_SetErr(ETIMEDOUT, "Timeout, no answer from RPC server");
346: }
1.12.2.2! misho 347:
1.10 misho 348: return -1;
349: }
1.12.2.2! misho 350:
1.10 misho 351: memset(buf, 0, AIT_LEN(&cli->cli_buf));
1.12.2.1 misho 352: if (cli->cli_id == SOCK_STREAM)
353: ret = recv(cli->cli_sock, buf, AIT_LEN(&cli->cli_buf), 0);
354: else
355: ret = recvfrom(cli->cli_sock, buf, AIT_LEN(&cli->cli_buf), 0,
356: &sa.sa, &salen);
357: if (ret < 1) {
1.10 misho 358: if (ret) {
359: if (errno == EAGAIN)
360: continue;
361: else
362: LOGERR;
363: }
364: return -1;
365: }
1.12.2.2! misho 366:
! 367: if (cli->cli_id == SOCK_DGRAM) {
! 368: /* check for response from known address */
! 369: if (io_addrcmp(&cli->cli_sa, &sa, 42)) {
! 370: rpc_SetErr(ERPCMISMATCH, "Received RPC response from unknown address");
! 371: continue;
! 372: }
1.12.2.1 misho 373: }
1.10 misho 374: } while (0);
1.7 misho 375: if (ret < sizeof(struct tagRPCCall)) {
1.10 misho 376: rpc_SetErr(ERPCMISMATCH, "Short RPC packet %d bytes", ret);
1.8 misho 377: return -1;
1.7 misho 378: }
1.8 misho 379:
380: /* calculate CRC */
381: crc = ntohs(rpc->call_crc);
382: rpc->call_crc ^= rpc->call_crc;
1.10 misho 383: if (crc != crcFletcher16((u_short*) buf, ntohs(rpc->call_len) / 2)) {
1.8 misho 384: rpc_SetErr(ERPCMISMATCH, "Bad CRC RPC packet");
385: return -1;
386: }
387:
1.3 misho 388: /* check RPC packet session info */
1.7 misho 389: if (rpc_chkPktSession(&rpc->call_session, cli->cli_parent)) {
1.10 misho 390: rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session");
1.8 misho 391: return -1;
1.2 misho 392: } else
1.7 misho 393: wlen = sizeof(struct tagRPCCall);
1.10 misho 394: if (ntohs(rpc->call_tag) != tag) {
395: rpc_SetErr(ERPCMISMATCH, "Get wrong RPC reply");
1.8 misho 396: return -1;
1.6 misho 397: }
1.10 misho 398: if (ntohl(rpc->call_rep.eno) && ntohl(rpc->call_rep.ret) == -1) {
1.8 misho 399: rpc_SetErr(ntohl(rpc->call_rep.eno), "Server side: retcode=%d #%d %s",
1.7 misho 400: ntohl(rpc->call_rep.ret), ntohl(rpc->call_rep.eno),
401: strerror(ntohl(rpc->call_rep.eno)));
1.8 misho 402: return -1;
1.1 misho 403: }
1.10 misho 404: if (ntohs(rpc->call_argc) * sizeof(ait_val_t) > AIT_LEN(&cli->cli_buf) - wlen) {
1.8 misho 405: rpc_SetErr(EMSGSIZE, "Reply RPC packet is too long ...");
406: return -1;
1.5 misho 407: }
408:
409: /* RPC is OK! Go de-marshaling variables ... */
1.10 misho 410: if (out_vars && ntohs(rpc->call_argc)) {
1.12 misho 411: #ifdef CLI_RES_ZCOPY
412: *out_vars = io_buffer2vars(buf + wlen, AIT_LEN(&cli->cli_buf) - wlen,
413: ntohs(rpc->call_argc), 42);
414: #else
1.10 misho 415: *out_vars = io_buffer2vars(buf + wlen, AIT_LEN(&cli->cli_buf) - wlen,
1.7 misho 416: ntohs(rpc->call_argc), 0);
1.12 misho 417: #endif
1.4 misho 418: if (!*out_vars) {
1.10 misho 419: rpc_SetErr(io_GetErrno(), "%s", io_GetError());
1.1 misho 420: return -1;
1.5 misho 421: }
1.1 misho 422: }
423:
1.7 misho 424: ret = ntohl(rpc->call_rep.ret);
1.10 misho 425: return ret;
426: }
427:
428: /*
1.12 misho 429: * rpc_cli_freeCall() - Free resouce allocated by RPC call
430: *
431: * @out_vars = Returned array with variables from RPC call
432: * return: none
433: */
434: inline void
435: rpc_cli_freeCall(array_t ** __restrict out_vars)
436: {
437: #ifdef CLI_RES_ZCOPY
438: io_arrayDestroy(out_vars);
439: #else
440: io_freeVars(out_vars);
441: #endif
442: }
443:
444: /*
1.10 misho 445: * rpc_cli_ping() - Ping RPC server
446: *
447: * @cli = connected client
448: * return: -1 error or !=-1 ping seq id
449: */
450: inline int
451: rpc_cli_ping(rpc_cli_t *cli)
452: {
453: int ret = 0;
454: array_t *arr = NULL;
455:
456: if (!cli)
457: return -1;
458:
459: if (rpc_cli_execCall(cli, RPC_REPLY, CALL_SRVPING, NULL, &arr))
460: return -1;
461: else
462: ret = AIT_GET_U16(io_array(arr, 0, ait_val_t*));
1.12 misho 463: rpc_cli_freeCall(&arr);
1.10 misho 464:
1.5 misho 465: return ret;
1.1 misho 466: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>