Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid DNS lookup if provided an IPv4 or IPv6 #221

Merged
merged 2 commits into from
Jan 20, 2025
Merged
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
21 changes: 15 additions & 6 deletions Source/HiveMQtt/Client/Transport/TCPTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
namespace HiveMQtt.Client.Transport;

using System.Data;

Check warning on line 18 in Source/HiveMQtt/Client/Transport/TCPTransport.cs

View workflow job for this annotation

GitHub Actions / pipeline-ubuntu-latest-dotnet-6.0.x

Using directive is unnecessary.

Check warning on line 18 in Source/HiveMQtt/Client/Transport/TCPTransport.cs

View workflow job for this annotation

GitHub Actions / pipeline-ubuntu-latest-dotnet-6.0.x

Using directive is unnecessary.

Check warning on line 18 in Source/HiveMQtt/Client/Transport/TCPTransport.cs

View workflow job for this annotation

GitHub Actions / pipeline-ubuntu-latest-dotnet-7.0.x

Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

Check warning on line 18 in Source/HiveMQtt/Client/Transport/TCPTransport.cs

View workflow job for this annotation

GitHub Actions / pipeline-ubuntu-latest-dotnet-7.0.x

Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

Check warning on line 18 in Source/HiveMQtt/Client/Transport/TCPTransport.cs

View workflow job for this annotation

GitHub Actions / pipeline-ubuntu-latest-dotnet-7.0.x

Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

Check warning on line 18 in Source/HiveMQtt/Client/Transport/TCPTransport.cs

View workflow job for this annotation

GitHub Actions / pipeline-ubuntu-latest-dotnet-7.0.x

Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

Check warning on line 18 in Source/HiveMQtt/Client/Transport/TCPTransport.cs

View workflow job for this annotation

GitHub Actions / pipeline-ubuntu-latest-dotnet-8.0.x

Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

Check warning on line 18 in Source/HiveMQtt/Client/Transport/TCPTransport.cs

View workflow job for this annotation

GitHub Actions / pipeline-ubuntu-latest-dotnet-8.0.x

Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

Check warning on line 18 in Source/HiveMQtt/Client/Transport/TCPTransport.cs

View workflow job for this annotation

GitHub Actions / pipeline-ubuntu-latest-dotnet-8.0.x

Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

Check warning on line 18 in Source/HiveMQtt/Client/Transport/TCPTransport.cs

View workflow job for this annotation

GitHub Actions / pipeline-ubuntu-latest-dotnet-9.0.x

Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

Check warning on line 18 in Source/HiveMQtt/Client/Transport/TCPTransport.cs

View workflow job for this annotation

GitHub Actions / pipeline-ubuntu-latest-dotnet-9.0.x

Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)
using System.IO.Pipelines;
using System.Net;
using System.Net.Security;
Expand Down Expand Up @@ -160,17 +161,25 @@
/// <returns>A boolean representing the success or failure of the operation.</returns>
public override async Task<bool> ConnectAsync(CancellationToken cancellationToken = default)
{
IPEndPoint ipEndPoint;
var ipAddress = await LookupHostNameAsync(this.Options.Host, this.Options.PreferIPv6).ConfigureAwait(false);
IPEndPoint? ipEndPoint = null;

// Create the IPEndPoint depending on whether it is a host name or IP address.
if (ipAddress == null)
if (IPAddress.TryParse(this.Options.Host, out var parsedIp))
{
ipEndPoint = new IPEndPoint(IPAddress.Parse(this.Options.Host), this.Options.Port);
ipEndPoint = new IPEndPoint(parsedIp, this.Options.Port);
}
else
{
ipEndPoint = new IPEndPoint(ipAddress, this.Options.Port);
var lookupResult = await LookupHostNameAsync(this.Options.Host, this.Options.PreferIPv6).ConfigureAwait(false);

if (lookupResult != null)
{
ipEndPoint = new IPEndPoint(lookupResult, this.Options.Port);
}
}

if (ipEndPoint == null)
{
throw new HiveMQttClientException("Failed to create IPEndPoint. Broker is no valid IP address or hostname.");
}

this.Socket = new(ipEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
Expand Down
Loading