[OpenSER-Devel] Accessing exported functions from another module

Henning Westerholt henning.westerholt at 1und1.de
Thu Jul 10 16:18:17 CEST 2008


On Thursday 10 July 2008, Ardjan Zwartjes wrote:
> I'm currently developing some OpenSER modules and regularly encounter
> situations where I wan't to do things which are already implemented in
> the textops (or other) module(s). Unfortunately the textops module
> doesn't export an API like the TM module to access these functions. Now
> since I'd prefer to keep the existing modules the way they are (so I
> don't want to change the textops module) I see two solutions to access
> these functions:
> 1. Copy the code I need from the textops module to my own module.
> Personally I don't like this solution since duplicating code is almost
> never a good idea.
> 2. Try to access the functions through the module itself.

Hi Ardjan,

both solutions are not optimal. If some common functions need to be accessed 
from more then one module, then they could be moved into the core. If this is 
not possible or feasible, then they should be accessed from the module API 
when available, as you know already. But booth ways needs some changes in 
other/ external parts of the server, unfortunally.

> In order to achieve option 2 I'm trying to to use "find_cmd_export_t" to
> find functions like append_hf and remove_hf. After some trial and error
> I came up with the following sequence of steps to call these functions:
>
> - I get a cmd_export_t* by calling find_cmd_export_t (lets call this
> variable to_cmd_export_t).
> - I fix the params by calling to_cmd_export_t->fixup(msg,params,1);
> - I call the function with
> to_cmd_export_t->function(msg,params[0],NULL);
> - I free the params with to_cmd_export_t->free_fixup(params,1);
>
> Now this doesn't work perfectly, if for instance I use this to call
> append_hf of the textops module I can see that the header is added, but
> sometimes the value is overwritten with other data in the final message.
> So I assume that I'm freeing something that shouldn't be freed,
> unfortunately I got lost in the code while trying to figure out what to
> free and what not.
>
> So this is my question: Can anybody explain how to call exported
> functions from the textop modules without memory leaks or problems as
> described above?
>
> In the (not so unlikely) case that what I'm trying to here is an
> unintended, ugly and stupid abuse of the find_cmd_export_t function ;)
> I'm very interested if there are other ways to add/remove/change headers
> in a module developer friendly way and/or how to call functions normally
> called from the script.

This way of accessing module functions is deprecated, the proper way is to use 
an API. You can take a look at the uac_redirect module:rd_acc_fct to get an 
example. This is the only module that uses find_export this way at the 
moment. I don't see any fixup here, but its possible that this is a bug. I 
looked at some old code here, i also found no fixups there. You should also  
use the wrapper "find_export" instead of the "find_cmd_export_t".

For your case "adding a header" i would simple write my own function. 35 LOCs 
of duplication (e.g. see maxforward:add_maxfwd_header) are IMHO justifiable.

Cheers,

Henning



More information about the Devel mailing list