Annotation of libaitrpc/src/aitrpc.c, revision 1.21.4.1

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

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