modules/websocket: Added support for MSRP WebSocket sub-protocol
authorPeter Dunkley <peter.dunkley@crocodile-rcs.com>
Thu, 11 Oct 2012 11:57:26 +0000 (12:57 +0100)
committerPeter Dunkley <peter.dunkley@crocodile-rcs.com>
Thu, 11 Oct 2012 11:57:26 +0000 (12:57 +0100)
- Prototype of draft-pd-msrp-websocket
- Enables session based chat and group chat from HTML5 clients

modules/websocket/README
modules/websocket/doc/websocket_admin.xml
modules/websocket/example/kamailio.cfg
modules/websocket/ws_conn.c
modules/websocket/ws_conn.h
modules/websocket/ws_frame.c
modules/websocket/ws_handshake.c
modules/websocket/ws_handshake.h
modules/websocket/ws_mod.c

index 9f55bb0..0ef44d5 100644 (file)
@@ -16,6 +16,7 @@ Peter Dunkley
 
               2.1. Initiating a connection
               2.2. SIP message routing
+              2.3. MSRP message routing
 
         3. Dependencies
 
@@ -29,6 +30,7 @@ Peter Dunkley
               4.3. keepalive_processes (integer)
               4.4. keepalive_interval (integer)
               4.5. ping_application_data (string)
+              4.6. sub_protocols (integer)
 
         5. Functions
 
@@ -56,7 +58,8 @@ Peter Dunkley
    1.5. Set keepalive_processes parameter
    1.6. Set keepalive_interval parameter
    1.7. Set ping_application_data parameter
-   1.8. ws_handle_handshake usage
+   1.8. Set sub_protocols parameter
+   1.9. ws_handle_handshake usage
 
 Chapter 1. Admin Guide
 
@@ -67,6 +70,7 @@ Chapter 1. Admin Guide
 
         2.1. Initiating a connection
         2.2. SIP message routing
+        2.3. MSRP message routing
 
    3. Dependencies
 
@@ -80,6 +84,7 @@ Chapter 1. Admin Guide
         4.3. keepalive_processes (integer)
         4.4. keepalive_interval (integer)
         4.5. ping_application_data (string)
+        4.6. sub_protocols (integer)
 
    5. Functions
 
@@ -102,8 +107,9 @@ Chapter 1. Admin Guide
 
    This module implements a WebSocket (RFC 6455) server and provides
    connection establishment (handshaking), management (including
-   connection keep-alive), and framing for the SIP WebSocket sub-protocol
-   (ietf-draft-sipcore-sip-websocket).
+   connection keep-alive), and framing for the SIP and MSRP WebSocket
+   sub-protocols (draft-ietf-sipcore-sip-websocket and
+   draft-pd-msrp-websocket).
 
    The module supports WebSockets (ws) and secure WebSockets (wss)
 
@@ -111,6 +117,7 @@ Chapter 1. Admin Guide
 
    2.1. Initiating a connection
    2.2. SIP message routing
+   2.3. MSRP message routing
 
 2.1. Initiating a connection
 
@@ -122,13 +129,14 @@ Chapter 1. Admin Guide
    headers before calling Section 5.1, “ ws_handle_handshake() ”. The
    event_route can also be used to make sure the HTTP GET has the correct
    URI, perform HTTP authentication on the WebSocket connection, and check
-   the Origin header (RFC 6454) to ensure a browser-based SIP UA has been
-   downloaded from the correct location.
+   the Origin header (RFC 6454) to ensure a browser-based SIP UA or MSRP
+   client has been downloaded from the correct location.
 
    Example 1.1. event_route[xhttp:request]
 ...
 loadmodule "sl.so"
 loadmodule "xhttp.so"
+loadmodule "msrp.so"  # Only required if using MSRP over WebSockets
 loadmodule "websocket.so"
 ...
 event_route[xhttp:request] {
@@ -194,7 +202,7 @@ event_route[xhttp:request] {
    WebSocket connection is made. This means that the routing headers
    cannot be used for request or response routing in the normal manner.
 
-   ietf-draft-sipcore-sip-websocket states that SIP WebSocket Clients and
+   draft-ietf-sipcore-sip-websocket states that SIP WebSocket Clients and
    the SIP registrar should implement Outbound (RFC 5626) and Path (RFC
    3327) to enable requests and responses to be correctly routed. However,
    Kamailio does not currently support Outbound and it may not be possible
@@ -265,6 +273,16 @@ onreply_route[WS_REPLY] {
 }
 ...
 
+2.3. MSRP message routing
+
+   MSRP over WebSocket clients create invalid local URIs for use in Path
+   headers (From-Path: and To-Path:) because a JavaScript stack running in
+   a browser has no way to determine the local address from which the
+   WebSocket connection is made. This is OK because MSRP over WebSocket
+   clients MUST use an MSRP relay and it is the MSRP relay's
+   responsibility to select the correct connection to the client based on
+   the MSRP URIs that it has created (and maintains a mapping for).
+
 3. Dependencies
 
    3.1. Kamailio Modules
@@ -284,6 +302,9 @@ onreply_route[WS_REPLY] {
    scheme:
      * tls.
 
+   The following module is required to support MSRP over WebSockets:
+     * msrp.
+
 3.2. External Libraries or Applications
 
    The following libraries must be installed before running Kamailio with
@@ -298,6 +319,7 @@ onreply_route[WS_REPLY] {
    4.3. keepalive_processes (integer)
    4.4. keepalive_interval (integer)
    4.5. ping_application_data (string)
+   4.6. sub_protocols (integer)
 
 4.1. keepalive_mechanism (integer)
 
@@ -367,6 +389,21 @@ modparam("websocket", "keepalive_interval", 2)
 modparam("websocket", "ping_application_data", "WebSockets rock")
 ...
 
+4.6. sub_protocols (integer)
+
+   A bitmap that allows you to control the sub-protocols supported by the
+   WebSocket server.
+     * 1 - sip (draft-ietf-sipcore-sip-websocket)
+     * 2 - msrp (draft-pd-msrp-websocket) - msrp.so must be loaded before
+       websocket.so
+
+   Default value is 1 when msrp.so is not loaded 3 when msrp.so is loaded.
+
+   Example 1.8. Set sub_protocols parameter
+...
+modparam("websocket", "sub_protocols", 2)
+...
+
 5. Functions
 
    5.1. ws_handle_handshake()
@@ -385,7 +422,7 @@ Note
    This function returns 0, stopping all further processing of the
    request, when there is a problem.
 
-   Example 1.8. ws_handle_handshake usage
+   Example 1.9. ws_handle_handshake usage
 ...
 ws_handle_handshake();
 ...
index 7dcdc13..895ed9a 100644 (file)
@@ -17,8 +17,9 @@
        <title>Overview</title>
        <para>This module implements a WebSocket (RFC 6455) server and provides
        connection establishment (handshaking), management (including
-       connection keep-alive), and framing for the SIP WebSocket sub-protocol
-       (ietf-draft-sipcore-sip-websocket).</para>
+       connection keep-alive), and framing for the SIP and MSRP WebSocket
+       sub-protocols (draft-ietf-sipcore-sip-websocket and
+       draft-pd-msrp-websocket).</para>
        <para>The module supports WebSockets (ws) and secure WebSockets (wss)
        </para>
        </section>
        used to make sure the HTTP GET has the correct URI, perform HTTP
        authentication on the WebSocket connection, and check the
        <emphasis>Origin</emphasis> header (RFC 6454) to ensure a
-       browser-based SIP UA has been downloaded from the correct location.
-       </para>
+       browser-based SIP UA or MSRP client has been downloaded from the
+       correct location.</para>
        <example>
        <title>event_route[xhttp:request]</title>
        <programlisting><![CDATA[
 ...
 loadmodule "sl.so"
 loadmodule "xhttp.so"
+loadmodule "msrp.so"  # Only required if using MSRP over WebSockets
 loadmodule "websocket.so"
 ...
 event_route[xhttp:request] {
@@ -107,13 +109,13 @@ event_route[xhttp:request] {
 
        <section>
        <title>SIP message routing</title>
-       <para> SIP over WebSockets uses invalid URIs in routing headers
+       <para>SIP over WebSockets uses invalid URIs in routing headers
        (Contact:, Record-Route:, and Via:) because a JavaScript stack running
        in a browser has no way to determine the local address from which the
        WebSocket connection is made.  This means that the routing headers
        cannot be used for request or response routing in the normal manner.
        </para>
-       <para>ietf-draft-sipcore-sip-websocket states that SIP WebSocket
+       <para>draft-ietf-sipcore-sip-websocket states that SIP WebSocket
        Clients and the SIP registrar should implement Outbound (RFC 5626) and
        Path (RFC 3327) to enable requests and responses to be correctly
        routed.  However, &kamailio; does not currently support Outbound and
@@ -192,6 +194,18 @@ onreply_route[WS_REPLY] {
        </example>
 
        </section>
+
+       <section>
+       <title>MSRP message routing</title>
+       <para>MSRP over WebSocket clients create invalid local URIs for use in
+       Path headers (From-Path: and To-Path:) because a JavaScript stack
+       running in a browser has no way to determine the local address from
+       which the WebSocket connection is made.  This is OK because MSRP over
+       WebSocket clients MUST use an MSRP relay and it is the MSRP relay's
+       responsibility to select the correct connection to the client based on
+       the MSRP URIs that it has created (and maintains a mapping for).</para>
+       </section>
+
        </section>
 
        <section>
@@ -230,6 +244,15 @@ onreply_route[WS_REPLY] {
                </listitem>
                </itemizedlist>
                </para>
+               <para>
+               The following module is required to support MSRP over
+               WebSockets:
+               <itemizedlist>
+               <listitem>
+               <para><emphasis>msrp</emphasis>.</para>
+               </listitem>
+               </itemizedlist>
+               </para>
        </section>
 
        <section>
@@ -348,6 +371,32 @@ modparam("websocket", "ping_application_data", "WebSockets rock")
                </example>
        </section>
 
+       <section>
+               <title><varname>sub_protocols</varname> (integer)</title>
+               <para>A bitmap that allows you to control the sub-protocols
+               supported by the WebSocket server.</para>
+               <itemizedlist>
+               <listitem><para>
+               <emphasis>1</emphasis> - sip (draft-ietf-sipcore-sip-websocket)
+               </para></listitem>
+               <listitem><para>
+               <emphasis>2</emphasis> - msrp (draft-pd-msrp-websocket) -
+               msrp.so must be loaded before websocket.so
+               </para></listitem>
+               </itemizedlist>
+               <para><emphasis>Default value is 1 when msrp.so is not loaded
+                3 when msrp.so is loaded.</emphasis></para>
+               <example>
+               <title>Set <varname>sub_protocols</varname>
+               parameter</title>
+               <programlisting format="linespecific">
+...
+modparam("websocket", "sub_protocols", 2)
+...
+</programlisting>
+               </example>
+       </section>
+
        </section>
 
        <section>
index f4b4c12..03827ad 100644 (file)
@@ -8,12 +8,15 @@
 #!substdef "!MY_DOMAIN!example.com!g"
 #!substdef "!MY_WS_PORT!80!g"
 #!substdef "!MY_WSS_PORT!443!g"
+#!substdef "!MY_MSRP_PORT!9000!g"
 #!substdef "!MY_WS_ADDR!tcp:MY_IP_ADDR:MY_WS_PORT!g"
 #!substdef "!MY_WSS_ADDR!tls:MY_IP_ADDR:MY_WSS_PORT!g"
+#!substdef "!MY_MSRP_ADDR!tls:MY_IP_ADDR:MY_MSRP_PORT!g"
 
 ##!define LOCAL_TEST_RUN
 #!define WITH_TLS
 #!define WITH_WEBSOCKETS
+#!define WITH_MSRP
 
 
 ####### Global Parameters #########
@@ -32,6 +35,9 @@ listen=MY_WS_ADDR
 listen=MY_WSS_ADDR
 #!endif
 #!endif
+#!ifdef WITH_MSRP
+listen=MY_MSRP_ADDR
+#!endif
 
 tcp_connection_lifetime=3604
 tcp_accept_no_cl=yes
@@ -68,6 +74,11 @@ loadmodule "corex.so"
 #!ifdef WITH_TLS
 loadmodule "tls.so"
 #!endif
+#!ifdef WITH_MSRP
+loadmodule "msrp.so"
+loadmodule "htable.so"
+loadmodule "cfgutils.so"
+#!endif
 #!ifdef WITH_WEBSOCKETS
 loadmodule "xhttp.so"
 loadmodule "websocket.so"
@@ -124,6 +135,11 @@ modparam("nathelper|registrar", "received_avp", "$avp(RECEIVED)")
 #       their own keep-alives.
 #!endif
 
+#!ifdef WITH_MSRP
+# ----- htable params -----
+modparam("htable", "htable", "msrp=>size=8;autoexpire=3600;")
+#!endif
+
 
 ####### Routing Logic ########
 
@@ -202,7 +218,7 @@ route[RELAY] {
 # Per SIP request initial checks
 route[REQINIT] {
        if (!mf_process_maxfwd_header("10")) {
-               sl_send_reply("483","Too Many Hops");
+               sl_send_reply("483", "Too Many Hops");
                exit;
        }
 
@@ -368,3 +384,86 @@ event_route[websocket:closed] {
        xlog("L_INFO", "WebSocket connection from $si:$sp has closed\n");
 }
 #!endif
+
+#!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();
+               exit;
+       }
+
+       # handle AUTH MSRP requests
+       if($msrp(method)=="AUTH")
+       {
+               if($msrp(nexthops)>0)
+               {
+                       msrp_relay();
+                       exit;
+               }
+               # frame for local server - send Use-Path
+               # -- passwd can be loaded from DB based on $au
+               $var(passwd) = "xyz123";
+               if(!pv_www_authenticate("myrealm", "$var(passwd)", "0"))
+               {
+                       if(auth_get_www_authenticate("myrealm", "0",
+                                               "$var(wauth)"))
+                       {
+                               msrp_reply("401", "Authorization Required",
+                                               "$var(wauth)");
+                       } else {
+                               msrp_reply("500", "Server Error");
+                       }
+                       exit;
+               }
+               $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);
+               # - Use-Path: the MSRP address for server + session id
+               $var(UsePath) = "Use-Path: msrp://MY_IP_ADDR:MY_MSRP_PORT/"
+                               + $var(sessid) + ";tcp\r\n";
+               msrp_reply("200", "OK", "$var(UsePath)");
+               exit;
+       }
+
+       if($msrp(method)=="SEND")
+       {
+               if($msrp(nexthops)>1)
+               {
+                       msrp_reply("200", "Received");
+                       msrp_relay();
+                       exit;
+               }
+               $var(sessid) = $msrp(sessid);
+               if($sht(msrp=>$var(sessid)::srcaddr) == $null)
+               {
+                       # one more hop, but we don't have address in htable
+                       msrp_reply("481", "No Such Session");
+                       exit;
+               }
+               msrp_relay_flags("1");
+               msrp_set_dst("$sht(msrp=>$var(sessid)::srcaddr)",
+                               "$sht(msrp=>$var(sessid)::srcsock)");
+               msrp_relay();
+               exit;
+       }
+
+       msrp_relay();
+}
+#!endif
index 5afeaa6..dde1e25 100644 (file)
@@ -176,7 +176,7 @@ void wsconn_destroy(void)
        }
 }
 
-int wsconn_add(struct receive_info rcv)
+int wsconn_add(struct receive_info rcv, unsigned int sub_protocol)
 {
        int cur_cons, max_cons;
        int id = rcv.proto_reserved1;
@@ -195,6 +195,7 @@ int wsconn_add(struct receive_info rcv)
        wsc->id_hash = id_hash;
        wsc->state = WS_S_OPEN;
        wsc->rcv = rcv;
+       wsc->sub_protocol = sub_protocol;
 
        WSCONN_LOCK;
        /* Add to WebSocket connection table */
index 5485458..de8d00e 100644 (file)
@@ -50,6 +50,8 @@ typedef struct ws_connection
        struct ws_connection *id_next;
 
        struct receive_info rcv;
+
+       unsigned int sub_protocol;
 } ws_connection_t;
 
 typedef struct
@@ -73,7 +75,7 @@ extern stat_var *ws_max_concurrent_connections;
 
 int wsconn_init(void);
 void wsconn_destroy(void);
-int wsconn_add(struct receive_info rcv);
+int wsconn_add(struct receive_info rcv, unsigned int sub_protocol);
 int wsconn_rm(ws_connection_t *wsc, ws_conn_eventroute_t run_event_route);
 int wsconn_update(ws_connection_t *wsc);
 void wsconn_close_now(ws_connection_t *wsc);
index eded7a4..ad26103 100644 (file)
 
 #include <limits.h>
 #include <unistr.h>
+#include "../../events.h"
 #include "../../receive.h"
 #include "../../stats.h"
 #include "../../str.h"
 #include "../../tcp_conn.h"
+#include "../../tcp_read.h"
 #include "../../tcp_server.h"
 #include "../../lib/kcore/kstats_wrapper.h"
 #include "../../lib/kmi/tree.h"
@@ -34,6 +36,7 @@
 #include "ws_conn.h"
 #include "ws_frame.h"
 #include "ws_mod.h"
+#include "ws_handshake.h"
 
 /*    0                   1                   2                   3
       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
@@ -571,10 +574,36 @@ int ws_frame_receive(void *data)
        {
        case OPCODE_TEXT_FRAME:
        case OPCODE_BINARY_FRAME:
-               LM_DBG("Rx SIP message:\n%.*s\n", frame.payload_len,
+               if (likely(frame.wsc->sub_protocol == SUB_PROTOCOL_SIP))
+               {
+                       LM_DBG("Rx SIP message:\n%.*s\n", frame.payload_len,
                                frame.payload_data);
-               return receive_msg(frame.payload_data, frame.payload_len,
-                               tcpinfo->rcv);
+                       return receive_msg(frame.payload_data,
+                                               frame.payload_len,
+                                               tcpinfo->rcv);
+               }
+               else if (frame.wsc->sub_protocol == SUB_PROTOCOL_MSRP)
+               {
+                       LM_DBG("Rx MSRP frame:\n%.*s\n", frame.payload_len,
+                               frame.payload_data);
+                       if (likely(sr_event_enabled(SREV_TCP_MSRP_FRAME)))
+                       {
+                               tcp_event_info_t tev;
+                               memset(&tev, 0, sizeof(tcp_event_info_t));
+                               tev.type = SREV_TCP_MSRP_FRAME;
+                               tev.buf = frame.payload_data;
+                               tev.len = frame.payload_len;
+                               tev.rcv = tcpinfo->rcv;
+                               tev.con = tcpinfo->con;
+                               return sr_event_exec(SREV_TCP_MSRP_FRAME,
+                                                       (void *) &tev);
+                       }
+                       else
+                       {
+                               LM_ERR("no callback registerd for MSRP\n");
+                               return -1;
+                       }
+               }
 
        case OPCODE_CLOSE:
                return handle_close(&frame);
@@ -608,12 +637,12 @@ int ws_frame_transmit(void *data)
        frame.payload_data = wsev->buf;
        frame.wsc = wsconn_get(wsev->id);
 
-       LM_DBG("Tx SIP message:\n%.*s\n", frame.payload_len,
+       LM_DBG("Tx message:\n%.*s\n", frame.payload_len,
                        frame.payload_data);
 
        if (encode_and_send_ws_frame(&frame, CONN_CLOSE_DONT) < 0)
        {       
-               LM_ERR("sending SIP message\n");
+               LM_ERR("sending message\n");
                return -1;
        }
 
index a874eac..bbf35fb 100644 (file)
 
 #define WS_VERSION             (13)
 
+int ws_sub_protocols = DEFAULT_SUB_PROTOCOLS;
+
 stat_var *ws_failed_handshakes;
 stat_var *ws_successful_handshakes;
 
 static str str_sip = str_init("sip");
+static str str_msrp = str_init("msrp");
 static str str_upgrade = str_init("upgrade");
 static str str_websocket = str_init("websocket");
 static str str_ws_guid = str_init("258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
@@ -110,7 +113,7 @@ int ws_handle_handshake(struct sip_msg *msg)
 {
        str key = {0, 0}, headers = {0, 0}, reply_key = {0, 0};
        unsigned char sha1[SHA_DIGEST_LENGTH];
-       unsigned int hdr_flags = 0;
+       unsigned int hdr_flags = 0, sub_protocol = 0;
        int version;
        struct hdr_field *hdr = msg->headers;
        struct tcp_connection *con;
@@ -188,7 +191,8 @@ int ws_handle_handshake(struct sip_msg *msg)
                                LM_WARN("%.*s found multiple times\n",
                                        hdr->name.len, hdr->name.s);
                                ws_send_reply(msg, 400,
-                                               &str_status_bad_request, NULL);
+                                               &str_status_bad_request,
+                                               NULL);
                                return 0;
                        }
 
@@ -210,6 +214,15 @@ int ws_handle_handshake(struct sip_msg *msg)
                                        hdr->name.len, hdr->name.s,
                                        hdr->body.len, hdr->body.s);
                                hdr_flags |= SEC_WEBSOCKET_PROTOCOL;
+                               sub_protocol |= SUB_PROTOCOL_SIP;
+                       }
+                       if (str_search(&hdr->body, &str_msrp) != NULL)
+                       {
+                               LM_DBG("found %.*s: %.*s\n",
+                                       hdr->name.len, hdr->name.s,
+                                       hdr->body.len, hdr->body.s);
+                               hdr_flags |= SEC_WEBSOCKET_PROTOCOL;
+                               sub_protocol |= SUB_PROTOCOL_MSRP;
                        }
                }
                /* Decode and validate Sec-WebSocket-Version */
@@ -222,7 +235,8 @@ int ws_handle_handshake(struct sip_msg *msg)
                                LM_WARN("%.*s found multiple times\n",
                                        hdr->name.len, hdr->name.s);
                                ws_send_reply(msg, 400,
-                                               &str_status_bad_request, NULL);
+                                               &str_status_bad_request,
+                                               NULL);
                                return 0;
                        }
 
@@ -254,20 +268,37 @@ int ws_handle_handshake(struct sip_msg *msg)
        }
 
        /* Final check that all required headers/values were found */
-       if (hdr_flags != REQUIRED_HEADERS)
+       sub_protocol &= ws_sub_protocols;
+       if (hdr_flags != REQUIRED_HEADERS || sub_protocol == 0)
        {
+
                LM_WARN("required headers not present\n");
                headers.s = headers_buf;
-               headers.len = snprintf(headers.s, HDR_BUF_LEN,
-                                       "%.*s: %.*s\r\n"
-                                       "%.*s: %d\r\n",
+               headers.len = 0;
+
+               if (ws_sub_protocols & SUB_PROTOCOL_SIP)
+                       headers.len += snprintf(headers.s + headers.len,
+                                               HDR_BUF_LEN - headers.len,
+                                               "%.*s: %.*s\r\n",
                                        str_hdr_sec_websocket_protocol.len,
                                        str_hdr_sec_websocket_protocol.s,
-                                       str_sip.len, str_sip.s,
+                                       str_sip.len, str_sip.s);
+
+               if (ws_sub_protocols & SUB_PROTOCOL_MSRP)
+                       headers.len += snprintf(headers.s + headers.len,
+                                               HDR_BUF_LEN - headers.len,
+                                               "%.*s: %.*s\r\n",
+                                       str_hdr_sec_websocket_protocol.len,
+                                       str_hdr_sec_websocket_protocol.s,
+                                       str_msrp.len, str_msrp.s);
+
+               headers.len += snprintf(headers.s + headers.len,
+                                       HDR_BUF_LEN - headers.len,
+                                       "%.*s: %d\r\n",
                                        str_hdr_sec_websocket_version.len,
                                        str_hdr_sec_websocket_version.s,
                                        WS_VERSION);
-               ws_send_reply(msg, 400, &str_status_bad_request, NULL);
+               ws_send_reply(msg, 400, &str_status_bad_request, &headers);
                return 0;
        }
 
@@ -292,7 +323,7 @@ int ws_handle_handshake(struct sip_msg *msg)
                                base64_enc_len(SHA_DIGEST_LENGTH));
 
        /* Add the connection to the WebSocket connection table */
-       wsconn_add(msg->rcv);
+       wsconn_add(msg->rcv, sub_protocol);
 
        /* Make sure Kamailio core sends future messages on this connection
           directly to this module */
@@ -304,23 +335,38 @@ int ws_handle_handshake(struct sip_msg *msg)
        /* Now Kamailio is ready to receive WebSocket frames build and send a
           101 reply */
        headers.s = headers_buf;
-       headers.len = snprintf(headers.s, HDR_BUF_LEN,
-                       "%.*s: %.*s\r\n"
-                       "%.*s: %.*s\r\n"
-                       "%.*s: %.*s\r\n"
-                       "%.*s: %.*s\r\n",
-                       str_hdr_upgrade.len, str_hdr_upgrade.s,
-                       str_websocket.len, str_websocket.s,
-                       str_hdr_connection.len, str_hdr_connection.s,
-                       str_upgrade.len, str_upgrade.s,
-                       str_hdr_sec_websocket_accept.len,
-                       str_hdr_sec_websocket_accept.s, reply_key.len,
-                       reply_key.s, str_hdr_sec_websocket_protocol.len,
-                       str_hdr_sec_websocket_protocol.s, str_sip.len,
-                       str_sip.s);
+       headers.len = 0;
+
+       if (sub_protocol & SUB_PROTOCOL_SIP)
+               headers.len += snprintf(headers.s + headers.len,
+                                       HDR_BUF_LEN - headers.len,
+                                       "%.*s: %.*s\r\n",
+                                       str_hdr_sec_websocket_protocol.len,
+                                       str_hdr_sec_websocket_protocol.s,
+                                       str_sip.len, str_sip.s);
+       else if (sub_protocol & SUB_PROTOCOL_MSRP)
+               headers.len += snprintf(headers.s + headers.len,
+                                       HDR_BUF_LEN - headers.len,
+                                       "%.*s: %.*s\r\n",
+                                       str_hdr_sec_websocket_protocol.len,
+                                       str_hdr_sec_websocket_protocol.s,
+                                       str_msrp.len, str_msrp.s);
+
+       headers.len += snprintf(headers.s + headers.len,
+                               HDR_BUF_LEN - headers.len,
+                               "%.*s: %.*s\r\n"
+                               "%.*s: %.*s\r\n"
+                               "%.*s: %.*s\r\n",
+                               str_hdr_upgrade.len, str_hdr_upgrade.s,
+                               str_websocket.len, str_websocket.s,
+                               str_hdr_connection.len, str_hdr_connection.s,
+                               str_upgrade.len, str_upgrade.s,
+                               str_hdr_sec_websocket_accept.len,
+                               str_hdr_sec_websocket_accept.s, reply_key.len,
+                               reply_key.s);
        msg->rpl_send_flags.f &= ~SND_F_CON_CLOSE;
-       if (ws_send_reply(msg, 101,
-                               &str_status_switching_protocols, &headers) < 0)
+       if (ws_send_reply(msg, 101, &str_status_switching_protocols,
+                               &headers) < 0)
        {
                if ((wsc = wsconn_get(msg->rcv.proto_reserved1)) != NULL)
                        wsconn_rm(wsc, WSCONN_EVENTROUTE_NO);
index 38c5dc5..fa51eb0 100644 (file)
 #include "../../sr_module.h"
 #include "../../parser/msg_parser.h"
 
-stat_var *ws_failed_handshakes;
-stat_var *ws_successful_handshakes;
+enum
+{
+       SUB_PROTOCOL_SIP  = (1 << 0),
+       SUB_PROTOCOL_MSRP = (1 << 1)
+};
+#define DEFAULT_SUB_PROTOCOLS  (SUB_PROTOCOL_SIP | SUB_PROTOCOL_MSRP)
+#define SUB_PROTOCOL_ALL       (SUB_PROTOCOL_SIP | SUB_PROTOCOL_MSRP)
+extern int ws_sub_protocols;
+
+extern stat_var *ws_failed_handshakes;
+extern stat_var *ws_successful_handshakes;
 
 int ws_handle_handshake(struct sip_msg *msg);
 struct mi_root *ws_mi_disable(struct mi_root *cmd, void *param);
index ced58ee..7049bfd 100644 (file)
@@ -72,6 +72,9 @@ static param_export_t params[]=
        { "keepalive_timeout",          INT_PARAM, &ws_keepalive_timeout },
        { "ping_application_data",      STR_PARAM, &ws_ping_application_data.s},
 
+       /* ws_handshake.c */
+       { "sub_protocols",              INT_PARAM, &ws_sub_protocols},
+
        /* ws_mod.c */
        { "keepalive_interval",         INT_PARAM, &ws_keepalive_interval },
        { "keepalive_processes",        INT_PARAM, &ws_keepalive_processes },
@@ -214,6 +217,22 @@ static int mod_init(void)
                register_sync_timers(ws_keepalive_processes);
        }
 
+       if (ws_sub_protocols & SUB_PROTOCOL_MSRP
+               && !sr_event_enabled(SREV_TCP_MSRP_FRAME))
+               ws_sub_protocols &= ~SUB_PROTOCOL_MSRP;
+
+       if ((ws_sub_protocols & SUB_PROTOCOL_ALL) == 0)
+       {
+               LM_ERR("no sub-protocols enabled\n");
+               goto error;
+       }
+
+       if ((ws_sub_protocols | SUB_PROTOCOL_ALL) != SUB_PROTOCOL_ALL)
+       {
+               LM_ERR("unrecognised sub-protocols enabled\n");
+               goto error;
+       }
+
        return 0;
 
 error: