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

Andy Green andy at warmcat.com
Thu Mar 7 05:52:36 CET 2019

On 06/03/2019 21:31, Mateusz Stępień wrote:
> Hello,
> When I try to bind to IPv4 source address in a client, it doesn't work 
> when IPv6 support is enabled. I see, that address is provided to bind in 
> a sockaddr_in structure because of inet_pton call
> https://libwebsockets.org/git/libwebsockets/tree/lib/plat/unix/unix-sockets.c?id=ecb22183f9c41bed6d8260a90f5f6f8c159662e3#n240 
>      if (inet_pton(AF_INET, ifname, &addr->sin_addr) == 1)
>          rc = LWS_ITOSA_USABLE;
> but later in bind call family is set to AF_INET6, thus being equivalent 
> to bind(sockfd, ([::], AF_INET6))
> https://libwebsockets.org/git/libwebsockets/tree/lib/core-net/network.c?id=ecb22183f9c41bed6d8260a90f5f6f8c159662e3#n271 
> With IPv6 support disabled, everything works fine.
> Reproduced on lws-minimal-ws-client-echo example with "iface" field set 
> to an IPv4 address in lws_client_context_info struct and enabled IPv6 
> support.

I can't seem to reproduce that.

I pushed a patch on lws-minimal-ws-client-echo that adds a -i iface 
switch that allows you to control the iface binding for the client 
connection from the commandline.

With IPv6 enabled at cmake, and running lws-minimal-ws-server-echo in 
another window, I can connect using

./bin/lws-minimal-ws-client-echo -s ::1
./bin/lws-minimal-ws-client-echo -s ::1 -i
./bin/lws-minimal-ws-client-echo -s ::1 -i ::1

./bin/lws-minimal-ws-client-echo -s
./bin/lws-minimal-ws-client-echo -s -i

The only combination that isn't happy is

./bin/lws-minimal-ws-client-echo -s -i ::1

You get the same results, or something different?


> Mateusz
> _______________________________________________
> Libwebsockets mailing list
> Libwebsockets at ml.libwebsockets.org
> https://libwebsockets.org/mailman/listinfo/libwebsockets

More information about the Libwebsockets mailing list