8d69e7e894ec2c65cdbab2834c54dc529b47853a
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.WiFi / Tizen.Network.WiFi / WiFiAddressInformation.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.Runtime.InteropServices;
19 using System.Net;
20 using Tizen.Network.Connection;
21
22 namespace Tizen.Network.WiFi
23 {
24     internal class WiFiAddressInformation : IAddressInformation
25     {
26         private Interop.WiFi.SafeWiFiAPHandle _handle;
27         private AddressFamily _family;
28
29         private const string DefaultIPv4 = "0.0.0.0";
30         private const string DefaultIPv6 = "::";
31
32         internal WiFiAddressInformation(Interop.WiFi.SafeWiFiAPHandle handle, AddressFamily family)
33         {
34             _handle = handle;
35             _family = family;
36         }
37
38         public System.Net.IPAddress Dns1
39         {
40             get
41             {
42                 IntPtr addrPtr;
43                 int ret = Interop.WiFi.AP.GetDnsAddress(_handle, 1, (int)_family, out addrPtr);
44                 return ParseIPAddress(ret, addrPtr);
45             }
46             set
47             {
48                 int ret = Interop.WiFi.AP.SetDnsAddress(_handle, 1, (int)_family, value.ToString());
49                 if (ret != (int)WiFiError.None)
50                 {
51                     Log.Error(Globals.LogTag, "Failed to set first dns address, Error - " + (WiFiError)ret);
52                     WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
53                 }
54             }
55         }
56
57         public System.Net.IPAddress Dns2
58         {
59             get
60             {
61                 IntPtr addrPtr;
62                 int ret = Interop.WiFi.AP.GetDnsAddress(_handle, 2, (int)_family, out addrPtr);
63                 return ParseIPAddress(ret, addrPtr);
64            }
65             set
66             {
67                 int ret = Interop.WiFi.AP.SetDnsAddress(_handle, 2, (int)_family, value.ToString());
68                 if (ret != (int)WiFiError.None)
69                 {
70                     Log.Error(Globals.LogTag, "Failed to set second dns address, Error - " + (WiFiError)ret);
71                     WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
72                 }
73             }
74         }
75
76         public System.Net.IPAddress Gateway
77         {
78             get
79             {
80                 IntPtr addrPtr;
81                 int ret = Interop.WiFi.AP.GetGatewayAddress(_handle, (int)_family, out addrPtr);
82                 return ParseIPAddress(ret, addrPtr);
83             }
84             set
85             {
86                 int ret = Interop.WiFi.AP.SetGatewayAddress(_handle, (int)_family, value.ToString());
87                 if (ret != (int)WiFiError.None)
88                 {
89                     Log.Error(Globals.LogTag, "Failed to set gateway address, Error - " + (WiFiError)ret);
90                     WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
91                 }
92             }
93         }
94
95         public System.Net.IPAddress SubnetMask
96         {
97             get
98             {
99                 IntPtr addrPtr;
100                 int ret = Interop.WiFi.AP.GetSubnetMask(_handle, (int)_family, out addrPtr);
101                 return ParseIPAddress(ret, addrPtr);
102             }
103             set
104             {
105                 int ret = Interop.WiFi.AP.SetSubnetMask(_handle, (int)_family, value.ToString());
106                 if (ret != (int)WiFiError.None)
107                 {
108                     Log.Error(Globals.LogTag, "Failed to set subnet mask, Error - " + (WiFiError)ret);
109                     WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
110                 }
111             }
112         }
113
114         public System.Net.IPAddress IP
115         {
116             get
117             {
118                 IntPtr addrPtr;
119                 int ret = Interop.WiFi.AP.GetIPAddress(_handle, (int)_family, out addrPtr);
120                 return ParseIPAddress(ret, addrPtr);
121             }
122             set
123             {
124                 int ret = Interop.WiFi.AP.SetIPAddress(_handle, (int)_family, value.ToString());
125                 if (ret != (int)WiFiError.None)
126                 {
127                     Log.Error(Globals.LogTag, "Failed to set ip address, Error - " + (WiFiError)ret);
128                     WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
129                 }
130             }
131         }
132
133         public IPConfigType IPConfigType
134         {
135             get
136             {
137                 int type;
138                 int ret = Interop.WiFi.AP.GetIPConfigType(_handle, (int)_family, out type);
139                 if (ret != (int)WiFiError.None)
140                 {
141                     Log.Error(Globals.LogTag, "Failed to get ip config type, Error - " + (WiFiError)ret);
142                 }
143                 return (IPConfigType)type;
144             }
145             set
146             {
147                 int ret = Interop.WiFi.AP.SetIPConfigType(_handle, (int)_family, (int)value);
148                 if (ret != (int)WiFiError.None)
149                 {
150                     Log.Error(Globals.LogTag, "Failed to set ip config type, Error - " + (WiFiError)ret);
151                     WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
152                 }
153             }
154         }
155
156         public int PrefixLength
157         {
158             get
159             {
160                 int Value;
161                 int ret = Interop.WiFi.AP.GetPrefixLength(_handle, (int)_family, out Value);
162                 if (ret != (int)WiFiError.None)
163                 {
164                     Log.Error(Globals.LogTag, "It failed to get prefix length, " + (WiFiError)ret);
165                     return -1;
166                 }
167                 return Value;
168             }
169
170             set
171             {
172                 int ret = Interop.WiFi.AP.SetPrefixLength(_handle, (int)_family, value);
173                 if (ret != (int)WiFiError.None)
174                 {
175                     Log.Error(Globals.LogTag, "It failed to set prefix length, " + (WiFiError)ret);
176                     WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
177                 }
178             }
179         }
180
181         public DnsConfigType DnsConfigType
182         {
183             get
184             {
185                 int Value;
186                 int ret = Interop.WiFi.AP.GetDnsConfigType(_handle, (int)_family, out Value);
187                 if ((WiFiError)ret != WiFiError.None)
188                 {
189                     Log.Error(Globals.LogTag, "It failed to get DNS config type, " + (WiFiError)ret);
190                 }
191                 return (DnsConfigType)Value;
192             }
193             set
194             {
195                 int ret = Interop.WiFi.AP.SetDnsConfigType(_handle, (int)_family, (int)value);
196                 if ((WiFiError)ret != WiFiError.None)
197                 {
198                     Log.Error(Globals.LogTag, "It failed to set DNS config type, " + (WiFiError)ret);
199                     WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
200                 }
201             }
202         }
203
204         /// <summary>
205         /// DHCP server address. It is only supported for IPv4 address family.
206         /// </summary>
207         /// <value>Represents DHCP server address.</value>
208         public System.Net.IPAddress DhcpServerAddress
209         {
210             get
211             {
212                 string dhcpServer;
213                 int ret = Interop.WiFi.AP.GetDhcpServerAddress(_handle, _family, out dhcpServer);
214                 if (ret != (int)WiFiError.None || dhcpServer == null || dhcpServer.Length == 0)
215                 {
216                     Log.Error(Globals.LogTag, "Failed to get DHCP server address, Error - " + (WiFiError)ret);
217                     return DefaultIPAddress();
218                 }
219
220                 return IPAddress.Parse(dhcpServer);
221             }
222         }
223
224         /// <summary>
225         /// DHCP lease duration. It is only supported for IPv4 address family.
226         /// </summary>
227         /// <value>Represents DHCP lease duration.</value>
228         public int  DhcpLeaseDuration
229         {
230             get
231             {
232                 int leaseDuration;
233                 int ret = Interop.WiFi.AP.GetDhcpLeaseDuration(_handle, AddressFamily.IPv4, out leaseDuration);
234                 if (ret != (int)WiFiError.None)
235                 {
236                     Log.Error(Globals.LogTag, "Failed to get DHCP lease duration, Error - " + (WiFiError)ret);
237                     return 0;
238                 }
239
240                 return leaseDuration;
241             }
242         }
243
244         private System.Net.IPAddress ParseIPAddress(int ret, IntPtr addrPtr)
245         {
246             if (ret != (int)WiFiError.None)
247             {
248                 Log.Error(Globals.LogTag, "Failed to get address, Error - " + (WiFiError)ret);
249                 return DefaultIPAddress();
250             }
251
252             string addr = Marshal.PtrToStringAnsi(addrPtr);
253             if (addr == null || addr.Length == 0)
254                 return DefaultIPAddress();
255             Interop.Glib.Free(addrPtr);
256             return System.Net.IPAddress.Parse(addr);
257         }
258
259         private System.Net.IPAddress DefaultIPAddress()
260         {
261             if (_family == AddressFamily.IPv4)
262                 return System.Net.IPAddress.Parse(DefaultIPv4);
263             else
264                 return System.Net.IPAddress.Parse(DefaultIPv6);
265         }
266     }
267 }