views:

1624

answers:

6

What libs have you used for that? How compatible are they with one another? Or did you write your own parsing routine?

I'm particularly interested in mutually-compatible implementations for Java, C++, Python, and JavaScript, which support:

  • 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)
A: 

From what I can gather, you should use getaddrinfo() on systems that have it, i.e. Linux and POSIX places. There should be no need to write your own low-level parser.

Windows also provides getaddrinfo(), on XP and later.

unwind
I don't think getaddrinfo supports parsing subnet bit-slicing notation. It also doesn't support canonicalization.
Omnifarious
+1  A: 

In Java, You could use

InetAddress.getByName(IP)

and then check for exceptions thrown by this for validating IPv6 addresses

You could also use Sun Propreitary API if thats oK to you. THis will not perform a DNS lookup. ( They might change it/remove it without notice since its their propreitary API.This is a warning that will come when compiling a code using this )

boolean sun.net.util.IPAddressUtil.isIPv6LiteralAddress(String IP)
Aviator
Thanks for the idea.Unfortunately that method does a DNS lookup before throwing the exception.
ngn
Okie.. I looked into source code of InetAddress. Its using sun.net.util.IPAddressUtil class. Its a sun propreietary API. But if that's okie to you, you could use it. Use the static method isIPv6LiteralAddress(String IP). It returns true or false
Aviator
IPAddressUtil works great. I guess I can cope with canonicalization too, via textToNumericFormatV*(String) and some custom formatting back to text.+1 and I wish I could accept more than one answer.
ngn
Wow. Glad i could help! :) No prob at all!
Aviator
+6  A: 

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.

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.

One strategy you could go for is using getaddrinfo to parse and inet_ntop to canonicalize.

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.

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.

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.

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: 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.

Omnifarious
I would like to comment that after playing with getaddrinfo myself, the OSX 10.4 and glibc-2.10.1 versions both have interesting bugs. The OSX version has much worse bugs.
Omnifarious
Windows does support inet_pton and inet_ntop for Vista and later (see http://msdn.microsoft.com/en-us/library/cc805843%28VS.85%29.aspx). However, Python 2.6 and 3.0 does not (docs indicate Availability Unix).
Jason R. Coombs
Watch out for speed in Windows. For some reason the Windows inet_pton and inet_ntop are horribly horribly slow. Or they were in my experience on Vista and Server 2008.
Zan Lynx
A: 

For Python, the best solution might be IPy (http://pypi.python.org/pypi/IPy/0.51)

Chris
A: 

It's too bad Python 3.1 lost the ipaddr lib.

It's still available as a third-party library: py-ipaddr available on PyPI.

Compression

>>> ipaddr.IPv6Address('0:0::0:1').compressed
'::1'

IPv4 mapping

>>> ipaddr.IPv6Address('::ffff:123.45.67.89').ipv4_mapped
IPv4Address('123.45.67.89')

CIDR

>>> ipaddr.IPv6Network('::ffff:123.45.67.89/128')
IPv6Network('::ffff:7b2d:4359/128')
Jason R. Coombs
A: 

getaddrinfo already returns the shortest textual form, so if you have the longer form you can canonicalize by running through getaddrinfo again. Example Python,

import sys, socket;
result = socket.getaddrinfo('0:0::0:1', None);
print "family:%i socktype:%i proto:%i canonname:%s sockaddr:%s"%result[0];

Outputs the following,

family:10 socktype:1 proto:6 canonname: sockaddr:('::1', 0, 0, 0)

IPv6 CIDR blocks don't appear to be well documented or defined so I wrote my own inet6_network implementation to handle that (C99).

Steve-o