views:

336

answers:

3

I have an ipv4 server that only accepts connections over localhost (using INADDR_LOOPBACK). I'd like to convert this server to be dual-stack ipv6/ipv4. However, using in6addr_loopback only accepts connections to ::1.

I've found that I can accept ipv4 and ipv6 connections simultaneously using in6addr_any, but as this also allows connections from anywhere it's not useful for my particular case.

Is it possible to bind to ipv6 localhost (::1) and ipv4 localhost (127.0.0.1) simultaneously?

+2  A: 

AFAIK, that is not possible.

What you will need to do is create two sockets, one bound to INADDR_LOOPBACK and one bound to in6addr_loopback. You can then wait on both of them simultaneously using your multiplexer of choice (poll, select, epoll, etc0).

Update

Just came up with some work-arounds that you may want to consider. In both of these work-arounds, you bind to in6addr_any.

  1. When a connection is established, check the remote address and if it's not 127.0.0.1 or ::1, close it. While the behavior of connecting on an IP address is not ideal (connections are established/immediately closed instead of being refused), the nice thing it that this can be done purely in your application.
  2. Adjust the settings of your OS's IP stack to refuse connections to your port from non loopback IP's. This is definitely doable on Linux with iptables. While the behavior is more ideal, it requires configuration external to your application.
R Samuel Klatchko
That's what I figured, but I wanted to make sure. Thanks.
paleozogt
A: 

Just because your server is dual-stack, does your localhost-only app really need to be? Can you just pick one and go with it?

Yuliy
A: 

Just as R Samuel Klatchko has said, it is not possible. Moreover, within a single network, only either IPv4 or IPv6 can exist, but not both. What I suggest you to do is to get your network to auto translate IPv4 addresses into IPv6. Then only handle those IPv6 addresses. For more details, please turn to ServerFault. We've got tons of experts there. Good luck! ;)

shinkou
That is simply not true. IPv4 and IPv6 can both run over a network. Sure, the line is not completely blurred — for example, security rules applied to IPv4 may not affect IPv6 traffic, but at the end of the day, dual-stack is where it's at.
Jeremy Visser
Do you mean that IPv4 and IPv6 can exist simultaneously within a single network segment? Without translation? If that's the case, could you point me to some reference please? I may have to update my info.
shinkou
As far as I understand, within a network segment you're not addressing through IP anyway, though (you broadcast ARP, and a host can respond to either its IPv4 address or IPv6 address, then you use MAC etc. for addressing)
Yuliy