sr_module.c
3bf76e49
 /* $Id$
  */
 
 #include "sr_module.h"
 #include "dprint.h"
404073d3
 #include "error.h"
3bf76e49
 
 #include <dlfcn.h>
 #include <strings.h>
 #include <stdlib.h>
 
 
031e278e
 struct sr_module* modules=0;
3bf76e49
 
404073d3
 #ifdef STATIC_TM
 	extern struct module_exports* tm_mod_register();
 #endif
6eb22d94
 #ifdef STATIC_MAXFWD
 	extern struct module_exports* maxfwd_mod_register();
 #endif
404073d3
 
 
 /* initializes statically built (compiled in) modules*/
 int init_builtin_modules()
 {
 	#ifdef STATIC_TM
 		register_module(tm_mod_register,"built-in", 0);
 	#endif
4c400453
 	#ifdef STATIC_MAXFWD
6eb22d94
 		register_module(maxfwd_mod_register, "built-in", 0);
4c400453
 	#endif
404073d3
 }
 
 
 
 /* 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 ret;
 	struct module_exports* e;
 	struct sr_module* t, *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");
 		ret=E_OUT_OF_MEM;
 		goto error;
 	}
 	memset(mod,0, sizeof(struct sr_module));
 	mod->path=path;
 	mod->handle=handle;
 	mod->exports=e;
 	mod->next=modules;
 	modules=mod;
 	return 0;
 error:
 	return ret;
 }
 
3bf76e49
 
 
 /* returns 0 on success , <0 on error */
 int load_module(char* path)
 {
 	void* handle;
 	char* error;
404073d3
 	module_register	mod_register;
 	struct sr_module* t;
3bf76e49
 	
 	handle=dlopen(path, RTLD_NOW); /* resolve all symbols now */
 	if (handle==0){
 		LOG(L_ERR, "ERROR: load_module: could not open module <%s>: %s\n",
 					path, dlerror() );
 		goto error;
 	}
 	
 	for(t=modules;t; t=t->next){
 		if (t->handle==handle){
 			LOG(L_WARN, "WARNING: load_module: attempting to load the same"
 						" module twice (%s)\n", path);
 			goto skip;
 		}
 	}
 	/* launch register */
 	mod_register = dlsym(handle, "mod_register");
 	if ( (error =dlerror())!=0 ){
 		LOG(L_ERR, "ERROR: load_module: %s\n", error);
 		goto error1;
 	}
404073d3
 	if (register_module(mod_register, path, handle)<0) goto error1;
3bf76e49
 	return 0;
 
 error1:
 	dlclose(handle);
 error:
 skip:
 	return -1;
 }
 
 
 
 /* searches the module list and returns a pointer to the "name" function or
  * 0 if not found */
34fd2612
 cmd_function find_export(char* name, int param_no)
3bf76e49
 {
 	struct sr_module* t;
 	int r;
 
 	for(t=modules;t;t=t->next){
 		for(r=0;r<t->exports->cmd_no;r++){
34fd2612
 			if((strcmp(name, t->exports->cmd_names[r])==0)&&
 				(t->exports->param_no[r]==param_no) ){
3bf76e49
 				DBG("find_export: found <%s> in module %s [%s]\n",
 						name, t->exports->name, t->path);
 				return t->exports->cmd_pointers[r];
 			}
 		}
 	}
 	DBG("find_export: <%s> not found \n", name);
 	return 0;
 }
 
34fd2612
 
 
 /* finds a module, given a pointer to a module function *
  * returns pointer to module, & if i i!=0, *i=the function index */
 struct sr_module* find_module(void* f, int *i)
 {
 	struct sr_module* t;
 	int r;
 	for (t=modules;t;t=t->next){
 		for(r=0;r<t->exports->cmd_no;r++) 
 			if (f==t->exports->cmd_pointers[r]) {
 				if (i) *i=r;
 				return t;
 			}
 	}
 	return 0;
 }
bf08223a
 
 
 
 void destroy_modules()
 {
 	struct sr_module* t;
 
 	for(t=modules;t;t=t->next)
 		if  ((t->exports)&&(t->exports->destroy_f)) t->exports->destroy_f();
 }