Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Zeroconf.cpp « murmur « src - github.com/mumble-voip/mumble.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5b74435eddd86dd94ee4d7658a857c1e979c255f (plain)
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
// Copyright 2020-2021 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.

#include "Zeroconf.h"

#define GET_SYMBOL(symbol) (symbol = reinterpret_cast< decltype(symbol) >(GetProcAddress(handle, #symbol)))

Zeroconf::Zeroconf() : m_ok(false) {
#ifdef Q_OS_WIN
	auto handle = GetModuleHandle(L"dnsapi.dll");
	if (handle) {
		GET_SYMBOL(DnsServiceConstructInstance);
		GET_SYMBOL(DnsServiceFreeInstance);
		GET_SYMBOL(DnsServiceRegister);
		GET_SYMBOL(DnsServiceDeRegister);
		GET_SYMBOL(DnsServiceRegisterCancel);

		if (DnsServiceConstructInstance && DnsServiceFreeInstance && DnsServiceRegister && DnsServiceDeRegister
			&& DnsServiceRegisterCancel) {
			m_ok = true;
			return;
		}
	}

	qWarning("Zeroconf: Native mDNS/DNS-SD API not available, falling back to third-party API");

	handle = LoadLibrary(L"dnssd.dll");
	if (!handle) {
		qWarning("Zeroconf: Failed to load dnssd.dll, assuming third-party API is not available");
		return;
	}
	FreeLibrary(handle);
#endif
	resetHelper();

	m_ok = true;
}

Zeroconf::~Zeroconf() {
	if (!m_helper) {
		unregisterService();
	}
}

void Zeroconf::resetHelper() {
	m_helper.reset(new BonjourServiceRegister(this));
	connect(m_helper.get(), &BonjourServiceRegister::error, this, &Zeroconf::helperError);
}

bool Zeroconf::registerService(const BonjourRecord &record, const uint16_t port) {
	if (!m_ok) {
		return false;
	}

	unregisterService();

	if (m_helper) {
		m_helper->registerService(record, port);
		return true;
	}
#ifdef Q_OS_WIN
	DWORD size = 0;
	GetComputerNameEx(ComputerNameDnsHostname, nullptr, &size);
	std::vector< wchar_t > hostname(size);
	if (!GetComputerNameEx(ComputerNameDnsHostname, &hostname[0], &size)) {
		qWarning("Zeroconf: GetComputerNameEx() failed with error %u!", GetLastError());
		return false;
	}

	const auto domain            = record.replyDomain.isEmpty() ? L".local" : L"." + record.replyDomain.toStdWString();
	const auto qualifiedHostname = &hostname[0] + domain;

	auto service = record.serviceName.isEmpty() ? &hostname[0] : record.serviceName.toStdWString();
	service += L".";
	service += record.registeredType.toStdWString();
	service += domain;

	auto instance = DnsServiceConstructInstance(service.c_str(), qualifiedHostname.c_str(), nullptr, nullptr, port, 0,
												0, 0, nullptr, nullptr);
	if (!instance) {
		qWarning("Zeroconf: DnsServiceConstructInstance() returned nullptr!");
		return false;
	}

	m_request.reset(new DNS_SERVICE_REGISTER_REQUEST{});
	m_request->Version                     = DNS_QUERY_REQUEST_VERSION1;
	m_request->pServiceInstance            = instance;
	m_request->pRegisterCompletionCallback = callbackRegisterComplete;
	m_request->pQueryContext               = this;

	m_cancel.reset(new DNS_SERVICE_CANCEL{});
	const auto ret = DnsServiceRegister(m_request.get(), m_cancel.get());
	DnsServiceFreeInstance(instance);

	if (ret == DNS_REQUEST_PENDING) {
		return true;
	}

	qWarning("Zeroconf: DnsServiceRegister() failed with error %u!", ret);
	m_request.reset();
	m_cancel.reset();
#endif
	return false;
}

bool Zeroconf::unregisterService() {
	if (!m_ok) {
		return false;
	}

	if (m_helper) {
		resetHelper();
		return true;
	}
#ifdef Q_OS_WIN
	if (m_cancel) {
		const auto ret = DnsServiceRegisterCancel(m_cancel.get());
		if (ret == ERROR_SUCCESS || ret == ERROR_CANCELLED) {
			return true;
		}

		m_cancel.reset();
		qWarning("Zeroconf: DnsServiceRegisterCancel() failed with error %u!", ret);
	} else if (m_request) {
		const auto ret = DnsServiceDeRegister(m_request.get(), nullptr);
		if (ret == DNS_REQUEST_PENDING) {
			return true;
		}

		qWarning("Zeroconf: DnsServiceDeRegister() failed with error %u!", ret);
	}
#endif
	return false;
}

void Zeroconf::helperError(const DNSServiceErrorType error) {
	qWarning("Zeroconf: Third-party API reports error %d, service registration probably failed", error);
}
#ifdef Q_OS_WIN
void WINAPI Zeroconf::callbackRegisterComplete(const DWORD status, void *context, DNS_SERVICE_INSTANCE *instance) {
	auto zeroconf = static_cast< Zeroconf * >(context);

	if (instance) {
		zeroconf->DnsServiceFreeInstance(instance);
	}

	if (status == ERROR_CANCELLED) {
		return;
	}

	zeroconf->m_cancel.reset();

	if (status != ERROR_SUCCESS) {
		qWarning("Zeroconf: DnsServiceRegister() reports status code %u, service registration probably failed", status);
	}
}
#endif

#undef GET_SYMBOL