/*
* 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);
}
}
}
///
/// This class is used for managing local service registration and its properties using DNSSD.
///
/// 4
public class DnssdService : INsdService
{
private uint _serviceHandle;
private string _serviceType;
private ushort _dnsRecordtype = 16;
private Interop.Nsd.Dnssd.ServiceRegisteredCallback _serviceRegisteredCallback;
///
/// Constructor to create DnssdService instance that sets the serviceType to a given value.
///
/// 4
/// The DNSSD service type. It is expressed as type followed by protocol, separated by a dot(e.g. "_ftp._tcp").
/// It must begin with an underscore, followed by 1-15 characters which may be letters, digits or hyphens.
///
/// http://tizen.org/feature/network.dnssd
/// Thrown while setting this property when DNSSD is not supported.
/// Thrown when serviceType is set to null.
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 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);
}
}
///
/// Name of DNSSD service.
///
///
/// Set Name for only unregistered service created locally.
/// It may be up to 63 bytes.
/// In case of error, null will be returned during get and exception will be thrown during set.
///
/// 4
/// http://tizen.org/feature/network.dnssd
/// Thrown while setting this property when DNSSD is not supported.
/// Thrown when Name value is set to null.
/// Thrown while setting this property when any other error occurred.
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);
}
}
}
///
/// Type of DNSSD local/remote service.
///
///
/// It is expressed as type followed by protocol, separated by a dot(e.g. "_ftp._tcp").
/// It must begin with an underscore, followed by 1-15 characters which may be letters, digits or hyphens.
/// In case of error, null will be returned.
///
/// 4
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;
}
}
///
/// Port number of DNSSD local/remote service.
///
///
/// Set Port for only unregistered service created locally. The default value of Port is 0.
/// In case of error, -1 will be returned during get and exception will be thrown during set.
///
/// 4
/// http://tizen.org/feature/network.dnssd
/// Thrown while setting this property when DNSSD is not supported.
/// Thrown if value of Port is set to less than 0 or more than 65535.
/// Thrown while setting this property when any other error occurred.
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);
}
}
}
///
/// IP address of DNSSD remote service.
///
///
/// 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 error, null object will be returned.
///
/// 4
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);
}
}
///
/// Adds the TXT record.
///
///
/// TXT record should be added after registering local service using RegisterService().
///
/// 4
/// The key of the TXT record. It must be a null-terminated string with 9 characters or fewer excluding null. It is case insensitive.
/// 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.
/// http://tizen.org/feature/network.dnssd
/// Thrown when DNSSD is not supported.
/// Thrown when value of key is null.
/// Thrown when any other error occurred.
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);
}
}
///
/// Removes the TXT record.
///
/// 4
/// The key of the TXT record to be removed.
/// http://tizen.org/feature/network.dnssd
/// Thrown when DNSSD is not supported.
/// Thrown when value of key is null.
/// Thrown when any other error occurred.
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);
}
}
}
///
/// Registers the DNSSD local service for publishing.
///
/// Name of the service must be set.
/// 4
/// http://tizen.org/privilege/internet
/// http://tizen.org/feature/network.dnssd
/// Thrown when any other error occurred.
/// Thrown when DNSSD is not supported.
/// Thrown when permission is denied.
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);
}
}
///
/// Deregisters the DNSSD local service.
///
///
/// A local service registered using RegisterService() must be passed.
///
/// 4
/// http://tizen.org/feature/network.dnssd
/// Thrown when any other error occurred.
/// Thrown when DNSSD is not supported.
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;
}
}
~DnssdService()
{
Dispose(false);
}
///
/// Disposes the memory allocated to unmanaged resources.
///
/// 4
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
///
/// This class manages the IP address properties of DNSSD service.
///
/// 4
public class IPAddressInformation
{
private string _ipv4, _ipv6;
internal IPAddressInformation()
{
}
internal IPAddressInformation(string ipv4, string ipv6)
{
_ipv4 = ipv4;
_ipv6 = ipv6;
}
///
/// The IP version 4 address of DNSSD service.
///
/// 4
public IPAddress IPv4Address
{
get
{
if (_ipv4 == null)
return IPAddress.Parse("0.0.0.0");
return IPAddress.Parse(_ipv4);
}
}
///
/// The IP version 6 address of DNSSD service.
///
/// 4
public IPAddress IPv6Address
{
get
{
if (_ipv6 == null)
return IPAddress.Parse("0:0:0:0:0:0:0:0");
return IPAddress.Parse(_ipv6);
}
}
}
}