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.29 2024/03/20 17:32:31 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
654: ret = AIT_GET_U16(array(arr, 0, ait_val_t*));
655: rpc_cli_freeCall(&arr);
656:
657: return ret;
658: }
659:
660:
661: /*
662: * rpc_cli_openClient2() - Connect to layer2 RPC Server
663: *
664: * @InstID = InstID for RPC session request
665: * @netBuf = Network buffer length (min:512 bytes), if =0 == BUFSIZ (also meaning max RPC packet)
666: * @csIface = Interface name for bind client, if NULL first interface on host
667: * @csHost = Host ethernet address
668: * return: NULL == error or !=NULL connection to RPC server established
669: */
670: rpc_cli_t *
671: rpc_cli_openClient2(u_char InstID, int netBuf, const char *csIface, const char *csHost)
672: {
673: #ifndef __linux__
674: rpc_cli_t *cli = NULL;
675: sockaddr_t sa = E_SOCKADDR_INIT, sal = E_SOCKADDR_INIT;
676: char szIface[64], szStr[STRSIZ];
677: register int i;
678: struct ifreq ifr;
679: int n = 1;
680: struct bpf_insn insns[] = {
681: BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12),
682: BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, RPC_DEFPORT, 0, 1),
683: BPF_STMT(BPF_RET + BPF_K, -1),
684: BPF_STMT(BPF_RET + BPF_K, 0),
685: };
686: struct bpf_program fcode = {
687: .bf_len = sizeof(insns) / sizeof(struct bpf_insn),
688: .bf_insns = insns
689: };
690:
691: if (!csHost || !e_getlinkbyname(csHost, &sa))
692: return NULL;
693: if (!csIface) {
694: if (e_get1stiface(szIface, sizeof szIface))
695: return NULL;
696: } else
697: strlcpy(szIface, csIface, sizeof szIface);
698: if (!e_getifacebyname(szIface, &sal))
699: return NULL;
700:
701: cli = e_malloc(sizeof(rpc_cli_t));
702: if (!cli) {
703: LOGERR;
704: return NULL;
705: } else
706: memset(cli, 0, sizeof(rpc_cli_t));
707:
708: /* build session */
709: cli->cli_parent = e_malloc(sizeof(rpc_sess_t));
710: if (!cli->cli_parent) {
711: LOGERR;
712: e_free(cli);
713: return NULL;
714: } else {
715: ((rpc_sess_t*) cli->cli_parent)->sess_version = RPC_VERSION;
716: ((rpc_sess_t*) cli->cli_parent)->sess_instance = InstID;
717: }
718:
719: cli->cli_id = SOCK_BPF;
720: memcpy(&cli->cli_sa, &sa, sizeof cli->cli_sa);
721:
722: /* connect to RPC server */
723: for (i = 0; i < 10; i++) {
724: memset(szStr, 0, sizeof szStr);
725: snprintf(szStr, sizeof szStr, "/dev/bpf%d", i);
726: cli->cli_sock = open(szStr, O_RDWR);
727: if (cli->cli_sock > STDERR_FILENO)
728: break;
729: }
730: if (cli->cli_sock < 3) {
731: LOGERR;
732: e_free(cli->cli_parent);
733: e_free(cli);
734: return NULL;
735: }
736:
737: if (ioctl(cli->cli_sock, BIOCIMMEDIATE, &n) == -1) {
738: LOGERR;
739: goto err;
740: }
741: if (ioctl(cli->cli_sock, BIOCSETF, &fcode) == -1) {
742: LOGERR;
743: goto err;
744: }
745: n = (netBuf < RPC_MIN_BUFSIZ) ? getpagesize() : E_ALIGN(netBuf, 2);
746: if (ioctl(cli->cli_sock, BIOCSBLEN, &n) == -1) {
747: LOGERR;
748: goto err;
749: } else
750: AIT_SET_BUFSIZ(&cli->cli_buf, 0, n);
751:
752: memset(&ifr, 0, sizeof ifr);
753: strlcpy(ifr.ifr_name, szIface, sizeof ifr.ifr_name);
754: if (ioctl(cli->cli_sock, BIOCSETIF, &ifr) == -1) {
755: LOGERR;
756: goto err;
757: } else
758: fcntl(cli->cli_sock, F_SETFL,
759: fcntl(cli->cli_sock, F_GETFL) | O_NONBLOCK);
760:
761: return cli;
762: err:
763: AIT_FREE_VAL(&cli->cli_buf);
764: if (cli->cli_sock > 2)
765: close(cli->cli_sock);
766: e_free(cli->cli_parent);
767: e_free(cli);
768: #else
769: rpc_SetErr(ENOTSUP, "Feature isn't supported on Linux!");
770: #endif
771: return NULL;
772: }
773:
774: /*
775: * rpc_cli_closeClient2() - Close layer2 connection to RPC server and free resources
776: *
777: * @cli = RPC Client session
778: * return: none
779: */
780: void
781: rpc_cli_closeClient2(rpc_cli_t ** __restrict cli)
782: {
783: if (!cli || !*cli || (*cli)->cli_id != SOCK_BPF)
784: return;
785:
786: close((*cli)->cli_sock);
787:
788: AIT_FREE_VAL(&(*cli)->cli_buf);
789:
790: if ((*cli)->cli_parent)
791: e_free((*cli)->cli_parent);
792:
793: e_free(*cli);
794: *cli = NULL;
795: }
796:
797: /*
798: * rpc_cli_openClientExt() - Connect to pipe RPC Server
799: *
800: * @InstID = InstID for RPC session request
801: * @netBuf = Network buffer length (min:512 bytes), if =0 == BUFSIZ (also meaning max RPC packet)
802: * @fd = File descriptor
803: * return: NULL == error or !=NULL connection to RPC server established
804: */
805: rpc_cli_t *
806: rpc_cli_openClientExt(u_char InstID, int netBuf, int fd)
807: {
808: rpc_cli_t *cli = NULL;
809: int n;
810:
811: cli = e_malloc(sizeof(rpc_cli_t));
812: if (!cli) {
813: LOGERR;
814: return NULL;
815: } else
816: memset(cli, 0, sizeof(rpc_cli_t));
817:
818: /* build session */
819: cli->cli_parent = e_malloc(sizeof(rpc_sess_t));
820: if (!cli->cli_parent) {
821: LOGERR;
822: e_free(cli);
823: return NULL;
824: } else {
825: ((rpc_sess_t*) cli->cli_parent)->sess_version = RPC_VERSION;
826: ((rpc_sess_t*) cli->cli_parent)->sess_instance = InstID;
827: }
828:
829: cli->cli_id = SOCK_EXT;
830: cli->cli_sock = fd;
831:
832: n = (netBuf < RPC_MIN_BUFSIZ) ? getpagesize() : E_ALIGN(netBuf, 2);
833: AIT_SET_BUFSIZ(&cli->cli_buf, 0, n);
834:
835: fcntl(cli->cli_sock, F_SETFL,
836: fcntl(cli->cli_sock, F_GETFL) | O_NONBLOCK);
837:
838: return cli;
839: }
840:
841: /*
842: * rpc_cli_closeClientExt() - Close pipe connection to RPC server and free resources
843: *
844: * @cli = RPC Client session
845: * return: none
846: */
847: void
848: rpc_cli_closeClientExt(rpc_cli_t ** __restrict cli)
849: {
850: if (!cli || !*cli || (*cli)->cli_id != SOCK_EXT)
851: return;
852:
853: AIT_FREE_VAL(&(*cli)->cli_buf);
854:
855: if ((*cli)->cli_parent)
856: e_free((*cli)->cli_parent);
857:
858: e_free(*cli);
859: *cli = NULL;
860: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>