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

Deferring DNS resolution until connection attempt #121

Closed
wants to merge 1 commit into from
Closed
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
70 changes: 37 additions & 33 deletions M2Mqtt/Net/MqttNetworkChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public class MqttNetworkChannel : IMqttNetworkChannel
#endif
// remote host information
private string remoteHostName;
private IPAddress remoteIpAddress;
private int remotePort;

// socket for communication
Expand All @@ -66,7 +65,41 @@ public class MqttNetworkChannel : IMqttNetworkChannel
/// <summary>
/// Remote IP address
/// </summary>
public IPAddress RemoteIpAddress { get { return this.remoteIpAddress; } }
public IPAddress RemoteIpAddress
{
get
{
try
{
// Check if remoteHostName is a valid IP address and get it
return IPAddress.Parse(remoteHostName);
}
catch
{
}

// In this case the parameter remoteHostName isn't a valid IP address
IPHostEntry hostEntry;
try
{
hostEntry = Dns.GetHostEntry(remoteHostName);
}
catch (Exception e)
{
throw new Exception("Failed to resolve the remote host name", e);
}

// check for the first address not null
// it seems that with .Net Micro Framework, the IPV6 addresses aren't supported and return "null"
foreach (IPAddress address in hostEntry.AddressList)
{
if (address != null)
return address;
}

throw new Exception("No address found for the remote host name");
}
}

/// <summary>
/// Remote port
Expand Down Expand Up @@ -180,36 +213,7 @@ public MqttNetworkChannel(string remoteHostName, int remotePort, bool secure, X5
public MqttNetworkChannel(string remoteHostName, int remotePort, bool secure, X509Certificate caCert, X509Certificate clientCert, MqttSslProtocols sslProtocol)
#endif
{
IPAddress remoteIpAddress = null;
try
{
// check if remoteHostName is a valid IP address and get it
remoteIpAddress = IPAddress.Parse(remoteHostName);
}
catch
{
}

// in this case the parameter remoteHostName isn't a valid IP address
if (remoteIpAddress == null)
{
IPHostEntry hostEntry = Dns.GetHostEntry(remoteHostName);
if ((hostEntry != null) && (hostEntry.AddressList.Length > 0))
{
// check for the first address not null
// it seems that with .Net Micro Framework, the IPV6 addresses aren't supported and return "null"
int i = 0;
while (hostEntry.AddressList[i] == null) i++;
remoteIpAddress = hostEntry.AddressList[i];
}
else
{
throw new Exception("No address found for the remote host name");
}
}

this.remoteHostName = remoteHostName;
this.remoteIpAddress = remoteIpAddress;
this.remotePort = remotePort;
this.secure = secure;
this.caCert = caCert;
Expand All @@ -226,9 +230,9 @@ public MqttNetworkChannel(string remoteHostName, int remotePort, bool secure, X5
/// </summary>
public void Connect()
{
this.socket = new Socket(this.remoteIpAddress.GetAddressFamily(), SocketType.Stream, ProtocolType.Tcp);
this.socket = new Socket(this.RemoteIpAddress.GetAddressFamily(), SocketType.Stream, ProtocolType.Tcp);
// try connection to the broker
this.socket.Connect(new IPEndPoint(this.remoteIpAddress, this.remotePort));
this.socket.Connect(new IPEndPoint(this.RemoteIpAddress, this.remotePort));

#if SSL
// secure channel requested
Expand Down