Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.Connection / Tizen.Network.Connection / IAddressInformation.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 using System;
18 using System.Collections.Generic;
19 using System.Linq;
20 using System.Text;
21 using System.Threading.Tasks;
22 using System.Collections;
23 using System.Runtime.InteropServices;
24
25 namespace Tizen.Network.Connection
26 {
27     /// <summary>
28     /// This interface provides properties to manage address information of the connection.
29     /// </summary>
30     /// <since_tizen> 3 </since_tizen>
31     public interface IAddressInformation
32     {
33         /// <summary>
34         /// The DNS address.
35         /// </summary>
36         /// <since_tizen> 3 </since_tizen>
37         /// <value>First DNS address of the connection.</value>
38         /// <exception cref="System.NotSupportedException">Thrown during set when feature is not supported.</exception>
39         /// <exception cref="System.ArgumentException">Thrown during set when value is invalid parameter.</exception>
40         /// <exception cref="System.InvalidOperationException">Thrown during set when profile instance is invalid or when method failed due to invalid operation.</exception>
41         System.Net.IPAddress Dns1 { get; set; }
42
43         /// <summary>
44         /// The DNS address.
45         /// </summary>
46         /// <since_tizen> 3 </since_tizen>
47         /// <value>Second DNS address of the connection.</value>
48         /// <exception cref="System.NotSupportedException">Thrown during set when feature is not supported.</exception>
49         /// <exception cref="System.ArgumentException">Thrown during set when value is invalid parameter.</exception>
50         /// <exception cref="System.InvalidOperationException">Thrown during set when profile instance is invalid or when method failed due to invalid operation.</exception>
51         System.Net.IPAddress Dns2 { get; set; }
52
53         /// <summary>
54         /// The gateway address.
55         /// </summary>
56         /// <since_tizen> 3 </since_tizen>
57         /// <value>Gateway address of the connection.</value>
58         /// <exception cref="System.NotSupportedException">Thrown during set when feature is not supported.</exception>
59         /// <exception cref="System.ArgumentException">Thrown during set when value is invalid parameter.</exception>
60         /// <exception cref="System.InvalidOperationException">Thrown during set when profile instance is invalid or when method failed due to invalid operation.</exception>
61         System.Net.IPAddress Gateway { get; set; }
62
63         /// <summary>
64         /// The subnet mask address.
65         /// </summary>
66         /// <since_tizen> 3 </since_tizen>
67         /// <value>Subnet mask of the connection.</value>
68         /// <exception cref="System.NotSupportedException">Thrown during set when feature is not supported.</exception>
69         /// <exception cref="System.ArgumentException">Thrown during set when value is invalid parameter.</exception>
70         /// <exception cref="System.InvalidOperationException">Thrown during set when profile instance is invalid or when method failed due to invalid operation.</exception>
71         System.Net.IPAddress SubnetMask { get; set; }
72
73         /// <summary>
74         /// The IP address.
75         /// </summary>
76         /// <since_tizen> 3 </since_tizen>
77         /// <value>IP address of the connection.</value>
78         /// <exception cref="System.NotSupportedException">Thrown during set when feature is not supported.</exception>
79         /// <exception cref="System.ArgumentException">Thrown during set when value is invalid parameter.</exception>
80         /// <exception cref="System.InvalidOperationException">Thrown during set when profile instance is invalid or when method failed due to invalid operation.</exception>
81         System.Net.IPAddress IP { get; set; }
82
83         /// <summary>
84         /// The type of IP config.
85         /// </summary>
86         /// <since_tizen> 3 </since_tizen>
87         /// <value>IP config type of the connection.</value>
88         /// <exception cref="System.NotSupportedException">Thrown during set when feature is not supported.</exception>
89         /// <exception cref="System.ArgumentException">Thrown during set when value is invalid parameter.</exception>
90         /// <exception cref="System.InvalidOperationException">Thrown during set when profile instance is invalid or when method failed due to invalid operation.</exception>
91         IPConfigType IPConfigType { get; set; }
92
93         /// <summary>
94         /// The prefix length.
95         /// </summary>
96         /// <since_tizen> 3 </since_tizen>
97         /// <value>Prefix length of the connection.</value>
98         /// <exception cref="System.NotSupportedException">Thrown during set when feature is not supported.</exception>
99         /// <exception cref="System.ArgumentException">Thrown during set when value is invalid parameter.</exception>
100         /// <exception cref="System.InvalidOperationException">Thrown during set when profile instance is invalid or when method failed due to invalid operation.</exception>
101         int PrefixLength { get; set; }
102
103         /// <summary>
104         /// The DNS config type.
105         /// </summary>
106         /// <since_tizen> 3 </since_tizen>
107         /// <value>Config type of the DNS.</value>
108         /// <exception cref="System.NotSupportedException">Thrown during set when feature is not supported.</exception>
109         /// <exception cref="System.ArgumentException">Thrown during set when value is invalid parameter.</exception>
110         /// <exception cref="System.InvalidOperationException">Thrown during set when profile instance is invalid or when method failed due to invalid operation.</exception>
111         DnsConfigType DnsConfigType { get; set; }
112
113         /// <summary>
114         /// The DHCP server address. It is only supported for IPV4 address family.
115         /// </summary>
116         /// <value>Server address of the DHCP.</value>
117         System.Net.IPAddress DhcpServerAddress { get; }
118     }
119
120     internal class ConnectionAddressInformation : IAddressInformation
121     {
122         private IntPtr _profileHandle;
123         private AddressFamily _family;
124
125         internal ConnectionAddressInformation(IntPtr handle, AddressFamily family)
126         {
127             _profileHandle = handle;
128             _family = family;
129         }
130
131         public System.Net.IPAddress Dns1
132         {
133             get
134             {
135                 IntPtr Value;
136                 int ret = Interop.ConnectionProfile.GetDnsAddress(_profileHandle, 1, (int)_family, out Value);
137                 if ((ConnectionError)ret != ConnectionError.None)
138                 {
139                     Log.Error(Globals.LogTag, "It failed to get dns1 address, " + (ConnectionError)ret);
140                 }
141                 string result = Marshal.PtrToStringAnsi(Value);
142                 Interop.Libc.Free(Value);
143                 if (result == null)
144                     return System.Net.IPAddress.Parse("0.0.0.0");
145                 return System.Net.IPAddress.Parse(result);
146             }
147
148             set
149             {
150                 int ret = Interop.ConnectionProfile.SetDnsAddress(_profileHandle, 1, (int)_family, value.ToString());
151                 if ((ConnectionError)ret != ConnectionError.None)
152                 {
153                     Log.Error(Globals.LogTag, "It failed to set dns1 address, " + (ConnectionError)ret);
154                     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");
155                     ConnectionErrorFactory.CheckHandleNullException(ret, (_profileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
156                     ConnectionErrorFactory.ThrowConnectionException(ret);
157                 }
158             }
159         }
160         public System.Net.IPAddress Dns2
161         {
162             get
163             {
164                 IntPtr Value;
165                 int ret = Interop.ConnectionProfile.GetDnsAddress(_profileHandle, 2, (int)_family, out Value);
166                 if ((ConnectionError)ret != ConnectionError.None)
167                 {
168                     Log.Error(Globals.LogTag, "It failed to get dns2 address, " + (ConnectionError)ret);
169                 }
170                 string result = Marshal.PtrToStringAnsi(Value);
171                 Interop.Libc.Free(Value);
172                 if (result == null)
173                     return System.Net.IPAddress.Parse("0.0.0.0");
174                 return System.Net.IPAddress.Parse(result);
175             }
176
177             set
178             {
179                 int ret = Interop.ConnectionProfile.SetDnsAddress(_profileHandle, 2, (int)_family, value.ToString());
180                 if ((ConnectionError)ret != ConnectionError.None)
181                 {
182                     Log.Error(Globals.LogTag, "It failed to set dns2 address, " + (ConnectionError)ret);
183                     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");
184                     ConnectionErrorFactory.CheckHandleNullException(ret, (_profileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
185                     ConnectionErrorFactory.ThrowConnectionException(ret);
186                 }
187             }
188         }
189
190         public System.Net.IPAddress Gateway
191         {
192             get
193             {
194                 IntPtr Value;
195                 int ret = Interop.ConnectionProfile.GetGatewayAddress(_profileHandle, (int)_family, out Value);
196                 if ((ConnectionError)ret != ConnectionError.None)
197                 {
198                     Log.Error(Globals.LogTag, "It failed to get gateway, " + (ConnectionError)ret);
199                 }
200                 string result = Marshal.PtrToStringAnsi(Value);
201                 Interop.Libc.Free(Value);
202                 if (result == null)
203                     return System.Net.IPAddress.Parse("0.0.0.0");
204                 return System.Net.IPAddress.Parse(result);
205             }
206
207             set
208             {
209                 int ret = Interop.ConnectionProfile.SetGatewayAddress(_profileHandle, (int)_family, value.ToString());
210                 if ((ConnectionError)ret != ConnectionError.None)
211                 {
212                     Log.Error(Globals.LogTag, "It failed to set gateway, " + (ConnectionError)ret);
213                     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");
214                     ConnectionErrorFactory.CheckHandleNullException(ret, (_profileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
215                     ConnectionErrorFactory.ThrowConnectionException(ret);
216                 }
217             }
218         }
219
220
221         public System.Net.IPAddress SubnetMask
222         {
223             get
224             {
225                 IntPtr Value;
226                 int ret = Interop.ConnectionProfile.GetSubnetMask(_profileHandle, (int)_family, out Value);
227                 if ((ConnectionError)ret != ConnectionError.None)
228                 {
229                     Log.Error(Globals.LogTag, "It failed to get subnet mask, " + (ConnectionError)ret);
230                 }
231                 string result = Marshal.PtrToStringAnsi(Value);
232                 Interop.Libc.Free(Value);
233                 if (result == null)
234                     return System.Net.IPAddress.Parse("0.0.0.0");
235                 return System.Net.IPAddress.Parse(result);
236             }
237
238             set
239             {
240                 int ret = Interop.ConnectionProfile.SetSubnetMask(_profileHandle, (int)_family, value.ToString());
241                 if ((ConnectionError)ret != ConnectionError.None)
242                 {
243                     Log.Error(Globals.LogTag, "It failed to set subnet mask, " + (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);
247                 }
248             }
249         }
250
251
252         public System.Net.IPAddress IP
253         {
254             get
255             {
256                 IntPtr Value;
257                 int ret = Interop.ConnectionProfile.GetIPAddress(_profileHandle, (int)_family, out Value);
258                 if ((ConnectionError)ret != ConnectionError.None)
259                 {
260                     Log.Error(Globals.LogTag, "It failed to get ip, " + (ConnectionError)ret);
261                 }
262                 string result = Marshal.PtrToStringAnsi(Value);
263                 Interop.Libc.Free(Value);
264                 if (result == null)
265                     return System.Net.IPAddress.Parse("0.0.0.0");
266                 return System.Net.IPAddress.Parse(result);
267             }
268
269             set
270             {
271                 int ret = Interop.ConnectionProfile.SetIPAddress(_profileHandle, (int)_family, value.ToString());
272                 if ((ConnectionError)ret != ConnectionError.None)
273                 {
274                     Log.Error(Globals.LogTag, "It failed to set ip, " + (ConnectionError)ret);
275                     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");
276                     ConnectionErrorFactory.CheckHandleNullException(ret, (_profileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
277                     ConnectionErrorFactory.ThrowConnectionException(ret);
278                 }
279             }
280         }
281
282         public IPConfigType IPConfigType
283         {
284             get
285             {
286                 int Value;
287                 int ret = Interop.ConnectionProfile.GetIPConfigType(_profileHandle, (int)_family, out Value);
288                 if ((ConnectionError)ret != ConnectionError.None)
289                 {
290                     Log.Error(Globals.LogTag, "It failed to get ip config type, " + (ConnectionError)ret);
291                 }
292                 return (IPConfigType)Value;
293             }
294
295             set
296             {
297                 int ret = Interop.ConnectionProfile.SetIPConfigType(_profileHandle, (int)_family, (int)value);
298                 if ((ConnectionError)ret != ConnectionError.None)
299                 {
300                     Log.Error(Globals.LogTag, "It failed to set ip config type, " + (ConnectionError)ret);
301                     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");
302                     ConnectionErrorFactory.CheckHandleNullException(ret, (_profileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
303                     ConnectionErrorFactory.ThrowConnectionException(ret);
304                 }
305             }
306         }
307
308         public int PrefixLength
309         {
310             get
311             {
312                 int Value;
313                 int ret = Interop.ConnectionProfile.GetPrefixLength(_profileHandle, (int)_family, out Value);
314                 if ((ConnectionError)ret != ConnectionError.None)
315                 {
316                     Log.Error(Globals.LogTag, "It failed to get prefix length, " + (ConnectionError)ret);
317                 }
318                 return Value;
319             }
320
321             set
322             {
323                 int ret = Interop.ConnectionProfile.SetPrefixLength(_profileHandle, (int)_family, value);
324                 if ((ConnectionError)ret != ConnectionError.None)
325                 {
326                     Log.Error(Globals.LogTag, "It failed to set prefix length, " + (ConnectionError)ret);
327                     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");
328                     ConnectionErrorFactory.CheckHandleNullException(ret, (_profileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
329                     ConnectionErrorFactory.ThrowConnectionException(ret);
330                 }
331             }
332         }
333
334         public DnsConfigType DnsConfigType
335         {
336             get
337             {
338                 int Value;
339                 int ret = Interop.ConnectionProfile.GetDnsConfigType(_profileHandle, (int)_family, out Value);
340                 if ((ConnectionError)ret != ConnectionError.None)
341                 {
342                     Log.Error(Globals.LogTag, "It failed to get DNS config type, " + (ConnectionError)ret);
343                 }
344                 return (DnsConfigType)Value;
345             }
346
347             set
348             {
349                 int ret = Interop.ConnectionProfile.SetDnsConfigType(_profileHandle, (int)_family, (int)value);
350                 if ((ConnectionError)ret != ConnectionError.None)
351                 {
352                     Log.Error(Globals.LogTag, "It failed to set DNS config type, " + (ConnectionError)ret);
353                     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");
354                     ConnectionErrorFactory.CheckHandleNullException(ret, (_profileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
355                     ConnectionErrorFactory.ThrowConnectionException(ret);
356                 }
357             }
358         }
359
360         public System.Net.IPAddress DhcpServerAddress
361         {
362             get
363             {
364                 string dhcpServer;
365                 int ret = Interop.ConnectionProfile.GetDhcpServerAddress(_profileHandle, _family, out dhcpServer);
366                 if ((ConnectionError)ret != ConnectionError.None)
367                 {
368                     Log.Error(Globals.LogTag, "It failed to get the DHCP server address, " + (ConnectionError)ret);
369                 }
370
371                 if (dhcpServer == null)
372                 {
373                     return System.Net.IPAddress.Parse("0.0.0.0");
374                 }
375
376                 else
377                 {
378                     return System.Net.IPAddress.Parse(dhcpServer);
379                 }
380             }
381         }
382     }
383 }