that's correct.
1. Check that _m->somefield is NULL and call
parse_headers() with the
appropriate HDR_FOO flag.
yes. you can skip the first check, parse_headers will do it;
don't forget to handle errors if parse_headers returns -1
(most likely mem alloc failure)
2. Check that _m->somefield is non-NULL and return
if false.
yes, that means, that the header field is not in message.
note too that some header fields may occur multiple times; in which case
msg->hdr_foo points to the first occurence; if you wish to
process all of them, you need to traverse the list
msg->headers
example from record-routing (XXX are my extra comments):
int find_first_route(struct sip_msg* _m)
{
/* XXX don't look at _m->route ... parse_headers will do it for you */
if (parse_headers(_m, HDR_ROUTE, 0) == -1) {
/* XXX -1 is some bad error, most likely lack of memory -- leave! */
LOG(L_ERR, "find_first_route(): Error while parsing headers\n");
return -1;
} else {
if (_m->route) {
return 0;
} else { /* XXX not found .... the header field is not there */
DBG("find_first_route(): No Route headers found\n");
return 1;
}
}
}
3. Modify _m->somefield according to the type of
that field.
4. Using del_lump() mark original version of the header for deletion and
using insert_new_lump() indicate where modified version should be placed
before sending a message out.
3 is the same as 4, isn't it. you modify a header field by creating
a delete lump refering the old value and an insert lump with the new
value. the new value must be created using pkg_malloc. the replace
action in textops module is a good example. (The lump lists, sort
of "diffs", is processed when later on the message is printed and forwarded.)
-Jiri