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

Allow configurable hostname lookup #934

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
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) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only behavior added. The rest of the changes in this PR are plumbing this optional dns interface from the user to this point

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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Properties;
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.internal.SSLNetworkModule;
import org.eclipse.paho.client.mqttv3.logging.Logger;
Expand All @@ -49,8 +50,8 @@ public class WebSocketSecureNetworkModule extends SSLNetworkModule{
*/
private ByteArrayOutputStream outputStream = new ExtendedByteArrayOutputStream(this);

public WebSocketSecureNetworkModule(SSLSocketFactory factory, String uri, String host, int port, String clientId, Properties customWebSocketHeaders) {
super(factory, host, port, clientId);
public WebSocketSecureNetworkModule(SSLSocketFactory factory, String uri, String host, int port, String clientId, IMqttDns dns, Properties customWebSocketHeaders) {
super(factory, host, port, clientId, dns);
this.uri = uri;
this.host = host;
this.port = port;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public NetworkModule createNetworkModule(URI brokerUri, MqttConnectOptions optio

// Create the network module...
WebSocketSecureNetworkModule netModule = new WebSocketSecureNetworkModule((SSLSocketFactory) factory,
brokerUri.toString(), host, port, clientId, options.getCustomWebSocketHeaders());
brokerUri.toString(), host, port, clientId, options.getDns(), options.getCustomWebSocketHeaders());
netModule.setSSLhandshakeTimeout(options.getConnectionTimeout());
netModule.setSSLHostnameVerifier(options.getSSLHostnameVerifier());
netModule.setHttpsHostnameVerificationEnabled(options.isHttpsHostnameVerificationEnabled());
Expand Down