Browse code

usrloc: added db_clean_tcp parameter

- if set, tcp contacts are deleted before loading location table at
start time
- it is very common that end points use only tcp client connections, on
restart those connections are lost and corresponding contacts are
useless

Daniel-Constantin Mierla authored on 10/09/2021 09:12:01
Showing 3 changed files
... ...
@@ -1517,6 +1517,29 @@ modparam("usrloc", "load_rank", 1)
1517 1517
 		</example>
1518 1518
 	</section>
1519 1519
 
1520
+	<section id="usrloc.p.db_clean_tcp">
1521
+		<title><varname>db_clean_tcp</varname> (int)</title>
1522
+		<para>
1523
+			If set to 1, when &kamailio; starts it removes the contacts with
1524
+			transport TCP, TLS or WSS, no longer loading them. Useful when
1525
+			end points do not listen for incoming connections on contact address,
1526
+			which is quite common (end point use only tcp client connections).
1527
+			On restart, connections are lost, therefore the corresponding
1528
+			contact records become useless.
1529
+		</para>
1530
+		<para>
1531
+		Default value is <quote>0</quote> (do not clean tcp contacts).
1532
+		</para>
1533
+		<example>
1534
+		<title><varname>db_clean_tcp</varname> parameter usage</title>
1535
+		<programlisting format="linespecific">
1536
+...
1537
+modparam("usrloc", "db_clean_tcp", 1)
1538
+...
1539
+		</programlisting>
1540
+		</example>
1541
+	</section>
1542
+
1520 1543
 	</section>
1521 1544
 
1522 1545
 	<section>
... ...
@@ -45,6 +45,7 @@
45 45
 #include "urecord.h"
46 46
 
47 47
 extern int ul_rm_expired_delay;
48
+extern int ul_db_clean_tcp;
48 49
 
49 50
 #ifdef STATISTICS
50 51
 static char *build_stat_name( str* domain, char *var_name)
... ...
@@ -372,6 +373,52 @@ static inline ucontact_info_t* dbrow2info(db_val_t *vals, str *contact, int rcon
372 373
 	return &ci;
373 374
 }
374 375
 
376
+/*!
377
+ * \brief Delete all location records with tcp connection
378
+ *
379
+ * \param _c database connection
380
+ * \param _d loaded domain
381
+ * \return 0 on success, -1 on failure
382
+ */
383
+int uldb_delete_tcp_records(db1_con_t* _c, udomain_t* _d)
384
+{
385
+	db_key_t keys[2];
386
+	db_op_t  ops[2];
387
+	db_val_t vals[2];
388
+	int nr_keys = 0;
389
+
390
+	LM_DBG("delete location tcp records\n");
391
+
392
+	keys[nr_keys] = &ul_con_id_col;;
393
+	ops[nr_keys] = OP_GT;
394
+	vals[nr_keys].type = DB1_INT;
395
+	vals[nr_keys].nul = 0;
396
+	vals[nr_keys].val.int_val = 0;
397
+	nr_keys++;
398
+
399
+	if (ul_db_srvid != 0) {
400
+		keys[nr_keys] = &ul_srv_id_col;
401
+		ops[nr_keys] = OP_EQ;
402
+		vals[nr_keys].type = DB1_INT;
403
+		vals[nr_keys].nul = 0;
404
+		vals[nr_keys].val.int_val = server_id;
405
+		nr_keys++;
406
+	}
407
+
408
+	if (ul_dbf.use_table(_c, _d->name) < 0) {
409
+		LM_ERR("sql use_table failed\n");
410
+		return -1;
411
+	}
412
+
413
+
414
+	if (ul_dbf.delete(_c, keys, ops, vals, nr_keys) < 0) {
415
+		LM_ERR("deleting from database failed\n");
416
+		return -1;
417
+	}
418
+
419
+	return 0;
420
+}
421
+
375 422
 
376 423
 /*!
377 424
  * \brief Load all records from a udomain
... ...
@@ -400,6 +447,10 @@ int preload_udomain(db1_con_t* _c, udomain_t* _d)
400 447
 	urecord_t* r;
401 448
 	ucontact_t* c;
402 449
 
450
+	if(ul_db_clean_tcp!=0) {
451
+		uldb_delete_tcp_records(_c, _d);
452
+	}
453
+
403 454
 	columns[0] = &ul_user_col;
404 455
 	columns[1] = &ul_contact_col;
405 456
 	columns[2] = &ul_expires_col;
... ...
@@ -176,6 +176,7 @@ int ul_desc_time_order = 0;				/*!< By default do not enable timestamp ordering
176 176
 int ul_handle_lost_tcp = 0;				/*!< By default do not remove contacts before expiration time */
177 177
 int ul_close_expired_tcp = 0;				/*!< By default do not close TCP connections for expired contacts */
178 178
 int ul_skip_remote_socket = 0;				/*!< By default do not skip remote socket */
179
+int ul_db_clean_tcp = 0;				/*!< Clean TCP/TLS/WSS contacts in DB before loading records */
179 180
 
180 181
 int ul_fetch_rows = 2000;				/*!< number of rows to fetch from result */
181 182
 int ul_hash_size = 10;
... ...
@@ -263,6 +264,7 @@ static param_export_t params[] = {
263 264
 	{"ka_loglevel",         PARAM_INT, &ul_ka_loglevel},
264 265
 	{"ka_logmsg",           PARAM_STR, &ul_ka_logmsg},
265 266
 	{"load_rank",           PARAM_INT, &ul_load_rank},
267
+	{"db_clean_tcp",        PARAM_INT, &ul_db_clean_tcp},
266 268
 	{0, 0, 0}
267 269
 };
268 270