IPv6地址验证和规范化 [英] IPv6 address validation and canonicalization
问题描述
您使用了哪些库?它们彼此之间有多兼容?或者您是否编写了自己的解析例程?
What libs have you used for that? How compatible are they with one another? Or did you write your own parsing routine?
我对Java,C ++,Python和JavaScript的相互兼容的实现特别感兴趣,它们支持:
I'm particularly interested in mutually-compatible implementations for Java, C++, Python, and JavaScript, which support:
- 零压缩(
::
) - IPv4映射地址(
:: ffff:123.45.67.89
) - 规范化(包括简写形式,适用于人类)可读性)
- CIDR风格的网络掩码(最后是
/ 64
)
- zero compression ("
::
") - IPv4-mapped addresses ("
::ffff:123.45.67.89
") - canonicalization (including to the short form, for human readability)
- CIDR-style netmasks (like "
/64
" at the end)
推荐答案
在POSIX系统上,您可以使用 inet_pton
和 inet_ntop
组合进行规范化。您仍然需要进行自己的CIDR解析。幸运的是,我认为IPv6的唯一有效CIDR语法是/ number_of_bits表示法,因此相当容易。
On POSIX systems you can use inet_pton
and inet_ntop
in combination to do canonicalization. You will still have to do your own CIDR parsing. Fortunately, I believe the only valid CIDR syntax for IPv6 is the /number_of_bits notation, so that's fairly easy.
您将遇到的另一个问题是缺乏对接口规格。对于链接本地地址,您将在末尾看到诸如%eth0
之类的内容,以指定它们本地的链接。 getaddrinfo
会解析但 inet_pton
不会。
The other issue you will run into is the lack of support for interface specifications. For link-local addresses, you will see things like %eth0
on the end to specify what link they are local too. getaddrinfo
will parse that but inet_pton
won't.
您可以采用的一种策略是使用 getaddrinfo
来解析和 inet_ntop
进行规范化。
One strategy you could go for is using getaddrinfo
to parse and inet_ntop
to canonicalize.
getaddrinfo
适用于Windows。 inet_pton
和 inet_ntop
不是。幸运的是,编写代码来生成规范格式的IPv6地址并不困难。它将需要两次通过,因为0压缩的规则是首先出现的最大0字符串。 IPv4表单(即 :: 127.0.0.1
)仅用于 :: IPv4
或 :: ffff:IPv4
。
getaddrinfo
is available for Windows. inet_pton
and inet_ntop
aren't. Fortunately, it isn't too hard to write code to produce a canonical form IPv6 address. It will require two passes though because the rule for 0 compression is the biggest string of 0s that occurs first. Also IPv4 form (i.e. ::127.0.0.1
) is only used for ::IPv4
or ::ffff:IPv4
.
我没有要测试的Windows机器,但是从文档中可以看出Windows上的Python支持<在套接字模块中code> inet_pton 和 inet_ntop
。
I have no Windows machine to test with, but from the documentation it appears that Python on Windows supports inet_pton
and inet_ntop
in its socket module.
编写你的生成规范形式的自己的例程可能并不是一个坏主意,因为即使你的规范形式与其他人不一样,只要它有效,其他人就可以解析它。但我绝不会在解析 IPv6地址上编写您自己的例程。
Writing your own routine for producing a canonical form might not be a bad idea, since even if your canonical form isn't the same as everybody else's, as long as it's valid other people can parse it. But I would under no circumstances write a routine of your own to parse IPv6 addresses.
我的建议对Python,C和C ++。我对如何用Java或Javascript解决这个问题知之甚少。
My advice above is good for Python, C, and C++. I know little or nothing about how to solve this problem in Java or Javascript.
EDIT :我一直在研究getaddrinfo及其对应的getnameinfo 。这些几乎在所有方面都优于 inet_pton
和 inet_ntop
。它们是线程安全的,你可以传递它们的选项( AI_NUMERICHOST
在 getaddrinfo
的情况下,以及 NI_NUMERCHOST
在 getnameinfo
的情况下),以防止他们进行任何类型的DNS查询。它们的界面有点复杂,让我想起了某些方面丑陋的Windows界面,但要弄清楚要通过哪些选项来获得你想要的东西是相当容易的。我衷心地推荐他们两个。
EDIT: I have been examining getaddrinfo and its counterpart, getnameinfo. These are in almost all ways better than inet_pton
and inet_ntop
. They are thread safe, and you can pass them options (AI_NUMERICHOST
in getaddrinfo
's case, and NI_NUMERCHOST
in getnameinfo
's case) to keep them from doing any kind of DNS queries. Their interface is a little complex and reminds me of an ugly Windows interface in some respects, but it's fairly easy to figure out what options to pass to get what you want. I heartily recommend them both.
这篇关于IPv6地址验证和规范化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!