Skip to content

Commit 3532746

Browse files
committed
cachedb_redis: Fix a bug with parsing custom ports
This patch fixes an issue where custom Redis ports (i.e. not 6379) in simple URLs with just 1 host were always being parsed as 6379. Most likely, the issue dates back to the "circular failover hosts" feature (6917928). Many thanks to Artiom Druz (@Shkiperion) for a full report + PR attempt! Fixes OpenSIPS#3168 Closes OpenSIPS#3169
1 parent 14df284 commit 3532746

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

cachedb/test/test_cachedb.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,15 @@ static void test_cachedb_url(void)
765765
ok(!db->database);
766766
ok(!db->extra_options);
767767

768+
CDB_PARSE("redis://:[email protected]:6380/1");
769+
ok(db->flags == 0);
770+
ok(!strcmp(db->username, ""));
771+
ok(!strcmp(db->password, "pwd"));
772+
ok(!strcmp(db->host, "172.31.180.127"));
773+
ok(db->port == 6380);
774+
ok(!strcmp(db->database, "1"));
775+
ok(!db->extra_options);
776+
768777
CDB_PARSE("redis:group1://:[email protected]:6379/d?x=1&q=2");
769778
ok(db->flags == 0);
770779
ok(!strcmp(db->username, ""));

modules/cachedb_redis/cachedb_redis_dbase.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,9 +399,23 @@ redis_con* redis_new_connection(struct cachedb_id* id)
399399

400400
memset(con,0,sizeof(redis_con));
401401

402-
if (redis_get_hostport(&it->s, &con->host, &con->port) != 0) {
403-
LM_ERR("no more pkg\n");
404-
goto out_err;
402+
{
403+
unsigned short _, *port;
404+
405+
/* if the DSN has a custom port, inherit it now & bypass parser */
406+
if (!(id->flags & CACHEDB_ID_MULTIPLE_HOSTS) && id->port) {
407+
con->port = id->port;
408+
port = &_;
409+
} else {
410+
port = &con->port;
411+
}
412+
413+
if (redis_get_hostport(&it->s, &con->host, port) != 0) {
414+
LM_ERR("no more pkg\n");
415+
goto out_err;
416+
}
417+
418+
LM_DBG("final hostport: %s:%u\n", con->host, con->port);
405419
}
406420

407421
con->id = id;

0 commit comments

Comments
 (0)