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:
/** * Get str value using its name * * msg: parsed SIP message * pv_name: the name of the pseudo variable * pv_Value: pointer to str in which the value will be stored * */ static int get_str_pv(struct sip_msg* msg, str *pv_name, str *pv_value) { pv_spec_t spec; pv_value_t value; pv_parse_spec(&pv_name, &spec); if (pv_get_spec_value(msg, &spec, &value) != 0) { LM_ERR("Can't get spec for [%.*s]n", pv_name->len, pv_name->s); return -1; } if (pkg_str_dup(pv_value, &value.rs) != 0) { LM_ERR("No more pkg memory availablen"); return -1; } return 0; }
And finally, the way we should call it:
str source_ip_pv = str_init("$si"); str value; if (get_str_pv(msg, &source_ip_pv, &value) != 0) return -1;
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.