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

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.2 ! misho       6: * $Id: builtin.c,v 1.7.2.1 2012/05/15 23:28:20 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: 
                     51: 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: 
                     94: 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: 
                    132: 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: 
                    156: 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.1   misho     174: #if 0
1.2       misho     175: int
1.4       misho     176: rpcBLOBServerShutdown(rpc_func_t *call, int ic, array_t *iv)
1.2       misho     177: {
                    178:        rpc_srv_t *srv;
                    179: 
                    180:        RPC_CALLBACK_CHECK_INPUT(call);
                    181:        if (!call->func_parent)
                    182:                return -1;
                    183:        else
                    184:                srv = call->func_parent;
                    185: 
1.3       misho     186:        srv->srv_blob.state = kill;
1.2       misho     187: 
                    188:        return 0;
                    189: }
                    190: 
                    191: int
1.4       misho     192: rpcBLOBServerVars(rpc_func_t *call, int ic, array_t *iv)
1.2       misho     193: {
                    194:        rpc_srv_t *srv;
1.4       misho     195:        array_t *vals;
1.2       misho     196:        rpc_blob_t *b;
                    197:        register int i;
1.4       misho     198:        char *val, str[64];
                    199:        int len;
1.2       misho     200: 
                    201:        RPC_CALLBACK_CHECK_INPUT(call);
                    202:        if (!call->func_parent)
                    203:                return -1;
                    204:        else
                    205:                srv = call->func_parent;
                    206: 
1.4       misho     207:        rpc_srv_getVars(call, &vals);
                    208: 
                    209:        if (srv->srv_blob.state == kill) {
                    210:                AIT_SET_STR(io_array(vals, 0, ait_val_t*), "BLOB Server is killed");
                    211:                return 1;
                    212:        }
                    213:        if (srv->srv_blob.state == disable) {
                    214:                AIT_SET_STR(io_array(vals, 0, ait_val_t*), "BLOB Server is disabled");
                    215:                return 1;
                    216:        }
                    217: 
1.2       misho     218:        for (i = 0, b = srv->srv_blob.blobs; b; i++, b = b->blob_next);
1.4       misho     219:        len = i * sizeof str;
                    220: 
                    221:        if (!len) {
                    222:                AIT_SET_STR(io_array(vals, 0, ait_val_t*), "");
1.2       misho     223:                return 0;
                    224:        }
                    225: 
1.4       misho     226:        if (!(val = malloc(len))) {
                    227:                LOGERR;
                    228:                return -1;
                    229:        } else
                    230:                memset(val, 0, len);
                    231: 
                    232:        for (b = srv->srv_blob.blobs, i = 0; b; i++, b = b->blob_next) {
                    233:                memset(str, 0, sizeof str);
                    234:                snprintf(str, sizeof str, "0x%0X(%lu)=%p ", b->blob_var, (u_long) b->blob_len, b->blob_data);
                    235:                strlcat(val, str, len);
                    236:        }
                    237: 
                    238:        AIT_SET_STR(io_array(vals, 0, ait_val_t*), val);
                    239:        free(val);
1.2       misho     240:        return 0;
                    241: }
                    242: 
                    243: int
1.4       misho     244: rpcBLOBServerState(rpc_func_t *call, int ic, array_t *iv)
1.2       misho     245: {
                    246:        rpc_srv_t *srv;
                    247: 
                    248:        RPC_CALLBACK_CHK_ARGS(call, ic);
                    249:        if (!call->func_parent)
                    250:                return -1;
                    251:        else
                    252:                srv = call->func_parent;
                    253: 
1.4       misho     254:        if (AIT_TYPE(io_array(iv, 0, ait_val_t*)) != i32)
1.2       misho     255:                return -1;
                    256: 
1.4       misho     257:        srv->srv_blob.state = AIT_GET_I32(io_array(iv, 0, ait_val_t*));
1.2       misho     258:        return 0;
                    259: }
                    260: 
                    261: int
1.4       misho     262: rpcBLOBServerClients(rpc_func_t *call, int ic, array_t *iv)
1.2       misho     263: {
                    264:        rpc_srv_t *srv;
1.4       misho     265:        array_t *vals;
1.2       misho     266:        rpc_cli_t *cli;
                    267:        register int i;
1.4       misho     268:        int len;
1.3       misho     269:        const char *str = NULL;
1.4       misho     270:        char *val, wrk[INET6_ADDRSTRLEN];
1.2       misho     271: 
                    272:        RPC_CALLBACK_CHECK_INPUT(call);
                    273:        if (!call->func_parent)
                    274:                return -1;
                    275:        else
                    276:                srv = call->func_parent;
                    277: 
1.4       misho     278:        rpc_srv_getVars(call, &vals);
                    279: 
                    280:        if (srv->srv_blob.state == kill) {
                    281:                AIT_SET_STR(io_array(vals, 0, ait_val_t*), "BLOB Server is killed");
                    282:                return 1;
                    283:        }
                    284:        if (srv->srv_blob.state == disable) {
                    285:                AIT_SET_STR(io_array(vals, 0, ait_val_t*), "BLOB Server is disabled");
                    286:                return 1;
                    287:        }
                    288: 
                    289:        len = srv->srv_numcli * STRSIZ;
                    290:        if (!(val = malloc(len))) {
                    291:                LOGERR;
1.2       misho     292:                return -1;
1.4       misho     293:        } else
                    294:                memset(val, 0, len);
1.2       misho     295: 
1.4       misho     296:        for (i = 0, cli = srv->srv_blob.clients; i < srv->srv_numcli; i++, cli++) {
1.5       misho     297:                if (!cli->cli_sa.sa.sa_family)
1.2       misho     298:                        continue;
                    299: 
1.5       misho     300:                switch (cli->cli_sa.sa.sa_family) {
1.3       misho     301:                        case AF_INET:
1.5       misho     302:                                str = inet_ntop(cli->cli_sa.sa.sa_family, &cli->cli_sa.sin.sin_addr, wrk, sizeof wrk);
1.3       misho     303:                                break;
                    304:                        case AF_INET6:
1.5       misho     305:                                str = inet_ntop(cli->cli_sa.sa.sa_family, &cli->cli_sa.sin6.sin6_addr, wrk, sizeof wrk);
1.3       misho     306:                                break;
                    307:                        case AF_LOCAL:
1.5       misho     308:                                str = cli->cli_sa.sun.sun_path;
1.3       misho     309:                                break;
1.2       misho     310:                }
                    311:                if (str)
1.4       misho     312:                        strlcat(val, (char*) str, len);
1.2       misho     313:                else
1.4       misho     314:                        strlcat(val, "0.0.0.0", len);
                    315:                strlcat(val, " ", len);
1.2       misho     316:        }
                    317: 
1.4       misho     318:        AIT_SET_STR(io_array(vals, 0, ait_val_t*), val);
                    319:        free(val);
1.2       misho     320:        return 0;
                    321: }
1.7.2.1   misho     322: #endif

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