Merge "Add getInterfaceMtu method in ProcfsParsingUtils" into main
This commit is contained in:
commit
b91b3afcd0
1 changed files with 57 additions and 2 deletions
|
|
@ -40,6 +40,12 @@ import java.util.regex.Pattern;
|
|||
public final class ProcfsParsingUtils {
|
||||
public static final String TAG = ProcfsParsingUtils.class.getSimpleName();
|
||||
|
||||
// rfc894 describes the minimum length of the data field of a packet sent over an
|
||||
// Ethernet is 1500 octets, thus the maximum length of an IP datagram
|
||||
// sent over an Ethernet is 1500 octets.
|
||||
public static final int DEFAULT_MTU = 1500;
|
||||
public static final int DEFAULT_TRAFFIC_CLASS = 0;
|
||||
|
||||
private static final Inet4Address IPV4_ADDR_ALL_HOST_MULTICAST =
|
||||
(Inet4Address) InetAddresses.parseNumericAddress("224.0.0.1");
|
||||
|
||||
|
|
@ -49,6 +55,7 @@ public final class ProcfsParsingUtils {
|
|||
private static final String IPV4_MCAST_PATH = "/proc/net/igmp";
|
||||
private static final String IPV6_MCAST_PATH = "/proc/net/igmp6";
|
||||
private static final String IPV4_DEFAULT_TTL_PATH = "/proc/sys/net/ipv4/ip_default_ttl";
|
||||
private static final String SYS_CLASS_NET_PATH = "/sys/class/net";
|
||||
|
||||
private static final Pattern WHITESPACE_PATTERN = Pattern.compile("\\s+");
|
||||
|
||||
|
|
@ -89,10 +96,15 @@ public final class ProcfsParsingUtils {
|
|||
@VisibleForTesting
|
||||
public static int parseNdTrafficClass(final List<String> lines) {
|
||||
if (lines.size() != 1) {
|
||||
return 0; // default
|
||||
return DEFAULT_TRAFFIC_CLASS;
|
||||
}
|
||||
|
||||
return Integer.parseInt(lines.get(0));
|
||||
try {
|
||||
return Integer.parseInt(lines.get(0));
|
||||
} catch (NumberFormatException e) {
|
||||
Log.e(TAG, "failed to parse ND traffic class.", e);
|
||||
return DEFAULT_TRAFFIC_CLASS;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -112,6 +124,32 @@ public final class ProcfsParsingUtils {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses an interface's Maximum Transmission Unit (MTU) value from a list of strings.
|
||||
*
|
||||
* This function expects a list containing a single string representing the interface MTU.
|
||||
* If the list is empty or contains multiple lines, it assumes a default value for 1500.
|
||||
*
|
||||
* @param lines A list of strings, expected to contain the MTU value as its sole element.
|
||||
* @return The parsed MTU value as an integer. Returns `IPV4_MIN_MTU` if `ipv4` is true
|
||||
* and the input `lines` size is not 1, otherwise returns `IPV6_MIN_MTU` if `ipv4`
|
||||
* is false and the input `lines` size is not 1. If `lines` has exactly one
|
||||
* element, returns the integer parsed from that string.
|
||||
*/
|
||||
@VisibleForTesting
|
||||
public static int parseInterfaceMtu(final List<String> lines) {
|
||||
if (lines.size() != 1) {
|
||||
return DEFAULT_MTU;
|
||||
}
|
||||
|
||||
try {
|
||||
return Integer.parseInt(lines.get(0));
|
||||
} catch (NumberFormatException e) {
|
||||
Log.e(TAG, "failed to parse interface mtu.", e);
|
||||
return DEFAULT_MTU;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses anycast6 addresses associated with a specific interface from a list of strings.
|
||||
*
|
||||
|
|
@ -299,6 +337,23 @@ public final class ProcfsParsingUtils {
|
|||
return parseNdTrafficClass(lines);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the interface MTU for the specified interface.
|
||||
* The function loads the existing interface MTU from the file
|
||||
* `/sys/class/net/{ifname}/mtu`. If the file does not exist, the
|
||||
* function returns the default value 1280.
|
||||
* rfc8200#5 describes IPv6 requires that every link in the Internet have an MTU of 1280
|
||||
* octets or greater. This is known as the IPv6 minimum link MTU.
|
||||
*
|
||||
* @param ifname The name of the interface.
|
||||
* @return The traffic class for the interface.
|
||||
*/
|
||||
public static int getInterfaceMtu(final String ifname) {
|
||||
final String mtuPath = SYS_CLASS_NET_PATH + ifname + "/mtu";
|
||||
final List<String> lines = readFile(mtuPath);
|
||||
return parseInterfaceMtu(lines);
|
||||
}
|
||||
|
||||
/**
|
||||
* The function loads the existing IPv6 anycast address from the file `/proc/net/anycast6`.
|
||||
* If the file does not exist or the interface is not found, the function
|
||||
|
|
|
|||
Loading…
Reference in a new issue