1: /*************************************************************************
2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
3: * by Michael Pounov <misho@openbsd-bg.org>
4: *
5: * $Author: misho $
6: * $Id: cli.c,v 1.30 2024/10/29 01:33:16 misho Exp $
7: *
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:
15: Copyright 2004 - 2024
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: */
46: #include "global.h"
47:
48:
49: /*
50: * rpc_cli_openBLOBClient() - Connect to BLOB Server
51: *
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;
60: int n;
61:
62: if (!rpccli) {
63: rpc_SetErr(EINVAL, "Invalid parameters can`t connect to BLOB server");
64: return NULL;
65: }
66:
67: cli = e_malloc(sizeof(rpc_cli_t));
68: if (!cli) {
69: LOGERR;
70: return NULL;
71: } else
72: memcpy(cli, rpccli, sizeof(rpc_cli_t));
73:
74: memcpy(&cli->cli_sa, &rpccli->cli_sa, sizeof cli->cli_sa);
75: switch (cli->cli_sa.sa.sa_family) {
76: case AF_INET:
77: cli->cli_sa.sin.sin_port =
78: htons(Port ? Port : ntohs(cli->cli_sa.sin.sin_port) + 1);
79: break;
80: case AF_INET6:
81: cli->cli_sa.sin6.sin6_port =
82: htons(Port ? Port : ntohs(cli->cli_sa.sin6.sin6_port) + 1);
83: break;
84: case AF_LOCAL:
85: strlcat(cli->cli_sa.sun.sun_path, ".blob", sizeof cli->cli_sa.sun.sun_path);
86: break;
87: default:
88: rpc_SetErr(EINVAL, "Invalid socket type %d", cli->cli_sa.sa.sa_family);
89: return NULL;
90: }
91:
92: AIT_COPY_VAL(&cli->cli_buf, &rpccli->cli_buf);
93: n = AIT_LEN(&cli->cli_buf);
94:
95: /* connect to BLOB server */
96: cli->cli_sock = socket(cli->cli_sa.sa.sa_family, SOCK_STREAM, 0);
97: if (cli->cli_sock == -1) {
98: LOGERR;
99: e_free(cli);
100: return NULL;
101: }
102: if (setsockopt(cli->cli_sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof n) == -1) {
103: LOGERR;
104: close(cli->cli_sock);
105: e_free(cli);
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);
111: e_free(cli);
112: return NULL;
113: }
114: if (connect(cli->cli_sock, &cli->cli_sa.sa, e_addrlen(&cli->cli_sa)) == -1) {
115: LOGERR;
116: close(cli->cli_sock);
117: e_free(cli);
118: return NULL;
119: } else
120: fcntl(cli->cli_sock, F_SETFL, fcntl(cli->cli_sock, F_GETFL) | O_NONBLOCK);
121:
122: return cli;
123: }
124:
125: /*
126: * rpc_cli_closeBLOBClient() - Close connection to BLOB server and free resources
127: *
128: * @cli = BLOB Client session
129: * return: none
130: */
131: void
132: rpc_cli_closeBLOBClient(rpc_cli_t ** __restrict cli)
133: {
134: if (!cli || !*cli)
135: return;
136:
137: shutdown((*cli)->cli_sock, SHUT_RDWR);
138: close((*cli)->cli_sock);
139:
140: AIT_FREE_VAL(&(*cli)->cli_buf);
141:
142: e_free(*cli);
143: *cli = NULL;
144: }
145:
146: /* -------------------------------------------------------------- */
147:
148: /*
149: * rpc_cli_openClient() - Connect to RPC Server
150: *
151: * @InstID = InstID for RPC session request
152: * @netBuf = Network buffer length (min:512 bytes), if =0 == BUFSIZ (also meaning max RPC packet)
153: * @csHost = Host name or IP address for bind server
154: * @Port = Port for bind server, if Port == 0 default port is selected
155: * @proto = Protocol, if == 0 choose SOCK_STREAM
156: * return: NULL == error or !=NULL connection to RPC server established
157: */
158: rpc_cli_t *
159: rpc_cli_openClient(u_char InstID, int netBuf, const char *csHost, u_short Port, int proto)
160: {
161: int n = 1;
162: rpc_cli_t *cli = NULL;
163: sockaddr_t sa = E_SOCKADDR_INIT;
164: socklen_t salen;
165:
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;
175: if (!(salen = e_gethostbyname(csHost, Port, &sa)))
176: return NULL;
177: if (!proto)
178: proto = SOCK_STREAM;
179: if (netBuf < RPC_MIN_BUFSIZ)
180: netBuf = BUFSIZ;
181: else
182: netBuf = E_ALIGN(netBuf, 2); /* align netBuf length */
183:
184: cli = e_malloc(sizeof(rpc_cli_t));
185: if (!cli) {
186: LOGERR;
187: return NULL;
188: } else
189: memset(cli, 0, sizeof(rpc_cli_t));
190:
191: /* build session */
192: cli->cli_parent = e_malloc(sizeof(rpc_sess_t));
193: if (!cli->cli_parent) {
194: LOGERR;
195: e_free(cli);
196: return NULL;
197: } else {
198: ((rpc_sess_t*) cli->cli_parent)->sess_version = RPC_VERSION;
199: ((rpc_sess_t*) cli->cli_parent)->sess_instance = InstID;
200: }
201:
202: cli->cli_id = proto;
203: memcpy(&cli->cli_sa, &sa, sizeof cli->cli_sa);
204: AIT_SET_BUFSIZ(&cli->cli_buf, 0, netBuf);
205:
206: /* connect to RPC server */
207: cli->cli_sock = socket(cli->cli_sa.sa.sa_family, cli->cli_id,
208: cli->cli_id == SOCK_RAW ? IPPROTO_ERPC : 0);
209: if (cli->cli_sock == -1) {
210: LOGERR;
211: goto err;
212: }
213: if (setsockopt(cli->cli_sock, SOL_SOCKET, SO_SNDBUF,
214: &netBuf, sizeof netBuf) == -1) {
215: LOGERR;
216: goto err;
217: }
218: if (setsockopt(cli->cli_sock, SOL_SOCKET, SO_RCVBUF,
219: &netBuf, sizeof netBuf) == -1) {
220: LOGERR;
221: goto err;
222: }
223: if (cli->cli_id == SOCK_STREAM) {
224: setsockopt(cli->cli_sock, IPPROTO_TCP, TCP_NODELAY, &n, sizeof n);
225: if (connect(cli->cli_sock, &cli->cli_sa.sa, salen) == -1) {
226: LOGERR;
227: goto err;
228: }
229: }
230: if (cli->cli_id == SOCK_DGRAM) {
231: sockaddr_t sa2;
232:
233: if (!(salen = e_gethostbyname(csHost, 0, &sa2)))
234: goto err;
235: if (bind(cli->cli_sock, &sa2.sa, salen) == -1) {
236: LOGERR;
237: goto err;
238: }
239: }
240:
241: fcntl(cli->cli_sock, F_SETFL, fcntl(cli->cli_sock, F_GETFL) | O_NONBLOCK);
242: return cli;
243: err:
244: AIT_FREE_VAL(&cli->cli_buf);
245: if (cli->cli_sock > 2)
246: close(cli->cli_sock);
247: e_free(cli->cli_parent);
248: e_free(cli);
249: return NULL;
250: }
251:
252: /*
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:
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;
272: socklen_t salen = sizeof sa2;
273:
274: #ifndef __linux__
275: sa2.sa.sa_len = salen;
276: #endif
277: if (!getsockname(cli->cli_sock, &sa2.sa, &salen))
278: unlink(sa2.sun.sun_path);
279: }
280: close(cli->cli_sock);
281:
282: srandom(time(NULL) ^ getpid());
283:
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)
302: if (connect(cli->cli_sock, &cli->cli_sa.sa, e_addrlen(&cli->cli_sa)) == -1) {
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: /*
313: * rpc_cli_closeClient() - Close connection to RPC server and free resources
314: *
315: * @cli = RPC Client session
316: * return: none
317: */
318: void
319: rpc_cli_closeClient(rpc_cli_t ** __restrict cli)
320: {
321: if (!cli || !*cli || (*cli)->cli_id == SOCK_BPF)
322: return;
323:
324: if ((*cli)->cli_id == SOCK_STREAM)
325: shutdown((*cli)->cli_sock, SHUT_RDWR);
326: if ((*cli)->cli_id == SOCK_DGRAM && (*cli)->cli_sa.sa.sa_family == AF_LOCAL) {
327: sockaddr_t sa2 = E_SOCKADDR_INIT;
328: socklen_t salen = sizeof sa2;
329:
330: #ifndef __linux__
331: sa2.sa.sa_len = salen;
332: #endif
333: if (!getsockname((*cli)->cli_sock, &sa2.sa, &salen))
334: unlink(sa2.sun.sun_path);
335: }
336: close((*cli)->cli_sock);
337:
338: AIT_FREE_VAL(&(*cli)->cli_buf);
339:
340: if ((*cli)->cli_parent)
341: e_free((*cli)->cli_parent);
342:
343: e_free(*cli);
344: *cli = NULL;
345: }
346:
347: /*
348: * rpc_pkt_Send() - Send RPC packet
349: *
350: * @sock = Socket
351: * @type = Type of socket
352: * @sa = Server address
353: * @pkt = RPC packet
354: * @len = Length of packet
355: * return: -1 error, 0 EOF or >0 sended bytes
356: */
357: int
358: rpc_pkt_Send(int sock, int type, sockaddr_t * __restrict sa, ait_val_t * __restrict pkt, int len)
359: {
360: if (!pkt) {
361: rpc_SetErr(EINVAL, "Invalid argument(s)!");
362: return -1;
363: }
364:
365: return rpc_Write(sock, type, MSG_NOSIGNAL, sa, pkt, len);
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
375: * @seq = Signed packet with seq.no
376: * return: -1 error, 0 EOF or >0 received bytes
377: */
378: int
379: rpc_pkt_Receive(int sock, int type, sockaddr_t * __restrict sa, ait_val_t * __restrict pkt, int seq)
380: {
381: int ret;
382: struct tagRPCCall *rpc;
383:
384: if (!pkt) {
385: rpc_SetErr(EINVAL, "Invalid argument(s)!");
386: return -1;
387: }
388:
389: /* reply from RPC server */
390: do {
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);
398: if (ret < ntohl(rpc->call_len))
399: continue;
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;
407: }
408: } while (ret < 0);
409:
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 = >0 We not want RPC reply, -1 IPC request with reply
421: * @nocrc = Without CRC calculation
422: * @seq = Sign packet with seq.no
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,
427: array_t * __restrict vars, int noreply, int nocrc, int seq)
428: {
429: struct tagRPCCall *rpc;
430: int ret = 0, estlen, len = sizeof(struct tagRPCCall);
431: u_char *buf;
432:
433: if (!pkt || !sess) {
434: rpc_SetErr(EINVAL, "Invalid argument(s)!");
435: return -1;
436: }
437:
438: /* calc estimated length */
439: estlen = ait_resideVars(vars) + len;
440: if (estlen > AIT_LEN(pkt)) {
441: rpc_SetErr(EMSGSIZE, "Message too long");
442: return -1;
443: }
444: buf = AIT_GET_BUF(pkt);
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
453: rpc->call_argc = (u_char) array_Size(vars);
454:
455: /* set reply */
456: rpc->call_req.flags = (uint64_t) htonl((noreply > 0) ? RPC_NOREPLY : RPC_REPLY);
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 */
469: rpc->call_len = htonl(len);
470: rpc->call_io = (noreply == -1) ? RPC_IPC : RPC_REQ;
471:
472: /* sign packet */
473: rpc->call_seq = htonl(seq);
474:
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: }
480:
481: return len;
482: }
483:
484: /*
485: * rpc_pkt_Replay() - Decode RPC Replay packet
486: *
487: * @pkt = Packet buffer
488: * @sess = RPC session info, if =NULL don't check session
489: * @tag = Function tag, if =CALL_TAG_MAX don't check tag
490: * @vars = Function argument array of values, may be NULL
491: * @nocrc = Without CRC calculation
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,
496: array_t ** __restrict vars, int nocrc)
497: {
498: struct tagRPCCall *rpc;
499: int len;
500: u_char *buf;
501: uint16_t crc;
502:
503: if (!pkt) {
504: rpc_SetErr(EINVAL, "Invalid argument(s)!");
505: return -1;
506: } else
507: buf = AIT_GET_BUF(pkt);
508:
509: rpc = (struct tagRPCCall*) buf;
510: if (!nocrc) {
511: /* calculate CRC */
512: crc = ntohs(rpc->call_crc);
513: rpc->call_crc ^= rpc->call_crc;
514: if (crc != crcFletcher16((u_short*) buf, ntohl(rpc->call_len) / 2)) {
515: rpc_SetErr(ERPCMISMATCH, "Bad CRC RPC packet");
516: return -1;
517: }
518: }
519:
520: /* check RPC packet session info */
521: if (sess && rpc_chkPktSession(&rpc->call_session, sess)) {
522: rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session");
523: return -1;
524: }
525: if (tag != CALL_TAG_MAX && ntohs(rpc->call_tag) != tag) {
526: rpc_SetErr(ERPCMISMATCH, "Get wrong RPC reply");
527: return -1;
528: }
529: if (ntohl(rpc->call_rep.eno) && ntohl(rpc->call_rep.ret) == -1) {
530: rpc_SetErr(ntohl(rpc->call_rep.eno), "Server side: retcode=%d #%d %s",
531: ntohl(rpc->call_rep.ret), ntohl(rpc->call_rep.eno),
532: strerror(ntohl(rpc->call_rep.eno)));
533: return -1;
534: }
535: len = rpc->call_argc * sizeof(ait_val_t);
536: if (len > AIT_LEN(pkt) - sizeof(struct tagRPCCall)) {
537: rpc_SetErr(EMSGSIZE, "Reply RPC packet not enough buffer space ...");
538: return -1;
539: }
540: if (len > ntohl(rpc->call_len) - sizeof(struct tagRPCCall)) {
541: rpc_SetErr(EMSGSIZE, "Reply RPC packet is too short ...");
542: return -1;
543: }
544:
545: /* RPC is OK! Go de-marshaling variables ... */
546: if (vars && rpc->call_argc) {
547: #ifdef CLI_RES_ZCOPY
548: *vars = ait_buffer2vars(buf + sizeof(struct tagRPCCall), len,
549: rpc->call_argc, 42);
550: #else
551: *vars = ait_buffer2vars(buf + sizeof(struct tagRPCCall), len,
552: rpc->call_argc, 0);
553: #endif
554: if (!*vars) {
555: rpc_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
556: return -1;
557: }
558: }
559:
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 = >0 We not want RPC reply, -1 IPC request with 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()
571: * return: -1 error, 0 ok result or 1 closed rpc connection
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: {
577: int type = 0, wlen;
578: u_int seq = 0;
579:
580: if (!cli) {
581: rpc_SetErr(EINVAL, "Can`t execute call because parameter is null or invalid!");
582: return -1;
583: } else {
584: if (cli->cli_id == SOCK_STREAM || cli->cli_id == SOCK_EXT)
585: type = cli->cli_id;
586: else
587: seq = random(); /* sign package */
588: }
589: if (out_vars)
590: *out_vars = NULL;
591:
592: if ((wlen = rpc_pkt_Request(&cli->cli_buf, cli->cli_parent, tag,
593: in_vars, noreply, type, seq)) == -1)
594: return -1;
595:
596: if ((wlen = rpc_pkt_Send(cli->cli_sock, cli->cli_id, &cli->cli_sa,
597: &cli->cli_buf, wlen)) == -1)
598: return -1;
599: if (!wlen) /* closed rpc connection */
600: return 1;
601:
602: if (noreply > 0) /* we not want reply */
603: return 0;
604:
605: if ((wlen = rpc_pkt_Receive(cli->cli_sock, cli->cli_id, &cli->cli_sa,
606: &cli->cli_buf, seq)) == -1)
607: return -1;
608: if (!wlen) { /* closed rpc connection */
609: rpc_SetErr(ECONNRESET, "Closed connection");
610: return -1;
611: }
612:
613: if ((wlen = rpc_pkt_Replay(&cli->cli_buf, cli->cli_parent, tag,
614: out_vars, type)) == -1)
615: return -1;
616:
617: return wlen;
618: }
619:
620: /*
621: * rpc_cli_freeCall() - Free resouce allocated by RPC call
622: *
623: * @out_vars = Returned array with variables from RPC call
624: * return: none
625: */
626: void
627: rpc_cli_freeCall(array_t ** __restrict out_vars)
628: {
629: #ifdef CLI_RES_ZCOPY
630: array_Destroy(out_vars);
631: #else
632: ait_freeVars(out_vars);
633: #endif
634: }
635:
636: /*
637: * rpc_cli_ping() - Ping RPC server
638: *
639: * @cli = connected client
640: * return: -1 error or !=-1 ping seq id
641: */
642: int
643: rpc_cli_ping(rpc_cli_t *cli)
644: {
645: int ret = 0;
646: array_t *arr = NULL;
647:
648: if (!cli)
649: return -1;
650:
651: if (rpc_cli_execCall(cli, RPC_REPLY, CALL_SRVPING, NULL, &arr))
652: return -1;
653: else if (array_Data(arr) && array(arr, 0, ait_val_t*))
654: ret = AIT_GET_U16(array(arr, 0, ait_val_t*));
655: else
656: ret = -1;
657: rpc_cli_freeCall(&arr);
658:
659: return ret;
660: }
661:
662:
663: /*
664: * rpc_cli_openClient2() - Connect to layer2 RPC Server
665: *
666: * @InstID = InstID for RPC session request
667: * @netBuf = Network buffer length (min:512 bytes), if =0 == BUFSIZ (also meaning max RPC packet)
668: * @csIface = Interface name for bind client, if NULL first interface on host
669: * @csHost = Host ethernet address
670: * return: NULL == error or !=NULL connection to RPC server established
671: */
672: rpc_cli_t *
673: rpc_cli_openClient2(u_char InstID, int netBuf, const char *csIface, const char *csHost)
674: {
675: #ifndef __linux__
676: rpc_cli_t *cli = NULL;
677: sockaddr_t sa = E_SOCKADDR_INIT, sal = E_SOCKADDR_INIT;
678: char szIface[64], szStr[STRSIZ];
679: register int i;
680: struct ifreq ifr;
681: int n = 1;
682: struct bpf_insn insns[] = {
683: BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12),
684: BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, RPC_DEFPORT, 0, 1),
685: BPF_STMT(BPF_RET + BPF_K, -1),
686: BPF_STMT(BPF_RET + BPF_K, 0),
687: };
688: struct bpf_program fcode = {
689: .bf_len = sizeof(insns) / sizeof(struct bpf_insn),
690: .bf_insns = insns
691: };
692:
693: if (!csHost || !e_getlinkbyname(csHost, &sa))
694: return NULL;
695: if (!csIface) {
696: if (e_get1stiface(szIface, sizeof szIface))
697: return NULL;
698: } else
699: strlcpy(szIface, csIface, sizeof szIface);
700: if (!e_getifacebyname(szIface, &sal))
701: return NULL;
702:
703: cli = e_malloc(sizeof(rpc_cli_t));
704: if (!cli) {
705: LOGERR;
706: return NULL;
707: } else
708: memset(cli, 0, sizeof(rpc_cli_t));
709:
710: /* build session */
711: cli->cli_parent = e_malloc(sizeof(rpc_sess_t));
712: if (!cli->cli_parent) {
713: LOGERR;
714: e_free(cli);
715: return NULL;
716: } else {
717: ((rpc_sess_t*) cli->cli_parent)->sess_version = RPC_VERSION;
718: ((rpc_sess_t*) cli->cli_parent)->sess_instance = InstID;
719: }
720:
721: cli->cli_id = SOCK_BPF;
722: memcpy(&cli->cli_sa, &sa, sizeof cli->cli_sa);
723:
724: /* connect to RPC server */
725: for (i = 0; i < 10; i++) {
726: memset(szStr, 0, sizeof szStr);
727: snprintf(szStr, sizeof szStr, "/dev/bpf%d", i);
728: cli->cli_sock = open(szStr, O_RDWR);
729: if (cli->cli_sock > STDERR_FILENO)
730: break;
731: }
732: if (cli->cli_sock < 3) {
733: LOGERR;
734: e_free(cli->cli_parent);
735: e_free(cli);
736: return NULL;
737: }
738:
739: if (ioctl(cli->cli_sock, BIOCIMMEDIATE, &n) == -1) {
740: LOGERR;
741: goto err;
742: }
743: if (ioctl(cli->cli_sock, BIOCSETF, &fcode) == -1) {
744: LOGERR;
745: goto err;
746: }
747: n = (netBuf < RPC_MIN_BUFSIZ) ? getpagesize() : E_ALIGN(netBuf, 2);
748: if (ioctl(cli->cli_sock, BIOCSBLEN, &n) == -1) {
749: LOGERR;
750: goto err;
751: } else
752: AIT_SET_BUFSIZ(&cli->cli_buf, 0, n);
753:
754: memset(&ifr, 0, sizeof ifr);
755: strlcpy(ifr.ifr_name, szIface, sizeof ifr.ifr_name);
756: if (ioctl(cli->cli_sock, BIOCSETIF, &ifr) == -1) {
757: LOGERR;
758: goto err;
759: } else
760: fcntl(cli->cli_sock, F_SETFL,
761: fcntl(cli->cli_sock, F_GETFL) | O_NONBLOCK);
762:
763: return cli;
764: err:
765: AIT_FREE_VAL(&cli->cli_buf);
766: if (cli->cli_sock > 2)
767: close(cli->cli_sock);
768: e_free(cli->cli_parent);
769: e_free(cli);
770: #else
771: rpc_SetErr(ENOTSUP, "Feature isn't supported on Linux!");
772: #endif
773: return NULL;
774: }
775:
776: /*
777: * rpc_cli_closeClient2() - Close layer2 connection to RPC server and free resources
778: *
779: * @cli = RPC Client session
780: * return: none
781: */
782: void
783: rpc_cli_closeClient2(rpc_cli_t ** __restrict cli)
784: {
785: if (!cli || !*cli || (*cli)->cli_id != SOCK_BPF)
786: return;
787:
788: close((*cli)->cli_sock);
789:
790: AIT_FREE_VAL(&(*cli)->cli_buf);
791:
792: if ((*cli)->cli_parent)
793: e_free((*cli)->cli_parent);
794:
795: e_free(*cli);
796: *cli = NULL;
797: }
798:
799: /*
800: * rpc_cli_openClientExt() - Connect to pipe RPC Server
801: *
802: * @InstID = InstID for RPC session request
803: * @netBuf = Network buffer length (min:512 bytes), if =0 == BUFSIZ (also meaning max RPC packet)
804: * @fd = File descriptor
805: * return: NULL == error or !=NULL connection to RPC server established
806: */
807: rpc_cli_t *
808: rpc_cli_openClientExt(u_char InstID, int netBuf, int fd)
809: {
810: rpc_cli_t *cli = NULL;
811: int n;
812:
813: cli = e_malloc(sizeof(rpc_cli_t));
814: if (!cli) {
815: LOGERR;
816: return NULL;
817: } else
818: memset(cli, 0, sizeof(rpc_cli_t));
819:
820: /* build session */
821: cli->cli_parent = e_malloc(sizeof(rpc_sess_t));
822: if (!cli->cli_parent) {
823: LOGERR;
824: e_free(cli);
825: return NULL;
826: } else {
827: ((rpc_sess_t*) cli->cli_parent)->sess_version = RPC_VERSION;
828: ((rpc_sess_t*) cli->cli_parent)->sess_instance = InstID;
829: }
830:
831: cli->cli_id = SOCK_EXT;
832: cli->cli_sock = fd;
833:
834: n = (netBuf < RPC_MIN_BUFSIZ) ? getpagesize() : E_ALIGN(netBuf, 2);
835: AIT_SET_BUFSIZ(&cli->cli_buf, 0, n);
836:
837: fcntl(cli->cli_sock, F_SETFL,
838: fcntl(cli->cli_sock, F_GETFL) | O_NONBLOCK);
839:
840: return cli;
841: }
842:
843: /*
844: * rpc_cli_closeClientExt() - Close pipe connection to RPC server and free resources
845: *
846: * @cli = RPC Client session
847: * return: none
848: */
849: void
850: rpc_cli_closeClientExt(rpc_cli_t ** __restrict cli)
851: {
852: if (!cli || !*cli || (*cli)->cli_id != SOCK_EXT)
853: return;
854:
855: AIT_FREE_VAL(&(*cli)->cli_buf);
856:
857: if ((*cli)->cli_parent)
858: e_free((*cli)->cli_parent);
859:
860: e_free(*cli);
861: *cli = NULL;
862: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>