/*
* 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.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Runtime.InteropServices;
namespace Tizen.Network.Connection
{
///
/// This interface provides properties to manage address information of the connection.
///
/// 3
public interface IAddressInformation
{
///
/// The DNS address.
///
/// 3
/// First DNS address of the connection.
/// Thrown during set when a feature is not supported.
/// Thrown during set when a value is an invalid parameter.
/// Thrown during set when a profile instance is invalid or when a method fails due to an invalid operation.
System.Net.IPAddress Dns1 { get; set; }
///
/// The DNS address.
///
/// 3
/// Second DNS address of the connection.
/// Thrown during set when a feature is not supported.
/// Thrown during set when a value is an invalid parameter.
/// Thrown during set when a profile instance is invalid or when a method fails due to an invalid operation.
System.Net.IPAddress Dns2 { get; set; }
///
/// The gateway address.
///
/// 3
/// Gateway address of the connection.
/// Thrown during set when a feature is not supported.
/// Thrown during set when a value is an invalid parameter.
/// Thrown during set when a profile instance is invalid or when a method fails due to an invalid operation.
System.Net.IPAddress Gateway { get; set; }
///
/// The subnet mask address.
///
/// 3
/// Subnet mask of the connection.
/// Thrown during set when a feature is not supported.
/// Thrown during set when a value is an invalid parameter.
/// Thrown during set when a profile instance is invalid or when a method fails due to an invalid operation.
System.Net.IPAddress SubnetMask { get; set; }
///
/// The IP address.
///
/// 3
/// IP address of the connection.
/// Thrown during set when a feature is not supported.
/// Thrown during set when a value is an invalid parameter.
/// Thrown during set when a profile instance is invalid or when a method fails due to an invalid operation.
System.Net.IPAddress IP { get; set; }
///
/// The type of IP config.
///
/// 3
/// IP config type of the connection.
/// Thrown during set when a feature is not supported.
/// Thrown during set when a value is an invalid parameter.
/// Thrown during set when a profile instance is invalid or when a method fails due to an invalid operation.
IPConfigType IPConfigType { get; set; }
///
/// The prefix length.
///
/// 3
/// Prefix length of the connection.
/// Thrown during set when a feature is not supported.
/// Thrown during set when a value is an invalid parameter.
/// Thrown during set when a profile instance is invalid or when a method fails due to an invalid operation.
int PrefixLength { get; set; }
///
/// The DNS config type.
///
/// 3
/// Config type of the DNS.
/// Thrown during set when a feature is not supported.
/// Thrown during set when a value is an invalid parameter.
/// Thrown during set when a profile instance is invalid or when a method fails due to an invalid operation.
DnsConfigType DnsConfigType { get; set; }
///
/// The DHCP server address. It is only supported for the IPV4 address family.
///
/// 4
/// Server address of the DHCP.
System.Net.IPAddress DhcpServerAddress { get; }
///
/// The DHCP lease duration. It is only supported for the IPV4 address family.
///
/// 5
/// Lease duration of the DHCP.
int DhcpLeaseDuration { get; }
}
internal class ConnectionAddressInformation : IAddressInformation
{
private IntPtr _profileHandle;
private AddressFamily _family;
private const String DefaultIPv4 = "0.0.0.0";
private const String DefaultIPv6 = "::";
internal ConnectionAddressInformation(IntPtr handle, AddressFamily family)
{
_profileHandle = handle;
_family = family;
}
public System.Net.IPAddress Dns1
{
get
{
IntPtr Value;
int ret = Interop.ConnectionProfile.GetDnsAddress(_profileHandle, 1, (int)_family, out Value);
return ParseIPAddress(ret, Value);
}
set
{
int ret = Interop.ConnectionProfile.SetDnsAddress(_profileHandle, 1, (int)_family, value.ToString());
if ((ConnectionError)ret != ConnectionError.None)
{
Log.Error(Globals.LogTag, "It failed to set dns1 address, " + (ConnectionError)ret);
ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.tethering.bluetooth " + "http://tizen.org/feature/network.ethernet");
ConnectionErrorFactory.CheckHandleNullException(ret, (_profileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
ConnectionErrorFactory.ThrowConnectionException(ret);
}
}
}
public System.Net.IPAddress Dns2
{
get
{
IntPtr Value;
int ret = Interop.ConnectionProfile.GetDnsAddress(_profileHandle, 2, (int)_family, out Value);
return ParseIPAddress(ret, Value);
}
set
{
int ret = Interop.ConnectionProfile.SetDnsAddress(_profileHandle, 2, (int)_family, value.ToString());
if ((ConnectionError)ret != ConnectionError.None)
{
Log.Error(Globals.LogTag, "It failed to set dns2 address, " + (ConnectionError)ret);
ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.tethering.bluetooth " + "http://tizen.org/feature/network.ethernet");
ConnectionErrorFactory.CheckHandleNullException(ret, (_profileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
ConnectionErrorFactory.ThrowConnectionException(ret);
}
}
}
public System.Net.IPAddress Gateway
{
get
{
IntPtr Value;
int ret = Interop.ConnectionProfile.GetGatewayAddress(_profileHandle, (int)_family, out Value);
return ParseIPAddress(ret, Value);
}
set
{
int ret = Interop.ConnectionProfile.SetGatewayAddress(_profileHandle, (int)_family, value.ToString());
if ((ConnectionError)ret != ConnectionError.None)
{
Log.Error(Globals.LogTag, "It failed to set gateway, " + (ConnectionError)ret);
ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.tethering.bluetooth " + "http://tizen.org/feature/network.ethernet");
ConnectionErrorFactory.CheckHandleNullException(ret, (_profileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
ConnectionErrorFactory.ThrowConnectionException(ret);
}
}
}
public System.Net.IPAddress SubnetMask
{
get
{
IntPtr Value;
int ret = Interop.ConnectionProfile.GetSubnetMask(_profileHandle, (int)_family, out Value);
return ParseIPAddress(ret, Value);
}
set
{
int ret = Interop.ConnectionProfile.SetSubnetMask(_profileHandle, (int)_family, value.ToString());
if ((ConnectionError)ret != ConnectionError.None)
{
Log.Error(Globals.LogTag, "It failed to set subnet mask, " + (ConnectionError)ret);
ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.tethering.bluetooth " + "http://tizen.org/feature/network.ethernet");
ConnectionErrorFactory.CheckHandleNullException(ret, (_profileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
ConnectionErrorFactory.ThrowConnectionException(ret);
}
}
}
public System.Net.IPAddress IP
{
get
{
IntPtr Value;
int ret = Interop.ConnectionProfile.GetIPAddress(_profileHandle, (int)_family, out Value);
return ParseIPAddress(ret, Value);
}
set
{
int ret = Interop.ConnectionProfile.SetIPAddress(_profileHandle, (int)_family, value.ToString());
if ((ConnectionError)ret != ConnectionError.None)
{
Log.Error(Globals.LogTag, "It failed to set ip, " + (ConnectionError)ret);
ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.tethering.bluetooth " + "http://tizen.org/feature/network.ethernet");
ConnectionErrorFactory.CheckHandleNullException(ret, (_profileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
ConnectionErrorFactory.ThrowConnectionException(ret);
}
}
}
public IPConfigType IPConfigType
{
get
{
int Value;
int ret = Interop.ConnectionProfile.GetIPConfigType(_profileHandle, (int)_family, out Value);
if ((ConnectionError)ret != ConnectionError.None)
{
Log.Error(Globals.LogTag, "It failed to get ip config type, " + (ConnectionError)ret);
}
return (IPConfigType)Value;
}
set
{
int ret = Interop.ConnectionProfile.SetIPConfigType(_profileHandle, (int)_family, (int)value);
if ((ConnectionError)ret != ConnectionError.None)
{
Log.Error(Globals.LogTag, "It failed to set ip config type, " + (ConnectionError)ret);
ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.tethering.bluetooth " + "http://tizen.org/feature/network.ethernet");
ConnectionErrorFactory.CheckHandleNullException(ret, (_profileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
ConnectionErrorFactory.ThrowConnectionException(ret);
}
}
}
public int PrefixLength
{
get
{
int Value;
int ret = Interop.ConnectionProfile.GetPrefixLength(_profileHandle, (int)_family, out Value);
if ((ConnectionError)ret != ConnectionError.None)
{
Log.Error(Globals.LogTag, "It failed to get prefix length, " + (ConnectionError)ret);
}
return Value;
}
set
{
int ret = Interop.ConnectionProfile.SetPrefixLength(_profileHandle, (int)_family, value);
if ((ConnectionError)ret != ConnectionError.None)
{
Log.Error(Globals.LogTag, "It failed to set prefix length, " + (ConnectionError)ret);
ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.tethering.bluetooth " + "http://tizen.org/feature/network.ethernet");
ConnectionErrorFactory.CheckHandleNullException(ret, (_profileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
ConnectionErrorFactory.ThrowConnectionException(ret);
}
}
}
public DnsConfigType DnsConfigType
{
get
{
int Value;
int ret = Interop.ConnectionProfile.GetDnsConfigType(_profileHandle, (int)_family, out Value);
if ((ConnectionError)ret != ConnectionError.None)
{
Log.Error(Globals.LogTag, "It failed to get DNS config type, " + (ConnectionError)ret);
}
return (DnsConfigType)Value;
}
set
{
int ret = Interop.ConnectionProfile.SetDnsConfigType(_profileHandle, (int)_family, (int)value);
if ((ConnectionError)ret != ConnectionError.None)
{
Log.Error(Globals.LogTag, "It failed to set DNS config type, " + (ConnectionError)ret);
ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.tethering.bluetooth " + "http://tizen.org/feature/network.ethernet");
ConnectionErrorFactory.CheckHandleNullException(ret, (_profileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
ConnectionErrorFactory.ThrowConnectionException(ret);
}
}
}
public System.Net.IPAddress DhcpServerAddress
{
get
{
string dhcpServer;
int ret = Interop.ConnectionProfile.GetDhcpServerAddress(_profileHandle, _family, out dhcpServer);
if ((ConnectionError)ret != ConnectionError.None ||
dhcpServer == null || dhcpServer.Length == 0)
{
Log.Error(Globals.LogTag, "It failed to get the DHCP server address, " + (ConnectionError)ret);
return DefaultIPAddress();
}
return System.Net.IPAddress.Parse(dhcpServer);
}
}
private System.Net.IPAddress ParseIPAddress(int ret, IntPtr addrPtr)
{
if (ret != (int)ConnectionError.None)
{
Log.Error(Globals.LogTag, "Failed to get address, Error - " + (ConnectionError)ret);
return DefaultIPAddress();
}
string addr = Marshal.PtrToStringAnsi(addrPtr);
if (addr == null || addr.Length == 0)
return DefaultIPAddress();
Interop.Libc.Free(addrPtr);
return System.Net.IPAddress.Parse(addr);
}
private System.Net.IPAddress DefaultIPAddress()
{
if (_family == AddressFamily.IPv4)
return System.Net.IPAddress.Parse(DefaultIPv4);
else
return System.Net.IPAddress.Parse(DefaultIPv6);
}
public int DhcpLeaseDuration
{
get
{
int leaseDuration;
int ret = Interop.ConnectionProfile.GetDhcpLeaseDuration(_profileHandle, _family, out leaseDuration);
if ((ConnectionError)ret != ConnectionError.None)
{
Log.Error(Globals.LogTag, "It failed to get the DHCP lease duration, " + (ConnectionError)ret);
leaseDuration = 0;
}
return leaseDuration;
}
}
}
}