Annotation of libaitrpc/src/cli.c, revision 1.16.8.5
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.16.8.5! misho 6: * $Id: cli.c,v 1.16.8.4 2013/08/22 11:55:45 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.14 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.14 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.14 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.14 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.14 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.14 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.14 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.14 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.15 misho 151: * @InstID = InstID for RPC session request
1.10 misho 152: * @netBuf = Network buffer length (min:512 bytes), if =0 == BUFSIZ (also meaning max RPC packet)
1.1 misho 153: * @csHost = Host name or IP address for bind server
154: * @Port = Port for bind server, if Port == 0 default port is selected
1.13 misho 155: * @proto = Protocol, if == 0 choose SOCK_STREAM
1.1 misho 156: * return: NULL == error or !=NULL connection to RPC server established
157: */
158: rpc_cli_t *
1.15 misho 159: rpc_cli_openClient(u_char InstID, int netBuf, const char *csHost, u_short Port, int proto)
1.1 misho 160: {
161: rpc_cli_t *cli = NULL;
1.14 misho 162: sockaddr_t sa = E_SOCKADDR_INIT;
1.1 misho 163:
1.14 misho 164: if (!e_gethostbyname(csHost, Port, &sa))
1.1 misho 165: return NULL;
166: if (!Port)
167: Port = RPC_DEFPORT;
1.13 misho 168: if (!proto)
169: proto = SOCK_STREAM;
1.10 misho 170: if (netBuf < RPC_MIN_BUFSIZ)
1.5 misho 171: netBuf = BUFSIZ;
1.10 misho 172: else
1.14 misho 173: netBuf = E_ALIGN(netBuf, 2); /* align netBuf length */
1.10 misho 174:
1.14 misho 175: cli = e_malloc(sizeof(rpc_cli_t));
1.1 misho 176: if (!cli) {
177: LOGERR;
178: return NULL;
179: } else
180: memset(cli, 0, sizeof(rpc_cli_t));
1.5 misho 181:
1.10 misho 182: /* build session */
1.14 misho 183: cli->cli_parent = e_malloc(sizeof(rpc_sess_t));
1.1 misho 184: if (!cli->cli_parent) {
185: LOGERR;
1.14 misho 186: e_free(cli);
1.1 misho 187: return NULL;
188: } else {
189: ((rpc_sess_t*) cli->cli_parent)->sess_version = RPC_VERSION;
1.15 misho 190: ((rpc_sess_t*) cli->cli_parent)->sess_instance = InstID;
1.1 misho 191: }
192:
1.13 misho 193: cli->cli_id = proto;
1.6 misho 194: memcpy(&cli->cli_sa, &sa, sizeof cli->cli_sa);
1.14 misho 195: AIT_SET_BUFSIZ(&cli->cli_buf, 0, netBuf);
1.4 misho 196:
197: /* connect to RPC server */
1.13 misho 198: cli->cli_sock = socket(cli->cli_sa.sa.sa_family, cli->cli_id, 0);
1.1 misho 199: if (cli->cli_sock == -1) {
200: LOGERR;
1.13 misho 201: goto err;
202: }
203: if (setsockopt(cli->cli_sock, SOL_SOCKET, SO_SNDBUF,
204: &netBuf, sizeof netBuf) == -1) {
205: LOGERR;
206: goto err;
1.5 misho 207: }
1.13 misho 208: if (setsockopt(cli->cli_sock, SOL_SOCKET, SO_RCVBUF,
209: &netBuf, sizeof netBuf) == -1) {
1.1 misho 210: LOGERR;
1.13 misho 211: goto err;
212: }
213: if (cli->cli_id == SOCK_STREAM)
214: if (connect(cli->cli_sock, &cli->cli_sa.sa, cli->cli_sa.sa.sa_len) == -1) {
215: LOGERR;
216: goto err;
217: }
1.1 misho 218:
1.13 misho 219: fcntl(cli->cli_sock, F_SETFL, fcntl(cli->cli_sock, F_GETFL) | O_NONBLOCK);
1.1 misho 220: return cli;
1.13 misho 221: err:
222: AIT_FREE_VAL(&cli->cli_buf);
223: if (cli->cli_sock > 2)
224: close(cli->cli_sock);
1.14 misho 225: e_free(cli->cli_parent);
226: e_free(cli);
1.13 misho 227: return NULL;
1.1 misho 228: }
229:
230: /*
1.7 misho 231: * rpc_cli_closeClient() - Close connection to RPC server and free resources
232: *
1.1 misho 233: * @cli = RPC Client session
234: * return: none
235: */
236: void
1.10 misho 237: rpc_cli_closeClient(rpc_cli_t ** __restrict cli)
1.1 misho 238: {
1.10 misho 239: if (!cli || !*cli)
1.1 misho 240: return;
241:
1.13 misho 242: if ((*cli)->cli_id == SOCK_STREAM)
243: shutdown((*cli)->cli_sock, SHUT_RDWR);
1.10 misho 244: close((*cli)->cli_sock);
245:
246: AIT_FREE_VAL(&(*cli)->cli_buf);
247:
248: if ((*cli)->cli_parent)
1.14 misho 249: e_free((*cli)->cli_parent);
1.1 misho 250:
1.14 misho 251: e_free(*cli);
1.10 misho 252: *cli = NULL;
1.1 misho 253: }
254:
255: /*
1.14 misho 256: * rpc_pkt_Send() - Send RPC packet
1.7 misho 257: *
1.14 misho 258: * @sock = Socket
259: * @type = Type of socket
260: * @sa = Server address
261: * @pkt = RPC packet
262: * @len = Length of packet
263: * return: -1 error or !=-1 sended bytes
1.1 misho 264: */
265: int
1.14 misho 266: rpc_pkt_Send(int sock, int type, sockaddr_t * __restrict sa, ait_val_t * __restrict pkt, int len)
1.1 misho 267: {
1.14 misho 268: struct pollfd pfd;
269: int ret;
1.10 misho 270: u_char *buf;
1.1 misho 271:
1.14 misho 272: if (!pkt) {
273: rpc_SetErr(EINVAL, "Invalid argument(s)!");
1.1 misho 274: return -1;
1.10 misho 275: } else
1.14 misho 276: buf = AIT_GET_BUF(pkt);
1.7 misho 277:
1.14 misho 278: pfd.fd = sock;
1.10 misho 279: pfd.events = POLLOUT;
280: if ((ret = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) < 1 ||
1.9 misho 281: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
1.4 misho 282: if (ret)
1.1 misho 283: LOGERR;
1.4 misho 284: else
1.10 misho 285: rpc_SetErr(ETIMEDOUT, "Timeout, can't send to RPC server");
1.1 misho 286: return -1;
287: }
1.10 misho 288: do {
1.14 misho 289: if (type == SOCK_STREAM)
290: ret = send(sock, buf, len, MSG_NOSIGNAL);
291: else if (sa)
292: ret = sendto(sock, buf, len, MSG_NOSIGNAL, &sa->sa, sa->sa.sa_len);
293: else {
294: rpc_SetErr(EINVAL, "Invalid argument(s)!");
295: return -1;
296: }
1.13 misho 297: if (ret == -1) {
1.10 misho 298: if (errno == EAGAIN)
299: continue;
300: LOGERR;
301: return -1;
1.14 misho 302: } else if (ret != len) {
1.10 misho 303: rpc_SetErr(EPROCUNAVAIL, "RPC request, should be send %d bytes, "
1.14 misho 304: "really sended %d bytes", len, ret);
1.10 misho 305: return -1;
306: }
307: } while (0);
308:
1.14 misho 309: return ret;
310: }
311:
312: /*
313: * rpc_pkt_Receive() - Receive RPC packet
314: *
315: * @sock = Socket
316: * @type = Type of socket
317: * @sa = Server address
318: * @pkt = RPC packet
319: * return: -1 error or !=-1 sended bytes
320: */
321: int
322: rpc_pkt_Receive(int sock, int type, sockaddr_t * __restrict sa, ait_val_t * __restrict pkt)
323: {
324: struct pollfd pfd;
1.16.8.3 misho 325: int ret, estlen, blen = sizeof(struct tagRPCCall), len = 0;
1.14 misho 326: u_char *buf;
327: sockaddr_t sa2;
328: socklen_t salen;
1.16.8.3 misho 329: struct tagRPCCall *rpc;
1.14 misho 330:
331: if (!pkt) {
332: rpc_SetErr(EINVAL, "Invalid argument(s)!");
333: return -1;
334: } else
335: buf = AIT_GET_BUF(pkt);
1.10 misho 336:
337: /* reply from RPC server */
1.14 misho 338: pfd.fd = sock;
1.10 misho 339: pfd.events = POLLIN | POLLPRI;
340: do {
341: if ((ret = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) < 1 ||
342: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
1.13 misho 343: if (ret) {
1.10 misho 344: LOGERR;
1.13 misho 345: } else {
1.14 misho 346: if (len++ < 7)
1.10 misho 347: continue;
348: else
1.14 misho 349: rpc_SetErr(ETIMEDOUT,
350: "Timeout, no answer from RPC server");
1.10 misho 351: }
1.13 misho 352:
1.10 misho 353: return -1;
354: }
1.13 misho 355:
1.16.8.5! misho 356: payload:
1.16.8.3 misho 357: if (type == SOCK_STREAM) {
358: memset(buf, 0, blen);
359: ret = recv(sock, buf, blen, 0);
360: } else {
361: blen = AIT_LEN(pkt);
362: memset(buf, 0, blen);
1.14 misho 363: memset(&sa2, 0, sizeof sa2);
364: salen = sa2.ss.ss_len = sizeof(sockaddr_t);
1.16.8.3 misho 365: ret = recvfrom(sock, buf, blen, 0, &sa2.sa, &salen);
1.14 misho 366: }
1.13 misho 367: if (ret < 1) {
1.10 misho 368: if (ret) {
369: if (errno == EAGAIN)
370: continue;
371: else
372: LOGERR;
373: }
374: return -1;
375: }
1.13 misho 376:
1.14 misho 377: /* check for response from known address */
1.16.8.3 misho 378: if (type == SOCK_DGRAM) {
1.14 misho 379: if (e_addrcmp(sa, &sa2, 42)) {
380: rpc_SetErr(ERPCMISMATCH,
381: "Received RPC response from unknown address");
1.13 misho 382: continue;
383: }
1.16.8.3 misho 384: } else {
385: /* 1st read for RPC header */
386: if (buf == AIT_GET_BUF(pkt)) {
387: if (ret < sizeof(struct tagRPCCall)) {
388: rpc_SetErr(ERPCMISMATCH, "Short RPC packet %d bytes", ret);
389: return -1;
390: }
391:
392: /* calc estimated length */
393: rpc = (struct tagRPCCall*) buf;
1.16.8.4 misho 394: estlen = ntohl(rpc->call_len) + blen;
1.16.8.3 misho 395: if (estlen > AIT_LEN(pkt))
396: AIT_RE_BUF(pkt, estlen);
397: buf = AIT_GET_BUF(pkt) + blen;
398: blen = AIT_LEN(pkt) - blen;
1.16.8.5! misho 399: goto payload;
1.16.8.3 misho 400: } else
401: ret += sizeof(struct tagRPCCall);
402: }
1.10 misho 403: } while (0);
1.16.8.3 misho 404:
1.7 misho 405: if (ret < sizeof(struct tagRPCCall)) {
1.10 misho 406: rpc_SetErr(ERPCMISMATCH, "Short RPC packet %d bytes", ret);
1.8 misho 407: return -1;
1.7 misho 408: }
1.8 misho 409:
1.14 misho 410: return ret;
411: }
412:
413: /*
414: * rpc_pkt_Request() - Build RPC Request packet
415: *
416: * @pkt = Packet buffer
417: * @sess = RPC session info
418: * @tag = Function tag for execution
419: * @vars = Function argument array of values, may be NULL
420: * @noreply = We not want RPC reply
1.15 misho 421: * @nocrc = Without CRC calculation
1.14 misho 422: * return: -1 error or != -1 prepared bytes into packet
423: */
424: int
425: rpc_pkt_Request(ait_val_t * __restrict pkt, rpc_sess_t * __restrict sess, u_short tag,
1.15 misho 426: array_t * __restrict vars, int noreply, int nocrc)
1.14 misho 427: {
428: struct tagRPCCall *rpc;
1.16.8.2 misho 429: int ret = 0, estlen, len = sizeof(struct tagRPCCall);
1.14 misho 430: u_char *buf;
431:
432: if (!pkt || !sess) {
433: rpc_SetErr(EINVAL, "Invalid argument(s)!");
434: return -1;
1.16.8.2 misho 435: }
436:
437: /* calc estimated length */
438: estlen = ait_resideVars(vars) + len;
439: if (estlen > AIT_LEN(pkt))
440: AIT_RE_BUF(pkt, estlen);
441: buf = AIT_GET_BUF(pkt);
1.14 misho 442:
443: /* prepare RPC call */
444: rpc = (struct tagRPCCall*) buf;
445: rpc_addPktSession(&rpc->call_session, sess);
446: rpc->call_tag = htons(tag);
447: if (!vars)
448: rpc->call_argc = 0;
449: else
450: rpc->call_argc = htons(array_Size(vars));
451:
452: /* set reply */
453: rpc->call_req.flags = noreply ? RPC_NOREPLY : RPC_REPLY;
454:
455: if (array_Size(vars)) {
456: /* marshaling variables */
457: ret = ait_vars2buffer(buf + len, AIT_LEN(pkt) - len, vars);
458: if (ret == -1) {
459: rpc_SetErr(EBADRPC, "Failed to prepare RPC packet values");
460: return -1;
461: } else
462: len += ret;
463: }
464:
465: /* total packet length */
1.16.8.4 misho 466: rpc->call_len = htonl(len);
1.14 misho 467:
1.15 misho 468: if (!nocrc) {
469: /* calculate CRC */
470: rpc->call_crc ^= rpc->call_crc;
471: rpc->call_crc = htons(crcFletcher16((u_short*) buf, len / 2));
472: }
1.14 misho 473:
474: return len;
475: }
476:
477: /*
478: * rpc_pkt_Replay() - Decode RPC Replay packet
479: *
480: * @pkt = Packet buffer
481: * @sess = RPC session info
482: * @tag = Function tag
483: * @vars = Function argument array of values, may be NULL
1.15 misho 484: * @nocrc = Without CRC calculation
1.14 misho 485: * return: -1 error or != -1 return value from function
486: */
487: int
488: rpc_pkt_Replay(ait_val_t * __restrict pkt, rpc_sess_t * __restrict sess, u_short tag,
1.15 misho 489: array_t ** __restrict vars, int nocrc)
1.14 misho 490: {
491: struct tagRPCCall *rpc;
492: int len;
493: u_char *buf;
494: uint16_t crc;
495:
496: if (!pkt || !sess) {
497: rpc_SetErr(EINVAL, "Invalid argument(s)!");
498: return -1;
499: } else
500: buf = AIT_GET_BUF(pkt);
501:
502: rpc = (struct tagRPCCall*) buf;
1.15 misho 503: if (!nocrc) {
504: /* calculate CRC */
505: crc = ntohs(rpc->call_crc);
506: rpc->call_crc ^= rpc->call_crc;
1.16.8.4 misho 507: if (crc != crcFletcher16((u_short*) buf, ntohl(rpc->call_len) / 2)) {
1.15 misho 508: rpc_SetErr(ERPCMISMATCH, "Bad CRC RPC packet");
509: return -1;
510: }
1.8 misho 511: }
512:
1.3 misho 513: /* check RPC packet session info */
1.14 misho 514: if (rpc_chkPktSession(&rpc->call_session, sess)) {
1.10 misho 515: rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session");
1.8 misho 516: return -1;
1.14 misho 517: }
1.10 misho 518: if (ntohs(rpc->call_tag) != tag) {
519: rpc_SetErr(ERPCMISMATCH, "Get wrong RPC reply");
1.8 misho 520: return -1;
1.6 misho 521: }
1.10 misho 522: if (ntohl(rpc->call_rep.eno) && ntohl(rpc->call_rep.ret) == -1) {
1.8 misho 523: rpc_SetErr(ntohl(rpc->call_rep.eno), "Server side: retcode=%d #%d %s",
1.7 misho 524: ntohl(rpc->call_rep.ret), ntohl(rpc->call_rep.eno),
525: strerror(ntohl(rpc->call_rep.eno)));
1.8 misho 526: return -1;
1.1 misho 527: }
1.14 misho 528: len = ntohs(rpc->call_argc) * sizeof(ait_val_t);
529: if (len > AIT_LEN(pkt) - sizeof(struct tagRPCCall)) {
530: rpc_SetErr(EMSGSIZE, "Reply RPC packet not enough buffer space ...");
531: return -1;
532: }
1.16.8.4 misho 533: if (len > ntohl(rpc->call_len) - sizeof(struct tagRPCCall)) {
1.14 misho 534: rpc_SetErr(EMSGSIZE, "Reply RPC packet is too short ...");
1.8 misho 535: return -1;
1.5 misho 536: }
537:
538: /* RPC is OK! Go de-marshaling variables ... */
1.14 misho 539: if (vars && ntohs(rpc->call_argc)) {
1.12 misho 540: #ifdef CLI_RES_ZCOPY
1.14 misho 541: *vars = ait_buffer2vars(buf + sizeof(struct tagRPCCall), len,
1.12 misho 542: ntohs(rpc->call_argc), 42);
543: #else
1.14 misho 544: *vars = ait_buffer2vars(buf + sizeof(struct tagRPCCall), len,
1.7 misho 545: ntohs(rpc->call_argc), 0);
1.12 misho 546: #endif
1.14 misho 547: if (!*vars) {
548: rpc_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
1.1 misho 549: return -1;
1.5 misho 550: }
1.1 misho 551: }
552:
1.14 misho 553: return ntohl(rpc->call_rep.ret);
554: }
555:
556: /*
557: * rpc_cli_execCall() - Execute RPC call
558: *
559: * @cli = RPC Client session
560: * @noreply = We not want RPC reply
561: * @tag = Function tag for execution
562: * @in_vars = IN function argument array of values, may be NULL
563: * @out_vars = OUT returned array of rpc values, if !=NULL must be free after use with ait_freeVars()
564: * return: -1 error or != -1 ok result
565: */
566: int
567: rpc_cli_execCall(rpc_cli_t *cli, int noreply, u_short tag,
568: array_t * __restrict in_vars, array_t ** __restrict out_vars)
569: {
1.15 misho 570: int type = 0, wlen;
1.14 misho 571: u_char *buf;
572:
573: if (!cli) {
574: rpc_SetErr(EINVAL, "Can`t execute call because parameter is null or invalid!");
575: return -1;
1.15 misho 576: } else {
577: if (cli->cli_id == SOCK_STREAM)
578: type = cli->cli_id;
1.14 misho 579: buf = AIT_GET_BUF(&cli->cli_buf);
1.15 misho 580: }
1.14 misho 581: if (out_vars)
582: *out_vars = NULL;
583:
1.15 misho 584: if ((wlen = rpc_pkt_Request(&cli->cli_buf, cli->cli_parent, tag, in_vars, noreply, type)) == -1)
1.14 misho 585: return -1;
586:
587: if (rpc_pkt_Send(cli->cli_sock, cli->cli_id, &cli->cli_sa, &cli->cli_buf, wlen) == -1)
588: return -1;
589:
590: if (noreply) /* we not want reply */
591: return 0;
592:
593: if (rpc_pkt_Receive(cli->cli_sock, cli->cli_id, &cli->cli_sa, &cli->cli_buf) == -1)
594: return -1;
595:
1.15 misho 596: if ((wlen = rpc_pkt_Replay(&cli->cli_buf, cli->cli_parent, tag, out_vars, type)) == -1)
1.14 misho 597: return -1;
598:
1.15 misho 599: return 0;
1.10 misho 600: }
601:
602: /*
1.12 misho 603: * rpc_cli_freeCall() - Free resouce allocated by RPC call
604: *
605: * @out_vars = Returned array with variables from RPC call
606: * return: none
607: */
1.16 misho 608: void
1.12 misho 609: rpc_cli_freeCall(array_t ** __restrict out_vars)
610: {
611: #ifdef CLI_RES_ZCOPY
1.14 misho 612: array_Destroy(out_vars);
1.12 misho 613: #else
1.14 misho 614: ait_freeVars(out_vars);
1.12 misho 615: #endif
616: }
617:
618: /*
1.10 misho 619: * rpc_cli_ping() - Ping RPC server
620: *
621: * @cli = connected client
622: * return: -1 error or !=-1 ping seq id
623: */
1.16 misho 624: int
1.10 misho 625: rpc_cli_ping(rpc_cli_t *cli)
626: {
627: int ret = 0;
628: array_t *arr = NULL;
629:
630: if (!cli)
631: return -1;
632:
633: if (rpc_cli_execCall(cli, RPC_REPLY, CALL_SRVPING, NULL, &arr))
634: return -1;
635: else
1.14 misho 636: ret = AIT_GET_U16(array(arr, 0, ait_val_t*));
1.12 misho 637: rpc_cli_freeCall(&arr);
1.10 misho 638:
1.5 misho 639: return ret;
1.1 misho 640: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>