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

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

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