Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>On POSIX systems you can use <code>inet_pton</code> and <code>inet_ntop</code> 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.</p> <p>The other issue you will run into is the lack of support for interface specifications. For link-local addresses, you will see things like <code>%eth0</code> on the end to specify what link they are local too. <code>getaddrinfo</code> will parse that but <code>inet_pton</code> won't.</p> <p>One strategy you could go for is using <code>getaddrinfo</code> to parse and <code>inet_ntop</code> to canonicalize.</p> <p><code>getaddrinfo</code> is available for Windows. <code>inet_pton</code> and <code>inet_ntop</code> 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. <code>::127.0.0.1</code>) is only used for <code>::IPv4</code> or <code>::ffff:IPv4</code>.</p> <p>I have no Windows machine to test with, but from the documentation it appears that Python on Windows supports <code>inet_pton</code> and <code>inet_ntop</code> in its socket module.</p> <p>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 <strong>parse</strong> IPv6 addresses.</p> <p>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.</p> <p><strong>EDIT</strong>: I have been examining getaddrinfo and its counterpart, getnameinfo. These are in almost all ways better than <code>inet_pton</code> and <code>inet_ntop</code>. They are thread safe, and you can pass them options (<code>AI_NUMERICHOST</code> in <code>getaddrinfo</code>'s case, and <code>NI_NUMERCHOST</code> in <code>getnameinfo</code>'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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload