Skip to content

Improve IP address handling for v1 and version detection#169

Open
rbqvq wants to merge 2 commits intopires:mainfrom
rbqvq:4in6
Open

Improve IP address handling for v1 and version detection#169
rbqvq wants to merge 2 commits intopires:mainfrom
rbqvq:4in6

Conversation

@rbqvq
Copy link
Copy Markdown

@rbqvq rbqvq commented Apr 17, 2026

The standard net.IP implementation inconsistently represents IPv4-mapped IPv6 addresses; its String() method returns an IPv4 string for mapped addresses, which causes formatting issues in v1 headers where strict version representation is required. (#167)

This patch migrates IP state management to netip.Addr to ensure that String() outputs and byte representations strictly follow the expected protocol format.

Additionally, this fix addresses a bug in the header construction logic.

The previous implementation only verified the source IP version, leading to incorrect protocol family assignments when source and destination address versions mismatched.

The logic now ensures that TCPv6/UDPv6 is selected if either address is IPv6, with IPv4 addresses correctly formatted as IPv4-mapped IPv6 addresses when necessary.

  • Migrate from net.IP to netip.Addr for robust IP handling
  • Update version detection to fallback to IPv6 if any address is IPv6
  • Ensure strict 4-byte or 16-byte alignment in v1 header output

Migrate from #115

rbqvq added 2 commits April 17, 2026 20:37
The standard `net.IP` implementation can not represent IPv4-mapped IPv6 addresses, `String()` on IPv4-mapped-IPv6 addresses will return IPv4 addresses, which may lead to formatting issues in v1 headers [1].

Migrate from `net.IP` to `netip.Addr` for IP state management to ensure `String()` outputs strictly follow the expected version format.

Link:
[1]: pires#167

Signed-off-by: Coia Prant <coiaprant@gmail.com>
…atch

The previous logic only checked the source IP version, which could
lead to incorrect protocol family assignment if the destination IP
used a different version.

This change ensures that TCPv6/UDPv6 is used if either address is IPv6.

The IPv4 address will be format as IPv4-mapped IPv6 address.

Signed-off-by: Coia Prant <coiaprant@gmail.com>
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.

1 participant