IPv6 question

J

John Burton

I'm not sure if this is a question about python socket support or sockets in
general ...

I have a socket server with which I want to accept incoming connections on
ipv4 and ipv6.
I've found that if I create the listening socket with
sock = socket(AF_INET, SOCK_STREAM)
it will accept connections on ipv4 (only) and if I create it with
sock = socket(AF_INET6, SOCK_STREAM)
it will accept connections with ipv6 (only)

Is there a way to make a single socket accept connections using both
protocols?
Or do I need create two sockets and listen on them both?
 
J

John Burton

John Burton said:
I'm not sure if this is a question about python socket support or sockets in
general ...

I have a socket server with which I want to accept incoming connections on
ipv4 and ipv6.
I've found that if I create the listening socket with
sock = socket(AF_INET, SOCK_STREAM)
it will accept connections on ipv4 (only) and if I create it with
sock = socket(AF_INET6, SOCK_STREAM)
it will accept connections with ipv6 (only)

Is there a way to make a single socket accept connections using both
protocols?
Or do I need create two sockets and listen on them both?

Oops I missed out the point of my question ...

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?
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

John said:
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
 
J

John Burton

Martin v. Löwis said:
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.

Thank you for your reply. That makes a lot of sense and I've had another
try and it does work as you suggest. (I was thinking that Ipv6 sockets
were not receiving connections via ipv4 because as you say they are
reporting an ipv6 style mapped address)

I've got a horrible feeling it doesn't work like this on windows though
(my python build on windows doesn't seem to support ipv6 so I can't
try it)
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

John said:
I've got a horrible feeling it doesn't work like this on windows though
(my python build on windows doesn't seem to support ipv6 so I can't
try it)

If you want to try IPv6 support on Windows, you can use a Python 2.4
snapshot I built, at

http://www.dcl.hpi.uni-potsdam.de/home/loewis/python2.4.0.12421.msi

If you don't want this snapshot to override file associations, make
sure you check the Advanced dialog.

Regards,
Martin
 
J

John Burton

Martin v. Löwis said:
If you want to try IPv6 support on Windows, you can use a Python 2.4
snapshot I built, at

http://www.dcl.hpi.uni-potsdam.de/home/loewis/python2.4.0.12421.msi

Thanks for this it seems to work well.
It does seem that on windows you need to bind different sockets to the same
address,
one for IPv4 and one for IPv6 whereas on linux the IPv6 socket will receive
connections to both.

I suppose that's not a big problem but a shame.
 
E

Erno Kuusela

John Burton said:
Thanks for this it seems to work well.
It does seem that on windows you need to bind different sockets to the same
address,
one for IPv4 and one for IPv6 whereas on linux the IPv6 socket will receive
connections to both.

I suppose that's not a big problem but a shame.

you can control this using the IPV6_V6ONLY socket option, like so:

s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)

unfortunately python 2.3.3 doesn't yet make that constant available
though it is fixed in cvs. you could glean the value from the headers.

the default setting has been in flux on various platforms recently,
all the more reason to want to always set it explicitly...

-- erno
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

Erno said:
you can control this using the IPV6_V6ONLY socket option, like so:

s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)

unfortunately python 2.3.3 doesn't yet make that constant available
though it is fixed in cvs. you could glean the value from the headers.

the default setting has been in flux on various platforms recently,
all the more reason to want to always set it explicitly...

I think the OP would prefer to have a single socket for both protocol
types, in which case he should set it to 1 on windows, right?

Regards,
Martin
 
J

John Burton

Martin said:
I think the OP would prefer to have a single socket for both protocol
types, in which case he should set it to 1 on windows, right?

Well it's not idea that different platforms work differently here
although it's easy enough to work around that. I'll try the setsockopt
and see if I can use that.
Thanks for the replies everyone.
 

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

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top