Browse code

New module interface

Jan Janak authored on 03/04/2002 21:55:09
Showing 4 changed files
... ...
@@ -88,6 +88,7 @@ CHECK_VIA	check_via
88 88
 LOOP_CHECKS	loop_checks
89 89
 
90 90
 LOADMODULE	loadmodule
91
+MODPARAM        modparam
91 92
 
92 93
 /* values */
93 94
 YES			"yes"|"true"|"on"|"enable"
... ...
@@ -162,6 +163,7 @@ EAT_ABLE	[\ \t\b\r]
162 162
 <INITIAL>{CHECK_VIA}	{ count(); yylval.strval=yytext; return CHECK_VIA; }
163 163
 <INITIAL>{LOOP_CHECKS}	{ count(); yylval.strval=yytext; return LOOP_CHECKS; }
164 164
 <INITIAL>{LOADMODULE}	{ count(); yylval.strval=yytext; return LOADMODULE; }
165
+<INITIAL>{MODPARAM}     { count(); yylval.strval=yytext; return MODPARAM; }
165 166
 
166 167
 <INITIAL>{EQUAL}	{ count(); return EQUAL; }
167 168
 <INITIAL>{EQUAL_T}	{ count(); return EQUAL_T; }
... ...
@@ -18,6 +18,7 @@
18 18
 #include "route.h"
19 19
 #include "dprint.h"
20 20
 #include "sr_module.h"
21
+#include "modparam.h"
21 22
 
22 23
 
23 24
 #ifdef DEBUG_DMALLOC
... ...
@@ -80,6 +81,7 @@ void* f_tmp;
80 80
 %token CHECK_VIA
81 81
 %token LOOP_CHECKS
82 82
 %token LOADMODULE
83
+%token MODPARAM
83 84
 %token MAXBUFFER
84 85
 
85 86
 
... ...
@@ -230,7 +232,18 @@ module_stm:	LOADMODULE STRING	{ DBG("loading module %s\n", $2);
230 230
 								  		yyerror("failed to load module");
231 231
 								  }
232 232
 								}
233
-		 |	LOADMODULE error	{ yyerror("string expected");  }
233
+		 | LOADMODULE error	{ yyerror("string expected");  }
234
+                 | MODPARAM LPAREN STRING COMMA STRING COMMA STRING RPAREN {
235
+			 if (set_mod_param($3, $5, STR_PARAM, $7) != 0) {
236
+				 yyerror("Can't set module parameter");
237
+			 }
238
+		   }
239
+                 | MODPARAM LPAREN STRING COMMA STRING COMMA NUMBER RPAREN {
240
+			 if (set_mod_param($3, $5, INT_PARAM, (void*)$7) != 0) {
241
+				 yyerror("Can't set module parameter");
242
+			 }
243
+		   }
244
+                 | MODPARAM error { yyerror("Invalid arguments"); }
234 245
 		 ;
235 246
 
236 247
 
... ...
@@ -31,32 +31,32 @@ struct sr_module* modules=0;
31 31
 
32 32
 
33 33
 /* initializes statically built (compiled in) modules*/
34
-int init_builtin_modules()
34
+int register_builtin_modules()
35 35
 {
36 36
 	int ret;
37 37
 
38 38
 	ret=0;
39 39
 	#ifdef STATIC_TM
40
-		ret=register_module(tm_mod_register,"built-in", 0);
40
+		ret=register_module(tm_exports,"built-in", 0);
41 41
 		if (ret<0) return ret;
42 42
 	#endif
43 43
 	#ifdef STATIC_MAXFWD
44
-		ret=register_module(maxfwd_mod_register, "built-in", 0);
44
+		ret=register_module(maxfwd_exports, "built-in", 0);
45 45
 		if (ret<0) return ret;
46 46
 	#endif
47 47
 
48 48
 #ifdef STATIC_AUTH
49
-		ret=register_module(tm_mod_register, "built-in", 0);
49
+		ret=register_module(tm_exports, "built-in", 0);
50 50
 		if (ret<0) return ret;
51 51
 #endif
52 52
 
53 53
 #ifdef STATIC_RR
54
-		ret=register_module(rr_mod_register, "built-in", 0);
54
+		ret=register_module(rr_exports, "built-in", 0);
55 55
 		if (ret<0) return ret;
56 56
 #endif
57 57
 
58 58
 #ifdef STATIC_USRLOC
59
-		ret=register_module(usrloc_mod_register, "built-in", 0);
59
+		ret=register_module(usrloc_exports, "built-in", 0);
60 60
 		if (ret<0) return ret;
61 61
 #endif
62 62
 	
... ...
@@ -67,18 +67,13 @@ int init_builtin_modules()
67 67
 
68 68
 /* registers a module,  register_f= module register  functions
69 69
  * returns <0 on error, 0 on success */
70
-int register_module(module_register register_f, char* path, void* handle)
70
+int register_module(struct module_exports* e, char* path, void* handle)
71 71
 {
72 72
 	int ret;
73
-	struct module_exports* e;
74 73
 	struct sr_module* mod;
75 74
 	
76 75
 	ret=-1;
77
-	e=(*register_f)();
78
-	if (e==0){
79
-		LOG(L_ERR, "ERROR: mod_register returned null\n");
80
-		goto error;
81
-	}
76
+
82 77
 	/* add module to the list */
83 78
 	if ((mod=malloc(sizeof(struct sr_module)))==0){
84 79
 		LOG(L_ERR, "load_module: memory allocation failure\n");
... ...
@@ -122,7 +117,7 @@ int load_module(char* path)
122 122
 {
123 123
 	void* handle;
124 124
 	char* error;
125
-	module_register	mod_register;
125
+	struct module_exports* exp;
126 126
 	struct sr_module* t;
127 127
 	
128 128
 	handle=dlopen(path, RTLD_NOW); /* resolve all symbols now */
... ...
@@ -140,12 +135,12 @@ int load_module(char* path)
140 140
 		}
141 141
 	}
142 142
 	/* launch register */
143
-	mod_register = (module_register)dlsym(handle, "mod_register");
143
+	exp = (struct module_exports*)dlsym(handle, "exports");
144 144
 	if ( (error =dlerror())!=0 ){
145 145
 		LOG(L_ERR, "ERROR: load_module: %s\n", error);
146 146
 		goto error1;
147 147
 	}
148
-	if (register_module(mod_register, path, handle)<0) goto error1;
148
+	if (register_module(exp, path, handle)<0) goto error1;
149 149
 	return 0;
150 150
 
151 151
 error1:
... ...
@@ -179,6 +174,28 @@ cmd_function find_export(char* name, int param_no)
179 179
 }
180 180
 
181 181
 
182
+void* find_param_export(char* mod, char* name, modparam_t type)
183
+{
184
+	struct sr_module* t;
185
+	int r;
186
+
187
+	for(t = modules; t; t = t->next) {
188
+		if (strcmp(mod, t->exports->name) == 0) {
189
+			for(r = 0; r < t->exports->par_no; r++) {
190
+				if ((strcmp(name, t->exports->param_names[r]) == 0) &&
191
+				    (t->exports->param_types[r] == type)) {
192
+					DBG("find_param_export: found <%s> in module %s [%s]\n",
193
+					    name, t->exports->name, t->path);
194
+					return t->exports->param_pointers[r];
195
+				}
196
+			}
197
+		}
198
+	}
199
+	DBG("find_param_export: parameter <%s> or module <%s> not found\n", name, mod);
200
+	return 0;
201
+}
202
+
203
+
182 204
 
183 205
 /* finds a module, given a pointer to a module function *
184 206
  * returns pointer to module, & if i i!=0, *i=the function index */
... ...
@@ -205,3 +222,22 @@ void destroy_modules()
205 205
 	for(t=modules;t;t=t->next)
206 206
 		if  ((t->exports)&&(t->exports->destroy_f)) t->exports->destroy_f();
207 207
 }
208
+
209
+
210
+/*
211
+ * Initialize all loaded modules, the initialization
212
+ * is done *AFTER* the configuration file is parsed
213
+ */
214
+int init_modules(void)
215
+{
216
+	struct sr_module* t;
217
+	
218
+	for(t = modules; t; t = t->next) {
219
+		if ((t->exports) && (t->exports->init_f))
220
+			if (t->exports->init_f() != 0) {
221
+				LOG(L_ERR, "init_modules(): Error while initializing module %s\n", t->exports->name);
222
+				return -1;
223
+			}
224
+	}
225
+	return 0;
226
+}
... ...
@@ -15,25 +15,47 @@ typedef  int (*fixup_function)(void** param, int param_no);
15 15
 typedef  int (*response_function)(struct sip_msg*);
16 16
 typedef  void (*onbreak_function)(struct sip_msg*);
17 17
 typedef void (*destroy_function)();
18
+typedef int (*init_function)(void);
18 19
 typedef int (*child_init_function)(int rank);
19 20
 
21
+
22
+typedef enum {
23
+	STR_PARAM,  /* String parameter type */
24
+	INT_PARAM,  /* Integer parameter type */
25
+} modparam_t;       /* Allowed types of parameters */
26
+
27
+
20 28
 struct module_exports{
21
-	char* name; /* null terminated module name */
22
-	char** cmd_names; /* cmd names registered by this modules */
23
-	cmd_function* cmd_pointers; /* pointers to the corresponding functions */
24
-	int* param_no; /* number of parameters used by the function */
29
+	char* name;                     /* null terminated module name */
30
+	char** cmd_names;               /* cmd names registered by this modules */
31
+	cmd_function* cmd_pointers;     /* pointers to the corresponding functions */
32
+	int* param_no;                  /* number of parameters used by the function */
25 33
 	fixup_function* fixup_pointers; /* pointers to functions called to "fix"
26
-										the params, e.g: precompile a re */
27
-	int cmd_no; /* number of registered commands 
28
-				   (size of cmd_{names,pointers}*/
34
+					 * the params, e.g: precompile a re 
35
+					 */
36
+	int cmd_no;       /* number of registered commands
37
+			   * (size of cmd_{names,pointers}
38
+			   */
39
+
40
+	char** param_names;    /* parameter names registered by this modules */
41
+	modparam_t* param_types; /* Type of parameters */
42
+	void** param_pointers; /* Pointers to the corresponding memory locations */
43
+	int par_no;            /* Number of registered parameters */
44
+
45
+
46
+	init_function init_f;         /* Initilization function */
29 47
 	response_function response_f; /* function used for responses,
30
-											   returns yes or no;
31
-									can be null */
32
-	destroy_function destroy_f; /*function called when the module should
33
-								  be "destroyed", e.g: on ser exit;
34
-								  can be null */
48
+				       * returns yes or no;
49
+				       * can be null 
50
+				       */
51
+	destroy_function destroy_f;  /* function called when the module should
52
+				      * be "destroyed", e.g: on ser exit;
53
+				      * can be null 
54
+				      */
35 55
 	onbreak_function onbreak_f;
36
-	child_init_function init_child_f;  /* Function will be called by all processes after the fork */
56
+	child_init_function init_child_f;  /* Function will be called by all 
57
+					    * processes after the fork 
58
+					    */
37 59
 };
38 60
 
39 61
 struct sr_module{
... ...
@@ -45,14 +67,21 @@ struct sr_module{
45 45
  
46 46
 struct sr_module* modules; /* global module list*/
47 47
 
48
-int init_builtin_modules();
49
-int register_module(module_register, char*,  void*);
48
+int register_builtin_modules();
49
+int register_module(struct module_exports*, char*,  void*);
50 50
 int load_module(char* path);
51 51
 cmd_function find_export(char* name, int param_no);
52 52
 struct sr_module* find_module(void *f, int* r);
53 53
 void destroy_modules();
54 54
 int init_child(int rank);
55
+int init_modules(void);
55 56
 
57
+/*
58
+ * Find a parameter with given type and return it's
59
+ * address in memory
60
+ * If there is no such parameter, NULL is returned
61
+ */
62
+void* find_param_export(char* mod, char* name, modparam_t type);
56 63
 
57 64
 /* modules function prototypes:
58 65
  * struct module_exports* mod_register(); (type module_register)