[Fix] IP Address Conversion Functions for Proper IPv4-Mapped IPv6 Format (Issue #641)#690
Conversation
parameter naming fix
WalkthroughAdds new IPv4/IPv6 conversion integration tests; adjusts CLI logic to append /32 or /128 to single IP inputs based on presence of ":"; updates ipNetToRange to map first and last IPv4 addresses to IPv4-mapped IPv6 form whenever To4() is non-nil. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant U as User
participant CLI as mapcidr (main)
participant IP as ipNetToRange
U->>CLI: Provide input (IP or CIDR) with target (-t4 / -t6)
alt Input is single IP
CLI->>CLI: Check for ":" in input
alt No ":" (IPv4)
CLI->>CLI: Append /32
else Has ":" (IPv6)
CLI->>CLI: Append /128
end
end
CLI->>IP: Compute range from CIDR
note over IP: If firstIP.To4()!=nil → map to ::ffff:...<br/>If lastIP.To4()!=nil → map to ::ffff:...
IP-->>CLI: First/Last IP (possibly IPv4-mapped IPv6)
CLI-->>U: Output converted addresses
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
cmd/integration-test/mapcidr.go (1)
86-93: New IPv6↔IPv4 conversion tests — LGTM.Solid coverage for hex and ::ffff forms and roundtrip. Consider adding a filter case with
-fi ::ffff:192.168.1.1to ensure only a single host is filtered (see main.go comment about/32vs/128consistency).
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
cmd/integration-test/mapcidr.go(1 hunks)cmd/mapcidr/main.go(1 hunks)ip.go(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: Test Builds
- GitHub Check: Analyze (go)
🔇 Additional comments (2)
ip.go (1)
282-287: Map both ends of IPv4 ranges to v4-mapped IPv6 — LGTM; verify coalescing.Making both first and last IPv4 endpoints v4-mapped ensures consistent width and fixes edge joins. Looks good. Please sanity-check coalescing/partition flows for small IPv4 CIDRs to ensure no regression in range math.
cmd/mapcidr/main.go (1)
393-397: Unify single-IP CIDR suffix in cidrsToNetworksIn cmd/mapcidr/main.go’s cidrsToNetworks, replace the existing To4() check with the colon-aware logic used in process to prevent treating IPv4-mapped IPv6 as /32:
- if ip.To4() != nil { - cidr += "/32" - } else { - cidr += "/128" - } + if ip.To4() != nil && !strings.Contains(cidr, ":") { + cidr += "/32" + } else { + cidr += "/128" + }Optional: add an integration test for filtering a single host via
-fi ::ffff:192.168.1.1.Likely an incorrect or invalid review comment.
Fix IPv6 → IPv4 Conversion and Adjust IPv4-to-IPv6 Mapping Format
Summary
This PR fixes the IPv6 ↔ IPv4 conversion logic in
mapcidrwith two key changes:::ffff:a.b.c.dand raw hex forms like00:00:00:00:00:ffff:c0a8:0101are recognized and mapped back toa.b.c.d.IPv4 → IPv6 → IPv4) is now correct and lossless.Motivation & linked issue
Fixes: Broken IPv6 ↔ IPv4 conversions in mapcidr.
00:00:00:00:00:192.168.1.1could not be parsed back to IPv4.00:00:00:00:00:ffff:c0a8:0101could not be parsed back to IPv4.ToIP4("::ffff:192.168.1.1")→192.168.1.1ToIP4("00:00:00:00:00:ffff:c0a8:0101")→192.168.1.1Issues
ToIP4failed to parse hex-form IPv6 literals into IPv4.Improvements
ToIP4can parse both hex-form and::ffff:-style mapped addresses.IPv4 → IPv6 → IPv4yields the same IPv4.CLI examples
IPv6 (hex) → IPv4
IPv6 (::ffff mapped) → IPv4
Round-trip
Test
[Problem 1]

=> This bug has a colon, but it is recognized as ipv4 (fixed in main.go)
[Problem 2 (After Problem1 fix)]

=> This bug occurs in the logic that adds unconditionally without verifying the IP format.
[1,2 > Solution]

[Problem 3]

[3 > Solution]

Summary by CodeRabbit
Bug Fixes
Tests