Annotation of libaitrpc/src/cli.c, revision 1.13.4.4
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.13.4.4! misho 6: * $Id: cli.c,v 1.13.4.3 2013/01/17 17:01:51 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.13.4.1 misho 15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
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.13 misho 60: int n;
1.4 misho 61:
1.10 misho 62: if (!rpccli) {
1.7 misho 63: rpc_SetErr(EINVAL, "Invalid parameters can`t connect to BLOB server");
1.2 misho 64: return NULL;
65: }
66:
1.13.4.1 misho 67: cli = e_malloc(sizeof(rpc_cli_t));
1.2 misho 68: if (!cli) {
69: LOGERR;
70: return NULL;
71: } else
72: memcpy(cli, rpccli, sizeof(rpc_cli_t));
73:
1.13.4.1 misho 74: memcpy(&cli->cli_sa, &rpccli->cli_sa, sizeof(sockaddr_t));
1.10 misho 75: switch (cli->cli_sa.sa.sa_family) {
1.4 misho 76: case AF_INET:
1.10 misho 77: cli->cli_sa.sin.sin_port =
78: htons(Port ? Port : ntohs(cli->cli_sa.sin.sin_port) + 1);
1.4 misho 79: break;
80: case AF_INET6:
1.10 misho 81: cli->cli_sa.sin6.sin6_port =
82: htons(Port ? Port : ntohs(cli->cli_sa.sin6.sin6_port) + 1);
1.4 misho 83: break;
84: case AF_LOCAL:
1.10 misho 85: strlcat(cli->cli_sa.sun.sun_path, ".blob", sizeof cli->cli_sa.sun.sun_path);
1.4 misho 86: break;
1.10 misho 87: default:
88: rpc_SetErr(EINVAL, "Invalid socket type %d", cli->cli_sa.sa.sa_family);
89: return NULL;
1.2 misho 90: }
1.10 misho 91:
92: AIT_COPY_VAL(&cli->cli_buf, &rpccli->cli_buf);
1.13 misho 93: n = AIT_LEN(&cli->cli_buf);
1.2 misho 94:
1.4 misho 95: /* connect to BLOB server */
1.6 misho 96: cli->cli_sock = socket(cli->cli_sa.sa.sa_family, SOCK_STREAM, 0);
1.2 misho 97: if (cli->cli_sock == -1) {
98: LOGERR;
1.13.4.1 misho 99: e_free(cli);
1.2 misho 100: return NULL;
101: }
1.13 misho 102: if (setsockopt(cli->cli_sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof n) == -1) {
103: LOGERR;
104: close(cli->cli_sock);
1.13.4.1 misho 105: e_free(cli);
1.13 misho 106: return NULL;
107: }
108: if (setsockopt(cli->cli_sock, SOL_SOCKET, SO_RCVBUF, &n, sizeof n) == -1) {
109: LOGERR;
110: close(cli->cli_sock);
1.13.4.1 misho 111: e_free(cli);
1.13 misho 112: return NULL;
113: }
1.6 misho 114: if (connect(cli->cli_sock, &cli->cli_sa.sa, cli->cli_sa.sa.sa_len) == -1) {
1.2 misho 115: LOGERR;
1.5 misho 116: close(cli->cli_sock);
1.13.4.1 misho 117: e_free(cli);
1.2 misho 118: return NULL;
1.10 misho 119: } else
120: fcntl(cli->cli_sock, F_SETFL, fcntl(cli->cli_sock, F_GETFL) | O_NONBLOCK);
1.2 misho 121:
122: return cli;
123: }
124:
125: /*
1.7 misho 126: * rpc_cli_closeBLOBClient() - Close connection to BLOB server and free resources
127: *
1.2 misho 128: * @cli = BLOB Client session
129: * return: none
130: */
131: void
1.10 misho 132: rpc_cli_closeBLOBClient(rpc_cli_t ** __restrict cli)
1.2 misho 133: {
1.10 misho 134: if (!cli || !*cli)
1.2 misho 135: return;
136:
1.10 misho 137: shutdown((*cli)->cli_sock, SHUT_RDWR);
138: close((*cli)->cli_sock);
139:
140: AIT_FREE_VAL(&(*cli)->cli_buf);
1.2 misho 141:
1.13.4.1 misho 142: e_free(*cli);
1.10 misho 143: *cli = NULL;
1.2 misho 144: }
145:
1.10 misho 146: /* -------------------------------------------------------------- */
1.2 misho 147:
148: /*
1.7 misho 149: * rpc_cli_openClient() - Connect to RPC Server
150: *
1.1 misho 151: * @ProgID = ProgramID for RPC session request
152: * @ProcID = ProcessID for RPC session request
1.10 misho 153: * @netBuf = Network buffer length (min:512 bytes), if =0 == BUFSIZ (also meaning max RPC packet)
1.1 misho 154: * @csHost = Host name or IP address for bind server
155: * @Port = Port for bind server, if Port == 0 default port is selected
1.13 misho 156: * @proto = Protocol, if == 0 choose SOCK_STREAM
1.1 misho 157: * return: NULL == error or !=NULL connection to RPC server established
158: */
159: rpc_cli_t *
1.13 misho 160: rpc_cli_openClient(u_int ProgID, u_char ProcID, int netBuf, const char *csHost,
161: u_short Port, int proto)
1.1 misho 162: {
163: rpc_cli_t *cli = NULL;
1.13.4.1 misho 164: sockaddr_t sa = E_SOCKADDR_INIT;
1.1 misho 165:
1.13.4.1 misho 166: if (!e_gethostbyname(csHost, Port, &sa))
1.1 misho 167: return NULL;
168: if (!Port)
169: Port = RPC_DEFPORT;
1.13 misho 170: if (!proto)
171: proto = SOCK_STREAM;
1.10 misho 172: if (netBuf < RPC_MIN_BUFSIZ)
1.5 misho 173: netBuf = BUFSIZ;
1.10 misho 174: else
1.13.4.1 misho 175: netBuf = E_ALIGN(netBuf, 2); /* align netBuf length */
1.10 misho 176:
177: #ifdef HAVE_SRANDOMDEV
178: srandomdev();
179: #else
180: time_t tim;
181:
182: srandom((time(&tim) ^ getpid()));
183: #endif
1.1 misho 184:
1.13.4.1 misho 185: cli = e_malloc(sizeof(rpc_cli_t));
1.1 misho 186: if (!cli) {
187: LOGERR;
188: return NULL;
189: } else
190: memset(cli, 0, sizeof(rpc_cli_t));
1.5 misho 191:
1.10 misho 192: /* build session */
1.13.4.1 misho 193: cli->cli_parent = e_malloc(sizeof(rpc_sess_t));
1.1 misho 194: if (!cli->cli_parent) {
195: LOGERR;
1.13.4.1 misho 196: e_free(cli);
1.1 misho 197: return NULL;
198: } else {
199: ((rpc_sess_t*) cli->cli_parent)->sess_version = RPC_VERSION;
200: ((rpc_sess_t*) cli->cli_parent)->sess_program = ProgID;
201: ((rpc_sess_t*) cli->cli_parent)->sess_process = ProcID;
202: }
203:
1.13 misho 204: cli->cli_id = proto;
1.6 misho 205: memcpy(&cli->cli_sa, &sa, sizeof cli->cli_sa);
1.13.4.1 misho 206: AIT_SET_BUFSIZ(&cli->cli_buf, 0, netBuf);
1.4 misho 207:
208: /* connect to RPC server */
1.13 misho 209: cli->cli_sock = socket(cli->cli_sa.sa.sa_family, cli->cli_id, 0);
1.1 misho 210: if (cli->cli_sock == -1) {
211: LOGERR;
1.13 misho 212: goto err;
213: }
214: if (setsockopt(cli->cli_sock, SOL_SOCKET, SO_SNDBUF,
215: &netBuf, sizeof netBuf) == -1) {
216: LOGERR;
217: goto err;
1.5 misho 218: }
1.13 misho 219: if (setsockopt(cli->cli_sock, SOL_SOCKET, SO_RCVBUF,
220: &netBuf, sizeof netBuf) == -1) {
1.1 misho 221: LOGERR;
1.13 misho 222: goto err;
223: }
224: if (cli->cli_id == SOCK_STREAM)
225: if (connect(cli->cli_sock, &cli->cli_sa.sa, cli->cli_sa.sa.sa_len) == -1) {
226: LOGERR;
227: goto err;
228: }
1.1 misho 229:
1.13 misho 230: fcntl(cli->cli_sock, F_SETFL, fcntl(cli->cli_sock, F_GETFL) | O_NONBLOCK);
1.1 misho 231: return cli;
1.13 misho 232: err:
233: AIT_FREE_VAL(&cli->cli_buf);
234: if (cli->cli_sock > 2)
235: close(cli->cli_sock);
1.13.4.1 misho 236: e_free(cli->cli_parent);
237: e_free(cli);
1.13 misho 238: return NULL;
1.1 misho 239: }
240:
241: /*
1.7 misho 242: * rpc_cli_closeClient() - Close connection to RPC server and free resources
243: *
1.1 misho 244: * @cli = RPC Client session
245: * return: none
246: */
247: void
1.10 misho 248: rpc_cli_closeClient(rpc_cli_t ** __restrict cli)
1.1 misho 249: {
1.10 misho 250: if (!cli || !*cli)
1.1 misho 251: return;
252:
1.13 misho 253: if ((*cli)->cli_id == SOCK_STREAM)
254: shutdown((*cli)->cli_sock, SHUT_RDWR);
1.10 misho 255: close((*cli)->cli_sock);
256:
257: AIT_FREE_VAL(&(*cli)->cli_buf);
258:
259: if ((*cli)->cli_parent)
1.13.4.1 misho 260: e_free((*cli)->cli_parent);
1.1 misho 261:
1.13.4.1 misho 262: e_free(*cli);
1.10 misho 263: *cli = NULL;
1.1 misho 264: }
265:
266: /*
1.13.4.4! misho 267: * rpc_pkt_Send() - Send RPC packet
1.7 misho 268: *
1.13.4.4! misho 269: * @sock = Socket
! 270: * @type = Type of socket
! 271: * @sa = Server address
! 272: * @pkt = RPC packet
! 273: * @len = Length of packet
! 274: * return: -1 error or !=-1 sended bytes
1.1 misho 275: */
276: int
1.13.4.4! misho 277: rpc_pkt_Send(int sock, int type, sockaddr_t * __restrict sa, ait_val_t * __restrict pkt, int len)
1.1 misho 278: {
1.9 misho 279: struct pollfd pfd;
1.13.4.4! misho 280: int ret;
! 281: u_char *buf;
1.1 misho 282:
1.13.4.4! misho 283: if (!pkt) {
! 284: rpc_SetErr(EINVAL, "Invalid argument(s)!");
1.1 misho 285: return -1;
1.10 misho 286: } else
1.13.4.4! misho 287: buf = AIT_GET_BUF(pkt);
1.13 misho 288:
1.13.4.4! misho 289: pfd.fd = sock;
1.10 misho 290: pfd.events = POLLOUT;
291: if ((ret = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) < 1 ||
1.9 misho 292: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
1.4 misho 293: if (ret)
1.1 misho 294: LOGERR;
1.4 misho 295: else
1.10 misho 296: rpc_SetErr(ETIMEDOUT, "Timeout, can't send to RPC server");
1.1 misho 297: return -1;
298: }
1.10 misho 299: do {
1.13.4.4! misho 300: if (type == SOCK_STREAM)
! 301: ret = send(sock, buf, len, MSG_NOSIGNAL);
! 302: else if (sa)
! 303: ret = sendto(sock, buf, len, MSG_NOSIGNAL, &sa->sa, sa->sa.sa_len);
! 304: else {
! 305: rpc_SetErr(EINVAL, "Invalid argument(s)!");
! 306: return -1;
! 307: }
1.13 misho 308: if (ret == -1) {
1.10 misho 309: if (errno == EAGAIN)
310: continue;
311: LOGERR;
312: return -1;
1.13.4.4! misho 313: } else if (ret != len) {
1.10 misho 314: rpc_SetErr(EPROCUNAVAIL, "RPC request, should be send %d bytes, "
1.13.4.4! misho 315: "really sended %d bytes", len, ret);
1.10 misho 316: return -1;
317: }
318: } while (0);
319:
1.13.4.4! misho 320: return ret;
! 321: }
! 322:
! 323: /*
! 324: * rpc_pkt_Receive() - Receive RPC packet
! 325: *
! 326: * @sock = Socket
! 327: * @type = Type of socket
! 328: * @sa = Server address
! 329: * @pkt = RPC packet
! 330: * return: -1 error or !=-1 sended bytes
! 331: */
! 332: int
! 333: rpc_pkt_Receive(int sock, int type, sockaddr_t * __restrict sa, ait_val_t * __restrict pkt)
! 334: {
! 335: struct pollfd pfd;
! 336: int ret, len = 0;
! 337: u_char *buf;
! 338: sockaddr_t sa2;
! 339: socklen_t salen;
! 340:
! 341: if (!pkt) {
! 342: rpc_SetErr(EINVAL, "Invalid argument(s)!");
! 343: return -1;
! 344: } else
! 345: buf = AIT_GET_BUF(pkt);
1.10 misho 346:
347: /* reply from RPC server */
1.13.4.4! misho 348: pfd.fd = sock;
1.10 misho 349: pfd.events = POLLIN | POLLPRI;
350: do {
351: if ((ret = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) < 1 ||
352: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
1.13 misho 353: if (ret) {
1.10 misho 354: LOGERR;
1.13 misho 355: } else {
1.13.4.4! misho 356: if (len++ < 7)
1.10 misho 357: continue;
358: else
1.13.4.4! misho 359: rpc_SetErr(ETIMEDOUT,
! 360: "Timeout, no answer from RPC server");
1.10 misho 361: }
1.13 misho 362:
1.10 misho 363: return -1;
364: }
1.13 misho 365:
1.13.4.4! misho 366: memset(buf, 0, AIT_LEN(pkt));
! 367: if (type == SOCK_STREAM)
! 368: ret = recv(sock, buf, AIT_LEN(pkt), 0);
1.13.4.2 misho 369: else {
1.13.4.4! misho 370: memset(&sa2, 0, sizeof sa2);
! 371: salen = sa2.ss.ss_len = sizeof(sockaddr_t);
! 372: ret = recvfrom(sock, buf, AIT_LEN(pkt), 0, &sa2.sa, &salen);
1.13.4.2 misho 373: }
1.13 misho 374: if (ret < 1) {
1.10 misho 375: if (ret) {
376: if (errno == EAGAIN)
377: continue;
378: else
379: LOGERR;
380: }
381: return -1;
382: }
1.13 misho 383:
1.13.4.4! misho 384: /* check for response from known address */
! 385: if (type == SOCK_DGRAM)
! 386: if (e_addrcmp(sa, &sa2, 42)) {
! 387: rpc_SetErr(ERPCMISMATCH,
! 388: "Received RPC response from unknown address");
1.13 misho 389: continue;
390: }
1.10 misho 391: } while (0);
1.7 misho 392: if (ret < sizeof(struct tagRPCCall)) {
1.10 misho 393: rpc_SetErr(ERPCMISMATCH, "Short RPC packet %d bytes", ret);
1.8 misho 394: return -1;
1.7 misho 395: }
1.8 misho 396:
1.13.4.4! misho 397: return ret;
! 398: }
! 399:
! 400: /*
! 401: * rpc_pkt_Request() - Build RPC Request packet
! 402: *
! 403: * @pkt = Packet buffer
! 404: * @sess = RPC session info
! 405: * @tag = Function tag for execution
! 406: * @vars = Function argument array of values, may be NULL
! 407: * @noreply = We not want RPC reply
! 408: * return: -1 error or != -1 prepared bytes into packet
! 409: */
! 410: int
! 411: rpc_pkt_Request(ait_val_t * __restrict pkt, rpc_sess_t * __restrict sess, u_short tag,
! 412: array_t * __restrict vars, int noreply)
! 413: {
! 414: struct tagRPCCall *rpc;
! 415: int ret = 0, len = sizeof(struct tagRPCCall);
! 416: u_char *buf;
! 417:
! 418: if (!pkt || !sess) {
! 419: rpc_SetErr(EINVAL, "Invalid argument(s)!");
! 420: return -1;
! 421: } else
! 422: buf = AIT_GET_BUF(pkt);
! 423:
! 424: /* prepare RPC call */
! 425: rpc = (struct tagRPCCall*) buf;
! 426: rpc_addPktSession(&rpc->call_session, sess);
! 427: rpc->call_tag = htons(tag);
! 428: rpc->call_seq = htons(random() % USHRT_MAX);
! 429: if (!vars)
! 430: rpc->call_argc = 0;
! 431: else
! 432: rpc->call_argc = htons(array_Size(vars));
! 433:
! 434: /* set reply */
! 435: rpc->call_req.flags = noreply ? RPC_NOREPLY : RPC_REPLY;
! 436:
! 437: if (array_Size(vars)) {
! 438: /* marshaling variables */
! 439: ret = ait_vars2buffer(buf + len, AIT_LEN(pkt) - len, vars);
! 440: if (ret == -1) {
! 441: rpc_SetErr(EBADRPC, "Failed to prepare RPC packet values");
! 442: return -1;
! 443: } else
! 444: len += ret;
! 445: }
! 446:
! 447: /* total packet length */
! 448: rpc->call_len = htons(len);
! 449:
! 450: /* calculate CRC */
! 451: rpc->call_crc ^= rpc->call_crc;
! 452: rpc->call_crc = htons(crcFletcher16((u_short*) buf, len / 2));
! 453:
! 454: return len;
! 455: }
! 456:
! 457: /*
! 458: * rpc_pkt_Replay() - Decode RPC Replay packet
! 459: *
! 460: * @pkt = Packet buffer
! 461: * @sess = RPC session info
! 462: * @tag = Function tag
! 463: * @vars = Function argument array of values, may be NULL
! 464: * return: -1 error or != -1 return value from function
! 465: */
! 466: int
! 467: rpc_pkt_Replay(ait_val_t * __restrict pkt, rpc_sess_t * __restrict sess, u_short tag,
! 468: array_t ** __restrict vars)
! 469: {
! 470: struct tagRPCCall *rpc;
! 471: int len;
! 472: u_char *buf;
! 473: uint16_t crc;
! 474:
! 475: if (!pkt || !sess) {
! 476: rpc_SetErr(EINVAL, "Invalid argument(s)!");
! 477: return -1;
! 478: } else
! 479: buf = AIT_GET_BUF(pkt);
! 480:
! 481: rpc = (struct tagRPCCall*) buf;
1.8 misho 482: /* calculate CRC */
483: crc = ntohs(rpc->call_crc);
484: rpc->call_crc ^= rpc->call_crc;
1.10 misho 485: if (crc != crcFletcher16((u_short*) buf, ntohs(rpc->call_len) / 2)) {
1.8 misho 486: rpc_SetErr(ERPCMISMATCH, "Bad CRC RPC packet");
487: return -1;
488: }
489:
1.3 misho 490: /* check RPC packet session info */
1.13.4.4! misho 491: if (rpc_chkPktSession(&rpc->call_session, sess)) {
1.10 misho 492: rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session");
1.8 misho 493: return -1;
1.13.4.4! misho 494: }
1.10 misho 495: if (ntohs(rpc->call_tag) != tag) {
496: rpc_SetErr(ERPCMISMATCH, "Get wrong RPC reply");
1.8 misho 497: return -1;
1.6 misho 498: }
1.10 misho 499: if (ntohl(rpc->call_rep.eno) && ntohl(rpc->call_rep.ret) == -1) {
1.8 misho 500: rpc_SetErr(ntohl(rpc->call_rep.eno), "Server side: retcode=%d #%d %s",
1.7 misho 501: ntohl(rpc->call_rep.ret), ntohl(rpc->call_rep.eno),
502: strerror(ntohl(rpc->call_rep.eno)));
1.8 misho 503: return -1;
1.1 misho 504: }
1.13.4.4! misho 505: len = ntohs(rpc->call_argc) * sizeof(ait_val_t);
! 506: if (len > AIT_LEN(pkt) - sizeof(struct tagRPCCall)) {
! 507: rpc_SetErr(EMSGSIZE, "Reply RPC packet not enough buffer space ...");
! 508: return -1;
! 509: }
! 510: if (len > ntohs(rpc->call_len) - sizeof(struct tagRPCCall)) {
! 511: rpc_SetErr(EMSGSIZE, "Reply RPC packet is too short ...");
1.8 misho 512: return -1;
1.5 misho 513: }
514:
515: /* RPC is OK! Go de-marshaling variables ... */
1.13.4.4! misho 516: if (vars && ntohs(rpc->call_argc)) {
1.12 misho 517: #ifdef CLI_RES_ZCOPY
1.13.4.4! misho 518: *vars = ait_buffer2vars(buf + sizeof(struct tagRPCCall), len,
1.12 misho 519: ntohs(rpc->call_argc), 42);
520: #else
1.13.4.4! misho 521: *vars = ait_buffer2vars(buf + sizeof(struct tagRPCCall), len,
1.7 misho 522: ntohs(rpc->call_argc), 0);
1.12 misho 523: #endif
1.13.4.4! misho 524: if (!*vars) {
1.13.4.1 misho 525: rpc_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
1.1 misho 526: return -1;
1.5 misho 527: }
1.1 misho 528: }
529:
1.13.4.4! misho 530: return ntohl(rpc->call_rep.ret);
! 531: }
! 532:
! 533: /*
! 534: * rpc_cli_execCall() - Execute RPC call
! 535: *
! 536: * @cli = RPC Client session
! 537: * @noreply = We not want RPC reply
! 538: * @tag = Function tag for execution
! 539: * @in_vars = IN function argument array of values, may be NULL
! 540: * @out_vars = OUT returned array of rpc values, if !=NULL must be free after use with ait_freeVars()
! 541: * return: -1 error or != -1 ok result
! 542: */
! 543: int
! 544: rpc_cli_execCall(rpc_cli_t *cli, int noreply, u_short tag,
! 545: array_t * __restrict in_vars, array_t ** __restrict out_vars)
! 546: {
! 547: int ret = 0, wlen;
! 548: u_char *buf;
! 549:
! 550: if (!cli) {
! 551: rpc_SetErr(EINVAL, "Can`t execute call because parameter is null or invalid!");
! 552: return -1;
! 553: } else
! 554: buf = AIT_GET_BUF(&cli->cli_buf);
! 555: if (out_vars)
! 556: *out_vars = NULL;
! 557:
! 558: if ((wlen = rpc_pkt_Request(&cli->cli_buf, cli->cli_parent, tag, in_vars, noreply)) == -1)
! 559: return -1;
! 560:
! 561: if (rpc_pkt_Send(cli->cli_sock, cli->cli_id, &cli->cli_sa, &cli->cli_buf, wlen) == -1)
! 562: return -1;
! 563:
! 564: if (noreply) /* we not want reply */
! 565: return 0;
! 566:
! 567: if (rpc_pkt_Receive(cli->cli_sock, cli->cli_id, &cli->cli_sa, &cli->cli_buf) == -1)
! 568: return -1;
! 569:
! 570: if ((wlen = rpc_pkt_Replay(&cli->cli_buf, cli->cli_parent, tag, out_vars)) == -1)
! 571: return -1;
! 572:
1.10 misho 573: return ret;
574: }
575:
576: /*
1.12 misho 577: * rpc_cli_freeCall() - Free resouce allocated by RPC call
578: *
579: * @out_vars = Returned array with variables from RPC call
580: * return: none
581: */
582: inline void
583: rpc_cli_freeCall(array_t ** __restrict out_vars)
584: {
585: #ifdef CLI_RES_ZCOPY
1.13.4.1 misho 586: array_Destroy(out_vars);
1.12 misho 587: #else
1.13.4.1 misho 588: ait_freeVars(out_vars);
1.12 misho 589: #endif
590: }
591:
592: /*
1.10 misho 593: * rpc_cli_ping() - Ping RPC server
594: *
595: * @cli = connected client
596: * return: -1 error or !=-1 ping seq id
597: */
598: inline int
599: rpc_cli_ping(rpc_cli_t *cli)
600: {
601: int ret = 0;
602: array_t *arr = NULL;
603:
604: if (!cli)
605: return -1;
606:
607: if (rpc_cli_execCall(cli, RPC_REPLY, CALL_SRVPING, NULL, &arr))
608: return -1;
609: else
1.13.4.1 misho 610: ret = AIT_GET_U16(array(arr, 0, ait_val_t*));
1.12 misho 611: rpc_cli_freeCall(&arr);
1.10 misho 612:
1.5 misho 613: return ret;
1.1 misho 614: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>