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