An example on how to use add_contact_alias() and handle_ruri_alias() functions

Introduction

The benefit of using add_contact_alias() and handle_ruri_alias() functions instead of conventional NAT traversal solutions is that Request-URI in requests to UAs behind NATs 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. In addition to handling NAT traversal, the example uses add_contact_alias() and handle_ruri_alias() functions for re-use of tcp/tls sessions between proxy and UAs.

Non-Register Initial Requests

Call add_contact_alias() on all non-register initial requests, which come from UAs behind NAT or which don't come via a proxy and transport protocol is not UDP. 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 (REQUEST_COMES_FROM_BEHIND_NAT || (!is_present_hf("Record-Route") && (proto != UDP))) {
        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 transport protocol is not UDP.

route [REGISTER_REQUESTS] {
    ...
    if (isflagset(FROM_NATED) || (proto != UDP)) {
        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 that don't come from another proxy and transport protocol is not UDP. 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 (REQUEST_COMES_FROM_BEHIND_NAT || (is_first_hop() && (proto != UDP))) {
        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

Personal Tools