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