File:  [ELWIX - Embedded LightWeight unIX -] / libaitrpc / src / aitrpc.c
Revision 1.21: download - view: text, annotated - select for diffs - revision graph
Mon Aug 8 13:21:13 2016 UTC (7 years, 10 months ago) by misho
Branches: MAIN
CVS tags: rpc9_5, rpc9_4, RPC9_4, RPC9_3, HEAD
version 9.3

    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: aitrpc.c,v 1.21 2016/08/08 13:21: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 - 2016
   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: #pragma GCC visibility push(hidden)
   50: 
   51: int rpc_Errno;
   52: char rpc_Error[STRSIZ];
   53: 
   54: #pragma GCC visibility pop
   55: 
   56: // rpc_GetErrno() Get error code of last operation
   57: int
   58: rpc_GetErrno()
   59: {
   60: 	return rpc_Errno;
   61: }
   62: 
   63: // rpc_GetError() Get error text of last operation
   64: const char *
   65: rpc_GetError()
   66: {
   67: 	return rpc_Error;
   68: }
   69: 
   70: // rpc_SetErr() Set error to variables for internal use!!!
   71: void
   72: rpc_SetErr(int eno, char *estr, ...)
   73: {
   74: 	va_list lst;
   75: 
   76: 	rpc_Errno = eno;
   77: 	memset(rpc_Error, 0, sizeof rpc_Error);
   78: 	va_start(lst, estr);
   79: 	vsnprintf(rpc_Error, sizeof rpc_Error, estr, lst);
   80: 	va_end(lst);
   81: }
   82: 
   83: 
   84: /*
   85:  * rpc_chkPktSession() - Check RPC session
   86:  *
   87:  * @p = packet session
   88:  * @s = active session
   89:  * return: -1, 1, 2, 3 are errors or 0 ok
   90:  */
   91: int
   92: rpc_chkPktSession(rpc_sess_t *p, rpc_sess_t *s)
   93: {
   94: 	if (!p || !s)
   95: 		return -1;
   96: 
   97: 	if (p->sess_version != s->sess_version)
   98: 		return 1;
   99: 	if (p->sess_instance != s->sess_instance)
  100: 		return 2;
  101: 
  102: 	return 0;
  103: }
  104: 
  105: /*
  106:  * rpc_addPktSession() - Prepare session into network format
  107:  *
  108:  * @p = packet session
  109:  * @s = host session
  110:  * return: -1 error or 0 ok
  111:  */
  112: int
  113: rpc_addPktSession(rpc_sess_t *p, rpc_sess_t *s)
  114: {
  115: 	if (!p || !s)
  116: 		return -1;
  117: 
  118: 	p->sess_version = s->sess_version;
  119: 	p->sess_instance = s->sess_instance;
  120: 
  121: 	return 0;
  122: }
  123: 
  124: /*
  125:  * rpc_Read() - RPC read operation
  126:  *
  127:  * @sock = socket
  128:  * @type = type of socket
  129:  * @flags = receive flags
  130:  * @sa = check client address, if you use udp protocol
  131:  * @pkt = RPC packet
  132:  * return: -1 error, 0 EOF or or >0 readed bytes into buffer
  133:  */
  134: ssize_t
  135: rpc_Read(int sock, int type, int flags, sockaddr_t * __restrict sa, ait_val_t * __restrict pkt)
  136: {
  137: 	struct pollfd pfd;
  138: 	sockaddr_t sa2;
  139: 	socklen_t salen;
  140: 	int ret = 0, hlen, cx = 0;
  141: 	ether_addr_t bcst = {{ [0 ... sizeof bcst - 1] = 0xff }};
  142: 	u_char *buf = AIT_GET_BUF(pkt);
  143: 	size_t blen = AIT_LEN(pkt);
  144: 	struct tagRPCCall *rpc = (struct tagRPCCall *) buf;
  145: #ifndef __linux__
  146: 	struct ether_header *eh;
  147: 	struct bpf_hdr *h;
  148: 	ait_val_t v = AIT_VAL_INIT;
  149: #endif
  150: 
  151: try2read:
  152: 	pfd.fd = sock;
  153: 	pfd.events = POLLIN | POLLPRI;
  154: 	memset(buf, 0, blen);
  155: 	memset(&sa2, 0, sizeof sa2);
  156: 	salen = E_SOCKADDR_MAX;
  157: #ifndef __linux__
  158: 	sa2.ss.ss_len = salen;
  159: #endif
  160: 	if ((ret = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) < 1 || 
  161: 			pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
  162: 		if (ret)
  163: 			LOGERR;
  164: 		else
  165: 			rpc_SetErr(ETIMEDOUT, "Timeout reached! Server not respond");
  166: 		return -1;
  167: 	}
  168: 
  169: 	switch (type) {
  170: 		case SOCK_STREAM:
  171: 			ret = recv(sock, buf, blen, flags);
  172: 			break;
  173: 		case SOCK_EXT:
  174: 			ret = read(sock, buf, blen);
  175: 			break;
  176: #ifndef __linux__
  177: 		case SOCK_BPF:
  178: 			if (!sa) {
  179: 				rpc_SetErr(EINVAL, "Invalid argument(s)!");
  180: 				return -1;
  181: 			}
  182: 
  183: 			AIT_SET_BUF(&v, NULL, blen);
  184: 			h = (struct bpf_hdr*) AIT_GET_BUF(&v);
  185: 
  186: 			ret = read(sock, AIT_GET_BUF(&v), AIT_LEN(&v));
  187: 			if (ret > 0) {
  188: 				ret -= h->bh_hdrlen;
  189: 				if (ret < h->bh_caplen || h->bh_caplen != h->bh_datalen || 
  190: 						ret < ETHER_HDR_LEN + sizeof(struct tagRPCCall)) {
  191: 					AIT_FREE_VAL(&v);
  192: 					if (cx < 3) {
  193: 						cx++;
  194: 						ret ^= ret;
  195: 						goto try2read;	/* wait for known address */
  196: 					} else
  197: 						return -1;
  198: 				}
  199: 				ret = h->bh_caplen;
  200: 				eh = (struct ether_header*) (AIT_GET_BUF(&v) + h->bh_hdrlen);
  201: 				ret -= ETHER_HDR_LEN;
  202: 				if (eh->ether_type != ntohs(RPC_DEFPORT)) {
  203: 					AIT_FREE_VAL(&v);
  204: 					if (cx < 3) {
  205: 						cx++;
  206: 						ret ^= ret;
  207: 						goto try2read;	/* wait for known address */
  208: 					} else
  209: 						return -1;
  210: 				}
  211: 
  212: 				if (!memcmp(bcst.octet, eh->ether_dhost, sizeof bcst) || 
  213: 						!memcmp(bcst.octet, eh->ether_shost, sizeof bcst)) {
  214: 					AIT_FREE_VAL(&v);
  215: 					if (cx < 3) {
  216: 						cx++;
  217: 						ret ^= ret;
  218: 						goto try2read;	/* wait for known address */
  219: 					} else
  220: 						return -1;
  221: 				}
  222: 				memcpy(buf, (u_char*) (eh + 1), MIN(ret, blen));
  223: 				AIT_FREE_VAL(&v);
  224: 			}
  225: 			break;
  226: #endif
  227: 		case SOCK_RAW:
  228: 		case SOCK_DGRAM:
  229: 			if (!sa) {
  230: 				rpc_SetErr(EINVAL, "Invalid argument(s)!");
  231: 				return -1;
  232: 			}
  233: 
  234: 			ret = recvfrom(sock, buf, blen, flags, &sa2.sa, &salen);
  235: 			if (ret > -1 && e_addrcmp(sa, &sa2, 42)) {
  236: 				if (cx < 3) {
  237: 					cx++;
  238: 					ret ^= ret;
  239: 					goto try2read;	/* wait for known address */
  240: 				} else
  241: 					return -1;
  242: 			}
  243: 			break;
  244: 		default:
  245: 			rpc_SetErr(EINVAL, "Invalid argument(s)!");
  246: 			return -1;
  247: 	}
  248: 	if (ret < 0) {
  249: 		LOGERR;
  250: 		return -1;
  251: 	}
  252: 	if (!ret)	/* EOF */
  253: 		return 0;
  254: 
  255: 	/* check RPC packet header */
  256: 	if (type == SOCK_RAW) {
  257: #ifdef IPV6_REMOVE_HEADER
  258: 		hlen = sa->sa.sa_family == AF_INET ? 
  259: 			sizeof(struct ip) : sizeof(struct ip6_hdr);
  260: #else
  261: 		hlen = sa->sa.sa_family == AF_INET ? 
  262: 			sizeof(struct ip) : 0;
  263: #endif
  264: 		ret -= hlen;
  265: 		if (ret > 0)
  266: 			memmove(buf, buf + hlen, blen - hlen);
  267: 	}
  268: 
  269: 	/* 1st read for RPC header */
  270: 	if (ret < sizeof(struct tagRPCCall) || ret < ntohl(rpc->call_len)) {
  271: 		rpc_SetErr(ERPCMISMATCH, "Short RPC packet %d bytes", ret);
  272: 		return -1;
  273: 	}
  274: 	/* check for loop request */
  275: 	if (!(rpc->call_io & RPC_ACK)) {
  276: 		if (cx < 3) {
  277: 			cx++;
  278: 			ret ^= ret;
  279: 			goto try2read;	/* wait for known address */
  280: 		} else {
  281: 			rpc_SetErr(ERPCMISMATCH, "Loop in RPC communication");
  282: 			return -1;
  283: 		}
  284: 	}
  285: 
  286: 	return ret;
  287: }
  288: 
  289: /*
  290:  * rpc_Write() - RPC write operation
  291:  *
  292:  * @sock = socket
  293:  * @type = type of socket
  294:  * @flags = send flags
  295:  * @sa = send to client address, if you use udp protocol
  296:  * @pkt = RPC packet
  297:  * @blen = buffer length
  298:  * return: -1 error, 0 EOF or >0 written bytes into buffer
  299:  */
  300: ssize_t
  301: rpc_Write(int sock, int type, int flags, sockaddr_t * __restrict sa, 
  302: 		ait_val_t * __restrict pkt, size_t blen)
  303: {
  304: 	struct pollfd pfd;
  305: 	int ret = 0;
  306: 	u_char *buf = AIT_GET_BUF(pkt);
  307: #ifndef __linux__
  308: 	ait_val_t v = AIT_VAL_INIT;
  309: 	struct ether_header *eh;
  310: #endif
  311: 
  312: 
  313: 	pfd.fd = sock;
  314: 	pfd.events = POLLOUT;
  315: 	if ((ret = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) < 1 || 
  316: 			pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
  317: 		if (ret > 0)
  318: 			rpc_SetErr(EPIPE, "Disconnected RPC session\n");
  319: 		else
  320: 			LOGERR;
  321: 		return -1;
  322: 	}
  323: 
  324: 	switch (type) {
  325: 		case SOCK_STREAM:
  326: 			ret = send(sock, buf, blen, flags);
  327: 			break;
  328: 		case SOCK_EXT:
  329: 			ret = write(sock, buf, blen);
  330: 			break;
  331: #ifndef __linux__
  332: 		case SOCK_BPF:
  333: 			if (!sa) {
  334: 				rpc_SetErr(EINVAL, "Invalid argument(s)!");
  335: 				return -1;
  336: 			}
  337: 
  338: 			AIT_SET_BUF(&v, NULL, blen + sizeof(struct ether_header));
  339: 			eh = (struct ether_header*) AIT_GET_BUF(&v);
  340: 			memcpy(eh->ether_dhost, LLADDR(&sa->sdl), ETHER_ADDR_LEN);
  341: 			eh->ether_type = htons(RPC_DEFPORT);
  342: 			memcpy(eh + 1, buf, blen);
  343: 			blen += sizeof(struct ether_header);
  344: 
  345: 			ret = write(sock, AIT_GET_BUF(&v), AIT_LEN(&v));
  346: 
  347: 			AIT_FREE_VAL(&v);
  348: 			break;
  349: #endif
  350: 		case SOCK_RAW:
  351: 		case SOCK_DGRAM:
  352: 			if (!sa) {
  353: 				rpc_SetErr(EINVAL, "Invalid argument(s)!");
  354: 				return -1;
  355: 			}
  356: 
  357: 			ret = sendto(sock, buf, blen, flags, &sa->sa, e_addrlen(sa));
  358: 			break;
  359: 		default:
  360: 			rpc_SetErr(EINVAL, "Invalid argument(s)!");
  361: 			return -1;
  362: 	}
  363: 	if (ret < 0) {
  364: 		LOGERR;
  365: 		return -1;
  366: 	}
  367: 	if (!ret)	/* EOF */
  368: 		return 0;
  369: 
  370: 	if (ret != blen) {
  371: 		rpc_SetErr(EPROCUNAVAIL, "RPC request, should be send %d bytes, "
  372: 				"really sended %d bytes", blen, ret);
  373: 		return -1;
  374: 	}
  375: 
  376: 	return ret;
  377: }
  378: 
  379: /*
  380:  * rpc_pktFreeSpace() - Get free space for payload into RPC packet
  381:  *
  382:  * @c = RPC client 
  383:  * return: remains free bytes from packet
  384:  */
  385: size_t
  386: rpc_pktFreeSpace(rpc_cli_t * __restrict c)
  387: {
  388: 	return (sizeof(struct tagRPCCall) + ait_resideVars(RPC_RETVARS(c)));
  389: }

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