ZeroconfBonjour.cxx 2.93 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

20 21
#include "ZeroconfBonjour.hxx"
#include "ZeroconfInternal.hxx"
22
#include "event/SocketEvent.hxx"
23 24
#include "util/Domain.hxx"
#include "Log.hxx"
25
#include "util/Compiler.h"
26 27 28

#include <dns_sd.h>

29 30
#include <arpa/inet.h>

31
static constexpr Domain bonjour_domain("bonjour");
32

33
class BonjourMonitor final {
34 35
	DNSServiceRef service_ref;

36 37
	SocketEvent socket_event;

38 39
public:
	BonjourMonitor(EventLoop &_loop, DNSServiceRef _service_ref)
40
		:service_ref(_service_ref),
41
		 socket_event(_loop,
42 43
			      BIND_THIS_METHOD(OnSocketReady),
			      SocketDescriptor(DNSServiceRefSockFD(service_ref)))
44 45
	{
		socket_event.ScheduleRead();
46 47 48 49 50 51
	}

	~BonjourMonitor() {
		DNSServiceRefDeallocate(service_ref);
	}

52 53 54 55
	void Cancel() noexcept {
		socket_event.Cancel();
	}

56
protected:
57
	/* virtual methods from class SocketMonitor */
58
	void OnSocketReady([[maybe_unused]] unsigned flags) noexcept {
59 60 61 62 63
		DNSServiceProcessResult(service_ref);
	}
};

static BonjourMonitor *bonjour_monitor;
64

65
static void
Rosen Penev's avatar
Rosen Penev committed
66 67
dnsRegisterCallback([[maybe_unused]] DNSServiceRef sdRef,
		    [[maybe_unused]] DNSServiceFlags flags,
68
		    DNSServiceErrorType errorCode, const char *name,
Rosen Penev's avatar
Rosen Penev committed
69 70 71
		    [[maybe_unused]] const char *regtype,
		    [[maybe_unused]] const char *domain,
		    [[maybe_unused]] void *context)
72 73
{
	if (errorCode != kDNSServiceErr_NoError) {
74 75
		LogError(bonjour_domain,
			 "Failed to register zeroconf service");
76

77
		bonjour_monitor->Cancel();
78
	} else {
79 80 81
		FormatDebug(bonjour_domain,
			    "Registered zeroconf service with name '%s'",
			    name);
82 83 84
	}
}

85
void
86
BonjourInit(EventLoop &loop, const char *service_name, unsigned port)
87
{
88
	DNSServiceRef dnsReference;
89
	DNSServiceErrorType error = DNSServiceRegister(&dnsReference,
90
						       0, 0, service_name,
91
						       SERVICE_TYPE, nullptr, nullptr,
92
						       htons(port), 0,
93
						       nullptr,
94
						       dnsRegisterCallback,
95
						       nullptr);
96 97

	if (error != kDNSServiceErr_NoError) {
98 99
		LogError(bonjour_domain,
			 "Failed to register zeroconf service");
100 101 102

		if (dnsReference) {
			DNSServiceRefDeallocate(dnsReference);
103
			dnsReference = nullptr;
104 105 106 107
		}
		return;
	}

108
	bonjour_monitor = new BonjourMonitor(loop, dnsReference);
109 110
}

111 112
void
BonjourDeinit()
113
{
114
	delete bonjour_monitor;
115
}