Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If I want to connect to a server, does it have to be the same protocol between the server and the client? #3340

Open
yjq91115 opened this issue Feb 19, 2025 · 9 comments

Comments

@yjq91115
Copy link

Hi, Andy, I just started learning libwebsocket. I have a question. If I want to connect to a server, does it have to be the same protocol between the server and the client? If I want to transmit data to some servers, such as echo.websocket.org, how should I do it?

@lws-team
Copy link
Member

Read rfc6455 for info on how ws itself works, see section 1.9 for how subprotocol works.

https://www.rfc-editor.org/rfc/rfc6455

Many servers take the lazy approach to just offer one, unnamed subprotocol, which lws can match with by choosing NULL on its side.

@yjq91115
Copy link
Author

Thanks for your answer. Sorry to bother you again, I would like to ask, if dns returns both ipv4 and ipv6 addresses, which address will lws choose to connect to? At present, it seems that the address returned first is the address returned by the connection.My libwebsocket version is 4.3.1

Image

@lws-team
Copy link
Member

Not sure about v4.3 based stuff but main branch lws (from git) sorts the DNS results according to various RFCs and compares the situation found from netlink about which nets (ipv4 and v6) are routable from available network interfaces.

https://github.com/warmcat/libwebsockets/blob/main/lib/core-net/client/sort-dns.c

There are many possible situations in the wild where, eg, ipv6 is present at a local network interface, but only link-local scope, or routes in the LAN OK but has no exit to the internet and so on. So it will do its best to choose the best bets first and retry through the list as it finds it is needed.

@yjq91115
Copy link
Author

When I turn off netlink, the dns_sorted_list has both ipv6 and ipv4 addresses , and the ipv6 address is connected first, but when the ipv6 does not return a SYN packet, it seems that lws will not try to establish a connection with the ipv4 address

Image

Image

@yjq91115
Copy link
Author

When I turn off netlink, the dns_sorted_list has both ipv6 and ipv4 addresses , and the ipv6 address is connected first, but when the ipv6 does not return a SYN packet, it seems that lws will not try to establish a connection with the ipv4 address

Image

Image

At this time, the client is retransmitting the SYN packet, hoping to get the SYN ACK packet from the server.

@lws-team
Copy link
Member

lws uses the network interface address information to choose which dest addresses have the best chance. If it reacts to it by discarding ipv6 it probably has a reason (or is buggy). main branch lws has the best code for all this stuff.

It looks like it tries to write at mbedtls layer before the underlying socket is connected. Is, eg, your code trying to do an lws_write() before the underlying tcp socket is ready?

@yjq91115
Copy link
Author

In my configs, netlink is not enabled. In lws_sort_dns, it seems that the IP address is only put into the list without filtering. When the timeout period is exceeded and the client is still retransmitting the syn packet, lws seems to think that the TCP socket is ready and can perform mbedtls handshake.

@yjq91115
Copy link
Author

yjq91115 commented Feb 26, 2025

When there is still an IP address in the list and the timeout period has expired, the address is connected. Therefore, I will delete the “!”, Is it reasonable? (After deletion, when the IPv6 address does not respond within the timeout period, it will connect to the second IPv4 address)

Image

@lws-team
Copy link
Member

Are you using main branch? You should do so for this stuff. It has significant work not in v4.3.

I don't think the code you point at is related. It's trying to see if there's no addresses in the list and no ongoing timeout to get addresses in the list, that we have essentially timed out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants