This is an old revision of the document!
Table of Contents
An example on how to use add_contact_alias() and handle_ruri_alias() functions
Introduction
The benefits of using add_contact_alias() and handle_ruri_alias() functions instead of conventional NAT traversal solutions are:
- support for re-use of tcp sessions between proxy and UAs
- supports for routing of requests to UAs behind NATs so that r-uri is always what UAs expect
In the example below only functions related to signaling are shown. Proxying of media using either Mediaprxy or RTP Proxy can be easily added.
Non-Register Initial Requests
Call add_contact_alias() for all non-register initial requests, which don't come from another proxy (which takes care of its own UAs). If you know that non-register initial request is going to another proxy, set TO_PROXY flag before relaying the request. In cases, where initial request may go to another proxy, but you are not sure about it, store the number of record-route headers in incoming request in an AVP that you can then later test in onreply route and find out, if next hop was a proxy.
route [NON_REGISTER_INITIAL_REQUESTS] { if (!is_present_hf("Record-Route")) { route(ADD_CONTACT_ALIAS); }; ... if (I_KNOW_FOR_SURE_THAT_NEXT_HOP_IS_ANOTHER_PROXY) { setbflag("TO_PROXY"); }; if (NEXT_HOP_MAY_BE_ANOTHER_PROXY_BUT_I_DONT_KNOW_FOR_SURE) { $avp("rr_count") = $rr_count; }; t_on_reply("REPLY"); if (!t_relay()) ... } route [ADD_CONTACT_ALIAS] { if (!add_contact_alias()) { xlog("L_ERR", "Error in aliasing contact <$ct>\n"); send_reply("400", "Bad request"); exit; }; }
Register Requests
Call fix_nated_register() on register requests, if registering ua is behind nat OR is using tcp.
route [REGISTER_REQUESTS] {
... if (isflagset(FROM_NATED) || (proto == TCP)) { fix_nated_register(); if (isflagset(FROM_NATED)) { setbflag("TO_NATED"); }; }; save("location"); ...
}
In-Dialog Requests
Call alias_contact() for all in-dialog requests that don't come from another proxy. Call handle_ruri_alias() for all in-dialog requests before t_relaying them to UAs. next hop is an UA if loose_route() didn't set $du. if next hop is a proxy, set TO_PROXY flag.
route [IN_DIALOG_REQUESTS] {
if (@via[2] == "") { route(ADD_CONTACT_ALIAS); } loose_route(); if ($du == "") { handle_ruri_alias(); switch ($rc) { case -1: xlog("L_ERR", "Failed to handle alias of R-URI <$ru>\n"); send_reply("400", "Bad request"); exit; case 1: xlog("L_INFO", "Routing in-dialog $rm from <$fu> to <$du>\n"); break; case 2: xlog("L_INFO", "Routing in-dialog $rm from <$fu> to <$ru>\n"); break; }; } else { setbflag("TO_PROXY"); } t_on_reply("REPLY"); if (!t_relay()) { ...
}
Replies
Call add_contact_alias() on all replies except the ones that come from another proxy.
onreply_route [REPLY] {
if (!isbflagset("TO_PROXY") { if (is_avp_set("$avp(rr_count)")) { if ($rr_count == $avp(rr_count) + $rr_top_count) { route(ADD_CONTACT_ALIAS); }; } else { route(ADD_CONTACT_ALIAS); } } ...
}
Notes
Do not call fix_nated_contact() on anything, because add_contact_alias() replaces it.