-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathmdns_server.cpp
348 lines (305 loc) · 11.3 KB
/
mdns_server.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
//
// mdns_server.cpp
//
// Copyright (c) 2019 2020 Andrea Bondavalli. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#include <boost/asio.hpp>
#include "config.hpp"
#include "interface.hpp"
#include "log.hpp"
#include "utils.hpp"
#include "mdns_server.hpp"
#ifdef _USE_AVAHI_
struct AvahiLockGuard {
AvahiLockGuard() = delete;
explicit AvahiLockGuard(AvahiThreadedPoll* poll) : poll_(poll) {
if (poll_ != nullptr) {
avahi_threaded_poll_lock(poll_);
}
}
~AvahiLockGuard() {
if (poll_ != nullptr) {
avahi_threaded_poll_unlock(poll_);
}
}
AvahiLockGuard(const AvahiLockGuard&) = delete;
AvahiLockGuard& operator=(const AvahiLockGuard&) = delete;
private:
AvahiThreadedPoll* poll_{nullptr};
};
void MDNSServer::entry_group_callback(AvahiEntryGroup* g,
AvahiEntryGroupState state,
void* userdata) {
MDNSServer& mdns = *(reinterpret_cast<MDNSServer*>(userdata));
/* Called whenever the entry group state changes */
auto it = mdns.groups_.right.find(g);
if (it == mdns.groups_.right.end()) {
BOOST_LOG_TRIVIAL(debug)
<< "mdns_server:: cannot find name associated to group, skipping ...";
return;
}
switch (state) {
case AVAHI_ENTRY_GROUP_ESTABLISHED:
/* The entry group has been established successfully */
BOOST_LOG_TRIVIAL(debug) << "mdns_server:: entry group name ("
<< it->second << ") established";
break;
case AVAHI_ENTRY_GROUP_COLLISION: {
BOOST_LOG_TRIVIAL(warning)
<< "mdns_server:: entry group name (" << it->second << ") collision";
break;
}
case AVAHI_ENTRY_GROUP_FAILURE:
BOOST_LOG_TRIVIAL(error) << "mdns_server:: entry group name "
<< "(" << it->second << ") "
<< "failure"
<< avahi_strerror(avahi_client_errno(
avahi_entry_group_get_client(g)));
/* Some kind of failure happened while we were registering our services */
avahi_threaded_poll_quit(mdns.poll_.get());
break;
case AVAHI_ENTRY_GROUP_UNCOMMITED:
case AVAHI_ENTRY_GROUP_REGISTERING:
break;
}
}
bool MDNSServer::create_services(AvahiClient* client) {
if (!config_->get_mdns_enabled()) {
return false;
}
std::unique_ptr<AvahiEntryGroup, decltype(&avahi_entry_group_free)> group{
avahi_entry_group_new(client, entry_group_callback, this),
&avahi_entry_group_free};
if (group == nullptr) {
BOOST_LOG_TRIVIAL(error)
<< "mdns_server:: create_services() failed avahi_entry_group_new(): "
<< avahi_strerror(avahi_client_errno(client));
return false;
}
/* register ravenna services, without user defined name */
int ret = avahi_entry_group_add_service(
group.get(), this->config_->get_interface_idx(), AVAHI_PROTO_INET, {},
node_id_.c_str(), "_http._tcp", nullptr, nullptr,
this->config_->get_http_port(), nullptr);
if (ret < 0) {
BOOST_LOG_TRIVIAL(error)
<< "mdns_server:: add_service() " << node_id_
<< "failed to add entry _http._tcp" << avahi_strerror(ret);
return false;
}
BOOST_LOG_TRIVIAL(info) << "mdns_server:: adding service _http._tcp for "
<< node_id_;
ret = avahi_entry_group_add_service_subtype(
group.get(), this->config_->get_interface_idx(), AVAHI_PROTO_INET, {},
node_id_.c_str(), "_http._tcp", nullptr, "_ravenna._sub._http._tcp");
if (ret < 0) {
BOOST_LOG_TRIVIAL(error) << "mdns_server:: add_service() " << node_id_
<< "failed to add subtype _ravenna._sub._http._tcp"
<< avahi_strerror(ret);
return false;
}
ret = avahi_entry_group_add_service(
group.get(), this->config_->get_interface_idx(), AVAHI_PROTO_INET, {},
node_id_.c_str(), "_rtsp._tcp", nullptr, nullptr,
this->config_->get_rtsp_port(), nullptr);
if (ret < 0) {
BOOST_LOG_TRIVIAL(error)
<< "mdns_server:: add_service() " << node_id_
<< "failed to add entry _rtsp._tcp" << avahi_strerror(ret);
return false;
}
BOOST_LOG_TRIVIAL(info) << "mdns_server:: adding service _rtsp._tcp for "
<< node_id_;
ret = avahi_entry_group_add_service_subtype(
group.get(), this->config_->get_interface_idx(), AVAHI_PROTO_INET, {},
node_id_.c_str(), "_rtsp._tcp", nullptr, "_ravenna._sub._rtsp._tcp");
if (ret < 0) {
BOOST_LOG_TRIVIAL(error) << "mdns_server:: add_service() " << node_id_
<< "failed to add subtype _ravenna._sub._rtsp._tcp"
<< avahi_strerror(ret);
return false;
}
/* Tell the server to register the service */
ret = avahi_entry_group_commit(group.get());
if (ret < 0) {
BOOST_LOG_TRIVIAL(error)
<< "mdns_server:: create_services() failed to commit entry group "
<< avahi_strerror(ret);
return false;
}
groups_.insert(entry_group_bimap_t::value_type(node_id_, group.release()));
return true;
}
#endif
bool MDNSServer::add_service(const std::string& name, const std::string& sdp) {
if (!running_ || !config_->get_mdns_enabled()) {
return false;
}
#ifdef _USE_AVAHI_
AvahiLockGuard avahi_lock(this->poll_.get());
if (avahi_client_get_state(client_.get()) != AVAHI_CLIENT_S_RUNNING) {
BOOST_LOG_TRIVIAL(error)
<< "mdns_server:: add_service() failed client is not running";
return false;
}
std::unique_ptr<AvahiEntryGroup, decltype(&avahi_entry_group_free)> group{
avahi_entry_group_new(client_.get(), entry_group_callback, this),
&avahi_entry_group_free};
if (group == nullptr) {
BOOST_LOG_TRIVIAL(error)
<< "mdns_server:: add_service() failed avahi_entry_group_new(): "
<< avahi_strerror(avahi_client_errno(client_.get()));
return false;
}
std::stringstream ss;
ss << node_id_ << " " << name;
int ret = avahi_entry_group_add_service(
group.get(), this->config_->get_interface_idx(), AVAHI_PROTO_INET, {},
ss.str().c_str(), "_rtsp._tcp", nullptr, nullptr,
this->config_->get_rtsp_port(), nullptr);
if (ret < 0) {
BOOST_LOG_TRIVIAL(error)
<< "mdns_server:: add_service() " << ss.str()
<< "failed to add service _rtsp._tcp" << avahi_strerror(ret);
return false;
}
ret = avahi_entry_group_add_service_subtype(
group.get(), this->config_->get_interface_idx(), AVAHI_PROTO_INET, {},
ss.str().c_str(), "_rtsp._tcp", nullptr,
"_ravenna_session._sub._rtsp._tcp");
if (ret < 0) {
BOOST_LOG_TRIVIAL(error)
<< "mdns_server:: add_service() " << ss.str()
<< "failed to add subtype _ravenna_session._sub._rtsp._tcp"
<< avahi_strerror(ret);
return false;
}
/* Tell the server to register the service */
ret = avahi_entry_group_commit(group.get());
if (ret < 0) {
BOOST_LOG_TRIVIAL(error)
<< "mdns_server:: add_service() " << ss.str()
<< "failed to commit entry group " << avahi_strerror(ret);
return false;
}
BOOST_LOG_TRIVIAL(info) << "mdns_server:: adding service for " << ss.str();
groups_.insert(entry_group_bimap_t::value_type(name, group.release()));
#endif
return true;
}
bool MDNSServer::remove_service(const std::string& name) {
if (!running_ || !config_->get_mdns_enabled()) {
return false;
}
#ifdef _USE_AVAHI_
AvahiLockGuard avahi_lock(poll_.get());
if (avahi_client_get_state(client_.get()) != AVAHI_CLIENT_S_RUNNING) {
BOOST_LOG_TRIVIAL(error)
<< "mdns_server:: remove_sub_service() failed client is not running";
return false;
}
auto it = groups_.left.find(name);
if (it == groups_.left.end()) {
return false;
}
BOOST_LOG_TRIVIAL(info) << "mdns_server:: removing service _rtsp._tcp for "
<< name;
avahi_entry_group_free(it->second);
groups_.left.erase(name);
#endif
return true;
}
#ifdef _USE_AVAHI_
void MDNSServer::client_callback(AvahiClient* client,
AvahiClientState state,
void* userdata) {
MDNSServer& mdns = *(reinterpret_cast<MDNSServer*>(userdata));
/* Called whenever the client or server state changes */
switch (state) {
case AVAHI_CLIENT_FAILURE:
BOOST_LOG_TRIVIAL(fatal) << "mdns_server:: server connection failure: "
<< avahi_strerror(avahi_client_errno(client));
/* TODO reconnect if disconnected */
avahi_threaded_poll_quit(mdns.poll_.get());
break;
case AVAHI_CLIENT_S_RUNNING:
/* The server has startup successfully and registered its host
* name on the network, so it's time to create our services */
if (!mdns.create_services(client)) {
avahi_threaded_poll_quit(mdns.poll_.get());
}
break;
case AVAHI_CLIENT_S_REGISTERING:
/* The server records are now being established. This
* might be caused by a host name change. We need to wait
* for our own records to register until the host name is
* properly esatblished. */
case AVAHI_CLIENT_S_COLLISION:
/* Let's drop our registered services. When the server is back
* in AVAHI_SERVER_RUNNING state we will register them
* again with the new host name. */
/* TODO */
break;
case AVAHI_CLIENT_CONNECTING:
break;
}
}
#endif
bool MDNSServer::init() {
if (running_) {
return true;
}
#ifdef _USE_AVAHI_
/* allocate poll loop object */
poll_.reset(avahi_threaded_poll_new());
if (poll_ == nullptr) {
BOOST_LOG_TRIVIAL(fatal)
<< "mdns_server:: failed to create threaded poll object";
return false;
}
/* allocate a new client */
int error;
client_.reset(avahi_client_new(avahi_threaded_poll_get(poll_.get()),
AVAHI_CLIENT_NO_FAIL, client_callback, this,
&error));
if (client_ == nullptr) {
BOOST_LOG_TRIVIAL(fatal)
<< "mdns_server:: failed to create client: " << avahi_strerror(error);
return false;
}
(void)avahi_threaded_poll_start(poll_.get());
#endif
session_manager_->add_source_observer(
SessionManager::SourceObserverType::add_source,
std::bind(&MDNSServer::add_service, this, std::placeholders::_2,
std::placeholders::_3));
session_manager_->add_source_observer(
SessionManager::SourceObserverType::remove_source,
std::bind(&MDNSServer::remove_service, this, std::placeholders::_2));
running_ = true;
return true;
}
bool MDNSServer::terminate() {
if (running_) {
running_ = false;
#ifdef _USE_AVAHI_
/* remove base services */
groups_.left.erase(node_id_);
avahi_threaded_poll_stop(poll_.get());
#endif
}
return true;
}