Annotation of libaitrpc/src/builtin.c, revision 1.7.2.8

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.7.2.8 ! misho       6: * $Id: builtin.c,v 1.7.2.7 2012/05/17 08:42:32 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.6       misho      15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
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: /* builtin RPC server functions */
                     50: 
1.7.2.4   misho      51: static int
1.7.2.7   misho      52: rpcServerClients(rpc_func_t *call, struct tagRPCCall *rpc, array_t *iv)
1.1       misho      53: {
1.2       misho      54:        rpc_srv_t *srv;
1.1       misho      55:        rpc_cli_t *cli;
                     56:        register int i;
1.4       misho      57:        int len;
1.3       misho      58:        const char *str = NULL;
1.7.2.1   misho      59:        char *val;
                     60:        ait_val_t v;
1.1       misho      61: 
1.2       misho      62:        RPC_CALLBACK_CHECK_INPUT(call);
1.7.2.6   misho      63:        srv = RPC_FUNC_SERVER(call);
1.2       misho      64: 
1.7.2.1   misho      65:        len = io_arraySize(srv->srv_clients) * STRSIZ;
1.4       misho      66:        if (!(val = malloc(len))) {
                     67:                LOGERR;
1.1       misho      68:                return -1;
1.4       misho      69:        } else
                     70:                memset(val, 0, len);
1.1       misho      71: 
1.7.2.1   misho      72:        for (i = 0; i < io_arraySize(srv->srv_clients); i++) {
                     73:                cli = io_array(srv->srv_clients, i, rpc_cli_t*);
                     74:                if (!cli)
1.1       misho      75:                        continue;
                     76: 
1.7.2.1   misho      77:                str = io_n2addr(&cli->cli_sa, &v);
1.2       misho      78:                if (str)
1.4       misho      79:                        strlcat(val, (char*) str, len);
1.2       misho      80:                else
1.4       misho      81:                        strlcat(val, "0.0.0.0", len);
                     82:                strlcat(val, " ", len);
1.7.2.1   misho      83:                AIT_FREE_VAL(&v);
1.1       misho      84:        }
                     85: 
1.7.2.6   misho      86:        AIT_SET_STR(io_array(RPC_FUNC_RETVARS(call), 0, ait_val_t*), val);
1.4       misho      87:        free(val);
1.1       misho      88:        return 0;
                     89: }
                     90: 
1.7.2.4   misho      91: static int
1.7.2.7   misho      92: rpcServerCalls(rpc_func_t *call, struct tagRPCCall *rpc, array_t *iv)
1.1       misho      93: {
1.2       misho      94:        rpc_srv_t *srv;
1.1       misho      95:        rpc_func_t *f;
1.7.2.1   misho      96:        register int i = 0;
1.4       misho      97:        int len;
                     98:        char *val, str[MAXPATHLEN];
1.1       misho      99: 
1.2       misho     100:        RPC_CALLBACK_CHECK_INPUT(call);
1.7.2.6   misho     101:        srv = RPC_FUNC_SERVER(call);
1.1       misho     102: 
1.7.2.1   misho     103:        TAILQ_FOREACH(f, &srv->srv_funcs, func_node)
                    104:                i++;
1.4       misho     105:        len = i * STRSIZ;
                    106: 
                    107:        if (!(val = malloc(len))) {
                    108:                LOGERR;
1.1       misho     109:                return -1;
1.4       misho     110:        } else
                    111:                memset(val, 0, len);
1.1       misho     112: 
1.7.2.1   misho     113:        TAILQ_FOREACH(f, &srv->srv_funcs, func_node)
                    114:                if (AIT_ADDR(&f->func_name)) {
1.3       misho     115:                        memset(str, 0, sizeof str);
1.7.2.1   misho     116:                        snprintf(str, sizeof str, "/%hu/0x%p(%d); ", AIT_KEY(&f->func_name), 
1.7.2.6   misho     117:                                        AIT_ADDR(&f->func_name), io_arraySize(RPC_FUNC_RETVARS(f)));
1.4       misho     118:                        strlcat(val, str, len);
1.1       misho     119:                }
                    120: 
1.7.2.7   misho     121:        AIT_SET_STR(io_getVars(RPC_FUNC_RETVARS(call), 0), val);
1.4       misho     122:        free(val);
1.1       misho     123:        return 0;
                    124: }
                    125: 
1.7.2.4   misho     126: static int
1.7.2.7   misho     127: rpcServerSessions(rpc_func_t *call, struct tagRPCCall *rpc, array_t *iv)
1.1       misho     128: {
1.2       misho     129:        rpc_srv_t *srv;
1.4       misho     130:        ait_val_t *v;
1.1       misho     131: 
1.2       misho     132:        RPC_CALLBACK_CHECK_INPUT(call);
1.7.2.6   misho     133:        srv = RPC_FUNC_SERVER(call);
1.1       misho     134: 
1.7.2.6   misho     135:        v = io_array(RPC_FUNC_RETVARS(call), 0, ait_val_t*);
1.4       misho     136:        AIT_SET_U8(v, srv->srv_session.sess_version);
1.7.2.6   misho     137:        v = io_array(RPC_FUNC_RETVARS(call), 1, ait_val_t*);
1.4       misho     138:        AIT_SET_U32(v, srv->srv_session.sess_program);
1.7.2.6   misho     139:        v = io_array(RPC_FUNC_RETVARS(call), 2, ait_val_t*);
1.7.2.1   misho     140:        AIT_SET_U8(v, srv->srv_session.sess_process);
1.7.2.6   misho     141:        v = io_array(RPC_FUNC_RETVARS(call), 3, ait_val_t*);
1.7.2.1   misho     142:        AIT_SET_I32(v, io_arraySize(srv->srv_clients));
1.1       misho     143: 
                    144:        return 0;
                    145: }
1.2       misho     146: 
1.7.2.4   misho     147: static int
1.7.2.7   misho     148: rpcServerShutdown(rpc_func_t *call, struct tagRPCCall *rpc, array_t *iv)
1.2       misho     149: {
                    150:        rpc_srv_t *srv;
                    151: 
                    152:        RPC_CALLBACK_CHECK_INPUT(call);
1.7.2.6   misho     153:        srv = RPC_FUNC_SERVER(call);
1.2       misho     154: 
1.7.2.2   misho     155:        srv->srv_kill = 1;
1.2       misho     156: 
                    157:        return 0;
                    158: }
                    159: 
1.7.2.6   misho     160: static int
1.7.2.7   misho     161: rpcServerPing(rpc_func_t *call, struct tagRPCCall *rpc, array_t *iv)
1.7.2.6   misho     162: {
1.7.2.8 ! misho     163:        if (rpc->call_req.flags == RPC_REPLY)
        !           164:                AIT_SET_U16(io_getVars(RPC_FUNC_RETVARS(call), 0), ntohs(rpc->call_seq));
        !           165: 
1.7.2.6   misho     166:        return 0;
                    167: }
                    168: 
1.7.2.1   misho     169: /* ---------------------------------------------------- */
1.2       misho     170: 
1.7.2.4   misho     171: static int
1.7.2.7   misho     172: rpcBLOBServerShutdown(rpc_func_t *call, struct tagRPCCall *rpc, array_t *iv)
1.2       misho     173: {
                    174:        rpc_srv_t *srv;
                    175: 
                    176:        RPC_CALLBACK_CHECK_INPUT(call);
1.7.2.6   misho     177:        srv = RPC_FUNC_SERVER(call);
1.2       misho     178: 
1.7.2.3   misho     179:        srv->srv_blob.kill = 1;
1.2       misho     180: 
                    181:        return 0;
                    182: }
                    183: 
1.7.2.4   misho     184: static int
1.7.2.7   misho     185: rpcBLOBServerVars(rpc_func_t *call, struct tagRPCCall *rpc, array_t *iv)
1.2       misho     186: {
                    187:        rpc_srv_t *srv;
                    188:        rpc_blob_t *b;
1.7.2.3   misho     189:        register int i = 0;
1.4       misho     190:        char *val, str[64];
                    191:        int len;
1.2       misho     192: 
                    193:        RPC_CALLBACK_CHECK_INPUT(call);
1.7.2.6   misho     194:        srv = RPC_FUNC_SERVER(call);
1.2       misho     195: 
1.7.2.3   misho     196:        if (srv->srv_blob.kill) {
1.7.2.6   misho     197:                AIT_SET_STR(io_array(RPC_FUNC_RETVARS(call), 0, ait_val_t*), "BLOB Server is killed");
1.4       misho     198:                return 1;
                    199:        }
                    200: 
1.7.2.3   misho     201:        TAILQ_FOREACH(b, &srv->srv_blob.blobs, blob_node)
                    202:                i++;
1.4       misho     203:        len = i * sizeof str;
                    204: 
                    205:        if (!len) {
1.7.2.6   misho     206:                AIT_SET_STR(io_array(RPC_FUNC_RETVARS(call), 0, ait_val_t*), "");
1.2       misho     207:                return 0;
                    208:        }
                    209: 
1.4       misho     210:        if (!(val = malloc(len))) {
                    211:                LOGERR;
                    212:                return -1;
                    213:        } else
                    214:                memset(val, 0, len);
                    215: 
1.7.2.3   misho     216:        TAILQ_FOREACH(b, &srv->srv_blob.blobs, blob_node) {
1.4       misho     217:                memset(str, 0, sizeof str);
                    218:                snprintf(str, sizeof str, "0x%0X(%lu)=%p ", b->blob_var, (u_long) b->blob_len, b->blob_data);
                    219:                strlcat(val, str, len);
                    220:        }
                    221: 
1.7.2.6   misho     222:        AIT_SET_STR(io_array(RPC_FUNC_RETVARS(call), 0, ait_val_t*), val);
1.4       misho     223:        free(val);
1.2       misho     224:        return 0;
                    225: }
                    226: 
1.7.2.4   misho     227: static int
1.7.2.7   misho     228: rpcBLOBServerClients(rpc_func_t *call, struct tagRPCCall *rpc, array_t *iv)
1.2       misho     229: {
                    230:        rpc_srv_t *srv;
                    231:        rpc_cli_t *cli;
                    232:        register int i;
1.4       misho     233:        int len;
1.3       misho     234:        const char *str = NULL;
1.7.2.3   misho     235:        char *val;
                    236:        ait_val_t v;
1.2       misho     237: 
                    238:        RPC_CALLBACK_CHECK_INPUT(call);
1.7.2.6   misho     239:        srv = RPC_FUNC_SERVER(call);
1.2       misho     240: 
1.7.2.3   misho     241:        if (srv->srv_blob.kill) {
1.7.2.6   misho     242:                AIT_SET_STR(io_array(RPC_FUNC_RETVARS(call), 0, ait_val_t*), "BLOB Server is killed");
1.4       misho     243:                return 1;
                    244:        }
                    245: 
1.7.2.3   misho     246:        len = io_arraySize(srv->srv_blob.clients) * STRSIZ;
1.4       misho     247:        if (!(val = malloc(len))) {
                    248:                LOGERR;
1.2       misho     249:                return -1;
1.4       misho     250:        } else
                    251:                memset(val, 0, len);
1.2       misho     252: 
1.7.2.3   misho     253:        for (i = 0; i < io_arraySize(srv->srv_clients); i++) {
                    254:                cli = io_array(srv->srv_blob.clients, i, rpc_cli_t*);
                    255:                if (!cli)
1.2       misho     256:                        continue;
                    257: 
1.7.2.3   misho     258:                str = io_n2addr(&cli->cli_sa, &v);
1.2       misho     259:                if (str)
1.4       misho     260:                        strlcat(val, (char*) str, len);
1.2       misho     261:                else
1.4       misho     262:                        strlcat(val, "0.0.0.0", len);
                    263:                strlcat(val, " ", len);
1.7.2.3   misho     264:                AIT_FREE_VAL(&v);
1.2       misho     265:        }
                    266: 
1.7.2.6   misho     267:        AIT_SET_STR(io_array(RPC_FUNC_RETVARS(call), 0, ait_val_t*), val);
1.4       misho     268:        free(val);
1.2       misho     269:        return 0;
                    270: }
1.7.2.4   misho     271: 
                    272: /* ----------------------------------------------------------------- */
                    273: 
                    274: /*
1.7.2.7   misho     275:  * rpc_register_srvPing() - Register ping service function
                    276:  *
                    277:  * @srv = RPC server instance
                    278:  * return: -1 error or 0 ok
                    279:  */
                    280: inline int
                    281: rpc_register_srvPing(rpc_srv_t * __restrict srv)
                    282: {
                    283:        if (!srv)
                    284:                return -1;
                    285: 
                    286:        if (rpc_srv_registerCall(srv, CALL_SRVPING, rpcServerPing, 1) < 1)
                    287:                return -1;
                    288: 
                    289:        return 0;
                    290: }
                    291: 
                    292: /*
1.7.2.4   misho     293:  * rpc_register_srvServices() - Register internal service functions
                    294:  *
                    295:  * @srv = RPC server instance
                    296:  * return: -1 error or 0 ok
                    297:  */
                    298: int
                    299: rpc_register_srvServices(rpc_srv_t * __restrict srv)
                    300: {
                    301:        if (!srv)
                    302:                return -1;
                    303: 
                    304:        if (rpc_srv_registerCall(srv, CALL_SRVSHUTDOWN, rpcServerShutdown, 0) < 1)
                    305:                return -1;
                    306:        if (rpc_srv_registerCall(srv, CALL_SRVCLIENTS, rpcServerClients, 1) < 1)
                    307:                return -1;
                    308:        if (rpc_srv_registerCall(srv, CALL_SRVSESSIONS, rpcServerSessions, 4) < 1)
                    309:                return -1;
                    310:        if (rpc_srv_registerCall(srv, CALL_SRVCALLS, rpcServerCalls, 1) < 1)
                    311:                return -1;
                    312: 
                    313:        return 0;
                    314: }
                    315: 
                    316: /*
                    317:  * rpc_register_blobServices() - Register internal service functions
                    318:  *
                    319:  * @srv = RPC server instance
                    320:  * return: -1 error or 0 ok
                    321:  */
                    322: int
                    323: rpc_register_blobServices(rpc_srv_t * __restrict srv)
                    324: {
                    325:        if (!srv)
                    326:                return -1;
                    327: 
                    328:        if (rpc_srv_registerCall(srv, CALL_BLOBSHUTDOWN, rpcBLOBServerShutdown, 0) < 1)
                    329:                return -1;
                    330:        if (rpc_srv_registerCall(srv, CALL_BLOBCLIENTS, rpcBLOBServerClients, 1) < 1)
                    331:                return -1;
                    332:        if (rpc_srv_registerCall(srv, CALL_BLOBVARS, rpcBLOBServerVars, 1) < 1)
                    333:                return -1;
                    334: 
                    335:        return 0;
                    336: }

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