Skip to content

Commit

Permalink
Added IPv6 scope
Browse files Browse the repository at this point in the history
  • Loading branch information
breese committed Sep 25, 2018
1 parent 951eec4 commit ca7e985
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
29 changes: 18 additions & 11 deletions example/dnssd/zmonitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ class my_monitor
public:
my_monitor(boost::asio::io_service& io,
trial::aware::factory& factory)
: socket(factory.make_monitor(io))
: contact(""),
socket(factory.make_monitor(io))
{}

void async_monitor(trial::aware::contact& contact)
void async_monitor(const trial::aware::contact& input)
{
contact = input;
socket->async_monitor(contact,
std::bind(&my_monitor::process_monitor,
this,
Expand All @@ -41,16 +43,18 @@ class my_monitor
{
if (contact.empty())
{
std::cout << "Removed:" << std::endl;
std::cout << " type = " << contact.type() << std::endl;
std::cout << " name = " << contact.name() << std::endl;
std::cout << "- " << contact.index() << std::endl;
std::cout << " type = " << contact.type() << std::endl;
std::cout << " name = " << contact.name() << std::endl;
}
else
{
std::cout << "Added:" << std::endl;
std::cout << " type = " << contact.type() << std::endl;
std::cout << " name = " << contact.name() << std::endl;
std::cout << " endpoint = " << contact.address().to_string() << ":" << contact.port() << std::endl;
std::cout << "+ " << contact.index() << std::endl;
std::cout << " type = " << contact.type() << std::endl;
std::cout << " name = " << contact.name() << std::endl;
std::cout << " domain = " << contact.domain() << std::endl;
std::cout << " address = " << contact.address().to_string() << std::endl;
std::cout << " port = " << contact.port() << std::endl;
auto properties = contact.properties();
for (const auto& property : properties)
{
Expand All @@ -71,6 +75,7 @@ class my_monitor
}

private:
trial::aware::contact contact;
std::unique_ptr<trial::aware::monitor_socket> socket;
};

Expand All @@ -83,10 +88,12 @@ int main(int argc, char *argv[])
}
boost::asio::io_service io;
trial::aware::dnssd::factory factory;
my_monitor monitor(io, factory);
my_monitor monitor4(io, factory);
my_monitor monitor6(io, factory);

trial::aware::contact contact(argv[1]);
monitor.async_monitor(contact);
monitor4.async_monitor(contact);
monitor6.async_monitor(contact.address(boost::asio::ip::address_v6()));
io.run();

return 0;
Expand Down
18 changes: 13 additions & 5 deletions src/dnssd/avahi/browser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ namespace aware
namespace avahi
{

boost::asio::ip::address to_address(const AvahiAddress& addr)
boost::asio::ip::address to_address(const AvahiAddress& addr,
int scope)
{
using namespace boost::asio;
switch (addr.proto)
Expand All @@ -34,7 +35,8 @@ boost::asio::ip::address to_address(const AvahiAddress& addr)
return ip::address_v4(reinterpret_cast<const ip::address_v4::bytes_type&>(addr.data.ipv4.address));

case AVAHI_PROTO_INET6:
return ip::address_v6(reinterpret_cast<const ip::address_v6::bytes_type&>(addr.data.ipv6.address));
return ip::address_v6(reinterpret_cast<const ip::address_v6::bytes_type&>(addr.data.ipv6.address),
scope);

default:
assert(false);
Expand All @@ -46,12 +48,12 @@ struct browser::wrapper
{
static void resolver_callback(
AvahiServiceResolver *resolver,
AvahiIfIndex,
AvahiIfIndex index,
AvahiProtocol,
AvahiResolverEvent event,
const char *name,
const char *full_type,
const char * /* domain */,
const char *domain,
const char * /* host_name */,
const AvahiAddress *address,
unsigned short port,
Expand All @@ -78,7 +80,13 @@ struct browser::wrapper
}
// Notify requester
auto type = avahi::type_decode(full_type);
auto contact = aware::contact(type).name(name).address(avahi::to_address(*address)).port(port).properties(properties);
auto contact = aware::contact(type)
.index(index)
.name(name)
.domain(domain)
.address(avahi::to_address(*address, index))
.port(port)
.properties(properties);
self->listener.on_appear(contact);
}
else
Expand Down
11 changes: 6 additions & 5 deletions src/dnssd/mdns/resolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ boost::asio::ip::address to_address(const sockaddr& addr)
return ip::address_v4(reinterpret_cast<const ip::address_v4::bytes_type&>(reinterpret_cast<const sockaddr_in&>(addr).sin_addr));

case AF_INET6:
return ip::address_v6(reinterpret_cast<const ip::address_v6::bytes_type&>(reinterpret_cast<const sockaddr_in6&>(addr).sin6_addr));
{
auto& addr6 = reinterpret_cast<const sockaddr_in6&>(addr);
return ip::address_v6(reinterpret_cast<const ip::address_v6::bytes_type&>(addr6.sin6_addr),
addr6.sin6_scope_id);
}

default:
assert(false);
Expand Down Expand Up @@ -169,10 +173,7 @@ resolver::resolver(mdns::handle& connection,
void resolver::on_resolved(const char *host)
{
const ::DNSServiceFlags flags = kDNSServiceFlagsShareConnection;
const ::DNSServiceProtocol protocol =
(contact.address().is_v6())
? kDNSServiceProtocol_IPv6
: kDNSServiceProtocol_IPv4;
const ::DNSServiceProtocol protocol = kDNSServiceProtocol_IPv4 | kDNSServiceProtocol_IPv6;

auto ref = connection.get<DNSServiceRef>();
::DNSServiceErrorType error = ::DNSServiceGetAddrInfo(&ref,
Expand Down

0 comments on commit ca7e985

Please sign in to comment.