Merge "Add the exception handling when using manual cert mode" into tizen_2.1
[platform/framework/native/net.git] / src / FNetDns.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                        FNetDns.cpp
19  * @brief               This is the implementation for the Dns class.
20  */
21
22 #include <unique_ptr.h>
23 #include <FNetDns.h>
24 #include <FNetIpAddress.h>
25 #include <FNetNetConnection.h>
26 #include <FNetIDnsEventListener.h>
27 #include <FBaseSysLog.h>
28 #include <FSec_AccessController.h>
29 #include "FNet_DnsImpl.h"
30
31 using namespace std;
32 using namespace Tizen::Base;
33 using namespace Tizen::Security;
34
35 namespace Tizen { namespace Net
36 {
37
38 Dns::Dns(void)
39         : __pDnsImpl(null)
40 {
41 }
42
43 Dns::~Dns(void)
44 {
45         delete __pDnsImpl;
46         __pDnsImpl = null;
47 }
48
49 result
50 Dns::Construct(const NetConnection& netConnection, IDnsEventListener& listener)
51 {
52         result r = E_SUCCESS;
53
54         SysAssertf(__pDnsImpl == null,
55                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
56
57         unique_ptr<_DnsImpl> pDnsImpl(new (std::nothrow) _DnsImpl());
58         SysTryReturnResult(NID_NET, pDnsImpl != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
59
60         r = pDnsImpl->Construct(netConnection, listener);
61         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
62
63         __pDnsImpl = pDnsImpl.release();
64
65         return r;
66 }
67
68 result
69 Dns::Construct(IDnsEventListener& listener)
70 {
71         result r = E_SUCCESS;
72
73         SysAssertf(__pDnsImpl == null,
74                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
75
76         unique_ptr<_DnsImpl> pDnsImpl(new (std::nothrow) _DnsImpl());
77         SysTryReturnResult(NID_NET, pDnsImpl != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
78
79         r = pDnsImpl->Construct(listener);
80         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
81
82         __pDnsImpl = pDnsImpl.release();
83
84         return r;
85 }
86
87 result
88 Dns::GetHostByName(const String& hostName)
89 {
90         result r = E_SUCCESS;
91
92         r = _AccessController::CheckUserPrivilege(_PRV_DNS);
93         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED, E_OUT_OF_MEMORY);
94         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "The application is not permitted to call this method.");
95
96         SysAssertf(__pDnsImpl != null, "Not yet constructed. Construct() should be called before use.");
97
98         r = __pDnsImpl->GetHostByName(hostName);
99         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
100
101         return r;
102 }
103
104 result
105 Dns::GetHostByAddress(const IpAddress& ipAddress)
106 {
107         result r = E_SUCCESS;
108
109         r = _AccessController::CheckUserPrivilege(_PRV_DNS);
110         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED, E_OUT_OF_MEMORY);
111         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "The application is not permitted to call this method.");
112
113         SysAssertf(__pDnsImpl != null, "Not yet constructed. Construct() should be called before use.");
114
115         r = __pDnsImpl->GetHostByAddress(ipAddress);
116         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
117
118         return r;
119 }
120
121 } }  // Tizen::Net