Kamailio: modifying “expires” attribute

Have you ever needed to change the expires attribute of the Contact header in a REGISTER message because you don’t use usrloc or registrar modules? Well, if that is your case, this code snippet may be helpful for you:

But why so complicated? Two things.

– We use a custom module for registration. It’s written in C and the expires attr is generated by the Authorization authority.
– Save(“location”) of usrloc module automatically sends the 200 OK with the correct headers. We do not have this in hand for usage.

This might not be the easiest way of doing it, but it works, serves the purpose and could be used as an alternative 🙂

Kamailio: shared memory hashtable

Using shared memory in Kamailio is fundamental and sometimes inevitable. Luckily, Kamailio’s core abstract us from A LOT of the complexity behind this kind of memory management and help us build a ready to use in-memory hash table in no time!

First of all, you need to include the str_hash header file but it is probably implicitly included by some other mandatory header files.

For this example, I’ll store the hashtable’s (HT from now on) address inside a global variable visible from the entire module. Being that the case, I need to create it as follows:

Later, I have to allocate some memory for the variable and there’s no better place for that than the module’s mod_init() function. Having allocated memory, I need to perform another allocation. This time is for the internal str_hash_table structure that contains the metadata for maintaining our information accessible. Usually, the function in charge of this task is str_hash_alloc() but looking its internals, we will find that it uses pkg_malloc() which certainly won’t work for us. Due this situation, I created an equivalent function for shared memory, mostly because Kamailio’s core doesn’t provide this functionality, or at least, I wasn’t able to find it.

Then, I need to define the structure that will be contained into the shared memory.

Storing and retrieving

In this example, I will store the call ID as the key of the HT’s entry and in the useful part, the call’s start and end timestamp.

First, I created a function that encapsulates all the logic for allocating a new call into shared memory. Please notice that having a HT storing your data, it doesn’t implicitly allocate your information in shared memory address space, this is, you have to allocate your structure using shm_malloc() before adding it to the HT.

Every entry in the HT is represented by str_hash_entry which is a linked-list ready structure that contains the key and the data part as an union. Take in consideration that an union is NOT an structure and its fields retain only one value at a time. Whatever modification you make to any field it will immediately affect to the others.

Finally we need a function callable from the script. In this case start_monitoring does this for us and it must be registered as an exported command.

And:

To finish this little tutorial, I present the stop_monitoring which tries to retrieve the entry using the callid of the call. In case it was found, it writes the current timestamp to the field end_timestamp.

PS: I am aware that I never deallocated the memory I used. I may write the dealloc function for the next entry 😀

Reading pvars with Kamailio

It’s really hard to find documentation in some opensource projects, specially when you want to achieve something that’s not very common. I’m speaking about Kamailio project which is actually very well documented but its core development manual is not profound in topics that we may need to develop a fully functional module entirely in C. This post pretends to be the first of many short tutorials to access to certain parts of the core dev API on which I wasn’t able to find any documentation or when it was present, it was not sufficient.

Reading system pvars.

This time is about reading pseudo variables like $rU, $si or $fU. I won’t explain what pvars are because I’m assuming the reader has some basic knowledge but if you want to have  a better understanding, the development guide has a section dedicated to it but not with the approach I’m using here.

Ok, the code:

And finally, the way we should call it:

If you really want to learn the right way of doing things with Kamailio, the tool you should master is grep. Grepping code was much more useful than reading mailing lists or consulting the devel guide. Consider it an advice.