diff --git a/src/main/java/com/bwssystems/HABridge/upnp/UpnpListener.java b/src/main/java/com/bwssystems/HABridge/upnp/UpnpListener.java index 0bcf81b..57710b9 100644 --- a/src/main/java/com/bwssystems/HABridge/upnp/UpnpListener.java +++ b/src/main/java/com/bwssystems/HABridge/upnp/UpnpListener.java @@ -9,6 +9,7 @@ import com.bwssystems.HABridge.Configuration; import com.bwssystems.HABridge.api.hue.HueConstants; import com.bwssystems.HABridge.api.hue.HuePublicConfig; import com.bwssystems.HABridge.util.UDPDatagramSender; +import com.bwssystems.HABridge.util.AddressUtil; import java.io.IOException; import java.net.*; @@ -331,8 +332,14 @@ public class UpnpListener { InetAddress requester = aPacket.getAddress(); int sourcePort = aPacket.getPort(); String discoveryResponse = null; - // refactored suggestion by https://github.com/pvint - String httpLocationAddress = getOutboundAddress(requesterAddress).getHostAddress(); + String httpLocationAddress = null; + if(useUpnpIface) { + httpLocationAddress = upnpConfigIP; + } else { + // refactored suggestion by https://github.com/pvint + httpLocationAddress = AddressUtil.getOutboundAddress(requesterAddress).getHostAddress(); + } + try { Thread.sleep(theUpnpSendDelay); } catch (InterruptedException e) { @@ -437,21 +444,4 @@ public class UpnpListener { log.debug("sendUpnpNotify notifyTemplate3 is <<<{}>>>", notifyData); sendUDPResponse(notifyData.getBytes(), aSocketAddress, Configuration.UPNP_DISCOVERY_PORT); } - - // added by https://github.com/pvint - // Ruthlessly stolen from - // https://stackoverflow.com/questions/22045165/java-datagrampacket-receive-how-to-determine-local-ip-interface - // Try to get a source IP that makes sense for the requestor to contact for use - // in the LOCATION header in replies - private InetAddress getOutboundAddress(SocketAddress remoteAddress) throws SocketException { - DatagramSocket sock = new DatagramSocket(); - // connect is needed to bind the socket and retrieve the local address - // later (it would return 0.0.0.0 otherwise) - sock.connect(remoteAddress); - final InetAddress localAddress = sock.getLocalAddress(); - sock.disconnect(); - sock.close(); - sock = null; - return localAddress; - } } diff --git a/src/main/java/com/bwssystems/HABridge/upnp/UpnpSettingsResource.java b/src/main/java/com/bwssystems/HABridge/upnp/UpnpSettingsResource.java index b62d84e..33d7534 100644 --- a/src/main/java/com/bwssystems/HABridge/upnp/UpnpSettingsResource.java +++ b/src/main/java/com/bwssystems/HABridge/upnp/UpnpSettingsResource.java @@ -7,6 +7,7 @@ import com.bwssystems.HABridge.BridgeSettings; import com.bwssystems.HABridge.BridgeSettingsDescriptor; import com.bwssystems.HABridge.api.hue.HueConstants; import com.bwssystems.HABridge.api.hue.HuePublicConfig; +import com.bwssystems.HABridge.util.AddressUtil; import com.bwssystems.HABridge.util.ParseRoute; import static spark.Spark.get; @@ -98,7 +99,12 @@ public class UpnpSettingsResource { httpLocationAddr = theSettings.getUpnpConfigAddress(); hueTemplate = hueTemplate_pre + hueTemplate_mid_orig + hueTemplate_post; } else { - httpLocationAddr = getOutboundAddress(request.ip(), request.port()).getHostAddress(); + + if(theSettings.isUseupnpiface()) { + httpLocationAddr = theSettings.getUpnpConfigAddress(); + } else { + httpLocationAddr = AddressUtil.getOutboundAddress(request.ip(), request.port()).getHostAddress(); + } hueTemplate = hueTemplate_pre + hueTemplate_post; } @@ -136,28 +142,4 @@ public class UpnpSettingsResource { return ""; } ); } - // added by https://github.com/pvint - // Ruthlessly stolen from https://stackoverflow.com/questions/22045165/java-datagrampacket-receive-how-to-determine-local-ip-interface - // Try to get a source IP that makes sense for the requester to contact for use in the LOCATION header in replies - private InetAddress getOutboundAddress(String remoteAddress, int remotePort) { - InetAddress localAddress = null; - try { - DatagramSocket sock = new DatagramSocket(); - // connect is needed to bind the socket and retrieve the local address - // later (it would return 0.0.0.0 otherwise) - sock.connect(new InetSocketAddress(remoteAddress, remotePort)); - localAddress = sock.getLocalAddress(); - sock.disconnect(); - sock.close(); - sock = null; - } catch(Exception e) { - ParseRoute theRoute = ParseRoute.getInstance(); - try { - localAddress = InetAddress.getByName(theRoute.getLocalIPAddress()); - } catch(Exception e1) {} - log.warn("Error <" + e.getMessage() + "> on determining interface to reply for <" + remoteAddress + ">. Using default route IP Address of " + localAddress.getHostAddress()); - } - log.debug("getOutbountAddress returning IP Address of " + localAddress.getHostAddress()); - return localAddress; - } } diff --git a/src/main/java/com/bwssystems/HABridge/util/AddressUtil.java b/src/main/java/com/bwssystems/HABridge/util/AddressUtil.java new file mode 100644 index 0000000..6eb4aa2 --- /dev/null +++ b/src/main/java/com/bwssystems/HABridge/util/AddressUtil.java @@ -0,0 +1,56 @@ +package com.bwssystems.HABridge.util; + +import java.net.*; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class AddressUtil { + final static Logger log = LoggerFactory.getLogger(AddressUtil.class); + + // added by https://github.com/pvint + // Ruthlessly stolen from + // https://stackoverflow.com/questions/22045165/java-datagrampacket-receive-how-to-determine-local-ip-interface + // Try to get a source IP that makes sense for the requester to contact for use + // in the LOCATION header in replies + public static InetAddress getOutboundAddress(String remoteAddress, int remotePort) { + InetAddress localAddress = null; + try { + DatagramSocket sock = new DatagramSocket(); + // connect is needed to bind the socket and retrieve the local address + // later (it would return 0.0.0.0 otherwise) + sock.connect(new InetSocketAddress(remoteAddress, remotePort)); + localAddress = sock.getLocalAddress(); + sock.disconnect(); + sock.close(); + sock = null; + } catch (Exception e) { + ParseRoute theRoute = ParseRoute.getInstance(); + try { + localAddress = InetAddress.getByName(theRoute.getLocalIPAddress()); + } catch (Exception e1) { + } + log.warn("Error <" + e.getMessage() + "> on determining interface to reply for <" + remoteAddress + + ">. Using default route IP Address of " + localAddress.getHostAddress()); + } + log.debug("getOutbountAddress returning IP Address of " + localAddress.getHostAddress()); + return localAddress; + } + + // added by https://github.com/pvint + // Ruthlessly stolen from + // https://stackoverflow.com/questions/22045165/java-datagrampacket-receive-how-to-determine-local-ip-interface + // Try to get a source IP that makes sense for the requestor to contact for use + // in the LOCATION header in replies + public static InetAddress getOutboundAddress(SocketAddress remoteAddress) throws SocketException { + DatagramSocket sock = new DatagramSocket(); + // connect is needed to bind the socket and retrieve the local address + // later (it would return 0.0.0.0 otherwise) + sock.connect(remoteAddress); + final InetAddress localAddress = sock.getLocalAddress(); + sock.disconnect(); + sock.close(); + sock = null; + return localAddress; + } +} \ No newline at end of file