DevSnippets

Home » Kamailio » Route by Caller Number

Route by Caller Number

Kamailio Route the number to another IP as per CALLER number 11 views

Code

# Kamailio configuration for routing based on caller number

# Define the caller number patterns and their corresponding servers
modparam("htable", "htable", "callerroutes=>size=10;autoexpire=3600")

request_route {
    # ... other configuration ...
    
    # Check if it's an INVITE
    if (is_method("INVITE")) {
        # Extract the caller number from the From header
        if ($fU =~ "^(1234.*)$") {
            # Route calls from numbers starting with 1234 to a specific server
            $du = "sip:$rU@192.168.1.10:5060";
            route(RELAY);
            exit;
        }
        
        if ($fU =~ "^(5678.*)$") {
            # Route calls from numbers starting with 5678 to another server
            $du = "sip:$rU@192.168.1.20:5060";
            route(RELAY);
            exit;
        }
        
        # Default routing if no pattern matches
        # ...
    }
    
    # ... continue with other routing logic ...
}

# Relay route
route[RELAY] {
    if (!t_relay()) {
        sl_reply_error();
    }
}

Explanation

This Kamailio configuration snippet shows how to route calls based on the caller's number (CLI/ANI). It extracts the caller number from the From header using the $fU variable and uses regular expressions to match patterns. Based on the caller number pattern, it routes the call to different SIP servers. This is useful for implementing caller-based routing policies, such as routing VIP callers to premium servers or routing based on caller geography.

Rate this snippet

Click to rate

Have a Better Solution?

Know a more efficient way to solve this problem? Share your own code snippet and help the community!

Submit Your Version