瀏覽代碼

New module interface

Jan Janak 23 年之前
父節點
當前提交
31309a3af4
共有 4 個文件被更改,包括 112 次插入32 次删除
  1. 2 0
      cfg.lex
  2. 14 1
      cfg.y
  3. 52 16
      sr_module.c
  4. 44 15
      sr_module.h

+ 2 - 0
cfg.lex

@@ -88,6 +88,7 @@ CHECK_VIA	check_via
 LOOP_CHECKS	loop_checks
 
 LOADMODULE	loadmodule
+MODPARAM        modparam
 
 /* values */
 YES			"yes"|"true"|"on"|"enable"
@@ -162,6 +163,7 @@ EAT_ABLE	[\ \t\b\r]
 <INITIAL>{CHECK_VIA}	{ count(); yylval.strval=yytext; return CHECK_VIA; }
 <INITIAL>{LOOP_CHECKS}	{ count(); yylval.strval=yytext; return LOOP_CHECKS; }
 <INITIAL>{LOADMODULE}	{ count(); yylval.strval=yytext; return LOADMODULE; }
+<INITIAL>{MODPARAM}     { count(); yylval.strval=yytext; return MODPARAM; }
 
 <INITIAL>{EQUAL}	{ count(); return EQUAL; }
 <INITIAL>{EQUAL_T}	{ count(); return EQUAL_T; }

+ 14 - 1
cfg.y

@@ -18,6 +18,7 @@
 #include "route.h"
 #include "dprint.h"
 #include "sr_module.h"
+#include "modparam.h"
 
 
 #ifdef DEBUG_DMALLOC
@@ -80,6 +81,7 @@ void* f_tmp;
 %token CHECK_VIA
 %token LOOP_CHECKS
 %token LOADMODULE
+%token MODPARAM
 %token MAXBUFFER
 
 
@@ -230,7 +232,18 @@ module_stm:	LOADMODULE STRING	{ DBG("loading module %s\n", $2);
 								  		yyerror("failed to load module");
 								  }
 								}
-		 |	LOADMODULE error	{ yyerror("string expected");  }
+		 | LOADMODULE error	{ yyerror("string expected");  }
+                 | MODPARAM LPAREN STRING COMMA STRING COMMA STRING RPAREN {
+			 if (set_mod_param($3, $5, STR_PARAM, $7) != 0) {
+				 yyerror("Can't set module parameter");
+			 }
+		   }
+                 | MODPARAM LPAREN STRING COMMA STRING COMMA NUMBER RPAREN {
+			 if (set_mod_param($3, $5, INT_PARAM, (void*)$7) != 0) {
+				 yyerror("Can't set module parameter");
+			 }
+		   }
+                 | MODPARAM error { yyerror("Invalid arguments"); }
 		 ;
 
 

+ 52 - 16
sr_module.c

@@ -31,32 +31,32 @@ struct sr_module* modules=0;
 
 
 /* initializes statically built (compiled in) modules*/
-int init_builtin_modules()
+int register_builtin_modules()
 {
 	int ret;
 
 	ret=0;
 	#ifdef STATIC_TM
-		ret=register_module(tm_mod_register,"built-in", 0);
+		ret=register_module(tm_exports,"built-in", 0);
 		if (ret<0) return ret;
 	#endif
 	#ifdef STATIC_MAXFWD
-		ret=register_module(maxfwd_mod_register, "built-in", 0);
+		ret=register_module(maxfwd_exports, "built-in", 0);
 		if (ret<0) return ret;
 	#endif
 
 #ifdef STATIC_AUTH
-		ret=register_module(tm_mod_register, "built-in", 0);
+		ret=register_module(tm_exports, "built-in", 0);
 		if (ret<0) return ret;
 #endif
 
 #ifdef STATIC_RR
-		ret=register_module(rr_mod_register, "built-in", 0);
+		ret=register_module(rr_exports, "built-in", 0);
 		if (ret<0) return ret;
 #endif
 
 #ifdef STATIC_USRLOC
-		ret=register_module(usrloc_mod_register, "built-in", 0);
+		ret=register_module(usrloc_exports, "built-in", 0);
 		if (ret<0) return ret;
 #endif
 	
@@ -67,18 +67,13 @@ int init_builtin_modules()
 
 /* registers a module,  register_f= module register  functions
  * returns <0 on error, 0 on success */
-int register_module(module_register register_f, char* path, void* handle)
+int register_module(struct module_exports* e, char* path, void* handle)
 {
 	int ret;
-	struct module_exports* e;
 	struct sr_module* mod;
 	
 	ret=-1;
-	e=(*register_f)();
-	if (e==0){
-		LOG(L_ERR, "ERROR: mod_register returned null\n");
-		goto error;
-	}
+
 	/* add module to the list */
 	if ((mod=malloc(sizeof(struct sr_module)))==0){
 		LOG(L_ERR, "load_module: memory allocation failure\n");
@@ -122,7 +117,7 @@ int load_module(char* path)
 {
 	void* handle;
 	char* error;
-	module_register	mod_register;
+	struct module_exports* exp;
 	struct sr_module* t;
 	
 	handle=dlopen(path, RTLD_NOW); /* resolve all symbols now */
@@ -140,12 +135,12 @@ int load_module(char* path)
 		}
 	}
 	/* launch register */
-	mod_register = (module_register)dlsym(handle, "mod_register");
+	exp = (struct module_exports*)dlsym(handle, "exports");
 	if ( (error =dlerror())!=0 ){
 		LOG(L_ERR, "ERROR: load_module: %s\n", error);
 		goto error1;
 	}
-	if (register_module(mod_register, path, handle)<0) goto error1;
+	if (register_module(exp, path, handle)<0) goto error1;
 	return 0;
 
 error1:
@@ -179,6 +174,28 @@ cmd_function find_export(char* name, int param_no)
 }
 
 
+void* find_param_export(char* mod, char* name, modparam_t type)
+{
+	struct sr_module* t;
+	int r;
+
+	for(t = modules; t; t = t->next) {
+		if (strcmp(mod, t->exports->name) == 0) {
+			for(r = 0; r < t->exports->par_no; r++) {
+				if ((strcmp(name, t->exports->param_names[r]) == 0) &&
+				    (t->exports->param_types[r] == type)) {
+					DBG("find_param_export: found <%s> in module %s [%s]\n",
+					    name, t->exports->name, t->path);
+					return t->exports->param_pointers[r];
+				}
+			}
+		}
+	}
+	DBG("find_param_export: parameter <%s> or module <%s> not found\n", name, mod);
+	return 0;
+}
+
+
 
 /* finds a module, given a pointer to a module function *
  * returns pointer to module, & if i i!=0, *i=the function index */
@@ -205,3 +222,22 @@ void destroy_modules()
 	for(t=modules;t;t=t->next)
 		if  ((t->exports)&&(t->exports->destroy_f)) t->exports->destroy_f();
 }
+
+
+/*
+ * Initialize all loaded modules, the initialization
+ * is done *AFTER* the configuration file is parsed
+ */
+int init_modules(void)
+{
+	struct sr_module* t;
+	
+	for(t = modules; t; t = t->next) {
+		if ((t->exports) && (t->exports->init_f))
+			if (t->exports->init_f() != 0) {
+				LOG(L_ERR, "init_modules(): Error while initializing module %s\n", t->exports->name);
+				return -1;
+			}
+	}
+	return 0;
+}

+ 44 - 15
sr_module.h

@@ -15,25 +15,47 @@ typedef  int (*fixup_function)(void** param, int param_no);
 typedef  int (*response_function)(struct sip_msg*);
 typedef  void (*onbreak_function)(struct sip_msg*);
 typedef void (*destroy_function)();
+typedef int (*init_function)(void);
 typedef int (*child_init_function)(int rank);
 
+
+typedef enum {
+	STR_PARAM,  /* String parameter type */
+	INT_PARAM,  /* Integer parameter type */
+} modparam_t;       /* Allowed types of parameters */
+
+
 struct module_exports{
-	char* name; /* null terminated module name */
-	char** cmd_names; /* cmd names registered by this modules */
-	cmd_function* cmd_pointers; /* pointers to the corresponding functions */
-	int* param_no; /* number of parameters used by the function */
+	char* name;                     /* null terminated module name */
+	char** cmd_names;               /* cmd names registered by this modules */
+	cmd_function* cmd_pointers;     /* pointers to the corresponding functions */
+	int* param_no;                  /* number of parameters used by the function */
 	fixup_function* fixup_pointers; /* pointers to functions called to "fix"
-										the params, e.g: precompile a re */
-	int cmd_no; /* number of registered commands 
-				   (size of cmd_{names,pointers}*/
+					 * the params, e.g: precompile a re 
+					 */
+	int cmd_no;       /* number of registered commands
+			   * (size of cmd_{names,pointers}
+			   */
+
+	char** param_names;    /* parameter names registered by this modules */
+	modparam_t* param_types; /* Type of parameters */
+	void** param_pointers; /* Pointers to the corresponding memory locations */
+	int par_no;            /* Number of registered parameters */
+
+
+	init_function init_f;         /* Initilization function */
 	response_function response_f; /* function used for responses,
-											   returns yes or no;
-									can be null */
-	destroy_function destroy_f; /*function called when the module should
-								  be "destroyed", e.g: on ser exit;
-								  can be null */
+				       * returns yes or no;
+				       * can be null 
+				       */
+	destroy_function destroy_f;  /* function called when the module should
+				      * be "destroyed", e.g: on ser exit;
+				      * can be null 
+				      */
 	onbreak_function onbreak_f;
-	child_init_function init_child_f;  /* Function will be called by all processes after the fork */
+	child_init_function init_child_f;  /* Function will be called by all 
+					    * processes after the fork 
+					    */
 };
 
 struct sr_module{
@@ -45,14 +67,21 @@ struct sr_module{
  
 struct sr_module* modules; /* global module list*/
 
-int init_builtin_modules();
-int register_module(module_register, char*,  void*);
+int register_builtin_modules();
+int register_module(struct module_exports*, char*,  void*);
 int load_module(char* path);
 cmd_function find_export(char* name, int param_no);
 struct sr_module* find_module(void *f, int* r);
 void destroy_modules();
 int init_child(int rank);
+int init_modules(void);
 
+/*
+ * Find a parameter with given type and return it's
+ * address in memory
+ * If there is no such parameter, NULL is returned
+ */
+void* find_param_export(char* mod, char* name, modparam_t type);
 
 /* modules function prototypes:
  * struct module_exports* mod_register(); (type module_register)