/*
* 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;
using System.Runtime.InteropServices;
using System.Collections.Generic;
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 the local service registration and its properties using DNS-SD.
///
/// 4
public class DnssdService : INsdService
{
private uint _serviceHandle;
private string _serviceType;
private ushort _dnsRecordtype = 16;
private Interop.Nsd.Dnssd.ServiceRegisteredCallback _serviceRegisteredCallback;
///
/// The constructor to create the DnssdService instance that sets the serviceType to a given value.
///
/// 4
/// 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.
///
/// http://tizen.org/feature/network.service_discovery.dnssd
/// Thrown while setting this property when DNS-SD is not supported.
/// Thrown when the 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 the DNS-SD service.
///
///
/// 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.
///
/// 4
/// http://tizen.org/feature/network.service_discovery.dnssd
/// Thrown while setting this property when DNS-SD is not supported.
/// Thrown when the 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 the DNS-SD local or remote service.
///
///
/// 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.
///
/// 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 the DNS-SD local or remote service.
///
///
/// 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.
///
/// 4
/// http://tizen.org/feature/network.service_discovery.dnssd
/// Thrown while setting this property when DNS-SD is not supported.
/// Thrown if the 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 the DNS-SD 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 an 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;
}
}
///
/// Returns raw TXT records.
///
/// Returns empty bytes array in case TXT record has not been set, else returns raw TXT record.
/// 9
/// http://tizen.org/feature/network.service_discovery.dnssd
/// Thrown when DNS-SD is not supported.
/// Thrown when any other error occurred.
public byte[] GetRawTXTRecords()
{
int ret = Interop.Nsd.Dnssd.GetAllTxtRecord(_serviceHandle, out ushort length, out IntPtr data);
if (ret != (int)DnssdError.None)
{
Log.Error(Globals.LogTag, "Failed to get the TXT record, Error: " + (DnssdError)ret);
NsdErrorFactory.ThrowDnssdException(ret);
}
byte[] value = Array.Empty();
if (length > 0)
{
value = new byte[length];
Marshal.Copy(data, value, 0, length);
Interop.Libc.Free(data);
}
return value;
}
///
/// Adds the TXT record.
///
///
/// TXT record should be added after registering the 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 the value_length is zero, then "key=" will be added with an empty value.
/// http://tizen.org/feature/network.service_discovery.dnssd
/// Thrown when DNS-SD is not supported.
/// Thrown when the value of the 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 = GetRawTXTRecords();
ret = Interop.Nsd.Dnssd.SetRecord(_serviceHandle, _dnsRecordtype, (ushort)txtValue.Length, 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.service_discovery.dnssd
/// Thrown when DNS-SD is not supported.
/// Thrown when the value of the 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);
}
}
///
/// Registers the DNS-SD local service for publishing.
///
/// Name of the service must be set.
/// 4
/// http://tizen.org/privilege/internet
/// http://tizen.org/feature/network.service_discovery.dnssd
/// Thrown when any other error occurred.
/// Thrown when DNS-SD is not supported.
/// Thrown when the 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 DNS-SD local service.
///
///
/// A local service registered using RegisterService() must be passed.
///
/// 4
/// http://tizen.org/feature/network.service_discovery.dnssd
/// Thrown when any other error occurred.
/// Thrown when DNS-SD 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;
}
}
///
/// Destroys the DnssdService object.
///
~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 the DNS-SD 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 the DNS-SD 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 the DNS-SD service.
///
/// 4
public IPAddress IPv6Address
{
get
{
if (_ipv6 == null)
return IPAddress.Parse("0:0:0:0:0:0:0:0");
return IPAddress.Parse(_ipv6);
}
}
}
}