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