react-native-zeroconf 0.17.4 → 1.0.0-rc.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. package/NOTICE +2 -6
  2. package/Package.swift +19 -3
  3. package/README.md +1 -3
  4. package/android/CMakeLists.txt +64 -0
  5. package/android/build.gradle +1 -17
  6. package/android/generated/java/com/balthazargronon/RCTZeroconf/NativeZeroconfSpec.java +96 -0
  7. package/android/generated/jni/CMakeLists.txt +28 -0
  8. package/android/generated/jni/RNZeroconfSpec-generated.cpp +77 -0
  9. package/android/generated/jni/RNZeroconfSpec.h +31 -0
  10. package/android/generated/jni/react/renderer/components/RNZeroconfSpec/RNZeroconfSpecJSI.h +798 -0
  11. package/android/src/main/java/com/balthazargronon/RCTZeroconf/{ZeroconfModule.java → NsdHost.java} +97 -140
  12. package/android/src/main/java/com/balthazargronon/RCTZeroconf/NsdPayload.java +87 -0
  13. package/android/src/main/java/com/balthazargronon/RCTZeroconf/NsdPromise.java +40 -0
  14. package/android/src/main/java/com/balthazargronon/RCTZeroconf/Zeroconf.java +4 -6
  15. package/android/src/main/java/com/balthazargronon/RCTZeroconf/ZeroconfNativeSupport.java +60 -0
  16. package/android/src/main/java/com/balthazargronon/RCTZeroconf/ZeroconfOptions.java +15 -26
  17. package/android/src/main/java/com/balthazargronon/RCTZeroconf/ZeroconfReactPackage.java +5 -15
  18. package/android/src/main/java/com/balthazargronon/RCTZeroconf/nsd/NsdServiceImpl.java +90 -96
  19. package/android/src/main/jni/mdnsresponder/mDNSShared/dnssd_clientshim.c +31 -6
  20. package/cpp/Backend.cpp +16 -0
  21. package/cpp/Backend.h +116 -0
  22. package/cpp/ZeroconfModule.cpp +280 -0
  23. package/cpp/ZeroconfModule.h +144 -0
  24. package/cpp/android/NsdBackend.cpp +462 -0
  25. package/cpp/android/NsdBackend.h +61 -0
  26. package/cpp/android/ZeroconfAndroidModule.cpp +49 -0
  27. package/cpp/android/ZeroconfAndroidModule.h +15 -0
  28. package/cpp/apple/DispatchExecutor.cpp +66 -0
  29. package/cpp/apple/DispatchExecutor.h +37 -0
  30. package/cpp/apple/LocalNetworkAccess.h +19 -0
  31. package/cpp/apple/LocalNetworkAccess.mm +87 -0
  32. package/cpp/dnssd/DnssdBackend.cpp +976 -0
  33. package/cpp/dnssd/DnssdBackend.h +114 -0
  34. package/cpp/dnssd/Executor.h +28 -0
  35. package/cpp/embedded/EmbeddedExecutor.cpp +124 -0
  36. package/cpp/embedded/EmbeddedExecutor.h +57 -0
  37. package/ios/ZeroconfOnLoad.mm +34 -0
  38. package/ios/generated/ReactCodegen/RNZeroconfSpec/RNZeroconfSpec-generated.mm +140 -0
  39. package/ios/generated/ReactCodegen/RNZeroconfSpec/RNZeroconfSpec.h +189 -0
  40. package/ios/generated/ReactCodegen/RNZeroconfSpecJSI.h +798 -0
  41. package/lib/module/NativeZeroconf.js +15 -0
  42. package/lib/module/NativeZeroconf.js.map +1 -0
  43. package/lib/module/bridge.js +124 -0
  44. package/lib/module/bridge.js.map +1 -0
  45. package/{dist → lib/module}/index.js +149 -149
  46. package/lib/module/index.js.map +1 -0
  47. package/lib/module/package.json +1 -0
  48. package/lib/module/types.js +24 -0
  49. package/lib/module/types.js.map +1 -0
  50. package/lib/typescript/package.json +1 -0
  51. package/lib/typescript/src/NativeZeroconf.d.ts +75 -0
  52. package/lib/typescript/src/NativeZeroconf.d.ts.map +1 -0
  53. package/lib/typescript/src/bridge.d.ts +22 -0
  54. package/lib/typescript/src/bridge.d.ts.map +1 -0
  55. package/lib/typescript/src/index.d.ts +111 -0
  56. package/lib/typescript/src/index.d.ts.map +1 -0
  57. package/lib/typescript/src/types.d.ts +175 -0
  58. package/lib/typescript/src/types.d.ts.map +1 -0
  59. package/package.json +64 -29
  60. package/react-native-zeroconf.podspec +2 -1
  61. package/react-native.config.js +17 -0
  62. package/src/NativeZeroconf.ts +120 -0
  63. package/src/bridge.ts +153 -0
  64. package/src/index.ts +607 -0
  65. package/{index.d.ts → src/types.ts} +5 -99
  66. package/windows/RNZeroconf/RNZeroconf.vcxproj +17 -2
  67. package/windows/RNZeroconf/RNZeroconf.vcxproj.filters +0 -6
  68. package/windows/RNZeroconf/ReactPackageProvider.cpp +23 -20
  69. package/windows/RNZeroconf/WindowsBackend.cpp +156 -0
  70. package/windows/RNZeroconf/WindowsBackend.h +13 -0
  71. package/windows/RNZeroconf/ZeroconfCore.cpp +2 -2
  72. package/windows/RNZeroconf/ZeroconfCore.h +2 -2
  73. package/windows/RNZeroconf/ZeroconfWindowsModule.cpp +22 -0
  74. package/windows/RNZeroconf/ZeroconfWindowsModule.h +14 -0
  75. package/android/src/main/java/com/balthazargronon/RCTZeroconf/ZeroConfImplFactory.java +0 -56
  76. package/android/src/main/java/com/balthazargronon/RCTZeroconf/dnssd/DnssdImpl.java +0 -654
  77. package/android/src/main/java/com/github/druk/dnssd/BaseListener.java +0 -33
  78. package/android/src/main/java/com/github/druk/dnssd/BrowseListener.java +0 -67
  79. package/android/src/main/java/com/github/druk/dnssd/DNSRecord.java +0 -47
  80. package/android/src/main/java/com/github/druk/dnssd/DNSSD.java +0 -681
  81. package/android/src/main/java/com/github/druk/dnssd/DNSSDEmbedded.java +0 -198
  82. package/android/src/main/java/com/github/druk/dnssd/DNSSDException.java +0 -66
  83. package/android/src/main/java/com/github/druk/dnssd/DNSSDRecordRegistrar.java +0 -55
  84. package/android/src/main/java/com/github/druk/dnssd/DNSSDRegistration.java +0 -54
  85. package/android/src/main/java/com/github/druk/dnssd/DNSSDService.java +0 -35
  86. package/android/src/main/java/com/github/druk/dnssd/DomainListener.java +0 -57
  87. package/android/src/main/java/com/github/druk/dnssd/InternalBrowseListener.java +0 -76
  88. package/android/src/main/java/com/github/druk/dnssd/InternalDNSSD.java +0 -870
  89. package/android/src/main/java/com/github/druk/dnssd/InternalDNSSDRecordRegistrar.java +0 -30
  90. package/android/src/main/java/com/github/druk/dnssd/InternalDNSSDRegistration.java +0 -34
  91. package/android/src/main/java/com/github/druk/dnssd/InternalDNSSDService.java +0 -29
  92. package/android/src/main/java/com/github/druk/dnssd/InternalDomainListener.java +0 -62
  93. package/android/src/main/java/com/github/druk/dnssd/InternalQueryListener.java +0 -61
  94. package/android/src/main/java/com/github/druk/dnssd/InternalRegisterListener.java +0 -51
  95. package/android/src/main/java/com/github/druk/dnssd/InternalResolveListener.java +0 -58
  96. package/android/src/main/java/com/github/druk/dnssd/NSClass.java +0 -16
  97. package/android/src/main/java/com/github/druk/dnssd/NSType.java +0 -58
  98. package/android/src/main/java/com/github/druk/dnssd/QueryListener.java +0 -54
  99. package/android/src/main/java/com/github/druk/dnssd/RegisterListener.java +0 -46
  100. package/android/src/main/java/com/github/druk/dnssd/RegisterRecordListener.java +0 -35
  101. package/android/src/main/java/com/github/druk/dnssd/ResolveListener.java +0 -55
  102. package/android/src/main/java/com/github/druk/dnssd/TXTRecord.java +0 -293
  103. package/android/src/main/jni/Android.mk +0 -73
  104. package/android/src/main/jni/Application.mk +0 -3
  105. package/android/src/main/jni/DNSSD.java.h +0 -290
  106. package/android/src/main/jni/JNISupport.c +0 -1221
  107. package/ios/RNZeroconf/RNNetServiceSerializer.h +0 -31
  108. package/ios/RNZeroconf/RNNetServiceSerializer.m +0 -84
  109. package/ios/RNZeroconf/RNZeroconf.h +0 -17
  110. package/ios/RNZeroconf/RNZeroconf.m +0 -1025
  111. package/ios/RNZeroconf.xcodeproj/project.pbxproj +0 -261
  112. package/ios/RNZeroconf.xcodeproj/project.xcworkspace/contents.xcworkspacedata +0 -7
  113. package/src/index.js +0 -563
  114. package/windows/RNZeroconf/RNZeroconf.cpp +0 -228
  115. package/windows/RNZeroconf/RNZeroconf.h +0 -70
@@ -1,681 +0,0 @@
1
- /*
2
- * Copyright (C) 2016 Andriy Druk
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * http://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- */
16
-
17
- package com.github.druk.dnssd;
18
-
19
- import android.content.Context;
20
- import android.net.wifi.WifiManager;
21
- import android.os.Handler;
22
- import android.os.Looper;
23
- import android.text.TextUtils;
24
- import android.util.Log;
25
-
26
- import java.nio.charset.Charset;
27
- import java.util.HashMap;
28
- import java.util.Map;
29
-
30
- public abstract class DNSSD implements InternalDNSSDService.DnssdServiceListener {
31
-
32
- public static final int DNSSD_DEFAULT_TIMEOUT = 60 * 1000; // 60 sec
33
-
34
- /** Flag indicates to a {@link BrowseListener} that another result is
35
- queued. Applications should not update their UI to display browse
36
- results if the MORE_COMING flag is set; they will be called at least once
37
- more with the flag clear.
38
- */
39
- public static final int MORE_COMING = 1 << 0;
40
-
41
- /** If flag is set in a {@link DomainListener} callback, indicates that the result is the default domain. */
42
- public static final int DEFAULT = 1 << 2;
43
-
44
- /** If flag is set, a name conflict will trigger an exception when registering non-shared records.<P>
45
- A name must be explicitly specified when registering a service if this bit is set
46
- (i.e. the default name may not not be used).
47
- */
48
- public static final int NO_AUTO_RENAME = 1 << 3;
49
-
50
- /** If flag is set, allow multiple records with this name on the network (e.g. PTR records)
51
- when registering individual records on a {@link DNSSDRegistration}.
52
- */
53
- public static final int SHARED = 1 << 4;
54
-
55
- /** If flag is set, records with this name must be unique on the network (e.g. SRV records). */
56
- public static final int UNIQUE = 1 << 5;
57
-
58
- /** Set flag when calling enumerateDomains() to restrict results to domains recommended for browsing. */
59
- public static final int BROWSE_DOMAINS = 1 << 6;
60
- /** Set flag when calling enumerateDomains() to restrict results to domains recommended for registration. */
61
- public static final int REGISTRATION_DOMAINS = 1 << 7;
62
-
63
- /** Maximum length, in bytes, of a domain name represented as an escaped C-String. */
64
- public static final int MAX_DOMAIN_NAME = 1009;
65
-
66
- /** Pass for ifIndex to specify all available interfaces. */
67
- public static final int ALL_INTERFACES = 0;
68
-
69
- /** Pass for ifIndex to specify the localhost interface. */
70
- public static final int LOCALHOST_ONLY = -1;
71
-
72
- private static final Charset UTF_8 = Charset.forName("UTF-8");
73
- private static final String MULTICAST_LOCK_NAME = "com.github.druk.dnssd.DNSSD";
74
-
75
- private final Handler handler;
76
- private final Context context;
77
-
78
- // Lock for multicast packages
79
- private volatile WifiManager.MulticastLock multicastLock = null;
80
-
81
- /** Timeout for resolve and query records operations. Default value: {@value #DNSSD_DEFAULT_TIMEOUT} */
82
- private final int serviceTimeout;
83
-
84
- DNSSD(Context context, String lib) {
85
- this(context, lib, Looper.getMainLooper());
86
- }
87
-
88
- DNSSD(Context context, String lib, Looper looper) {
89
- this.context = context.getApplicationContext();
90
- InternalDNSSD.init(lib);
91
- this.handler = new Handler(looper);
92
- this.serviceTimeout = DNSSD_DEFAULT_TIMEOUT;
93
- }
94
-
95
- DNSSD(Context context, String lib, Handler handler) {
96
- this.context = context.getApplicationContext();
97
- InternalDNSSD.init(lib);
98
- this.handler = handler;
99
- this.serviceTimeout = DNSSD_DEFAULT_TIMEOUT;
100
- }
101
-
102
- DNSSD(Context context, String lib, Handler handler, int serviceTimeout) {
103
- this.context = context.getApplicationContext();
104
- InternalDNSSD.init(lib);
105
- this.handler = handler;
106
- this.serviceTimeout = serviceTimeout;
107
- }
108
-
109
- // The embedded responder can answer before the call that starts an operation has returned, from its
110
- // own thread or from inside the call. The operation is assigned under the monitor of its array while
111
- // that call runs, and the callbacks posted to the main thread take the monitor first, so they wait for
112
- // the call to return. If it threw, there is no operation and the callback is dropped.
113
-
114
- private interface Start<T> {
115
- T start() throws DNSSDException;
116
- }
117
-
118
- private interface OperationCallback<T> {
119
- void run(T operation);
120
- }
121
-
122
- private static <T> void start(T[] operation, Start<T> start) throws DNSSDException {
123
- synchronized (operation) {
124
- operation[0] = start.start();
125
- }
126
- }
127
-
128
- private <T> void postWith(final T[] operation, final OperationCallback<T> callback) {
129
- handler.post(() -> {
130
- T started;
131
- synchronized (operation) {
132
- started = operation[0];
133
- }
134
- if (started != null) {
135
- callback.run(started);
136
- }
137
- });
138
- }
139
-
140
- /** Browse for instances of a service.<P>
141
-
142
- Note: browsing consumes network bandwidth. Call {@link InternalDNSSDService#stop} when you have finished browsing.<P>
143
-
144
- @param flags
145
- Currently ignored, reserved for future use.
146
- <P>
147
- @param ifIndex
148
- If non-zero, specifies the interface on which to browse for services
149
- (the index for a given interface is determined via the if_nametoindex()
150
- family of calls.) Most applications will pass 0 to browse on all available
151
- interfaces. Pass -1 to only browse for services provided on the local host.
152
- <P>
153
- @param regType
154
- The registration type being browsed for followed by the protocol, separated by a
155
- dot (e.g. "_ftp._tcp"). The transport protocol must be "_tcp" or "_udp".
156
- <P>
157
- @param domain
158
- If non-null, specifies the domain on which to browse for services.
159
- Most applications will not specify a domain, instead browsing on the
160
- default domain(s).
161
- <P>
162
- @param listener
163
- This object will get called when instances of the service are discovered (or disappear).
164
- <P>
165
- @return A {@link InternalDNSSDService} that represents the active browse operation.
166
-
167
- @throws SecurityException If a security manager is present and denies <tt>RuntimePermission("getDNSSDInstance")</tt>.
168
- @see RuntimePermission
169
- */
170
- public DNSSDService browse(int flags, int ifIndex, String regType, String domain, final BrowseListener listener) throws DNSSDException {
171
- onServiceStarting();
172
- final InternalDNSSDService[] services = new InternalDNSSDService[1];
173
- start(services, () -> new InternalDNSSDService(this, InternalDNSSD.browse(flags, ifIndex, regType, domain, new InternalBrowseListener() {
174
- @Override
175
- public void serviceFound(final DNSSDService browser, final int flags, final int ifIndex, final byte[] serviceName, final byte[] regType, final byte[] domain) {
176
- final String serviceNameStr = new String(serviceName, UTF_8);
177
- final String regTypeStr = new String(regType, UTF_8);
178
- final String domainStr = new String(domain, UTF_8);
179
- postWith(services, operation -> listener.serviceFound(operation, flags, ifIndex, serviceNameStr, regTypeStr, domainStr));
180
- }
181
-
182
- @Override
183
- public void serviceLost(DNSSDService browser, final int flags, final int ifIndex, byte[] serviceName, byte[] regType, byte[] domain) {
184
- final String serviceNameStr = new String(serviceName, UTF_8);
185
- final String regTypeStr = new String(regType, UTF_8);
186
- final String domainStr = new String(domain, UTF_8);
187
- postWith(services, operation -> listener.serviceLost(operation, flags, ifIndex, serviceNameStr, regTypeStr, domainStr));
188
- }
189
-
190
- @Override
191
- public void operationFailed(final DNSSDService service, final int errorCode) {
192
- postWith(services, operation -> listener.operationFailed(operation, errorCode));
193
- }
194
- })));
195
- return services[0];
196
-
197
- }
198
-
199
- /** Browse for instances of a service. Use default flags, ifIndex and domain.<P>
200
-
201
- @param regType
202
- The registration type being browsed for followed by the protocol, separated by a
203
- dot (e.g. "_ftp._tcp"). The transport protocol must be "_tcp" or "_udp".
204
- <P>
205
- @param listener
206
- This object will get called when instances of the service are discovered (or disappear).
207
- <P>
208
- @return A {@link DNSSDService} that represents the active browse operation.
209
-
210
- @throws SecurityException If a security manager is present and denies <tt>RuntimePermission("getDNSSDInstance")</tt>.
211
- @see RuntimePermission
212
- */
213
- public DNSSDService browse(String regType, BrowseListener listener) throws DNSSDException {
214
- return browse(0, 0, regType, "", listener);
215
- }
216
-
217
- /** Resolve a service name discovered via browse() to a target host name, port number, and txt record.<P>
218
-
219
- Note: Applications should NOT use resolve() solely for txt record monitoring - use
220
- queryRecord() instead, as it is more efficient for this task.<P>
221
-
222
- Note: When the desired results have been returned, the client MUST terminate the resolve by
223
- calling {@link DNSSDService#stop}.<P>
224
-
225
- Note: resolve() behaves correctly for typical services that have a single SRV record and
226
- a single TXT record (the TXT record may be empty.) To resolve non-standard services with
227
- multiple SRV or TXT records, use queryRecord().<P>
228
-
229
- @param flags
230
- Currently ignored, reserved for future use.
231
- <P>
232
- @param ifIndex
233
- The interface on which to resolve the service. The client should
234
- pass the interface on which the serviceName was discovered (i.e.
235
- the ifIndex passed to the serviceFound() callback)
236
- or 0 to resolve the named service on all available interfaces.
237
- <P>
238
- @param serviceName
239
- The servicename to be resolved.
240
- <P>
241
- @param regType
242
- The registration type being resolved followed by the protocol, separated by a
243
- dot (e.g. "_ftp._tcp"). The transport protocol must be "_tcp" or "_udp".
244
- <P>
245
- @param domain
246
- The domain on which the service is registered, i.e. the domain passed
247
- to the serviceFound() callback.
248
- <P>
249
- @param listener
250
- This object will get called when the service is resolved.
251
- <P>
252
- @return A {@link DNSSDService} that represents the active resolve operation.
253
-
254
- @throws SecurityException If a security manager is present and denies <tt>RuntimePermission("getDNSSDInstance")</tt>.
255
- @see RuntimePermission
256
- */
257
- public DNSSDService resolve(int flags, int ifIndex, String serviceName, String regType, String domain, final ResolveListener listener) throws DNSSDException {
258
- onServiceStarting();
259
- final DNSSDService[] services = new DNSSDService[1];
260
-
261
- final Runnable timeoutRunnable = () -> services[0].stop();
262
-
263
- start(services, () -> new InternalDNSSDService(this, InternalDNSSD.resolve(flags, ifIndex, serviceName, regType, domain, new InternalResolveListener() {
264
- @Override
265
- public void serviceResolved(final DNSSDService resolver, final int flags, final int ifIndex, byte[] fullName, byte[] hostName, final int port, TXTRecord txtRecord) {
266
- final String fullNameStr = new String(fullName, UTF_8);
267
- final String hostNameStr = new String(hostName, UTF_8);
268
- final Map<String, String> record = parseTXTRecords(txtRecord);
269
- handler.removeCallbacks(timeoutRunnable);
270
- postWith(services, operation -> {
271
- listener.serviceResolved(operation, flags, ifIndex, fullNameStr, hostNameStr, port, record);
272
- operation.stop();
273
- });
274
- }
275
-
276
- @Override
277
- public void operationFailed(final DNSSDService service, final int errorCode) {
278
- handler.removeCallbacks(timeoutRunnable);
279
- postWith(services, operation -> {
280
- listener.operationFailed(operation, errorCode);
281
- operation.stop();
282
- });
283
- }
284
- })));
285
-
286
- handler.postDelayed(timeoutRunnable, serviceTimeout);
287
- return services[0];
288
- }
289
-
290
- /** Register a service, to be discovered via browse() and resolve() calls.<P>
291
- @param flags
292
- Possible values are: NO_AUTO_RENAME.
293
- <P>
294
- @param ifIndex
295
- If non-zero, specifies the interface on which to register the service
296
- (the index for a given interface is determined via the if_nametoindex()
297
- family of calls.) Most applications will pass 0 to register on all
298
- available interfaces. Pass -1 to register a service only on the local
299
- machine (service will not be visible to remote hosts).
300
- <P>
301
- @param serviceName
302
- If non-null, specifies the service name to be registered.
303
- Applications need not specify a name, in which case the
304
- computer name is used (this name is communicated to the client via
305
- the serviceRegistered() callback).
306
- <P>
307
- @param regType
308
- The registration type being registered followed by the protocol, separated by a
309
- dot (e.g. "_ftp._tcp"). The transport protocol must be "_tcp" or "_udp".
310
- <P>
311
- @param domain
312
- If non-null, specifies the domain on which to advertise the service.
313
- Most applications will not specify a domain, instead automatically
314
- registering in the default domain(s).
315
- <P>
316
- @param host
317
- If non-null, specifies the SRV target host name. Most applications
318
- will not specify a host, instead automatically using the machine's
319
- default host name(s). Note that specifying a non-null host does NOT
320
- create an address record for that host - the application is responsible
321
- for ensuring that the appropriate address record exists, or creating it
322
- via {@link DNSSDRegistration#addRecord}.
323
- <P>
324
- @param port
325
- The port on which the service accepts connections. Pass 0 for a
326
- "placeholder" service (i.e. a service that will not be discovered by
327
- browsing, but will cause a name conflict if another client tries to
328
- register that same name.) Most clients will not use placeholder services.
329
- <P>
330
- @param txtRecord
331
- The txt record rdata. May be null. Note that a non-null txtRecord
332
- MUST be a properly formatted DNS TXT record, i.e. &lt;length byte&gt; &lt;data&gt;
333
- &lt;length byte&gt; &lt;data&gt; ...
334
- <P>
335
- @param listener
336
- This object will get called when the service is registered.
337
- <P>
338
- @return A {@link DNSSDRegistration} that controls the active registration.
339
-
340
- @throws SecurityException If a security manager is present and denies <tt>RuntimePermission("getDNSSDInstance")</tt>.
341
- @see RuntimePermission
342
- */
343
- public DNSSDRegistration register(int flags, int ifIndex, String serviceName, String regType, String domain, String host, int port, TXTRecord txtRecord,
344
- final RegisterListener listener) throws DNSSDException {
345
- onServiceStarting();
346
- final DNSSDRegistration[] services = new DNSSDRegistration[1];
347
- start(services, () -> new InternalDNSSDRegistration(this, InternalDNSSD.register(flags, ifIndex, serviceName, regType, domain, host, port, txtRecord,
348
- new InternalRegisterListener() {
349
-
350
- @Override
351
- public void serviceRegistered(DNSSDRegistration registration, final int flags, final byte[] serviceName, byte[] regType, final byte[] domain) {
352
- final String serviceNameStr = new String(serviceName, UTF_8);
353
- final String regTypeStr = new String(regType, UTF_8);
354
- final String domainStr = new String(domain, UTF_8);
355
- postWith(services, operation -> listener.serviceRegistered(operation, flags, serviceNameStr, regTypeStr, domainStr));
356
- }
357
-
358
- @Override
359
- public void operationFailed(DNSSDService service, final int errorCode) {
360
- postWith(services, operation -> listener.operationFailed(operation, errorCode));
361
- }
362
- })));
363
- return services[0];
364
- }
365
-
366
- /** Register a service, to be discovered via browse() and resolve() calls. Use default flags, ifIndex, domain, host and txtRecord.<P>
367
- @param serviceName
368
- If non-null, specifies the service name to be registered.
369
- Applications need not specify a name, in which case the
370
- computer name is used (this name is communicated to the client via
371
- the serviceRegistered() callback).
372
- <P>
373
- @param regType
374
- The registration type being registered followed by the protocol, separated by a
375
- dot (e.g. "_ftp._tcp"). The transport protocol must be "_tcp" or "_udp".
376
- <P>
377
- @param port
378
- The port on which the service accepts connections. Pass 0 for a
379
- "placeholder" service (i.e. a service that will not be discovered by
380
- browsing, but will cause a name conflict if another client tries to
381
- register that same name.) Most clients will not use placeholder services.
382
- <P>
383
- @param listener
384
- This object will get called when the service is registered.
385
- <P>
386
- @return A {@link DNSSDRegistration} that controls the active registration.
387
-
388
- @throws SecurityException If a security manager is present and denies <tt>RuntimePermission("getDNSSDInstance")</tt>.
389
- @see RuntimePermission
390
- */
391
- public DNSSDService register(String serviceName, String regType, int port, RegisterListener listener) throws DNSSDException {
392
- return register(0, 0, serviceName, regType, null, null, port, null, listener);
393
- }
394
-
395
- /** Create a {@link DNSSDRecordRegistrar} allowing efficient registration of
396
- multiple individual records.<P>
397
- <P>
398
- @return A {@link DNSSDRecordRegistrar} that can be used to register records.
399
-
400
- @throws SecurityException If a security manager is present and denies <tt>RuntimePermission("getDNSSDInstance")</tt>.
401
- @see RuntimePermission
402
- */
403
- public DNSSDRecordRegistrar createRecordRegistrar(RegisterRecordListener listener) throws DNSSDException {
404
- onServiceStarting();
405
- return new InternalDNSSDRecordRegistrar(this, InternalDNSSD.createRecordRegistrar(listener));
406
- }
407
-
408
- /** Query for an arbitrary DNS record.<P>
409
- @param flags
410
- Possible values are: MORE_COMING.
411
- <P>
412
- @param ifIndex
413
- If non-zero, specifies the interface on which to issue the query
414
- (the index for a given interface is determined via the if_nametoindex()
415
- family of calls.) Passing 0 causes the name to be queried for on all
416
- interfaces. Passing -1 causes the name to be queried for only on the
417
- local host.
418
- <P>
419
- @param serviceName
420
- The full domain name of the resource record to be queried for.
421
- <P>
422
- @param rrtype
423
- The numerical type of the resource record to be queried for (e.g. PTR, SRV, etc)
424
- as defined in nameser.h.
425
- <P>
426
- @param rrclass
427
- The class of the resource record, as defined in nameser.h
428
- (usually 1 for the Internet class).
429
- <P>
430
- @param listener
431
- This object will get called when the query completes.
432
- <P>
433
- @return A {@link DNSSDService} that controls the active query.
434
-
435
- @throws SecurityException If a security manager is present and denies <tt>RuntimePermission("getDNSSDInstance")</tt>.
436
- @see RuntimePermission
437
- */
438
- public DNSSDService queryRecord(int flags, int ifIndex, final String serviceName, int rrtype, int rrclass, final QueryListener listener) throws DNSSDException {
439
- return this.queryRecord(flags, ifIndex, serviceName, rrtype, rrclass, false, listener);
440
- }
441
-
442
- /** Query for an arbitrary DNS record.<P>
443
- @param flags
444
- Possible values are: MORE_COMING.
445
- <P>
446
- @param ifIndex
447
- If non-zero, specifies the interface on which to issue the query
448
- (the index for a given interface is determined via the if_nametoindex()
449
- family of calls.) Passing 0 causes the name to be queried for on all
450
- interfaces. Passing -1 causes the name to be queried for only on the
451
- local host.
452
- <P>
453
- @param serviceName
454
- The full domain name of the resource record to be queried for.
455
- <P>
456
- @param rrtype
457
- The numerical type of the resource record to be queried for (e.g. PTR, SRV, etc)
458
- as defined in nameser.h.
459
- <P>
460
- @param rrclass
461
- The class of the resource record, as defined in nameser.h
462
- (usually 1 for the Internet class).
463
- <P>
464
- @param autoStop
465
- Stop querying after the first response or timeout
466
- <P>
467
- @param listener
468
- This object will get called when the query completes.
469
- <P>
470
- @return A {@link DNSSDService} that controls the active query.
471
-
472
- @throws SecurityException If a security manager is present and denies <tt>RuntimePermission("getDNSSDInstance")</tt>.
473
- @see RuntimePermission
474
- */
475
- public DNSSDService queryRecord(int flags, int ifIndex, final String serviceName, int rrtype, int rrclass, boolean autoStop, final QueryListener listener) throws DNSSDException {
476
- onServiceStarting();
477
- final DNSSDService[] services = new DNSSDService[1];
478
-
479
- final Runnable timeoutRunnable = () -> services[0].stop();
480
-
481
- start(services, () -> new InternalDNSSDService(this, InternalDNSSD.queryRecord(flags, ifIndex, serviceName, rrtype, rrclass, new InternalQueryListener() {
482
- @Override
483
- public void queryAnswered(DNSSDService query, final int flags, final int ifIndex, byte[] fullName, final int rrtype, final int rrclass, byte[] rdata, final int ttl) {
484
- final String fullNameStr = new String(fullName, UTF_8);
485
- handler.removeCallbacks(timeoutRunnable);
486
- postWith(services, operation -> {
487
- listener.queryAnswered(operation, flags, ifIndex, fullNameStr, rrtype, rrclass, rdata, ttl);
488
- if (autoStop) {
489
- operation.stop();
490
- }
491
- });
492
- }
493
-
494
- @Override
495
- public void operationFailed(DNSSDService service, final int errorCode) {
496
- handler.removeCallbacks(timeoutRunnable);
497
- postWith(services, operation -> {
498
- listener.operationFailed(operation, errorCode);
499
- operation.stop();
500
- });
501
- }
502
- })));
503
-
504
- if (autoStop) {
505
- handler.postDelayed(timeoutRunnable, serviceTimeout);
506
- }
507
-
508
- return services[0];
509
- }
510
-
511
- /** Asynchronously enumerate domains available for browsing and registration.<P>
512
-
513
- Currently, the only domain returned is "local.", but other domains will be returned in future.<P>
514
-
515
- The enumeration MUST be cancelled by calling {@link DNSSDService#stop} when no more domains
516
- are to be found.<P>
517
- @param flags
518
- Possible values are: BROWSE_DOMAINS, REGISTRATION_DOMAINS.
519
- <P>
520
- @param ifIndex
521
- If non-zero, specifies the interface on which to look for domains.
522
- (the index for a given interface is determined via the if_nametoindex()
523
- family of calls.) Most applications will pass 0 to enumerate domains on
524
- all interfaces.
525
- <P>
526
- @param listener
527
- This object will get called when domains are found.
528
- <P>
529
- @return A {@link DNSSDService} that controls the active enumeration.
530
-
531
- @throws SecurityException If a security manager is present and denies <tt>RuntimePermission("getDNSSDInstance")</tt>.
532
- @see RuntimePermission
533
- */
534
- public DNSSDService enumerateDomains(int flags, int ifIndex, final DomainListener listener) throws DNSSDException {
535
- onServiceStarting();
536
- final DNSSDService[] services = new DNSSDService[1];
537
- start(services, () -> new InternalDNSSDService(this, InternalDNSSD.enumerateDomains(flags, ifIndex, new InternalDomainListener() {
538
- @Override
539
- public void domainFound(DNSSDService domainEnum, final int flags, final int ifIndex, byte[] domain) {
540
- final String domainStr = new String(domain, UTF_8);
541
- postWith(services, operation -> listener.domainFound(operation, flags, ifIndex, domainStr));
542
- }
543
-
544
- @Override
545
- public void domainLost(DNSSDService domainEnum, final int flags, final int ifIndex, byte[] domain) {
546
- final String domainStr = new String(domain, UTF_8);
547
- postWith(services, operation -> listener.domainLost(operation, flags, ifIndex, domainStr));
548
- }
549
-
550
- @Override
551
- public void operationFailed(final DNSSDService service, final int errorCode) {
552
- postWith(services, operation -> listener.operationFailed(operation, errorCode));
553
- }
554
- })));
555
- return services[0];
556
- }
557
-
558
- /** Concatenate a three-part domain name (as provided to the listeners) into a
559
- properly-escaped full domain name. Note that strings passed to listeners are
560
- ALREADY ESCAPED where necessary.<P>
561
- @param serviceName
562
- The service name - any dots or slashes must NOT be escaped.
563
- May be null (to construct a PTR record name, e.g. "_ftp._tcp.apple.com").
564
- <P>
565
- @param regType
566
- The registration type followed by the protocol, separated by a dot (e.g. "_ftp._tcp").
567
- <P>
568
- @param domain
569
- The domain name, e.g. "apple.com". Any literal dots or backslashes must be escaped.
570
- <P>
571
- @return The full domain name.
572
-
573
- @throws SecurityException If a security manager is present and denies <tt>RuntimePermission("getDNSSDInstance")</tt>.
574
- @see RuntimePermission
575
- */
576
- public String constructFullName(String serviceName, String regType, String domain) throws DNSSDException {
577
- onServiceStarting();
578
- String result = InternalDNSSD.constructFullName(serviceName, regType, domain);
579
- onServiceStopped();
580
- return result;
581
- }
582
-
583
- /** Instruct the daemon to verify the validity of a resource record that appears to
584
- be out of date. (e.g. because tcp connection to a service's target failed.) <P>
585
-
586
- Causes the record to be flushed from the daemon's cache (as well as all other
587
- daemons' caches on the network) if the record is determined to be invalid.<P>
588
- @param flags
589
- Currently unused, reserved for future use.
590
- <P>
591
- @param ifIndex
592
- If non-zero, specifies the interface on which to reconfirm the record
593
- (the index for a given interface is determined via the if_nametoindex()
594
- family of calls.) Passing 0 causes the name to be reconfirmed on all
595
- interfaces. Passing -1 causes the name to be reconfirmed only on the
596
- local host.
597
- <P>
598
- @param fullName
599
- The resource record's full domain name.
600
- <P>
601
- @param rrtype
602
- The resource record's type (e.g. PTR, SRV, etc) as defined in nameser.h.
603
- <P>
604
- @param rrclass
605
- The class of the resource record, as defined in nameser.h (usually 1).
606
- <P>
607
- @param rdata
608
- The raw rdata of the resource record.
609
-
610
- @return Error DNSServiceErrorType
611
-
612
- @throws SecurityException If a security manager is present and denies <tt>RuntimePermission("getDNSSDInstance")</tt>.
613
- @see RuntimePermission
614
- */
615
- public int reconfirmRecord(int flags, int ifIndex, String fullName, int rrtype, int rrclass, byte[] rdata) {
616
- onServiceStarting();
617
- int error = InternalDNSSD.reconfirmRecord(flags, ifIndex, fullName, rrtype, rrclass, rdata);
618
- onServiceStopped();
619
- return error;
620
- }
621
-
622
- @Override
623
- public void onServiceStarting() {
624
- if (multicastLock == null) {
625
- synchronized (this) { // Double-check lock
626
- if (multicastLock == null) {
627
- WifiManager wifi = (WifiManager) context.getApplicationContext()
628
- .getSystemService(Context.WIFI_SERVICE);
629
- if (wifi == null) {
630
- Log.wtf("DNSSD", "Can't get WIFI Service");
631
- return;
632
- }
633
- multicastLock = wifi.createMulticastLock(MULTICAST_LOCK_NAME);
634
- multicastLock.setReferenceCounted(true);
635
- }
636
- }
637
- }
638
- multicastLock.acquire();
639
- }
640
-
641
- @Override
642
- public void onServiceStopped() {
643
- if (multicastLock == null) {
644
- Log.wtf("DNSSD", "Multicast lock doesn't exist");
645
- return;
646
- }
647
- multicastLock.release();
648
- }
649
-
650
- /** Return the index of a named interface.<P>
651
- @param ifName
652
- A valid interface name. An example is java.net.NetworkInterface.getName().
653
- <P>
654
- @return The interface index.
655
-
656
- @throws SecurityException If a security manager is present and denies <tt>RuntimePermission("getDNSSDInstance")</tt>.
657
- @see RuntimePermission
658
- */
659
- public static int getIfIndexForName(String ifName) {
660
- return InternalDNSSD.getIfIndexForName(ifName);
661
- }
662
-
663
- public static Map<String, String> parseTXTRecords(byte[] data) {
664
- return parseTXTRecords(new TXTRecord(data));
665
- }
666
-
667
- static Map<String, String> parseTXTRecords(TXTRecord record) {
668
- Map<String, String> result = new HashMap<>(record.size());
669
- for (int i = 0; i < record.size(); i++) {
670
- try {
671
- if (!TextUtils.isEmpty(record.getKey(i))) {
672
- result.put(record.getKey(i), record.getValueAsString(i));
673
- }
674
- } catch (Exception e) {
675
- Log.w("RxResolveListener", "Parsing error of " + i + " TXT record", e);
676
- }
677
- }
678
- return result;
679
- }
680
-
681
- }