Debian Codegraph @

src/modules/websocket/README

raw
1 WebSocket Module
2
3 Peter Dunkley
4
5 Crocodile RCS Ltd
6
7 Copyright © 2012-2013 Crocodile RCS Ltd
8 __________________________________________________________________
9
10 Table of Contents
11
12 1. Admin Guide
13
14 1. Overview
15 2. How it works
16
17 2.1. Initiating a connection
18 2.2. SIP message routing
19 2.3. MSRP message routing
20
21 3. Dependencies
22
23 3.1. Kamailio Modules
24 3.2. External Libraries or Applications
25
26 4. Parameters
27
28 4.1. keepalive_mechanism (integer)
29 4.2. keepalive_timeout (integer)
30 4.3. keepalive_processes (integer)
31 4.4. keepalive_interval (integer)
32 4.5. ping_application_data (string)
33 4.6. sub_protocols (integer)
34 4.7. cors_mode (integer)
35 4.8. verbose_list (int)
36 4.9. event_callback (str)
37 4.10. timer_interval (integer)
38 4.11. rm_delay_interval (integer)
39
40 5. Functions
41
42 5.1. ws_handle_handshake()
43 5.2. ws_close([status, reason[, connection_id]])
44
45 6. RPC Commands
46
47 6.1. ws.dump
48 6.2. ws.close
49 6.3. ws.ping
50 6.4. ws.pong
51 6.5. ws.disable
52 6.6. ws.enable
53
54 7. Event routes
55
56 7.1. websocket:closed
57
58 8. Exported Pseudo Variables
59
60 8.1. $ws_conid
61
62 List of Examples
63
64 1.1. event_route[xhttp:request]
65 1.2. WebSocket SIP Routing
66 1.3. Set keepalive_mechanism parameter
67 1.4. Set keepalive_timeout parameter
68 1.5. Set keepalive_processes parameter
69 1.6. Set keepalive_interval parameter
70 1.7. Set ping_application_data parameter
71 1.8. Set sub_protocols parameter
72 1.9. Set cors_mode parameter
73 1.10. Set verbose_list parameter
74 1.11. Set event_callback parameter
75 1.12. Set timer_interval parameter
76 1.13. Set rm_delay_intervalparameter
77 1.14. ws_handle_handshake usage
78 1.15. ws_close usage
79 1.16. event_route[websocket:closed] usage
80 1.17. $ws_conid usage
81
82 Chapter 1. Admin Guide
83
84 Table of Contents
85
86 1. Overview
87 2. How it works
88
89 2.1. Initiating a connection
90 2.2. SIP message routing
91 2.3. MSRP message routing
92
93 3. Dependencies
94
95 3.1. Kamailio Modules
96 3.2. External Libraries or Applications
97
98 4. Parameters
99
100 4.1. keepalive_mechanism (integer)
101 4.2. keepalive_timeout (integer)
102 4.3. keepalive_processes (integer)
103 4.4. keepalive_interval (integer)
104 4.5. ping_application_data (string)
105 4.6. sub_protocols (integer)
106 4.7. cors_mode (integer)
107 4.8. verbose_list (int)
108 4.9. event_callback (str)
109 4.10. timer_interval (integer)
110 4.11. rm_delay_interval (integer)
111
112 5. Functions
113
114 5.1. ws_handle_handshake()
115 5.2. ws_close([status, reason[, connection_id]])
116
117 6. RPC Commands
118
119 6.1. ws.dump
120 6.2. ws.close
121 6.3. ws.ping
122 6.4. ws.pong
123 6.5. ws.disable
124 6.6. ws.enable
125
126 7. Event routes
127
128 7.1. websocket:closed
129
130 8. Exported Pseudo Variables
131
132 8.1. $ws_conid
133
134 1. Overview
135
136 This module implements a WebSocket (RFC 6455) server and provides
137 connection establishment (handshaking), management (including
138 connection keep-alive), and framing for the SIP and MSRP WebSocket
139 sub-protocols (RFC 7118 and RFC 7977).
140
141 The module supports WebSockets (ws) and secure WebSockets (wss)
142 transports
143
144 2. How it works
145
146 2.1. Initiating a connection
147 2.2. SIP message routing
148 2.3. MSRP message routing
149
150 2.1. Initiating a connection
151
152 A WebSocket connection is initiated with an HTTP GET. The xhttp module
153 is used to handle this GET and call the Section 5.1, “
154 ws_handle_handshake() ” exported function.
155
156 event_route[xhttp:request] should perform some validation of the HTTP
157 headers before calling Section 5.1, “ ws_handle_handshake() ”. The
158 event_route can also be used to make sure the HTTP GET has the correct
159 URI, perform HTTP authentication on the WebSocket connection, and check
160 the Origin header (RFC 6454) to ensure a browser-based SIP UA or MSRP
161 client has been downloaded from the correct location.
162
163 Example 1.1. event_route[xhttp:request]
164 ...
165 tcp_accept_no_cl=yes
166 listen=tcp:127.0.0.17:80
167 listen=tls:127.0.0.17:443
168 ...
169 loadmodule "sl.so"
170 loadmodule "xhttp.so"
171 loadmodule "msrp.so" # Only required if using MSRP over WebSockets
172 loadmodule "websocket.so"
173 ...
174 event_route[xhttp:request] {
175 set_reply_close();
176 set_reply_no_connect();
177
178 if ($Rp != 80
179 #!ifdef WITH_TLS
180 && $Rp != 443
181 #!endif
182 ) {
183
184 xlog("L_WARN", "HTTP request received on $Rp\n");
185 xhttp_reply("403", "Forbidden", "", "");
186 exit;
187 }
188
189 xlog("L_DBG", "HTTP Request Received\n");
190
191 if ($hdr(Upgrade)=~"websocket"
192 && $hdr(Connection)=~"Upgrade"
193 && $rm=~"GET") {
194
195 # Validate Host - make sure the client is using the correct
196 # alias for WebSockets
197 if ($hdr(Host) == $null || !is_myself("sip:" + $hdr(Host))) {
198 xlog("L_WARN", "Bad host $hdr(Host)\n");
199 xhttp_reply("403", "Forbidden", "", "");
200 exit;
201 }
202
203 # Optional... validate Origin - make sure the client is from an
204 # authorised website. For example,
205 #
206 # if ($hdr(Origin) != "http://communicator.MY_DOMAIN"
207 # && $hdr(Origin) != "https://communicator.MY_DOMAIN") {
208 # xlog("L_WARN", "Unauthorised client $hdr(Origin)\n");
209 # xhttp_reply("403", "Forbidden", "", "");
210 # exit;
211 # }
212
213 # Optional... perform HTTP authentication
214
215 # ws_handle_handshake() exits (no further configuration file
216 # processing of the request) when complete in case of failure.
217 if (ws_handle_handshake())
218 {
219 # Optional... cache some information about the
220 # successful connection
221 exit;
222 }
223 }
224
225 xhttp_reply("404", "Not found", "", "");
226 }
227 ...
228
229 2.2. SIP message routing
230
231 SIP over WebSockets uses invalid URIs in routing headers (Contact:,
232 Record-Route:, and Via:) because a JavaScript stack running in a
233 browser has no way to determine the local address from which the
234 WebSocket connection is made. This means that the routing headers
235 cannot be used for request or response routing in the normal manner.
236
237 RFC 7118 - The WebSocket Protocol as a Transport for the Session
238 Initiation Protocol - states that SIP WebSocket Clients and the SIP
239 registrar should implement SIP Outbound (RFC 5626) and Path header
240 support (RFC 3327) to enable requests and responses to be correctly
241 routed. Kamailio has a module called "Outbound" for this functionality.
242
243 The nathelper module functions (nat_uac_test(), fix_nated_register(),
244 add_contact_alias(), and handle_ruri_alias()) and the Kamailio core
245 force_rport() can be used to ensure correct routing of SIP WebSocket
246 requests without using Outbound and Path.
247
248 Example 1.2. WebSocket SIP Routing
249 ...
250 loadmodule "sl.so"
251 loadmodule "tm.so"
252 loadmodule "xhttp.so"
253 ...
254 loadmodule "websocket.so"
255 ...
256 request_route {
257
258 # per request initial checks
259 route(REQINIT);
260
261 if (nat_uac_test(64)) {
262 # Do NAT traversal stuff for requests from a WebSocket
263 # connection - even if it is not behind a NAT!
264 # This won't be needed in the future if Kamailio and the
265 # WebSocket client support Outbound and Path.
266 force_rport();
267 if (is_method("REGISTER"))
268 fix_nated_register();
269 else {
270 if (!add_contact_alias()) {
271 xlog("L_ERR", "Error aliasing contact <$ct>\n");
272 sl_send_reply("400", "Bad Request");
273 exit;
274 }
275 }
276 }
277
278 if (!is_method("REGISTER"))
279 t_on_reply("WS_REPLY");
280 ...
281 # Handle requests within SIP dialogs
282 route[WITHINDLG] {
283 if (has_totag()) {
284 # sequential request within a dialog should
285 # take the path determined by record-routing
286 if (loose_route()) {
287 if ($du == "") {
288 if (!handle_ruri_alias()) {
289 xlog("L_ERR", "Bad alias <$ru>\n");
290 sl_send_reply("400", "Bad Request");
291 exit;
292 }
293 }
294 route(RELAY);
295 } else {
296 if ( is_method("ACK") ) {
297 ...
298 onreply_route[WS_REPLY] {
299 if (nat_uac_test(64)) {
300 # Do NAT traversal stuff for replies to a WebSocket connection
301 # - even if it is not behind a NAT!
302 # This won't be needed in the future if Kamailio and the
303 # WebSocket client support Outbound and Path.
304 add_contact_alias();
305 }
306 }
307 ...
308
309 2.3. MSRP message routing
310
311 MSRP over WebSocket clients create invalid local URIs for use in Path
312 headers (From-Path: and To-Path:) because a JavaScript stack running in
313 a browser has no way to determine the local address from which the
314 WebSocket connection is made. This is OK because MSRP over WebSocket
315 clients MUST use an MSRP relay and it is the MSRP relay's
316 responsibility to select the correct connection to the client based on
317 the MSRP URIs that it has created (and maintains a mapping for).
318
319 3. Dependencies
320
321 3.1. Kamailio Modules
322 3.2. External Libraries or Applications
323
324 3.1. Kamailio Modules
325
326 The following module must be loaded before this module:
327 * sl.
328 * tm.
329
330 The following modules are required to make proper use of this module:
331 * nathelper or outbound.
332 * xhttp.
333
334 The following module is required to use the secure WebSocket (wss)
335 scheme:
336 * tls.
337
338 The following module is required to support MSRP over WebSockets:
339 * msrp.
340
341 3.2. External Libraries or Applications
342
343 The following libraries must be installed before running Kamailio with
344 this module loaded:
345 * OpenSSL.
346 * GNU libunistring.
347
348 4. Parameters
349
350 4.1. keepalive_mechanism (integer)
351 4.2. keepalive_timeout (integer)
352 4.3. keepalive_processes (integer)
353 4.4. keepalive_interval (integer)
354 4.5. ping_application_data (string)
355 4.6. sub_protocols (integer)
356 4.7. cors_mode (integer)
357 4.8. verbose_list (int)
358 4.9. event_callback (str)
359 4.10. timer_interval (integer)
360 4.11. rm_delay_interval (integer)
361
362 4.1. keepalive_mechanism (integer)
363
364 The keep-alive mechanism to use for WebSocket connections.
365
366 Note
367
368 If nathelper is only being used for WebSocket connections then
369 nathelper NAT pinging is not required. If nathelper is used for
370 WebSocket connections and TCP/TLS aliasing/NAT-traversal then WebSocket
371 keep-alives are not required.
372
373 * 0 - no WebSocket keep-alives
374 * 1 - Ping WebSocket keep-alives
375 * 2 - Pong WebSocket keep-alives
376
377 Default value is 1.
378
379 Example 1.3. Set keepalive_mechanism parameter
380 ...
381 modparam("websocket", "keepalive_mechanism", 0)
382 ...
383
384 4.2. keepalive_timeout (integer)
385
386 The time (in seconds) after which to send a keep-alive on idle
387 WebSocket connections.
388
389 Default value is 180.
390
391 Example 1.4. Set keepalive_timeout parameter
392 ...
393 modparam("websocket", "keepalive_timeout", 30)
394 ...
395
396 4.3. keepalive_processes (integer)
397
398 The number of processes to start to perform WebSocket connection
399 keep-alives.
400
401 Default value is 1.
402
403 Example 1.5. Set keepalive_processes parameter
404 ...
405 modparam("websocket", "keepalive_processes", 2)
406 ...
407
408 4.4. keepalive_interval (integer)
409
410 The number of seconds between each keep-alive process run
411
412 Default value is 1.
413
414 Example 1.6. Set keepalive_interval parameter
415 ...
416 modparam("websocket", "keepalive_interval", 2)
417 ...
418
419 4.5. ping_application_data (string)
420
421 The application data to use in keep-alive Ping and Pong frames.
422
423 Default value is Kamailio Server: header content
424
425 Example 1.7. Set ping_application_data parameter
426 ...
427 modparam("websocket", "ping_application_data", "WebSockets rock")
428 ...
429
430 4.6. sub_protocols (integer)
431
432 A bitmap that allows you to control the sub-protocols supported by the
433 WebSocket server.
434 * 1 - sip (RFC 7118)
435 * 2 - msrp (RFC 7977) - the msrp module must be loaded before the
436 websocket module
437
438 Default value is 1 when msrp module is not loaded, 3 when msrp module
439 is loaded.
440
441 Example 1.8. Set sub_protocols parameter
442 ...
443 modparam("websocket", "sub_protocols", 2)
444 ...
445
446 4.7. cors_mode (integer)
447
448 This parameter lets you set the "Cross-origin resource sharing"
449 behaviour of the WebSocket server.
450 * 0 - Do not add an "Access-Control-Allow-Origin:" header to the
451 response accepting the WebSocket handshake.
452 * 1 - Add an "Access-Control-Allow-Origin: *" header to the response
453 accepting the WebSocket handshake.
454 * 2 - Add an "Access-Control-Allow-Origin:" header containing the
455 same body as the "Origin:" header from the request to the response
456 accepting the WebSocket handshake. If there is no "Origin:" header
457 in the request no header will be added to the response.
458
459 Default value is 0.
460
461 Example 1.9. Set cors_mode parameter
462 ...
463 modparam("websocket", "cors_mode", 2)
464 ...
465
466 4.8. verbose_list (int)
467
468 Allows to enable/disable the printing of debug messages when getting
469 the list of websocket connections. If enabled, it prints debug messages
470 every second for ping operations.
471
472 Default value is 0 (disabled).
473
474 Example 1.10. Set verbose_list parameter
475 ...
476 modparam("websocket", "verbose_list", 1)
477 ...
478
479 4.9. event_callback (str)
480
481 The name of the function in the kemi configuration file (embedded
482 scripting language such as Lua, Python, ...) to be executed instead of
483 event_route[...] blocks specific for websocket module.
484
485 The function has one string parameter, the value is the name of the
486 event_route block, respectively "websocket:closed".
487
488 Default value is 'empty' (no function is executed for events).
489
490 Example 1.11. Set event_callback parameter
491 ...
492 modparam("websocket", "event_callback", "ksr_websocket_event")
493 ...
494 -- event callback function implemented in Lua
495 function ksr_websocket_event(evname)
496 KSR.info("===== websocket module triggered event: " .. evname .. "\n");
497 return 1;
498 end
499 ...
500
501 4.10. timer_interval (integer)
502
503 The number of seconds between each timer process run
504
505 Default value is 1.
506
507 Example 1.12. Set timer_interval parameter
508 ...
509 modparam("websocket", "timer_interval", 5)
510 ...
511
512 4.11. rm_delay_interval (integer)
513
514 The number of seconds to wait before destroying the websocket
515 connection once put in remove state.
516
517 Default value is 5.
518
519 Example 1.13. Set rm_delay_intervalparameter
520 ...
521 modparam("websocket", "rm_delay_interval", 2)
522 ...
523
524 5. Functions
525
526 5.1. ws_handle_handshake()
527 5.2. ws_close([status, reason[, connection_id]])
528
529 5.1. ws_handle_handshake()
530
531 This function checks an HTTP GET request for the required headers and
532 values, and (if successful) upgrades the connection from HTTP to
533 WebSocket.
534
535 This function can be used from ANY_ROUTE (but will only work in
536 event_route[xhttp:request]).
537
538 Note
539
540 This function returns 0, stopping all further processing of the
541 request, when there is a problem. Otherwise, it returns 1 (or positive
542 number) in case of success.
543
544 Example 1.14. ws_handle_handshake usage
545 ...
546 ws_handle_handshake();
547 ...
548
549 5.2. ws_close([status, reason[, connection_id]])
550
551 This function closes a WebSocket connection.
552
553 The function returns -1 if there is an error and 1 if it succeeds.
554
555 The meaning of the parameters is as follows:
556 * status - an integer indicating the reason for closure.
557 * reason - a string describing the reason for closure.
558 * connection_id - the connection to close. If not specified the
559 connection the current message arrived on will be closed.
560
561 Note
562
563 status and reason values SHOULD correspond to the definitions in
564 section 7.4 of RFC 6455. If these parameters are not used the defaults
565 of "1000" and "Normal closure" will be used.
566
567 This function can be used from ANY_ROUTE.
568
569 Example 1.15. ws_close usage
570 ...
571 ws_close(4000, "Because I say so");
572 ...
573
574 6. RPC Commands
575
576 6.1. ws.dump
577 6.2. ws.close
578 6.3. ws.ping
579 6.4. ws.pong
580 6.5. ws.disable
581 6.6. ws.enable
582
583 6.1. ws.dump
584
585 Provides the details of the first 50 WebSocket connections.
586
587 Name: ws.dump
588
589 Parameters:
590 * order (optional) - “id_hash”, “used_desc”, or “used_asc”.
591
592 Note
593
594 If no parameter is provided id_hash order is used.
595
596 RPC Command Usage:
597 ...
598 kamctl rpc ws.dump used_asc
599 ...
600
601 6.2. ws.close
602
603 Starts the close handshake for the specified WebSocket connection.
604
605 Name: ws.close
606
607 Parameters:
608 * id - WebSocket connection ID.
609
610 RPC Command Usage:
611 ...
612 kamctl rpc ws.close: 1
613 ...
614
615 6.3. ws.ping
616
617 Sends a Ping frame on the specified WebSocket connection.
618
619 Name: ws.ping
620
621 Parameters:
622 * id - WebSocket connection ID.
623
624 RPC Command Usage:
625 ...
626 kamctl rpc ws.ping 1
627 ...
628
629 6.4. ws.pong
630
631 Sends a Pong frame on the specified WebSocket connection.
632
633 Name: ws.pong
634
635 Parameters:
636 * id - WebSocket connection ID.
637
638 RPC Command Usage:
639 ...
640 kamctl rpc ws.pong 1
641 ...
642
643 6.5. ws.disable
644
645 Disables WebSockets preventing new connections from being established.
646
647 Name: ws.disable
648
649 Parameters: none
650
651 RPC Command Usage:
652 ...
653 kamctl rpc ws.disable
654 ...
655
656 6.6. ws.enable
657
658 Enables WebSockets allowing new connections to be established.
659
660 Note
661
662 WebSockets are enabled at start-up.
663
664 Name: ws.enable
665
666 Parameters: none
667
668 RPC Command Format:
669 ...
670 kamctl rpc ws.enable
671 ...
672
673 7. Event routes
674
675 7.1. websocket:closed
676
677 7.1. websocket:closed
678
679 When defined, the module calls event_route[websocket:closed] when a
680 connection closes. The connection may be identified using the $si and
681 $sp pseudo-variables.
682
683 Example 1.16. event_route[websocket:closed] usage
684 ...
685 event_route[websocket:closed] {
686 xlog("L_INFO", "WebSocket connection from $si:$sp has closed\n");
687 }
688 ...
689
690 8. Exported Pseudo Variables
691
692 8.1. $ws_conid
693
694 8.1. $ws_conid
695
696 Connection id of closed websocket connection. Can only be used in
697 websocket:closed event route.
698
699 Example 1.17. $ws_conid usage
700 ...
701 event_route[websocket:closed] {
702 xlog("L_INFO", "WebSocket connection with id $ws_conid has closed\n");
703 }
704 ...