Skip to content

Comments

[Fix] IP Address Conversion Functions for Proper IPv4-Mapped IPv6 Format (Issue #641)#690

Merged
Mzack9999 merged 3 commits intoprojectdiscovery:mainfrom
s-cu-bot:main
Sep 24, 2025
Merged

[Fix] IP Address Conversion Functions for Proper IPv4-Mapped IPv6 Format (Issue #641)#690
Mzack9999 merged 3 commits intoprojectdiscovery:mainfrom
s-cu-bot:main

Conversation

@s-cu-bot
Copy link
Contributor

@s-cu-bot s-cu-bot commented Sep 20, 2025

Fix IPv6 → IPv4 Conversion and Adjust IPv4-to-IPv6 Mapping Format

Summary

This PR fixes the IPv6 ↔ IPv4 conversion logic in mapcidr with two key changes:

  • IPv6 → IPv4 conversion now works correctly: both ::ffff:a.b.c.d and raw hex forms like 00:00:00:00:00:ffff:c0a8:0101 are recognized and mapped back to a.b.c.d.
  • Round-trip conversion (IPv4 → IPv6 → IPv4) is now correct and lossless.

Motivation & linked issue

Fixes: Broken IPv6 ↔ IPv4 conversions in mapcidr.

  • Problem:
    • Inputs like 00:00:00:00:00:192.168.1.1 could not be parsed back to IPv4.
    • Inputs like 00:00:00:00:00:ffff:c0a8:0101 could not be parsed back to IPv4.
  • Expected:
    • ToIP4("::ffff:192.168.1.1")192.168.1.1
    • ToIP4("00:00:00:00:00:ffff:c0a8:0101")192.168.1.1

Issues

  • ToIP4 failed to parse hex-form IPv6 literals into IPv4.
  • Round-trip conversions were inconsistent.

Improvements

  • ToIP4 can parse both hex-form and ::ffff:-style mapped addresses.
  • Bidirectional consistency guaranteed: IPv4 → IPv6 → IPv4 yields the same IPv4.

CLI examples

IPv6 (hex) → IPv4

echo ::ffff:c0a8:0101 | mapcidr -t4
# Before: malformed output(bug)
# After : 192.168.1.1

IPv6 (::ffff mapped) → IPv4

echo ::ffff:192.168.1.1 | mapcidr -t4
# Before: malformed output(bug)
# After : 192.168.1.1

Round-trip

echo 192.168.1.1 | mapcidr -t6 | mapcidr -t4
# Before: malformed output(bug)
# After : 192.168.1.1

Test

[Problem 1]
이슈0
=> This bug has a colon, but it is recognized as ipv4 (fixed in main.go)

[Problem 2 (After Problem1 fix)]
이슈1
=> This bug occurs in the logic that adds unconditionally without verifying the IP format.

[1,2 > Solution]
이슈1_해결

[Problem 3]
이슈2

[3 > Solution]
이슈2_해결

Summary by CodeRabbit

  • Bug Fixes

    • Correctly append /32 for IPv4 and /128 for IPv6 when processing single IP inputs.
    • Ensure IPv4 addresses in ranges are consistently represented as IPv4‑mapped IPv6 where applicable.
    • Improve IPv4↔IPv6 conversion accuracy, including support for ::ffff formats and round-trips.
  • Tests

    • Added integration tests covering IPv6/IPv4 conversions and round‑trip cases for mapped (::ffff) and hex formats.

@coderabbitai
Copy link

coderabbitai bot commented Sep 24, 2025

Walkthrough

Adds 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

Cohort / File(s) Summary
Integration tests: IPv4/IPv6 conversions
cmd/integration-test/mapcidr.go
Adds test cases covering IPv6-mapped IPv4 inputs and round-trip conversions between IPv4 and IPv6-mapped forms using -t4 and -t6.
CLI single-IP CIDR suffix logic
cmd/mapcidr/main.go
Replaces type-switch with string-based check: append /32 when no colon is present (IPv4), else /128 (IPv6); modifies control flow for standalone IP handling.
IP range mapping adjustments
ip.go
Simplifies ipNetToRange to map firstIP and lastIP to IPv4-mapped IPv6 whenever To4() is non-nil; separates conditions for first and last IP mappings.

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
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Poem

A hop, a skip, through colons and dots,
I nibble at bits in mapped garden plots.
/32 here, /128 there—
Carrots of tests in meticulous care.
From v4 to v6, I bound with delight,
Ears up for ranges that now map just right. 🥕🐇

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title accurately describes the primary change of correcting IP conversion functions to handle IPv4-mapped IPv6 formats and aligns with the content of the pull request, making it clear to reviewers what is being addressed. It is concise, focused on the core functionality update, and avoids irrelevant details. The inclusion of the issue reference does not detract from its clarity.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@Mzack9999 Mzack9999 merged commit 56c7bcd into projectdiscovery:main Sep 24, 2025
6 of 7 checks passed
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.1 to ensure only a single host is filtered (see main.go comment about /32 vs /128 consistency).

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f63ba9a and 4798974.

📒 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 cidrsToNetworks

In 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix IP Address Conversion Functions for Proper IPv4-Mapped IPv6 Format

2 participants