blob: ddd46309ccd383d075719b286e1fbdcdb0f72f9d [file] [log] [blame]
Vitaly Buka4615e0d2015-10-14 15:35:12 -07001// Copyright 2015 The Weave Authors. All rights reserved.
Vitaly Buka17b0a8a2015-08-31 19:12:35 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Johan Euphrosine3523fdd2015-10-14 20:46:05 -07005#include "examples/provider/avahi_client.h"
Vitaly Buka17b0a8a2015-08-31 19:12:35 -07006
7#include <cstdlib>
8#include <vector>
9
10#include <avahi-common/error.h>
11
12namespace weave {
13namespace examples {
14
15namespace {
16
17void 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
Johan Euphrosine0b7bb9f2015-09-29 01:11:21 -070024std::string GetId() {
25 return "WEAVE" + std::to_string(gethostid());
26}
27
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070028} // namespace
29
Vitaly Bukabeddc602015-09-24 15:28:03 -070030AvahiClient::AvahiClient() {
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070031 thread_pool_.reset(avahi_threaded_poll_new());
32 CHECK(thread_pool_);
33
Johan Euphrosine1b64c432015-09-25 13:55:00 -070034 LOG(INFO) << "connecting to avahi-daemon";
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070035 int ret = 0;
36 client_.reset(avahi_client_new(avahi_threaded_poll_get(thread_pool_.get()),
37 {}, nullptr, this, &ret));
Johan Euphrosine73c8cfc2016-01-20 17:03:48 -080038 CHECK(client_) << "failed to connect to avahi-daemon: "
39 << avahi_strerror(ret);
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070040
41 avahi_threaded_poll_start(thread_pool_.get());
42
43 group_.reset(avahi_entry_group_new(client_.get(), GroupCallback, nullptr));
Johan Euphrosinea1e6c002015-09-18 23:55:10 -070044 CHECK(group_) << avahi_strerror(avahi_client_errno(client_.get()))
45 << ". Check avahi-daemon configuration";
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070046}
47
Vitaly Bukabeddc602015-09-24 15:28:03 -070048AvahiClient::~AvahiClient() {
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070049 if (thread_pool_)
50 avahi_threaded_poll_stop(thread_pool_.get());
51}
52
Vitaly Bukabeddc602015-09-24 15:28:03 -070053void AvahiClient::PublishService(const std::string& service_type,
54 uint16_t port,
55 const std::vector<std::string>& txt) {
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070056 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()) {
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070063 std::vector<const char*> txt_vector_ptr;
Vitaly Buka3dc2f532015-09-08 18:01:32 -070064 for (const auto& i : txt)
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070065 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;
Vitaly Buka3dc2f532015-09-08 18:01:32 -070072 if (prev_port_ == port && prev_type_ == service_type) {
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070073 ret = avahi_entry_group_update_service_txt_strlst(
74 group_.get(), AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, {}, GetId().c_str(),
Vitaly Buka3dc2f532015-09-08 18:01:32 -070075 service_type.c_str(), nullptr, txt_list.get());
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070076 CHECK_GE(ret, 0) << avahi_strerror(ret);
77 } else {
Vitaly Buka978e7122016-03-04 17:32:23 -080078 avahi_entry_group_reset(group_.get());
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070079 prev_port_ = port;
Vitaly Buka3dc2f532015-09-08 18:01:32 -070080 prev_type_ = service_type;
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070081
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070082 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(),
Vitaly Buka3dc2f532015-09-08 18:01:32 -070086 service_type.c_str(), nullptr, nullptr, port, txt_list.get());
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070087 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
Vitaly Bukabeddc602015-09-24 15:28:03 -070093void AvahiClient::StopPublishing(const std::string& service_name) {
Vitaly Buka978e7122016-03-04 17:32:23 -080094 prev_port_ = 0;
95 prev_type_.clear();
96
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070097 CHECK(group_);
98 avahi_entry_group_reset(group_.get());
99}
100
Vitaly Buka17b0a8a2015-08-31 19:12:35 -0700101} // namespace examples
102} // namespace weave