Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions ext/mysqli/tests/mysqli_connect_port.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
--TEST--
mysqli_connect() with port in host
--EXTENSIONS--
mysqli
--SKIPIF--
<?php
require_once 'skipifconnectfailure.inc';
?>
--FILE--
<?php
require_once 'connect.inc';

// using port / host arguments
if (!$link = mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
printf("Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
$host, $user, $db, $port, $socket);
}

mysqli_close($link);

// using port in host
if (!$link = mysqli_connect("$host:$port", $user, $passwd, $db, "1$port", $socket)) {
printf("Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
"$host:$port", $user, $db, "1$port", $socket);
}

mysqli_close($link);
?>
Done
--EXPECTF--
Done
17 changes: 14 additions & 3 deletions ext/mysqlnd/mysqlnd_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -553,13 +553,24 @@ MYSQLND_METHOD(mysqlnd_conn_data, get_scheme)(MYSQLND_CONN_DATA * conn, MYSQLND_
port = 3306;
}

/* ipv6 addresses are in the format [address]:port */
if (hostname.s[0] != '[' && mysqlnd_fast_is_ipv6_address(hostname.s)) {
/* IPv6 without square brackets so without port */
transport.l = mnd_sprintf(&transport.s, 0, "tcp://[%s]:%u", hostname.s, port);
} else {
/* Not ipv6, but could already contain a port number, in which case we should not add an extra port.
char *p;

/* IPv6 addresses are in the format [address]:port */
if (hostname.s[0] == '[') { /* IPv6 */

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a unit test covering this logic

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not so easy, $host is inherited from env MYSQL_TEST_HOST

p = strchr(hostname.s, ']');
if (p && p[1] != ':') {
p = NULL;
}
} else { /* IPv4 or name */
p = strchr(hostname.s, ':');
}
/* Could already contain a port number, in which case we should not add an extra port.
* See GH-8978. In a port doubling scenario, the first port would be used so we do the same to keep BC. */
if (strchr(hostname.s, ':')) {
if (p) {
/* TODO: Ideally we should be able to get rid of this workaround in the future. */
transport.l = mnd_sprintf(&transport.s, 0, "tcp://%s", hostname.s);
} else {
Expand Down
Loading