[Libwebsockets] Binding to IPv4 source address fails with IPv6 enabled

Andy Green andy at warmcat.com
Fri Mar 8 02:08:49 CET 2019



On 07/03/2019 17:18, Mateusz Stępień wrote:
> On 3/7/19 5:52 AM, Andy Green wrote:
>>
>> You get the same results, or something different?
> 
> I am reproducing this in a following way:
> 
> I have a server running on a Linux in a VM, which is connected with 
> interface vboxnet0 to the host. Host and guest are able to communicate 
> through this interface with an address 10.10.10.100
> 
> I have a second interface, vboxnet1, which is not connected to the VM, 
> and it has IP address assigned on the host side, 10.10.20.100
> 
> The client connects to the server on address 10.10.10.100, but I'm 
> binding it to the address 10.10.20.100. With this configuration I 
> expect, that the client would not be able connect to the server, and 
> that is the case with IPv6 support DISABLED. With IPv6 support enabled,
> socket is bound to ([::], AF_INET6), as explained it the previous post, 
> thus it manages to connect to the server.
> 
> I hope that this explanation is sufficient.

This took a long time for me to align my understanding with the actual 
problem.  Part of the difficulty is my home internet connection has no 
native ipv6, so my home router has none either.  Without a working ipv6 
link, this problem can't be reproduced.  In the end I brought up a VM 
and ran it against a server on the host as you described.

What's happening here is the client connection code has to make a 
decision about whether to create an IPv4 or IPv6 socket before 
attempting to bind it somewhere.

Until now, if IPv6 is enabled, it favours an IPv6 socket, and should the 
name resolution came up with an IPv4 address, it sticks with IPv6 and 
translates the address to IPv4-in-IPv6.  And that's all workable.

However before the connect() we may bind() the socket, if we try to do 
that and it uses the ipv4 identity of the network interface, that 
doesn't work.

I pushed a patch on master that checks the situation with client info 
.iface before creating the socket as ipv6... if it was going to use an 
ipv6 connection but finds an IPv4 address in iface, then it 
force-downgrades the socket to use ipv4 so the interface binding can happen.

-Andy

> Mateusz


More information about the Libwebsockets mailing list