%docentities; ]> &adminguide;
Overview This module implements a WebSocket (RFC 6455) server and provides connection establishment (handshaking), management (including 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)
How it works
Initiating a connection A WebSocket connection is initiated with an HTTP GET. The xhttp module is used to handle this GET and call the exported function. event_route[xhttp:request] should perform some validation of the HTTP headers before calling . 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 or MSRP client has been downloaded from the correct location. event_route[xhttp:request]
SIP message routing 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. 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 to guarantee all SIP WebSocket clients will support Outbound and Path. The nathelper module functions (nat_uac_test(), fix_nated_register(), add_contact_alias(), and handle_ruri_alias()) and the Kamailio core force_rport() can be used to ensure correct routing of SIP WebSocket requests without using Outbound and Path. WebSocket SIP Routing \n"); sl_send_reply("400", "Bad Request"); exit; } } } if (!is_method("REGISTER")) t_on_reply("WS_REPLY"); ... # Handle requests within SIP dialogs route[WITHINDLG] { if (has_totag()) { # sequential request withing a dialog should # take the path determined by record-routing if (loose_route()) { if ($du == "") { if (!handle_ruri_alias()) { xlog("L_ERR", "Bad alias <$ru>\n"); sl_send_reply("400", "Bad Request"); exit; } } route(RELAY); } else { if ( is_method("ACK") ) { ... onreply_route[WS_REPLY] { if (nat_uac_test(64)) { # Do NAT traversal stuff for replies to a WebSocket connection # - even if it is not behind a NAT! # This won't be needed in the future if Kamailio and the # WebSocket client support Outbound and Path. add_contact_alias(); } } ... ]]>
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).
Dependencies
&kamailio; Modules The following module must be loaded before this module: sl. tm. The following modules are required to make proper use of this module: nathelper or outbound. xhttp. The following module is required to use the secure WebSocket (wss) scheme: tls. The following module is required to support MSRP over WebSockets: msrp.
External Libraries or Applications The following libraries must be installed before running &kamailio; with this module loaded: OpenSSL. GNU libunistring.
Parameters
<varname>keepalive_mechanism</varname> (integer) The keep-alive mechanism to use for WebSocket connections. If nathelper is only being used for WebSocket connections then nathelper NAT pinging is not required. If nathelper is used for WebSocket connections and TCP/TLS aliasing/NAT-traversal then WebSocket keep-alives are not required. 0 - no WebSocket keep-alives 1 - Ping WebSocket keep-alives 2 - Pong WebSocket keep-alives Default value is 1. Set <varname>keepalive_mechanism</varname> parameter ... modparam("websocket", "keepalive_mechanism", 0) ...
<varname>keepalive_timeout</varname> (integer) The time (in seconds) after which to send a keep-alive on idle WebSocket connections. Default value is 180. Set <varname>keepalive_timeout</varname> parameter ... modparam("websocket", "keepalive_timeout", 30) ...
<varname>keepalive_processes</varname> (integer) The number of processes to start to perform WebSocket connection keep-alives. Default value is 1. Set <varname>keepalive_processes</varname> parameter ... modparam("websocket", "keepalive_processes", 2) ...
<varname>keepalive_interval</varname> (integer) The number of seconds between each keep-alice process run Default value is 1. Set <varname>keepalive_interval</varname> parameter ... modparam("websocket", "keepalive_interval", 2) ...
<varname>ping_application_data</varname> (string) The application data to use in keep-alive Ping and Pong frames. Default value is &kamailio; Server: header content Set <varname>ping_application_data</varname> parameter ... modparam("websocket", "ping_application_data", "WebSockets rock") ...
<varname>sub_protocols</varname> (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. Set <varname>sub_protocols</varname> parameter ... modparam("websocket", "sub_protocols", 2) ...
<varname>cors_mode</varname> (integer) This parameter lets you set the "Cross-origin resource sharing" behaviour of the WebSocket server. 0 - Do not add an "Access-Control-Allow-Origin:" header to the response accepting the WebSocket handshake. 1 - Add a "Access-Control-Allow-Origin: *" header to the response accepting the WebSocket handshake. 2 - Add a "Access-Control-Allow-Origin:" header containing the same body as the "Origin:" header from the request to the response accepting the WebSocket handshake. If there is no "Origin:" header in the request no header will be added to the response. Default value is 0. Set <varname>cors_mode</varname> parameter ... modparam("websocket", "cors_mode", 2) ...
Functions
<function moreinfo="none">ws_handle_handshake()</function> This function checks an HTTP GET request for the required headers and values, and (if successful) upgrades the connection from HTTP to WebSocket. This function can be used from ANY_ROUTE (but will only work in event_route[xhttp:request]). This function returns 0, stopping all further processing of the request, when there is a problem. <function>ws_handle_handshake</function> usage ... ws_handle_handshake(); ...
<function moreinfo="none">ws_close([status, reason[, connection_id]])</function> This function closes a WebSocket connection. The function returns -1 if there is an error and 1 if it succeeds. The meaning of the parameters is as follows: status - an integer indicating the reason for closure. reason - a string describing the reason for closure. connection_id - the connection to close. If not specified the connection the current message arrived on will be closed. status and reason values SHOULD correspond to the definitions in section 7.4 of RFC 6455. If these parameters are not used the defaults of "1000" and "Normal closure" will be used. This function can be used from ANY_ROUTE. <function>ws_close</function> usage ... ws_close(4000, "Because I say so"); ...
MI Commands
<function moreinfo="none">ws.dump</function> Provides the details of the first 50 WebSocket connections. Name: ws.dump Parameters: order (optional) - id_hash, used_desc, or used_asc. If no parameter is provided id_hash order is used. MI FIFO Command Format: :ws.dump:fifo_reply used_asc _empty_line_
<function moreinfo="none">ws.close</function> Starts the close handshake for the specified WebSocket connection. Name: ws.close Parameters: id - WebSocket connection ID. MI FIFO Command Format: :ws.close:fifo_reply 1 _empty_line_
<function moreinfo="none">ws.ping</function> Sends a Ping frame on the specified WebSocket connection. Name: ws.ping Parameters: id - WebSocket connection ID. MI FIFO Command Format: :ws.ping:fifo_reply 1 _empty_line_
<function moreinfo="none">ws.pong</function> Sends a Pong frame on the specified WebSocket connection. Name: ws.pong Parameters: id - WebSocket connection ID. MI FIFO Command Format: :ws.pong:fifo_reply 1 _empty_line_
<function moreinfo="none">ws.disable</function> Disables WebSockets preventing new connections from being established. Name: ws.disable Parameters: none MI FIFO Command Format: :ws.disable:fifo_reply _empty_line_
<function moreinfo="none">ws.enable</function> Enables WebSockets allowing new connections to be established. WebSockets are enabled at start-up. Name: ws.enable Parameters: none MI FIFO Command Format: :ws.enable:fifo_reply _empty_line_
Event routes
<function moreinfo="none">websocket:closed</function> When defined, the module calls event_route[websocket:closed] when a connection closes. The connection may be identified using the the $si and $sp pseudo-variables. <function moreinfo="none">event_route[websocket:closed]</function> usage ... event_route[websocket:closed] { xlog("L_INFO", "WebSocket connection from $si:$sp has closed\n"); } ...