/
DnssdService.cs
executable file
·441 lines (404 loc) · 17.5 KB
/
DnssdService.cs
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
* Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Text;
using System.Threading;
using System.Net;
namespace Tizen.Network.Nsd
{
internal class DnssdInitializer
{
internal DnssdInitializer()
{
Globals.DnssdInitialize();
}
~DnssdInitializer()
{
int ret = Interop.Nsd.Dnssd.Deinitialize();
if (ret != (int)DnssdError.None)
{
Log.Error(Globals.LogTag, "Failed to deinitialize Dnssd, Error - " + (DnssdError)ret);
}
}
}
/// <summary>
/// This class is used for managing the local service registration and its properties using DNS-SD.
/// </summary>
/// <since_tizen> 4 </since_tizen>
public class DnssdService : INsdService
{
private uint _serviceHandle;
private string _serviceType;
private ushort _dnsRecordtype = 16;
private Interop.Nsd.Dnssd.ServiceRegisteredCallback _serviceRegisteredCallback;
/// <summary>
/// The constructor to create the DnssdService instance that sets the serviceType to a given value.
/// </summary>
/// <since_tizen> 4 </since_tizen>
/// <param name="serviceType">The DNS-SD service type. It is expressed as a type followed by the protocol, separated by a dot (For example, "_ftp._tcp").
/// It must begin with an underscore followed by 1-15 characters, which may be letters, digits, or hyphens.
/// </param>
/// <feature>http://tizen.org/feature/network.service_discovery.dnssd</feature>
/// <exception cref="NotSupportedException">Thrown while setting this property when DNS-SD is not supported.</exception>
/// <exception cref="ArgumentException">Thrown when the serviceType is set to null.</exception>
public DnssdService(string serviceType)
{
_serviceType = serviceType;
DnssdInitializeCreateService();
}
internal DnssdService(uint service)
{
_serviceHandle = service;
}
internal void DnssdInitializeCreateService()
{
DnssdInitializer dnssdInit = Globals.s_threadDns.Value;
Log.Info(Globals.LogTag, "Initialize ThreadLocal<DnssdInitializer> instance = " + dnssdInit);
int ret = Interop.Nsd.Dnssd.CreateService(_serviceType, out _serviceHandle);
if (ret != (int)DnssdError.None)
{
Log.Error(Globals.LogTag, "Failed to create a local Dnssd service handle, Error - " + (DnssdError)ret);
NsdErrorFactory.ThrowDnssdException(ret);
}
}
/// <summary>
/// Name of the DNS-SD service.
/// </summary>
/// <remarks>
/// Set the name for only an unregistered service created locally.
/// It may be up to 63 bytes.
/// In case of an error, null will be returned during get and exception will be thrown during set.
/// </remarks>
/// <since_tizen> 4 </since_tizen>
/// <feature>http://tizen.org/feature/network.service_discovery.dnssd</feature>
/// <exception cref="NotSupportedException">Thrown while setting this property when DNS-SD is not supported.</exception>
/// <exception cref="ArgumentException">Thrown when the name value is set to null.</exception>
/// <exception cref="InvalidOperationException">Thrown while setting this property when any other error occurred.</exception>
public string Name
{
get
{
string name;
int ret = Interop.Nsd.Dnssd.GetName(_serviceHandle, out name);
if (ret != (int)DnssdError.None)
{
Log.Error(Globals.LogTag, "Failed to get name of service, Error: " + (DnssdError)ret);
return null;
}
return name;
}
set
{
if (!Globals.s_threadDns.IsValueCreated)
{
DnssdInitializeCreateService();
}
int ret = Interop.Nsd.Dnssd.SetName(_serviceHandle, value);
if (ret != (int)DnssdError.None)
{
Log.Error(Globals.LogTag, "Failed to set name of service, Error: " + (DnssdError)ret);
NsdErrorFactory.ThrowDnssdException(ret);
}
}
}
/// <summary>
/// Type of the DNS-SD local or remote service.
/// </summary>
/// <remarks>
/// It is expressed as a type followed by the protocol, separated by a dot (For example, "_ftp._tcp").
/// It must begin with an underscore followed by 1-15 characters, which may be letters, digits, or hyphens.
/// In case of an error, null will be returned.
/// </remarks>
/// <since_tizen> 4 </since_tizen>
public string Type
{
get
{
string type;
int ret = Interop.Nsd.Dnssd.GetType(_serviceHandle, out type);
if (ret != (int)DnssdError.None)
{
Log.Error(Globals.LogTag, "Failed to get type of service, Error: " + (DnssdError)ret);
return null;
}
return type;
}
}
/// <summary>
/// Port number of the DNS-SD local or remote service.
/// </summary>
/// <remarks>
/// Set the port for only an unregistered service created locally. The default value of the port is 0.
/// In case of an error, -1 will be returned during get and exception will be thrown during set.
/// </remarks>
/// <since_tizen> 4 </since_tizen>
/// <feature>http://tizen.org/feature/network.service_discovery.dnssd</feature>
/// <exception cref="NotSupportedException">Thrown while setting this property when DNS-SD is not supported.</exception>
/// <exception cref="ArgumentException">Thrown if the value of port is set to less than 0 or more than 65535.</exception>
/// <exception cref="InvalidOperationException">Thrown while setting this property when any other error occurred.</exception>
public int Port
{
get
{
int port;
int ret = Interop.Nsd.Dnssd.GetPort(_serviceHandle, out port);
if (ret != (int)DnssdError.None)
{
Log.Error(Globals.LogTag, "Failed to get port number of Dnssd service, Error: " + (DnssdError)ret);
return -1;
}
return port;
}
set
{
if (!Globals.s_threadDns.IsValueCreated)
{
DnssdInitializeCreateService();
}
int ret = Interop.Nsd.Dnssd.SetPort(_serviceHandle, value);
if (ret != (int)DnssdError.None)
{
Log.Error(Globals.LogTag, "Failed to set port number of Dnssd service, Error: " + (DnssdError)ret);
NsdErrorFactory.ThrowDnssdException(ret);
}
}
}
/// <summary>
/// IP address of the DNS-SD remote service.
/// </summary>
/// <remarks>
/// If the remote service has no IPv4 Address, then IPv4Address would contain null and if it has no IPv6 Address, then IPv6Address would contain null.
/// In case of an error, null object will be returned.
/// </remarks>
/// <since_tizen> 4 </since_tizen>
public IPAddressInformation IP
{
get
{
string IPv4, IPv6;
int ret = Interop.Nsd.Dnssd.GetIP(_serviceHandle, out IPv4, out IPv6);
if (ret != (int)DnssdError.None)
{
Log.Error(Globals.LogTag, "Failed to get the IP of Dnssd remote service, Error: " + (DnssdError)ret);
return null;
}
IPAddressInformation IPAddressInstance = new IPAddressInformation(IPv4, IPv6);
return IPAddressInstance;
}
}
private void GetTxtRecord(out ushort length, out byte[] value)
{
int ret = Interop.Nsd.Dnssd.GetAllTxtRecord(_serviceHandle, out length, out value);
if (ret != (int)DnssdError.None)
{
Log.Error(Globals.LogTag, "Failed to get the TXT record, Error: " + (DnssdError)ret);
NsdErrorFactory.ThrowDnssdException(ret);
}
}
/// <summary>
/// Adds the TXT record.
/// </summary>
/// <remarks>
/// TXT record should be added after registering the local service using RegisterService().
/// </remarks>
/// <since_tizen> 4 </since_tizen>
/// <param name="key">The key of the TXT record. It must be a null-terminated string with 9 characters or fewer excluding null. It is case insensitive.</param>
/// <param name="value">The value of the TXT record. If null, then "key" will be added with no value. If non-null but the value_length is zero, then "key=" will be added with an empty value.</param>
/// <feature>http://tizen.org/feature/network.service_discovery.dnssd</feature>
/// <exception cref="NotSupportedException">Thrown when DNS-SD is not supported.</exception>
/// <exception cref="ArgumentException">Thrown when the value of the key is null.</exception>
/// <exception cref="InvalidOperationException">Thrown when any other error occurred.</exception>
public void AddTXTRecord(string key, string value)
{
byte[] byteValue = Encoding.UTF8.GetBytes(value);
ushort length = Convert.ToUInt16(byteValue.Length);
int ret = Interop.Nsd.Dnssd.AddTxtRecord(_serviceHandle, key, length, byteValue);
if (ret != (int)DnssdError.None)
{
Log.Error(Globals.LogTag, "Failed to add the TXT record, Error: " + (DnssdError)ret);
NsdErrorFactory.ThrowDnssdException(ret);
}
byte[] txtValue;
ushort txtLength;
GetTxtRecord(out txtLength, out txtValue);
ret = Interop.Nsd.Dnssd.SetRecord(_serviceHandle, _dnsRecordtype, txtLength, txtValue);
if (ret != (int)DnssdError.None)
{
Log.Error(Globals.LogTag, "Failed to set the DNS resource record, Error: " + (DnssdError)ret);
NsdErrorFactory.ThrowDnssdException(ret);
}
}
/// <summary>
/// Removes the TXT record.
/// </summary>
/// <since_tizen> 4 </since_tizen>
/// <param name="key">The key of the TXT record to be removed.</param>
/// <feature>http://tizen.org/feature/network.service_discovery.dnssd</feature>
/// <exception cref="NotSupportedException">Thrown when DNS-SD is not supported.</exception>
/// <exception cref="ArgumentException">Thrown when the value of the key is null.</exception>
/// <exception cref="InvalidOperationException">Thrown when any other error occurred.</exception>
public void RemoveTXTRecord(string key)
{
int ret = Interop.Nsd.Dnssd.RemoveTxtRecord(_serviceHandle, key);
if (ret != (int)DnssdError.None)
{
Log.Error(Globals.LogTag, "Failed to remove the TXT record, Error: " + (DnssdError)ret);
NsdErrorFactory.ThrowDnssdException(ret);
}
byte[] txtValue;
ushort txtLength;
GetTxtRecord(out txtLength, out txtValue);
if (txtLength == 0)
{
ret = Interop.Nsd.Dnssd.UnsetRecord(_serviceHandle, _dnsRecordtype);
if (ret != (int)DnssdError.None)
{
Log.Error(Globals.LogTag, "Failed to unset the DNS resource record, Error: " + (DnssdError)ret);
NsdErrorFactory.ThrowDnssdException(ret);
}
}
}
/// <summary>
/// Registers the DNS-SD local service for publishing.
/// </summary>
/// Name of the service must be set.
/// <since_tizen> 4 </since_tizen>
/// <privilege>http://tizen.org/privilege/internet</privilege>
/// <feature>http://tizen.org/feature/network.service_discovery.dnssd</feature>
/// <exception cref="InvalidOperationException">Thrown when any other error occurred.</exception>
/// <exception cref="NotSupportedException">Thrown when DNS-SD is not supported.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
public void RegisterService()
{
if (!Globals.s_threadDns.IsValueCreated)
{
DnssdInitializeCreateService();
}
_serviceRegisteredCallback = (DnssdError result, uint service, IntPtr userData) =>
{
if (result != DnssdError.None)
{
Log.Error(Globals.LogTag, "Failed to finish the registration of Dnssd local service, Error: " + result);
NsdErrorFactory.ThrowDnssdException((int)result);
}
};
int ret = Interop.Nsd.Dnssd.RegisterService(_serviceHandle, _serviceRegisteredCallback, IntPtr.Zero);
if (ret != (int)DnssdError.None)
{
Log.Error(Globals.LogTag, "Failed to register the Dnssd local service, Error: " + (DnssdError)ret);
NsdErrorFactory.ThrowDnssdException(ret);
}
}
/// <summary>
/// Deregisters the DNS-SD local service.
/// </summary>
/// <remarks>
/// A local service registered using RegisterService() must be passed.
/// </remarks>
/// <since_tizen> 4 </since_tizen>
/// <feature>http://tizen.org/feature/network.service_discovery.dnssd</feature>
/// <exception cref="InvalidOperationException">Thrown when any other error occurred.</exception>
/// <exception cref="NotSupportedException">Thrown when DNS-SD is not supported.</exception>
public void DeregisterService()
{
int ret = Interop.Nsd.Dnssd.DeregisterService(_serviceHandle);
if (ret != (int)DnssdError.None)
{
Log.Error(Globals.LogTag, "Failed to deregister the Dnssd local service, Error: " + (DnssdError)ret);
NsdErrorFactory.ThrowDnssdException(ret);
}
}
#region IDisposable Support
private bool _disposedValue = false; // To detect redundant calls
private void Dispose(bool disposing)
{
if (!_disposedValue)
{
if (disposing)
{
if (_serviceHandle != 0)
{
int ret = Interop.Nsd.Dnssd.DestroyService(_serviceHandle);
if (ret != (int)DnssdError.None)
{
Log.Error(Globals.LogTag, "Failed to destroy the local Dnssd service handle, Error - " + (DnssdError)ret);
}
}
}
_disposedValue = true;
}
}
/// <summary>
/// Destroys the DnssdService object.
/// </summary>
~DnssdService()
{
Dispose(false);
}
/// <summary>
/// Disposes the memory allocated to unmanaged resources.
/// </summary>
/// <since_tizen> 4 </since_tizen>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
/// <summary>
/// This class manages the IP address properties of the DNS-SD service.
/// </summary>
/// <since_tizen> 4 </since_tizen>
public class IPAddressInformation
{
private string _ipv4, _ipv6;
internal IPAddressInformation()
{
}
internal IPAddressInformation(string ipv4, string ipv6)
{
_ipv4 = ipv4;
_ipv6 = ipv6;
}
/// <summary>
/// The IP version 4 address of the DNS-SD service.
/// </summary>
/// <since_tizen> 4 </since_tizen>
public IPAddress IPv4Address
{
get
{
if (_ipv4 == null)
return IPAddress.Parse("0.0.0.0");
return IPAddress.Parse(_ipv4);
}
}
/// <summary>
/// The IP version 6 address of the DNS-SD service.
/// </summary>
/// <since_tizen> 4 </since_tizen>
public IPAddress IPv6Address
{
get
{
if (_ipv6 == null)
return IPAddress.Parse("0:0:0:0:0:0:0:0");
return IPAddress.Parse(_ipv6);
}
}
}
}