Skip to content

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

@chenjb04

Description

@chenjb04

https://github.com/projectdiscovery/mapcidr/blob/1de65eda55f3cb189fe049520bfcc4b530dd995c/ip.go#L911C1-L940C1

Desciption

The current implementation of ToIP6 and ToIP4 functions does not properly convert between IPv4 and IPv4-mapped IPv6 address formats. The IPv4-mapped IPv6 addresses should follow the format ::ffff:a.b.c.d where a.b.c.d is the IPv4 address in dotted-decimal notation.

Current Behavior

  • ToIP6 converts IPv4 addresses to a format that is not properly recognized as IPv4-mapped IPv6 addresses
  • ToIP4 does not correctly extract the IPv4 component from IPv4-mapped IPv6 addresses
  • Bidirectional conversion (IPv4 → IPv6 → IPv4) does not preserve the original address

Expected Behavior

  • ToIP6 should convert IPv4 addresses to the standard IPv4-mapped IPv6 format (::ffff:a.b.c.d)
  • ToIP4 should extract and return the IPv4 component from IPv4-mapped IPv6 addresses
  • Conversion should be bidirectionally consistent (IPv4 → IPv6 → IPv4 should return the original IPv4 address)
ip := "192.168.1.1"
ipv6, err := ToIP6(ip)
// Expected: ipv6 = "::ffff:192.168.1.1"
// Actual: Different format not conforming to standards

ipv6 := "::ffff:192.168.1.1"
ipv4, err := ToIP4(ipv6)
// Expected: ipv4 = "192.168.1.1"
// Actual: Error or incorrect extraction

Proposed Solution

Update the functions to handle the conversion properly:
1.ToIP6 should:

  • Check if the input is already an IPv6 address
  • For IPv4 addresses, convert to the format ::ffff:a.b.c.d

2.ToIP4 should:

  • Check if the input is a native IPv4 address
  • For IPv6 addresses, verify if it's an IPv4-mapped address (starts with ::ffff:)
  • Extract the IPv4 component from valid IPv4-mapped addresses
  • Return an error for non-IPv4-mapped IPv6 addresses

Metadata

Metadata

Assignees

No one assigned

    Labels

    InvestigationSomething to InvestigateType: BugInconsistencies or issues which will cause an issue or problem for users or implementors.

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions