Annotation of libaitrpc/src/builtin.c, revision 1.3.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.3.2.4 ! misho       6: * $Id: builtin.c,v 1.3.2.3 2011/08/31 17:11:58 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: 
                     15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
                     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.3.2.3   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.3.2.3   misho      55:        array_t *vals;
1.1       misho      56:        rpc_cli_t *cli;
                     57:        register int i;
1.3       misho      58:        const char *str = NULL;
1.3.2.4 ! misho      59:        char *val, wrk[INET6_ADDRSTRLEN];
1.1       misho      60:        struct sockaddr_in *s;
                     61:        struct sockaddr_in6 *s6;
1.3       misho      62:        struct sockaddr_un *su;
1.1       misho      63: 
1.2       misho      64:        RPC_CALLBACK_CHECK_INPUT(call);
                     65:        if (!call->func_parent)
1.1       misho      66:                return -1;
                     67:        else
1.2       misho      68:                srv = call->func_parent;
                     69: 
1.3.2.4 ! misho      70:        rpc_srv_getVars(call, &vals);
        !            71: 
        !            72:        if (!(val = calloc(srv->srv_numcli, STRSIZ))) {
        !            73:                LOGERR;
1.1       misho      74:                return -1;
1.3.2.4 ! misho      75:        } else
        !            76:                memset(val, 0, srv->srv_numcli * STRSIZ);
1.1       misho      77: 
1.3.2.3   misho      78:        for (i = 0, cli = srv->srv_clients; i < srv->srv_numcli; i++, cli++) {
1.3.2.4 ! misho      79:                if (!cli->cli_sa.sa_family)
1.1       misho      80:                        continue;
                     81: 
1.3       misho      82:                switch (cli->cli_sa.sa_family) {
                     83:                        case AF_INET:
                     84:                                s = (struct sockaddr_in*) &cli->cli_sa;
                     85:                                str = inet_ntop(cli->cli_sa.sa_family, &s->sin_addr, wrk, sizeof wrk);
                     86:                                break;
                     87:                        case AF_INET6:
                     88:                                s6 = (struct sockaddr_in6*) &cli->cli_sa;
                     89:                                str = inet_ntop(cli->cli_sa.sa_family, &s6->sin6_addr, wrk, sizeof wrk);
                     90:                                break;
                     91:                        case AF_LOCAL:
                     92:                                su = (struct sockaddr_un*) &cli->cli_sa;
                     93:                                str = su->sun_path;
                     94:                                break;
1.1       misho      95:                }
1.2       misho      96:                if (str)
1.3.2.4 ! misho      97:                        strlcat(val, (char*) str, srv->srv_numcli * STRSIZ);
1.2       misho      98:                else
1.3.2.4 ! misho      99:                        strlcat(val, "0.0.0.0", srv->srv_numcli * STRSIZ);
        !           100:                strlcat(val, " ", srv->srv_numcli * STRSIZ);
1.1       misho     101:        }
                    102: 
1.3.2.4 ! misho     103:        AIT_SET_STR(io_array(vals, 0, ait_val_t*), val);
        !           104:        free(val);
1.1       misho     105:        return 0;
                    106: }
                    107: 
                    108: int
1.3.2.3   misho     109: rpcServerCalls(rpc_func_t *call, int ic, array_t *iv)
1.1       misho     110: {
1.2       misho     111:        rpc_srv_t *srv;
1.3.2.3   misho     112:        array_t *vals;
1.1       misho     113:        rpc_func_t *f;
                    114:        register int i;
1.3.2.4 ! misho     115:        int len;
        !           116:        char *val, str[MAXPATHLEN];
1.1       misho     117: 
1.2       misho     118:        RPC_CALLBACK_CHECK_INPUT(call);
                    119:        if (!call->func_parent)
1.1       misho     120:                return -1;
1.2       misho     121:        else
                    122:                srv = call->func_parent;
1.1       misho     123: 
                    124:        for (i = 0, f = srv->srv_funcs; f; i++, f = f->func_next);
1.3.2.4 ! misho     125:        len = i * STRSIZ;
        !           126: 
        !           127:        rpc_srv_getVars(call, &vals);
        !           128: 
        !           129:        if (!(val = malloc(len))) {
        !           130:                LOGERR;
1.1       misho     131:                return -1;
1.3.2.4 ! misho     132:        } else
        !           133:                memset(val, 0, len);
1.1       misho     134: 
1.3.2.3   misho     135:        for (f = srv->srv_funcs, i = 0; f; f = f->func_next) {
1.1       misho     136:                if (*f->func_name) {
1.3       misho     137:                        memset(str, 0, sizeof str);
1.3.2.4 ! misho     138:                        snprintf(str, sizeof str, "/%s/%s(%d); ", 
        !           139:                                        f->func_file, f->func_name, io_arraySize(f->func_vars));
        !           140:                        strlcat(val, str, len);
1.1       misho     141:                }
                    142:        }
                    143: 
1.3.2.4 ! misho     144:        AIT_SET_STR(io_array(vals, 0, ait_val_t*), val);
        !           145:        free(val);
1.1       misho     146:        return 0;
                    147: }
                    148: 
                    149: int
1.3.2.3   misho     150: rpcServerSessions(rpc_func_t *call, int ic, array_t *iv)
1.1       misho     151: {
1.2       misho     152:        rpc_srv_t *srv;
1.3.2.3   misho     153:        array_t *vals;
                    154:        ait_val_t *v;
1.1       misho     155: 
1.2       misho     156:        RPC_CALLBACK_CHECK_INPUT(call);
                    157:        if (!call->func_parent)
1.1       misho     158:                return -1;
1.2       misho     159:        else
                    160:                srv = call->func_parent;
1.1       misho     161: 
1.3.2.4 ! misho     162:        rpc_srv_getVars(call, &vals);
1.1       misho     163: 
1.3.2.3   misho     164:        v = io_array(vals, 0, ait_val_t*);
                    165:        AIT_SET_U8(v, srv->srv_session.sess_version);
                    166:        v = io_array(vals, 1, ait_val_t*);
                    167:        AIT_SET_U32(v, srv->srv_session.sess_program);
                    168:        v = io_array(vals, 2, ait_val_t*);
                    169:        AIT_SET_U32(v, srv->srv_session.sess_process);
                    170:        v = io_array(vals, 3, ait_val_t*);
                    171:        AIT_SET_I32(v, srv->srv_numcli);
1.1       misho     172: 
                    173:        return 0;
                    174: }
1.2       misho     175: 
                    176: int
1.3.2.3   misho     177: rpcServerShutdown(rpc_func_t *call, int ic, array_t *iv)
1.2       misho     178: {
                    179:        rpc_srv_t *srv;
                    180: 
                    181:        RPC_CALLBACK_CHECK_INPUT(call);
                    182:        if (!call->func_parent)
                    183:                return -1;
                    184:        else
                    185:                srv = call->func_parent;
                    186: 
                    187:        pthread_mutex_lock(&srv->srv_mtx);
1.3       misho     188:        srv->srv_kill = kill;
1.2       misho     189:        pthread_mutex_unlock(&srv->srv_mtx);
                    190: 
                    191:        return 0;
                    192: }
                    193: 
                    194: // ----------------------------------------------------
                    195: 
                    196: int
1.3.2.3   misho     197: rpcBLOBServerShutdown(rpc_func_t *call, int ic, array_t *iv)
1.2       misho     198: {
                    199:        rpc_srv_t *srv;
                    200: 
                    201:        RPC_CALLBACK_CHECK_INPUT(call);
                    202:        if (!call->func_parent)
                    203:                return -1;
                    204:        else
                    205:                srv = call->func_parent;
                    206: 
                    207:        pthread_mutex_lock(&srv->srv_blob.mtx);
1.3       misho     208:        srv->srv_blob.state = kill;
1.2       misho     209:        pthread_mutex_unlock(&srv->srv_blob.mtx);
                    210: 
                    211:        return 0;
                    212: }
                    213: 
                    214: int
1.3.2.3   misho     215: rpcBLOBServerVars(rpc_func_t *call, int ic, array_t *iv)
1.2       misho     216: {
                    217:        rpc_srv_t *srv;
1.3.2.3   misho     218:        array_t *vals;
1.2       misho     219:        rpc_blob_t *b;
                    220:        register int i;
1.3.2.4 ! misho     221:        char *val, str[12];
        !           222:        int len;
1.2       misho     223: 
                    224:        RPC_CALLBACK_CHECK_INPUT(call);
                    225:        if (!call->func_parent)
                    226:                return -1;
                    227:        else
                    228:                srv = call->func_parent;
                    229: 
                    230:        pthread_mutex_lock(&srv->srv_blob.mtx);
                    231:        for (i = 0, b = srv->srv_blob.blobs; b; i++, b = b->blob_next);
1.3.2.4 ! misho     232:        len = i * sizeof str;
        !           233: 
        !           234:        rpc_srv_getVars(call, &vals);
        !           235: 
        !           236:        if (!(val = malloc(len))) {
        !           237:                LOGERR;
        !           238:                return -1;
        !           239:        } else
        !           240:                memset(val, 0, len);
1.2       misho     241: 
1.3.2.3   misho     242:        for (b = srv->srv_blob.blobs, i = 0; b; i++, b = b->blob_next) {
1.3.2.4 ! misho     243:                memset(str, 0, sizeof str);
        !           244:                snprintf(str, sizeof str, "0x%0X ", b->blob_var);
        !           245:                strlcat(val, str, len);
1.3.2.3   misho     246:        }
1.2       misho     247:        pthread_mutex_unlock(&srv->srv_blob.mtx);
                    248: 
1.3.2.4 ! misho     249:        AIT_SET_STR(io_array(vals, 0, ait_val_t*), val);
        !           250:        free(val);
1.2       misho     251:        return 0;
                    252: }
                    253: 
                    254: int
1.3.2.3   misho     255: rpcBLOBServerState(rpc_func_t *call, int ic, array_t *iv)
1.2       misho     256: {
                    257:        rpc_srv_t *srv;
                    258: 
                    259:        RPC_CALLBACK_CHK_ARGS(call, ic);
                    260:        if (!call->func_parent)
                    261:                return -1;
                    262:        else
                    263:                srv = call->func_parent;
                    264: 
1.3.2.3   misho     265:        if (AIT_TYPE(io_array(iv, 0, ait_val_t*)) != i32)
1.2       misho     266:                return -1;
                    267: 
1.3.2.3   misho     268:        srv->srv_blob.state = AIT_GET_I32(io_array(iv, 0, ait_val_t*));
1.2       misho     269:        return 0;
                    270: }
                    271: 
                    272: int
1.3.2.3   misho     273: rpcBLOBServerClients(rpc_func_t *call, int ic, array_t *iv)
1.2       misho     274: {
                    275:        rpc_srv_t *srv;
1.3.2.3   misho     276:        array_t *vals;
1.2       misho     277:        rpc_cli_t *cli;
                    278:        register int i;
1.3       misho     279:        const char *str = NULL;
1.3.2.4 ! misho     280:        char *val, wrk[INET6_ADDRSTRLEN];
1.2       misho     281:        struct sockaddr_in *s;
                    282:        struct sockaddr_in6 *s6;
1.3       misho     283:        struct sockaddr_un *su;
1.2       misho     284: 
                    285:        RPC_CALLBACK_CHECK_INPUT(call);
                    286:        if (!call->func_parent)
                    287:                return -1;
                    288:        else
                    289:                srv = call->func_parent;
                    290: 
1.3.2.4 ! misho     291:        rpc_srv_getVars(call, &vals);
        !           292: 
        !           293:        if (!(val = calloc(srv->srv_numcli, STRSIZ))) {
        !           294:                LOGERR;
1.2       misho     295:                return -1;
1.3.2.4 ! misho     296:        } else
        !           297:                memset(val, 0, srv->srv_numcli * STRSIZ);
1.2       misho     298: 
1.3.2.3   misho     299:        for (i = 0, cli = srv->srv_blob.clients; i < srv->srv_numcli; i++, cli++) {
1.3.2.4 ! misho     300:                if (!cli->cli_sa.sa_family)
1.2       misho     301:                        continue;
                    302: 
1.3       misho     303:                switch (cli->cli_sa.sa_family) {
                    304:                        case AF_INET:
                    305:                                s = (struct sockaddr_in*) &cli->cli_sa;
                    306:                                str = inet_ntop(cli->cli_sa.sa_family, &s->sin_addr, wrk, sizeof wrk);
                    307:                                break;
                    308:                        case AF_INET6:
                    309:                                s6 = (struct sockaddr_in6*) &cli->cli_sa;
                    310:                                str = inet_ntop(cli->cli_sa.sa_family, &s6->sin6_addr, wrk, sizeof wrk);
                    311:                                break;
                    312:                        case AF_LOCAL:
                    313:                                su = (struct sockaddr_un*) &cli->cli_sa;
                    314:                                str = su->sun_path;
                    315:                                break;
1.2       misho     316:                }
                    317:                if (str)
1.3.2.4 ! misho     318:                        strlcat(val, (char*) str, srv->srv_numcli * STRSIZ);
1.2       misho     319:                else
1.3.2.4 ! misho     320:                        strlcat(val, "0.0.0.0", srv->srv_numcli * STRSIZ);
        !           321:                strlcat(val, " ", srv->srv_numcli * STRSIZ);
1.2       misho     322:        }
                    323: 
1.3.2.4 ! misho     324:        AIT_SET_STR(io_array(vals, 0, ait_val_t*), val);
        !           325:        free(val);
1.2       misho     326:        return 0;
                    327: }

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