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 27, 2022
1 parent 240f82f commit f6681ea
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 10 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 @@ -30,6 +30,7 @@
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

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 Down Expand Up @@ -64,8 +65,8 @@ public class SSLNetworkModule extends TCPNetworkModule {
* @param resourceContext
* Resource Context
*/
public SSLNetworkModule(SSLSocketFactory factory, String host, int port, String resourceContext) {
super(factory, host, port, resourceContext);
public SSLNetworkModule(SSLSocketFactory factory, String host, int port, String resourceContext, IMqttDns dns) {
super(factory, host, port, resourceContext, dns);
this.host = host;
this.port = port;
log.setResourceName(resourceContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public NetworkModule createNetworkModule(URI brokerUri, MqttConnectOptions optio
}

// Create the network module...
SSLNetworkModule netModule = new SSLNetworkModule((SSLSocketFactory) factory, host, port, clientId);
SSLNetworkModule netModule = new SSLNetworkModule((SSLSocketFactory) factory, host, port, clientId, options.getDns());
netModule.setSSLhandshakeTimeout(options.getConnectionTimeout());
netModule.setSSLHostnameVerifier(options.getSSLHostnameVerifier());
netModule.setHttpsHostnameVerificationEnabled(options.isHttpsHostnameVerificationEnabled());
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 @@ -50,13 +53,14 @@ public class TCPNetworkModule implements NetworkModule {
* @param host The server hostname
* @param port The server port
* @param resourceContext The Resource Context
* @param dns the {@link IMqttDns} to be used for hostname lookup
*/
public TCPNetworkModule(SocketFactory factory, String host, int port, String resourceContext) {
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;
}

/**
Expand All @@ -69,7 +73,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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import javax.net.SocketFactory;

import org.eclipse.paho.client.mqttv3.IMqttDns;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule;
import org.eclipse.paho.client.mqttv3.logging.Logger;
Expand All @@ -50,8 +51,8 @@ public class WebSocketNetworkModule extends TCPNetworkModule {
*/
private ByteArrayOutputStream outputStream = new ExtendedByteArrayOutputStream(this);

public WebSocketNetworkModule(SocketFactory factory, String uri, String host, int port, String resourceContext, Properties customWebsocketHeaders){
super(factory, host, port, resourceContext);
public WebSocketNetworkModule(SocketFactory factory, String uri, String host, int port, String resourceContext, Properties customWebsocketHeaders, IMqttDns dns){
super(factory, host, port, resourceContext, dns);
this.uri = uri;
this.host = host;
this.port = port;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public NetworkModule createNetworkModule(URI brokerUri, MqttConnectOptions optio
throw ExceptionHelper.createMqttException(MqttException.REASON_CODE_SOCKET_FACTORY_MISMATCH);
}
WebSocketNetworkModule netModule = new WebSocketNetworkModule(factory, brokerUri.toString(), host, port,
clientId, options.getCustomWebSocketHeaders());
clientId, options.getCustomWebSocketHeaders(), options.getDns());
netModule.setConnectTimeout(options.getConnectionTimeout());
return netModule;
}
Expand Down

0 comments on commit f6681ea

Please sign in to comment.