By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,235 Members | 1,057 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,235 IT Pros & Developers. It's quick & easy.

IPv6 question

P: n/a
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?
Jul 18 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
"John Burton" <jo*********@jbmail.com> wrote in message
news:9r************@neon.jbmail.com...
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?
Jul 18 '05 #2

P: n/a
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

Jul 18 '05 #3

P: n/a
Martin v. Lwis" <ma****@v.loewis.de> wrote in message
news:c0*************@news.t-online.com...
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.


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)
Jul 18 '05 #4

P: n/a
John Burton wrote:
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/ho....4.0.12421.msi

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

Regards,
Martin

Jul 18 '05 #5

P: n/a
"Martin v. Lwis" <ma****@v.loewis.de> wrote in message
news:c0*************@news.t-online.com...
John Burton wrote:
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/ho....4.0.12421.msi

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


Thanks! I'll take a look.
Jul 18 '05 #6

P: n/a
"Martin v. Lwis" <ma****@v.loewis.de> wrote in message
news:c0*************@news.t-online.com...
John Burton wrote:
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/ho....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.
Jul 18 '05 #7

P: n/a
"John Burton" <jo*********@jbmail.com> writes:
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
Jul 18 '05 #8

P: n/a
Erno Kuusela wrote:
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

Jul 18 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.