File:  [ELWIX - Embedded LightWeight unIX -] / libaitrpc / src / cli.c
Revision 1.9.2.10: download - view: text, annotated - select for diffs - revision graph
Thu May 17 15:14:16 2012 UTC (12 years, 1 month ago) by misho
Branches: rpc3_3
Diff to: branchpoint 1.9: preferred, unified
OpenBSD issues fixed::
 - get blob command packet with data payload at one time
 - set async client sockets
 - add forgotten error message for blob timeout

    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.10 2012/05/17 15:14:16 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: 	} else
  106: 		fcntl(cli->cli_sock, F_SETFL, fcntl(cli->cli_sock, F_GETFL) | O_NONBLOCK);
  107: 
  108: 	return cli;
  109: }
  110: 
  111: /*
  112:  * rpc_cli_closeBLOBClient() - Close connection to BLOB server and free resources
  113:  *
  114:  * @cli = BLOB Client session
  115:  * return: none
  116:  */
  117: void
  118: rpc_cli_closeBLOBClient(rpc_cli_t ** __restrict cli)
  119: {
  120: 	if (!cli || !*cli)
  121: 		return;
  122: 
  123: 	shutdown((*cli)->cli_sock, SHUT_RDWR);
  124: 	close((*cli)->cli_sock);
  125: 
  126: 	AIT_FREE_VAL(&(*cli)->cli_buf);
  127: 
  128: 	free(*cli);
  129: 	*cli = NULL;
  130: }
  131: 
  132: /* -------------------------------------------------------------- */
  133: 
  134: /*
  135:  * rpc_cli_openClient() - Connect to RPC Server
  136:  *
  137:  * @ProgID = ProgramID for RPC session request
  138:  * @ProcID = ProcessID for RPC session request
  139:  * @netBuf = Network buffer length, if =0 == BUFSIZ (also meaning max RPC packet)
  140:  * @csHost = Host name or IP address for bind server
  141:  * @Port = Port for bind server, if Port == 0 default port is selected
  142:  * return: NULL == error or !=NULL connection to RPC server established
  143:  */
  144: rpc_cli_t *
  145: rpc_cli_openClient(u_int ProgID, u_char ProcID, int netBuf, const char *csHost, u_short Port)
  146: {
  147: 	rpc_cli_t *cli = NULL;
  148: 	io_sockaddr_t sa;
  149: 
  150: 	if (!io_gethostbyname(csHost, Port, &sa))
  151: 		return NULL;
  152: 	if (!Port)
  153: 		Port = RPC_DEFPORT;
  154: 	if (!netBuf)
  155: 		netBuf = BUFSIZ;
  156: 
  157: #ifdef HAVE_SRANDOMDEV
  158: 	srandomdev();
  159: #else
  160: 	time_t tim;
  161: 
  162: 	srandom((time(&tim) ^ getpid()));
  163: #endif
  164: 
  165: 	cli = malloc(sizeof(rpc_cli_t));
  166: 	if (!cli) {
  167: 		LOGERR;
  168: 		return NULL;
  169: 	} else
  170: 		memset(cli, 0, sizeof(rpc_cli_t));
  171: 
  172: 	/* build session */
  173: 	cli->cli_parent = malloc(sizeof(rpc_sess_t));
  174: 	if (!cli->cli_parent) {
  175: 		LOGERR;
  176: 		free(cli);
  177: 		return NULL;
  178: 	} else {
  179: 		((rpc_sess_t*) cli->cli_parent)->sess_version = RPC_VERSION;
  180: 		((rpc_sess_t*) cli->cli_parent)->sess_program = ProgID;
  181: 		((rpc_sess_t*) cli->cli_parent)->sess_process = ProcID;
  182: 	}
  183: 
  184: 	memcpy(&cli->cli_sa, &sa, sizeof cli->cli_sa);
  185: 	AIT_SET_BUF2(&cli->cli_buf, 0, netBuf);
  186: 
  187: 	/* connect to RPC server */
  188: 	cli->cli_sock = socket(cli->cli_sa.sa.sa_family, SOCK_STREAM, 0);
  189: 	if (cli->cli_sock == -1) {
  190: 		LOGERR;
  191: 		AIT_FREE_VAL(&cli->cli_buf);
  192: 		free(cli->cli_parent);
  193: 		free(cli);
  194: 		return NULL;
  195: 	}
  196: 	if (connect(cli->cli_sock, &cli->cli_sa.sa, cli->cli_sa.sa.sa_len) == -1) {
  197: 		LOGERR;
  198: 		AIT_FREE_VAL(&cli->cli_buf);
  199: 		close(cli->cli_sock);
  200: 		free(cli->cli_parent);
  201: 		free(cli);
  202: 		return NULL;
  203: 	} else
  204: 		fcntl(cli->cli_sock, F_SETFL, fcntl(cli->cli_sock, F_GETFL) | O_NONBLOCK);
  205: 
  206: 	return cli;
  207: }
  208: 
  209: /*
  210:  * rpc_cli_closeClient() - Close connection to RPC server and free resources
  211:  *
  212:  * @cli = RPC Client session
  213:  * return: none
  214:  */
  215: void
  216: rpc_cli_closeClient(rpc_cli_t ** __restrict cli)
  217: {
  218: 	if (!cli || !*cli)
  219: 		return;
  220: 
  221: 	shutdown((*cli)->cli_sock, SHUT_RDWR);
  222: 	close((*cli)->cli_sock);
  223: 
  224: 	AIT_FREE_VAL(&(*cli)->cli_buf);
  225: 
  226: 	if ((*cli)->cli_parent)
  227: 		free((*cli)->cli_parent);
  228: 
  229: 	free(*cli);
  230: 	*cli = NULL;
  231: }
  232: 
  233: 
  234: /*
  235:  * rpc_cli_execCall() - Execute RPC call
  236:  *
  237:  * @cli = RPC Client session
  238:  * @noreply = We not want RPC reply
  239:  * @tag = Function tag for execution
  240:  * @in_vars = IN RPC call array of rpc values, may be NULL
  241:  * @out_vars = OUT returned array of rpc values, if !=NULL must be free after use with io_freeVars()
  242:  * return: -1 error or != -1 ok result
  243:  */
  244: int
  245: rpc_cli_execCall(rpc_cli_t *cli, int noreply, u_short tag, 
  246: 		array_t * __restrict in_vars, array_t ** __restrict out_vars)
  247: {
  248: 	struct tagRPCCall *rpc;
  249: 	int ret = 0, wlen = sizeof(struct tagRPCCall);
  250: 	uint16_t crc;
  251: 	u_char *buf;
  252: 	struct pollfd pfd;
  253: 
  254: 	if (!cli) {
  255: 		rpc_SetErr(EINVAL, "Can`t execute call because parameter is null or invalid!");
  256: 		return -1;
  257: 	} else
  258: 		buf = AIT_GET_BUF(&cli->cli_buf);
  259: 	if (out_vars)
  260: 		*out_vars = NULL;
  261: 
  262: 	/* prepare RPC call */
  263: 	rpc = (struct tagRPCCall*) buf;
  264: 	rpc_addPktSession(&rpc->call_session, cli->cli_parent);
  265: 	rpc->call_argc = htons(io_arraySize(in_vars));
  266: 	rpc->call_tag = htons(tag);
  267: 	rpc->call_seq = htons(random() % USHRT_MAX);
  268: 
  269: 	/* set reply */
  270: 	rpc->call_req.flags = noreply ? RPC_NOREPLY : RPC_REPLY;
  271: 
  272: 	if (io_arraySize(in_vars)) {
  273: 		/* marshaling variables */
  274: 		ret = io_vars2buffer(buf + wlen, AIT_LEN(&cli->cli_buf) - wlen, in_vars);
  275: 		if (ret == -1) {
  276: 			rpc_SetErr(EBADRPC, "Failed to prepare RPC packet values");
  277: 			return -1;
  278: 		} else
  279: 			wlen += ret;
  280: 	}
  281: 
  282: 	/* total packet length */
  283: 	rpc->call_len = htons(wlen);
  284: 
  285: 	/* calculate CRC */
  286: 	rpc->call_crc ^= rpc->call_crc;
  287: 	rpc->call_crc = htons(crcFletcher16((u_short*) buf, wlen / 2));
  288: 
  289: 	if ((ret = send(cli->cli_sock, buf, wlen, MSG_NOSIGNAL)) == -1) {
  290: 		LOGERR;
  291: 		return -1;
  292: 	} else if (ret != wlen) {
  293: 		rpc_SetErr(EPROCUNAVAIL, "RPC request, should be send %d bytes, "
  294: 				"really sended %d bytes", wlen, ret);
  295: 		return -1;
  296: 	}
  297: 
  298: 	if (noreply)	/* we not want reply */
  299: 		return 0;
  300: 
  301: 	/* reply from RPC server */
  302: 	pfd.fd = cli->cli_sock;
  303: 	pfd.events = POLLIN | POLLPRI;
  304: 	if ((ret = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) == -1 || 
  305: 			pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
  306: 		if (ret)
  307: 			LOGERR;
  308: 		else
  309: 			rpc_SetErr(ETIMEDOUT, "Timeout, no answer from RPC server");
  310: 
  311: 		return -1;
  312: 	}
  313: 	memset(buf, 0, AIT_LEN(&cli->cli_buf));
  314: 	if ((ret = recv(cli->cli_sock, buf, AIT_LEN(&cli->cli_buf), 0)) < 1) {
  315: 		if (ret)
  316: 			LOGERR;
  317: 		return -1;
  318: 	}
  319: 	if (ret < sizeof(struct tagRPCCall)) {
  320: 		rpc_SetErr(ERPCMISMATCH, "Short RPC packet");
  321: 		return -1;
  322: 	}
  323: 
  324: 	/* calculate CRC */
  325: 	crc = ntohs(rpc->call_crc);
  326: 	rpc->call_crc ^= rpc->call_crc;
  327: 	if (crc != crcFletcher16((u_short*) buf, ret / 2)) {
  328: 		rpc_SetErr(ERPCMISMATCH, "Bad CRC RPC packet");
  329: 		return -1;
  330: 	}
  331: 
  332: 	/* check RPC packet session info */
  333: 	if (rpc_chkPktSession(&rpc->call_session, cli->cli_parent)) {
  334: 		rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session");
  335: 		return -1;
  336: 	} else
  337: 		wlen = sizeof(struct tagRPCCall);
  338: 	if (ntohs(rpc->call_tag) != tag) {
  339: 		rpc_SetErr(ERPCMISMATCH, "Get wrong RPC reply");
  340: 		return -1;
  341: 	}
  342: 	if (ntohl(rpc->call_rep.eno) && ntohl(rpc->call_rep.ret) == -1) {
  343: 		rpc_SetErr(ntohl(rpc->call_rep.eno), "Server side: retcode=%d #%d %s", 
  344: 				ntohl(rpc->call_rep.ret), ntohl(rpc->call_rep.eno), 
  345: 				strerror(ntohl(rpc->call_rep.eno)));
  346: 		return -1;
  347: 	}
  348: 	if (ntohs(rpc->call_argc) * sizeof(ait_val_t) > AIT_LEN(&cli->cli_buf) - wlen) {
  349: 		rpc_SetErr(EMSGSIZE, "Reply RPC packet is too long ...");
  350: 		return -1;
  351: 	}
  352: 
  353: 	/* RPC is OK! Go de-marshaling variables ... */
  354: 	if (out_vars && ntohs(rpc->call_argc)) {
  355: 		*out_vars = io_buffer2vars(buf + wlen, AIT_LEN(&cli->cli_buf) - wlen, 
  356: 				ntohs(rpc->call_argc), 0);
  357: 		if (!*out_vars) {
  358: 			rpc_SetErr(io_GetErrno(), "%s", io_GetError());
  359: 			return -1;
  360: 		}
  361: 	}
  362: 
  363: 	ret = ntohl(rpc->call_rep.ret);
  364: 	return ret;
  365: }
  366: 
  367: /*
  368:  * rpc_cli_ping() - Ping RPC server
  369:  *
  370:  * @cli = connected client
  371:  * return: -1 error or !=-1 ping seq id
  372:  */
  373: inline int
  374: rpc_cli_ping(rpc_cli_t *cli)
  375: {
  376: 	array_t *arr;
  377: 	int ret = 0;
  378: 
  379: 	if (!cli)
  380: 		return -1;
  381: 
  382: 	if (rpc_cli_execCall(cli, RPC_REPLY, CALL_SRVPING, NULL, &arr))
  383: 		return -1;
  384: 	else
  385: 		ret = AIT_GET_U16(io_getVars(arr, 0));
  386: 	io_freeVars(&arr);
  387: 
  388: 	return ret;
  389: }

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