Skip to content

Commit d53cc48

Browse files
committed
fix: ipv6 made working when ipv4 off
Signed-off-by: MMaiero <matteo.maiero@eurotech.com>
1 parent cd75b4b commit d53cc48

File tree

6 files changed

+644
-17
lines changed

6 files changed

+644
-17
lines changed

bundles/org.eclipse.kura.linux.net/src/main/java/org/eclipse/kura/linux/net/util/IpAddrShow.java

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2011, 2020 Eurotech and/or its affiliates and others
2+
* Copyright (c) 2011, 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
@@ -11,6 +11,7 @@
1111
* Eurotech
1212
*******************************************************************************/
1313

14+
// Content with portions generated by generative AI platform
1415
package org.eclipse.kura.linux.net.util;
1516

1617
import java.io.ByteArrayOutputStream;
@@ -54,6 +55,7 @@ public IpAddrShow(String interfaceName, CommandExecutorService executoService) {
5455
public LinuxIfconfig[] exec() throws KuraException {
5556
execLink();
5657
execInet();
58+
execInet6();
5759
return this.configs.toArray(new LinuxIfconfig[0]);
5860
}
5961

@@ -220,6 +222,85 @@ private void parseInet(LinuxIfconfig config, String inet) throws UnknownHostExce
220222
}
221223
}
222224

225+
private void execInet6() throws KuraException {
226+
StringBuilder sb = new StringBuilder("ip -o -6 addr show");
227+
if (this.ifaceName != null) {
228+
sb.append(" dev ").append(this.ifaceName);
229+
}
230+
String[] cmd = sb.toString().split("\\s+");
231+
Command command = new Command(cmd);
232+
command.setTimeout(60);
233+
command.setOutputStream(new ByteArrayOutputStream());
234+
CommandStatus status = this.executorService.execute(command);
235+
if (!status.getExitStatus().isSuccessful()) {
236+
// IPv6 might not be available or configured, so we don't throw an exception
237+
logger.debug("IPv6 address command failed: {} (exit code: {})", sb.toString(), status.getExitStatus().getExitCode());
238+
return;
239+
}
240+
parseExecInet6(new String(((ByteArrayOutputStream) status.getOutputStream()).toByteArray(), Charsets.UTF_8));
241+
}
242+
243+
private void parseExecInet6(String commandOutput) throws KuraException {
244+
// FIXME: Note that one interface might have multiple inet6 addresses
245+
// while this implementation assumes a single inet6 address.
246+
try {
247+
for (String line : commandOutput.split("\n")) {
248+
line = line.trim();
249+
if (line.isEmpty()) {
250+
continue;
251+
}
252+
253+
logger.debug(line);
254+
255+
String ifname = findIfname(line);
256+
logger.debug("ifname: '{}'", ifname);
257+
if (ifname == null) {
258+
logger.warn("Interface name not found in IPv6 output");
259+
continue;
260+
}
261+
262+
LinuxIfconfig config = null;
263+
for (LinuxIfconfig conf : this.configs) {
264+
if (conf.getName().equals(ifname)) {
265+
config = conf;
266+
break;
267+
}
268+
}
269+
270+
if (config == null) {
271+
logger.warn("Config for interface {} not found for IPv6", ifname);
272+
continue;
273+
}
274+
275+
String inet6 = findValue(line, "inet6", " ");
276+
logger.debug("inet6: '{}'", inet6);
277+
278+
if (inet6 != null) {
279+
parseInet6(config, inet6);
280+
}
281+
}
282+
} catch (Exception e) {
283+
throw new KuraException(KuraErrorCode.PROCESS_EXECUTION_ERROR, e);
284+
}
285+
}
286+
287+
private void parseInet6(LinuxIfconfig config, String inet6) {
288+
String[] parts = inet6.split("/");
289+
if (parts.length > 0) {
290+
String inet6addr = parts[0];
291+
config.setInet6Address(inet6addr);
292+
String sprefix = null;
293+
if (parts.length > 1) {
294+
sprefix = parts[1];
295+
}
296+
int prefix = 128; // Default IPv6 prefix
297+
if (sprefix != null) {
298+
prefix = Integer.valueOf(sprefix);
299+
}
300+
config.setInet6Prefix(prefix);
301+
}
302+
}
303+
223304
private String findIfname(String input) {
224305
int start = input.indexOf(':');
225306
if (start < 0) {

bundles/org.eclipse.kura.linux.net/src/main/java/org/eclipse/kura/linux/net/util/LinuxIfconfig.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2011, 2020 Eurotech and/or its affiliates and others
2+
* Copyright (c) 2011, 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
@@ -11,6 +11,7 @@
1111
* Eurotech
1212
*******************************************************************************/
1313

14+
// Content with portions generated by generative AI platform
1415
package org.eclipse.kura.linux.net.util;
1516

1617
import java.util.Map;
@@ -27,6 +28,8 @@ public class LinuxIfconfig {
2728
private String peerInetAddr;
2829
private String inetBcast;
2930
private String inetMask;
31+
private String inet6Address;
32+
private int inet6Prefix;
3033
private int mtu;
3134
private boolean multicast;
3235
private Map<String, String> driver;
@@ -90,6 +93,22 @@ public void setPeerInetAddr(String peerInetAddr) {
9093
this.peerInetAddr = peerInetAddr;
9194
}
9295

96+
public String getInet6Address() {
97+
return this.inet6Address;
98+
}
99+
100+
public void setInet6Address(String inet6Address) {
101+
this.inet6Address = inet6Address;
102+
}
103+
104+
public int getInet6Prefix() {
105+
return this.inet6Prefix;
106+
}
107+
108+
public void setInet6Prefix(int inet6Prefix) {
109+
this.inet6Prefix = inet6Prefix;
110+
}
111+
93112
public int getMtu() {
94113
return this.mtu;
95114
}
@@ -118,12 +137,10 @@ public boolean isUp() {
118137
if (this.up != null) {
119138
return this.up;
120139
} else {
121-
// old code
122-
boolean ret = false;
123-
if (this.inetAddress != null && this.inetMask != null) {
124-
ret = true;
125-
}
126-
return ret;
140+
// fallback logic: check if interface has either IPv4 or IPv6 addresses
141+
boolean hasIpv4 = this.inetAddress != null && this.inetMask != null;
142+
boolean hasIpv6 = this.inet6Address != null;
143+
return hasIpv4 || hasIpv6;
127144
}
128145
}
129146

bundles/org.eclipse.kura.linux.net/src/main/java/org/eclipse/kura/linux/net/util/LinuxNetworkUtil.java

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
* Contributors:
1111
* Eurotech
1212
*******************************************************************************/
13+
14+
// Content with portions generated by generative AI platform
1315
package org.eclipse.kura.linux.net.util;
1416

1517
import java.io.BufferedReader;
@@ -127,7 +129,9 @@ private static List<String> getAllInterfaceNamesInternalParse(String commandOutp
127129
}
128130

129131
/*
130-
* Returns null if the interface is not found
132+
* Returns the current IP address of the interface. Prefers IPv4 over IPv6.
133+
* Returns IPv6 address if IPv4 is not available but IPv6 is configured.
134+
* Returns null if the interface is not found or has no addresses.
131135
*/
132136
public String getCurrentIpAddress(String ifaceName) throws KuraException {
133137
// ignore logical interfaces like "1-1.2"
@@ -137,7 +141,17 @@ public String getCurrentIpAddress(String ifaceName) throws KuraException {
137141

138142
LinuxIfconfig ifconfig = getInterfaceConfiguration(ifaceName);
139143

140-
return ifconfig != null ? ifconfig.getInetAddress() : null;
144+
if (ifconfig == null) {
145+
return null;
146+
}
147+
148+
// Prefer IPv4 address if available
149+
if (ifconfig.getInetAddress() != null) {
150+
return ifconfig.getInetAddress();
151+
}
152+
153+
// Fall back to IPv6 address if IPv4 is not available
154+
return ifconfig.getInet6Address();
141155
}
142156

143157
/*
@@ -1012,8 +1026,8 @@ public void enableInterface(String interfaceName) throws KuraException {
10121026
}
10131027

10141028
/*
1015-
* Returns true if both the inet address and inet mask are non-null.
1016-
* Returns false if the interface is not found.
1029+
* Returns true if interface has either IPv4 addresses (inet address and inet mask) or IPv6 addresses.
1030+
* Returns false if the interface is not found or has no addresses.
10171031
*/
10181032
public boolean hasAddress(String ifaceName) throws KuraException {
10191033
// ignore logical interfaces like "1-1.2"
@@ -1024,12 +1038,17 @@ public boolean hasAddress(String ifaceName) throws KuraException {
10241038
LinuxIfconfig ifconfig = getInterfaceConfiguration(ifaceName);
10251039

10261040
// FIXME: should we throw an exception if config is null?
1027-
boolean ret = false;
1028-
if (ifconfig != null && ifconfig.getInetAddress() != null && ifconfig.getInetMask() != null) {
1029-
ret = true;
1041+
if (ifconfig == null) {
1042+
return false;
10301043
}
1031-
1032-
return ret;
1044+
1045+
// Check IPv4 addresses
1046+
boolean hasIpv4 = ifconfig.getInetAddress() != null && ifconfig.getInetMask() != null;
1047+
1048+
// Check IPv6 addresses
1049+
boolean hasIpv6 = ifconfig.getInet6Address() != null;
1050+
1051+
return hasIpv4 || hasIpv6;
10331052
}
10341053

10351054
/*

0 commit comments

Comments
 (0)