modules/websocket: Updated example kamailio.cfg
authorPeter Dunkley <peter.dunkley@crocodile-rcs.com>
Fri, 26 Oct 2012 15:09:09 +0000 (16:09 +0100)
committerPeter Dunkley <peter.dunkley@crocodile-rcs.com>
Fri, 26 Oct 2012 15:09:09 +0000 (16:09 +0100)
- Better MSRP over WebSocket support

modules/websocket/example/kamailio.cfg

index b43583b..21b9925 100644 (file)
@@ -120,7 +120,7 @@ modparam("auth", "qop", "auth")
 modparam("auth_db", "db_url", "DBURL")
 modparam("auth_db", "calculate_ha1", yes)
 modparam("auth_db", "password_column", "password")
-modparam("auth_db", "load_credentials", "")
+modparam("auth_db", "load_credentials", "id")
 
 # ----- corex params -----
 modparam("corex", "alias_subdomains", "MY_DOMAIN")
@@ -153,6 +153,12 @@ modparam("htable", "htable", "msrp=>size=8;autoexpire=MSRP_MAX_EXPIRES;")
 # - processing of any incoming SIP request starts with this route
 # - note: this is the same as route { ... }
 request_route {
+       if ((($Rp == MY_WS_PORT || $Rp == MY_WSS_PORT)
+               && !(proto == WS || proto == WSS)) || $Rp == MY_MSRP_PORT) {
+               xlog("L_WARN", "SIP request received on $Rp\n");
+               sl_send_reply("403", "Forbidden");
+               exit;
+       }
 
        # per request initial checks
        route(REQINIT);
@@ -164,9 +170,9 @@ request_route {
                # This won't be needed in the future if Kamailio and the
                # WebSocket client support Outbound and Path.
                force_rport();
-               if (is_method("REGISTER"))
+               if (is_method("REGISTER")) {
                        fix_nated_register();
-               else {
+               else {
                        if (!add_contact_alias()) {
                                xlog("L_ERR", "Error aliasing contact <$ct>\n");
                                sl_send_reply("400", "Bad Request");
@@ -183,8 +189,9 @@ request_route {
 
        # CANCEL processing
        if (is_method("CANCEL")) {
-               if (t_check_trans())
+               if (t_check_trans()) {
                        t_relay();
+               }
                exit;
        }
 
@@ -196,15 +203,16 @@ request_route {
        # record routing for dialog forming requests (in case they are routed)
        # - remove preloaded route headers
        remove_hf("Route");
-       if (is_method("INVITE"))
+       if (is_method("INVITE")) {
                record_route();
+       }
 
        # handle registrations
        route(REGISTRAR);
 
        if ($rU==$null) {
                # request with no Username in RURI
-               sl_send_reply("484","Address Incomplete");
+               sl_send_reply("484", "Address Incomplete");
                exit;
        }
 
@@ -269,7 +277,7 @@ route[WITHINDLG] {
                                        exit;
                                }
                        }
-                       sl_send_reply("404","Not Found");
+                       sl_send_reply("404", "Not Found");
                }
                exit;
        }
@@ -278,26 +286,34 @@ route[WITHINDLG] {
 # Handle SIP registrations
 route[REGISTRAR] {
        if (is_method("REGISTER")) {
-               if (!save("location"))
+               if (!save("location")) {
                        sl_reply_error();
-
+               }
                exit;
        }
 }
 
 # USER location service
 route[LOCATION] {
+       if (!is_subscriber("$ru", "subscriber", "1")) {
+               t_newtran();
+               send_reply("404", "Not Found");
+               exit;
+       }
+
        if (!lookup("location")) {
                $var(rc) = $rc;
                t_newtran();
                switch ($var(rc)) {
-                       case -1:
-                       case -3:
-                               send_reply("404", "Not Found");
-                               exit;
-                       case -2:
-                               send_reply("405", "Method Not Allowed");
-                               exit;
+               case -1:
+                       send_reply("480", "Temporarily Unavailable");
+                       exit;
+               case -2:
+                       send_reply("405", "Method Not Allowed");
+                       exit;
+               case -3:
+                       send_reply("500", "Server Internal Error");
+                       exit;
                }
        }
 }
@@ -311,19 +327,27 @@ route[AUTH] {
                        exit;
                }
                # user authenticated - remove auth header
-               if(!is_method("REGISTER"))
+               if(!is_method("REGISTER")) {
                        consume_credentials();
+               }
        }
        # if caller is not local subscriber, then check if it calls
        # a local destination, otherwise deny, not an open relay here
        if (from_uri!=myself && uri!=myself) {
-               sl_send_reply("403","Forbidden");
+               sl_send_reply("403", "Forbidden");
                exit;
        }
 }
 
 #!ifdef WITH_WEBSOCKETS
 onreply_route {
+       if ((($Rp == MY_WS_PORT || $Rp == MY_WSS_PORT)
+               && !(proto == WS || proto == WSS)) || $Rp == MY_MSRP_PORT) {
+               xlog("L_WARN", "SIP response received on $Rp\n");
+               drop;
+               exit;
+       }
+
        if (nat_uac_test(64)) {
                # Do NAT traversal stuff for replies to a WebSocket connection
                # - even if it is not behind a NAT!
@@ -393,40 +417,29 @@ event_route[websocket:closed] {
 
 #!ifdef WITH_MSRP
 event_route[msrp:frame-in] {
-       xdbg("============#[[$msrp(method)]]===========\n");
-       xdbg("============*[[$si:$sp]]\n");
-       xdbg("============ crthop:   [$msrp(crthop)]\n");
-       xdbg("============ prevhop:  [$msrp(prevhop)]\n");
-       xdbg("============ nexthop:  [$msrp(nexthop)]\n");
-       xdbg("============ firsthop: [$msrp(firsthop)]\n");
-       xdbg("============ lasthop:  [$msrp(lasthop)]\n");
-       xdbg("============ prevhops: [$msrp(prevhops)]\n");
-       xdbg("============ nexthops: [$msrp(nexthops)]\n");
-       xdbg("============ srcaddr:  [$msrp(srcaddr)]\n");
-       xdbg("============ srcsock:  [$msrp(srcsock)]\n");
-       xdbg("============ sessid:   [$msrp(sessid)]\n");
-
        msrp_reply_flags("1");
 
-       if(msrp_is_reply())
-       {
-               msrp_relay();
+       if ((($Rp == MY_WS_PORT || $Rp == MY_WSS_PORT)
+               && !(proto == WS || proto == WSS)) && $Rp != MY_MSRP_PORT) {
+               xlog("L_WARN", "MSRP request received on $Rp\n");
+               msrp_reply("403", "Action-not-allowed");
+               exit;
        }
-       else if($msrp(method)=="AUTH")
-       {
-               if($msrp(nexthops)>0)
-               {
+
+       if (msrp_is_reply()) {
+               msrp_relay();
+       } else if($msrp(method)=="AUTH") {
+               if($msrp(nexthops)>0) {
                        msrp_relay();
                        exit;
                }
 
-               if(!www_authenticate("MY_DOMAIN", "subscriber", "$msrp(method)"))
-               {
-                       if(auth_get_www_authenticate("myrealm", "1",
-                                               "$var(wauth)"))
-                       {
+               if (!www_authenticate("MY_DOMAIN", "subscriber",
+                                       "$msrp(method)")) {
+                       if (auth_get_www_authenticate("MY_DOMAIN", "1",
+                                                       "$var(wauth)")) {
                                msrp_reply("401", "Unauthorized",
-                                               "$var(wauth)");
+                                                       "$var(wauth)");
                        } else {
                                msrp_reply("500", "Server Error");
                        }
@@ -435,7 +448,6 @@ event_route[msrp:frame-in] {
 
                if ($hdr(Expires) != $null) {
                        $var(expires) = (int) $hdr(Expires);
-
                        if ($var(expires) < MSRP_MIN_EXPIRES) {
                                msrp_reply("423", "Interval Out-of-Bounds",
                                        "Min-Expires: MSRP_MIN_EXPIRES\r\n");
@@ -443,51 +455,64 @@ event_route[msrp:frame-in] {
                        } else if ($var(expires) > MSRP_MAX_EXPIRES) {
                                msrp_reply("423", "Interval Out-of-Bounds",
                                        "Max-Expires: MSRP_MAX_EXPIRES\r\n");
-                                       exit;
+                               exit;
                        }
-               } else
+               } else {
                        $var(expires) = MSRP_MAX_EXPIRES;
+               }
 
                $var(cnt) = $var(cnt) + 1;
                pv_printf("$var(sessid)", "s.$(pp).$(var(cnt)).$(RANDOM)");
                $sht(msrp=>$var(sessid)::srcaddr) = $msrp(srcaddr);
                $sht(msrp=>$var(sessid)::srcsock) = $msrp(srcsock);
-               $shtex(msrp=>$var(sessid)) = MSRP_MAX_EXPIRES + 5;
+               $shtex(msrp=>$var(sessid)) = $var(expires) + 5;
                # - Use-Path: the MSRP address for server + session id
-               $var(headers) = "Use-Path: msrp://MY_IP_ADDR:MY_MSRP_PORT/"
-                               + $var(sessid) + ";tcp\r\n"
+               if (proto == WS || proto == WSS) {
+                       if (proto == WS) {
+                               $var(hdrs) =
+                               "Use-Path: msrp://MY_IP_ADDR:MY_WS_PORT/"
+                                       + $var(sessid) + ";ws ";
+                       } else {
+                               $var(hdrs) =
+                               "Use-Path: msrps://MY_IP_ADDR:MY_WSS_PORT/"
+                                       + $var(sessid) + ";ws ";
+                       }
+                       $var(cnt) = $var(cnt) + 1;
+                       pv_printf("$var(sessid)",
+                                       "s.$(pp).$(var(cnt)).$(RANDOM)");
+                       $var(hdrs) = $var(hdrs)
+                                       + "msrps://MY_IP_ADDR:MY_MSRP_PORT/"
+                                       + $var(sessid) + ";tcp\r\n";
+               } else {
+                       $var(hdrs) =
+                               "Use-Path: msrps://MY_IP_ADDR:MY_MSRP_PORT/"
+                                       + $var(sessid) + ";tcp\r\n";
+               }
+               $var(hdrs) = $var(hdrs)
                                + "Expires: " + $var(expires) + "\r\n";
-               msrp_reply("200", "OK", "$var(expires)");
-       }
-       else if($msrp(method)=="SEND" || $msrp(method)=="REPORT")
-       {
-               if($msrp(nexthops)>1)
-               {
-                       if ($msrp(method)!="REPORT")
-                       {
+               msrp_reply("200", "OK", "$var(hdrs)");
+       } else if ($msrp(method)=="SEND" || $msrp(method)=="REPORT") {
+               if ($msrp(nexthops)>1) {
+                       if ($msrp(method)!="REPORT") {
                                msrp_reply("200", "OK");
                        }
                        msrp_relay();
                        exit;
                }
                $var(sessid) = $msrp(sessid);
-               if($sht(msrp=>$var(sessid)::srcaddr) == $null)
-               {
+               if ($sht(msrp=>$var(sessid)::srcaddr) == $null) {
                        # one more hop, but we don't have address in htable
                        msrp_reply("481", "Session-does-not-exist");
                        exit;
                }
-               else if($msrp(method)!="REPORT")
-               {
-                       msrp_relay("200", "OK");
+               else if ($msrp(method)!="REPORT") {
+                       msrp_reply("200", "OK");
                }
                msrp_relay_flags("1");
                msrp_set_dst("$sht(msrp=>$var(sessid)::srcaddr)",
                                "$sht(msrp=>$var(sessid)::srcsock)");
                msrp_relay();
-       }
-       else
-       {
+       } else {
                msrp_reply("501", "Request-method-not-understood");
        }
 }