IPv6 question

"Martin v. Löwis" martin at v.loewis.de
Sun Feb 15 13:01:54 EST 2004


John Burton wrote:
> If I need to create an IPv4 socket and IPv6 socket and listen on both that
> doesn't
> seem to work.
> 
>  sock4 = socket(AF_INET, SOCK_STREAM)
>  sock4.bind(('', 12345))
>  sock6 = socket(AF_INET6, SOCK_STREAM)
>  sock6.bind(('', 12345))
> 
> I get an error that the address is already in use on the 2nd bind even
> though it's to a different
> socket type. This is on linux.
> Has anyone got this to work? Or am I missing something?

You are missing something important. According to RFC 2553, a PF_INET6
socket can be used for IPv4 communication, by means of IPV4_MAPPED
addresses (::FFFF:<IPv4-address>). So an application openening a
PF_INET6 listening socket will accept both IPv4 and IPv6 incoming
connections.

If you want a socket that listens only on IPv6 connections, you
need to set the IPV6_V6ONLY socket option. In your case, it is 
sufficient to just not create the IPv4 socket. When clients connect
through IPv4, you will find that the peername(2) of the socket
is an IPv4-mapped address. You should never ever transmit such
an address over the wire; it is meant for local use only.

Regards,
Martin




More information about the Python-list mailing list