README 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. WebSocket Module
  2. Peter Dunkley
  3. Crocodile RCS Ltd
  4. Copyright © 2012-2013 Crocodile RCS Ltd
  5. __________________________________________________________________
  6. Table of Contents
  7. 1. Admin Guide
  8. 1. Overview
  9. 2. How it works
  10. 2.1. Initiating a connection
  11. 2.2. SIP message routing
  12. 2.3. MSRP message routing
  13. 3. Dependencies
  14. 3.1. Kamailio Modules
  15. 3.2. External Libraries or Applications
  16. 4. Parameters
  17. 4.1. keepalive_mechanism (integer)
  18. 4.2. keepalive_timeout (integer)
  19. 4.3. keepalive_processes (integer)
  20. 4.4. keepalive_interval (integer)
  21. 4.5. ping_application_data (string)
  22. 4.6. sub_protocols (integer)
  23. 4.7. cors_mode (integer)
  24. 5. Functions
  25. 5.1. ws_handle_handshake()
  26. 5.2. ws_close([status, reason[, connection_id]])
  27. 6. MI Commands
  28. 6.1. ws.dump
  29. 6.2. ws.close
  30. 6.3. ws.ping
  31. 6.4. ws.pong
  32. 6.5. ws.disable
  33. 6.6. ws.enable
  34. 7. Event routes
  35. 7.1. websocket:closed
  36. List of Examples
  37. 1.1. event_route[xhttp:request]
  38. 1.2. WebSocket SIP Routing
  39. 1.3. Set keepalive_mechanism parameter
  40. 1.4. Set keepalive_timeout parameter
  41. 1.5. Set keepalive_processes parameter
  42. 1.6. Set keepalive_interval parameter
  43. 1.7. Set ping_application_data parameter
  44. 1.8. Set sub_protocols parameter
  45. 1.9. Set cors_mode parameter
  46. 1.10. ws_handle_handshake usage
  47. 1.11. ws_close usage
  48. 1.12. event_route[websocket:closed] usage
  49. Chapter 1. Admin Guide
  50. Table of Contents
  51. 1. Overview
  52. 2. How it works
  53. 2.1. Initiating a connection
  54. 2.2. SIP message routing
  55. 2.3. MSRP message routing
  56. 3. Dependencies
  57. 3.1. Kamailio Modules
  58. 3.2. External Libraries or Applications
  59. 4. Parameters
  60. 4.1. keepalive_mechanism (integer)
  61. 4.2. keepalive_timeout (integer)
  62. 4.3. keepalive_processes (integer)
  63. 4.4. keepalive_interval (integer)
  64. 4.5. ping_application_data (string)
  65. 4.6. sub_protocols (integer)
  66. 4.7. cors_mode (integer)
  67. 5. Functions
  68. 5.1. ws_handle_handshake()
  69. 5.2. ws_close([status, reason[, connection_id]])
  70. 6. MI Commands
  71. 6.1. ws.dump
  72. 6.2. ws.close
  73. 6.3. ws.ping
  74. 6.4. ws.pong
  75. 6.5. ws.disable
  76. 6.6. ws.enable
  77. 7. Event routes
  78. 7.1. websocket:closed
  79. 1. Overview
  80. This module implements a WebSocket (RFC 6455) server and provides
  81. connection establishment (handshaking), management (including
  82. connection keep-alive), and framing for the SIP and MSRP WebSocket
  83. sub-protocols (draft-ietf-sipcore-sip-websocket and
  84. draft-pd-msrp-websocket).
  85. The module supports WebSockets (ws) and secure WebSockets (wss)
  86. 2. How it works
  87. 2.1. Initiating a connection
  88. 2.2. SIP message routing
  89. 2.3. MSRP message routing
  90. 2.1. Initiating a connection
  91. A WebSocket connection is initiated with an HTTP GET. The xhttp module
  92. is used to handle this GET and call the Section 5.1, “
  93. ws_handle_handshake() ” exported function.
  94. event_route[xhttp:request] should perform some validation of the HTTP
  95. headers before calling Section 5.1, “ ws_handle_handshake() ”. The
  96. event_route can also be used to make sure the HTTP GET has the correct
  97. URI, perform HTTP authentication on the WebSocket connection, and check
  98. the Origin header (RFC 6454) to ensure a browser-based SIP UA or MSRP
  99. client has been downloaded from the correct location.
  100. Example 1.1. event_route[xhttp:request]
  101. ...
  102. loadmodule "sl.so"
  103. loadmodule "xhttp.so"
  104. loadmodule "msrp.so" # Only required if using MSRP over WebSockets
  105. loadmodule "websocket.so"
  106. ...
  107. event_route[xhttp:request] {
  108. set_reply_close();
  109. set_reply_no_connect();
  110. if ($Rp != 80
  111. #!ifdef WITH_TLS
  112. && $Rp != 443
  113. #!endif
  114. ) {
  115. xlog("L_WARN", "HTTP request received on $Rp\n");
  116. xhttp_reply("403", "Forbidden", "", "");
  117. exit;
  118. }
  119. xlog("L_DBG", "HTTP Request Received\n");
  120. if ($hdr(Upgrade)=~"websocket"
  121. && $hdr(Connection)=~"Upgrade"
  122. && $rm=~"GET") {
  123. # Validate Host - make sure the client is using the correct
  124. # alias for WebSockets
  125. if ($hdr(Host) == $null || !is_myself("sip:" + $hdr(Host))) {
  126. xlog("L_WARN", "Bad host $hdr(Host)\n");
  127. xhttp_reply("403", "Forbidden", "", "");
  128. exit;
  129. }
  130. # Optional... validate Origin - make sure the client is from an
  131. # authorised website. For example,
  132. #
  133. # if ($hdr(Origin) != "http://communicator.MY_DOMAIN"
  134. # && $hdr(Origin) != "https://communicator.MY_DOMAIN") {
  135. # xlog("L_WARN", "Unauthorised client $hdr(Origin)\n");
  136. # xhttp_reply("403", "Forbidden", "", "");
  137. # exit;
  138. # }
  139. # Optional... perform HTTP authentication
  140. # ws_handle_handshake() exits (no further configuration file
  141. # processing of the request) when complete.
  142. if (ws_handle_handshake())
  143. {
  144. # Optional... cache some information about the
  145. # successful connection
  146. exit;
  147. }
  148. }
  149. xhttp_reply("404", "Not found", "", "");
  150. }
  151. ...
  152. 2.2. SIP message routing
  153. SIP over WebSockets uses invalid URIs in routing headers (Contact:,
  154. Record-Route:, and Via:) because a JavaScript stack running in a
  155. browser has no way to determine the local address from which the
  156. WebSocket connection is made. This means that the routing headers
  157. cannot be used for request or response routing in the normal manner.
  158. draft-ietf-sipcore-sip-websocket states that SIP WebSocket Clients and
  159. the SIP registrar should implement Outbound (RFC 5626) and Path (RFC
  160. 3327) to enable requests and responses to be correctly routed. However,
  161. Kamailio does not currently support Outbound and it may not be possible
  162. to guarantee all SIP WebSocket clients will support Outbound and Path.
  163. The nathelper module functions (nat_uac_test(), fix_nated_register(),
  164. add_contact_alias(), and handle_ruri_alias()) and the Kamailio core
  165. force_rport() can be used to ensure correct routing of SIP WebSocket
  166. requests without using Outbound and Path.
  167. Example 1.2. WebSocket SIP Routing
  168. ...
  169. loadmodule "sl.so"
  170. loadmodule "tm.so"
  171. ...
  172. loadmodule "websocket.so"
  173. ...
  174. request_route {
  175. # per request initial checks
  176. route(REQINIT);
  177. if (nat_uac_test(64)) {
  178. # Do NAT traversal stuff for requests from a WebSocket
  179. # connection - even if it is not behind a NAT!
  180. # This won't be needed in the future if Kamailio and the
  181. # WebSocket client support Outbound and Path.
  182. force_rport();
  183. if (is_method("REGISTER"))
  184. fix_nated_register();
  185. else {
  186. if (!add_contact_alias()) {
  187. xlog("L_ERR", "Error aliasing contact <$ct>\n");
  188. sl_send_reply("400", "Bad Request");
  189. exit;
  190. }
  191. }
  192. }
  193. if (!is_method("REGISTER"))
  194. t_on_reply("WS_REPLY");
  195. ...
  196. # Handle requests within SIP dialogs
  197. route[WITHINDLG] {
  198. if (has_totag()) {
  199. # sequential request withing a dialog should
  200. # take the path determined by record-routing
  201. if (loose_route()) {
  202. if ($du == "") {
  203. if (!handle_ruri_alias()) {
  204. xlog("L_ERR", "Bad alias <$ru>\n");
  205. sl_send_reply("400", "Bad Request");
  206. exit;
  207. }
  208. }
  209. route(RELAY);
  210. } else {
  211. if ( is_method("ACK") ) {
  212. ...
  213. onreply_route[WS_REPLY] {
  214. if (nat_uac_test(64)) {
  215. # Do NAT traversal stuff for replies to a WebSocket connection
  216. # - even if it is not behind a NAT!
  217. # This won't be needed in the future if Kamailio and the
  218. # WebSocket client support Outbound and Path.
  219. add_contact_alias();
  220. }
  221. }
  222. ...
  223. 2.3. MSRP message routing
  224. MSRP over WebSocket clients create invalid local URIs for use in Path
  225. headers (From-Path: and To-Path:) because a JavaScript stack running in
  226. a browser has no way to determine the local address from which the
  227. WebSocket connection is made. This is OK because MSRP over WebSocket
  228. clients MUST use an MSRP relay and it is the MSRP relay's
  229. responsibility to select the correct connection to the client based on
  230. the MSRP URIs that it has created (and maintains a mapping for).
  231. 3. Dependencies
  232. 3.1. Kamailio Modules
  233. 3.2. External Libraries or Applications
  234. 3.1. Kamailio Modules
  235. The following module must be loaded before this module:
  236. * sl.
  237. The following modules are required to make proper use of this module:
  238. * nathelper or outbound.
  239. * xhttp.
  240. The following module is required to use the secure WebSocket (wss)
  241. scheme:
  242. * tls.
  243. The following module is required to support MSRP over WebSockets:
  244. * msrp.
  245. 3.2. External Libraries or Applications
  246. The following libraries must be installed before running Kamailio with
  247. this module loaded:
  248. * OpenSSL.
  249. * GNU libunistring.
  250. 4. Parameters
  251. 4.1. keepalive_mechanism (integer)
  252. 4.2. keepalive_timeout (integer)
  253. 4.3. keepalive_processes (integer)
  254. 4.4. keepalive_interval (integer)
  255. 4.5. ping_application_data (string)
  256. 4.6. sub_protocols (integer)
  257. 4.7. cors_mode (integer)
  258. 4.1. keepalive_mechanism (integer)
  259. The keep-alive mechanism to use for WebSocket connections.
  260. Note
  261. If nathelper is only being used for WebSocket connections then
  262. nathelper NAT pinging is not required. If nathelper is used for
  263. WebSocket connections and TCP/TLS aliasing/NAT-traversal then WebSocket
  264. keep-alives are not required.
  265. * 0 - no WebSocket keep-alives
  266. * 1 - Ping WebSocket keep-alives
  267. * 2 - Pong WebSocket keep-alives
  268. Default value is 1.
  269. Example 1.3. Set keepalive_mechanism parameter
  270. ...
  271. modparam("websocket", "keepalive_mechanism", 0)
  272. ...
  273. 4.2. keepalive_timeout (integer)
  274. The time (in seconds) after which to send a keep-alive on idle
  275. WebSocket connections.
  276. Default value is 180.
  277. Example 1.4. Set keepalive_timeout parameter
  278. ...
  279. modparam("websocket", "keepalive_timeout", 30)
  280. ...
  281. 4.3. keepalive_processes (integer)
  282. The number of processes to start to perform WebSocket connection
  283. keep-alives.
  284. Default value is 1.
  285. Example 1.5. Set keepalive_processes parameter
  286. ...
  287. modparam("websocket", "keepalive_processes", 2)
  288. ...
  289. 4.4. keepalive_interval (integer)
  290. The number of seconds between each keep-alice process run
  291. Default value is 1.
  292. Example 1.6. Set keepalive_interval parameter
  293. ...
  294. modparam("websocket", "keepalive_interval", 2)
  295. ...
  296. 4.5. ping_application_data (string)
  297. The application data to use in keep-alive Ping and Pong frames.
  298. Default value is Kamailio Server: header content
  299. Example 1.7. Set ping_application_data parameter
  300. ...
  301. modparam("websocket", "ping_application_data", "WebSockets rock")
  302. ...
  303. 4.6. sub_protocols (integer)
  304. A bitmap that allows you to control the sub-protocols supported by the
  305. WebSocket server.
  306. * 1 - sip (draft-ietf-sipcore-sip-websocket)
  307. * 2 - msrp (draft-pd-msrp-websocket) - msrp.so must be loaded before
  308. websocket.so
  309. Default value is 1 when msrp.so is not loaded 3 when msrp.so is loaded.
  310. Example 1.8. Set sub_protocols parameter
  311. ...
  312. modparam("websocket", "sub_protocols", 2)
  313. ...
  314. 4.7. cors_mode (integer)
  315. This parameter lets you set the "Cross-origin resource sharing"
  316. behaviour of the WebSocket server.
  317. * 0 - Do not add an "Access-Control-Allow-Origin:" header to the
  318. response accepting the WebSocket handshake.
  319. * 1 - Add a "Access-Control-Allow-Origin: *" header to the response
  320. accepting the WebSocket handshake.
  321. * 2 - Add a "Access-Control-Allow-Origin:" header containing the same
  322. body as the "Origin:" header from the request to the response
  323. accepting the WebSocket handshake. If there is no "Origin:" header
  324. in the request no header will be added to the response.
  325. Default value is 0.
  326. Example 1.9. Set cors_mode parameter
  327. ...
  328. modparam("websocket", "cors_mode", 2)
  329. ...
  330. 5. Functions
  331. 5.1. ws_handle_handshake()
  332. 5.2. ws_close([status, reason[, connection_id]])
  333. 5.1. ws_handle_handshake()
  334. This function checks an HTTP GET request for the required headers and
  335. values, and (if successful) upgrades the connection from HTTP to
  336. WebSocket.
  337. This function can be used from ANY_ROUTE (but will only work in
  338. event_route[xhttp:request]).
  339. Note
  340. This function returns 0, stopping all further processing of the
  341. request, when there is a problem.
  342. Example 1.10. ws_handle_handshake usage
  343. ...
  344. ws_handle_handshake();
  345. ...
  346. 5.2. ws_close([status, reason[, connection_id]])
  347. This function closes a WebSocket connection.
  348. The function returns -1 if there is an error and 1 if it succeeds.
  349. The meaning of the parameters is as follows:
  350. * status - an integer indicating the reason for closure.
  351. * reason - a string describing the reason for closure.
  352. * connection_id - the connection to close. If not specified the
  353. connection the current message arrived on will be closed.
  354. Note
  355. status and reason values SHOULD correspond to the definitions in
  356. section 7.4 of RFC 6455. If these parameters are not used the defaults
  357. of "1000" and "Normal closure" will be used.
  358. This function can be used from ANY_ROUTE.
  359. Example 1.11. ws_close usage
  360. ...
  361. ws_close(4000, "Because I say so");
  362. ...
  363. 6. MI Commands
  364. 6.1. ws.dump
  365. 6.2. ws.close
  366. 6.3. ws.ping
  367. 6.4. ws.pong
  368. 6.5. ws.disable
  369. 6.6. ws.enable
  370. 6.1. ws.dump
  371. Provides the details of the first 50 WebSocket connections.
  372. Name: ws.dump
  373. Parameters:
  374. * order (optional) - “id_hash”, “used_desc”, or “used_asc”.
  375. Note
  376. If no parameter is provided id_hash order is used.
  377. MI FIFO Command Format:
  378. :ws.dump:fifo_reply
  379. used_asc
  380. _empty_line_
  381. 6.2. ws.close
  382. Starts the close handshake for the specified WebSocket connection.
  383. Name: ws.close
  384. Parameters:
  385. * id - WebSocket connection ID.
  386. MI FIFO Command Format:
  387. :ws.close:fifo_reply
  388. 1
  389. _empty_line_
  390. 6.3. ws.ping
  391. Sends a Ping frame on the specified WebSocket connection.
  392. Name: ws.ping
  393. Parameters:
  394. * id - WebSocket connection ID.
  395. MI FIFO Command Format:
  396. :ws.ping:fifo_reply
  397. 1
  398. _empty_line_
  399. 6.4. ws.pong
  400. Sends a Pong frame on the specified WebSocket connection.
  401. Name: ws.pong
  402. Parameters:
  403. * id - WebSocket connection ID.
  404. MI FIFO Command Format:
  405. :ws.pong:fifo_reply
  406. 1
  407. _empty_line_
  408. 6.5. ws.disable
  409. Disables WebSockets preventing new connections from being established.
  410. Name: ws.disable
  411. Parameters: none
  412. MI FIFO Command Format:
  413. :ws.disable:fifo_reply
  414. _empty_line_
  415. 6.6. ws.enable
  416. Enables WebSockets allowing new connections to be established.
  417. Note
  418. WebSockets are enabled at start-up.
  419. Name: ws.enable
  420. Parameters: none
  421. MI FIFO Command Format:
  422. :ws.enable:fifo_reply
  423. _empty_line_
  424. 7. Event routes
  425. 7.1. websocket:closed
  426. 7.1. websocket:closed
  427. When defined, the module calls event_route[websocket:closed] when a
  428. connection closes. The connection may be identified using the the $si
  429. and $sp pseudo-variables.
  430. Example 1.12. event_route[websocket:closed] usage
  431. ...
  432. event_route[websocket:closed] {
  433. xlog("L_INFO", "WebSocket connection from $si:$sp has closed\n");
  434. }
  435. ...