Convert IPv6 address to binary representation on 2.x/Windows

  • Thread starter Philipp Hagemeister
  • Start date
P

Philipp Hagemeister

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

socket.inet_pton which does exactly what I want is not available on 2.x
on Windows. Strangely, the documentation of socket.inet_aton (IPv4 only)
reads:

"inet_aton() does not support IPv6, and getnameinfo() should be used
instead for IPv4/v6 dual stack support."

Let's try that:
.... try:
.... res = socket.getnameinfo((addr,port), flag)
.... except:
.... res = "error"
.... return res
....
[(ni,gni(socket.__dict__[ni])) for ni in filter(lambda
k:k.startswith("NI_"), dir(socket))]

[('NI_DGRAM', ('localhost', 'www')), ('NI_MAXHOST', 'error'),
('NI_MAXSERV', ('localhost', 'www')), ('NI_NAMEREQD', ('localhost',
'www')), ('NI_NOFQDN', ('localhost', 'www')), ('NI_NUMERICHOST', ('::1',
'www')), ('NI_NUMERICSERV', ('localhost', '80'))]
Neither of these values looks like 0x0000000000000001. Am I missing
something or is the documentation just wrong? If so, how am I supposed
to get a binary representation of an IPv6 address in the absence of
socket.inet_pton? Should I write my I own version?
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEAREKAAYFAkmu5N4ACgkQ9eq1gvr7CFwNUgCdF4QdT2LlWWXNlKYbBvKEeTlh
lDMAn2eGmFdx7rvM9+gr7tnHlENhgmq7
=Gsal
-----END PGP SIGNATURE-----
 
M

Martin v. Löwis

Neither of these values looks like 0x0000000000000001. Am I missing
something or is the documentation just wrong? If so, how am I supposed
to get a binary representation of an IPv6 address in the absence of
socket.inet_pton? Should I write my I own version?

I do wonder why you need a binary representation of an IPv6 address...

Yes, writing your own routine is certainly an option. Alternatively,
you can try one of the IP address manipulation libraries, such as
ipaddr, or netaddr. Disclaimer: I haven't checked whether these support
the requested functionality; please report back when you know.

Regards,
Martin
 
P

Philipp Hagemeister

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

Hi Martin,
I do wonder why you need a binary representation of an IPv6 address...
I'd like to subscribe to an IPv6 multicast address via
socket.setsockopt(IPPROTO_IPV6, IPV6_JOIN_GROUP, binary_address).
Yes, writing your own routine is certainly an option.
Is it the preferred one? Because I need this for two purposes: My own
code as well as the python multicast example Demo/socket/mcast.py [1].
Alternatively,
you can try one of the IP address manipulation libraries, such as
ipaddr, or netaddr. Disclaimer: I haven't checked whether these support
the requested functionality; please report back when you know.
ipaddr[2] has functions that would certainly helpful (int(IP('::1'))
yields 1), but not quite the one I'm looking for, although it would be
trivial to write it. But then, why should I need ipaddr?
netaddr[3] has the function I'm looking for Addr('::1').packed(), but
it's way over the top for this purpose; an assembler implementation
would be more readable.

Kind regards,

Philipp Hagemeister

[1] http://bugs.python.org/issue5379
[2] http://ipaddr-py.googlecode.com
[3] http://code.google.com/p/netaddr/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEAREKAAYFAkmu96cACgkQ9eq1gvr7CFzlAwCgrMXI6PVrBGXP5phvv2Fk//9b
pQ0An37q6/KNZtIP4OvzYh68NXg4HCU4
=bNqC
-----END PGP SIGNATURE-----
 
M

Martin v. Löwis

I do wonder why you need a binary representation of an IPv6 address...
I'd like to subscribe to an IPv6 multicast address via
socket.setsockopt(IPPROTO_IPV6, IPV6_JOIN_GROUP, binary_address).

I see.
Is it the preferred one?

Preferred by whom? I would prefer to pass a socket.ipv6_mreq structure
to IPV6_JOIN_GROUP, rather than a byte array. Failing that, I would
prefer to use inet_pton. Since the first one is not implemented, and the
second one not on Windows, I would prefer to receive patches that
make them implemented.

Regards,
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top