How can I get a string representation of an IPv4-mapped IPv6 that does not use a dotted quad?

huangapple go评论98阅读模式
英文:

How can I get a string representation of an IPv4-mapped IPv6 that does not use a dotted quad?

问题

考虑一下,例如这个IPv4映射的IPv6地址::ffff:7f7f:7f7f。在我测试过的所有浏览器的地址栏中输入http://[::ffff:7f7f:7f7f]时,格式保持不变:

How can I get a string representation of an IPv4-mapped IPv6 that does not use a dotted quad?

然而,netip(更具体地说,netip.AddrString方法)通过以IPv4点十进制表示法写入其最低有效32位来格式化所讨论的地址,如下所示:::ffff:127.127.127.127

package main

import (
	"fmt"
	"log"
	"net/netip"
)

func main() {
	ip, err := netip.ParseAddr("::ffff:7f7f:7f7f")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(ip) // ::ffff:127.127.127.127
}

(playground)

我需要复制浏览器地址栏格式化IPv4映射的IPv6地址的方式。有没有办法让netip::ffff:7f7f:7f7f格式化为::ffff:7f7f:7f7f,而不是::ffff:127.127.127.127

英文:

Consider, for example, this IPv4-mapped IPv6 address: ::ffff:7f7f:7f7f. When submitting http://[::ffff:7f7f:7f7f] in the address bar of all browsers I've tested, the format is retained:

How can I get a string representation of an IPv4-mapped IPv6 that does not use a dotted quad?

However, the netip package (more specifically, the String method of netip.Addr) formats the address in question by writing its least-significant 32 bits in the IPv4 dot-decimal notation, like so: ::ffff:127.127.127.127.

package main

import (
	"fmt"
	"log"
	"net/netip"
)

func main() {
	ip, err := netip.ParseAddr("::ffff:7f7f:7f7f")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(ip) // ::ffff:127.127.127.127
}

(playground)

I need to replicate the way browsers' address bar format IPv4-mapped IPv6 addresses.
Is there any way to get netip to format ::ffff:7f7f:7f7f, not as ::ffff:127.127.127.127, but as ::ffff:7f7f:7f7f?

答案1

得分: 2

首先,请记住,在网络上不允许使用IPv4-Mapped IPv6地址,如_IANA IPv6特殊用途地址注册表_所述。请注意,它们不能用作源地址或目的地址,不能进行转发或全局路由,并且它们由IP本身保留。它们不是实际的IPv6地址,只是IPv4地址在IPv6格式中的表示,以便拥有一个共同的地址存储,例如数据库。它们不应该在您的浏览器中工作。

英文:

First, remember that IPv4-Mapped IPv6 addresses are not allowed to be used on a network, as explained in the IANA IPv6 Special-Purpose Address Registry, Notice that they cannot be used as source or destination addresses, cannot be forwarded or globally routable, and they are reserved by IP itself They are not actual IPv6 addresses, only a representation of IPv4 addresses in the IPv6 format in order to have a common address store, e.g. database. They should not work in your browser.

答案2

得分: 1

你可以使用ipaddress-go库,它可以生成各种字符串。免责声明:我是该库的项目经理。

addr := ipaddr.NewIPAddressString("::ffff:7f7f:7f7f").GetAddress()
addrStr := addr.String()
fmt.Println(addrStr)
mixedAddrStr, _ := addr.ToIPv6().ToMixedString()
fmt.Println(mixedAddrStr)

输出:

::ffff:7f7f:7f7f
::ffff:127.127.127.127
英文:

You can use the ipaddress-go library, which can produce a variety of strings. Disclaimer: I am the project manager of the library.

addr := ipaddr.NewIPAddressString("::ffff:7f7f:7f7f").GetAddress()
addrStr := addr.String()
fmt.Println(addrStr)
mixedAddrStr, _ := addr.ToIPv6().ToMixedString()
fmt.Println(mixedAddrStr)

Output:

::ffff:7f7f:7f7f
::ffff:127.127.127.127

答案3

得分: 0

你可以使用net.IP而不是netip.Addr,根据文档的说明:

Addr.String():

> 需要注意的是,与net包中的IP.String方法不同,IPv4映射的IPv6地址在点分十进制之前会有一个“::ffff:”前缀。

英文:

You could use net.IP rather than netip.Addr, as per the docs:

Addr.String():

> Note that unlike package net's IP.String method, IPv4-mapped IPv6 addresses format with a "::ffff:" prefix before the dotted quad.

huangapple
  • 本文由 发表于 2023年1月6日 22:39:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75032348.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定