mjansson / mdns

Public domain mDNS/DNS-SD library in C
The Unlicense
437 stars 117 forks source link

open_client_sockets bad local address #49

Closed ben-sedg closed 3 years ago

ben-sedg commented 3 years ago

Hi Mattias.

When running on Linux, I get a print from the open_client_sockets() function: Local IPv4 address: 0.0.0.0

However when running, it seems the local IP address used for the server, or other cases, is not 0.0.0.0, but the actual IP address of the network interface. I suspect that the call to mdns_socket_open_ipv4() inside the function open_client_sockets(), changes the sockaddr structure and changes the source IP address to 0.0.0.0. I added another print: Once at the start of the for loop, iterating the network devices, and once at the end. The result was: Local IPv4 address: 10.5.205.79 Local IPv4 address: 0.0.0.0

So we can see the source ip address is modified in the sockaddr. probably because mdns_socket_open_ipv4(). The fact that all other mechanisms using the local IP address for server side work correctly is because the assignment: if (first_ipv4) { service_address_ipv4 = *saddr; first_ipv4 = 0; } Happens before the call that changes the source ip address.

Is it possible to move the print for the local ip address further up, before the call to mdns_socket_open_ipv4(). A similar issue may happen with ipv6. i have not tested that case.

ben-sedg commented 3 years ago

Note: This issue is for the mdns.c file

mjansson commented 3 years ago

I’ll have a look at this

mjansson commented 3 years ago

I've changed the open/setup functions to take a const pointer instead and use a local copy instead to avoid modifying the input. Can you give it a try, it's in the mjansson/setup-const-pointer branch