Skip to content

Commit deb7ed7

Browse files
committed
mdns: Fix getifaddrs fallback
This primarily fix build & behavior on android which doesn't have getifaddrs
1 parent b394b72 commit deb7ed7

File tree

1 file changed

+41
-7
lines changed

1 file changed

+41
-7
lines changed

src/mdns.c

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,22 +179,56 @@ mdns_list_interfaces(uint32_t** pp_intfs, struct sockaddr_storage **pp_mdns_ips,
179179
}
180180
#else
181181
static size_t
182-
mdns_list_interfaces(uint32_t** pp_intfs, struct sockaddr_storage **pp_mdns_ips, size_t* p_nb_intf, int ai_family)
182+
mdns_list_interfaces(uint32_t** pp_intfs, struct sockaddr_storage **pp_mdns_ips,
183+
size_t* p_nb_intf, struct sockaddr_storage **pp_mcast_addrs,
184+
const struct addrinfo* addrs)
183185
{
184-
uint32_t *intfs;
185186
struct sockaddr_storage *mdns_ips;
186-
*pp_intfs = intfs = malloc(sizeof(*intfs));
187+
uint32_t *intfs;
188+
size_t nb_intfs = 0;
189+
for( const struct addrinfo* addr = addrs; addr != NULL; addr = addr->ai_next ) {
190+
if( addr->ai_family == AF_INET || addr->ai_family == AF_INET6 )
191+
++nb_intfs;
192+
}
193+
*pp_intfs = intfs = malloc( nb_intfs * sizeof(*intfs) );
187194
if (intfs == NULL)
188195
return (MDNS_ERROR);
189-
memset(intfs, 0, sizeof(*intfs));
190-
*pp_mdns_ips = mdns_ips = malloc(sizeof(*mdns_ips));
196+
*pp_mdns_ips = mdns_ips = malloc( nb_intfs * sizeof(*mdns_ips) );
191197
if (mdns_ips == NULL) {
192198
free(intfs);
193199
*pp_intfs = NULL;
194200
return (MDNS_ERROR);
195201
}
196-
memset(mdns_ips, 0, sizeof(*mdns_ips));
197-
*p_nb_intf = 1;
202+
203+
struct sockaddr_storage* mcast_addrs;
204+
mcast_addrs = *pp_mcast_addrs = malloc( nb_intfs * sizeof( *mcast_addrs ) );
205+
if( mcast_addrs == NULL ) {
206+
free(mdns_ips);
207+
free(intfs);
208+
*pp_intfs = NULL;
209+
*pp_mdns_ips = NULL;
210+
return (MDNS_ERROR);
211+
}
212+
for( const struct addrinfo* addr = addrs; addr != NULL; addr = addr->ai_next ) {
213+
if( addr->ai_family == AF_INET || addr->ai_family == AF_INET6 ) {
214+
*intfs = 0;
215+
/* Take a shortcut assuming when we are in this case, we
216+
* can use MCAST_JOIN_GROUP. Otherwise, we need the
217+
* adapter IP address, which without getifaddrs is a pain
218+
* in the lower back to fetch
219+
*/
220+
#ifndef MCAST_JOIN_GROUP
221+
# error We need the adapter address
222+
#endif
223+
mdns_ips->ss_family = addr->ai_family;
224+
memcpy(mcast_addrs, addr->ai_addr, sa_len(addr->ai_addr));
225+
intfs++;
226+
mdns_ips++;
227+
mcast_addrs++;
228+
}
229+
}
230+
231+
*p_nb_intf = nb_intfs;
198232
return (0);
199233
}
200234
#endif // HAVE_GETIFADDRS

0 commit comments

Comments
 (0)