ZeroconfBonjour.cxx 2.83 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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 "Listen.hxx"
23
#include "event/SocketMonitor.hxx"
24 25
#include "util/Domain.hxx"
#include "Log.hxx"
26
#include "util/Compiler.h"
27 28 29

#include <dns_sd.h>

30 31
#include <arpa/inet.h>

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

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

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

	~BonjourMonitor() {
		DNSServiceRefDeallocate(service_ref);
	}

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

static BonjourMonitor *bonjour_monitor;
58

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

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

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

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

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

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

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