2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 using System.Threading;
22 namespace Tizen.Network.Nsd
24 internal class DnssdInitializer
26 internal DnssdInitializer()
28 Globals.DnssdInitialize();
33 int ret = Interop.Nsd.Dnssd.Deinitialize();
34 if (ret != (int)DnssdError.None)
36 Log.Error(Globals.LogTag, "Failed to deinitialize Dnssd, Error - " + (DnssdError)ret);
41 /// This class is used for managing local service registration and its properties using DNSSD.
43 /// <since_tizen> 4 </since_tizen>
44 public class DnssdService : INsdService
46 private uint _serviceHandle;
47 private string _serviceType;
48 private ushort _dnsRecordtype = 16;
49 private Interop.Nsd.Dnssd.ServiceRegisteredCallback _serviceRegisteredCallback;
52 /// Constructor to create DnssdService instance that sets the serviceType to a given value.
54 /// <since_tizen> 4 </since_tizen>
55 /// <param name="serviceType">The DNSSD service type. It is expressed as type followed by protocol, separated by a dot(e.g. "_ftp._tcp").
56 /// It must begin with an underscore, followed by 1-15 characters which may be letters, digits or hyphens.
58 /// <feature>http://tizen.org/feature/network.service_discovery.dnssd</feature>
59 /// <exception cref="NotSupportedException">Thrown while setting this property when DNSSD is not supported.</exception>
60 /// <exception cref="ArgumentException">Thrown when serviceType is set to null.</exception>
61 public DnssdService(string serviceType)
63 _serviceType = serviceType;
64 DnssdInitializeCreateService();
67 internal DnssdService(uint service)
69 _serviceHandle = service;
72 internal void DnssdInitializeCreateService()
74 DnssdInitializer dnssdInit = Globals.s_threadDns.Value;
75 Log.Info(Globals.LogTag, "Initialize ThreadLocal<DnssdInitializer> instance = " + dnssdInit);
76 int ret = Interop.Nsd.Dnssd.CreateService(_serviceType, out _serviceHandle);
77 if (ret != (int)DnssdError.None)
79 Log.Error(Globals.LogTag, "Failed to create a local Dnssd service handle, Error - " + (DnssdError)ret);
80 NsdErrorFactory.ThrowDnssdException(ret);
85 /// Name of DNSSD service.
88 /// Set Name for only unregistered service created locally.
89 /// It may be up to 63 bytes.
90 /// In case of error, null will be returned during get and exception will be thrown during set.
92 /// <since_tizen> 4 </since_tizen>
93 /// <feature>http://tizen.org/feature/network.service_discovery.dnssd</feature>
94 /// <exception cref="NotSupportedException">Thrown while setting this property when DNSSD is not supported.</exception>
95 /// <exception cref="ArgumentException">Thrown when Name value is set to null.</exception>
96 /// <exception cref="InvalidOperationException">Thrown while setting this property when any other error occurred.</exception>
102 int ret = Interop.Nsd.Dnssd.GetName(_serviceHandle, out name);
103 if (ret != (int)DnssdError.None)
105 Log.Error(Globals.LogTag, "Failed to get name of service, Error: " + (DnssdError)ret);
114 if (!Globals.s_threadDns.IsValueCreated)
116 DnssdInitializeCreateService();
119 int ret = Interop.Nsd.Dnssd.SetName(_serviceHandle, value);
120 if (ret != (int)DnssdError.None)
122 Log.Error(Globals.LogTag, "Failed to set name of service, Error: " + (DnssdError)ret);
123 NsdErrorFactory.ThrowDnssdException(ret);
129 /// Type of DNSSD local/remote service.
132 /// It is expressed as type followed by protocol, separated by a dot(e.g. "_ftp._tcp").
133 /// It must begin with an underscore, followed by 1-15 characters which may be letters, digits or hyphens.
134 /// In case of error, null will be returned.
136 /// <since_tizen> 4 </since_tizen>
142 int ret = Interop.Nsd.Dnssd.GetType(_serviceHandle, out type);
143 if (ret != (int)DnssdError.None)
145 Log.Error(Globals.LogTag, "Failed to get type of service, Error: " + (DnssdError)ret);
154 /// Port number of DNSSD local/remote service.
157 /// Set Port for only unregistered service created locally. The default value of Port is 0.
158 /// In case of error, -1 will be returned during get and exception will be thrown during set.
160 /// <since_tizen> 4 </since_tizen>
161 /// <feature>http://tizen.org/feature/network.service_discovery.dnssd</feature>
162 /// <exception cref="NotSupportedException">Thrown while setting this property when DNSSD is not supported.</exception>
163 /// <exception cref="ArgumentException">Thrown if value of Port is set to less than 0 or more than 65535.</exception>
164 /// <exception cref="InvalidOperationException">Thrown while setting this property when any other error occurred.</exception>
170 int ret = Interop.Nsd.Dnssd.GetPort(_serviceHandle, out port);
171 if (ret != (int)DnssdError.None)
173 Log.Error(Globals.LogTag, "Failed to get port number of Dnssd service, Error: " + (DnssdError)ret);
182 if (!Globals.s_threadDns.IsValueCreated)
184 DnssdInitializeCreateService();
187 int ret = Interop.Nsd.Dnssd.SetPort(_serviceHandle, value);
188 if (ret != (int)DnssdError.None)
190 Log.Error(Globals.LogTag, "Failed to set port number of Dnssd service, Error: " + (DnssdError)ret);
191 NsdErrorFactory.ThrowDnssdException(ret);
197 /// IP address of DNSSD remote service.
200 /// 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.
201 /// In case of error, null object will be returned.
203 /// <since_tizen> 4 </since_tizen>
204 public IPAddressInformation IP
209 int ret = Interop.Nsd.Dnssd.GetIP(_serviceHandle, out IPv4, out IPv6);
210 if (ret != (int)DnssdError.None)
212 Log.Error(Globals.LogTag, "Failed to get the IP of Dnssd remote service, Error: " + (DnssdError)ret);
216 IPAddressInformation IPAddressInstance = new IPAddressInformation(IPv4, IPv6);
217 return IPAddressInstance;
221 private void GetTxtRecord(out ushort length, out byte[] value)
223 int ret = Interop.Nsd.Dnssd.GetAllTxtRecord(_serviceHandle, out length, out value);
224 if (ret != (int)DnssdError.None)
226 Log.Error(Globals.LogTag, "Failed to get the TXT record, Error: " + (DnssdError)ret);
227 NsdErrorFactory.ThrowDnssdException(ret);
232 /// Adds the TXT record.
235 /// TXT record should be added after registering local service using RegisterService().
237 /// <since_tizen> 4 </since_tizen>
238 /// <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>
239 /// <param name="value">The value of the TXT record.If null, then "key" will be added with no value. If non-null but value_length is zero, then "key=" will be added with empty value.</param>
240 /// <feature>http://tizen.org/feature/network.service_discovery.dnssd</feature>
241 /// <exception cref="NotSupportedException">Thrown when DNSSD is not supported.</exception>
242 /// <exception cref="ArgumentException">Thrown when value of key is null.</exception>
243 /// <exception cref="InvalidOperationException">Thrown when any other error occurred.</exception>
244 public void AddTXTRecord(string key, string value)
246 byte[] byteValue = Encoding.UTF8.GetBytes(value);
247 ushort length = Convert.ToUInt16(byteValue.Length);
248 int ret = Interop.Nsd.Dnssd.AddTxtRecord(_serviceHandle, key, length, byteValue);
249 if (ret != (int)DnssdError.None)
251 Log.Error(Globals.LogTag, "Failed to add the TXT record, Error: " + (DnssdError)ret);
252 NsdErrorFactory.ThrowDnssdException(ret);
257 GetTxtRecord(out txtLength, out txtValue);
259 ret = Interop.Nsd.Dnssd.SetRecord(_serviceHandle, _dnsRecordtype, txtLength, txtValue);
260 if (ret != (int)DnssdError.None)
262 Log.Error(Globals.LogTag, "Failed to set the DNS resource record, Error: " + (DnssdError)ret);
263 NsdErrorFactory.ThrowDnssdException(ret);
268 /// Removes the TXT record.
270 /// <since_tizen> 4 </since_tizen>
271 /// <param name="key">The key of the TXT record to be removed.</param>
272 /// <feature>http://tizen.org/feature/network.service_discovery.dnssd</feature>
273 /// <exception cref="NotSupportedException">Thrown when DNSSD is not supported.</exception>
274 /// <exception cref="ArgumentException">Thrown when value of key is null.</exception>
275 /// <exception cref="InvalidOperationException">Thrown when any other error occurred.</exception>
276 public void RemoveTXTRecord(string key)
278 int ret = Interop.Nsd.Dnssd.RemoveTxtRecord(_serviceHandle, key);
279 if (ret != (int)DnssdError.None)
281 Log.Error(Globals.LogTag, "Failed to remove the TXT record, Error: " + (DnssdError)ret);
282 NsdErrorFactory.ThrowDnssdException(ret);
287 GetTxtRecord(out txtLength, out txtValue);
290 ret = Interop.Nsd.Dnssd.UnsetRecord(_serviceHandle, _dnsRecordtype);
291 if (ret != (int)DnssdError.None)
293 Log.Error(Globals.LogTag, "Failed to unset the DNS resource record, Error: " + (DnssdError)ret);
294 NsdErrorFactory.ThrowDnssdException(ret);
300 /// Registers the DNSSD local service for publishing.
302 /// Name of the service must be set.
303 /// <since_tizen> 4 </since_tizen>
304 /// <privilege>http://tizen.org/privilege/internet</privilege>
305 /// <feature>http://tizen.org/feature/network.service_discovery.dnssd</feature>
306 /// <exception cref="InvalidOperationException">Thrown when any other error occurred.</exception>
307 /// <exception cref="NotSupportedException">Thrown when DNSSD is not supported.</exception>
308 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
309 public void RegisterService()
311 if (!Globals.s_threadDns.IsValueCreated)
313 DnssdInitializeCreateService();
316 _serviceRegisteredCallback = (DnssdError result, uint service, IntPtr userData) =>
318 if (result != DnssdError.None)
320 Log.Error(Globals.LogTag, "Failed to finish the registration of Dnssd local service, Error: " + result);
321 NsdErrorFactory.ThrowDnssdException((int)result);
325 int ret = Interop.Nsd.Dnssd.RegisterService(_serviceHandle, _serviceRegisteredCallback, IntPtr.Zero);
326 if (ret != (int)DnssdError.None)
328 Log.Error(Globals.LogTag, "Failed to register the Dnssd local service, Error: " + (DnssdError)ret);
329 NsdErrorFactory.ThrowDnssdException(ret);
334 /// Deregisters the DNSSD local service.
337 /// A local service registered using RegisterService() must be passed.
339 /// <since_tizen> 4 </since_tizen>
340 /// <feature>http://tizen.org/feature/network.service_discovery.dnssd</feature>
341 /// <exception cref="InvalidOperationException">Thrown when any other error occurred.</exception>
342 /// <exception cref="NotSupportedException">Thrown when DNSSD is not supported.</exception>
343 public void DeregisterService()
345 int ret = Interop.Nsd.Dnssd.DeregisterService(_serviceHandle);
346 if (ret != (int)DnssdError.None)
348 Log.Error(Globals.LogTag, "Failed to deregister the Dnssd local service, Error: " + (DnssdError)ret);
349 NsdErrorFactory.ThrowDnssdException(ret);
353 #region IDisposable Support
354 private bool _disposedValue = false; // To detect redundant calls
356 private void Dispose(bool disposing)
362 if (_serviceHandle != 0)
364 int ret = Interop.Nsd.Dnssd.DestroyService(_serviceHandle);
365 if (ret != (int)DnssdError.None)
367 Log.Error(Globals.LogTag, "Failed to destroy the local Dnssd service handle, Error - " + (DnssdError)ret);
372 _disposedValue = true;
377 /// Destroy the DnssdService object
385 /// Disposes the memory allocated to unmanaged resources.
387 /// <since_tizen> 4 </since_tizen>
388 public void Dispose()
391 GC.SuppressFinalize(this);
397 /// This class manages the IP address properties of DNSSD service.
399 /// <since_tizen> 4 </since_tizen>
400 public class IPAddressInformation
402 private string _ipv4, _ipv6;
403 internal IPAddressInformation()
407 internal IPAddressInformation(string ipv4, string ipv6)
414 /// The IP version 4 address of DNSSD service.
416 /// <since_tizen> 4 </since_tizen>
417 public IPAddress IPv4Address
422 return IPAddress.Parse("0.0.0.0");
423 return IPAddress.Parse(_ipv4);
428 /// The IP version 6 address of DNSSD service.
430 /// <since_tizen> 4 </since_tizen>
431 public IPAddress IPv6Address
436 return IPAddress.Parse("0:0:0:0:0:0:0:0");
437 return IPAddress.Parse(_ipv6);