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