-
Notifications
You must be signed in to change notification settings - Fork 23
/
SoapyMDNSEndpointApple.cpp
269 lines (238 loc) · 9.04 KB
/
SoapyMDNSEndpointApple.cpp
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// Copyright (c) 2018-2018 Josh Blum
// SPDX-License-Identifier: BSL-1.0
#include "SoapyMDNSEndpoint.hpp"
#include "SoapyRemoteDefs.hpp"
#include <SoapySDR/Logger.hpp>
#include "SoapyURLUtils.hpp"
#include <dns_sd.h>
#include <cerrno>
#include <cstdlib> //atoi
/***********************************************************************
* Storage for mdns services
**********************************************************************/
struct SoapyMDNSEndpointData
{
SoapyMDNSEndpointData(void);
~SoapyMDNSEndpointData(void);
DNSServiceRef sdRef;
};
SoapyMDNSEndpointData::SoapyMDNSEndpointData(void):
sdRef(nullptr)
{
return;
}
SoapyMDNSEndpointData::~SoapyMDNSEndpointData(void)
{
if (sdRef != nullptr) DNSServiceRefDeallocate(sdRef);
}
/***********************************************************************
* SoapyMDNSEndpoint interface hooks
**********************************************************************/
SoapyMDNSEndpoint::SoapyMDNSEndpoint(void):
_impl(new SoapyMDNSEndpointData())
{
return;
}
SoapyMDNSEndpoint::~SoapyMDNSEndpoint(void)
{
delete _impl;
}
void SoapyMDNSEndpoint::printInfo(void)
{
static const int mDNSResponderVersion(_DNS_SD_H+0);
SoapySDR::logf(SOAPY_SDR_INFO, "mDNSResponder version: v%d.%d.%d",
mDNSResponderVersion/10000, //major
(mDNSResponderVersion/100)%100, //minor
mDNSResponderVersion%100); //build
uint32_t version;
uint32_t size = sizeof(version);
auto ret = DNSServiceGetProperty(kDNSServiceProperty_DaemonVersion, &version, &size);
if (ret == kDNSServiceErr_NoError) SoapySDR::logf(
SOAPY_SDR_INFO, "Bonjour daemon version: v%d.%d.%d",
version/10000, (version/100)%100, version%100);
}
bool SoapyMDNSEndpoint::status(void)
{
return true;
}
static void registerServiceCallback(
DNSServiceRef sdRef,
DNSServiceFlags flags,
DNSServiceErrorType errorCode,
const char *name,
const char *regtype,
const char *domain,
void *context)
{
char fullname[kDNSServiceMaxDomainName];
DNSServiceConstructFullName(fullname, name, regtype, domain);
SoapySDR::logf(SOAPY_SDR_DEBUG, "SoapyMDNS resolving %s...", fullname);
if (errorCode != kDNSServiceErr_NoError) return SoapySDR::logf(
SOAPY_SDR_ERROR, "SoapyMDNS registerServiceCallback(%s) error: %d",
fullname, errorCode);
SoapySDR::logf(SOAPY_SDR_INFO, "DNSServiceRegister(%s)", fullname);
}
void SoapyMDNSEndpoint::registerService(const std::string &uuid, const std::string &service, const int ipVer)
{
//text record with uuid
TXTRecordRef txtRecord;
TXTRecordCreate(&txtRecord, 0, nullptr);
auto ret = TXTRecordSetValue(&txtRecord, "uuid", uuid.size(), uuid.data());
if (ret != kDNSServiceErr_NoError) return SoapySDR::logf(
SOAPY_SDR_ERROR, "TXTRecordSetValue() failed %d", ret);
ret = DNSServiceRegister(
&_impl->sdRef,
DNSServiceFlags(0),
kDNSServiceInterfaceIndexAny,
nullptr, //use hostname
SOAPY_REMOTE_DNSSD_TYPE,
nullptr, //domain automatic
nullptr, //host automatic
htons(atoi(service.c_str())),
TXTRecordGetLength(&txtRecord),
TXTRecordGetBytesPtr(&txtRecord),
®isterServiceCallback,
_impl);
TXTRecordDeallocate(&txtRecord);
if (ret != kDNSServiceErr_NoError) SoapySDR::logf(
SOAPY_SDR_ERROR, "DNSServiceRegister() failed %d", ret);
else DNSServiceProcessResult(_impl->sdRef);
}
/***********************************************************************
* Implement host discovery
**********************************************************************/
struct SoapyMDNSBrowseResult
{
int ipVerRequest;
std::map<std::string, std::map<int, std::string>> serverURLs;
};
static void getAddrInfoCallback(
DNSServiceRef sdRef,
DNSServiceFlags flags,
uint32_t interfaceIndex,
DNSServiceErrorType errorCode,
const char *hostname,
const struct sockaddr *address,
uint32_t ttl,
void *context)
{
auto addrStr = (std::string *)context;
if (errorCode != kDNSServiceErr_NoError) return SoapySDR::logf(
SOAPY_SDR_ERROR, "SoapyMDNS getAddrInfoCallback(%s) error: %d", hostname, errorCode);
*addrStr = SoapyURL(address).getNode();
}
static void resolveReplyCallback(
DNSServiceRef sdRef,
DNSServiceFlags flags,
uint32_t interfaceIndex,
DNSServiceErrorType errorCode,
const char *fullname,
const char *hosttarget,
uint16_t port, /* In network byte order */
uint16_t txtLen,
const unsigned char *txtRecord,
void *context)
{
auto result = (SoapyMDNSBrowseResult *)context;
if (errorCode != kDNSServiceErr_NoError) return SoapySDR::logf(
SOAPY_SDR_ERROR, "SoapyMDNS resolveReplyCallback(%s) error: %d", hosttarget, errorCode);
//extract uuid
std::string uuid;
uint8_t valueLen(0);
auto uuidPtr = TXTRecordGetValuePtr(txtLen, txtRecord, "uuid", &valueLen);
if (uuidPtr != nullptr) uuid = std::string((const char *)uuidPtr, valueLen);
else return SoapySDR::logf(SOAPY_SDR_ERROR, "SoapyMDNS resolve missing uuid record for %s", hosttarget);
//address lookup
static const int IP_VERS[] = {SOAPY_REMOTE_IPVER_INET, SOAPY_REMOTE_IPVER_INET6};
static const DNSServiceProtocol PROTS[] = {kDNSServiceProtocol_IPv4, kDNSServiceProtocol_IPv6};
const auto service = std::to_string(ntohs(port));
for (size_t i = 0; i < 2; i++)
{
const auto ipVer = IP_VERS[i];
const auto protocol = PROTS[i];
if ((ipVer & result->ipVerRequest) == 0) continue;
std::string addrStr;
if (DNSServiceGetAddrInfo(
&sdRef,
DNSServiceFlags(0),
interfaceIndex,
protocol,
hosttarget,
&getAddrInfoCallback,
&addrStr) != kDNSServiceErr_NoError) continue;
if (DNSServiceProcessResult(sdRef) != kDNSServiceErr_NoError) continue;
if (addrStr.empty()) continue;
const auto serverURL = SoapyURL("tcp", addrStr, service).toString();
SoapySDR::logf(SOAPY_SDR_DEBUG, "SoapyMDNS discovered %s [%s] IPv%d", serverURL.c_str(), uuid.c_str(), ipVer);
result->serverURLs[uuid][ipVer] = serverURL;
}
}
static void browseReplyCallback(
DNSServiceRef sdRef,
DNSServiceFlags flags,
uint32_t interfaceIndex,
DNSServiceErrorType errorCode,
const char *serviceName,
const char *regtype,
const char *replyDomain,
void *context)
{
char fullname[kDNSServiceMaxDomainName];
DNSServiceConstructFullName(fullname, serviceName, regtype, replyDomain);
SoapySDR::logf(SOAPY_SDR_DEBUG, "SoapyMDNS resolving %s...", fullname);
if (errorCode != kDNSServiceErr_NoError) return SoapySDR::logf(
SOAPY_SDR_ERROR, "SoapyMDNS browseReplyCallback(#%d, %s) error: %d",
interfaceIndex, fullname, errorCode);
auto ret = DNSServiceResolve(
&sdRef,
DNSServiceFlags(0),
interfaceIndex,
serviceName,
regtype,
replyDomain,
&resolveReplyCallback,
context);
if (ret != kDNSServiceErr_NoError) SoapySDR::logf(
SOAPY_SDR_ERROR, "DNSServiceResolve(#%d, %s) failed %d",
interfaceIndex, fullname, ret);
else DNSServiceProcessResult(sdRef);
}
static bool waitDNSServiceRef(DNSServiceRef sdRef, const long timeoutUs)
{
//get the underlying Unix domain socket for timeout
auto sock = DNSServiceRefSockFD(sdRef);
if (sock == -1)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "DNSServiceRefSockFD() failed %d", sock);
return false;
}
struct timeval tv;
tv.tv_sec = timeoutUs / 1000000;
tv.tv_usec = timeoutUs % 1000000;
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(sock, &readfds);
const int ret = ::select(sock+1, &readfds, NULL, NULL, &tv);
if (ret == 0) SoapySDR::logf(SOAPY_SDR_DEBUG, "select(DNSServiceRefSockFD()) timeout");
if (ret == -1) SoapySDR::logf(SOAPY_SDR_ERROR, "select(DNSServiceRefSockFD()) failed %d", errno);
return ret == 1;
}
std::map<std::string, std::map<int, std::string>> SoapyMDNSEndpoint::getServerURLs(const int ipVer, const long timeoutUs)
{
SoapyMDNSBrowseResult result;
result.ipVerRequest = ipVer;
DNSServiceRef sdRef(nullptr);
auto ret = DNSServiceBrowse(
&sdRef,
DNSServiceFlags(0),
kDNSServiceInterfaceIndexAny,
SOAPY_REMOTE_DNSSD_TYPE,
nullptr, //domain automatic
&browseReplyCallback,
&result);
if (ret != kDNSServiceErr_NoError) SoapySDR::logf(
SOAPY_SDR_ERROR, "DNSServiceBrowse() failed %d", ret);
else if (waitDNSServiceRef(sdRef, timeoutUs)) DNSServiceProcessResult(sdRef);
DNSServiceRefDeallocate(sdRef);
return result.serverURLs;
}