paho.mqtt.c icon indicating copy to clipboard operation
paho.mqtt.c copied to clipboard

IpV6 link local addresses resolve but don't connect

Open rdaum opened this issue 10 months ago • 2 comments

Describe the bug

ipv6 link local style addresses don't work.

paho.mqtt.c successfully resolves addresses of the form

tcp://[xxxx::xxxx:xxxx:xxxx%eth0]:1883

But when it comes to connect, the complete results from getaddrinfo are not copied over into the addrinfo, as per line 1130 of Socket.c:

		if (res->ai_family == AF_INET6)
		{
			address6.sin6_port = htons(port);
			address6.sin6_family = family = AF_INET6;
			memcpy(&address6.sin6_addr, &((struct sockaddr_in6*)(res->ai_addr))->sin6_addr, sizeof(address6.sin6_addr));
```		

This only copies over the address, but not the `sin6_scope_id` and `sin6_flowinfo`, so the complete link local address cannot connect.

Adding these two lines (or cleaner variant thereof) led to success for me:

```c
			memcpy(&address6.sin6_scope_id, &((struct sockaddr_in6*)(res->ai_addr))->sin6_scope_id, sizeof(address6.sin6_scope_id));
			memcpy(&address6.sin6_flowinfo, &((struct sockaddr_in6*)(res->ai_addr))->sin6_flowinfo, sizeof(address6.sin6_flowinfo));
		}

To Reproduce

Attempt to connect to an ipv6 link local address to a mqtt broker. It will fail at connect time. Adding the two lines listed above makes it succeed.

Expected behavior

Paho should be able to connect to a broker using such an address.

** Environment (please complete the following information):**

  • Linux
  • paho.mqtt.c checked out @ commit hash 6b1e202a701ffcdaa277b5644ed291287a70a7aa

rdaum avatar Apr 09 '24 18:04 rdaum

Fix at https://github.com/eclipse/paho.mqtt.c/compare/master...rdaum:paho.mqtt.c:master

I'd create a PR, but I'm not going to go register and sign the Eclipse contributors agreement just for this. Please apply it yourselves.

rdaum avatar Apr 10 '24 15:04 rdaum

I made a PR.. #1466 I had to reset the author to pass eca check. I hope thats fine for everybody.

jumoog avatar Apr 10 '24 17:04 jumoog