Repository navigation
Read the Linux default gateways from the kernel routing table - #3853
Conversation
getIpv4DefaultGateway() and getIpv6DefaultGateway() ran `route -A inet[6] -n`. route is in /usr/sbin, which a normal user's PATH does not include on Debian 13, and net-tools is absent altogether on many current distributions, so both returned "". Measured on cfarm191 (Debian 13) with net-tools installed. Select the gateway instead from the routes RouteTable already parses from /proc/net/route and /proc/net/ipv6_route: the next hop of the default route with the lowest metric, the same choice the route-based code made. This also saves two process spawns per call. The IPv6 compression that kept the text in route's form moves out of parseUtAddrV6toIP into ParseUtil.formatIpAddress, which both now use.
|
No actionable comments were generated in the recent review.
|
| Layer / File(s) | Summary |
|---|---|
IP address formatting oshi-common/src/main/java/oshi/util/ParseUtil.java, oshi-common/src/test/java/oshi/util/ParseUtilTest.java |
ParseUtil.formatIpAddress formats byte arrays as IP addresses and returns Constants.UNKNOWN if address construction fails. parseUtAddrV6toIP uses the helper. Tests cover IPv4, IPv6, zero-group compression, and an invalid-length input. |
Route-based gateway selection oshi-common/src/main/java/oshi/software/common/os/linux/LinuxNetworkParams.java, oshi-common/src/test/java/oshi/software/common/os/linux/LinuxNetworkParamsTest.java, CHANGELOG.md |
The IPv4 and IPv6 gateway methods query RouteTable. A shared selector chooses a qualifying default route with the lowest metric, or returns an empty string when none qualifies. Tests cover route selection and missing next hops. The changelog records the change. |
Estimated code review effort: 2 (Simple) | ~10 minutes
Change: Bug fix · Severity of issue fixed: Low
Sequence Diagram(s)
sequenceDiagram
participant LinuxNetworkParams
participant RouteTable
participant GatewaySelector
participant ParseUtil
LinuxNetworkParams->>RouteTable: Query routes
RouteTable-->>LinuxNetworkParams: Return routes
LinuxNetworkParams->>GatewaySelector: Select qualifying default route
GatewaySelector->>ParseUtil: Format gateway address
ParseUtil-->>LinuxNetworkParams: Return formatted address
Possibly related PRs
- oshi/oshi#3852: Changes IPv4 route address decoding used by the RouteTable results that this PR examines.
Merge Risk: 
Minimal · up to d65fd
-
No merge-blocking risk was identified in the route-based gateway lookup.
Pre-merge checks: 
4 · 
1
-
Failed checks: 1 warning Passed checks: 4 passedFull details: Docstring Coverage
-
Explanation
Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 4 functions across 4 files. (1 skipped: 1 unsupported.)
Resolution
Write docstrings for the functions missing them to satisfy the coverage threshold.
-
Finishing Touches
-
Generate docstrings
-
- Commit to this branch
- Create a new PR
Generate unit tests (beta)
-
- Commit to this branch
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
A rabbit checks the routes at dawn,
And finds the gateway, neatly drawn.
The bytes take shape in v4 and v6,
With zero groups compressed with tricks.
If no next hop is there to see,
An empty string is returned free.
Problem
On Linux,
NetworkParams.getIpv4DefaultGateway()andgetIpv6DefaultGateway()ranroute -A inet -nandroute -A inet6 -n.routelives in/usr/sbin, which is not on a normal user'sPATHon Debian 13, and net-tools is not installed at all on many current distributions. Both methods then return"". Found during the s390x survey on cfarm191 (Debian 13, net-tools installed), whereip routereporteddefault via 148.100.74.1and OSHI reported nothing. This is not specific to s390x.Fix
RouteTablealready parses from/proc/net/routeand/proc/net/ipv6_route(the same source asgetRoutes()): the next hop of the default route (prefix length 0) with the lowest metric, which is the choice theroute-based code made. A default route with no next hop, such as the kernel's unreachable::/0onlo, is ignored.routeprinted (fe80::4a2e:72ff:fe54:5e9f): the compression regex moves out ofparseUtAddrV6toIPinto a newParseUtil.formatIpAddress(byte[]), whichparseUtAddrV6toIPnow calls, so its output is unchanged.Tests
LinuxNetworkParamsTest: gateway selection over route lists modelled on cfarm191's real IPv4 and IPv6 tables, including a higher-metric default route listed first and a gateway-less::/0; the empty cases. The class-level@EnabledOnOs(OS.LINUX)moved onto the one test that reads/proc, so these run on every CI platform.ParseUtilTest:formatIpAddressfor IPv4, IPv6 (including longest-zero-run compression) and an invalid length.Verification
clean install -Perror-prone.""to148.100.74.1andfe80::4a2e:72ff:fe54:5e9f, matchingip route show defaultandip -6 route show default.NativeComparisonTestandNativeFreeComparisonTestnetworkParams()pass there.Summary by CodeRabbit
routecommand is unavailable.