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.
18 using System.Collections.Generic;
21 using System.Threading.Tasks;
22 using System.Collections;
23 using System.Runtime.InteropServices;
25 namespace Tizen.Network.Connection
28 /// This interface provides properties to manage address information of the connection.
30 /// <since_tizen> 3 </since_tizen>
31 public interface IAddressInformation
36 /// <since_tizen> 3 </since_tizen>
37 /// <value>First DNS address of the connection.</value>
38 /// <exception cref="System.NotSupportedException">Thrown during set when a feature is not supported.</exception>
39 /// <exception cref="System.ArgumentException">Thrown during set when a value is an invalid parameter.</exception>
40 /// <exception cref="System.InvalidOperationException">Thrown during set when a profile instance is invalid or when a method fails due to an invalid operation.</exception>
41 System.Net.IPAddress Dns1 { get; set; }
46 /// <since_tizen> 3 </since_tizen>
47 /// <value>Second DNS address of the connection.</value>
48 /// <exception cref="System.NotSupportedException">Thrown during set when a feature is not supported.</exception>
49 /// <exception cref="System.ArgumentException">Thrown during set when a value is an invalid parameter.</exception>
50 /// <exception cref="System.InvalidOperationException">Thrown during set when a profile instance is invalid or when a method fails due to an invalid operation.</exception>
51 System.Net.IPAddress Dns2 { get; set; }
54 /// The gateway address.
56 /// <since_tizen> 3 </since_tizen>
57 /// <value>Gateway address of the connection.</value>
58 /// <exception cref="System.NotSupportedException">Thrown during set when a feature is not supported.</exception>
59 /// <exception cref="System.ArgumentException">Thrown during set when a value is an invalid parameter.</exception>
60 /// <exception cref="System.InvalidOperationException">Thrown during set when a profile instance is invalid or when a method fails due to an invalid operation.</exception>
61 System.Net.IPAddress Gateway { get; set; }
64 /// The subnet mask address.
66 /// <since_tizen> 3 </since_tizen>
67 /// <value>Subnet mask of the connection.</value>
68 /// <exception cref="System.NotSupportedException">Thrown during set when a feature is not supported.</exception>
69 /// <exception cref="System.ArgumentException">Thrown during set when a value is an invalid parameter.</exception>
70 /// <exception cref="System.InvalidOperationException">Thrown during set when a profile instance is invalid or when a method fails due to an invalid operation.</exception>
71 System.Net.IPAddress SubnetMask { get; set; }
76 /// <since_tizen> 3 </since_tizen>
77 /// <value>IP address of the connection.</value>
78 /// <exception cref="System.NotSupportedException">Thrown during set when a feature is not supported.</exception>
79 /// <exception cref="System.ArgumentException">Thrown during set when a value is an invalid parameter.</exception>
80 /// <exception cref="System.InvalidOperationException">Thrown during set when a profile instance is invalid or when a method fails due to an invalid operation.</exception>
81 System.Net.IPAddress IP { get; set; }
84 /// The type of IP config.
86 /// <since_tizen> 3 </since_tizen>
87 /// <value>IP config type of the connection.</value>
88 /// <exception cref="System.NotSupportedException">Thrown during set when a feature is not supported.</exception>
89 /// <exception cref="System.ArgumentException">Thrown during set when a value is an invalid parameter.</exception>
90 /// <exception cref="System.InvalidOperationException">Thrown during set when a profile instance is invalid or when a method fails due to an invalid operation.</exception>
91 IPConfigType IPConfigType { get; set; }
94 /// The prefix length.
96 /// <since_tizen> 3 </since_tizen>
97 /// <value>Prefix length of the connection.</value>
98 /// <exception cref="System.NotSupportedException">Thrown during set when a feature is not supported.</exception>
99 /// <exception cref="System.ArgumentException">Thrown during set when a value is an invalid parameter.</exception>
100 /// <exception cref="System.InvalidOperationException">Thrown during set when a profile instance is invalid or when a method fails due to an invalid operation.</exception>
101 int PrefixLength { get; set; }
104 /// The DNS config type.
106 /// <since_tizen> 3 </since_tizen>
107 /// <value>Config type of the DNS.</value>
108 /// <exception cref="System.NotSupportedException">Thrown during set when a feature is not supported.</exception>
109 /// <exception cref="System.ArgumentException">Thrown during set when a value is an invalid parameter.</exception>
110 /// <exception cref="System.InvalidOperationException">Thrown during set when a profile instance is invalid or when a method fails due to an invalid operation.</exception>
111 DnsConfigType DnsConfigType { get; set; }
114 /// The DHCP server address. It is only supported for the IPV4 address family.
116 /// <since_tizen> 4 </since_tizen>
117 /// <value>Server address of the DHCP.</value>
118 System.Net.IPAddress DhcpServerAddress { get; }
121 /// The DHCP lease duration. It is only supported for the IPV4 address family.
123 /// <since_tizen> 5 </since_tizen>
124 /// <value>Lease duration of the DHCP.</value>
125 int DhcpLeaseDuration { get; }
128 internal class ConnectionAddressInformation : IAddressInformation
130 private IntPtr _profileHandle;
131 private AddressFamily _family;
132 private const String DefaultIPv4 = "0.0.0.0";
133 private const String DefaultIPv6 = "::";
135 internal ConnectionAddressInformation(IntPtr handle, AddressFamily family)
137 _profileHandle = handle;
141 public System.Net.IPAddress Dns1
146 int ret = Interop.ConnectionProfile.GetDnsAddress(_profileHandle, 1, (int)_family, out Value);
147 return ParseIPAddress(ret, Value);
152 int ret = Interop.ConnectionProfile.SetDnsAddress(_profileHandle, 1, (int)_family, value.ToString());
153 if ((ConnectionError)ret != ConnectionError.None)
155 Log.Error(Globals.LogTag, "It failed to set dns1 address, " + (ConnectionError)ret);
156 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");
157 ConnectionErrorFactory.CheckHandleNullException(ret, (_profileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
158 ConnectionErrorFactory.ThrowConnectionException(ret);
162 public System.Net.IPAddress Dns2
167 int ret = Interop.ConnectionProfile.GetDnsAddress(_profileHandle, 2, (int)_family, out Value);
168 return ParseIPAddress(ret, Value);
172 int ret = Interop.ConnectionProfile.SetDnsAddress(_profileHandle, 2, (int)_family, value.ToString());
173 if ((ConnectionError)ret != ConnectionError.None)
175 Log.Error(Globals.LogTag, "It failed to set dns2 address, " + (ConnectionError)ret);
176 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");
177 ConnectionErrorFactory.CheckHandleNullException(ret, (_profileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
178 ConnectionErrorFactory.ThrowConnectionException(ret);
183 public System.Net.IPAddress Gateway
188 int ret = Interop.ConnectionProfile.GetGatewayAddress(_profileHandle, (int)_family, out Value);
189 return ParseIPAddress(ret, Value);
194 int ret = Interop.ConnectionProfile.SetGatewayAddress(_profileHandle, (int)_family, value.ToString());
195 if ((ConnectionError)ret != ConnectionError.None)
197 Log.Error(Globals.LogTag, "It failed to set gateway, " + (ConnectionError)ret);
198 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");
199 ConnectionErrorFactory.CheckHandleNullException(ret, (_profileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
200 ConnectionErrorFactory.ThrowConnectionException(ret);
206 public System.Net.IPAddress SubnetMask
211 int ret = Interop.ConnectionProfile.GetSubnetMask(_profileHandle, (int)_family, out Value);
212 return ParseIPAddress(ret, Value);
217 int ret = Interop.ConnectionProfile.SetSubnetMask(_profileHandle, (int)_family, value.ToString());
218 if ((ConnectionError)ret != ConnectionError.None)
220 Log.Error(Globals.LogTag, "It failed to set subnet mask, " + (ConnectionError)ret);
221 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");
222 ConnectionErrorFactory.CheckHandleNullException(ret, (_profileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
223 ConnectionErrorFactory.ThrowConnectionException(ret);
229 public System.Net.IPAddress IP
234 int ret = Interop.ConnectionProfile.GetIPAddress(_profileHandle, (int)_family, out Value);
235 return ParseIPAddress(ret, Value);
240 int ret = Interop.ConnectionProfile.SetIPAddress(_profileHandle, (int)_family, value.ToString());
241 if ((ConnectionError)ret != ConnectionError.None)
243 Log.Error(Globals.LogTag, "It failed to set ip, " + (ConnectionError)ret);
244 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");
245 ConnectionErrorFactory.CheckHandleNullException(ret, (_profileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
246 ConnectionErrorFactory.ThrowConnectionException(ret);
251 public IPConfigType IPConfigType
256 int ret = Interop.ConnectionProfile.GetIPConfigType(_profileHandle, (int)_family, out Value);
257 if ((ConnectionError)ret != ConnectionError.None)
259 Log.Error(Globals.LogTag, "It failed to get ip config type, " + (ConnectionError)ret);
261 return (IPConfigType)Value;
266 int ret = Interop.ConnectionProfile.SetIPConfigType(_profileHandle, (int)_family, (int)value);
267 if ((ConnectionError)ret != ConnectionError.None)
269 Log.Error(Globals.LogTag, "It failed to set ip config type, " + (ConnectionError)ret);
270 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");
271 ConnectionErrorFactory.CheckHandleNullException(ret, (_profileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
272 ConnectionErrorFactory.ThrowConnectionException(ret);
277 public int PrefixLength
282 int ret = Interop.ConnectionProfile.GetPrefixLength(_profileHandle, (int)_family, out Value);
283 if ((ConnectionError)ret != ConnectionError.None)
285 Log.Error(Globals.LogTag, "It failed to get prefix length, " + (ConnectionError)ret);
292 int ret = Interop.ConnectionProfile.SetPrefixLength(_profileHandle, (int)_family, value);
293 if ((ConnectionError)ret != ConnectionError.None)
295 Log.Error(Globals.LogTag, "It failed to set prefix length, " + (ConnectionError)ret);
296 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");
297 ConnectionErrorFactory.CheckHandleNullException(ret, (_profileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
298 ConnectionErrorFactory.ThrowConnectionException(ret);
303 public DnsConfigType DnsConfigType
308 int ret = Interop.ConnectionProfile.GetDnsConfigType(_profileHandle, (int)_family, out Value);
309 if ((ConnectionError)ret != ConnectionError.None)
311 Log.Error(Globals.LogTag, "It failed to get DNS config type, " + (ConnectionError)ret);
313 return (DnsConfigType)Value;
318 int ret = Interop.ConnectionProfile.SetDnsConfigType(_profileHandle, (int)_family, (int)value);
319 if ((ConnectionError)ret != ConnectionError.None)
321 Log.Error(Globals.LogTag, "It failed to set DNS config type, " + (ConnectionError)ret);
322 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");
323 ConnectionErrorFactory.CheckHandleNullException(ret, (_profileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
324 ConnectionErrorFactory.ThrowConnectionException(ret);
329 public System.Net.IPAddress DhcpServerAddress
334 int ret = Interop.ConnectionProfile.GetDhcpServerAddress(_profileHandle, _family, out dhcpServer);
335 if ((ConnectionError)ret != ConnectionError.None ||
336 dhcpServer == null || dhcpServer.Length == 0)
338 Log.Error(Globals.LogTag, "It failed to get the DHCP server address, " + (ConnectionError)ret);
339 return DefaultIPAddress();
341 return System.Net.IPAddress.Parse(dhcpServer);
345 private System.Net.IPAddress ParseIPAddress(int ret, IntPtr addrPtr)
347 if (ret != (int)ConnectionError.None)
349 Log.Error(Globals.LogTag, "Failed to get address, Error - " + (ConnectionError)ret);
350 return DefaultIPAddress();
353 string addr = Marshal.PtrToStringAnsi(addrPtr);
354 if (addr == null || addr.Length == 0)
355 return DefaultIPAddress();
356 Interop.Glib.Free(addrPtr);
357 return System.Net.IPAddress.Parse(addr);
360 private System.Net.IPAddress DefaultIPAddress()
362 if (_family == AddressFamily.IPv4)
363 return System.Net.IPAddress.Parse(DefaultIPv4);
365 return System.Net.IPAddress.Parse(DefaultIPv6);
368 public int DhcpLeaseDuration
373 int ret = Interop.ConnectionProfile.GetDhcpLeaseDuration(_profileHandle, _family, out leaseDuration);
374 if ((ConnectionError)ret != ConnectionError.None)
376 Log.Error(Globals.LogTag, "It failed to get the DHCP lease duration, " + (ConnectionError)ret);
379 return leaseDuration;