Browse code

websocket: added $ws_conid pseudo variable - gives id of closed connection in websocket:closed event route

Juha Heinanen authored on 11/03/2016 07:51:38
Showing 4 changed files
... ...
@@ -4,7 +4,7 @@ Peter Dunkley
4 4
 
5 5
    Crocodile RCS Ltd
6 6
 
7
-   Copyright � 2012-2013 Crocodile RCS Ltd
7
+   Copyright © 2012-2013 Crocodile RCS Ltd
8 8
      __________________________________________________________________
9 9
 
10 10
    Table of Contents
... ...
@@ -51,6 +51,10 @@ Peter Dunkley
51 51
 
52 52
               7.1. websocket:closed
53 53
 
54
+        8. Exported Pseudo Variables
55
+
56
+              8.1. $ws_conid
57
+
54 58
    List of Examples
55 59
 
56 60
    1.1. event_route[xhttp:request]
... ...
@@ -65,6 +69,7 @@ Peter Dunkley
65 69
    1.10. ws_handle_handshake usage
66 70
    1.11. ws_close usage
67 71
    1.12. event_route[websocket:closed] usage
72
+   1.13. $ws_conid usage
68 73
 
69 74
 Chapter 1. Admin Guide
70 75
 
... ...
@@ -110,6 +115,10 @@ Chapter 1. Admin Guide
110 115
 
111 116
         7.1. websocket:closed
112 117
 
118
+   8. Exported Pseudo Variables
119
+
120
+        8.1. $ws_conid
121
+
113 122
 1. Overview
114 123
 
115 124
    This module implements a WebSocket (RFC 6455) server and provides
... ...
@@ -129,11 +138,11 @@ Chapter 1. Admin Guide
129 138
 2.1. Initiating a connection
130 139
 
131 140
    A WebSocket connection is initiated with an HTTP GET. The xhttp module
132
-   is used to handle this GET and call the Section 5.1, "
133
-   ws_handle_handshake() " exported function.
141
+   is used to handle this GET and call the Section 5.1, “
142
+   ws_handle_handshake() ” exported function.
134 143
 
135 144
    event_route[xhttp:request] should perform some validation of the HTTP
136
-   headers before calling Section 5.1, " ws_handle_handshake() ". The
145
+   headers before calling Section 5.1, “ ws_handle_handshake() ”. The
137 146
    event_route can also be used to make sure the HTTP GET has the correct
138 147
    URI, perform HTTP authentication on the WebSocket connection, and check
139 148
    the Origin header (RFC 6454) to ensure a browser-based SIP UA or MSRP
... ...
@@ -437,7 +446,7 @@ modparam("websocket", "cors_mode", 2)
437 446
    5.1. ws_handle_handshake()
438 447
    5.2. ws_close([status, reason[, connection_id]])
439 448
 
440
-5.1. ws_handle_handshake()
449
+5.1.  ws_handle_handshake()
441 450
 
442 451
    This function checks an HTTP GET request for the required headers and
443 452
    values, and (if successful) upgrades the connection from HTTP to
... ...
@@ -456,7 +465,7 @@ Note
456 465
 ws_handle_handshake();
457 466
 ...
458 467
 
459
-5.2. ws_close([status, reason[, connection_id]])
468
+5.2.  ws_close([status, reason[, connection_id]])
460 469
 
461 470
    This function closes a WebSocket connection.
462 471
 
... ...
@@ -497,7 +506,7 @@ ws_close(4000, "Because I say so");
497 506
    Name: ws.dump
498 507
 
499 508
    Parameters:
500
-     * order (optional) - "id_hash", "used_desc", or "used_asc".
509
+     * order (optional) - “id_hash”, “used_desc”, or “used_asc”.
501 510
 
502 511
 Note
503 512
 
... ...
@@ -582,7 +591,7 @@ Note
582 591
 
583 592
    7.1. websocket:closed
584 593
 
585
-7.1. websocket:closed
594
+7.1.  websocket:closed
586 595
 
587 596
    When defined, the module calls event_route[websocket:closed] when a
588 597
    connection closes. The connection may be identified using the the $si
... ...
@@ -594,3 +603,19 @@ event_route[websocket:closed] {
594 603
         xlog("L_INFO", "WebSocket connection from $si:$sp has closed\n");
595 604
 }
596 605
 ...
606
+
607
+8. Exported Pseudo Variables
608
+
609
+   8.1. $ws_conid
610
+
611
+8.1. $ws_conid
612
+
613
+   Connection id of closed websocket connection. Can only be used in
614
+   websocket:closed event route.
615
+
616
+   Example 1.13. $ws_conid usage
617
+...
618
+event_route[websocket:closed] {
619
+        xlog("L_INFO", "WebSocket connection with id $ws_conid has closed\n");
620
+}
621
+...
... ...
@@ -632,6 +632,29 @@ event_route[websocket:closed] {
632 632
 		</example>
633 633
 	</section>
634 634
 
635
-	</section>	
635
+	</section>
636
+    
637
+	<section>
638
+		<title>Exported Pseudo Variables</title>
639
+		<section>
640
+			<title><function moreinfo="none">$ws_conid</function></title>
641
+			<para>
642
+			Connection id of closed websocket connection.  Can only be
643
+            used in websocket:closed event route.
644
+			</para>
645
+		<example>
646
+		<title>$ws_conid usage</title>
647
+		<programlisting format="linespecific">
648
+...
649
+event_route[websocket:closed] {
650
+        xlog("L_INFO", "WebSocket connection with id $ws_conid has closed\n");
651
+}
652
+...
653
+		</programlisting>
654
+		</example>
655
+	        </section>
656
+
657
+	</section>
658
+
636 659
 </chapter>
637 660
 
... ...
@@ -292,7 +292,9 @@ static void wsconn_run_route(ws_connection_t *wsc)
292 292
 		LM_ERR("faked_msg_init() failed\n");
293 293
 		return;
294 294
 	}
295
+	
295 296
 	fmsg = faked_msg_next();
297
+	wsc->rcv.proto_reserved1 = wsc->id;
296 298
 	fmsg->rcv = wsc->rcv;
297 299
 
298 300
 	backup_rt = get_route_type();
... ...
@@ -52,6 +52,7 @@ static int mod_init(void);
52 52
 static int child_init(int rank);
53 53
 static void destroy(void);
54 54
 static int ws_close_fixup(void** param, int param_no);
55
+static int pv_get_ws_conid_f(struct sip_msg *, pv_param_t *, pv_value_t *);
55 56
 
56 57
 sl_api_t ws_slb;
57 58
 
... ...
@@ -159,6 +160,12 @@ static mi_export_t mi_cmds[] =
159 160
 	{ 0, 0, 0, 0, 0 }
160 161
 };
161 162
 
163
+static pv_export_t mod_pvs[] = {
164
+    {{"ws_conid", (sizeof("ws_conid")-1)}, PVT_CONTEXT,
165
+     pv_get_ws_conid_f, 0, 0, 0, 0, 0},
166
+    {{0, 0}, 0, 0, 0, 0, 0, 0, 0}
167
+};
168
+
162 169
 struct module_exports exports= 
163 170
 {
164 171
 	"websocket",
... ...
@@ -167,7 +174,7 @@ struct module_exports exports=
167 174
 	params,			/* Exported parameters */
168 175
 	stats,			/* exported statistics */
169 176
 	mi_cmds,		/* exported MI functions */
170
-	0,			/* exported pseudo-variables */
177
+	mod_pvs,                /* exported pseudo-variables */
171 178
 	0,			/* extra processes */
172 179
 	mod_init,		/* module initialization function */
173 180
 	0,			/* response function */
... ...
@@ -341,3 +348,11 @@ static int ws_close_fixup(void** param, int param_no)
341 348
 		return 0;
342 349
 	}
343 350
 }
351
+
352
+static int pv_get_ws_conid_f(struct sip_msg *msg, pv_param_t *param,
353
+			     pv_value_t *res)
354
+{
355
+    if (msg == NULL) return -1;
356
+
357
+    return pv_get_sintval(msg, param, res, msg->rcv.proto_reserved1);
358
+}