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.27 2016/08/08 13:21:13 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 - 2016
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 < 1);
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 = We not want RPC 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 ? 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 = 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 = 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()
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) /* 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: return 1;
610:
611: if ((wlen = rpc_pkt_Replay(&cli->cli_buf, cli->cli_parent, tag,
612: out_vars, type)) == -1)
613: return -1;
614:
615: return 0;
616: }
617:
618: /*
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: */
624: void
625: rpc_cli_freeCall(array_t ** __restrict out_vars)
626: {
627: #ifdef CLI_RES_ZCOPY
628: array_Destroy(out_vars);
629: #else
630: ait_freeVars(out_vars);
631: #endif
632: }
633:
634: /*
635: * rpc_cli_ping() - Ping RPC server
636: *
637: * @cli = connected client
638: * return: -1 error or !=-1 ping seq id
639: */
640: int
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
652: ret = AIT_GET_U16(array(arr, 0, ait_val_t*));
653: rpc_cli_freeCall(&arr);
654:
655: return ret;
656: }
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: {
671: #ifndef __linux__
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);
766: #else
767: rpc_SetErr(ENOTSUP, "Feature isn't supported on Linux!");
768: #endif
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: }
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>