mirror of
https://github.com/badaix/snapcast.git
synced 2025-06-02 10:51:45 +02:00
zeroconf works with IPv6
This commit is contained in:
parent
e9ff66e78b
commit
b34f8a6e00
5 changed files with 57 additions and 38 deletions
|
@ -15,6 +15,9 @@ endif()
|
|||
|
||||
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
|
||||
set(MACOSX TRUE)
|
||||
elseif (${CMAKE_SYSTEM_NAME} MATCHES "Android")
|
||||
set (ANDROID TRUE)
|
||||
message(FATAL_ERROR "Android not yet supported")
|
||||
endif()
|
||||
|
||||
# Configure paths
|
||||
|
@ -87,6 +90,8 @@ if(MACOSX)
|
|||
endif (BONJOUR_FOUND)
|
||||
|
||||
add_definitions(-DFREEBSD -DHAS_DAEMON)
|
||||
elseif(ANDROID)
|
||||
add_definitions("-DNO_CPP11_STRING")
|
||||
else()
|
||||
pkg_search_module(ALSA REQUIRED alsa)
|
||||
if (ALSA_FOUND)
|
||||
|
@ -97,6 +102,8 @@ else()
|
|||
if (AVAHI_FOUND)
|
||||
add_definitions(-DHAS_AVAHI)
|
||||
endif (AVAHI_FOUND)
|
||||
|
||||
add_definitions(-DHAS_DAEMON)
|
||||
endif()
|
||||
|
||||
pkg_search_module(FLAC flac)
|
||||
|
|
|
@ -90,11 +90,13 @@ void BrowseAvahi::resolve_callback(
|
|||
LOG(INFO) << "Service '" << name << "' of type '" << type << "' in domain '" << domain << "':\n";
|
||||
|
||||
avahi_address_snprint(a, sizeof(a), address);
|
||||
browseAvahi->result_.host_ = host_name;
|
||||
browseAvahi->result_.ip_ = a;
|
||||
browseAvahi->result_.port_ = port;
|
||||
browseAvahi->result_.proto_ = protocol;
|
||||
browseAvahi->result_.valid_ = true;
|
||||
browseAvahi->result_.host = host_name;
|
||||
browseAvahi->result_.ip = a;
|
||||
browseAvahi->result_.port = port;
|
||||
// protocol seems to be unreliable (0 for IPv4 and for IPv6)
|
||||
browseAvahi->result_.ip_version = (browseAvahi->result_.ip.find(":") == std::string::npos)?(IPVersion::IPv4):(IPVersion::IPv6);
|
||||
browseAvahi->result_.valid = true;
|
||||
browseAvahi->result_.iface_idx = interface;
|
||||
|
||||
t = avahi_string_list_to_string(txt);
|
||||
LOG(INFO) << "\t" << host_name << ":" << port << " (" << a << ")\n";
|
||||
|
@ -196,12 +198,12 @@ bool BrowseAvahi::browse(const std::string& serviceName, mDNSResult& result, int
|
|||
if (!(sb_ = avahi_service_browser_new(client_, AVAHI_IF_UNSPEC, AVAHI_PROTO_INET, serviceName.c_str(), NULL, (AvahiLookupFlags)0, browse_callback, this)))
|
||||
throw SnapException("BrowseAvahi - Failed to create service browser: " + std::string(avahi_strerror(avahi_client_errno(client_))));
|
||||
|
||||
result_.valid_ = false;
|
||||
result_.valid = false;
|
||||
while (timeout > 0)
|
||||
{
|
||||
avahi_simple_poll_iterate(simple_poll, 100);
|
||||
timeout -= 100;
|
||||
if (result_.valid_)
|
||||
if (result_.valid)
|
||||
{
|
||||
result = result_;
|
||||
cleanUp();
|
||||
|
|
|
@ -226,33 +226,33 @@ bool BrowseBonjour::browse(const string& serviceName, mDNSResult& result, int ti
|
|||
{
|
||||
resultCollection[i].port_ = resolve.port;
|
||||
CHECKED(DNSServiceGetAddrInfo(service.get(), kDNSServiceFlagsLongLivedQuery, 0, kDNSServiceProtocol_IPv4, resolve.fullName.c_str(),
|
||||
[](DNSServiceRef service,
|
||||
DNSServiceFlags flags,
|
||||
uint32_t interfaceIndex,
|
||||
DNSServiceErrorType errorCode,
|
||||
const char* hostname,
|
||||
const sockaddr* address,
|
||||
uint32_t ttl,
|
||||
void* context)
|
||||
{
|
||||
auto result = static_cast<mDNSResult*>(context);
|
||||
[](DNSServiceRef service,
|
||||
DNSServiceFlags flags,
|
||||
uint32_t interfaceIndex,
|
||||
DNSServiceErrorType errorCode,
|
||||
const char* hostname,
|
||||
const sockaddr* address,
|
||||
uint32_t ttl,
|
||||
void* context)
|
||||
{
|
||||
auto result = static_cast<mDNSResult*>(context);
|
||||
|
||||
result->host_ = string(hostname);
|
||||
result->proto_ = address->sa_family;
|
||||
result->host = string(hostname);
|
||||
result->ip_version = (address->sa_family == AF_INET)?(IPVersion::IPv4):(IPVersion::IPv6);
|
||||
result->iface_idx = static_cast<int>(interfaceIndex);
|
||||
|
||||
char hostIP[NI_MAXHOST];
|
||||
char hostService[NI_MAXSERV];
|
||||
if (getnameinfo(address, sizeof(*address),
|
||||
hostIP, sizeof(hostIP),
|
||||
hostService, sizeof(hostService),
|
||||
NI_NUMERICHOST|NI_NUMERICSERV) == 0)
|
||||
result->ip_ = string(hostIP);
|
||||
else
|
||||
return;
|
||||
result->valid_ = true;
|
||||
}, &resultCollection[i++]));
|
||||
char hostIP[NI_MAXHOST];
|
||||
char hostService[NI_MAXSERV];
|
||||
if (getnameinfo(address, sizeof(*address),
|
||||
hostIP, sizeof(hostIP),
|
||||
hostService, sizeof(hostService),
|
||||
NI_NUMERICHOST|NI_NUMERICSERV) == 0)
|
||||
result->ip_ = string(hostIP);
|
||||
else
|
||||
return;
|
||||
result->valid_ = true;
|
||||
}, &resultCollection[i++]));
|
||||
}
|
||||
|
||||
runService(service);
|
||||
}
|
||||
|
||||
|
|
|
@ -3,13 +3,21 @@
|
|||
|
||||
#include <string>
|
||||
|
||||
enum IPVersion
|
||||
{
|
||||
IPv4 = 0,
|
||||
IPv6 = 1
|
||||
};
|
||||
|
||||
|
||||
struct mDNSResult
|
||||
{
|
||||
int proto_;
|
||||
std::string ip_;
|
||||
std::string host_;
|
||||
uint16_t port_;
|
||||
bool valid_;
|
||||
IPVersion ip_version;
|
||||
int iface_idx;
|
||||
std::string ip;
|
||||
std::string host;
|
||||
uint16_t port;
|
||||
bool valid;
|
||||
};
|
||||
|
||||
class BrowsemDNS
|
||||
|
|
|
@ -218,8 +218,10 @@ int main (int argc, char **argv)
|
|||
{
|
||||
if (browser.browse("_snapcast._tcp", avahiResult, 5000))
|
||||
{
|
||||
host = avahiResult.ip_;
|
||||
port = avahiResult.port_;
|
||||
host = avahiResult.ip;
|
||||
port = avahiResult.port;
|
||||
if (avahiResult.ip_version == IPVersion::IPv6)
|
||||
host += "%" + cpt::to_string(avahiResult.iface_idx);
|
||||
LOG(INFO) << "Found server " << host << ":" << port << "\n";
|
||||
break;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue