This is an old revision of the document!


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:

  • Request-URI in requests to UAs behind NATs is always what UAs expect
  • Re-use of tcp sessions between proxy and UAs (optional)

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() on all non-register initial requests, which come from UAs behind NAT, or (if you want re-use of TCP sessions) which don't come via a proxy. 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] {

    # Use this test (and delete the next) if you don't care about re-use of TCP sessions    
    if (REQUEST_COMES_FROM_BEHIND_NAT) {
        route(ADD_CONTACT_ALIAS);
    };
    # Use this test (and delete the previous) if you want also re-use of TCP sessions    
    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) || (proto == TLS)) {
        fix_nated_register();
        if (isflagset(FROM_NATED)) {
            setbflag("TO_NATED");
        };
    };
    save("location");
    ...
}

In-Dialog Requests

Call add_contact_alias() on all in-dialog requests that come from behind NAT or (if you want re-use of TCP sessions) 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] {

    # Choose this test (and delete the next) if you don't care about re-use of TCP sessions
    if (REQUEST_COMES_FROM_BEHIND_NAT) {
        route(ADD_CONTACT_ALIAS);
    }
    # Choose this test (and delete the previous) if you want re-use of TCP sessions
    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.


Navigation

Wiki

Other

QR Code
QR Code tutorials:alias-example (generated for current page)