Browse code

uac: support to set contact address in uacreg database record

- enable per registration contact address
- GH #2470

ovoshlook authored on 09/09/2020 11:11:59 • Daniel-Constantin Mierla committed on 09/09/2020 11:11:59
Showing 3 changed files
... ...
@@ -487,6 +487,10 @@ modparam("uac", "reg_db_table", "uacreg")
487 487
 			host part, can have port and parameters. Must not include 'sip:'.
488 488
 			The username part of the Contact: URI will be the L_UUID field in the database.
489 489
 			</para>
490
+			<para>
491
+			A contact_addr value in the uacreg table will
492
+			override the parameter for this particular entry.
493
+			</para>
490 494
 			<example>
491 495
 				<title>Set <varname>reg_contact_addr</varname> parameter</title>
492 496
 				<programlisting format="linespecific">
... ...
@@ -1355,6 +1359,7 @@ event_route[uac:reply] {
1355 1359
 			<listitem><para>expires</para></listitem>
1356 1360
 			<listitem><para>flags</para></listitem>
1357 1361
 			<listitem><para>reg_delay</para></listitem>
1362
+			<listitem><para>contact_addr</para></listitem>
1358 1363
 			<listitem><para>socket</para></listitem>
1359 1364
 		</itemizedlist>
1360 1365
 		</para>
... ...
@@ -1456,6 +1461,12 @@ event_route[uac:reply] {
1456 1461
 			<emphasis>reg_delay</emphasis> - delay initial registration with at least reg_delay seconds, e.g.,: 3
1457 1462
 			</para></listitem>
1458 1463
 		</itemizedlist>
1464
+		<itemizedlist>
1465
+			<listitem><para>
1466
+			<emphasis>contact_addr</emphasis> - Used contact address for sending out registration requests,
1467
+			e.g.:, 192.168.0.125:5060
1468
+			</para></listitem>
1469
+		</itemizedlist>
1459 1470
 		<itemizedlist>
1460 1471
 			<listitem><para>
1461 1472
 			<emphasis>socket</emphasis> - Used socket for sending out registration requests,
... ...
@@ -59,6 +59,7 @@
59 59
 
60 60
 #define MAX_UACH_SIZE 2048
61 61
 #define UAC_REG_TM_CALLID_SIZE 90
62
+#define UAC_REG_DB_COLS_NUM 15
62 63
 
63 64
 int _uac_reg_gc_interval = 150;
64 65
 
... ...
@@ -77,6 +78,7 @@ typedef struct _reg_uac
77 78
 	str   auth_password;
78 79
 	str   auth_ha1;
79 80
 	str   callid;
81
+	str   contact_addr;
80 82
 	str   socket;
81 83
 	unsigned int cseq;
82 84
 	unsigned int flags;
... ...
@@ -140,6 +142,7 @@ str expires_column = str_init("expires");
140 142
 str flags_column = str_init("flags");
141 143
 str reg_delay_column = str_init("reg_delay");
142 144
 str socket_column = str_init("socket");
145
+str contact_addr_column = str_init("contact_addr");
143 146
 
144 147
 str str_empty = str_init("");
145 148
 
... ...
@@ -522,6 +525,12 @@ int reg_ht_add(reg_uac_t *reg)
522 525
 		LM_ERR("bad parameters: %p/%p\n", reg, _reg_htable);
523 526
 		return -1;
524 527
 	}
528
+
529
+	if (!reg->contact_addr.s || reg->contact_addr.len == 0) 
530
+	{
531
+		reg->contact_addr=reg_contact_addr;
532
+	}
533
+
525 534
 	len = reg->l_uuid.len + 1
526 535
 		+ reg->l_username.len + 1
527 536
 		+ reg->l_domain.len + 1
... ...
@@ -533,6 +542,7 @@ int reg_ht_add(reg_uac_t *reg)
533 542
 		+ reg->auth_password.len + 1
534 543
 		+ reg->auth_ha1.len + 1
535 544
 		+ reg->socket.len + 1
545
+		+ reg->contact_addr.len + 1
536 546
 		+ (reg_keep_callid ? UAC_REG_TM_CALLID_SIZE : 0) + 1;
537 547
 	nr = (reg_uac_t*)shm_malloc(sizeof(reg_uac_t) + len);
538 548
 	if(nr==NULL)
... ...
@@ -543,7 +553,7 @@ int reg_ht_add(reg_uac_t *reg)
543 553
 	memset(nr, 0, sizeof(reg_uac_t) + len);
544 554
 	nr->expires = reg->expires;
545 555
 	nr->flags   = reg->flags;
546
-	if (reg->reg_delay)
556
+	if (reg->reg_delay) 
547 557
 		nr->reg_delay = reg->reg_delay;
548 558
 	else if (reg_random_delay>0)
549 559
 		nr->reg_delay = kam_rand() % reg_random_delay;
... ...
@@ -564,6 +574,7 @@ int reg_ht_add(reg_uac_t *reg)
564 574
 	reg_copy_shm(&nr->auth_password, &reg->auth_password, 0);
565 575
 	reg_copy_shm(&nr->auth_ha1, &reg->auth_ha1, 0);
566 576
 	reg_copy_shm(&nr->socket, &reg->socket, 0);
577
+	reg_copy_shm(&nr->contact_addr, &reg->contact_addr, 0)
567 578
 	reg_copy_shm(&nr->callid, &str_empty, reg_keep_callid ? UAC_REG_TM_CALLID_SIZE : 0);
568 579
 
569 580
 	for(i=0; i<nr->auth_ha1.len; i++) {
... ...
@@ -977,7 +988,7 @@ void uac_reg_tm_callback( struct cell *t, int type, struct tmcb_params *ps)
977 988
 				"Expires: %d\r\n"
978 989
 				"%.*s",
979 990
 				ri->l_uuid.len, ri->l_uuid.s,
980
-				reg_contact_addr.len, reg_contact_addr.s,
991
+				ri->contact_addr.len, ri->contact_addr.s,
981 992
 				ri->expires,
982 993
 				new_auth_hdr->len, new_auth_hdr->s);
983 994
 		s_hdrs.s = b_hdrs; s_hdrs.len = strlen(s_hdrs.s);
... ...
@@ -1118,7 +1129,7 @@ int uac_reg_update(reg_uac_t *reg, time_t tn)
1118 1129
 			"Contact: <sip:%.*s@%.*s>\r\n"
1119 1130
 			"Expires: %d\r\n",
1120 1131
 			reg->l_uuid.len, reg->l_uuid.s,
1121
-			reg_contact_addr.len, reg_contact_addr.s,
1132
+			reg->contact_addr.len, reg->contact_addr.s,
1122 1133
 			reg->expires);
1123 1134
 	s_hdrs.s = b_hdrs; s_hdrs.len = strlen(s_hdrs.s);
1124 1135
 
... ...
@@ -1290,8 +1301,10 @@ static int uac_reg_db_to_reg(reg_uac_t *reg, db1_res_t* db_res, int i, db_key_t
1290 1301
 	reg->flags = (unsigned int)RES_ROWS(db_res)[i].values[11].val.int_val;
1291 1302
 	reg->reg_delay = (unsigned int)RES_ROWS(db_res)[i].values[12].val.int_val;
1292 1303
 
1304
+	/*contact may be empty */
1305
+	reg_db_set_attr(contact_addr, 13, 1);
1293 1306
 	/* socket may be empty */
1294
-	reg_db_set_attr(socket, 13, 1);
1307
+	reg_db_set_attr(socket, 14, 1);
1295 1308
 
1296 1309
 	return 0;
1297 1310
 }
... ...
@@ -1305,7 +1318,7 @@ int uac_reg_load_db(void)
1305 1318
 	db1_con_t *reg_db_con = NULL;
1306 1319
 	db_func_t reg_dbf;
1307 1320
 	reg_uac_t reg;
1308
-	db_key_t db_cols[14] = {
1321
+	db_key_t db_cols[UAC_REG_DB_COLS_NUM] = {
1309 1322
 		&l_uuid_column,
1310 1323
 		&l_username_column,
1311 1324
 		&l_domain_column,
... ...
@@ -1319,6 +1332,7 @@ int uac_reg_load_db(void)
1319 1332
 		&expires_column,
1320 1333
 		&flags_column,
1321 1334
 		&reg_delay_column,
1335
+		&contact_addr_column,
1322 1336
 		&socket_column
1323 1337
 	};
1324 1338
 	db1_res_t* db_res = NULL;
... ...
@@ -1364,7 +1378,7 @@ int uac_reg_load_db(void)
1364 1378
 	}
1365 1379
 
1366 1380
 	if (DB_CAPABILITY(reg_dbf, DB_CAP_FETCH)) {
1367
-		if(reg_dbf.query(reg_db_con, 0, 0, 0, db_cols, 0, 14, 0, 0) < 0)
1381
+		if(reg_dbf.query(reg_db_con, 0, 0, 0, db_cols, 0, UAC_REG_DB_COLS_NUM, 0, 0) < 0)
1368 1382
 		{
1369 1383
 			LM_ERR("Error while querying db\n");
1370 1384
 			return -1;
... ...
@@ -1383,7 +1397,7 @@ int uac_reg_load_db(void)
1383 1397
 		}
1384 1398
 	} else {
1385 1399
 		if((ret=reg_dbf.query(reg_db_con, NULL, NULL, NULL, db_cols,
1386
-						0, 14, 0, &db_res))!=0
1400
+						0, UAC_REG_DB_COLS_NUM, 0, &db_res))!=0
1387 1401
 				|| RES_ROW_N(db_res)<=0 )
1388 1402
 		{
1389 1403
 			reg_dbf.free_result(reg_db_con, db_res);
... ...
@@ -1441,7 +1455,7 @@ int uac_reg_db_refresh(str *pl_uuid)
1441 1455
 	db_func_t reg_dbf;
1442 1456
 	reg_uac_t reg;
1443 1457
 	reg_uac_t *cur_reg;
1444
-	db_key_t db_cols[14] = {
1458
+	db_key_t db_cols[UAC_REG_DB_COLS_NUM] = {
1445 1459
 		&l_uuid_column,
1446 1460
 		&l_username_column,
1447 1461
 		&l_domain_column,
... ...
@@ -1455,6 +1469,7 @@ int uac_reg_db_refresh(str *pl_uuid)
1455 1469
 		&expires_column,
1456 1470
 		&flags_column,
1457 1471
 		&reg_delay_column,
1472
+		&contact_addr_column,
1458 1473
 		&socket_column
1459 1474
 	};
1460 1475
 	db_key_t db_keys[1] = {&l_uuid_column};
... ...
@@ -1502,7 +1517,7 @@ int uac_reg_db_refresh(str *pl_uuid)
1502 1517
 	db_vals[0].val.str_val.len = pl_uuid->len;
1503 1518
 
1504 1519
 	if((ret=reg_dbf.query(reg_db_con, db_keys, NULL, db_vals, db_cols,
1505
-					1 /*nr keys*/, 14 /*nr cols*/, 0, &db_res))!=0
1520
+					1 /*nr keys*/, UAC_REG_DB_COLS_NUM /*nr cols*/, 0, &db_res))!=0
1506 1521
 			|| RES_ROW_N(db_res)<=0 )
1507 1522
 	{
1508 1523
 		reg_dbf.free_result(reg_db_con, db_res);
... ...
@@ -1841,7 +1856,7 @@ static int rpc_uac_reg_add_node_helper(rpc_t* rpc, void* ctx, reg_uac_t *reg, ti
1841 1856
 		rpc->fault(ctx, 500, "Internal error creating rpc");
1842 1857
 		return -1;
1843 1858
 	}
1844
-	if (rpc->struct_add(th, "SSSSSSSSSSddddddS",
1859
+	if (rpc->struct_add(th, "SSSSSSSSSSddddddSS",
1845 1860
 				"l_uuid",        &reg->l_uuid,
1846 1861
 				"l_username",    &reg->l_username,
1847 1862
 				"l_domain",      &reg->l_domain,
... ...
@@ -1861,6 +1876,8 @@ static int rpc_uac_reg_add_node_helper(rpc_t* rpc, void* ctx, reg_uac_t *reg, ti
1861 1876
 				"timer_expires", (int)reg->timer_expires,
1862 1877
 				"reg_init",      (int)reg->reg_init,
1863 1878
 				"reg_delay",     (int)reg->reg_delay,
1879
+				"contact_addr",  (reg->contact_addr.len)?
1880
+										&reg->contact_addr:&none,
1864 1881
 				"socket",        &reg->socket
1865 1882
 				)<0) {
1866 1883
 		rpc->fault(ctx, 500, "Internal error adding item");
... ...
@@ -2105,7 +2122,7 @@ static void rpc_uac_reg_add(rpc_t* rpc, void* ctx)
2105 2122
 	reg_uac_t reg;
2106 2123
 	reg_uac_t *cur_reg;
2107 2124
 
2108
-	if(rpc->scan(ctx, "SSSSSSSSSSdddS",
2125
+	if(rpc->scan(ctx, "SSSSSSSSSSdddSS",
2109 2126
 				&reg.l_uuid,
2110 2127
 				&reg.l_username,
2111 2128
 				&reg.l_domain,
... ...
@@ -2119,6 +2136,7 @@ static void rpc_uac_reg_add(rpc_t* rpc, void* ctx)
2119 2136
 				&reg.expires,
2120 2137
 				&reg.flags,
2121 2138
 				&reg.reg_delay,
2139
+				&reg.contact_addr,
2122 2140
 				&reg.socket
2123 2141
 			)<1)
2124 2142
 	{
... ...
@@ -23,7 +23,7 @@
23 23
 
24 24
 #include "../../core/pvar.h"
25 25
 
26
-#define UACREG_TABLE_VERSION 4
26
+#define UACREG_TABLE_VERSION 5
27 27
 
28 28
 #define UACREG_REQTO_MASK_USER 1
29 29
 #define UACREG_REQTO_MASK_AUTH 2