Annotation of libaitrpc/src/lists.c, revision 1.4.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.4.2.2 ! misho       6: * $Id: lists.c,v 1.4.2.1 2011/08/29 23:26:56 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: 
                     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: */
                     46: #include "global.h"
                     47: 
                     48: 
                     49: /*
1.4       misho      50:  * rpc_srv_returnVars() Init return variables for RPC call and zeroed values
1.2       misho      51:                                        (for safe handling return values, use this!)
                     52:  * @call = RPC function call
1.4       misho      53:  * @varnum = Number of return variables
                     54:  * return: NULL error, !=NULL array with return values for RPC call with varnum items
1.2       misho      55:  */
1.4.2.2 ! misho      56: inline array_t *
1.4       misho      57: rpc_srv_returnVars(rpc_func_t * __restrict call, int varnum)
1.2       misho      58: {
1.4.2.2 ! misho      59:        array_t *v = NULL;
1.2       misho      60: 
1.4       misho      61:        if (rpc_srv_allocVars(call, varnum) == -1)
1.2       misho      62:                return NULL;
1.4       misho      63:        if (rpc_srv_getVars(call, &v) == -1)
1.2       misho      64:                return NULL;
                     65: 
                     66:        return v;
                     67: }
                     68: 
                     69: /*
1.4       misho      70:  * rpc_srv_allocVars() Allocate array for call variables, 
                     71:                                if already allocated memory for RPC call reallocate used space
1.2       misho      72:  * @call = RPC function call
1.4       misho      73:  * @varnum = Number of variables, if ==0 free previous allocated variables
                     74:  * return: -1 error, !=-1 return varnum value
1.2       misho      75:  */
                     76: inline int
1.4       misho      77: rpc_srv_allocVars(rpc_func_t * __restrict call, int varnum)
1.2       misho      78: {
1.4.2.2 ! misho      79:        register int i;
        !            80:        ait_val_t *v;
1.2       misho      81: 
1.4       misho      82:        if (!call || varnum < 0) {
                     83:                rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t allocate variables for RPC call...\n");
1.2       misho      84:                return -1;
1.4.2.2 ! misho      85:        }
1.2       misho      86: 
1.4.2.2 ! misho      87:        if (call->func_vars)
        !            88:                for (i = 0; i < io_arraySize(call->func_vars); i++)
        !            89:                        if (io_arrayGet(call->func_vars, i))
        !            90:                                AIT_FREE_VAL(io_array(call->func_vars, i, ait_val_t*));
        !            91:        io_arrayFree(call->func_vars);
        !            92:        io_arrayDestroy(&call->func_vars);
        !            93: 
        !            94:        if (varnum) {
        !            95:                if (!(call->func_vars = io_arrayInit(varnum)))
1.2       misho      96:                        return -1;
1.4.2.2 ! misho      97: 
        !            98:                for (i = 0; i < io_arraySize(call->func_vars); i++) {
        !            99:                        v = malloc(sizeof(ait_val_t));
        !           100:                        if (!v) {
        !           101:                                LOGERR;
        !           102:                                rpc_srv_freeVars(call);
        !           103:                                return -1;
        !           104:                        } else {
        !           105:                                memset(v, 0, sizeof(ait_val_t));
        !           106:                                io_arraySet(call->func_vars, i, v);
        !           107:                        }
        !           108:                }
        !           109: 
        !           110:                varnum = io_arraySize(call->func_vars);
1.2       misho     111:        }
                    112: 
1.4       misho     113:        return varnum;
1.2       misho     114: }
                    115: 
                    116: /*
1.4       misho     117:  * rpc_srv_zeroVars() Clean values from variables of RPC call
1.2       misho     118:  * @call = RPC function call
                    119:  * return: -1 error, !=-1 Returned number of cleaned RPC variables
                    120:  */
                    121: inline int
1.4       misho     122: rpc_srv_zeroVars(rpc_func_t * __restrict call)
1.2       misho     123: {
                    124:        if (!call) {
1.4       misho     125:                rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t delete variables ...\n");
1.2       misho     126:                return -1;
                    127:        }
                    128: 
1.4.2.2 ! misho     129:        io_arrayFree(call->func_vars);
        !           130:        io_arrayZero(call->func_vars);
        !           131:        return io_arraySize(call->func_vars);
1.2       misho     132: }
                    133: 
                    134: /*
1.4       misho     135:  * rpc_srv_copyVars() Copy variables for RPC call to new variable array
1.2       misho     136:  * @call = RPC function call
1.4       misho     137:  * @newvars = New allocated variables array, must be free after use
1.2       misho     138:  * return: -1 error, !=-1 Returned number of copied RPC variables
                    139:  */
                    140: inline int
1.4.2.2 ! misho     141: rpc_srv_copyVars(rpc_func_t * __restrict call, array_t ** __restrict newvars)
1.2       misho     142: {
1.4       misho     143:        if (!call || !newvars) {
                    144:                rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t copy variables to new array\n");
1.2       misho     145:                return -1;
                    146:        }
                    147: 
1.4.2.2 ! misho     148:        if (io_arrayCopy(newvars, call->func_vars) == -1)
1.2       misho     149:                return -1;
                    150: 
1.4.2.2 ! misho     151:        return io_arraySize(*newvars);
1.2       misho     152: }
                    153: 
                    154: /*
1.4       misho     155:  * rpc_srv_getVars() Get variables array for RPC call
1.2       misho     156:  * @call = RPC function call
1.4       misho     157:  * @vars = Returned variables array, may be NULL
1.2       misho     158:  * return: -1 error, !=-1 Number of returned variables
                    159:  */
                    160: inline int
1.4.2.2 ! misho     161: rpc_srv_getVars(rpc_func_t * __restrict call, array_t ** __restrict vars)
1.2       misho     162: {
                    163:        if (!call) {
1.4       misho     164:                rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t get variables ...\n");
1.2       misho     165:                return -1;
                    166:        }
                    167: 
1.4       misho     168:        if (vars)
                    169:                *vars = call->func_vars;
1.4.2.2 ! misho     170:        return (call->func_vars ? io_arraySize(call->func_vars) : 0);
1.2       misho     171: }
                    172: 
                    173: // ---------------------------------------------------------
                    174: 
                    175: /*
                    176:  * rpc_srv_registerCall() Register call to RPC server
                    177:  * @srv = RPC Server instance
                    178:  * @csModule = Module name, if NULL self binary
                    179:  * @csFunc = Function name
                    180:  * @args = Number of return function arguments, use for restriction case!
                    181:  * return: -1 error or 0 register ok
                    182:  */
                    183: int
1.4.2.2 ! misho     184: rpc_srv_registerCall(rpc_srv_t * __restrict srv, const char *csModule, const char *csFunc, u_short args)
1.2       misho     185: {
                    186:        rpc_func_t *func;
                    187:        u_char str[MAXPATHLEN + UCHAR_MAX + 1];
                    188: 
1.4       misho     189:        memset(str, 0, sizeof str);
1.2       misho     190:        if (!srv || !csFunc) {
                    191:                rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t register function to RPC server ...\n");
                    192:                return -1;
                    193:        }
                    194:        if (!(func = malloc(sizeof(rpc_func_t)))) {
                    195:                LOGERR;
                    196:                return -1;
                    197:        } else {
                    198:                memset(func, 0, sizeof(rpc_func_t));
1.4       misho     199:                strlcpy((char*) func->func_name, csFunc, sizeof func->func_name);
1.2       misho     200:        }
                    201:        if (csModule) {
1.4       misho     202:                strlcpy((char*) func->func_file, csModule, sizeof func->func_file);
                    203:                strlcpy((char*) str, csModule, sizeof str);
1.2       misho     204:        }
1.4       misho     205:        strlcat((char*) str, "__", sizeof str);
                    206:        strlcat((char*) str, csFunc, sizeof str);
1.2       misho     207: 
1.4       misho     208:        func->func_tag = crcFletcher16((u_short*) str, sizeof str / 2);
                    209:        func->func_hash = hash_fnv((char*) str, sizeof str);
1.2       misho     210: 
                    211:        func->func_parent = srv;
                    212: 
1.4       misho     213:        if (args > 0 && rpc_srv_allocVars(func, args) == -1) {
1.2       misho     214:                free(func);
                    215:                return -1;
                    216:        }
                    217: 
                    218:        pthread_mutex_lock(&srv->srv_mtx);
                    219:        func->func_next = srv->srv_funcs;
                    220:        srv->srv_funcs = func;
                    221:        pthread_mutex_unlock(&srv->srv_mtx);
                    222:        return 0;
                    223: }
                    224: 
                    225: /*
                    226:  * rpc_srv_unregisterCall() Unregister call from RPC server
                    227:  * @srv = RPC Server instance
                    228:  * @csModule = Module name, if NULL self binary
                    229:  * @csFunc = Function name
                    230:  * return: -1 error, 0 not found call, 1 unregister ok
                    231:  */
                    232: int
                    233: rpc_srv_unregisterCall(rpc_srv_t * __restrict srv, const char *csModule, const char *csFunc)
                    234: {
                    235:        rpc_func_t func, *f, *curr;
                    236:        u_char str[MAXPATHLEN + UCHAR_MAX + 1];
                    237: 
                    238:        memset(&func, 0, sizeof(rpc_func_t));
1.4       misho     239:        memset(str, 0, sizeof str);
1.2       misho     240:        if (!srv || !csFunc) {
                    241:                rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t unregister function from RPC server ...\n");
                    242:                return -1;
                    243:        } else
1.4       misho     244:                strlcpy((char*) func.func_name, csFunc, sizeof func.func_name);
1.2       misho     245:        if (csModule) {
1.4       misho     246:                strlcpy((char*) func.func_file, csModule, sizeof func.func_file);
                    247:                strlcpy((char*) str, csModule, sizeof str);
1.2       misho     248:        }
1.4       misho     249:        strlcat((char*) str, "__", sizeof str);
                    250:        strlcat((char*) str, csFunc, sizeof str);
1.2       misho     251: 
1.4       misho     252:        func.func_tag = crcFletcher16((u_short*) str, sizeof str / 2);
                    253:        func.func_hash = hash_fnv((char*) str, sizeof str);
1.2       misho     254: 
                    255:        f = rpc_srv_getCall(srv, func.func_tag, func.func_hash);
1.3       misho     256:        if (!f)                         /* not found element for unregister */
1.2       misho     257:                return 0;
                    258: 
                    259:        pthread_mutex_lock(&srv->srv_mtx);
1.3       misho     260:        if (srv->srv_funcs == f) {      /* if is 1st element */
1.2       misho     261:                srv->srv_funcs = srv->srv_funcs->func_next;
                    262: 
1.4       misho     263:                if (f->func_args && f->func_vars)
                    264:                        free(f->func_vars);
1.2       misho     265:                free(f);
                    266:        } else {
                    267:                for (curr = srv->srv_funcs; curr->func_next != f; curr = curr->func_next);
                    268:                curr->func_next = curr->func_next->func_next;
                    269: 
1.4       misho     270:                if (f->func_args && f->func_vars)
                    271:                        free(f->func_vars);
1.2       misho     272:                free(f);
                    273:        }
                    274:        pthread_mutex_unlock(&srv->srv_mtx);
                    275: 
                    276:        return 1;
                    277: }
                    278: 
                    279: /*
                    280:  * rpc_srv_getCall() Get registered call from RPC server
                    281:  * @srv = RPC Server instance
                    282:  * @tag = tag for function
                    283:  * @hash = hash for function
                    284:  * return: NULL not found call, !=NULL return call
                    285:  */
                    286: inline rpc_func_t *
                    287: rpc_srv_getCall(rpc_srv_t * __restrict srv, uint16_t tag, uint32_t hash)
                    288: {
                    289:        rpc_func_t *f;
                    290: 
                    291:        if (!srv) {
                    292:                rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t get function from RPC server ...\n");
                    293:                return NULL;
                    294:        }
                    295: 
                    296:        for (f = srv->srv_funcs; f; f = f->func_next)
                    297:                if (f->func_tag == tag && f->func_hash == hash)
                    298:                        break;
                    299: 
                    300:        return f;
                    301: }
                    302: 
                    303: /*
                    304:  * rpc_srv_getFunc() Get registered call from RPC server by Name
                    305:  * @srv = RPC Server instance
                    306:  * @csModule = Module name, if NULL self binary
                    307:  * @csFunc = Function name
                    308:  * return: NULL not found call, !=NULL return call
                    309:  */
                    310: rpc_func_t *
                    311: rpc_srv_getFunc(rpc_srv_t * __restrict srv, const char *csModule, const char *csFunc)
                    312: {
                    313:        rpc_func_t func;
                    314:        u_char str[MAXPATHLEN + UCHAR_MAX + 1];
                    315: 
                    316:        memset(&func, 0, sizeof(rpc_func_t));
1.4       misho     317:        memset(str, 0, sizeof str);
1.2       misho     318:        if (!srv || !csFunc) {
                    319:                rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t get function from RPC server ...\n");
                    320:                return NULL;
                    321:        } else
1.4       misho     322:                strlcpy((char*) func.func_name, csFunc, sizeof func.func_name);
1.2       misho     323:        if (csModule) {
1.4       misho     324:                strlcpy((char*) func.func_file, csModule, sizeof func.func_file);
                    325:                strlcpy((char*) str, csModule, sizeof str);
1.2       misho     326:        }
1.4       misho     327:        strlcat((char*) str, "__", sizeof str);
                    328:        strlcat((char*) str, csFunc, sizeof str);
1.2       misho     329: 
1.4       misho     330:        func.func_tag = crcFletcher16((u_short*) str, sizeof str / 2);
                    331:        func.func_hash = hash_fnv((char*) str, sizeof str);
1.2       misho     332: 
                    333:        return rpc_srv_getCall(srv, func.func_tag, func.func_hash);
                    334: }
                    335: 
                    336: // ---------------------------------------------------------
                    337: 
                    338: /*
                    339:  * rpc_srv_getBLOB() Get registered BLOB 
                    340:  * @srv = RPC Server instance
                    341:  * @var = hash for variable
                    342:  * return: NULL not found, !=NULL return blob var
                    343:  */
                    344: inline rpc_blob_t *
                    345: rpc_srv_getBLOB(rpc_srv_t * __restrict srv, uint32_t var)
                    346: {
                    347:        rpc_blob_t *b;
                    348: 
                    349:        if (!srv) {
                    350:                rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t get variable from BLOB server ...\n");
                    351:                return NULL;
                    352:        }
                    353: 
                    354:        pthread_mutex_lock(&srv->srv_blob.mtx);
                    355:        for (b = srv->srv_blob.blobs; b; b = b->blob_next) {
                    356:                if (b->blob_var == var)
                    357:                        break;
                    358:        }
                    359:        pthread_mutex_unlock(&srv->srv_blob.mtx);
                    360: 
                    361:        return b;
                    362: }
                    363: 
                    364: /*
                    365:  * rpc_srv_registerBLOB() Register new BLOB to server
                    366:  * @srv = RPC Server instance
                    367:  * @len = BLOB length
                    368:  * return: NULL error or new registered BLOB
                    369:  */
                    370: rpc_blob_t *
                    371: rpc_srv_registerBLOB(rpc_srv_t * __restrict srv, size_t len)
                    372: {
                    373:        rpc_blob_t *blob = NULL;
                    374: 
                    375:        if (!srv || !len) {
                    376:                rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t register BLOB to server ...\n");
                    377:                return blob;
                    378:        }
                    379: 
                    380:        blob = rpc_srv_blobCreate(srv, len);
                    381:        if (blob) {
                    382:                pthread_mutex_lock(&srv->srv_blob.mtx);
                    383:                blob->blob_next = srv->srv_blob.blobs;
                    384:                srv->srv_blob.blobs = blob;
                    385:                pthread_mutex_unlock(&srv->srv_blob.mtx);
                    386:        }
                    387: 
                    388:        return blob;
                    389: }
                    390: 
                    391: /*
                    392:  * rpc_srv_unregisterBLOB() Unregister BLOB from server
                    393:  * @srv = RPC Server instance
                    394:  * @var = BLOB Variable for unregister
                    395:  * return: -1 error, 0 not found call, 1 unregister ok
                    396:  */
                    397: int
                    398: rpc_srv_unregisterBLOB(rpc_srv_t * __restrict srv, uint32_t var)
                    399: {
                    400:        rpc_blob_t *b, *curr;
                    401: 
                    402:        if (!srv) {
                    403:                rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t unregister BLOB from server ...\n");
                    404:                return -1;
                    405:        }
                    406: 
                    407:        b = rpc_srv_getBLOB(srv, var);
1.3       misho     408:        if (!b)                         /* not found element for unregister */
1.2       misho     409:                return 0;
                    410: 
                    411:        pthread_mutex_lock(&srv->srv_blob.mtx);
1.3       misho     412:        if (srv->srv_blob.blobs == b) { /* if is 1st element */
1.2       misho     413:                srv->srv_blob.blobs = srv->srv_blob.blobs->blob_next;
                    414:        } else {
                    415:                for (curr = srv->srv_blob.blobs; curr->blob_next != b; curr = curr->blob_next);
                    416:                curr->blob_next = curr->blob_next->blob_next;
                    417:        }
                    418:        rpc_srv_blobFree(srv, b);
                    419:        free(b);
                    420:        pthread_mutex_unlock(&srv->srv_blob.mtx);
                    421: 
                    422:        return 1;
                    423: }

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