ZeroconfBonjour.cxx 2.84 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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
#include "config.h"
21 22
#include "ZeroconfBonjour.hxx"
#include "ZeroconfInternal.hxx"
23
#include "Listen.hxx"
24
#include "event/SocketMonitor.hxx"
25 26
#include "util/Domain.hxx"
#include "Log.hxx"
27
#include "Compiler.h"
28 29 30

#include <dns_sd.h>

31 32
#include <arpa/inet.h>

33
static constexpr Domain bonjour_domain("bonjour");
34

35 36 37 38 39
class BonjourMonitor final : public SocketMonitor {
	DNSServiceRef service_ref;

public:
	BonjourMonitor(EventLoop &_loop, DNSServiceRef _service_ref)
40 41
		:SocketMonitor(SocketDescriptor(DNSServiceRefSockFD(_service_ref)),
			       _loop),
42 43 44 45 46 47 48 49 50
		 service_ref(_service_ref) {
		ScheduleRead();
	}

	~BonjourMonitor() {
		DNSServiceRefDeallocate(service_ref);
	}

protected:
51 52
	/* virtual methods from class SocketMonitor */
	bool OnSocketReady(gcc_unused unsigned flags) noexcept override {
53
		DNSServiceProcessResult(service_ref);
54
		return false;
55 56 57 58
	}
};

static BonjourMonitor *bonjour_monitor;
59

60
static void
61 62
dnsRegisterCallback(gcc_unused DNSServiceRef sdRef,
		    gcc_unused DNSServiceFlags flags,
63
		    DNSServiceErrorType errorCode, const char *name,
64 65 66
		    gcc_unused const char *regtype,
		    gcc_unused const char *domain,
		    gcc_unused void *context)
67 68
{
	if (errorCode != kDNSServiceErr_NoError) {
69 70
		LogError(bonjour_domain,
			 "Failed to register zeroconf service");
71

72
		bonjour_monitor->Cancel();
73
	} else {
74 75 76
		FormatDebug(bonjour_domain,
			    "Registered zeroconf service with name '%s'",
			    name);
77 78 79
	}
}

80
void
81
BonjourInit(EventLoop &loop, const char *service_name)
82
{
83
	DNSServiceRef dnsReference;
84
	DNSServiceErrorType error = DNSServiceRegister(&dnsReference,
85
						       0, 0, service_name,
86
						       SERVICE_TYPE, nullptr, nullptr,
87
						       htons(listen_port), 0,
88
						       nullptr,
89
						       dnsRegisterCallback,
90
						       nullptr);
91 92

	if (error != kDNSServiceErr_NoError) {
93 94
		LogError(bonjour_domain,
			 "Failed to register zeroconf service");
95 96 97

		if (dnsReference) {
			DNSServiceRefDeallocate(dnsReference);
98
			dnsReference = nullptr;
99 100 101 102
		}
		return;
	}

103
	bonjour_monitor = new BonjourMonitor(loop, dnsReference);
104 105
}

106 107
void
BonjourDeinit()
108
{
109
	delete bonjour_monitor;
110
}