tags:

views:

14

answers:

1

I swear I've seen this asked before, but I can't hit upon the magic search string to find it for me:

I create a socket for listening using:

s = socket()...
getaddrinfo("::1",...);
bind()...

and I end up with an IPV6 socket bound to "::1" and an IPV4 socket bound to "0.0.0.0". I expect to get the IPV4 socket bound to "127.0.0.1". Why is this happening, and how can I fix it to only accept localhost connections on the ipv4 socket as wel?

This is on Win7 and I've previously turned off IPV6_V6ONLY.

A: 

getaddrinfo() and bind() only operate on one IP at a time. IPv4 binds to 0.0.0.0 if you do not specify your own explicit binding. It sounds like you are calling bind() only once for the IPv6 portion of a dual-stack socket and ignoring its IPv4 portion. I do not know if this will work, but try calling bind() twice, once with the IPv6 "::1" address, and again with the IPv6 "::FFFF:127.0.0.1" address (remember that IPv4 addresses on a dual-stack socket must be represented as IPv4-mapped IPv6 addresses).

Remy Lebeau - TeamB
I am not sure i follow. I cannot call bind twice on the same socket, the second call should fall, no?
Will I Am
A dual-stack socket (which you get when you disable the IPV6_V6ONLY option) supports both IPv4 and IPv6 on the same socket. I have never worked with dual-stack sockets yet, that is why I said "I do not know if this will work". I cannot find any documentation, for any platform, that explains how the IPv4 side of a dual-stack socket behaves during bind(). However, ultimately it is still just an IPv6 socket, so I would expect bind() to actually ignore the IPv4 0.0.0.0 altogether and only accept traffic from 127.0.0.1, since that is what IPv6 is binding to.
Remy Lebeau - TeamB
BTW, you are not the only person to notice the "::1/0.0.0.0" issue on Windows: Dave noted the same issue in this discussion: http://stackoverflow.com/questions/1618240/how-to-support-both-ipv4-and-ipv6-connections
Remy Lebeau - TeamB
One thing you could try as a workaround is to enable the `SO_CONDITIONAL_ACCEPT` socket option, and then use a WSAAccept() callback function to reject IPv4 connections that are not received on 127.0.0.1.
Remy Lebeau - TeamB