Skip to content

Commit

Permalink
Subnet Mask & Prefix Length (oshi#1057)
Browse files Browse the repository at this point in the history
* Added IPv4 Subnet Masks and IPv6 Prefix Lengths to the NetworkIF class

* Updated Changelog
  • Loading branch information
Vesyrak authored and dbwiddis committed Dec 12, 2019
1 parent d7d2e9f commit fd39349
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
4.3.0 (in progress)
================
* Your contribution here.
* [#1057](https://github.com/oshi/oshi/pull/1057): Added Subnet Mask & Prefix Length to NetworkIF. - [@vesyrak](https://github.com/Vesyrak).

4.2.0 (11/9/2019), 4.2.1 (11/14/2019)
================
Expand Down
50 changes: 49 additions & 1 deletion oshi-core/src/main/java/oshi/hardware/NetworkIF.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package oshi.hardware;

import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
Expand Down Expand Up @@ -54,7 +55,9 @@ public class NetworkIF {
private int mtu;
private String mac;
private String[] ipv4;
private Short[] subnetMasks;
private String[] ipv6;
private Short[] prefixLengths;
private long bytesRecv;
private long bytesSent;
private long packetsRecv;
Expand Down Expand Up @@ -100,18 +103,28 @@ public void setNetworkInterface(NetworkInterface networkInterface) {
}
// Set IP arrays
ArrayList<String> ipv4list = new ArrayList<>();
ArrayList<Short> subnetMaskList = new ArrayList<>();
ArrayList<String> ipv6list = new ArrayList<>();
for (InetAddress address : Collections.list(networkInterface.getInetAddresses())) {
ArrayList<Short> prefixLengthList = new ArrayList<>();

for(InterfaceAddress interfaceAddress: networkInterface.getInterfaceAddresses())
{
InetAddress address = interfaceAddress.getAddress();
if (address.getHostAddress().length() > 0) {
if (address.getHostAddress().contains(":")) {
ipv6list.add(address.getHostAddress().split("%")[0]);
prefixLengthList.add(interfaceAddress.getNetworkPrefixLength());
} else {
ipv4list.add(address.getHostAddress());
subnetMaskList.add(interfaceAddress.getNetworkPrefixLength());
}
}
}

this.ipv4 = ipv4list.toArray(new String[0]);
this.subnetMasks = subnetMaskList.toArray(new Short[0]);
this.ipv6 = ipv6list.toArray(new String[0]);
this.prefixLengths = prefixLengthList.toArray(new Short[0]);
} catch (SocketException e) {
LOG.error("Socket exception: {}", e);
}
Expand Down Expand Up @@ -182,6 +195,23 @@ public String[] getIPv4addr() {
return Arrays.copyOf(this.ipv4, this.ipv4.length);
}

/**
* <p>
* The Internet Protocol (IP) v4 subnet masks.
* </p>
*
* @return The IPv4 subnet mask length. Ranges between 0-32
* This value is set when the
* {@link oshi.hardware.NetworkIF} is instantiated and may not be up to
* date. To update this value, execute the
* {@link #setNetworkInterface(NetworkInterface)} method.
*
*/
public Short[] getSubnetMasks()
{
return Arrays.copyOf(this.subnetMasks, this.subnetMasks.length);
}

/**
* <p>
* The Internet Protocol (IP) v6 address.
Expand All @@ -196,6 +226,22 @@ public String[] getIPv6addr() {
return Arrays.copyOf(this.ipv6, this.ipv6.length);
}

/**
* <p>
* The Internet Protocol (IP) v6 address.
* </p>
*
* @return The IPv6 address prefix lengths. Ranges between 0-128.
* This value is set when the
* {@link oshi.hardware.NetworkIF} is instantiated and may not be up to
* date. To update this value, execute the
* {@link #setNetworkInterface(NetworkInterface)} method
*/
public Short[] getPrefixLengths()
{
return Arrays.copyOf(this.prefixLengths, this.prefixLengths.length);
}

/**
* <p>
* Getter for the field <code>bytesRecv</code>.
Expand Down Expand Up @@ -435,7 +481,9 @@ public String toString() {
sb.append(" MAC Address: ").append(getMacaddr()).append("\n");
sb.append(" MTU: ").append(getMTU()).append(", ").append("Speed: ").append(getSpeed()).append("\n");
sb.append(" IPv4: ").append(Arrays.toString(getIPv4addr())).append("\n");
sb.append(" Netmask: ").append(Arrays.toString(getSubnetMasks())).append("\n");
sb.append(" IPv6: ").append(Arrays.toString(getIPv6addr())).append("\n");
sb.append(" Prefix Lengths: ").append(Arrays.toString(getPrefixLengths())).append("\n");
sb.append(" Traffic: received ").append(getPacketsRecv()).append(" packets/")
.append(FormatUtil.formatBytes(getBytesRecv())).append(" (" + getInErrors() + " err);");
sb.append(" transmitted ").append(getPacketsSent()).append(" packets/")
Expand Down
2 changes: 2 additions & 0 deletions oshi-core/src/test/java/oshi/hardware/NetworksTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ public void testNetworkInterfaces() throws IOException {
assertNotNull(net.getDisplayName());
assertNotNull(net.getMacaddr());
assertNotNull(net.getIPv4addr());
assertNotNull(net.getSubnetMasks());
assertNotNull(net.getIPv6addr());
assertNotNull(net.getPrefixLengths());
assertTrue(net.getBytesRecv() >= 0);
assertTrue(net.getBytesSent() >= 0);
assertTrue(net.getPacketsRecv() >= 0);
Expand Down

0 comments on commit fd39349

Please sign in to comment.