File:  [ELWIX - Embedded LightWeight unIX -] / libaitrpc / src / cli.c
Revision 1.26.4.2: download - view: text, annotated - select for diffs - revision graph
Tue Aug 2 10:26:58 2016 UTC (8 years ago) by misho
Branches: rpc9_3
Diff to: branchpoint 1.26: preferred, unified
port to linux

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>