change version to 1.2.2.0
[platform/framework/native/net.git] / src / FNet_DnsImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 /**
18  * @file                FNet_DnsImpl.cpp
19  * @brief               This is the implementation for the  _DnsImpl class.
20  */
21
22 #include <FNetNetConnection.h>
23 #include <FNetDns.h>
24 #include <FNetIDnsEventListener.h>
25 #include <FNetManagedNetConnection.h>
26 #include <FNetNetConnectionManager.h>
27 #include <FBaseSysLog.h>
28 #include <FBase_StringConverter.h>
29 #include "FNet_NetTypes.h"
30 #include "FNet_ManagedNetConnectionImpl.h"
31 #include "FNet_NetConnectionManagerImpl.h"
32 #include "FNet_DnsImpl.h"
33 #include "FNet_DnsEvent.h"
34 #include "FNet_DnsManagedNetConnectionEventListener.h"
35 #include "FNet_DnsRequestHandler.h"
36
37 using namespace std;
38 using namespace Tizen::Base;
39 using namespace Tizen::Base::Collection;
40 using namespace Tizen::Base::Utility;
41
42 namespace Tizen { namespace Net
43 {
44
45 _DnsImpl::_DnsImpl(void)
46         : __pNetConnection(null)
47         , __pDnsEvent(null)
48         , __pDnsRequestHandlerList(null)
49         , __pManagedNetConnectionEventListener(null)
50         , __pManagedNetConnection(null)
51 {
52 }
53
54 _DnsImpl::~_DnsImpl(void)
55 {
56         if (__pManagedNetConnection != null)
57         {
58                 __pManagedNetConnection->SetManagedNetConnectionEventListener(null);
59         }
60 }
61
62 result
63 _DnsImpl::Construct(const NetConnection& netConnection, IDnsEventListener& listener)
64 {
65         result r = E_SUCCESS;
66         NetConnectionState netConnectionState = NET_CONNECTION_STATE_NONE;
67
68         SysAssertf(__pDnsEvent == null,
69                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
70
71         netConnectionState = netConnection.GetConnectionState();
72         SysTryReturnResult(NID_NET, (netConnectionState == NET_CONNECTION_STATE_STARTED ||              
73                         netConnectionState == NET_CONNECTION_STATE_RESUMED ||
74                         netConnectionState == NET_CONNECTION_STATE_SUSPENDED),
75                          E_INVALID_CONNECTION, "Network Connection is not established.");
76
77         unique_ptr<_DnsEvent> pDnsEvent(new (std::nothrow) _DnsEvent());
78         SysTryReturnResult(NID_NET, pDnsEvent != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
79
80         r = pDnsEvent->Construct();
81         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
82
83         r = pDnsEvent->AddListener(listener, true);
84         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
85
86         unique_ptr<ArrayList, _CollectionDeleter> pDnsRequestHandlerList(new (std::nothrow) ArrayList());
87         SysTryReturnResult(NID_NET, pDnsRequestHandlerList != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
88
89         r = pDnsRequestHandlerList->Construct();
90         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
91
92         __pNetConnection = const_cast <NetConnection*>(&netConnection);
93         __pDnsEvent = move(pDnsEvent);
94         __pDnsRequestHandlerList = move(pDnsRequestHandlerList);
95
96         return r;
97 }
98
99 result
100 _DnsImpl::Construct(IDnsEventListener& listener)
101 {
102         result r = E_SUCCESS;
103
104         SysAssertf(__pDnsEvent == null,
105                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
106
107         unique_ptr<_DnsEvent> pDnsEvent(new (std::nothrow) _DnsEvent());
108         SysTryReturnResult(NID_NET, pDnsEvent != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
109
110         r = pDnsEvent->Construct();
111         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
112
113         r = pDnsEvent->AddListener(listener, true);
114         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
115
116         unique_ptr<ArrayList, _CollectionDeleter> pDnsRequestHandlerList(new (std::nothrow) ArrayList());
117         SysTryReturnResult(NID_NET, pDnsRequestHandlerList != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
118
119         r = pDnsRequestHandlerList->Construct();
120         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
121
122         //check for default mode
123         if (!_NetConnectionManagerImpl::IsDefaultMode())
124         {
125                 SysLog(NID_NET, "DNS uses ManagedNetConnection(Non-default mode)");
126
127                 _NetConnectionManagerImpl* pConnectionManagerImpl = _NetConnectionManagerImpl::GetInstance();
128                 SysTryReturnResult(NID_NET, pConnectionManagerImpl != null, E_SYSTEM,
129                                 "A system error has been occurred. Network framework is not ready.");
130
131                 unique_ptr<_DnsManagedNetConnectionEventListener> pManagedNetConnectionEventListener(new (std::nothrow) _DnsManagedNetConnectionEventListener(pDnsRequestHandlerList.get()));
132                 SysTryReturnResult(NID_NET, pManagedNetConnectionEventListener != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
133
134                 unique_ptr<ManagedNetConnection> pManagedNetConnection(pConnectionManagerImpl->GetManagedNetConnectionN());
135                 SysTryReturnResult(NID_NET, pManagedNetConnection != null, E_INVALID_CONNECTION, "ManagedNetConnection is not found.");
136
137                 _ManagedNetConnectionImpl* pManagedNCImpl= _ManagedNetConnectionImpl::GetInstance(*pManagedNetConnection);
138                 SysTryReturnResult(NID_NET, pManagedNCImpl != null, E_INVALID_CONNECTION, "_ManagedNetConnectionImpl is null.");
139
140                 r = pManagedNetConnection->SetManagedNetConnectionEventListener(pManagedNetConnectionEventListener.get());
141                 SysTryReturnResult(NID_NET, r == E_SUCCESS, E_INVALID_CONNECTION, "Failed to set ManagedNetConnectionEventListener.");
142
143                 r = pManagedNCImpl->Start();
144                 SysTryReturnResult(NID_NET, r == E_SUCCESS, E_INVALID_CONNECTION, "Failed to start ManagedNetConnection.");
145
146                 NetConnectionState connState = pManagedNCImpl->GetConnectionState();
147                 if (connState == NET_CONNECTION_STATE_STARTED || connState == NET_CONNECTION_STATE_RESUMED || connState == NET_CONNECTION_STATE_SUSPENDED)
148                 {
149                         SysLog(NID_NET, "ManagedNetConnection is already connected.");
150                         pManagedNetConnectionEventListener->__isConnected = true;
151                 }
152
153                 __pManagedNetConnectionEventListener = move(pManagedNetConnectionEventListener);
154                 __pManagedNetConnection = move(pManagedNetConnection);
155         }
156
157         __pDnsEvent = move(pDnsEvent);
158         __pDnsRequestHandlerList = move(pDnsRequestHandlerList);
159
160         return r;
161 }
162
163 result
164 _DnsImpl::GetHostByName(const String& hostName)
165 {
166         result r = E_SUCCESS;
167
168         SysAssertf(__pDnsEvent != null, "Not yet constructed. Construct() should be called before use.");
169
170         SysTryReturnResult(NID_NET, !hostName.IsEmpty(), E_INVALID_ARG, "Invalid argument is used. hostName is an empty string.");
171
172         ByteBuffer* pByteBuff = null;
173
174         //Check if proper utf8 string, if fails, return
175         pByteBuff = StringUtil::StringToUtf8N(hostName);
176         SysTryReturnResult(NID_NET, GetLastResult() != E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "Propagating.");
177         SysTryReturnResult(NID_NET, pByteBuff != null, E_INVALID_ARG, "Invalid argument is used. hostName=%ls.", hostName.GetPointer());
178         delete pByteBuff;
179
180         unique_ptr<char[]> pHostName(_StringConverter::CopyToCharArrayN(hostName));
181         SysTryReturnResult(NID_NET, pHostName != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
182
183         // create a request to be handled asynchronously
184         unique_ptr<_DnsRequest> pDnsRequest(new (std::nothrow) _DnsRequest(NET_DNS_EVENT_HOSTNAME, pHostName.get(), *__pDnsEvent));
185         SysTryReturnResult(NID_NET, pDnsRequest != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
186
187         // Ownership is transferred to pDnsRequest
188         pHostName.release();
189
190         // create request handler and pass request to handle
191         unique_ptr<_DnsRequestHandler> pDnsRequestHandler(new (std::nothrow) _DnsRequestHandler(pDnsRequest.get()));
192         SysTryReturnResult(NID_NET, pDnsRequestHandler != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
193
194         // Ownership is transferred to pDnsRequestHandler
195         pDnsRequest.release();
196
197         r = pDnsRequestHandler->Construct();
198         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
199
200         r = __pDnsRequestHandlerList->Add(*pDnsRequestHandler);
201         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
202
203         // if current network is not Custom NetConnection, or ManagedNetConnection is not already started.
204         if (__pManagedNetConnection != null &&  !__pManagedNetConnectionEventListener->__isConnected)
205         {
206                 SysLog(NID_NET, "Waiting for ManagedNetConnection started event");      
207         }
208         else
209         {
210                 r = pDnsRequestHandler->Start();
211                 SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
212         }
213
214         pDnsRequestHandler.release();
215
216         return r;
217 }
218
219 result
220 _DnsImpl::GetHostByAddress(const IpAddress& ipAddress)
221 {
222         result r = E_SUCCESS;
223
224         SysAssertf(__pDnsEvent != null, "Not yet constructed. Construct() should be called before use.");
225
226         SysTryReturnResult(NID_NET, !ipAddress.ToString().IsEmpty(), E_INVALID_ARG,
227                         "Invalid argument is used. IpAddress is empty.");
228
229         SysTryReturnResult(NID_NET, ipAddress.GetNetAddressFamily() == NET_AF_IPV4, E_INVALID_ARG, 
230                         "Invalid argument is used. IPv4 only supported.");
231
232         unique_ptr<char[]> pIpAddr(_StringConverter::CopyToCharArrayN(ipAddress.ToString()));
233         SysTryReturnResult(NID_NET, pIpAddr != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
234
235         unique_ptr<_DnsRequest> pDnsRequest(new (std::nothrow) _DnsRequest(NET_DNS_EVENT_ADDRESS, pIpAddr.get(), *__pDnsEvent));
236         SysTryReturnResult(NID_NET, pDnsRequest != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
237
238         // Ownership is transferred to pDnsRequest
239         pIpAddr.release();
240
241         // create request handler and pass request to handle
242         unique_ptr<_DnsRequestHandler> pDnsRequestHandler(new (std::nothrow) _DnsRequestHandler(pDnsRequest.get()));
243         SysTryReturnResult(NID_NET, pDnsRequestHandler != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
244
245         // Ownership is transferred to pDnsRequestHandler
246         pDnsRequest.release();
247
248         r = pDnsRequestHandler->Construct();
249         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
250
251         r = __pDnsRequestHandlerList->Add(*pDnsRequestHandler);
252         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
253
254         // if current network is not Custom NetConnection, or ManagedNetConnection is not already started.
255         if (__pManagedNetConnection != null &&  !__pManagedNetConnectionEventListener->__isConnected)
256         {
257                 SysLog(NID_NET, "Waiting for ManagedNetConnection started event");      
258         }
259         else
260         {
261                 r = pDnsRequestHandler->Start();
262                 SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
263         }
264
265         pDnsRequestHandler.release();
266
267         return r;
268 }
269
270 _DnsImpl*
271 _DnsImpl::GetInstance(Dns& dns)
272 {
273         return dns.__pDnsImpl;
274 }
275
276 const _DnsImpl*
277 _DnsImpl::GetInstance(const Dns& dns)
278 {
279         return dns.__pDnsImpl;
280 }
281
282 } } // Tizen::Net