Skip to content

Commit 93939d5

Browse files
authored
fix: Do not include router option if no nat (#5727)
* fix: Do not include router option if no nat Signed-off-by: MMaiero <matteo.maiero@eurotech.com> * Improvements Signed-off-by: MMaiero <matteo.maiero@eurotech.com> * fix: Fixed remaining issues Signed-off-by: MMaiero <matteo.maiero@eurotech.com> * chore: header fix Signed-off-by: MMaiero <matteo.maiero@eurotech.com> * feat: passDNS linked with NAT Signed-off-by: MMaiero <matteo.maiero@eurotech.com> * chore: Update copyright DhcpdConfigConverterTest.java Signed-off-by: MMaiero <matteo.maiero@eurotech.com> * chore: Update copyright DhcpdConfigConverter.java Signed-off-by: MMaiero <matteo.maiero@eurotech.com> * fix: Reduced cognitive complexity for DhcpdConfigConverter Signed-off-by: MMaiero <matteo.maiero@eurotech.com> * fix: Reduced duplication in DhcpdConfigConverterTest Signed-off-by: MMaiero <matteo.maiero@eurotech.com> * fix: Fixed tabs DhcpdConfigConverter.java Signed-off-by: MMaiero <matteo.maiero@eurotech.com> * fix: Improved passdns message in Messages.properties Signed-off-by: MMaiero <matteo.maiero@eurotech.com> * fix: Improved passdns message in Japanese Signed-off-by: MMaiero <matteo.maiero@eurotech.com> * fix: Fixed messages Signed-off-by: MMaiero <matteo.maiero@eurotech.com> --------- Signed-off-by: MMaiero <matteo.maiero@eurotech.com>
1 parent 45b0bc4 commit 93939d5

8 files changed

Lines changed: 365 additions & 260 deletions

File tree

Lines changed: 55 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
/*******************************************************************************
2-
* Copyright (c) 2023 Eurotech and/or its affiliates and others
3-
*
2+
* Copyright (c) 2023, 2025 Eurotech and/or its affiliates and others
3+
*
44
* This program and the accompanying materials are made
55
* available under the terms of the Eclipse Public License 2.0
66
* which is available at https://www.eclipse.org/legal/epl-2.0/
7-
*
7+
*
88
* SPDX-License-Identifier: EPL-2.0
9-
*
9+
*
1010
* Contributors:
1111
* Eurotech
1212
*******************************************************************************/
1313
package org.eclipse.kura.linux.net.dhcp.server;
1414

15+
import static java.util.Objects.isNull;
16+
1517
import org.eclipse.kura.linux.net.dhcp.DhcpServerConfigConverter;
1618
import org.eclipse.kura.linux.net.dhcp.DhcpServerManager;
1719
import org.eclipse.kura.net.dhcp.DhcpServerConfig;
@@ -32,8 +34,55 @@ public String convert(DhcpServerConfig config) {
3234
sb.append("subnet " + config.getSubnet().getHostAddress() + " netmask "
3335
+ config.getSubnetMask().getHostAddress() + " {\n");
3436

35-
// DNS servers
36-
if (config.isPassDns() && config.getDnsServers() != null && !config.getDnsServers().isEmpty()) {
37+
appendDNSServers(config, sb);
38+
39+
appendInterfaceName(config, sb);
40+
appendRouterAddress(config, sb);
41+
appendPassDNS(config, sb);
42+
appendLeaseTime(config, sb);
43+
appendPoolRange(config, sb);
44+
45+
sb.append(" }\n");
46+
sb.append("}\n");
47+
48+
return sb.toString();
49+
}
50+
51+
private void appendPoolRange(DhcpServerConfig config, StringBuilder sb) {
52+
sb.append(" pool {\n");
53+
sb.append(" range " + config.getRangeStart().getHostAddress() + " "
54+
+ config.getRangeEnd().getHostAddress() + ";\n");
55+
}
56+
57+
private void appendLeaseTime(DhcpServerConfig config, StringBuilder sb) {
58+
sb.append(" default-lease-time " + config.getDefaultLeaseTime() + ";\n");
59+
if (config.getMaximumLeaseTime() > -1) {
60+
sb.append(" max-lease-time " + config.getMaximumLeaseTime() + ";\n");
61+
}
62+
}
63+
64+
private void appendPassDNS(DhcpServerConfig config, StringBuilder sb) {
65+
if (!config.isPassDns() || isNull(config.getRouterAddress())) {
66+
sb.append(" ddns-update-style none;\n");
67+
sb.append(" ddns-updates off;\n");
68+
}
69+
}
70+
71+
private void appendRouterAddress(DhcpServerConfig config, StringBuilder sb) {
72+
if (!isNull(config.getRouterAddress())) {
73+
sb.append(" option routers " + config.getRouterAddress().getHostAddress() + ";\n");
74+
}
75+
}
76+
77+
private void appendInterfaceName(DhcpServerConfig config, StringBuilder sb) {
78+
if (!isNull(config.getInterfaceName())) {
79+
sb.append(" interface " + config.getInterfaceName() + ";\n");
80+
}
81+
}
82+
83+
private void appendDNSServers(DhcpServerConfig config, StringBuilder sb) {
84+
if (config.isPassDns() && !isNull(config.getDnsServers()) && !config.getDnsServers().isEmpty()
85+
&& !isNull(config.getRouterAddress())) {
3786
sb.append(" option domain-name-servers ");
3887
for (int i = 0; i < config.getDnsServers().size(); i++) {
3988
if (config.getDnsServers().get(i) != null) {
@@ -47,32 +96,6 @@ public String convert(DhcpServerConfig config) {
4796
}
4897
}
4998
}
50-
// interface
51-
if (config.getInterfaceName() != null) {
52-
sb.append(" interface " + config.getInterfaceName() + ";\n");
53-
}
54-
// router address
55-
if (config.getRouterAddress() != null) {
56-
sb.append(" option routers " + config.getRouterAddress().getHostAddress() + ";\n");
57-
}
58-
// if DNS should not be forwarded, add the following lines
59-
if (!config.isPassDns()) {
60-
sb.append(" ddns-update-style none;\n");
61-
sb.append(" ddns-updates off;\n");
62-
}
63-
// Lease times
64-
sb.append(" default-lease-time " + config.getDefaultLeaseTime() + ";\n");
65-
if (config.getMaximumLeaseTime() > -1) {
66-
sb.append(" max-lease-time " + config.getMaximumLeaseTime() + ";\n");
67-
}
68-
// Add the pool and range
69-
sb.append(" pool {\n");
70-
sb.append(" range " + config.getRangeStart().getHostAddress() + " "
71-
+ config.getRangeEnd().getHostAddress() + ";\n");
72-
sb.append(" }\n");
73-
sb.append("}\n");
74-
75-
return sb.toString();
7699
}
77100

78101
}

kura/org.eclipse.kura.linux.net/src/main/java/org/eclipse/kura/linux/net/dhcp/server/DnsmasqConfigConverter.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
/*******************************************************************************
2-
* Copyright (c) 2023 Eurotech and/or its affiliates and others
3-
*
2+
* Copyright (c) 2023, 2025 Eurotech and/or its affiliates and others
3+
*
44
* This program and the accompanying materials are made
55
* available under the terms of the Eclipse Public License 2.0
66
* which is available at https://www.eclipse.org/legal/epl-2.0/
7-
*
7+
*
88
* SPDX-License-Identifier: EPL-2.0
9-
*
9+
*
1010
* Contributors:
1111
* Eurotech
1212
*******************************************************************************/
1313
package org.eclipse.kura.linux.net.dhcp.server;
1414

15+
import static java.util.Objects.isNull;
16+
1517
import org.eclipse.kura.core.net.util.NetworkUtil;
1618
import org.eclipse.kura.linux.net.dhcp.DhcpServerConfigConverter;
1719
import org.eclipse.kura.net.dhcp.DhcpServerConfig;
@@ -26,17 +28,17 @@ public String convert(DhcpServerConfig config) {
2628
sb.append("interface=").append(config.getInterfaceName()).append("\n");
2729

2830
StringBuilder dhcpRangeProp = new StringBuilder("dhcp-range=").append(config.getInterfaceName()).append(",")
29-
.append(config.getRangeStart()).append(",").append(config.getRangeEnd())
30-
.append(",").append(config.getDefaultLeaseTime()).append("s");
31+
.append(config.getRangeStart()).append(",").append(config.getRangeEnd()).append(",")
32+
.append(config.getDefaultLeaseTime()).append("s");
3133
sb.append(dhcpRangeProp.toString()).append("\n");
3234

3335
sb.append(DHCP_OPTION_KEY + config.getInterfaceName()).append(",1,")
3436
.append(NetworkUtil.getNetmaskStringForm(config.getPrefix())).append("\n");
3537
// router property
36-
sb.append(DHCP_OPTION_KEY).append(config.getInterfaceName()).append(",3,")
37-
.append(config.getRouterAddress().toString()).append("\n");
38+
String routerAddress = isNull(config.getRouterAddress()) ? "" : "," + config.getRouterAddress().toString();
39+
sb.append(DHCP_OPTION_KEY).append(config.getInterfaceName()).append(",3").append(routerAddress).append("\n");
3840

39-
if (config.isPassDns()) {
41+
if (config.isPassDns() && !isNull(config.getRouterAddress())) {
4042
// announce DNS servers on this device
4143
sb.append(DHCP_OPTION_KEY).append(config.getInterfaceName()).append(",6,0.0.0.0").append("\n");
4244
} else {

kura/org.eclipse.kura.linux.net/src/main/java/org/eclipse/kura/linux/net/dhcp/server/UdhcpdConfigConverter.java

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
/*******************************************************************************
2-
* Copyright (c) 2023 Eurotech and/or its affiliates and others
3-
*
2+
* Copyright (c) 2023, 2025 Eurotech and/or its affiliates and others
3+
*
44
* This program and the accompanying materials are made
55
* available under the terms of the Eclipse Public License 2.0
66
* which is available at https://www.eclipse.org/legal/epl-2.0/
7-
*
7+
*
88
* SPDX-License-Identifier: EPL-2.0
9-
*
9+
*
1010
* Contributors:
1111
* Eurotech
12-
*******************************************************************************/
12+
******************************************************************************/
1313
package org.eclipse.kura.linux.net.dhcp.server;
1414

15+
import static java.util.Objects.isNull;
16+
1517
import java.util.Objects;
1618

1719
import org.eclipse.kura.linux.net.dhcp.DhcpServerConfigConverter;
@@ -24,22 +26,25 @@ public class UdhcpdConfigConverter implements DhcpServerConfigConverter {
2426
@Override
2527
public String convert(DhcpServerConfig config) {
2628
StringBuilder sb = new StringBuilder();
27-
sb.append("start ").append(config.getRangeStart().getHostAddress()).append("\n")
28-
.append("end ").append(config.getRangeEnd().getHostAddress()).append("\n")
29-
.append("interface ").append(config.getInterfaceName()).append("\n")
30-
.append("pidfile ").append(DhcpServerManager.getPidFilename(config.getInterfaceName())).append("\n")
31-
.append("lease_file ").append(DhcpServerManager.getLeasesFilename(config.getInterfaceName()))
32-
.append("\n")
33-
.append("max_leases ").append(getMaxLeases(config)).append("\n")
34-
.append("auto_time 30").append("\n")
35-
.append("decline_time ").append(config.getDefaultLeaseTime()).append("\n")
36-
.append("conflict_time ").append(config.getDefaultLeaseTime()).append("\n")
37-
.append("offer_time ").append(config.getDefaultLeaseTime()).append("\n")
38-
.append("min_lease ").append(config.getDefaultLeaseTime()).append("\n")
39-
.append("opt subnet ").append(config.getSubnetMask().getHostAddress()).append("\n")
40-
.append("opt router ").append(config.getRouterAddress().getHostAddress()).append("\n")
41-
.append("opt lease ").append(config.getDefaultLeaseTime()).append("\n");
42-
if (config.getDnsServers() != null && !config.getDnsServers().isEmpty()) {
29+
sb.append("start ").append(config.getRangeStart().getHostAddress()).append("\n").append("end ")
30+
.append(config.getRangeEnd().getHostAddress()).append("\n").append("interface ")
31+
.append(config.getInterfaceName()).append("\n").append("pidfile ")
32+
.append(DhcpServerManager.getPidFilename(config.getInterfaceName())).append("\n").append("lease_file ")
33+
.append(DhcpServerManager.getLeasesFilename(config.getInterfaceName())).append("\n")
34+
.append("max_leases ").append(getMaxLeases(config)).append("\n").append("auto_time 30").append("\n")
35+
.append("decline_time ").append(config.getDefaultLeaseTime()).append("\n").append("conflict_time ")
36+
.append(config.getDefaultLeaseTime()).append("\n").append("offer_time ")
37+
.append(config.getDefaultLeaseTime()).append("\n").append("min_lease ")
38+
.append(config.getDefaultLeaseTime()).append("\n").append("opt subnet ")
39+
.append(config.getSubnetMask().getHostAddress()).append("\n");
40+
41+
if (!isNull(config.getRouterAddress())) {
42+
sb.append("opt router ").append(config.getRouterAddress().getHostAddress()).append("\n");
43+
}
44+
45+
sb.append("opt lease ").append(config.getDefaultLeaseTime()).append("\n");
46+
47+
if (config.getDnsServers() != null && !config.getDnsServers().isEmpty() && !isNull(config.getRouterAddress())) {
4348
sb.append(addDNSServersOption(config)).append("\n");
4449
}
4550
return sb.toString();

kura/org.eclipse.kura.nm/src/main/java/org/eclipse/kura/nm/configuration/writer/DhcpServerConfigWriter.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2023 Eurotech and/or its affiliates and others
2+
* Copyright (c) 2023, 2025 Eurotech and/or its affiliates and others
33
*
44
* This program and the accompanying materials are made
55
* available under the terms of the Eclipse Public License 2.0
@@ -80,8 +80,7 @@ public void writeConfiguration() throws KuraException, UnknownHostException {
8080
}
8181
}
8282

83-
private void writeConfigFile(String configFileName, DhcpServerConfig4 dhcpServerConfig)
84-
throws KuraException {
83+
private void writeConfigFile(String configFileName, DhcpServerConfig4 dhcpServerConfig) throws KuraException {
8584
try (FileOutputStream fos = new FileOutputStream(configFileName); PrintWriter pw = new PrintWriter(fos)) {
8685
logger.debug("writing to {} with: {}", configFileName, dhcpServerConfig);
8786
Optional<DhcpServerConfigConverter> configConverter = DhcpServerManager.getConfigConverter();
@@ -109,7 +108,12 @@ private DhcpServerConfigIP4 buildDhcpServerConfiguration() throws UnknownHostExc
109108
IP4Address rangeEnd = getDhcpServer4RangeEnd();
110109
List<IP4Address> dnsServers = new ArrayList<>();
111110
dnsServers.add(address);
112-
DhcpServerCfgIP4 dhcpServerCfgIP4 = new DhcpServerCfgIP4(subnet, subnetMask, prefix, address, rangeStart,
111+
112+
IP4Address router = null;
113+
if (getDhcpServer4NatEnabled()) {
114+
router = getIP4Address();
115+
}
116+
DhcpServerCfgIP4 dhcpServerCfgIP4 = new DhcpServerCfgIP4(subnet, subnetMask, prefix, router, rangeStart,
113117
rangeEnd, dnsServers);
114118
return new DhcpServerConfigIP4(dhcpServerCfg, dhcpServerCfgIP4);
115119
}
@@ -124,6 +128,16 @@ private Boolean getDhcpServer4Enabled() {
124128
}
125129
}
126130

131+
private boolean getDhcpServer4NatEnabled() {
132+
Optional<Boolean> isNatEnabled = this.networkProperties.getOpt(Boolean.class,
133+
"net.interface.%s.config.nat.enabled", this.interfaceName);
134+
if (isNatEnabled.isPresent()) {
135+
return isNatEnabled.get();
136+
} else {
137+
return false;
138+
}
139+
}
140+
127141
private Integer getDhcpServer4DefaultLeaseTime() {
128142
Optional<Integer> defaultLeaseTime = this.networkProperties.getOpt(Integer.class,
129143
"net.interface.%s.config.dhcpServer4.defaultLeaseTime", this.interfaceName);

kura/org.eclipse.kura.web2/src/main/resources/org/eclipse/kura/web/client/messages/Messages.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ netRouterToolTipDhcpEndAddr=For DHCP, enter the last available client address.
607607
netRouterToolTipDhcpSubnet=For DHCP, enter the network mask.
608608
netRouterToolTipDhcpDefaultLeaseTime=For DHCP, enter the default time that a client will retain a provided IP address (e.g. 7200). It must be greater than 0.
609609
netRouterToolTipDhcpMaxLeaseTime=For DHCP, enter the maximum time that a client will retain a provided IP address (e.g. 9600). It must be greater than 0.
610-
netRouterToolTipPassDns=Select whether the interface will provide DNS server information.
610+
netRouterToolTipPassDns=Select whether the interface will provide DNS server information. When enabled, it provides DNS Proxy functionality through DHCP (i.e. passing DNS servers through DHCP). The DNS servers are passed only if the Router Mode is set as "DHCP and NAT".
611611

612612
netWifiWireless=Wireless
613613
netWifiWirelessMode=Wireless Mode

kura/org.eclipse.kura.web2/src/main/resources/org/eclipse/kura/web/client/messages/Messages_ja.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ netRouterToolTipDhcpEndAddr=DHCP向けに、最後に使用可能なクライア
584584
netRouterToolTipDhcpSubnet=DHCP向けに、ネットワークマスクを入力してください。
585585
netRouterToolTipDhcpDefaultLeaseTime=DHCP向けに、クライアントが提供されたIPアドレスを保持する時間の初期値を入力してください。(例 7200)
586586
netRouterToolTipDhcpMaxLeaseTime=DHCP向けに、クライアントが提供されたIPアドレスを保持する時間の最大値を入力してください。(例 9600)
587-
netRouterToolTipPassDns=インターフェースはDNSサーバーインフォメーションを提供しますか?
587+
netRouterToolTipPassDns=インターフェースがDNSサーバー情報を提供するかどうかを選択します。有効にすると、DHCPを通じてDNSプロキシ機能を提供し(すなわち、DHCP経由でDNSサーバーを渡す)、ルーターモードが「DHCPおよびNAT」に設定されている場合のみDNSサーバーが渡されます
588588

589589
netWifiWireless=ワイヤレス
590590
netWifiWirelessMode=ワイヤレスモード

0 commit comments

Comments
 (0)