File:  [ELWIX - Embedded LightWeight unIX -] / libaitrpc / src / cli.c
Revision 1.13.4.2: download - view: text, annotated - select for diffs - revision graph
Thu Jan 17 16:49:02 2013 UTC (11 years, 5 months ago) by misho
Branches: rpc5_0
Diff to: branchpoint 1.13: preferred, unified
fix issue related to UDP

    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.13.4.2 2013/01/17 16:49:02 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, 2013
   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:  * @ProgID = ProgramID for RPC session request
  152:  * @ProcID = ProcessID for RPC session request
  153:  * @netBuf = Network buffer length (min:512 bytes), if =0 == BUFSIZ (also meaning max RPC packet)
  154:  * @csHost = Host name or IP address for bind server
  155:  * @Port = Port for bind server, if Port == 0 default port is selected
  156:  * @proto = Protocol, if == 0 choose SOCK_STREAM
  157:  * return: NULL == error or !=NULL connection to RPC server established
  158:  */
  159: rpc_cli_t *
  160: rpc_cli_openClient(u_int ProgID, u_char ProcID, int netBuf, const char *csHost, 
  161: 		u_short Port, int proto)
  162: {
  163: 	rpc_cli_t *cli = NULL;
  164: 	sockaddr_t sa = E_SOCKADDR_INIT;
  165: 
  166: 	if (!e_gethostbyname(csHost, Port, &sa))
  167: 		return NULL;
  168: 	if (!Port)
  169: 		Port = RPC_DEFPORT;
  170: 	if (!proto)
  171: 		proto = SOCK_STREAM;
  172: 	if (netBuf < RPC_MIN_BUFSIZ)
  173: 		netBuf = BUFSIZ;
  174: 	else
  175: 		netBuf = E_ALIGN(netBuf, 2);	/* align netBuf length */
  176: 
  177: #ifdef HAVE_SRANDOMDEV
  178: 	srandomdev();
  179: #else
  180: 	time_t tim;
  181: 
  182: 	srandom((time(&tim) ^ getpid()));
  183: #endif
  184: 
  185: 	cli = e_malloc(sizeof(rpc_cli_t));
  186: 	if (!cli) {
  187: 		LOGERR;
  188: 		return NULL;
  189: 	} else
  190: 		memset(cli, 0, sizeof(rpc_cli_t));
  191: 
  192: 	/* build session */
  193: 	cli->cli_parent = e_malloc(sizeof(rpc_sess_t));
  194: 	if (!cli->cli_parent) {
  195: 		LOGERR;
  196: 		e_free(cli);
  197: 		return NULL;
  198: 	} else {
  199: 		((rpc_sess_t*) cli->cli_parent)->sess_version = RPC_VERSION;
  200: 		((rpc_sess_t*) cli->cli_parent)->sess_program = ProgID;
  201: 		((rpc_sess_t*) cli->cli_parent)->sess_process = ProcID;
  202: 	}
  203: 
  204: 	cli->cli_id = proto;
  205: 	memcpy(&cli->cli_sa, &sa, sizeof cli->cli_sa);
  206: 	AIT_SET_BUFSIZ(&cli->cli_buf, 0, netBuf);
  207: 
  208: 	/* connect to RPC server */
  209: 	cli->cli_sock = socket(cli->cli_sa.sa.sa_family, cli->cli_id, 0);
  210: 	if (cli->cli_sock == -1) {
  211: 		LOGERR;
  212: 		goto err;
  213: 	}
  214: 	if (setsockopt(cli->cli_sock, SOL_SOCKET, SO_SNDBUF, 
  215: 				&netBuf, sizeof netBuf) == -1) {
  216: 		LOGERR;
  217: 		goto err;
  218: 	}
  219: 	if (setsockopt(cli->cli_sock, SOL_SOCKET, SO_RCVBUF, 
  220: 				&netBuf, sizeof netBuf) == -1) {
  221: 		LOGERR;
  222: 		goto err;
  223: 	}
  224: 	if (cli->cli_id == SOCK_STREAM)
  225: 		if (connect(cli->cli_sock, &cli->cli_sa.sa, cli->cli_sa.sa.sa_len) == -1) {
  226: 			LOGERR;
  227: 			goto err;
  228: 		}
  229: 
  230: 	fcntl(cli->cli_sock, F_SETFL, fcntl(cli->cli_sock, F_GETFL) | O_NONBLOCK);
  231: 	return cli;
  232: err:
  233: 	AIT_FREE_VAL(&cli->cli_buf);
  234: 	if (cli->cli_sock > 2)
  235: 		close(cli->cli_sock);
  236: 	e_free(cli->cli_parent);
  237: 	e_free(cli);
  238: 	return NULL;
  239: }
  240: 
  241: /*
  242:  * rpc_cli_closeClient() - Close connection to RPC server and free resources
  243:  *
  244:  * @cli = RPC Client session
  245:  * return: none
  246:  */
  247: void
  248: rpc_cli_closeClient(rpc_cli_t ** __restrict cli)
  249: {
  250: 	if (!cli || !*cli)
  251: 		return;
  252: 
  253: 	if ((*cli)->cli_id == SOCK_STREAM)
  254: 		shutdown((*cli)->cli_sock, SHUT_RDWR);
  255: 	close((*cli)->cli_sock);
  256: 
  257: 	AIT_FREE_VAL(&(*cli)->cli_buf);
  258: 
  259: 	if ((*cli)->cli_parent)
  260: 		e_free((*cli)->cli_parent);
  261: 
  262: 	e_free(*cli);
  263: 	*cli = NULL;
  264: }
  265: 
  266: 
  267: /*
  268:  * rpc_cli_execCall() - Execute RPC call
  269:  *
  270:  * @cli = RPC Client session
  271:  * @noreply = We not want RPC reply
  272:  * @tag = Function tag for execution
  273:  * @in_vars = IN RPC call array of rpc values, may be NULL
  274:  * @out_vars = OUT returned array of rpc values, if !=NULL must be free after use with ait_freeVars()
  275:  * return: -1 error or != -1 ok result
  276:  */
  277: int
  278: rpc_cli_execCall(rpc_cli_t *cli, int noreply, u_short tag, 
  279: 		array_t * __restrict in_vars, array_t ** __restrict out_vars)
  280: {
  281: 	struct tagRPCCall *rpc;
  282: 	int ret = 0, wlen = sizeof(struct tagRPCCall);
  283: 	uint16_t crc;
  284: 	u_char *buf;
  285: 	struct pollfd pfd;
  286: 	sockaddr_t sa;
  287: 	socklen_t salen;
  288: 
  289: 	if (!cli) {
  290: 		rpc_SetErr(EINVAL, "Can`t execute call because parameter is null or invalid!");
  291: 		return -1;
  292: 	} else
  293: 		buf = AIT_GET_BUF(&cli->cli_buf);
  294: 	if (out_vars)
  295: 		*out_vars = NULL;
  296: 
  297: 	memset(&sa, 0, sizeof sa);
  298: 
  299: 	/* prepare RPC call */
  300: 	rpc = (struct tagRPCCall*) buf;
  301: 	rpc_addPktSession(&rpc->call_session, cli->cli_parent);
  302: 	rpc->call_argc = htons(array_Size(in_vars));
  303: 	rpc->call_tag = htons(tag);
  304: 	rpc->call_seq = htons(random() % USHRT_MAX);
  305: 
  306: 	/* set reply */
  307: 	rpc->call_req.flags = noreply ? RPC_NOREPLY : RPC_REPLY;
  308: 
  309: 	if (array_Size(in_vars)) {
  310: 		/* marshaling variables */
  311: 		ret = ait_vars2buffer(buf + wlen, AIT_LEN(&cli->cli_buf) - wlen, in_vars);
  312: 		if (ret == -1) {
  313: 			rpc_SetErr(EBADRPC, "Failed to prepare RPC packet values");
  314: 			return -1;
  315: 		} else
  316: 			wlen += ret;
  317: 	}
  318: 
  319: 	/* total packet length */
  320: 	rpc->call_len = htons(wlen);
  321: 
  322: 	/* calculate CRC */
  323: 	rpc->call_crc ^= rpc->call_crc;
  324: 	rpc->call_crc = htons(crcFletcher16((u_short*) buf, wlen / 2));
  325: 
  326: 	pfd.fd = cli->cli_sock;
  327: 	pfd.events = POLLOUT;
  328: 	if ((ret = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) < 1 || 
  329: 			pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
  330: 		if (ret)
  331: 			LOGERR;
  332: 		else
  333: 			rpc_SetErr(ETIMEDOUT, "Timeout, can't send to RPC server");
  334: 		return -1;
  335: 	}
  336: 	do {
  337: 		if (cli->cli_id == SOCK_STREAM)
  338: 			ret = send(cli->cli_sock, buf, wlen, MSG_NOSIGNAL);
  339: 		else
  340: 			ret = sendto(cli->cli_sock, buf, wlen, MSG_NOSIGNAL, 
  341: 					&cli->cli_sa.sa, cli->cli_sa.sa.sa_len);
  342: 		if (ret == -1) {
  343: 			if (errno == EAGAIN)
  344: 				continue;
  345: 			LOGERR;
  346: 			return -1;
  347: 		} else if (ret != wlen) {
  348: 			rpc_SetErr(EPROCUNAVAIL, "RPC request, should be send %d bytes, "
  349: 					"really sended %d bytes", wlen, ret);
  350: 			return -1;
  351: 		}
  352: 	} while (0);
  353: 
  354: 	if (noreply)	/* we not want reply */
  355: 		return 0;
  356: 
  357: 	wlen = 0;
  358: 	/* reply from RPC server */
  359: 	pfd.events = POLLIN | POLLPRI;
  360: 	do {
  361: 		if ((ret = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) < 1 || 
  362: 				pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
  363: 			if (ret) {
  364: 				LOGERR;
  365: 			} else {
  366: 				if (wlen++ < 7)
  367: 					continue;
  368: 				else
  369: 					rpc_SetErr(ETIMEDOUT, "Timeout, no answer from RPC server");
  370: 			}
  371: 
  372: 			return -1;
  373: 		}
  374: 
  375: 		memset(buf, 0, AIT_LEN(&cli->cli_buf));
  376: 		if (cli->cli_id == SOCK_STREAM)
  377: 			ret = recv(cli->cli_sock, buf, AIT_LEN(&cli->cli_buf), 0);
  378: 		else {
  379: 			salen = sa.ss.ss_len = sizeof(struct sockaddr_storage);
  380: 			ret = recvfrom(cli->cli_sock, buf, AIT_LEN(&cli->cli_buf), 0, 
  381: 					&sa.sa, &salen);
  382: 		}
  383: 		if (ret < 1) {
  384: 			if (ret) {
  385: 				if (errno == EAGAIN)
  386: 					continue;
  387: 				else
  388: 					LOGERR;
  389: 			}
  390: 			return -1;
  391: 		}
  392: 
  393: 		if (cli->cli_id == SOCK_DGRAM) {
  394: 			/* check for response from known address */
  395: 			if (e_addrcmp(&cli->cli_sa, &sa, 42)) {
  396: 				rpc_SetErr(ERPCMISMATCH, "Received RPC response from unknown address");
  397: 				continue;
  398: 			}
  399: 		}
  400: 	} while (0);
  401: 	if (ret < sizeof(struct tagRPCCall)) {
  402: 		rpc_SetErr(ERPCMISMATCH, "Short RPC packet %d bytes", ret);
  403: 		return -1;
  404: 	}
  405: 
  406: 	/* calculate CRC */
  407: 	crc = ntohs(rpc->call_crc);
  408: 	rpc->call_crc ^= rpc->call_crc;
  409: 	if (crc != crcFletcher16((u_short*) buf, ntohs(rpc->call_len) / 2)) {
  410: 		rpc_SetErr(ERPCMISMATCH, "Bad CRC RPC packet");
  411: 		return -1;
  412: 	}
  413: 
  414: 	/* check RPC packet session info */
  415: 	if (rpc_chkPktSession(&rpc->call_session, cli->cli_parent)) {
  416: 		rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session");
  417: 		return -1;
  418: 	} else
  419: 		wlen = sizeof(struct tagRPCCall);
  420: 	if (ntohs(rpc->call_tag) != tag) {
  421: 		rpc_SetErr(ERPCMISMATCH, "Get wrong RPC reply");
  422: 		return -1;
  423: 	}
  424: 	if (ntohl(rpc->call_rep.eno) && ntohl(rpc->call_rep.ret) == -1) {
  425: 		rpc_SetErr(ntohl(rpc->call_rep.eno), "Server side: retcode=%d #%d %s", 
  426: 				ntohl(rpc->call_rep.ret), ntohl(rpc->call_rep.eno), 
  427: 				strerror(ntohl(rpc->call_rep.eno)));
  428: 		return -1;
  429: 	}
  430: 	if (ntohs(rpc->call_argc) * sizeof(ait_val_t) > AIT_LEN(&cli->cli_buf) - wlen) {
  431: 		rpc_SetErr(EMSGSIZE, "Reply RPC packet is too long ...");
  432: 		return -1;
  433: 	}
  434: 
  435: 	/* RPC is OK! Go de-marshaling variables ... */
  436: 	if (out_vars && ntohs(rpc->call_argc)) {
  437: #ifdef CLI_RES_ZCOPY
  438: 		*out_vars = ait_buffer2vars(buf + wlen, AIT_LEN(&cli->cli_buf) - wlen, 
  439: 				ntohs(rpc->call_argc), 42);
  440: #else
  441: 		*out_vars = ait_buffer2vars(buf + wlen, AIT_LEN(&cli->cli_buf) - wlen, 
  442: 				ntohs(rpc->call_argc), 0);
  443: #endif
  444: 		if (!*out_vars) {
  445: 			rpc_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
  446: 			return -1;
  447: 		}
  448: 	}
  449: 
  450: 	ret = ntohl(rpc->call_rep.ret);
  451: 	return ret;
  452: }
  453: 
  454: /*
  455:  * rpc_cli_freeCall() - Free resouce allocated by RPC call
  456:  *
  457:  * @out_vars = Returned array with variables from RPC call
  458:  * return: none
  459:  */
  460: inline void
  461: rpc_cli_freeCall(array_t ** __restrict out_vars)
  462: {
  463: #ifdef CLI_RES_ZCOPY
  464: 	array_Destroy(out_vars);
  465: #else
  466: 	ait_freeVars(out_vars);
  467: #endif
  468: }
  469: 
  470: /*
  471:  * rpc_cli_ping() - Ping RPC server
  472:  *
  473:  * @cli = connected client
  474:  * return: -1 error or !=-1 ping seq id
  475:  */
  476: inline int
  477: rpc_cli_ping(rpc_cli_t *cli)
  478: {
  479: 	int ret = 0;
  480: 	array_t *arr = NULL;
  481: 
  482: 	if (!cli)
  483: 		return -1;
  484: 
  485: 	if (rpc_cli_execCall(cli, RPC_REPLY, CALL_SRVPING, NULL, &arr))
  486: 		return -1;
  487: 	else
  488: 		ret = AIT_GET_U16(array(arr, 0, ait_val_t*));
  489: 	rpc_cli_freeCall(&arr);
  490: 
  491: 	return ret;
  492: }

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