File:  [ELWIX - Embedded LightWeight unIX -] / libaitrpc / src / cli.c
Revision 1.9.2.3: download - view: text, annotated - select for diffs - revision graph
Tue May 15 16:06:13 2012 UTC (12 years, 2 months ago) by misho
Branches: rpc3_3
Diff to: branchpoint 1.9: preferred, unified
start massive redesign and rework of rpc

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

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