forked from analogdevicesinc/libiio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdns_sd_bonjour.c
167 lines (137 loc) · 4.19 KB
/
dns_sd_bonjour.c
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
/*
* libiio - Library for interfacing industrial I/O (IIO) devices
*
* Copyright (C) 2020 Matej Kenda.
* Author: Matej Kenda <matejken<at>gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* */
#include <errno.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <CFNetwork/CFNetwork.h>
#include "debug.h"
/*
Implementation for DNS SD discovery for macOS using CFNetServices.
*/
struct cfnet_discovery_data {
bool have_v4;
bool have_v6;
char address_v4[80];
char address_v6[80];
int16_t port;
};
static void __cfnet_browser_cb (
CFNetServiceBrowserRef browser,
CFOptionFlags flags,
CFTypeRef domainOrService,
CFStreamError* error,
void* info)
{
CFStreamError anError;
if ((flags & kCFNetServiceFlagIsDomain) != 0) {
ERROR("DNS SD: FATAL. Callback called for domain, not service.\n");
goto stop_browsing;
}
struct cfnet_discovery_data *dd = (struct cfnet_discovery_data *)info;
if (dd == NULL) {
ERROR("DNS SD: Missing info structure. Stop browsing.\n");
goto stop_browsing;
}
if (dd->have_v4 || dd->have_v6) {
// Already resolved one IIO service. Stop.
DEBUG("DNS SD: first service already resolved. Skip.\n");
goto stop_browsing;
}
const CFNetServiceRef netService = (CFNetServiceRef)domainOrService;
if (netService == NULL) {
ERROR("DNS SD: Net service is null.\n");
goto stop_browsing;
}
if (!CFNetServiceResolveWithTimeout(netService, 10.0, &anError)) {
ERROR("DNS SD: Resolve error: %ld.%d\n", anError.domain, anError.error);
goto stop_browsing;
}
dd->port = CFNetServiceGetPortNumber(netService);
CFArrayRef addrArr = CFNetServiceGetAddressing(netService);
if (addrArr == NULL) {
ERROR("DNS SD: No valid addresses for service.\n");
goto stop_browsing;
}
for (CFIndex i = 0; i < CFArrayGetCount(addrArr); i++) {
struct sockaddr_in *sa = (struct sockaddr_in *)
CFDataGetBytePtr(CFArrayGetValueAtIndex(addrArr, i));
switch(sa->sin_family) {
case AF_INET:
if (inet_ntop(sa->sin_family, &sa->sin_addr,
dd->address_v4, sizeof(dd->address_v4))) {
dd->have_v4 = TRUE;
}
case AF_INET6:
if (inet_ntop(sa->sin_family, &sa->sin_addr,
dd->address_v6, sizeof(dd->address_v6))) {
dd->have_v6 = TRUE;
}
}
}
if ((flags & kCFNetServiceFlagMoreComing) == 0) {
DEBUG("DNS SD: No more entries coming.\n");
goto stop_browsing;
}
return;
stop_browsing:
CFNetServiceBrowserStopSearch(browser, &anError);
}
int discover_host(char *addr_str, size_t addr_len, uint16_t *port)
{
int ret = 0;
struct cfnet_discovery_data ddata = { FALSE, FALSE };
CFNetServiceClientContext clientContext = { 0, &ddata, NULL, NULL, NULL };
CFStreamError error;
Boolean result;
CFStringRef type = CFSTR("_iio._tcp.");
CFStringRef domain = CFSTR("");
DEBUG("DNS SD: Resolving hostname.");
CFNetServiceBrowserRef gServiceBrowserRef = CFNetServiceBrowserCreate(
kCFAllocatorDefault, __cfnet_browser_cb, &clientContext);
if (gServiceBrowserRef == NULL) {
ERROR("DNS SD: Failed to create service browser.\n");
ret = ENOMEM;
goto exit;
}
result = CFNetServiceBrowserSearchForServices(
gServiceBrowserRef, domain, type, &error);
if (result == false) {
ERROR("DNS SD: CFNetServiceBrowserSearchForServices failed (domain = %ld, error = %d)\n",
(long)error.domain, error.error);
ret = ENXIO;
goto free_browser;
}
// Resolved address and port. ipv4 has precedence over ipv6.
*port = ddata.port;
if (ddata.have_v4) {
strncpy(addr_str, ddata.address_v4, addr_len);
}
else if (ddata.have_v6) {
strncpy(addr_str, ddata.address_v6, addr_len);
}
else {
ERROR("DNS SD: IIO service not found.\n");
ret = ENXIO;
}
free_browser:
CFRelease(gServiceBrowserRef);
gServiceBrowserRef = NULL;
exit:
return ret;
}