zeroconf/avahi: move generic sources to lib/avahi

This commit is contained in:
Max Kellermann
2022-06-30 09:30:08 +02:00
parent 8783ed1981
commit 4564d251a8
14 changed files with 8 additions and 8 deletions

139
src/lib/avahi/Client.cxx Normal file
View File

@@ -0,0 +1,139 @@
/*
* Copyright 2007-2021 CM4all GmbH
* All rights reserved.
*
* author: Max Kellermann <mk@cm4all.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "Client.hxx"
#include "ConnectionListener.hxx"
#include "ErrorHandler.hxx"
#include "Error.hxx"
#include <avahi-common/error.h>
#include <cassert>
namespace Avahi {
Client::Client(EventLoop &event_loop, ErrorHandler &_error_handler) noexcept
:error_handler(_error_handler),
reconnect_timer(event_loop, BIND_THIS_METHOD(OnReconnectTimer)),
poll(event_loop)
{
reconnect_timer.Schedule({});
}
Client::~Client() noexcept
{
Close();
}
void
Client::Close() noexcept
{
if (client != nullptr) {
for (auto *l : listeners)
l->OnAvahiDisconnect();
avahi_client_free(client);
client = nullptr;
}
reconnect_timer.Cancel();
}
void
Client::ClientCallback(AvahiClient *c, AvahiClientState state) noexcept
{
int error;
switch (state) {
case AVAHI_CLIENT_S_RUNNING:
for (auto *l : listeners)
l->OnAvahiConnect(c);
break;
case AVAHI_CLIENT_FAILURE:
error = avahi_client_errno(c);
if (error == AVAHI_ERR_DISCONNECTED) {
Close();
reconnect_timer.Schedule(std::chrono::seconds(10));
} else {
if (!error_handler.OnAvahiError(std::make_exception_ptr(MakeError(error,
"Avahi connection error"))))
return;
reconnect_timer.Schedule(std::chrono::minutes(1));
}
for (auto *l : listeners)
l->OnAvahiDisconnect();
break;
case AVAHI_CLIENT_S_COLLISION:
case AVAHI_CLIENT_S_REGISTERING:
for (auto *l : listeners)
l->OnAvahiChanged();
break;
case AVAHI_CLIENT_CONNECTING:
break;
}
}
void
Client::ClientCallback(AvahiClient *c, AvahiClientState state,
void *userdata) noexcept
{
auto &client = *(Client *)userdata;
client.ClientCallback(c, state);
}
void
Client::OnReconnectTimer() noexcept
{
int error;
client = avahi_client_new(&poll, AVAHI_CLIENT_NO_FAIL,
ClientCallback, this,
&error);
if (client == nullptr) {
if (!error_handler.OnAvahiError(std::make_exception_ptr(MakeError(error,
"Failed to create Avahi client"))))
return;
reconnect_timer.Schedule(std::chrono::minutes(1));
return;
}
}
} // namespace Avahi

93
src/lib/avahi/Client.hxx Normal file
View File

@@ -0,0 +1,93 @@
/*
* Copyright 2007-2021 CM4all GmbH
* All rights reserved.
*
* author: Max Kellermann <mk@cm4all.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include "Poll.hxx"
#include "event/CoarseTimerEvent.hxx"
#include <avahi-client/client.h>
#include <forward_list>
class EventLoop;
namespace Avahi {
class ErrorHandler;
class ConnectionListener;
class Client final {
ErrorHandler &error_handler;
CoarseTimerEvent reconnect_timer;
Poll poll;
AvahiClient *client = nullptr;
std::forward_list<ConnectionListener *> listeners;
public:
Client(EventLoop &event_loop, ErrorHandler &_error_handler) noexcept;
~Client() noexcept;
Client(const Client &) = delete;
Client &operator=(const Client &) = delete;
EventLoop &GetEventLoop() const noexcept {
return poll.GetEventLoop();
}
void Close() noexcept;
AvahiClient *GetClient() noexcept {
return client;
}
void AddListener(ConnectionListener &listener) noexcept {
listeners.push_front(&listener);
}
void RemoveListener(ConnectionListener &listener) noexcept {
listeners.remove(&listener);
}
private:
void ClientCallback(AvahiClient *c, AvahiClientState state) noexcept;
static void ClientCallback(AvahiClient *c, AvahiClientState state,
void *userdata) noexcept;
void OnReconnectTimer() noexcept;
};
} // namespace Avahi

View File

@@ -0,0 +1,61 @@
/*
* Copyright 2007-2021 CM4all GmbH
* All rights reserved.
*
* author: Max Kellermann <mk@cm4all.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <avahi-client/client.h>
namespace Avahi {
class ConnectionListener {
public:
/**
* The connection to the Avahi daemon has been established.
*
* Note that this may be called again after a collision
* (AVAHI_CLIENT_S_COLLISION) or a host name change
* (AVAHI_CLIENT_S_REGISTERING).
*/
virtual void OnAvahiConnect(AvahiClient *client) noexcept = 0;
virtual void OnAvahiDisconnect() noexcept = 0;
/**
* Something about the Avahi connection has changed, e.g. a
* collision (AVAHI_CLIENT_S_COLLISION) or a host name change
* (AVAHI_CLIENT_S_REGISTERING). Services shall be
* unpublished now, and will be re-published in the following
* OnAvahiConnect() call.
*/
virtual void OnAvahiChanged() noexcept {}
};
} // namespace Avahi

View File

@@ -0,0 +1,49 @@
/*
* Copyright 2007-2021 CM4all GmbH
* All rights reserved.
*
* author: Max Kellermann <mk@cm4all.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <avahi-client/publish.h>
#include <memory>
namespace Avahi {
struct EntryGroupDeleter {
void operator()(AvahiEntryGroup *g) noexcept {
avahi_entry_group_free(g);
}
};
using EntryGroupPtr = std::unique_ptr<AvahiEntryGroup, EntryGroupDeleter>;
} // namespace Avahi

56
src/lib/avahi/Error.cxx Normal file
View File

@@ -0,0 +1,56 @@
/*
* Copyright 2007-2021 CM4all GmbH
* All rights reserved.
*
* author: Max Kellermann <mk@cm4all.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "Error.hxx"
#include <avahi-client/client.h>
#include <avahi-common/error.h>
#include <system_error>
namespace Avahi {
ErrorCategory error_category;
std::string
ErrorCategory::message(int condition) const
{
return avahi_strerror(condition);
}
std::system_error
MakeError(AvahiClient &client, const char *msg) noexcept
{
return MakeError(avahi_client_errno(&client), msg);
}
} // namespace Avahi

61
src/lib/avahi/Error.hxx Normal file
View File

@@ -0,0 +1,61 @@
/*
* Copyright 2007-2021 CM4all GmbH
* All rights reserved.
*
* author: Max Kellermann <mk@cm4all.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <system_error>
struct AvahiClient;
namespace Avahi {
class ErrorCategory final : public std::error_category {
public:
const char *name() const noexcept override {
return "avahi-client";
}
std::string message(int condition) const override;
};
extern ErrorCategory error_category;
inline std::system_error
MakeError(int error, const char *msg) noexcept
{
return std::system_error(error, error_category, msg);
}
std::system_error
MakeError(AvahiClient &client, const char *msg) noexcept;
} // namespace Avahi

View File

@@ -0,0 +1,50 @@
/*
* Copyright 2007-2021 CM4all GmbH
* All rights reserved.
*
* author: Max Kellermann <mk@cm4all.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <exception>
struct AvahiClient;
namespace Avahi {
class ErrorHandler {
public:
/**
* @return true to keep retrying, false if the failed object
* has been disposed
*/
virtual bool OnAvahiError(std::exception_ptr e) noexcept = 0;
};
} // namespace Avahi

174
src/lib/avahi/Poll.cxx Normal file
View File

@@ -0,0 +1,174 @@
/*
* Copyright 2003-2021 The Music Player Daemon Project
* http://www.musicpd.org
*
* 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 2 of the License, or
* (at your option) 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "Poll.hxx"
#include "event/SocketEvent.hxx"
#include "event/FineTimerEvent.hxx"
#include "time/Convert.hxx"
static constexpr unsigned
FromAvahiWatchEvent(AvahiWatchEvent e) noexcept
{
return (e & AVAHI_WATCH_IN ? SocketEvent::READ : 0) |
(e & AVAHI_WATCH_OUT ? SocketEvent::WRITE : 0);
}
static constexpr AvahiWatchEvent
ToAvahiWatchEvent(unsigned e) noexcept
{
return AvahiWatchEvent((e & SocketEvent::READ ? AVAHI_WATCH_IN : 0) |
(e & SocketEvent::WRITE ? AVAHI_WATCH_OUT : 0) |
(e & SocketEvent::ERROR ? AVAHI_WATCH_ERR : 0) |
(e & SocketEvent::HANGUP ? AVAHI_WATCH_HUP : 0));
}
struct AvahiWatch final {
private:
SocketEvent event;
const AvahiWatchCallback callback;
void *const userdata;
AvahiWatchEvent received = AvahiWatchEvent(0);
public:
AvahiWatch(EventLoop &_loop,
SocketDescriptor _fd, AvahiWatchEvent _event,
AvahiWatchCallback _callback, void *_userdata) noexcept
:event(_loop, BIND_THIS_METHOD(OnSocketReady), _fd),
callback(_callback), userdata(_userdata) {
event.Schedule(FromAvahiWatchEvent(_event));
}
static void WatchUpdate(AvahiWatch *w,
AvahiWatchEvent _event) noexcept {
w->event.Schedule(FromAvahiWatchEvent(_event));
}
static AvahiWatchEvent WatchGetEvents(AvahiWatch *w) noexcept {
return w->received;
}
static void WatchFree(AvahiWatch *w) noexcept {
delete w;
}
private:
void OnSocketReady(unsigned events) noexcept {
received = ToAvahiWatchEvent(events);
callback(this, event.GetSocket().Get(), received, userdata);
received = AvahiWatchEvent(0);
}
};
struct AvahiTimeout final {
/* note: cannot use CoarseTimerEvent because libavahi-client
sometimes schedules events immediately, and
CoarseTimerEvent may delay the timer callback for too
long, causing timeouts */
FineTimerEvent event;
const AvahiTimeoutCallback callback;
void *const userdata;
public:
AvahiTimeout(EventLoop &_loop, const struct timeval *tv,
AvahiTimeoutCallback _callback, void *_userdata) noexcept
:event(_loop, BIND_THIS_METHOD(OnTimeout)),
callback(_callback), userdata(_userdata) {
if (tv != nullptr)
Schedule(*tv);
}
static void TimeoutUpdate(AvahiTimeout *t,
const struct timeval *tv) noexcept {
if (tv != nullptr)
t->Schedule(*tv);
else
t->event.Cancel();
}
static void TimeoutFree(AvahiTimeout *t) noexcept {
delete t;
}
private:
[[gnu::pure]]
Event::Duration AbsoluteToDuration(const struct timeval &tv) noexcept {
if (tv.tv_sec == 0)
/* schedule immediately */
return {};
struct timeval now;
if (gettimeofday(&now, nullptr) < 0)
/* shouldn't ever fail, but if it does, do
something reasonable */
return std::chrono::seconds(1);
auto d = ToSteadyClockDuration(tv)
- ToSteadyClockDuration(now);
if (d.count() < 0)
return {};
return d;
}
void Schedule(const struct timeval &tv) noexcept {
event.Schedule(AbsoluteToDuration(tv));
}
void OnTimeout() noexcept {
callback(this, userdata);
}
};
namespace Avahi {
Poll::Poll(EventLoop &_loop) noexcept
:event_loop(_loop)
{
watch_new = WatchNew;
watch_update = AvahiWatch::WatchUpdate;
watch_get_events = AvahiWatch::WatchGetEvents;
watch_free = AvahiWatch::WatchFree;
timeout_new = TimeoutNew;
timeout_update = AvahiTimeout::TimeoutUpdate;
timeout_free = AvahiTimeout::TimeoutFree;
}
AvahiWatch *
Poll::WatchNew(const AvahiPoll *api, int fd, AvahiWatchEvent event,
AvahiWatchCallback callback, void *userdata) noexcept
{
const Poll &poll = *(const Poll *)api;
return new AvahiWatch(poll.event_loop, SocketDescriptor(fd), event,
callback, userdata);
}
AvahiTimeout *
Poll::TimeoutNew(const AvahiPoll *api, const struct timeval *tv,
AvahiTimeoutCallback callback, void *userdata) noexcept
{
const Poll &poll = *(const Poll *)api;
return new AvahiTimeout(poll.event_loop, tv, callback, userdata);
}
} // namespace Avahi

56
src/lib/avahi/Poll.hxx Normal file
View File

@@ -0,0 +1,56 @@
/*
* Copyright 2003-2021 The Music Player Daemon Project
* http://www.musicpd.org
*
* 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 2 of the License, or
* (at your option) 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef MPD_AVAHI_POLL_HXX
#define MPD_AVAHI_POLL_HXX
#include <avahi-common/watch.h>
class EventLoop;
namespace Avahi {
class Poll final : public AvahiPoll {
EventLoop &event_loop;
public:
explicit Poll(EventLoop &_loop) noexcept;
Poll(const Poll &) = delete;
Poll &operator=(const Poll &) = delete;
EventLoop &GetEventLoop() const noexcept {
return event_loop;
}
private:
static AvahiWatch *WatchNew(const AvahiPoll *api, int fd,
AvahiWatchEvent event,
AvahiWatchCallback callback,
void *userdata) noexcept;
static AvahiTimeout *TimeoutNew(const AvahiPoll *api,
const struct timeval *tv,
AvahiTimeoutCallback callback,
void *userdata) noexcept;
};
} // namespace Avahi
#endif

231
src/lib/avahi/Publisher.cxx Normal file
View File

@@ -0,0 +1,231 @@
/*
* Copyright 2007-2021 CM4all GmbH
* All rights reserved.
*
* author: Max Kellermann <mk@cm4all.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "Publisher.hxx"
#include "Service.hxx"
#include "Client.hxx"
#include "Error.hxx"
#include "ErrorHandler.hxx"
#include "net/SocketAddress.hxx"
#include <avahi-common/error.h>
#include <avahi-common/malloc.h>
#include <avahi-common/alternative.h>
#include <cassert>
#include <stdio.h>
#include <unistd.h>
namespace Avahi {
/**
* Append the process id to the given prefix string. This is used as
* a workaround for an avahi-daemon bug/problem: when a service gets
* restarted, and then binds to a new port number (e.g. beng-proxy
* with automatic port assignment), we don't get notified, and so we
* never query the new port. By appending the process id to the
* client name, we ensure that the exiting old process broadcasts
* AVAHI_BROWSER_REMOVE, and hte new process broadcasts
* AVAHI_BROWSER_NEW.
*/
static std::string
MakePidName(const char *prefix)
{
char buffer[256];
snprintf(buffer, sizeof(buffer), "%s[%u]", prefix, (unsigned)getpid());
return buffer;
}
Publisher::Publisher(Client &_client, const char *_name,
std::forward_list<Service> _services,
ErrorHandler &_error_handler) noexcept
:error_handler(_error_handler),
name(MakePidName(_name)),
client(_client), services(std::move(_services))
{
assert(!services.empty());
client.AddListener(*this);
auto *c = client.GetClient();
if (c != nullptr)
RegisterServices(c);
}
Publisher::~Publisher() noexcept
{
client.RemoveListener(*this);
}
inline void
Publisher::GroupCallback(AvahiEntryGroup *g,
AvahiEntryGroupState state) noexcept
{
switch (state) {
case AVAHI_ENTRY_GROUP_ESTABLISHED:
break;
case AVAHI_ENTRY_GROUP_COLLISION:
if (!visible)
/* meanwhile, HideServices() has been called */
return;
/* pick a new name */
{
char *new_name = avahi_alternative_service_name(name.c_str());
name = new_name;
avahi_free(new_name);
}
/* And recreate the services */
RegisterServices(avahi_entry_group_get_client(g));
break;
case AVAHI_ENTRY_GROUP_FAILURE:
error_handler.OnAvahiError(std::make_exception_ptr(MakeError(*avahi_entry_group_get_client(g),
"Avahi service group failure")));
break;
case AVAHI_ENTRY_GROUP_UNCOMMITED:
case AVAHI_ENTRY_GROUP_REGISTERING:
break;
}
}
void
Publisher::GroupCallback(AvahiEntryGroup *g,
AvahiEntryGroupState state,
void *userdata) noexcept
{
auto &publisher = *(Publisher *)userdata;
publisher.GroupCallback(g, state);
}
static void
AddService(AvahiEntryGroup &group, const Service &service,
const char *name)
{
int error = avahi_entry_group_add_service(&group,
service.interface,
service.protocol,
AvahiPublishFlags(0),
name, service.type.c_str(),
nullptr, nullptr,
service.port, nullptr);
if (error != AVAHI_OK)
throw MakeError(error, "Failed to add Avahi service");
}
static void
AddServices(AvahiEntryGroup &group,
const std::forward_list<Service> &services, const char *name)
{
for (const auto &i : services)
AddService(group, i, name);
}
static EntryGroupPtr
MakeEntryGroup(AvahiClient &c,
const std::forward_list<Service> &services, const char *name,
AvahiEntryGroupCallback callback, void *userdata)
{
EntryGroupPtr group(avahi_entry_group_new(&c, callback, userdata));
if (!group)
throw MakeError(c, "Failed to create Avahi service group");
AddServices(*group, services, name);
int error = avahi_entry_group_commit(group.get());
if (error != AVAHI_OK)
throw MakeError(error, "Failed to commit Avahi service group");
return group;
}
void
Publisher::RegisterServices(AvahiClient *c) noexcept
{
assert(visible);
try {
group = MakeEntryGroup(*c, services, name.c_str(),
GroupCallback, this);
} catch (...) {
error_handler.OnAvahiError(std::current_exception());
}
}
void
Publisher::HideServices() noexcept
{
if (!visible)
return;
visible = false;
group.reset();
}
void
Publisher::ShowServices() noexcept
{
if (visible)
return;
visible = true;
auto *c = client.GetClient();
if (c != nullptr)
RegisterServices(c);
}
void
Publisher::OnAvahiConnect(AvahiClient *c) noexcept
{
if (group == nullptr && visible)
RegisterServices(c);
}
void
Publisher::OnAvahiDisconnect() noexcept
{
group.reset();
}
void
Publisher::OnAvahiChanged() noexcept
{
group.reset();
}
} // namespace Avahi

107
src/lib/avahi/Publisher.hxx Normal file
View File

@@ -0,0 +1,107 @@
/*
* Copyright 2007-2021 CM4all GmbH
* All rights reserved.
*
* author: Max Kellermann <mk@cm4all.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include "EntryGroup.hxx"
#include "ConnectionListener.hxx"
#include <avahi-client/publish.h>
#include <string>
#include <forward_list>
class SocketAddress;
namespace Avahi {
struct Service;
class ErrorHandler;
class Client;
/**
* A helper class which manages a list of services to be published via
* Avahi/Zeroconf.
*/
class Publisher final : ConnectionListener {
ErrorHandler &error_handler;
std::string name;
Client &client;
EntryGroupPtr group;
const std::forward_list<Service> services;
/**
* Shall the published services be visible? This is controlled by
* HideServices() and ShowServices().
*/
bool visible = true;
public:
Publisher(Client &client, const char *_name,
std::forward_list<Service> _services,
ErrorHandler &_error_handler) noexcept;
~Publisher() noexcept;
Publisher(const Publisher &) = delete;
Publisher &operator=(const Publisher &) = delete;
/**
* Temporarily hide all registered services. You can undo this
* with ShowServices().
*/
void HideServices() noexcept;
/**
* Undo HideServices().
*/
void ShowServices() noexcept;
private:
void GroupCallback(AvahiEntryGroup *g,
AvahiEntryGroupState state) noexcept;
static void GroupCallback(AvahiEntryGroup *g,
AvahiEntryGroupState state,
void *userdata) noexcept;
void RegisterServices(AvahiClient *c) noexcept;
/* virtual methods from class AvahiConnectionListener */
void OnAvahiConnect(AvahiClient *client) noexcept override;
void OnAvahiDisconnect() noexcept override;
void OnAvahiChanged() noexcept override;
};
} // namespace Avahi

54
src/lib/avahi/Service.hxx Normal file
View File

@@ -0,0 +1,54 @@
/*
* Copyright 2007-2021 CM4all GmbH
* All rights reserved.
*
* author: Max Kellermann <mk@cm4all.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <avahi-common/address.h>
#include <cstdint>
#include <string>
namespace Avahi {
struct Service {
AvahiIfIndex interface = AVAHI_IF_UNSPEC;
AvahiProtocol protocol = AVAHI_PROTO_UNSPEC;
std::string type;
uint16_t port;
Service(AvahiIfIndex _interface, AvahiProtocol _protocol,
const char *_type, uint16_t _port) noexcept
:interface(_interface), protocol(_protocol),
type(_type), port(_port) {}
};
} // namespace Avahi