Skip to content

Commit

Permalink
Add configurable hostname lookup
Browse files Browse the repository at this point in the history
Signed-off-by: James Tomson <[email protected]>
  • Loading branch information
jtomson committed May 26, 2022
1 parent 240f82f commit 908739f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.eclipse.paho.client.mqttv3;

import java.net.InetAddress;
import java.net.UnknownHostException;

public interface IMqttDns {
InetAddress lookup(String host) throws UnknownHostException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public class MqttConnectOptions {
private boolean automaticReconnect = false;
private int maxReconnectDelay = 128000;
private Properties customWebSocketHeaders = null;
private IMqttDns dns = null;

// Client Operation Parameters
private int executorServiceTimeout = 1; // How long to wait in seconds when terminating the executor service.
Expand Down Expand Up @@ -646,6 +647,14 @@ public int getExecutorServiceTimeout() {
return executorServiceTimeout;
}

public IMqttDns getDns() {
return dns;
}

public void setDns(IMqttDns dns) {
this.dns = dns;
}

/**
* Set the time in seconds that the executor service should wait when
* terminating before forcefully terminating. It is not recommended to change
Expand Down Expand Up @@ -679,6 +688,11 @@ public Properties getDebug() {
} else {
p.put("SSLProperties", getSSLProperties());
}
if (getDns() == null) {
p.put("Dns", strNull);
} else {
p.put("Dns", getDns());
}
return p;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.InetAddress;

import javax.net.SocketFactory;

import org.eclipse.paho.client.mqttv3.IMqttDns;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.logging.Logger;
import org.eclipse.paho.client.mqttv3.logging.LoggerFactory;
Expand All @@ -41,6 +43,7 @@ public class TCPNetworkModule implements NetworkModule {
private String host;
private int port;
private int conTimeout;
private IMqttDns dns;

/**
* Constructs a new TCPNetworkModule using the specified host and
Expand All @@ -59,6 +62,14 @@ public TCPNetworkModule(SocketFactory factory, String host, int port, String res

}

public TCPNetworkModule(SocketFactory factory, String host, int port, String resourceContext, IMqttDns dns) {
log.setResourceName(resourceContext);
this.factory = factory;
this.host = host;
this.port = port;
this.dns = dns;
}

/**
* Starts the module, by creating a TCP socket to the server.
* @throws IOException if there is an error creating the socket
Expand All @@ -69,7 +80,13 @@ public void start() throws IOException, MqttException {
try {
// @TRACE 252=connect to host {0} port {1} timeout {2}
log.fine(CLASS_NAME,methodName, "252", new Object[] {host, Integer.valueOf(port), Long.valueOf(conTimeout*1000)});
SocketAddress sockaddr = new InetSocketAddress(host, port);
final SocketAddress sockaddr;
if (dns != null) {
InetAddress inetAddr = dns.lookup(host);
sockaddr = new InetSocketAddress(inetAddr, port);
} else {
sockaddr = new InetSocketAddress(host, port); // default system resolver
}
socket = factory.createSocket();
socket.connect(sockaddr, conTimeout*1000);
socket.setSoTimeout(1000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public NetworkModule createNetworkModule(URI brokerUri, MqttConnectOptions optio
} else if (factory instanceof SSLSocketFactory) {
throw ExceptionHelper.createMqttException(MqttException.REASON_CODE_SOCKET_FACTORY_MISMATCH);
}
TCPNetworkModule networkModule = new TCPNetworkModule(factory, host, port, clientId);
TCPNetworkModule networkModule = new TCPNetworkModule(factory, host, port, clientId, options.getDns());
networkModule.setConnectTimeout(options.getConnectionTimeout());
return networkModule;
}
Expand Down

0 comments on commit 908739f

Please sign in to comment.