Browse code

rpc: added register function and switched to hash table

- rpc switched to hashtable
- added rpc register function, that can be called from modules not
using ser module interface (e.g. kamailio or future sip-router
module interface).

Andrei Pelinescu-Onciul authored on 11/05/2009 22:39:03
Showing 6 changed files
... ...
@@ -31,6 +31,7 @@
31 31
 #include "mem/mem.h"
32 32
 #include "mem/shm_mem.h"
33 33
 #include "sr_module.h"
34
+#include "rpc_lookup.h"
34 35
 #include "dprint.h"
35 36
 #include "core_cmd.h"
36 37
 #include "globals.h"
... ...
@@ -229,18 +230,10 @@ static const char* system_listMethods_doc[] = {
229 230
 
230 231
 static void system_listMethods(rpc_t* rpc, void* c)
231 232
 {
232
-	struct sr_module* t;
233
-	rpc_export_t* ptr;
234
-
235
-	for(ptr = core_rpc_methods; ptr && ptr->name; ptr++) {
236
-		if (rpc->add(c, "s", ptr->name) < 0) return;
237
-	}
238
-
239
-	for(t = modules; t; t = t->next) {
240
-		if (t->mod_interface_ver!=0) continue;
241
-		for(ptr = t->exports->v0.rpc_methods; ptr && ptr->name; ptr++) {
242
-			if (rpc->add(c, "s", ptr->name) < 0) return;
243
-		}
233
+	int i;
234
+	
235
+	for(i=0; i<rpc_sarray_crt_size; i++){
236
+		if (rpc->add(c, "s", rpc_sarray[i]->name) < 0) return;
244 237
 	}
245 238
 }
246 239
 
... ...
@@ -262,40 +255,25 @@ static const char* system_methodHelp_doc[] = {
262 255
 
263 256
 static void system_methodHelp(rpc_t* rpc, void* c)
264 257
 {
265
-	struct sr_module* t;
266
-	rpc_export_t* ptr;
258
+	rpc_export_t* r;
267 259
 	char* name;
268 260
 
269 261
 	if (rpc->scan(c, "s", &name) < 1) {
270 262
 		rpc->fault(c, 400, "Method Name Expected");
271 263
 		return;
272 264
 	}
273
-
274
-	for(t = modules; t; t = t->next) {
275
-		if (t->mod_interface_ver!=0) continue;
276
-		for(ptr = t->exports->v0.rpc_methods; ptr && ptr->name; ptr++) {
277
-			if (strcmp(name, ptr->name) == 0) {
278
-				if (ptr->doc_str && ptr->doc_str[0]) {
279
-					rpc->add(c, "s", ptr->doc_str[0]);
280
-				} else {
281
-					rpc->add(c, "s", "undocumented");
282
-				}
283
-				return;
284
-			}
265
+	
266
+	r=rpc_lookup(name, strlen(name));
267
+	if (r==0){
268
+		rpc->fault(c, 400, "command not found");
269
+	}else{
270
+		if (r->doc_str && r->doc_str[0]) {
271
+			rpc->add(c, "s", r->doc_str[0]);
272
+		} else {
273
+			rpc->add(c, "s", "undocumented");
285 274
 		}
286 275
 	}
287
-	/* try the core methods too */
288
-	for (ptr=core_rpc_methods;ptr && ptr->name; ptr++){
289
-			if (strcmp(name, ptr->name) == 0) {
290
-				if (ptr->doc_str && ptr->doc_str[0]) {
291
-					rpc->add(c, "s", ptr->doc_str[0]);
292
-				} else {
293
-					rpc->add(c, "s", "undocumented");
294
-				}
295
-				return;
296
-			}
297
-	}
298
-	rpc->fault(c, 400, "command not found");
276
+	return;
299 277
 }
300 278
 
301 279
 
... ...
@@ -701,7 +679,7 @@ static void core_sctpinfo(rpc_t* rpc, void* c)
701 679
 /*
702 680
  * RPC Methods exported by this module
703 681
  */
704
-rpc_export_t core_rpc_methods[] = {
682
+static rpc_export_t core_rpc_methods[] = {
705 683
 	{"system.listMethods",     system_listMethods,     system_listMethods_doc,     RET_ARRAY},
706 684
 	{"system.methodSignature", system_methodSignature, system_methodSignature_doc, 0        },
707 685
 	{"system.methodHelp",      system_methodHelp,      system_methodHelp_doc,      0        },
... ...
@@ -785,6 +763,28 @@ rpc_export_t core_rpc_methods[] = {
785 763
 	{0, 0, 0, 0}
786 764
 };
787 765
 
766
+
767
+
768
+int register_core_rpcs(void)
769
+{
770
+	int i;
771
+	
772
+	i=rpc_register_array(core_rpc_methods);
773
+	if (i<0){
774
+		BUG("failed to register core RPCs\n");
775
+		goto error;
776
+	}else if (i>0){
777
+		ERR("%d duplicate RPCs name detected while registering core RPCs\n",
778
+			 i);
779
+		goto error;
780
+	}
781
+	return 0;
782
+error:
783
+	return -1;
784
+}
785
+
786
+
787
+
788 788
 int rpc_init_time(void)
789 789
 {
790 790
 	char *t;
... ...
@@ -30,8 +30,7 @@
30 30
 
31 31
 #include "rpc.h"
32 32
 
33
-extern rpc_export_t core_rpc_methods[];
34
-
33
+int register_core_rpcs(void);
35 34
 int rpc_init_time(void);
36 35
 
37 36
 #endif /* _CORE_CMD_H */
... ...
@@ -153,6 +153,7 @@
153 153
 #include "sctp_server.h"
154 154
 #endif
155 155
 #include "usr_avp.h"
156
+#include "rpc_lookup.h"
156 157
 #include "core_cmd.h"
157 158
 #include "flags.h"
158 159
 #include "lock_ops_init.h"
... ...
@@ -1630,6 +1631,8 @@ int main(int argc, char** argv)
1630 1631
 	}
1631 1632
 	if (init_routes()<0) goto error;
1632 1633
 	if (init_nonsip_hooks()<0) goto error;
1634
+	if (init_rpcs()<0) goto error;
1635
+	if (register_core_rpcs()!=0) goto error;
1633 1636
 
1634 1637
 	/* Fix the value of cfg_file variable.*/
1635 1638
 	if (fix_cfg_file() < 0) goto error;
1636 1639
new file mode 100644
... ...
@@ -0,0 +1,220 @@
1
+/* 
2
+ * $Id$
3
+ * 
4
+ * Copyright (C) 2009 iptelorg GmbH
5
+ *
6
+ * Permission to use, copy, modify, and distribute this software for any
7
+ * purpose with or without fee is hereby granted, provided that the above
8
+ * copyright notice and this permission notice appear in all copies.
9
+ *
10
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
+ */
18
+/*
19
+ * SER RPC lookup and register functions
20
+ */
21
+/*
22
+ * History:
23
+ * --------
24
+ *  2009-05-11  initial version (andrei)
25
+*/
26
+
27
+#include "rpc.h"
28
+#include "str_hash.h"
29
+#include "ut.h"
30
+
31
+#define RPC_HASH_SIZE	32
32
+#define RPC_SARRAY_SIZE	32 /* initial size */
33
+
34
+#define RPC_COPY_EXPORT
35
+
36
+static struct str_hash_table rpc_hash_table;
37
+
38
+/* array of pointer to rpc exports, sorted after their name
39
+ *  (used by listMethods) */
40
+rpc_export_t** rpc_sarray;
41
+int rpc_sarray_crt_size; /* used */
42
+static int rpc_sarray_max_size; /* number of entries alloc'ed */
43
+
44
+/** init the rpc hash table.
45
+  * @return 0 on success, -1 on error
46
+  */
47
+int init_rpcs()
48
+{
49
+	if (str_hash_alloc(&rpc_hash_table, RPC_HASH_SIZE)<0)
50
+		return -1;
51
+	str_hash_init(&rpc_hash_table);
52
+	rpc_sarray_max_size=RPC_SARRAY_SIZE;
53
+	rpc_sarray=pkg_malloc(sizeof(*rpc_sarray)* rpc_sarray_max_size);
54
+	rpc_sarray_crt_size=0;
55
+	return 0;
56
+}
57
+
58
+
59
+
60
+void destroy_rpcs()
61
+{
62
+	int r;
63
+	struct str_hash_entry* e;
64
+	struct str_hash_entry* bak;
65
+	for (r=0; r<rpc_hash_table.size; r++){
66
+		clist_foreach_safe(&rpc_hash_table.table[r], e, bak, next){
67
+			pkg_free(e);
68
+		}
69
+	}
70
+	pkg_free(rpc_hash_table.table);
71
+	pkg_free(rpc_sarray);
72
+	rpc_hash_table.table=0;
73
+	rpc_hash_table.size=0;
74
+	rpc_sarray=0;
75
+	rpc_sarray_crt_size=0;
76
+	rpc_sarray_max_size=0;
77
+}
78
+
79
+
80
+
81
+/** adds a new rpc to the hash table (no checks).
82
+ * @return 0 on success, -1 on error, 1 on duplicate
83
+ */
84
+static int rpc_hash_add(struct rpc_export* rpc)
85
+{
86
+	struct str_hash_entry* e;
87
+	int name_len;
88
+	int doc0_len, doc1_len;
89
+	struct rpc_export* n_rpc;
90
+	struct rpc_export** r;
91
+	
92
+	name_len=strlen(rpc->name);
93
+	doc0_len=rpc->doc_str[0]?strlen(rpc->doc_str[0]):0;
94
+	doc1_len=rpc->doc_str[1]?strlen(rpc->doc_str[1]):0;
95
+	/* alloc everything into one block */
96
+	e=pkg_malloc(ROUND_POINTER(sizeof(struct str_hash_entry))
97
+#ifdef RPC_COPY_EXPORT
98
+								+ROUND_POINTER(sizeof(*rpc))+2*sizeof(char*)+
99
+								+name_len+1+doc0_len+(rpc->doc_str[0]!=0)
100
+								+doc1_len+(rpc->doc_str[1]!=0)
101
+#endif /* RPC_COPY_EXPORT */
102
+								);
103
+	if (e==0){
104
+		ERR("out of memory\n");
105
+		goto error;
106
+	}
107
+#ifdef RPC_COPY_EXPORT
108
+	n_rpc=(rpc_export_t*)((char*)e+
109
+			ROUND_POINTER(sizeof(struct str_hash_entry)));
110
+	/* copy rpc into n_rpc */
111
+	*n_rpc=*rpc;
112
+	n_rpc->doc_str=(const char**)((char*)n_rpc+ROUND_POINTER(sizeof(*rpc)));
113
+	n_rpc->name=(char*)n_rpc->doc_str+2*sizeof(char*);
114
+	memcpy((char*)n_rpc->name, rpc->name, name_len);
115
+	*((char*)&n_rpc->name[name_len])=0;
116
+	if (rpc->doc_str[0]){
117
+		n_rpc->doc_str[0]=&n_rpc->name[name_len+1];
118
+		memcpy((char*)n_rpc->doc_str[0], rpc->doc_str[0], doc0_len);
119
+		*(char*)&(n_rpc->doc_str[0][doc0_len])=0;
120
+	}else{
121
+		n_rpc->doc_str[0]=0;
122
+	}
123
+	if (rpc->doc_str[1]){
124
+		n_rpc->doc_str[1]=n_rpc->doc_str[0]?&n_rpc->doc_str[0][doc0_len+1]:
125
+							&n_rpc->name[name_len+1];;
126
+		memcpy((char*)n_rpc->doc_str[1], rpc->doc_str[1], doc1_len);
127
+		*(char*)&(n_rpc->doc_str[1][doc1_len])=0;
128
+	}else{
129
+		n_rpc->doc_str[1]=0;
130
+	}
131
+#else /* RPC_COPY_EXPORT */
132
+	n_rpc=rpc;
133
+#endif /* RPC_COPY_EXPORT */
134
+	
135
+	e->key.s=(char*)n_rpc->name;
136
+	e->key.len=name_len;
137
+	e->flags=0;
138
+	e->u.p=n_rpc;
139
+	str_hash_add(&rpc_hash_table, e);
140
+	
141
+	/* insert it into the sorted array */
142
+	if (rpc_sarray_max_size<=rpc_sarray_crt_size){
143
+		/* array must be increased */
144
+		r=pkg_realloc(rpc_sarray, 2*rpc_sarray_max_size*sizeof(*rpc_sarray));
145
+		if (r==0){
146
+			ERR("out of memory while adding RPC to the sorted list\n");
147
+			goto error;
148
+		}
149
+		rpc_sarray=r;
150
+		rpc_sarray_max_size*=2;
151
+	};
152
+	/* insert into array, sorted */
153
+	for (r=rpc_sarray;r<(rpc_sarray+rpc_sarray_crt_size); r++){
154
+		if (strcmp(n_rpc->name, (*r)->name)<0)
155
+			break;
156
+	}
157
+	if (r!=(rpc_sarray+rpc_sarray_crt_size))
158
+		memmove(r+1, r, (int)(long)((char*)(rpc_sarray+rpc_sarray_crt_size)-
159
+											(char*)r));
160
+	rpc_sarray_crt_size++;
161
+	*r=n_rpc;
162
+	return 0;
163
+error:
164
+	return -1;
165
+}
166
+
167
+
168
+
169
+/** lookup an rpc export after its name.
170
+ * @return pointer to rpc export on success, 0 on error
171
+ */
172
+rpc_export_t* rpc_lookup(const char* name, int len)
173
+{
174
+	struct str_hash_entry* e;
175
+	
176
+	e=str_hash_get(&rpc_hash_table, (char*)name, len);
177
+	return e?(rpc_export_t*)e->u.p:0;
178
+}
179
+
180
+
181
+
182
+/** register a new rpc.
183
+ * @return 0 on success, -1 on error, 1 on duplicate
184
+ */
185
+int rpc_register(rpc_export_t* rpc)
186
+{
187
+	
188
+	/* check if the entry is already registered */
189
+	if (rpc_lookup(rpc->name, strlen(rpc->name)))
190
+		return 1;
191
+	if (rpc_hash_add(rpc)!=0) return -1;
192
+	return 0;
193
+}
194
+
195
+
196
+
197
+/** register all the rpc in a null-terminated array.
198
+  * @return 0 on success, >0 if duplicates were found (number of 
199
+  * duplicates), -1 on error
200
+  */
201
+int rpc_register_array(rpc_export_t* rpc_array)
202
+{
203
+	rpc_export_t* rpc;
204
+	int ret,i;
205
+	
206
+	ret=0;
207
+	for (rpc=rpc_array; rpc && rpc->name; rpc++){
208
+		i=rpc_register(rpc);
209
+		if (i!=0){
210
+			if (i<0) goto error;
211
+			ret++;
212
+		}
213
+	}
214
+	return ret;
215
+error:
216
+	return -1;
217
+}
218
+
219
+
220
+/* vi: set ts=4 sw=4 tw=79:ai:cindent: */
0 221
new file mode 100644
... ...
@@ -0,0 +1,46 @@
1
+/* 
2
+ * $Id$
3
+ * 
4
+ * Copyright (C) 2009 iptelorg GmbH
5
+ *
6
+ * Permission to use, copy, modify, and distribute this software for any
7
+ * purpose with or without fee is hereby granted, provided that the above
8
+ * copyright notice and this permission notice appear in all copies.
9
+ *
10
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
+ */
18
+/*
19
+ * SER RPC lookup and register functions
20
+ */
21
+/*
22
+ * History:
23
+ * --------
24
+ *  2009-05-11  initial version (andrei)
25
+*/
26
+
27
+#ifndef __rpc_lookup_h
28
+#define __rpc_lookup_h
29
+
30
+#include "rpc.h"
31
+/* must be exported for listing the rpcs */
32
+extern rpc_export_t** rpc_sarray;
33
+extern int rpc_sarray_crt_size;
34
+
35
+int init_rpcs();
36
+int destroy_rpcs();
37
+
38
+rpc_export_t* rpc_lookup(const char* name, int len);
39
+int rpc_register(rpc_export_t* rpc);
40
+int rpc_register_array(rpc_export_t* rpc_array);
41
+
42
+
43
+
44
+#endif /*__rpc_lookup_h*/
45
+
46
+/* vi: set ts=4 sw=4 tw=79:ai:cindent: */
... ...
@@ -53,6 +53,7 @@
53 53
 #include "flags.h"
54 54
 #include "trim.h"
55 55
 #include "globals.h"
56
+#include "rpc_lookup.h"
56 57
 
57 58
 #include <sys/stat.h>
58 59
 #include <regex.h>
... ...
@@ -147,7 +148,7 @@ int register_builtin_modules()
147 148
 static int register_module(unsigned ver, union module_exports_u* e,
148 149
 					char* path, void* handle)
149 150
 {
150
-	int ret;
151
+	int ret, i;
151 152
 	struct sr_module* mod;
152 153
 
153 154
 	ret=-1;
... ...
@@ -166,8 +167,8 @@ static int register_module(unsigned ver, union module_exports_u* e,
166 167
 	mod->next=modules;
167 168
 	modules=mod;
168 169
 
169
-	/* register module pseudo-variables */
170 170
 	if (ver==1 && e->v1.items) {
171
+		/* register module pseudo-variables for kamailio modules */
171 172
 		LM_DBG("register PV from: %s\n", e->c.name);
172 173
 		if (register_pvars_mod(e->c.name, e->v1.items)!=0) {
173 174
 			LM_ERR("failed to register pseudo-variables for module %s\n",
... ...
@@ -175,6 +176,18 @@ static int register_module(unsigned ver, union module_exports_u* e,
175 176
 			pkg_free(mod);
176 177
 			return -1;
177 178
 		}
179
+	}else if (ver==0 && e->v0.rpc_methods){
180
+		/* register rpcs for ser modules */
181
+		i=rpc_register_array(e->v0.rpc_methods);
182
+		if (i<0){
183
+			ERR("failed to register RPCs for module %s\n", e->c.name);
184
+			goto error;
185
+		}else if (i>0){
186
+			ERR("%d duplicate RPCs name detected while registering RPCs"
187
+					" declared in modules %s\n", i, e->c.name);
188
+			goto error;
189
+		}
190
+		/* i==0 => success */
178 191
 	}
179 192
 
180 193
 	return 0;
... ...
@@ -489,31 +502,7 @@ cmd_function find_export(char* name, int param_no, int flags)
489 502
 
490 503
 rpc_export_t* find_rpc_export(char* name, int flags)
491 504
 {
492
-	struct sr_module* t;
493
-	rpc_export_t* rpc;
494
-
495
-	     /* Scan the list of core methods first, they are always
496
-	      * present
497
-	      */
498
-	for(rpc = core_rpc_methods; rpc && rpc->name; rpc++) {
499
-		if ((strcmp(name, rpc->name) == 0) &&
500
-		    ((rpc->flags & flags) == flags)
501
-		    ) {
502
-			return rpc;
503
-		}
504
-	}
505
-	     /* Continue with modules if not found */
506
-	for(t = modules; t; t = t->next) {
507
-		if (t->mod_interface_ver!=0) continue;
508
-		for(rpc = t->exports->v0.rpc_methods; rpc && rpc->name; rpc++) {
509
-			if ((strcmp(name, rpc->name) == 0) &&
510
-			    ((rpc->flags & flags) == flags)
511
-			   ) {
512
-				return rpc;
513
-			}
514
-		}
515
-	}
516
-	return 0;
505
+	return rpc_lookup((char*)name, strlen(name));
517 506
 }
518 507
 
519 508