File:  [ELWIX - Embedded LightWeight unIX -] / libaitrpc / src / cli.c
Revision 1.25.2.2: download - view: text, annotated - select for diffs - revision graph
Sun Jun 28 21:52:26 2015 UTC (9 years, 1 month ago) by misho
Branches: rpc9_1
Diff to: branchpoint 1.25: preferred, unified
patch connect for local udp sockets

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

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