Skip to content

Commit

Permalink
Fix for Bug #29682333 UNABLE TO CONNECT USING IPV6(::1) PROTOCOL IN C…
Browse files Browse the repository at this point in the history
…LASSIC CONNECTION (Enhancement)

This change gives priority to IPv4 when resolving a host name.
  • Loading branch information
Roberto Garcia committed Jun 29, 2019
1 parent 1bd79ce commit 9bc4484
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
11 changes: 10 additions & 1 deletion MySQL.Data/src/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions MySQL.Data/src/Resources.resx
Expand Up @@ -553,4 +553,7 @@
<data name="LocalInfileDisabled" xml:space="preserve">
<value>Server asked for stream in response to LOAD DATA LOCAL INFILE, but the functionality is disabled by the client setting 'allowlocalinfile' to 'false'.</value>
</data>
<data name="InvalidHostNameOrAddress" xml:space="preserve">
<value>The host name or IP address is invalid.</value>
</data>
</root>
9 changes: 7 additions & 2 deletions MySQL.Data/src/common/StreamCreator.cs
Expand Up @@ -29,6 +29,7 @@
using MySql.Data.MySqlClient;
using System.IO.Pipes;
using System.Net;
using System.Linq;
#if !NETSTANDARD1_3
using MySql.Data.MySqlClient.Common;
using System.IO.MemoryMappedFiles;
Expand Down Expand Up @@ -77,7 +78,7 @@ public static Stream GetStream(MySqlConnectionStringBuilder settings)
switch (settings.ConnectionProtocol)
{
case MySqlConnectionProtocol.Tcp: return GetTcpStream(settings);
case MySqlConnectionProtocol.UnixSocket: return GetUnixSocketStream(settings);
case MySqlConnectionProtocol.UnixSocket: return GetUnixSocketStream(settings);
case MySqlConnectionProtocol.SharedMemory: return GetSharedMemoryStream(settings);
case MySqlConnectionProtocol.NamedPipe: return GetNamedPipeStream(settings);
}
Expand All @@ -88,7 +89,11 @@ private static Stream GetTcpStream(MySqlConnectionStringBuilder settings)
{
Task<IPAddress[]> dnsTask = Dns.GetHostAddressesAsync(settings.Server);
dnsTask.Wait();
IPAddress addr = dnsTask.Result?[0] ?? null;
if (dnsTask.Result == null || dnsTask.Result.Length == 0)
throw new ArgumentException(Resources.InvalidHostNameOrAddress);
IPAddress addr = dnsTask.Result.SingleOrDefault(c => c.AddressFamily == AddressFamily.InterNetwork);
if (addr == null)
addr = dnsTask.Result[0];
TcpClient client = new TcpClient(addr.AddressFamily);
Task task = client.ConnectAsync(settings.Server, (int)settings.Port);

Expand Down

0 comments on commit 9bc4484

Please sign in to comment.