1 // Copyright 2015 The Weave Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "examples/provider/avahi_client.h"
6 
7 #include <cstdlib>
8 #include <vector>
9 
10 #include <avahi-common/error.h>
11 
12 namespace weave {
13 namespace examples {
14 
15 namespace {
16 
GroupCallback(AvahiEntryGroup * g,AvahiEntryGroupState state,AVAHI_GCC_UNUSED void * userdata)17 void GroupCallback(AvahiEntryGroup* g,
18                    AvahiEntryGroupState state,
19                    AVAHI_GCC_UNUSED void* userdata) {
20   CHECK_NE(state, AVAHI_ENTRY_GROUP_COLLISION);
21   CHECK_NE(state, AVAHI_ENTRY_GROUP_FAILURE);
22 }
23 
GetId()24 std::string GetId() {
25   return "WEAVE" + std::to_string(gethostid());
26 }
27 
28 }  // namespace
29 
AvahiClient()30 AvahiClient::AvahiClient() {
31   thread_pool_.reset(avahi_threaded_poll_new());
32   CHECK(thread_pool_);
33 
34   LOG(INFO) << "connecting to avahi-daemon";
35   int ret = 0;
36   client_.reset(avahi_client_new(avahi_threaded_poll_get(thread_pool_.get()),
37                                  {}, nullptr, this, &ret));
38   CHECK(client_) << "failed to connect to avahi-daemon: "
39                  << avahi_strerror(ret);
40 
41   avahi_threaded_poll_start(thread_pool_.get());
42 
43   group_.reset(avahi_entry_group_new(client_.get(), GroupCallback, nullptr));
44   CHECK(group_) << avahi_strerror(avahi_client_errno(client_.get()))
45                 << ". Check avahi-daemon configuration";
46 }
47 
~AvahiClient()48 AvahiClient::~AvahiClient() {
49   if (thread_pool_)
50     avahi_threaded_poll_stop(thread_pool_.get());
51 }
52 
PublishService(const std::string & service_type,uint16_t port,const std::vector<std::string> & txt)53 void AvahiClient::PublishService(const std::string& service_type,
54                                  uint16_t port,
55                                  const std::vector<std::string>& txt) {
56   LOG(INFO) << "Publishing service";
57   CHECK(group_);
58 
59   // Create txt record.
60   std::unique_ptr<AvahiStringList, decltype(&avahi_string_list_free)> txt_list{
61       nullptr, &avahi_string_list_free};
62   if (!txt.empty()) {
63     std::vector<const char*> txt_vector_ptr;
64     for (const auto& i : txt)
65       txt_vector_ptr.push_back(i.c_str());
66     txt_list.reset(avahi_string_list_new_from_array(txt_vector_ptr.data(),
67                                                     txt_vector_ptr.size()));
68     CHECK(txt_list);
69   }
70 
71   int ret = 0;
72   if (prev_port_ == port && prev_type_ == service_type) {
73     ret = avahi_entry_group_update_service_txt_strlst(
74         group_.get(), AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, {}, GetId().c_str(),
75         service_type.c_str(), nullptr, txt_list.get());
76     CHECK_GE(ret, 0) << avahi_strerror(ret);
77   } else {
78     prev_port_ = port;
79     prev_type_ = service_type;
80 
81     avahi_entry_group_reset(group_.get());
82     CHECK(avahi_entry_group_is_empty(group_.get()));
83 
84     ret = avahi_entry_group_add_service_strlst(
85         group_.get(), AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, {}, GetId().c_str(),
86         service_type.c_str(), nullptr, nullptr, port, txt_list.get());
87     CHECK_GE(ret, 0) << avahi_strerror(ret);
88     ret = avahi_entry_group_commit(group_.get());
89     CHECK_GE(ret, 0) << avahi_strerror(ret);
90   }
91 }
92 
StopPublishing(const std::string & service_name)93 void AvahiClient::StopPublishing(const std::string& service_name) {
94   CHECK(group_);
95   avahi_entry_group_reset(group_.get());
96 }
97 
98 }  // namespace examples
99 }  // namespace weave
100