Skip to content

Commit

Permalink
Merge pull request #3565 from NormB/unix_socket
Browse files Browse the repository at this point in the history
Add support for MySQL UNIX Socket connections
  • Loading branch information
liviuchircu authored Jan 23, 2025
2 parents c436ec8 + f48b56e commit 36a2df6
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
30 changes: 30 additions & 0 deletions db/db_id.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ static int parse_db_url(struct db_id* id, const str* url)
ST_HOST, /* Hostname part */
ST_HOST6, /* Hostname part IPv6 */
ST_PORT, /* Port part */
ST_UNIX_SOCKET, /* Unix socket */
ST_DB, /* Database part */
ST_PARAMS /* Parameters part */
};
Expand All @@ -89,6 +90,7 @@ static int parse_db_url(struct db_id* id, const str* url)
unsigned int len, i, ipv6_flag = 0;
const char* begin;
char* prev_token = NULL;
str unix_socket_host = str_init("localhost");

if (!id || !url || !url->s) {
return -1;
Expand Down Expand Up @@ -182,6 +184,12 @@ static int parse_db_url(struct db_id* id, const str* url)
break;

case ST_HOST:
if (strncasecmp(begin, "unix(", 5) == 0) {
st = ST_UNIX_SOCKET;
i+=5;
begin = url->s + i;
break;
}
switch(url->s[i]) {
case '[':
st = ST_HOST6;
Expand Down Expand Up @@ -211,6 +219,20 @@ static int parse_db_url(struct db_id* id, const str* url)
}
break;

case ST_UNIX_SOCKET:
switch(url->s[i]) {
case ')':
if (dupl_string(&id->unix_socket, begin, url->s + i) < 0) goto err;
if (dupl_string(&id->host, unix_socket_host.s, unix_socket_host.s + unix_socket_host.len) < 0) goto err;
begin = url->s + i + 1;
if (*begin == '/') {
i++;
begin = url->s + i + 1;
}
st = ST_DB;
}
break;

case ST_PORT:
switch(url->s[i]) {
case '/':
Expand Down Expand Up @@ -249,6 +271,7 @@ static int parse_db_url(struct db_id* id, const str* url)
if (id->username) pkg_free(id->username);
if (id->password) pkg_free(id->password);
if (id->host) pkg_free(id->host);
if (id->unix_socket) pkg_free(id->unix_socket);
if (id->database) pkg_free(id->database);
if (prev_token) pkg_free(prev_token);
return -1;
Expand Down Expand Up @@ -321,6 +344,12 @@ unsigned char cmp_db_id(const struct db_id* id1, const struct db_id* id2)

if (strcasecmp(id1->host, id2->host)) return 0;

if (id1->unix_socket!=0 && id2->unix_socket!=0) {
if (strcasecmp(id1->unix_socket, id2->unix_socket)) return 0;
} else {
if (id1->unix_socket!=0 || id2->unix_socket!=0) return 0;
}

if (strcmp(id1->database, id2->database)) return 0;

if (id1->parameters != 0 && id2->parameters != 0) {
Expand All @@ -345,6 +374,7 @@ void free_db_id(struct db_id* id)
if (id->username) pkg_free(id->username);
if (id->password) pkg_free(id->password);
if (id->host) pkg_free(id->host);
if (id->unix_socket) pkg_free(id->unix_socket);
if (id->database) pkg_free(id->database);
if (id->parameters) pkg_free(id->parameters);
pkg_free(id);
Expand Down
1 change: 1 addition & 0 deletions db/db_id.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ struct db_id {
char* username; /**< Username, case sensitive */
char* password; /**< Password, case sensitive */
char* host; /**< Host or IP, case insensitive */
char* unix_socket; /**< Unix socket location */
unsigned short port; /**< Port number */
char* database; /**< Database, case sensitive */
char *parameters; /**< Parameters, case sensitive */
Expand Down
7 changes: 6 additions & 1 deletion db/doc/db-api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ prefix). This function MUST be called __FIRST__ !
The function takes two parameters, the first parameter must contain a database
connection URL or a database module name. The db_url is of the form
"mysql://username:password@host:port/database" or
"mysql://username:password@unix(/named/socket/location)/database or
"mysql" (database module name).
In the case of a database connection URL, this function looks only at the first
token (the database protocol). In the example above that would be "mysql":
Expand Down Expand Up @@ -368,14 +369,18 @@ function is called.

The function takes one parameter, the parameter must contain database
connection URL. The URL is of the form
mysql://username:password@host:port/database where:
mysql://username:password@host:port/database or
mysql://username:password@unix(/named/socket/location)/database where:

username: Username to use when logging into database (optional).
password: password if it was set (optional)
host: Hosname or IP address of the host where database server lives
(mandatory)
port: Port number of the server if the port differs from default value
(optional)
unix: Indicates that a named socket location within parens follows.
If this is specified, the host and port are not allowed. The
host is internally set to "localhost".
database: If the database server supports multiple databases, you must specify
name of the database (optional).

Expand Down
5 changes: 4 additions & 1 deletion modules/db_mysql/my_con.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,17 @@ int db_mysql_connect(struct my_con* ptr)
if (ptr->id->port) {
LM_DBG("opening connection: mysql://xxxx:xxxx@%s:%d/%s\n",
ZSW(ptr->id->host), ptr->id->port, ZSW(ptr->id->database));
} else if (ptr->id->unix_socket) {
LM_DBG("opening connection: mysql://xxxx:xxxx@unix(%s)/%s\n",
ZSW(ptr->id->unix_socket), ZSW(ptr->id->database));
} else {
LM_DBG("opening connection: mysql://xxxx:xxxx@%s/%s\n",
ZSW(ptr->id->host), ZSW(ptr->id->database));
}

if (!mysql_real_connect(ptr->con, ptr->id->host,
ptr->id->username, ptr->id->password,
ptr->id->database, ptr->id->port, 0,
ptr->id->database, ptr->id->port, ptr->id->unix_socket,
#if (MYSQL_VERSION_ID >= 40100)
CLIENT_MULTI_STATEMENTS|CLIENT_REMEMBER_OPTIONS
#else
Expand Down

0 comments on commit 36a2df6

Please sign in to comment.