File:  [ELWIX - Embedded LightWeight unIX -] / libaitrpc / src / cli.c
Revision 1.25.2.1: download - view: text, annotated - select for diffs - revision graph
Sun Jun 28 21:40:45 2015 UTC (9 years, 1 month ago) by misho
Branches: rpc9_1
Diff to: branchpoint 1.25: preferred, unified
rework srv/cli sides

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

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