Debian Codegraph @

src/modules/tcpops/README

raw
1 TCP Ops module
2
3 Camille Oudot
4
5 Orange
6
7 Olle E. Johansson
8
9 Edvina AB
10
11 Copyright © 2015 Orange
12
13 Copyright © 2015 Edvina AB, Sollentuna, Sweden
14 __________________________________________________________________
15
16 Table of Contents
17
18 1. Admin Guide
19
20 1. Overview
21 2. Parameters
22
23 2.1. closed_event (int)
24 2.2. event_callback (str)
25
26 3. Functions
27
28 3.1. tcp_conid_alive(conid)
29 3.2. tcp_con_alive(addr)
30 3.3. tcp_conid_state(conid)
31 3.4. tcp_keepalive_enable([conid], idle, count, interval)
32 3.5. tcp_keepalive_disable([conid])
33 3.6. tcp_set_connection_lifetime([conid], lifetime)
34 3.7. tcp_enable_closed_event([conid])
35 3.8. tcp_get_conid(hostport, pvname)
36 3.9. tcp_set_otcpid(conid)
37 3.10. tcp_set_otcpid_flag(mode)
38 3.11. tcp_close_connection([conid])
39
40 4. Event routes
41
42 4.1. tcp:closed
43 4.2. tcp:timeout
44 4.3. tcp:reset
45 4.4. Example
46
47 List of Examples
48
49 1.1. Set closed_event parameter
50 1.2. Set event_callback parameter
51 1.3. tcp_conid_alive usage
52 1.4. tcp_con_alive usage
53 1.5. tcp_conid_state usage
54 1.6. tcp_keepalive_enable usage
55 1.7. tcp_keepalive_disable usage
56 1.8. tcp_set_connection_lifetime usage
57 1.9. tcp_set_closed_event usage
58 1.10. tcp_get_conid usage
59 1.11. tcp_set_otcpid usage
60 1.12. tcp_set_otcpid_flag usage
61 1.13. tcp_close_connection usage
62
63 Chapter 1. Admin Guide
64
65 Table of Contents
66
67 1. Overview
68 2. Parameters
69
70 2.1. closed_event (int)
71 2.2. event_callback (str)
72
73 3. Functions
74
75 3.1. tcp_conid_alive(conid)
76 3.2. tcp_con_alive(addr)
77 3.3. tcp_conid_state(conid)
78 3.4. tcp_keepalive_enable([conid], idle, count, interval)
79 3.5. tcp_keepalive_disable([conid])
80 3.6. tcp_set_connection_lifetime([conid], lifetime)
81 3.7. tcp_enable_closed_event([conid])
82 3.8. tcp_get_conid(hostport, pvname)
83 3.9. tcp_set_otcpid(conid)
84 3.10. tcp_set_otcpid_flag(mode)
85 3.11. tcp_close_connection([conid])
86
87 4. Event routes
88
89 4.1. tcp:closed
90 4.2. tcp:timeout
91 4.3. tcp:reset
92 4.4. Example
93
94 1. Overview
95
96 This module allows Kamailio to control the TCP connection options (such
97 as the keepalive mechanism), on demand, and on a per-socket basis.
98
99 Note: the keepalive functions only work on systems with the
100 HAVE_TCP_KEEPIDLE, HAVE_TCP_KEEPCNT and HAVE_TCP_KEEPINTVL macros
101 defined (Linux, FreeBSD, DragonFly BSD, NetBSD).
102
103 2. Parameters
104
105 2.1. closed_event (int)
106 2.2. event_callback (str)
107
108 2.1. closed_event (int)
109
110 If set to 0 (globally disabled), the "tcp:closed" event route will
111 never be called on TCP disconnections.
112
113 If set to 1 (globally enabled), the "tcp:closed" event route will
114 always be called on TCP disconnections.
115
116 If set to 2 ("manual" mode), the "tcp:closed" event route will only be
117 called on TCP connections for which tcp_enable_closed_event() has been
118 applied, when a disconnection occurs.
119
120 Default value is 1 (globally enabled).
121
122 Example 1.1. Set closed_event parameter
123 ...
124 modparam("tcpops", "closed_event", 0)
125 ...
126
127 2.2. event_callback (str)
128
129 The name of the function in the kemi configuration file (embedded
130 scripting language such as Lua, Python, ...) to be executed instead of
131 event_route[...] blocks.
132
133 The function receives a string parameter with the name of the event,
134 the values are: 'tcp:closed', 'tcp:timeout', 'tcp:reset'.
135
136 Default value is 'empty' (no function is executed for events).
137
138 Example 1.2. Set event_callback parameter
139 ...
140 modparam("tcpops", "event_callback", "ksr_tcpops_event")
141 ...
142 -- event callback function implemented in Lua
143 function ksr_tcpops_event(evname)
144 KSR.info("===== tcpops module triggered event: " .. evname .. "\n");
145 return 1;
146 end
147 ...
148
149 3. Functions
150
151 3.1. tcp_conid_alive(conid)
152 3.2. tcp_con_alive(addr)
153 3.3. tcp_conid_state(conid)
154 3.4. tcp_keepalive_enable([conid], idle, count, interval)
155 3.5. tcp_keepalive_disable([conid])
156 3.6. tcp_set_connection_lifetime([conid], lifetime)
157 3.7. tcp_enable_closed_event([conid])
158 3.8. tcp_get_conid(hostport, pvname)
159 3.9. tcp_set_otcpid(conid)
160 3.10. tcp_set_otcpid_flag(mode)
161 3.11. tcp_close_connection([conid])
162
163 3.1. tcp_conid_alive(conid)
164
165 Check the state of a TCP or WS connection ID
166
167 Meaning of the parameters is as follows:
168 * conid (optional): the Kamailio internal connection id (as in the
169 $conid pseudovariable).
170
171 Return values:
172
173 1: Connection is OK
174
175 -1: Connection has errors, does not exist or is about to be closed)
176
177 Example 1.3. tcp_conid_alive usage
178 ...
179 $var(conid) = $conid;
180 if(!tcp_conid_alive("$var(conid)")) {
181 xlog("L_ERR", "Connection $conid can no longer be used\n");
182 }
183 ...
184
185 3.2. tcp_con_alive(addr)
186
187 Check the connection based on target addrt.
188
189 Meaning of the parameters is as follows:
190 * addr - target "host:port" or SIP address. The port can be omitted
191 (default to 5060) and the parameter can contain variables.
192
193 Return values:
194
195 1: connection was found
196
197 -1: connection was not found or an error occurred
198
199 Example 1.4. tcp_con_alive usage
200 ...
201 if(!tcp_con_alive("1.2.3.4:5060")) {
202 xlog("connection not found\n");
203 }
204 if(!tcp_con_alive("sip:2.3.4.5:5070")) {
205 xlog("connection not found\n");
206 }
207
208 ...
209
210 3.3. tcp_conid_state(conid)
211
212 Check the state of a TCP or WS connection ID
213
214 Meaning of the parameters is as follows:
215 * conid (optional): the Kamailio internal connection id (as in the
216 $conid pseudovariable).
217
218 Return values:
219
220 1: Connection is OK
221
222 2: Socket is accepting incoming connections
223
224 3: Socket is setting up outgoing connection
225
226 -1: Connection does not exist (or was closed)
227
228 -2: Socket has reached EOF
229
230 -3: Socket error has occurred. Connection will likely close.
231
232 -4: Socket is in unknown bad state. Connection will likely close.
233
234 Example 1.5. tcp_conid_state usage
235 ...
236 if(!tcp_conid_state("$var(conid)")) {
237 xlog("L_ERR", "Connection $conid is closed or malfunctional\n");
238 }
239 ...
240
241 3.4. tcp_keepalive_enable([conid], idle, count, interval)
242
243 Enables keepalive on a TCP connection.
244
245 Meaning of the parameters is as follows:
246 * conid (optional): the Kamailio internal connection id on which TCP
247 keepalive will be enabled. If no parameter is given, the keepalive
248 mechanism will be enabled on the current message source connection.
249 * idle (seconds): the time before the first keepalive packet is sent
250 out.
251 * count: number of non-acked keepalive before resetting the
252 connection.
253 * interval (seconds): time between two keepalive probes.
254
255 Returns 1 on success, -1 on failure.
256
257 Example 1.6. tcp_keepalive_enable usage
258 request_route {
259 if (is_method("INVITE")) {
260 $avp(caller_conid) = $conid;
261 t_on_reply("foo");
262 }
263 ...
264 }
265
266 onreply_route[foo] {
267 if (is_method("INVITE") && status == 200) {
268 # enable on callee's connection
269 tcp_keepalive_enable("60", "5", "5");
270 # enable on caller's connection
271 tcp_keepalive_enable("$avp(caller_conid)", "60", "5", "2");
272 }
273 ...
274 }
275
276 3.5. tcp_keepalive_disable([conid])
277
278 Disables keepalive on a TCP connection.
279
280 Meaning of the parameters is as follows:
281 * conid (optional): the Kamailio internal connection id on which TCP
282 keepalive will be disabled. If no parameter is given, the keepalive
283 mechanism will be disabled on the current message source
284 connection.
285
286 Returns 1 on success, -1 on failure.
287
288 Example 1.7. tcp_keepalive_disable usage
289 request_route {
290 ...
291 if (is_method("BYE")) {
292 $avp(bye_conid) = $conid;
293 t_on_reply("foo");
294 }
295 ...
296 }
297
298 onreply_route[foo] {
299 ...
300 if (is_method("BYE") && status == 200) {
301 tcp_keepalive_disable();
302 tcp_keepalive_disable("$avp(bye_conid)");
303 }
304 ...
305 }
306
307 3.6. tcp_set_connection_lifetime([conid], lifetime)
308
309 Sets the connection lifetime of a connection (TCP).
310
311 Meaning of the parameters is as follows:
312 * conid (optional): the Kamailio internal connection id on which to
313 set the new lifetime. If no parameter is given, it will be set on
314 the current message source connection.
315 * lifetime (seconds): the new connection lifetime.
316
317 Returns 1 on success, -1 on failure.
318
319 Example 1.8. tcp_set_connection_lifetime usage
320 ...
321 # use 10s as default lifetime
322 tcp_connection_lifetime=10
323 ...
324
325 request_route {
326 ...
327 if (is_method("REGISTER") && pv_www_authenticate("$td", "xxx", "0")) {
328 # raise the TCP lifetime to a bigger value
329 tcp_set_connection_lifetime("3605");
330 }
331 ...
332 }
333
334 3.7. tcp_enable_closed_event([conid])
335
336 Explicitly enables the "tcp:closed" event route on a TCP connection.
337
338 Meaning of the parameters is as follows:
339 * conid (optional): the Kamailio internal connection id. If no
340 parameter is given, it will be enabled on the current message
341 source connection.
342
343 Returns 1 on success, -1 on failure.
344
345 Example 1.9. tcp_set_closed_event usage
346 ...
347 # "tcp:closed" event route is "manual" mode
348 modparam("tcpops", "closed_event", 2)
349 ...
350
351 request_route {
352 ...
353 if (is_method("REGISTER") && pv_www_authenticate("$td", "xxx", "0")) {
354 # it will be called for this specific connection
355 tcp_enable_closed_event();
356 }
357 ...
358
359 }
360
361 event_route[tcp:closed] {
362 xlog("connection $conid was closed");
363 }
364
365 3.8. tcp_get_conid(hostport, pvname)
366
367 Get the connection id based on target host:port. The connection id is
368 set in the variable named by pvname parameter.
369
370 Meaning of the parameters is as follows:
371 * hostport - target "host:port" address, the port can be omitted
372 (default to 5060) and the parameter can contain variables.
373 * pvname - target variable name.
374
375 Return values:
376
377 1: connection was found
378
379 -1: connection was not found or an error occurred
380
381 Example 1.10. tcp_get_conid usage
382 ...
383 if(tcp_get_conid("127.0.0.1:5060", "$var(conid)")) {
384 xlog("connection id is: $var(conid)\n");
385 }
386 ...
387
388 3.9. tcp_set_otcpid(conid)
389
390 Set the value for outbound tcp connection id.
391
392 Meaning of the parameters is as follows:
393 * conid - the value of tcp connection id. It can be an integer number
394 or a variable holding an integer value.
395
396 Return values:
397 * 1: success
398 * -1: failure
399
400 Example 1.11. tcp_set_otcpid usage
401 ...
402 $var(conid) = 10;
403 tcp_set_otcpid("$var(conid)");
404 ...
405
406 3.10. tcp_set_otcpid_flag(mode)
407
408 Set or reset the internal flag for using or not the outbound tcp
409 connection id for sending out. The outbound connection id can be set by
410 module or by config using tcp_set_otcpid(...) function. An example of a
411 module setting the otcpid is registrar via lookup location function,
412 which sets the field to the connection id used to receive the
413 registration request.
414
415 Meaning of the parameters is as follows:
416 * mode - if 0, then the flag is reset, otherwise it is set.
417
418 Return values:
419 * 1: success
420 * -1: failure
421
422 Note: if you set the flag to use the outbound tcp connection id, then
423 custom config changes to the destination address, like updating the
424 r-uri ($ru) or dst uri ($du) are not resetting it, so the same already
425 set connection id is used and the SIP request might be sent to the
426 unexpected destination. Reset the flag in such case, if you set it
427 previously.
428
429 Example 1.12. tcp_set_otcpid_flag usage
430 ...
431 $var(cmode) = 1;
432 tcp_set_otcpid_flag("$var(cmode)");
433 ...
434
435 3.11. tcp_close_connection([conid])
436
437 Trigger a close of the connection corresponding to current SIP message
438 or to connection id 'conid'.
439
440 Meaning of the parameters is as follows:
441 * conid - the value of tcp connection id. It can be an integer number
442 or a variable holding an integer value.
443
444 Return values:
445 * 1: success
446 * -1 (or other negative values): failure
447
448 Example 1.13. tcp_close_connection usage
449 ...
450 $var(conid) = 10;
451 tcp_close_connection("$var(conid)");
452 ...
453
454 4. Event routes
455
456 4.1. tcp:closed
457 4.2. tcp:timeout
458 4.3. tcp:reset
459 4.4. Example
460
461 The 3 following routes are called when a socket is closed.
462
463 The corresponding $conid , $Ri , $Rp , $si , $sp and $proto variable
464 will be available in the event routes.
465
466 Whether these routes are always called, never, or on a per socket basis
467 is controlled by the closed_event parameter.
468
469 Note that the event routes can be executed by TCP main process, which
470 manages the TCP connections but does not hande the SIP traffic over
471 them. It is very important not do do any time consuming operations
472 inside the event routes. Also, many resources might not be available in
473 the TCP main process (e.g., database connections), consider using async
474 module with async_task_data(...) or async_task_group_data() functions
475 for delegating task execution to async workers.
476
477 4.1. tcp:closed
478
479 Called for each "normal" TCP socket closure by the other side
480 (FIN,ACK).
481
482 4.2. tcp:timeout
483
484 Called for connection timeouts (unanswered keepalives).
485
486 4.3. tcp:reset
487
488 Called if the connection is reset by peer (RST).
489
490 4.4. Example
491
492 ...
493 event_route[tcp:closed] {
494 xlog("L_INFO", "$proto connection closed ($conid)\n");
495 }
496
497 event_route[tcp:timeout] {
498 xlog("L_INFO", "$proto connection timed out ($conid)\n");
499 }
500
501 event_route[tcp:reset] {
502 xlog("L_INFO", "$proto connection reset by peer ($conid)\n");
503 }
504 ...