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

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

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