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