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

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

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