Annotation of libaitrpc/src/lists.c, revision 1.8.2.2

1.2       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.8.2.2 ! misho       6: * $Id: lists.c,v 1.8.2.1 2012/05/11 08:46:41 misho Exp $
1.2       misho       7: *
                      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.7       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: */
                     46: #include "global.h"
                     47: 
                     48: 
                     49: /*
1.7       misho      50:  * rpc_srv_allocVars() - Allocate array for call variables
                     51:  *
1.2       misho      52:  * @call = RPC function call
1.6       misho      53:  * @varnum = Number of variables, if ==0 return already allocated variables number
1.4       misho      54:  * return: -1 error, !=-1 return varnum value
1.2       misho      55:  */
1.6       misho      56: static inline int
1.4       misho      57: rpc_srv_allocVars(rpc_func_t * __restrict call, int varnum)
1.2       misho      58: {
1.4       misho      59:        if (!call || varnum < 0) {
1.7       misho      60:                rpc_SetErr(EINVAL, "Invalid parameter can`t allocate variables for RPC call");
1.2       misho      61:                return -1;
1.5       misho      62:        }
                     63: 
                     64:        if (varnum) {
1.6       misho      65:                call->func_vars = io_allocVars(varnum);
1.5       misho      66:                if (!call->func_vars)
                     67:                        return -1;
                     68:        }
                     69: 
                     70:        return io_arraySize(call->func_vars);
1.2       misho      71: }
                     72: 
                     73: /*
1.7       misho      74:  * rpc_srv_getVars() - Get variables array for RPC call
                     75:  *
1.2       misho      76:  * @call = RPC function call
1.4       misho      77:  * @vars = Returned variables array, may be NULL
1.2       misho      78:  * return: -1 error, !=-1 Number of returned variables
                     79:  */
                     80: inline int
1.5       misho      81: rpc_srv_getVars(rpc_func_t * __restrict call, array_t ** __restrict vars)
1.2       misho      82: {
                     83:        if (!call) {
1.7       misho      84:                rpc_SetErr(EINVAL, "Invalid parameter can`t get variables");
1.2       misho      85:                return -1;
                     86:        }
                     87: 
1.4       misho      88:        if (vars)
                     89:                *vars = call->func_vars;
1.5       misho      90:        return io_arraySize(call->func_vars);
1.2       misho      91: }
                     92: 
1.7       misho      93: /* --------------------------------------------------------- */
1.2       misho      94: 
                     95: /*
1.7       misho      96:  * rpc_srv_registerCall() - Register call to RPC server
                     97:  *
1.2       misho      98:  * @srv = RPC Server instance
                     99:  * @csModule = Module name, if NULL self binary
                    100:  * @csFunc = Function name
                    101:  * @args = Number of return function arguments, use for restriction case!
                    102:  * return: -1 error or 0 register ok
                    103:  */
                    104: int
1.7       misho     105: rpc_srv_registerCall(rpc_srv_t * __restrict srv, const char *csModule, 
                    106:                const char *csFunc, u_short args)
1.2       misho     107: {
                    108:        rpc_func_t *func;
                    109: 
                    110:        if (!srv || !csFunc) {
1.7       misho     111:                rpc_SetErr(EINVAL, "Invalid parameter can`t register function");
1.2       misho     112:                return -1;
                    113:        }
                    114:        if (!(func = malloc(sizeof(rpc_func_t)))) {
                    115:                LOGERR;
                    116:                return -1;
1.7       misho     117:        } else
1.2       misho     118:                memset(func, 0, sizeof(rpc_func_t));
1.7       misho     119: 
                    120:        /* calculate hashes */
                    121:        if (rpc_calcHashes(func, csModule, csFunc) == -1) {
                    122:                AIT_FREE_VAL(&func->func_name);
                    123:                AIT_FREE_VAL(&func->func_file);
                    124:                free(func);
                    125:                return -1;
1.2       misho     126:        }
                    127: 
                    128:        func->func_parent = srv;
                    129: 
1.7       misho     130:        /* allocate return variables */
1.4       misho     131:        if (args > 0 && rpc_srv_allocVars(func, args) == -1) {
1.7       misho     132:                AIT_FREE_VAL(&func->func_name);
                    133:                AIT_FREE_VAL(&func->func_file);
1.2       misho     134:                free(func);
                    135:                return -1;
                    136:        }
                    137: 
1.7       misho     138:        /* add to list of functions */
1.2       misho     139:        func->func_next = srv->srv_funcs;
                    140:        srv->srv_funcs = func;
                    141:        return 0;
                    142: }
                    143: 
                    144: /*
1.7       misho     145:  * rpc_srv_unregisterCall() - Unregister call from RPC server
                    146:  *
1.2       misho     147:  * @srv = RPC Server instance
                    148:  * @csModule = Module name, if NULL self binary
                    149:  * @csFunc = Function name
                    150:  * return: -1 error, 0 not found call, 1 unregister ok
                    151:  */
                    152: int
                    153: rpc_srv_unregisterCall(rpc_srv_t * __restrict srv, const char *csModule, const char *csFunc)
                    154: {
                    155:        rpc_func_t func, *f, *curr;
                    156: 
                    157:        if (!srv || !csFunc) {
1.7       misho     158:                rpc_SetErr(EINVAL, "Invalid parameter can`t unregister function");
1.2       misho     159:                return -1;
                    160:        } else
1.7       misho     161:                memset(&func, 0, sizeof func);
1.2       misho     162: 
1.7       misho     163:        /* calculate hashes */
                    164:        if (rpc_calcHashes(&func, csModule, csFunc) == -1)
                    165:                return -1;
1.2       misho     166: 
                    167:        f = rpc_srv_getCall(srv, func.func_tag, func.func_hash);
1.7       misho     168:        AIT_FREE_VAL(&func.func_name);
                    169:        AIT_FREE_VAL(&func.func_file);
1.8.2.1   misho     170:        if (!f)                 /* not found element for unregister */
1.2       misho     171:                return 0;
                    172: 
1.7       misho     173:        /* remove from list of functions */
1.5       misho     174:        if (srv->srv_funcs == f)        /* if is 1st element */
1.2       misho     175:                srv->srv_funcs = srv->srv_funcs->func_next;
1.5       misho     176:        else {
1.2       misho     177:                for (curr = srv->srv_funcs; curr->func_next != f; curr = curr->func_next);
                    178:                curr->func_next = curr->func_next->func_next;
                    179:        }
1.6       misho     180:        io_freeVars(&f->func_vars);
1.7       misho     181:        AIT_FREE_VAL(&f->func_name);
                    182:        AIT_FREE_VAL(&f->func_file);
1.5       misho     183:        free(f);
1.2       misho     184: 
                    185:        return 1;
                    186: }
                    187: 
                    188: /*
1.7       misho     189:  * rpc_srv_getCall()  - Get registered call from RPC server
                    190:  *
1.2       misho     191:  * @srv = RPC Server instance
                    192:  * @tag = tag for function
                    193:  * @hash = hash for function
                    194:  * return: NULL not found call, !=NULL return call
                    195:  */
                    196: inline rpc_func_t *
                    197: rpc_srv_getCall(rpc_srv_t * __restrict srv, uint16_t tag, uint32_t hash)
                    198: {
                    199:        rpc_func_t *f;
                    200: 
                    201:        if (!srv) {
1.7       misho     202:                rpc_SetErr(EINVAL, "Invalid parameter can`t get function");
1.2       misho     203:                return NULL;
                    204:        }
                    205: 
                    206:        for (f = srv->srv_funcs; f; f = f->func_next)
                    207:                if (f->func_tag == tag && f->func_hash == hash)
                    208:                        break;
                    209: 
                    210:        return f;
                    211: }
                    212: 
                    213: /*
1.7       misho     214:  * rpc_srv_getFunc() - Get registered call from RPC server by Name
                    215:  *
1.2       misho     216:  * @srv = RPC Server instance
                    217:  * @csModule = Module name, if NULL self binary
                    218:  * @csFunc = Function name
                    219:  * return: NULL not found call, !=NULL return call
                    220:  */
                    221: rpc_func_t *
                    222: rpc_srv_getFunc(rpc_srv_t * __restrict srv, const char *csModule, const char *csFunc)
                    223: {
1.7       misho     224:        rpc_func_t func, *f = NULL;
1.2       misho     225: 
                    226:        if (!srv || !csFunc) {
1.7       misho     227:                rpc_SetErr(EINVAL, "Invalid parameter can`t get function");
1.2       misho     228:                return NULL;
                    229:        } else
1.7       misho     230:                memset(&func, 0, sizeof(rpc_func_t));
1.2       misho     231: 
1.7       misho     232:        /* calculate hashes */
                    233:        if (rpc_calcHashes(&func, csModule, csFunc) == -1)
                    234:                return NULL;
1.2       misho     235: 
1.7       misho     236:        f = rpc_srv_getCall(srv, func.func_tag, func.func_hash);
                    237: 
                    238:        AIT_FREE_VAL(&func.func_name);
                    239:        AIT_FREE_VAL(&func.func_file);
                    240:        return f;
1.2       misho     241: }
                    242: 
                    243: // ---------------------------------------------------------
                    244: 
                    245: /*
1.7       misho     246:  * rpc_srv_getBLOB() - Get registered BLOB 
                    247:  *
1.2       misho     248:  * @srv = RPC Server instance
                    249:  * @var = hash for variable
                    250:  * return: NULL not found, !=NULL return blob var
                    251:  */
                    252: inline rpc_blob_t *
                    253: rpc_srv_getBLOB(rpc_srv_t * __restrict srv, uint32_t var)
                    254: {
                    255:        rpc_blob_t *b;
                    256: 
                    257:        if (!srv) {
1.7       misho     258:                rpc_SetErr(EINVAL, "Invalid parameter can`t get BLOB variable");
1.2       misho     259:                return NULL;
                    260:        }
                    261: 
                    262:        for (b = srv->srv_blob.blobs; b; b = b->blob_next) {
                    263:                if (b->blob_var == var)
                    264:                        break;
                    265:        }
                    266: 
                    267:        return b;
                    268: }
                    269: 
                    270: /*
1.7       misho     271:  * rpc_srv_registerBLOB() - Register new BLOB to server
                    272:  *
1.2       misho     273:  * @srv = RPC Server instance
                    274:  * @len = BLOB length
                    275:  * return: NULL error or new registered BLOB
                    276:  */
                    277: rpc_blob_t *
                    278: rpc_srv_registerBLOB(rpc_srv_t * __restrict srv, size_t len)
                    279: {
                    280:        rpc_blob_t *blob = NULL;
                    281: 
                    282:        if (!srv || !len) {
1.7       misho     283:                rpc_SetErr(EINVAL, "Invalid parameter can`t register BLOB variable");
1.2       misho     284:                return blob;
                    285:        }
                    286: 
                    287:        blob = rpc_srv_blobCreate(srv, len);
                    288:        if (blob) {
                    289:                blob->blob_next = srv->srv_blob.blobs;
                    290:                srv->srv_blob.blobs = blob;
                    291:        }
                    292: 
                    293:        return blob;
                    294: }
                    295: 
                    296: /*
1.7       misho     297:  * rpc_srv_unregisterBLOB() - Unregister BLOB from server
                    298:  *
1.2       misho     299:  * @srv = RPC Server instance
                    300:  * @var = BLOB Variable for unregister
                    301:  * return: -1 error, 0 not found call, 1 unregister ok
                    302:  */
                    303: int
                    304: rpc_srv_unregisterBLOB(rpc_srv_t * __restrict srv, uint32_t var)
                    305: {
                    306:        rpc_blob_t *b, *curr;
                    307: 
                    308:        if (!srv) {
1.7       misho     309:                rpc_SetErr(EINVAL, "Invalid parameter can`t unregister BLOB variable");
1.2       misho     310:                return -1;
                    311:        }
                    312: 
                    313:        b = rpc_srv_getBLOB(srv, var);
1.3       misho     314:        if (!b)                         /* not found element for unregister */
1.2       misho     315:                return 0;
1.5       misho     316:        /* if BLOB is unmapped force to unmap object */
                    317:        if (b->blob_data)
                    318:                rpc_srv_blobUnmap(b);
1.2       misho     319: 
1.3       misho     320:        if (srv->srv_blob.blobs == b) { /* if is 1st element */
1.2       misho     321:                srv->srv_blob.blobs = srv->srv_blob.blobs->blob_next;
                    322:        } else {
                    323:                for (curr = srv->srv_blob.blobs; curr->blob_next != b; curr = curr->blob_next);
                    324:                curr->blob_next = curr->blob_next->blob_next;
                    325:        }
                    326:        rpc_srv_blobFree(srv, b);
                    327:        free(b);
                    328: 
                    329:        return 1;
                    330: }
1.7       misho     331: 
                    332: /*
                    333:  * rpc_calcHashes() - Calculate hashes for RPC call
                    334:  *
                    335:  * @func = function
                    336:  * @csModule = Module name, if NULL self binary
                    337:  * @csFunc = Function name
                    338:  * return: -1 error or 0 ok
                    339:  */
                    340: int
                    341: rpc_calcHashes(rpc_func_t * __restrict func, const char *csModule, const char *csFunc)
                    342: {
                    343:        char *str = NULL;
                    344:        int len = 0;
                    345: 
                    346:        assert(func && csFunc);
                    347: 
                    348:        /* set function name */
                    349:        AIT_SET_STR(&func->func_name, csFunc);
                    350:        len = strlen(csFunc) + 3;       /* extra 3 bytes, because add string "__" and 0 */
                    351:        /* set module name if exists */
                    352:        if (csModule) {
                    353:                AIT_SET_STR(&func->func_file, csModule);
                    354:                len += strlen(csModule);
                    355:        }
                    356:        /* align len to 2 */
                    357:        len = io_align(len, 1);
                    358: 
                    359:        /* prepare hash source string */
                    360:        str = malloc(len);
                    361:        if (!str) {
                    362:                LOGERR;
                    363:                return -1;
                    364:        } else {
                    365:                memset(str, 0, len);
                    366:                if (csModule)
                    367:                        strlcpy((char*) str, csModule, len);
                    368:                strlcat((char*) str, "__", len);
                    369:                strlcat((char*) str, csFunc, len);
                    370:        }
                    371: 
                    372:        func->func_tag = crcFletcher16((u_short*) str, len / 2);
                    373:        func->func_hash = hash_fnv((char*) str, len);
                    374: 
                    375:        free(str);
                    376:        return len;
                    377: }
                    378: 

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