1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
|
WebSocket Module
Peter Dunkley
Crocodile RCS Ltd
Copyright © 2012-2013 Crocodile RCS Ltd
__________________________________________________________________
Table of Contents
1. Admin Guide
1. Overview
2. How it works
2.1. Initiating a connection
2.2. SIP message routing
2.3. MSRP message routing
3. Dependencies
3.1. Kamailio Modules
3.2. External Libraries or Applications
4. Parameters
4.1. keepalive_mechanism (integer)
4.2. keepalive_timeout (integer)
4.3. keepalive_processes (integer)
4.4. keepalive_interval (integer)
4.5. ping_application_data (string)
4.6. sub_protocols (integer)
4.7. cors_mode (integer)
5. Functions
5.1. ws_handle_handshake()
5.2. ws_close([status, reason[, connection_id]])
6. MI Commands
6.1. ws.dump
6.2. ws.close
6.3. ws.ping
6.4. ws.pong
6.5. ws.disable
6.6. ws.enable
7. Event routes
7.1. websocket:closed
List of Examples
1.1. event_route[xhttp:request]
1.2. WebSocket SIP Routing
1.3. Set keepalive_mechanism parameter
1.4. Set keepalive_timeout parameter
1.5. Set keepalive_processes parameter
1.6. Set keepalive_interval parameter
1.7. Set ping_application_data parameter
1.8. Set sub_protocols parameter
1.9. Set cors_mode parameter
1.10. ws_handle_handshake usage
1.11. ws_close usage
1.12. event_route[websocket:closed] usage
Chapter 1. Admin Guide
Table of Contents
1. Overview
2. How it works
2.1. Initiating a connection
2.2. SIP message routing
2.3. MSRP message routing
3. Dependencies
3.1. Kamailio Modules
3.2. External Libraries or Applications
4. Parameters
4.1. keepalive_mechanism (integer)
4.2. keepalive_timeout (integer)
4.3. keepalive_processes (integer)
4.4. keepalive_interval (integer)
4.5. ping_application_data (string)
4.6. sub_protocols (integer)
4.7. cors_mode (integer)
5. Functions
5.1. ws_handle_handshake()
5.2. ws_close([status, reason[, connection_id]])
6. MI Commands
6.1. ws.dump
6.2. ws.close
6.3. ws.ping
6.4. ws.pong
6.5. ws.disable
6.6. ws.enable
7. Event routes
7.1. websocket:closed
1. 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)
2. How it works
2.1. Initiating a connection
2.2. SIP message routing
2.3. MSRP message routing
2.1. Initiating a connection
A WebSocket connection is initiated with an HTTP GET. The xhttp module
is used to handle this GET and call the Section 5.1, “
ws_handle_handshake() ” exported function.
event_route[xhttp:request] should perform some validation of the HTTP
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 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] {
set_reply_close();
set_reply_no_connect();
if ($Rp != 80
#!ifdef WITH_TLS
&& $Rp != 443
#!endif
) {
xlog("L_WARN", "HTTP request received on $Rp\n");
xhttp_reply("403", "Forbidden", "", "");
exit;
}
xlog("L_DBG", "HTTP Request Received\n");
if ($hdr(Upgrade)=~"websocket"
&& $hdr(Connection)=~"Upgrade"
&& $rm=~"GET") {
# Validate Host - make sure the client is using the correct
# alias for WebSockets
if ($hdr(Host) == $null || !is_myself("sip:" + $hdr(Host))) {
xlog("L_WARN", "Bad host $hdr(Host)\n");
xhttp_reply("403", "Forbidden", "", "");
exit;
}
# Optional... validate Origin - make sure the client is from an
# authorised website. For example,
#
# if ($hdr(Origin) != "http://communicator.MY_DOMAIN"
# && $hdr(Origin) != "https://communicator.MY_DOMAIN") {
# xlog("L_WARN", "Unauthorised client $hdr(Origin)\n");
# xhttp_reply("403", "Forbidden", "", "");
# exit;
# }
# Optional... perform HTTP authentication
# ws_handle_handshake() exits (no further configuration file
# processing of the request) when complete.
if (ws_handle_handshake())
{
# Optional... cache some information about the
# successful connection
exit;
}
}
xhttp_reply("404", "Not found", "", "");
}
...
2.2. 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.
Example 1.2. WebSocket SIP Routing
...
loadmodule "sl.so"
loadmodule "tm.so"
...
loadmodule "websocket.so"
...
request_route {
# per request initial checks
route(REQINIT);
if (nat_uac_test(64)) {
# Do NAT traversal stuff for requests from 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.
force_rport();
if (is_method("REGISTER"))
fix_nated_register();
else {
if (!add_contact_alias()) {
xlog("L_ERR", "Error aliasing contact <$ct>\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();
}
}
...
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
3.2. External Libraries or Applications
3.1. Kamailio Modules
The following module must be loaded before this module:
* sl.
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.
3.2. External Libraries or Applications
The following libraries must be installed before running Kamailio with
this module loaded:
* OpenSSL.
* GNU libunistring.
4. Parameters
4.1. keepalive_mechanism (integer)
4.2. keepalive_timeout (integer)
4.3. keepalive_processes (integer)
4.4. keepalive_interval (integer)
4.5. ping_application_data (string)
4.6. sub_protocols (integer)
4.7. cors_mode (integer)
4.1. keepalive_mechanism (integer)
The keep-alive mechanism to use for WebSocket connections.
Note
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.
Example 1.3. Set keepalive_mechanism parameter
...
modparam("websocket", "keepalive_mechanism", 0)
...
4.2. keepalive_timeout (integer)
The time (in seconds) after which to send a keep-alive on idle
WebSocket connections.
Default value is 180.
Example 1.4. Set keepalive_timeout parameter
...
modparam("websocket", "keepalive_timeout", 30)
...
4.3. keepalive_processes (integer)
The number of processes to start to perform WebSocket connection
keep-alives.
Default value is 1.
Example 1.5. Set keepalive_processes parameter
...
modparam("websocket", "keepalive_processes", 2)
...
4.4. keepalive_interval (integer)
The number of seconds between each keep-alice process run
Default value is 1.
Example 1.6. Set keepalive_interval parameter
...
modparam("websocket", "keepalive_interval", 2)
...
4.5. ping_application_data (string)
The application data to use in keep-alive Ping and Pong frames.
Default value is Kamailio Server: header content
Example 1.7. Set ping_application_data parameter
...
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)
...
4.7. cors_mode (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.
Example 1.9. Set cors_mode parameter
...
modparam("websocket", "cors_mode", 2)
...
5. Functions
5.1. ws_handle_handshake()
5.2. ws_close([status, reason[, connection_id]])
5.1. ws_handle_handshake()
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]).
Note
This function returns 0, stopping all further processing of the
request, when there is a problem.
Example 1.10. ws_handle_handshake usage
...
ws_handle_handshake();
...
5.2. ws_close([status, reason[, connection_id]])
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.
Note
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.
Example 1.11. ws_close usage
...
ws_close(4000, "Because I say so");
...
6. MI Commands
6.1. ws.dump
6.2. ws.close
6.3. ws.ping
6.4. ws.pong
6.5. ws.disable
6.6. ws.enable
6.1. ws.dump
Provides the details of the first 50 WebSocket connections.
Name: ws.dump
Parameters:
* order (optional) - “id_hash”, “used_desc”, or “used_asc”.
Note
If no parameter is provided id_hash order is used.
MI FIFO Command Format:
:ws.dump:fifo_reply
used_asc
_empty_line_
6.2. ws.close
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_
6.3. ws.ping
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_
6.4. ws.pong
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_
6.5. ws.disable
Disables WebSockets preventing new connections from being established.
Name: ws.disable
Parameters: none
MI FIFO Command Format:
:ws.disable:fifo_reply
_empty_line_
6.6. ws.enable
Enables WebSockets allowing new connections to be established.
Note
WebSockets are enabled at start-up.
Name: ws.enable
Parameters: none
MI FIFO Command Format:
:ws.enable:fifo_reply
_empty_line_
7. Event routes
7.1. websocket:closed
7.1. websocket:closed
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.
Example 1.12. event_route[websocket:closed] usage
...
event_route[websocket:closed] {
xlog("L_INFO", "WebSocket connection from $si:$sp has closed\n");
}
...
|