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 internal class ConnectionAddressInformation : IAddressInformation
123 private IntPtr _profileHandle;
124 private AddressFamily _family;
126 internal ConnectionAddressInformation(IntPtr handle, AddressFamily family)
128 _profileHandle = handle;
132 public System.Net.IPAddress Dns1
137 int ret = Interop.ConnectionProfile.GetDnsAddress(_profileHandle, 1, (int)_family, out Value);
138 if ((ConnectionError)ret != ConnectionError.None)
140 Log.Error(Globals.LogTag, "It failed to get dns1 address, " + (ConnectionError)ret);
142 string result = Marshal.PtrToStringAnsi(Value);
143 Interop.Libc.Free(Value);
144 if (result == null || result.Length == 0)
145 return System.Net.IPAddress.Parse("0.0.0.0");
146 return System.Net.IPAddress.Parse(result);
151 int ret = Interop.ConnectionProfile.SetDnsAddress(_profileHandle, 1, (int)_family, value.ToString());
152 if ((ConnectionError)ret != ConnectionError.None)
154 Log.Error(Globals.LogTag, "It failed to set dns1 address, " + (ConnectionError)ret);
155 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");
156 ConnectionErrorFactory.CheckHandleNullException(ret, (_profileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
157 ConnectionErrorFactory.ThrowConnectionException(ret);
161 public System.Net.IPAddress Dns2
166 int ret = Interop.ConnectionProfile.GetDnsAddress(_profileHandle, 2, (int)_family, out Value);
167 if ((ConnectionError)ret != ConnectionError.None)
169 Log.Error(Globals.LogTag, "It failed to get dns2 address, " + (ConnectionError)ret);
171 string result = Marshal.PtrToStringAnsi(Value);
172 Interop.Libc.Free(Value);
173 if (result == null || result.Length == 0)
174 return System.Net.IPAddress.Parse("0.0.0.0");
175 return System.Net.IPAddress.Parse(result);
180 int ret = Interop.ConnectionProfile.SetDnsAddress(_profileHandle, 2, (int)_family, value.ToString());
181 if ((ConnectionError)ret != ConnectionError.None)
183 Log.Error(Globals.LogTag, "It failed to set dns2 address, " + (ConnectionError)ret);
184 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");
185 ConnectionErrorFactory.CheckHandleNullException(ret, (_profileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
186 ConnectionErrorFactory.ThrowConnectionException(ret);
191 public System.Net.IPAddress Gateway
196 int ret = Interop.ConnectionProfile.GetGatewayAddress(_profileHandle, (int)_family, out Value);
197 if ((ConnectionError)ret != ConnectionError.None)
199 Log.Error(Globals.LogTag, "It failed to get gateway, " + (ConnectionError)ret);
201 string result = Marshal.PtrToStringAnsi(Value);
202 Interop.Libc.Free(Value);
203 if (result == null || result.Length == 0)
204 return System.Net.IPAddress.Parse("0.0.0.0");
205 return System.Net.IPAddress.Parse(result);
210 int ret = Interop.ConnectionProfile.SetGatewayAddress(_profileHandle, (int)_family, value.ToString());
211 if ((ConnectionError)ret != ConnectionError.None)
213 Log.Error(Globals.LogTag, "It failed to set gateway, " + (ConnectionError)ret);
214 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");
215 ConnectionErrorFactory.CheckHandleNullException(ret, (_profileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
216 ConnectionErrorFactory.ThrowConnectionException(ret);
222 public System.Net.IPAddress SubnetMask
227 int ret = Interop.ConnectionProfile.GetSubnetMask(_profileHandle, (int)_family, out Value);
228 if ((ConnectionError)ret != ConnectionError.None)
230 Log.Error(Globals.LogTag, "It failed to get subnet mask, " + (ConnectionError)ret);
232 string result = Marshal.PtrToStringAnsi(Value);
233 Interop.Libc.Free(Value);
234 if (result == null || result.Length == 0)
235 return System.Net.IPAddress.Parse("0.0.0.0");
236 return System.Net.IPAddress.Parse(result);
241 int ret = Interop.ConnectionProfile.SetSubnetMask(_profileHandle, (int)_family, value.ToString());
242 if ((ConnectionError)ret != ConnectionError.None)
244 Log.Error(Globals.LogTag, "It failed to set subnet mask, " + (ConnectionError)ret);
245 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");
246 ConnectionErrorFactory.CheckHandleNullException(ret, (_profileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
247 ConnectionErrorFactory.ThrowConnectionException(ret);
253 public System.Net.IPAddress IP
258 int ret = Interop.ConnectionProfile.GetIPAddress(_profileHandle, (int)_family, out Value);
259 if ((ConnectionError)ret != ConnectionError.None)
261 Log.Error(Globals.LogTag, "It failed to get ip, " + (ConnectionError)ret);
263 string result = Marshal.PtrToStringAnsi(Value);
264 Interop.Libc.Free(Value);
265 if (result == null || result.Length == 0)
266 return System.Net.IPAddress.Parse("0.0.0.0");
267 return System.Net.IPAddress.Parse(result);
272 int ret = Interop.ConnectionProfile.SetIPAddress(_profileHandle, (int)_family, value.ToString());
273 if ((ConnectionError)ret != ConnectionError.None)
275 Log.Error(Globals.LogTag, "It failed to set ip, " + (ConnectionError)ret);
276 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");
277 ConnectionErrorFactory.CheckHandleNullException(ret, (_profileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
278 ConnectionErrorFactory.ThrowConnectionException(ret);
283 public IPConfigType IPConfigType
288 int ret = Interop.ConnectionProfile.GetIPConfigType(_profileHandle, (int)_family, out Value);
289 if ((ConnectionError)ret != ConnectionError.None)
291 Log.Error(Globals.LogTag, "It failed to get ip config type, " + (ConnectionError)ret);
293 return (IPConfigType)Value;
298 int ret = Interop.ConnectionProfile.SetIPConfigType(_profileHandle, (int)_family, (int)value);
299 if ((ConnectionError)ret != ConnectionError.None)
301 Log.Error(Globals.LogTag, "It failed to set ip config type, " + (ConnectionError)ret);
302 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");
303 ConnectionErrorFactory.CheckHandleNullException(ret, (_profileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
304 ConnectionErrorFactory.ThrowConnectionException(ret);
309 public int PrefixLength
314 int ret = Interop.ConnectionProfile.GetPrefixLength(_profileHandle, (int)_family, out Value);
315 if ((ConnectionError)ret != ConnectionError.None)
317 Log.Error(Globals.LogTag, "It failed to get prefix length, " + (ConnectionError)ret);
324 int ret = Interop.ConnectionProfile.SetPrefixLength(_profileHandle, (int)_family, value);
325 if ((ConnectionError)ret != ConnectionError.None)
327 Log.Error(Globals.LogTag, "It failed to set prefix length, " + (ConnectionError)ret);
328 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");
329 ConnectionErrorFactory.CheckHandleNullException(ret, (_profileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
330 ConnectionErrorFactory.ThrowConnectionException(ret);
335 public DnsConfigType DnsConfigType
340 int ret = Interop.ConnectionProfile.GetDnsConfigType(_profileHandle, (int)_family, out Value);
341 if ((ConnectionError)ret != ConnectionError.None)
343 Log.Error(Globals.LogTag, "It failed to get DNS config type, " + (ConnectionError)ret);
345 return (DnsConfigType)Value;
350 int ret = Interop.ConnectionProfile.SetDnsConfigType(_profileHandle, (int)_family, (int)value);
351 if ((ConnectionError)ret != ConnectionError.None)
353 Log.Error(Globals.LogTag, "It failed to set DNS config type, " + (ConnectionError)ret);
354 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");
355 ConnectionErrorFactory.CheckHandleNullException(ret, (_profileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
356 ConnectionErrorFactory.ThrowConnectionException(ret);
361 public System.Net.IPAddress DhcpServerAddress
366 int ret = Interop.ConnectionProfile.GetDhcpServerAddress(_profileHandle, _family, out dhcpServer);
367 if ((ConnectionError)ret != ConnectionError.None)
369 Log.Error(Globals.LogTag, "It failed to get the DHCP server address, " + (ConnectionError)ret);
372 if (dhcpServer == null || dhcpServer.Length == 0)
374 return System.Net.IPAddress.Parse("0.0.0.0");
379 return System.Net.IPAddress.Parse(dhcpServer);