File:  [ELWIX - Embedded LightWeight unIX -] / libaitrpc / src / cli.c
Revision 1.9.2.7: download - view: text, annotated - select for diffs - revision graph
Wed May 16 16:36:10 2012 UTC (12 years, 2 months ago) by misho
Branches: rpc3_3
Diff to: branchpoint 1.9: preferred, unified
fix nasty bug into rpc client return error result
and other changes and fixes

    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.9.2.7 2012/05/16 16:36:10 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, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
   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: 
   61: 	if (!rpccli) {
   62: 		rpc_SetErr(EINVAL, "Invalid parameters can`t connect to BLOB server");
   63: 		return NULL;
   64: 	}
   65: 
   66: 	cli = malloc(sizeof(rpc_cli_t));
   67: 	if (!cli) {
   68: 		LOGERR;
   69: 		return NULL;
   70: 	} else
   71: 		memcpy(cli, rpccli, sizeof(rpc_cli_t));
   72: 
   73: 	memcpy(&cli->cli_sa, &rpccli->cli_sa, sizeof(io_sockaddr_t));
   74: 	switch (cli->cli_sa.sa.sa_family) {
   75: 		case AF_INET:
   76: 			cli->cli_sa.sin.sin_port = 
   77: 				htons(Port ? Port : ntohs(cli->cli_sa.sin.sin_port) + 1);
   78: 			break;
   79: 		case AF_INET6:
   80: 			cli->cli_sa.sin6.sin6_port = 
   81: 				htons(Port ? Port : ntohs(cli->cli_sa.sin6.sin6_port) + 1);
   82: 			break;
   83: 		case AF_LOCAL:
   84: 			strlcat(cli->cli_sa.sun.sun_path, ".blob", sizeof cli->cli_sa.sun.sun_path);
   85: 			break;
   86: 		default:
   87: 			rpc_SetErr(EINVAL, "Invalid socket type %d", cli->cli_sa.sa.sa_family);
   88: 			return NULL;
   89: 	}
   90: 
   91: 	AIT_COPY_VAL(&cli->cli_buf, &rpccli->cli_buf);
   92: 
   93: 	/* connect to BLOB server */
   94: 	cli->cli_sock = socket(cli->cli_sa.sa.sa_family, SOCK_STREAM, 0);
   95: 	if (cli->cli_sock == -1) {
   96: 		LOGERR;
   97: 		free(cli);
   98: 		return NULL;
   99: 	}
  100: 	if (connect(cli->cli_sock, &cli->cli_sa.sa, cli->cli_sa.sa.sa_len) == -1) {
  101: 		LOGERR;
  102: 		close(cli->cli_sock);
  103: 		free(cli);
  104: 		return NULL;
  105: 	}
  106: 
  107: 	return cli;
  108: }
  109: 
  110: /*
  111:  * rpc_cli_closeBLOBClient() - Close connection to BLOB server and free resources
  112:  *
  113:  * @cli = BLOB Client session
  114:  * return: none
  115:  */
  116: void
  117: rpc_cli_closeBLOBClient(rpc_cli_t ** __restrict cli)
  118: {
  119: 	if (!cli || !*cli)
  120: 		return;
  121: 
  122: 	shutdown((*cli)->cli_sock, SHUT_RDWR);
  123: 	close((*cli)->cli_sock);
  124: 
  125: 	AIT_FREE_VAL(&(*cli)->cli_buf);
  126: 
  127: 	free(*cli);
  128: 	*cli = NULL;
  129: }
  130: 
  131: /* -------------------------------------------------------------- */
  132: 
  133: /*
  134:  * rpc_cli_openClient() - Connect to RPC Server
  135:  *
  136:  * @ProgID = ProgramID for RPC session request
  137:  * @ProcID = ProcessID for RPC session request
  138:  * @netBuf = Network buffer length, if =0 == BUFSIZ (also meaning max RPC packet)
  139:  * @csHost = Host name or IP address for bind server
  140:  * @Port = Port for bind server, if Port == 0 default port is selected
  141:  * return: NULL == error or !=NULL connection to RPC server established
  142:  */
  143: rpc_cli_t *
  144: rpc_cli_openClient(u_int ProgID, u_char ProcID, int netBuf, const char *csHost, u_short Port)
  145: {
  146: 	rpc_cli_t *cli = NULL;
  147: 	io_sockaddr_t sa;
  148: 
  149: 	if (!io_gethostbyname(csHost, Port, &sa))
  150: 		return NULL;
  151: 	if (!Port)
  152: 		Port = RPC_DEFPORT;
  153: 	if (!netBuf)
  154: 		netBuf = BUFSIZ;
  155: 
  156: #ifdef HAVE_SRANDOMDEV
  157: 	srandomdev();
  158: #else
  159: 	time_t tim;
  160: 
  161: 	srandom((time(&tim) ^ getpid()));
  162: #endif
  163: 
  164: 	cli = malloc(sizeof(rpc_cli_t));
  165: 	if (!cli) {
  166: 		LOGERR;
  167: 		return NULL;
  168: 	} else
  169: 		memset(cli, 0, sizeof(rpc_cli_t));
  170: 
  171: 	/* build session */
  172: 	cli->cli_parent = malloc(sizeof(rpc_sess_t));
  173: 	if (!cli->cli_parent) {
  174: 		LOGERR;
  175: 		free(cli);
  176: 		return NULL;
  177: 	} else {
  178: 		((rpc_sess_t*) cli->cli_parent)->sess_version = RPC_VERSION;
  179: 		((rpc_sess_t*) cli->cli_parent)->sess_program = ProgID;
  180: 		((rpc_sess_t*) cli->cli_parent)->sess_process = ProcID;
  181: 	}
  182: 
  183: 	memcpy(&cli->cli_sa, &sa, sizeof cli->cli_sa);
  184: 	AIT_SET_BUF2(&cli->cli_buf, 0, netBuf);
  185: 
  186: 	/* connect to RPC server */
  187: 	cli->cli_sock = socket(cli->cli_sa.sa.sa_family, SOCK_STREAM, 0);
  188: 	if (cli->cli_sock == -1) {
  189: 		LOGERR;
  190: 		AIT_FREE_VAL(&cli->cli_buf);
  191: 		free(cli->cli_parent);
  192: 		free(cli);
  193: 		return NULL;
  194: 	}
  195: 	if (connect(cli->cli_sock, &cli->cli_sa.sa, cli->cli_sa.sa.sa_len) == -1) {
  196: 		LOGERR;
  197: 		AIT_FREE_VAL(&cli->cli_buf);
  198: 		close(cli->cli_sock);
  199: 		free(cli->cli_parent);
  200: 		free(cli);
  201: 		return NULL;
  202: 	}
  203: 
  204: 	return cli;
  205: }
  206: 
  207: /*
  208:  * rpc_cli_closeClient() - Close connection to RPC server and free resources
  209:  *
  210:  * @cli = RPC Client session
  211:  * return: none
  212:  */
  213: void
  214: rpc_cli_closeClient(rpc_cli_t ** __restrict cli)
  215: {
  216: 	if (!cli || !*cli)
  217: 		return;
  218: 
  219: 	shutdown((*cli)->cli_sock, SHUT_RDWR);
  220: 	close((*cli)->cli_sock);
  221: 
  222: 	AIT_FREE_VAL(&(*cli)->cli_buf);
  223: 
  224: 	if ((*cli)->cli_parent)
  225: 		free((*cli)->cli_parent);
  226: 
  227: 	free(*cli);
  228: 	*cli = NULL;
  229: }
  230: 
  231: 
  232: /*
  233:  * rpc_cli_execCall() - Execute RPC call
  234:  *
  235:  * @cli = RPC Client session
  236:  * @noreply = We not want RPC reply
  237:  * @tag = Function tag for execution
  238:  * @in_vars = IN RPC call array of rpc values
  239:  * @out_vars = OUT returned array of rpc values, must be free after use with rpc_cli_freeVals()
  240:  * return: -1 error or != -1 ok result
  241:  */
  242: int
  243: rpc_cli_execCall(rpc_cli_t *cli, int noreply, u_short tag, 
  244: 		array_t * __restrict in_vars, array_t ** __restrict out_vars)
  245: {
  246: 	struct tagRPCCall *rpc;
  247: 	int ret = 0, wlen = sizeof(struct tagRPCCall);
  248: 	uint16_t crc;
  249: 	u_char *buf;
  250: 	struct pollfd pfd;
  251: 
  252: 	if (!cli) {
  253: 		rpc_SetErr(EINVAL, "Can`t execute call because parameter is null or invalid!");
  254: 		return -1;
  255: 	} else
  256: 		buf = AIT_GET_BUF(&cli->cli_buf);
  257: 	if (out_vars)
  258: 		*out_vars = NULL;
  259: 
  260: 	/* prepare RPC call */
  261: 	rpc = (struct tagRPCCall*) buf;
  262: 	rpc_addPktSession(&rpc->call_session, cli->cli_parent);
  263: 	rpc->call_argc = htons(io_arraySize(in_vars));
  264: 	rpc->call_tag = htons(tag);
  265: 
  266: 	/* set reply */
  267: 	rpc->call_req.flags = noreply ? RPC_NOREPLY : RPC_REPLY;
  268: 
  269: 	if (io_arraySize(in_vars)) {
  270: 		/* marshaling variables */
  271: 		ret = io_vars2buffer(buf + wlen, AIT_LEN(&cli->cli_buf) - wlen, in_vars);
  272: 		if (ret == -1) {
  273: 			rpc_SetErr(EBADRPC, "Failed to prepare RPC packet values");
  274: 			return -1;
  275: 		} else
  276: 			wlen += ret;
  277: 	}
  278: 
  279: 	/* total packet length */
  280: 	rpc->call_len = htons(wlen);
  281: 
  282: 	/* calculate CRC */
  283: 	rpc->call_crc ^= rpc->call_crc;
  284: 	rpc->call_crc = htons(crcFletcher16((u_short*) buf, wlen / 2));
  285: 
  286: 	if ((ret = send(cli->cli_sock, buf, wlen, MSG_NOSIGNAL)) == -1) {
  287: 		LOGERR;
  288: 		return -1;
  289: 	} else if (ret != wlen) {
  290: 		rpc_SetErr(EPROCUNAVAIL, "RPC request, should be send %d bytes, "
  291: 				"really sended %d bytes", wlen, ret);
  292: 		return -1;
  293: 	}
  294: 
  295: 	if (noreply)	/* we not want reply */
  296: 		return 0;
  297: 
  298: 	/* reply from RPC server */
  299: 	pfd.fd = cli->cli_sock;
  300: 	pfd.events = POLLIN | POLLPRI;
  301: 	if ((ret = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) == -1 || 
  302: 			pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
  303: 		if (ret)
  304: 			LOGERR;
  305: 		else
  306: 			rpc_SetErr(ETIMEDOUT, "Timeout, no answer from RPC server");
  307: 
  308: 		return -1;
  309: 	}
  310: 	memset(buf, 0, AIT_LEN(&cli->cli_buf));
  311: 	if ((ret = recv(cli->cli_sock, buf, AIT_LEN(&cli->cli_buf), 0)) < 1) {
  312: 		if (ret)
  313: 			LOGERR;
  314: 		return -1;
  315: 	}
  316: 	if (ret < sizeof(struct tagRPCCall)) {
  317: 		rpc_SetErr(ERPCMISMATCH, "Short RPC packet");
  318: 		return -1;
  319: 	}
  320: 
  321: 	/* calculate CRC */
  322: 	crc = ntohs(rpc->call_crc);
  323: 	rpc->call_crc ^= rpc->call_crc;
  324: 	if (crc != crcFletcher16((u_short*) buf, ret / 2)) {
  325: 		rpc_SetErr(ERPCMISMATCH, "Bad CRC RPC packet");
  326: 		return -1;
  327: 	}
  328: 
  329: 	/* check RPC packet session info */
  330: 	if (rpc_chkPktSession(&rpc->call_session, cli->cli_parent)) {
  331: 		rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session");
  332: 		return -1;
  333: 	} else
  334: 		wlen = sizeof(struct tagRPCCall);
  335: 	if (ntohs(rpc->call_tag) != tag) {
  336: 		rpc_SetErr(ERPCMISMATCH, "Get wrong RPC reply");
  337: 		return -1;
  338: 	}
  339: 	if (ntohl(rpc->call_rep.eno) && ntohl(rpc->call_rep.ret) == -1) {
  340: 		rpc_SetErr(ntohl(rpc->call_rep.eno), "Server side: retcode=%d #%d %s", 
  341: 				ntohl(rpc->call_rep.ret), ntohl(rpc->call_rep.eno), 
  342: 				strerror(ntohl(rpc->call_rep.eno)));
  343: 		return -1;
  344: 	}
  345: 	if (ntohs(rpc->call_argc) * sizeof(ait_val_t) > AIT_LEN(&cli->cli_buf) - wlen) {
  346: 		rpc_SetErr(EMSGSIZE, "Reply RPC packet is too long ...");
  347: 		return -1;
  348: 	}
  349: 
  350: 	/* RPC is OK! Go de-marshaling variables ... */
  351: 	if (ntohs(rpc->call_argc)) {
  352: 		*out_vars = io_buffer2vars(buf + wlen, AIT_LEN(&cli->cli_buf) - wlen, 
  353: 				ntohs(rpc->call_argc), 0);
  354: 		if (!*out_vars) {
  355: 			return -1;
  356: 		}
  357: 	}
  358: 
  359: 	ret = ntohl(rpc->call_rep.ret);
  360: 	return ret;
  361: }

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