This repository has been archived by the owner on May 7, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented Option to choose default network interface
Fixes #3884 Signed-off-by: Stefan Triller <stefan.triller@telekom.de>
- Loading branch information
1 parent
67bffae
commit 5875895
Showing
11 changed files
with
256 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
bundles/config/org.eclipse.smarthome.config.core/OSGI-INF/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
/org.eclipse.smarthome.config.core.internal.i18n.I18nConfigOptionsProvider.xml | ||
/org.eclipse.smarthome.config.core.net.internal.NetworkConfigOptionProvider.xml |
99 changes: 99 additions & 0 deletions
99
...main/java/org/eclipse/smarthome/config/core/net/internal/NetworkConfigOptionProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/** | ||
* Copyright (c) 2014-2017 by the respective copyright holders. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.eclipse.smarthome.config.core.net.internal; | ||
|
||
import java.net.Inet6Address; | ||
import java.net.InetAddress; | ||
import java.net.InterfaceAddress; | ||
import java.net.NetworkInterface; | ||
import java.net.SocketException; | ||
import java.net.URI; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Enumeration; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Locale; | ||
|
||
import org.apache.commons.net.util.SubnetUtils; | ||
import org.eclipse.smarthome.config.core.ConfigOptionProvider; | ||
import org.eclipse.smarthome.config.core.ParameterOption; | ||
import org.osgi.service.component.annotations.Component; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Provides a list of IPv4 addresses of the local machine and shows the user which interface belongs to which IP address | ||
* | ||
* @author Stefan Triller - initial contribution | ||
* | ||
*/ | ||
@Component | ||
public class NetworkConfigOptionProvider implements ConfigOptionProvider { | ||
|
||
static final URI CONFIG_URI = URI.create("system:network"); | ||
static final String PARAM_PRIMARY_ADDRESS = "primaryAddress"; | ||
|
||
private final Logger logger = LoggerFactory.getLogger(NetworkConfigOptionProvider.class); | ||
|
||
@Override | ||
public Collection<ParameterOption> getParameterOptions(URI uri, String param, Locale locale) { | ||
if (!uri.equals(CONFIG_URI)) { | ||
return null; | ||
} | ||
|
||
if (param.equals(PARAM_PRIMARY_ADDRESS)) { | ||
return getIPv4Addresses(); | ||
} | ||
return null; | ||
} | ||
|
||
private List<ParameterOption> getIPv4Addresses() { | ||
ArrayList<ParameterOption> interfaceOptions = new ArrayList<>(); | ||
|
||
HashSet<String> subnets = new HashSet<>(); | ||
|
||
try { | ||
final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); | ||
while (interfaces.hasMoreElements()) { | ||
final NetworkInterface current = interfaces.nextElement(); | ||
if (!current.isUp() || current.isLoopback() || current.isVirtual()) { | ||
continue; | ||
} | ||
|
||
for (InterfaceAddress ifAddr : current.getInterfaceAddresses()) { | ||
InetAddress addr = ifAddr.getAddress(); | ||
|
||
if (addr.isLoopbackAddress() || (addr instanceof Inet6Address)) { | ||
continue; | ||
} | ||
|
||
String ipv4Address = addr.getHostAddress(); | ||
|
||
SubnetUtils su = new SubnetUtils( | ||
ipv4Address + "/" + String.valueOf(ifAddr.getNetworkPrefixLength())); | ||
String subNetString = su.getInfo().getNetworkAddress() + "/" | ||
+ String.valueOf(ifAddr.getNetworkPrefixLength()); | ||
|
||
subnets.add(subNetString); | ||
} | ||
} | ||
} catch (SocketException ex) { | ||
logger.error("Could not retrieve network interface: {}", ex.getMessage(), ex); | ||
return null; | ||
} | ||
|
||
for (String subnet : subnets) { | ||
ParameterOption po = new ParameterOption(subnet, subnet); | ||
interfaceOptions.add(po); | ||
} | ||
|
||
return interfaceOptions; | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
bundles/core/org.eclipse.smarthome.core/ESH-INF/config/networkinterface.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<config-description:config-descriptions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:config-description="http://eclipse.org/smarthome/schemas/config-description/v1.0.0" | ||
xsi:schemaLocation="http://eclipse.org/smarthome/schemas/config-description/v1.0.0 | ||
http://eclipse.org/smarthome/schemas/config-description-1.0.0.xsd"> | ||
|
||
<config-description uri="system:network"> | ||
<parameter name="primaryAddress" type="text"> | ||
<label>Primary Address</label> | ||
<description><![CDATA[<p>The primary network to be used</p> | ||
<p>Alternative: Enter an IP address (XXX.XXX.XXX.XXX) manually</p>]]></description> | ||
<limitToOptions>false</limitToOptions> | ||
</parameter> | ||
</config-description> | ||
|
||
</config-description:config-descriptions> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/org.eclipse.smarthome.network.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
...e.smarthome.core/src/main/java/org/eclipse/smarthome/core/net/NetworkAddressProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* Copyright (c) 2014-2017 by the respective copyright holders. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.eclipse.smarthome.core.net; | ||
|
||
/** | ||
* Interface that provides access to configured network addresses | ||
* | ||
* @author Stefan Triller - initial contribution | ||
* | ||
*/ | ||
public interface NetworkAddressProvider { | ||
|
||
public String getPrimaryIpv4HostAddress(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters