File:  [ELWIX - Embedded LightWeight unIX -] / libaitrpc / src / cli.c
Revision 1.24.2.2: download - view: text, annotated - select for diffs - revision graph
Wed Jan 28 00:06:22 2015 UTC (9 years, 6 months ago) by misho
Branches: rpc8_1
Diff to: branchpoint 1.24: preferred, unified
initial support of raw protocol

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

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