File:  [ELWIX - Embedded LightWeight unIX -] / libaitrpc / src / cli.c
Revision 1.6: download - view: text, annotated - select for diffs - revision graph
Thu Nov 3 15:32:21 2011 UTC (12 years, 8 months ago) by misho
Branches: MAIN
CVS tags: rpc2_1, RPC2_0, HEAD
version 2.0

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

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