Annotation of libaitrpc/src/cli.c, revision 1.25.2.3
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.25.2.3! misho 6: * $Id: cli.c,v 1.25.2.2 2015/06/28 21:52:26 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.24 misho 15: Copyright 2004 - 2015
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.25 misho 164: if (proto < 0 || proto > SOCK_RAW) {
165: rpc_SetErr(EINVAL, "Invalid parameters can`t open RPC client");
166: return NULL;
167: }
168:
169: srandom(time(NULL) ^ getpid());
170:
171: if (!Port && proto < SOCK_RAW)
172: Port = RPC_DEFPORT;
1.14 misho 173: if (!e_gethostbyname(csHost, Port, &sa))
1.1 misho 174: return NULL;
1.13 misho 175: if (!proto)
176: proto = SOCK_STREAM;
1.10 misho 177: if (netBuf < RPC_MIN_BUFSIZ)
1.5 misho 178: netBuf = BUFSIZ;
1.10 misho 179: else
1.14 misho 180: netBuf = E_ALIGN(netBuf, 2); /* align netBuf length */
1.10 misho 181:
1.14 misho 182: cli = e_malloc(sizeof(rpc_cli_t));
1.1 misho 183: if (!cli) {
184: LOGERR;
185: return NULL;
186: } else
187: memset(cli, 0, sizeof(rpc_cli_t));
1.5 misho 188:
1.10 misho 189: /* build session */
1.14 misho 190: cli->cli_parent = e_malloc(sizeof(rpc_sess_t));
1.1 misho 191: if (!cli->cli_parent) {
192: LOGERR;
1.14 misho 193: e_free(cli);
1.1 misho 194: return NULL;
195: } else {
196: ((rpc_sess_t*) cli->cli_parent)->sess_version = RPC_VERSION;
1.15 misho 197: ((rpc_sess_t*) cli->cli_parent)->sess_instance = InstID;
1.1 misho 198: }
199:
1.13 misho 200: cli->cli_id = proto;
1.6 misho 201: memcpy(&cli->cli_sa, &sa, sizeof cli->cli_sa);
1.14 misho 202: AIT_SET_BUFSIZ(&cli->cli_buf, 0, netBuf);
1.4 misho 203:
204: /* connect to RPC server */
1.25 misho 205: cli->cli_sock = socket(cli->cli_sa.sa.sa_family, cli->cli_id,
206: cli->cli_id == SOCK_RAW ? IPPROTO_ERPC : 0);
1.1 misho 207: if (cli->cli_sock == -1) {
208: LOGERR;
1.13 misho 209: goto err;
210: }
211: if (setsockopt(cli->cli_sock, SOL_SOCKET, SO_SNDBUF,
212: &netBuf, sizeof netBuf) == -1) {
213: LOGERR;
214: goto err;
1.5 misho 215: }
1.13 misho 216: if (setsockopt(cli->cli_sock, SOL_SOCKET, SO_RCVBUF,
217: &netBuf, sizeof netBuf) == -1) {
1.1 misho 218: LOGERR;
1.13 misho 219: goto err;
220: }
221: if (cli->cli_id == SOCK_STREAM)
222: if (connect(cli->cli_sock, &cli->cli_sa.sa, cli->cli_sa.sa.sa_len) == -1) {
223: LOGERR;
224: goto err;
225: }
1.25.2.2 misho 226: if (cli->cli_id == SOCK_DGRAM) {
227: sockaddr_t sa2;
228:
229: if (!e_gethostbyname(csHost, 0, &sa2))
230: goto err;
231: if (bind(cli->cli_sock, &sa2.sa, sa2.sa.sa_len) == -1) {
232: LOGERR;
233: goto err;
234: }
235: }
1.1 misho 236:
1.13 misho 237: fcntl(cli->cli_sock, F_SETFL, fcntl(cli->cli_sock, F_GETFL) | O_NONBLOCK);
1.1 misho 238: return cli;
1.13 misho 239: err:
240: AIT_FREE_VAL(&cli->cli_buf);
241: if (cli->cli_sock > 2)
242: close(cli->cli_sock);
1.14 misho 243: e_free(cli->cli_parent);
244: e_free(cli);
1.13 misho 245: return NULL;
1.1 misho 246: }
247:
248: /*
1.21 misho 249: * rpc_cli_reconnectClient() - Reconnecting client to RPC server
250: *
251: * @cli = RPC Client session
252: * return: -1 error or 0 ok
253: */
254: int
255: rpc_cli_reconnectClient(rpc_cli_t * __restrict cli)
256: {
257: int netBuf;
258:
259: if (!cli)
260: return -1;
261: else
262: netBuf = AIT_LEN(&cli->cli_buf);
263:
1.25.2.3! misho 264: if (cli->cli_id == SOCK_STREAM)
! 265: shutdown(cli->cli_sock, SHUT_RDWR);
! 266: if (cli->cli_id == SOCK_DGRAM && cli->cli_sa.sa.sa_family == AF_LOCAL) {
! 267: sockaddr_t sa2 = E_SOCKADDR_INIT;
! 268: socklen_t salen;
! 269:
! 270: sa2.sa.sa_len = salen = sizeof sa2;
! 271: if (!getsockname(cli->cli_sock, &sa2.sa, &salen))
! 272: unlink(sa2.sun.sun_path);
! 273: }
1.21 misho 274: close(cli->cli_sock);
275:
1.25 misho 276: srandom(time(NULL) ^ getpid());
277:
1.21 misho 278: cli->cli_sock = socket(cli->cli_sa.sa.sa_family, cli->cli_id, 0);
279: if (cli->cli_sock == -1) {
280: LOGERR;
281: return -1;
282: }
283: if (setsockopt(cli->cli_sock, SOL_SOCKET, SO_SNDBUF,
284: &netBuf, sizeof netBuf) == -1) {
285: LOGERR;
286: close(cli->cli_sock);
287: return -1;
288: }
289: if (setsockopt(cli->cli_sock, SOL_SOCKET, SO_RCVBUF,
290: &netBuf, sizeof netBuf) == -1) {
291: LOGERR;
292: close(cli->cli_sock);
293: return -1;
294: }
295: if (cli->cli_id == SOCK_STREAM)
296: if (connect(cli->cli_sock, &cli->cli_sa.sa, cli->cli_sa.sa.sa_len) == -1) {
297: LOGERR;
298: close(cli->cli_sock);
299: return -1;
300: }
301:
302: fcntl(cli->cli_sock, F_SETFL, fcntl(cli->cli_sock, F_GETFL) | O_NONBLOCK);
303: return 0;
304: }
305:
306: /*
1.7 misho 307: * rpc_cli_closeClient() - Close connection to RPC server and free resources
308: *
1.1 misho 309: * @cli = RPC Client session
310: * return: none
311: */
312: void
1.10 misho 313: rpc_cli_closeClient(rpc_cli_t ** __restrict cli)
1.1 misho 314: {
1.23 misho 315: if (!cli || !*cli || (*cli)->cli_id == SOCK_BPF)
1.1 misho 316: return;
317:
1.13 misho 318: if ((*cli)->cli_id == SOCK_STREAM)
319: shutdown((*cli)->cli_sock, SHUT_RDWR);
1.25.2.3! misho 320: if ((*cli)->cli_id == SOCK_DGRAM && (*cli)->cli_sa.sa.sa_family == AF_LOCAL) {
! 321: sockaddr_t sa2 = E_SOCKADDR_INIT;
! 322: socklen_t salen;
! 323:
! 324: sa2.sa.sa_len = salen = sizeof sa2;
! 325: if (!getsockname((*cli)->cli_sock, &sa2.sa, &salen))
! 326: unlink(sa2.sun.sun_path);
! 327: }
1.10 misho 328: close((*cli)->cli_sock);
329:
330: AIT_FREE_VAL(&(*cli)->cli_buf);
331:
332: if ((*cli)->cli_parent)
1.14 misho 333: e_free((*cli)->cli_parent);
1.1 misho 334:
1.14 misho 335: e_free(*cli);
1.10 misho 336: *cli = NULL;
1.1 misho 337: }
338:
339: /*
1.14 misho 340: * rpc_pkt_Send() - Send RPC packet
1.7 misho 341: *
1.14 misho 342: * @sock = Socket
343: * @type = Type of socket
344: * @sa = Server address
345: * @pkt = RPC packet
346: * @len = Length of packet
1.21 misho 347: * return: -1 error, 0 EOF or >0 sended bytes
1.1 misho 348: */
349: int
1.14 misho 350: rpc_pkt_Send(int sock, int type, sockaddr_t * __restrict sa, ait_val_t * __restrict pkt, int len)
1.1 misho 351: {
1.14 misho 352: if (!pkt) {
353: rpc_SetErr(EINVAL, "Invalid argument(s)!");
1.1 misho 354: return -1;
1.25 misho 355: }
1.7 misho 356:
1.25 misho 357: return rpc_Write(sock, type, MSG_NOSIGNAL, sa, pkt, len);
1.14 misho 358: }
359:
360: /*
361: * rpc_pkt_Receive() - Receive RPC packet
362: *
363: * @sock = Socket
364: * @type = Type of socket
365: * @sa = Server address
366: * @pkt = RPC packet
1.25 misho 367: * @seq = Signed packet with seq.no
1.21 misho 368: * return: -1 error, 0 EOF or >0 received bytes
1.14 misho 369: */
370: int
1.25 misho 371: rpc_pkt_Receive(int sock, int type, sockaddr_t * __restrict sa, ait_val_t * __restrict pkt, int seq)
1.14 misho 372: {
1.25 misho 373: int ret;
1.17 misho 374: struct tagRPCCall *rpc;
1.14 misho 375:
376: if (!pkt) {
377: rpc_SetErr(EINVAL, "Invalid argument(s)!");
378: return -1;
1.20 misho 379: }
1.10 misho 380:
381: /* reply from RPC server */
382: do {
1.25 misho 383: if (type == SOCK_STREAM || type == SOCK_EXT)
384: ret = rpc_Read(sock, type, 0, NULL, pkt);
385: else
386: ret = rpc_Read(sock, type, 0, sa, pkt);
387:
388: if (ret > 0) {
389: rpc = (struct tagRPCCall*) AIT_GET_BUF(pkt);
1.25.2.1 misho 390: if (ret < ntohl(rpc->call_len))
391: continue;
1.25 misho 392:
393: /* check for loop request */
394: if (!(rpc->call_io & RPC_ACK))
395: continue;
396: /* check for signed request */
397: if (seq != ntohl(rpc->call_seq))
398: continue;
1.17 misho 399: }
1.25 misho 400: } while (ret < 1);
1.8 misho 401:
1.14 misho 402: return ret;
403: }
404:
405: /*
406: * rpc_pkt_Request() - Build RPC Request packet
407: *
408: * @pkt = Packet buffer
409: * @sess = RPC session info
410: * @tag = Function tag for execution
411: * @vars = Function argument array of values, may be NULL
412: * @noreply = We not want RPC reply
1.15 misho 413: * @nocrc = Without CRC calculation
1.25 misho 414: * @seq = Sign packet with seq.no
1.14 misho 415: * return: -1 error or != -1 prepared bytes into packet
416: */
417: int
418: rpc_pkt_Request(ait_val_t * __restrict pkt, rpc_sess_t * __restrict sess, u_short tag,
1.25 misho 419: array_t * __restrict vars, int noreply, int nocrc, int seq)
1.14 misho 420: {
421: struct tagRPCCall *rpc;
1.17 misho 422: int ret = 0, estlen, len = sizeof(struct tagRPCCall);
1.14 misho 423: u_char *buf;
424:
425: if (!pkt || !sess) {
426: rpc_SetErr(EINVAL, "Invalid argument(s)!");
427: return -1;
1.17 misho 428: }
429:
430: /* calc estimated length */
431: estlen = ait_resideVars(vars) + len;
432: if (estlen > AIT_LEN(pkt))
433: AIT_RE_BUF(pkt, estlen);
434: buf = AIT_GET_BUF(pkt);
1.14 misho 435:
436: /* prepare RPC call */
437: rpc = (struct tagRPCCall*) buf;
438: rpc_addPktSession(&rpc->call_session, sess);
439: rpc->call_tag = htons(tag);
440: if (!vars)
441: rpc->call_argc = 0;
442: else
1.24 misho 443: rpc->call_argc = (u_char) array_Size(vars);
1.14 misho 444:
445: /* set reply */
1.19 misho 446: rpc->call_req.flags = (uint64_t) htonl(noreply ? RPC_NOREPLY : RPC_REPLY);
1.14 misho 447:
448: if (array_Size(vars)) {
449: /* marshaling variables */
450: ret = ait_vars2buffer(buf + len, AIT_LEN(pkt) - len, vars);
451: if (ret == -1) {
452: rpc_SetErr(EBADRPC, "Failed to prepare RPC packet values");
453: return -1;
454: } else
455: len += ret;
456: }
457:
458: /* total packet length */
1.17 misho 459: rpc->call_len = htonl(len);
1.24 misho 460: rpc->call_io = RPC_REQ;
1.14 misho 461:
1.25 misho 462: /* sign packet */
463: rpc->call_seq = htonl(seq);
464:
1.15 misho 465: if (!nocrc) {
466: /* calculate CRC */
467: rpc->call_crc ^= rpc->call_crc;
468: rpc->call_crc = htons(crcFletcher16((u_short*) buf, len / 2));
469: }
1.14 misho 470:
471: return len;
472: }
473:
474: /*
475: * rpc_pkt_Replay() - Decode RPC Replay packet
476: *
477: * @pkt = Packet buffer
1.23 misho 478: * @sess = RPC session info, if =NULL don't check session
1.25 misho 479: * @tag = Function tag, if =CALL_TAG_MAX don't check tag
1.14 misho 480: * @vars = Function argument array of values, may be NULL
1.15 misho 481: * @nocrc = Without CRC calculation
1.14 misho 482: * return: -1 error or != -1 return value from function
483: */
484: int
485: rpc_pkt_Replay(ait_val_t * __restrict pkt, rpc_sess_t * __restrict sess, u_short tag,
1.15 misho 486: array_t ** __restrict vars, int nocrc)
1.14 misho 487: {
488: struct tagRPCCall *rpc;
489: int len;
490: u_char *buf;
491: uint16_t crc;
492:
1.23 misho 493: if (!pkt) {
1.14 misho 494: rpc_SetErr(EINVAL, "Invalid argument(s)!");
495: return -1;
496: } else
497: buf = AIT_GET_BUF(pkt);
498:
499: rpc = (struct tagRPCCall*) buf;
1.15 misho 500: if (!nocrc) {
501: /* calculate CRC */
502: crc = ntohs(rpc->call_crc);
503: rpc->call_crc ^= rpc->call_crc;
1.17 misho 504: if (crc != crcFletcher16((u_short*) buf, ntohl(rpc->call_len) / 2)) {
1.15 misho 505: rpc_SetErr(ERPCMISMATCH, "Bad CRC RPC packet");
506: return -1;
507: }
1.8 misho 508: }
509:
1.3 misho 510: /* check RPC packet session info */
1.23 misho 511: if (sess && rpc_chkPktSession(&rpc->call_session, sess)) {
1.10 misho 512: rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session");
1.8 misho 513: return -1;
1.14 misho 514: }
1.25 misho 515: if (tag != CALL_TAG_MAX && ntohs(rpc->call_tag) != tag) {
1.10 misho 516: rpc_SetErr(ERPCMISMATCH, "Get wrong RPC reply");
1.8 misho 517: return -1;
1.6 misho 518: }
1.10 misho 519: if (ntohl(rpc->call_rep.eno) && ntohl(rpc->call_rep.ret) == -1) {
1.8 misho 520: rpc_SetErr(ntohl(rpc->call_rep.eno), "Server side: retcode=%d #%d %s",
1.7 misho 521: ntohl(rpc->call_rep.ret), ntohl(rpc->call_rep.eno),
522: strerror(ntohl(rpc->call_rep.eno)));
1.8 misho 523: return -1;
1.1 misho 524: }
1.24 misho 525: len = rpc->call_argc * sizeof(ait_val_t);
1.14 misho 526: if (len > AIT_LEN(pkt) - sizeof(struct tagRPCCall)) {
527: rpc_SetErr(EMSGSIZE, "Reply RPC packet not enough buffer space ...");
528: return -1;
529: }
1.17 misho 530: if (len > ntohl(rpc->call_len) - sizeof(struct tagRPCCall)) {
1.14 misho 531: rpc_SetErr(EMSGSIZE, "Reply RPC packet is too short ...");
1.8 misho 532: return -1;
1.5 misho 533: }
534:
535: /* RPC is OK! Go de-marshaling variables ... */
1.24 misho 536: if (vars && rpc->call_argc) {
1.12 misho 537: #ifdef CLI_RES_ZCOPY
1.14 misho 538: *vars = ait_buffer2vars(buf + sizeof(struct tagRPCCall), len,
1.24 misho 539: rpc->call_argc, 42);
1.12 misho 540: #else
1.14 misho 541: *vars = ait_buffer2vars(buf + sizeof(struct tagRPCCall), len,
1.24 misho 542: rpc->call_argc, 0);
1.12 misho 543: #endif
1.14 misho 544: if (!*vars) {
545: rpc_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
1.1 misho 546: return -1;
1.5 misho 547: }
1.1 misho 548: }
549:
1.14 misho 550: return ntohl(rpc->call_rep.ret);
551: }
552:
553: /*
554: * rpc_cli_execCall() - Execute RPC call
555: *
556: * @cli = RPC Client session
557: * @noreply = We not want RPC reply
558: * @tag = Function tag for execution
559: * @in_vars = IN function argument array of values, may be NULL
560: * @out_vars = OUT returned array of rpc values, if !=NULL must be free after use with ait_freeVars()
1.21 misho 561: * return: -1 error, 0 ok result or 1 closed rpc connection
1.14 misho 562: */
563: int
564: rpc_cli_execCall(rpc_cli_t *cli, int noreply, u_short tag,
565: array_t * __restrict in_vars, array_t ** __restrict out_vars)
566: {
1.15 misho 567: int type = 0, wlen;
1.14 misho 568: u_char *buf;
1.25 misho 569: u_int seq = 0;
1.14 misho 570:
571: if (!cli) {
572: rpc_SetErr(EINVAL, "Can`t execute call because parameter is null or invalid!");
573: return -1;
1.15 misho 574: } else {
1.24 misho 575: if (cli->cli_id == SOCK_STREAM || cli->cli_id == SOCK_EXT)
1.15 misho 576: type = cli->cli_id;
1.25 misho 577: else
578: seq = random(); /* sign package */
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.25 misho 584: if ((wlen = rpc_pkt_Request(&cli->cli_buf, cli->cli_parent, tag, in_vars, noreply, type, seq)) == -1)
1.14 misho 585: return -1;
586:
1.21 misho 587: if ((wlen = rpc_pkt_Send(cli->cli_sock, cli->cli_id, &cli->cli_sa, &cli->cli_buf, wlen)) == -1)
1.14 misho 588: return -1;
1.21 misho 589: if (!wlen) /* closed rpc connection */
590: return 1;
1.14 misho 591:
592: if (noreply) /* we not want reply */
593: return 0;
594:
1.25 misho 595: if ((wlen = rpc_pkt_Receive(cli->cli_sock, cli->cli_id, &cli->cli_sa, &cli->cli_buf, seq)) == -1)
1.14 misho 596: return -1;
1.21 misho 597: if (!wlen) /* closed rpc connection */
598: return 1;
1.14 misho 599:
1.15 misho 600: if ((wlen = rpc_pkt_Replay(&cli->cli_buf, cli->cli_parent, tag, out_vars, type)) == -1)
1.14 misho 601: return -1;
602:
1.15 misho 603: return 0;
1.10 misho 604: }
605:
606: /*
1.12 misho 607: * rpc_cli_freeCall() - Free resouce allocated by RPC call
608: *
609: * @out_vars = Returned array with variables from RPC call
610: * return: none
611: */
1.16 misho 612: void
1.12 misho 613: rpc_cli_freeCall(array_t ** __restrict out_vars)
614: {
615: #ifdef CLI_RES_ZCOPY
1.14 misho 616: array_Destroy(out_vars);
1.12 misho 617: #else
1.14 misho 618: ait_freeVars(out_vars);
1.12 misho 619: #endif
620: }
621:
622: /*
1.10 misho 623: * rpc_cli_ping() - Ping RPC server
624: *
625: * @cli = connected client
626: * return: -1 error or !=-1 ping seq id
627: */
1.16 misho 628: int
1.10 misho 629: rpc_cli_ping(rpc_cli_t *cli)
630: {
631: int ret = 0;
632: array_t *arr = NULL;
633:
634: if (!cli)
635: return -1;
636:
637: if (rpc_cli_execCall(cli, RPC_REPLY, CALL_SRVPING, NULL, &arr))
638: return -1;
639: else
1.14 misho 640: ret = AIT_GET_U16(array(arr, 0, ait_val_t*));
1.12 misho 641: rpc_cli_freeCall(&arr);
1.10 misho 642:
1.5 misho 643: return ret;
1.1 misho 644: }
1.23 misho 645:
646:
647: /*
648: * rpc_cli_openClient2() - Connect to layer2 RPC Server
649: *
650: * @InstID = InstID for RPC session request
651: * @netBuf = Network buffer length (min:512 bytes), if =0 == BUFSIZ (also meaning max RPC packet)
652: * @csIface = Interface name for bind client, if NULL first interface on host
653: * @csHost = Host ethernet address
654: * return: NULL == error or !=NULL connection to RPC server established
655: */
656: rpc_cli_t *
657: rpc_cli_openClient2(u_char InstID, int netBuf, const char *csIface, const char *csHost)
658: {
659: rpc_cli_t *cli = NULL;
660: sockaddr_t sa = E_SOCKADDR_INIT, sal = E_SOCKADDR_INIT;
661: char szIface[64], szStr[STRSIZ];
662: register int i;
663: struct ifreq ifr;
664: int n = 1;
665: struct bpf_insn insns[] = {
666: BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12),
667: BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, RPC_DEFPORT, 0, 1),
668: BPF_STMT(BPF_RET + BPF_K, -1),
669: BPF_STMT(BPF_RET + BPF_K, 0),
670: };
671: struct bpf_program fcode = {
672: .bf_len = sizeof(insns) / sizeof(struct bpf_insn),
673: .bf_insns = insns
674: };
675:
676: if (!csHost || !e_getlinkbyname(csHost, &sa))
677: return NULL;
678: if (!csIface) {
679: if (e_get1stiface(szIface, sizeof szIface))
680: return NULL;
681: } else
682: strlcpy(szIface, csIface, sizeof szIface);
683: if (!e_getifacebyname(szIface, &sal))
684: return NULL;
685:
686: cli = e_malloc(sizeof(rpc_cli_t));
687: if (!cli) {
688: LOGERR;
689: return NULL;
690: } else
691: memset(cli, 0, sizeof(rpc_cli_t));
692:
693: /* build session */
694: cli->cli_parent = e_malloc(sizeof(rpc_sess_t));
695: if (!cli->cli_parent) {
696: LOGERR;
697: e_free(cli);
698: return NULL;
699: } else {
700: ((rpc_sess_t*) cli->cli_parent)->sess_version = RPC_VERSION;
701: ((rpc_sess_t*) cli->cli_parent)->sess_instance = InstID;
702: }
703:
704: cli->cli_id = SOCK_BPF;
705: memcpy(&cli->cli_sa, &sa, sizeof cli->cli_sa);
706:
707: /* connect to RPC server */
708: for (i = 0; i < 10; i++) {
709: memset(szStr, 0, sizeof szStr);
710: snprintf(szStr, sizeof szStr, "/dev/bpf%d", i);
711: cli->cli_sock = open(szStr, O_RDWR);
712: if (cli->cli_sock > STDERR_FILENO)
713: break;
714: }
715: if (cli->cli_sock < 3) {
716: LOGERR;
717: e_free(cli->cli_parent);
718: e_free(cli);
719: return NULL;
720: }
721:
722: if (ioctl(cli->cli_sock, BIOCIMMEDIATE, &n) == -1) {
723: LOGERR;
724: goto err;
725: }
726: if (ioctl(cli->cli_sock, BIOCSETF, &fcode) == -1) {
727: LOGERR;
728: goto err;
729: }
730: n = (netBuf < RPC_MIN_BUFSIZ) ? getpagesize() : E_ALIGN(netBuf, 2);
731: if (ioctl(cli->cli_sock, BIOCSBLEN, &n) == -1) {
732: LOGERR;
733: goto err;
734: } else
735: AIT_SET_BUFSIZ(&cli->cli_buf, 0, n);
736:
737: memset(&ifr, 0, sizeof ifr);
738: strlcpy(ifr.ifr_name, szIface, sizeof ifr.ifr_name);
739: if (ioctl(cli->cli_sock, BIOCSETIF, &ifr) == -1) {
740: LOGERR;
741: goto err;
742: } else
743: fcntl(cli->cli_sock, F_SETFL,
744: fcntl(cli->cli_sock, F_GETFL) | O_NONBLOCK);
745:
746: return cli;
747: err:
748: AIT_FREE_VAL(&cli->cli_buf);
749: if (cli->cli_sock > 2)
750: close(cli->cli_sock);
751: e_free(cli->cli_parent);
752: e_free(cli);
753: return NULL;
754: }
755:
756: /*
757: * rpc_cli_closeClient2() - Close layer2 connection to RPC server and free resources
758: *
759: * @cli = RPC Client session
760: * return: none
761: */
762: void
763: rpc_cli_closeClient2(rpc_cli_t ** __restrict cli)
764: {
765: if (!cli || !*cli || (*cli)->cli_id != SOCK_BPF)
766: return;
767:
768: close((*cli)->cli_sock);
769:
770: AIT_FREE_VAL(&(*cli)->cli_buf);
771:
772: if ((*cli)->cli_parent)
773: e_free((*cli)->cli_parent);
774:
775: e_free(*cli);
776: *cli = NULL;
777: }
1.24 misho 778:
779: /*
780: * rpc_cli_openClientExt() - Connect to pipe RPC Server
781: *
782: * @InstID = InstID for RPC session request
783: * @netBuf = Network buffer length (min:512 bytes), if =0 == BUFSIZ (also meaning max RPC packet)
784: * @fd = File descriptor
785: * return: NULL == error or !=NULL connection to RPC server established
786: */
787: rpc_cli_t *
788: rpc_cli_openClientExt(u_char InstID, int netBuf, int fd)
789: {
790: rpc_cli_t *cli = NULL;
791: int n;
792:
793: cli = e_malloc(sizeof(rpc_cli_t));
794: if (!cli) {
795: LOGERR;
796: return NULL;
797: } else
798: memset(cli, 0, sizeof(rpc_cli_t));
799:
800: /* build session */
801: cli->cli_parent = e_malloc(sizeof(rpc_sess_t));
802: if (!cli->cli_parent) {
803: LOGERR;
804: e_free(cli);
805: return NULL;
806: } else {
807: ((rpc_sess_t*) cli->cli_parent)->sess_version = RPC_VERSION;
808: ((rpc_sess_t*) cli->cli_parent)->sess_instance = InstID;
809: }
810:
811: cli->cli_id = SOCK_EXT;
812: cli->cli_sock = fd;
813:
814: n = (netBuf < RPC_MIN_BUFSIZ) ? getpagesize() : E_ALIGN(netBuf, 2);
815: AIT_SET_BUFSIZ(&cli->cli_buf, 0, n);
816:
817: fcntl(cli->cli_sock, F_SETFL,
818: fcntl(cli->cli_sock, F_GETFL) | O_NONBLOCK);
819:
820: return cli;
821: }
822:
823: /*
824: * rpc_cli_closeClientExt() - Close pipe connection to RPC server and free resources
825: *
826: * @cli = RPC Client session
827: * return: none
828: */
829: void
830: rpc_cli_closeClientExt(rpc_cli_t ** __restrict cli)
831: {
832: if (!cli || !*cli || (*cli)->cli_id != SOCK_EXT)
833: return;
834:
835: AIT_FREE_VAL(&(*cli)->cli_buf);
836:
837: if ((*cli)->cli_parent)
838: e_free((*cli)->cli_parent);
839:
840: e_free(*cli);
841: *cli = NULL;
842: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>