From 908739f87633f5c01b5b86ea759b58fea8c5f9c1 Mon Sep 17 00:00:00 2001 From: James Tomson Date: Thu, 26 May 2022 17:37:50 -0400 Subject: [PATCH] Add configurable hostname lookup Signed-off-by: James Tomson --- .../eclipse/paho/client/mqttv3/IMqttDns.java | 8 ++++++++ .../client/mqttv3/MqttConnectOptions.java | 14 ++++++++++++++ .../mqttv3/internal/TCPNetworkModule.java | 19 ++++++++++++++++++- .../internal/TCPNetworkModuleFactory.java | 2 +- 4 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/IMqttDns.java diff --git a/org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/IMqttDns.java b/org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/IMqttDns.java new file mode 100644 index 000000000..d1b7e29a3 --- /dev/null +++ b/org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/IMqttDns.java @@ -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; +} diff --git a/org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/MqttConnectOptions.java b/org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/MqttConnectOptions.java index 78c966eea..b2b9e3cf3 100644 --- a/org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/MqttConnectOptions.java +++ b/org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/MqttConnectOptions.java @@ -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. @@ -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 @@ -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; } diff --git a/org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/internal/TCPNetworkModule.java b/org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/internal/TCPNetworkModule.java index ddcff906a..3da986555 100644 --- a/org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/internal/TCPNetworkModule.java +++ b/org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/internal/TCPNetworkModule.java @@ -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; @@ -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 @@ -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 @@ -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); diff --git a/org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/internal/TCPNetworkModuleFactory.java b/org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/internal/TCPNetworkModuleFactory.java index b75441a6e..17e901e6b 100644 --- a/org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/internal/TCPNetworkModuleFactory.java +++ b/org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/internal/TCPNetworkModuleFactory.java @@ -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; }