Merge "Add the exception handling when using manual cert mode" into tizen_2.1
[platform/framework/native/net.git] / src / FNet_Ip4AddressImpl.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_Ip4AddressImpl.cpp
19  * @brief               This is the implementation for the _Ip4AddressImpl class.
20  */
21
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
25 #include <unique_ptr.h>
26 #include <FBaseObject.h>
27 #include <FBaseString.h>
28 #include <FBaseResult.h>
29 #include <FBaseUtilStringTokenizer.h>
30 #include <FBaseUtilStringUtil.h>
31 #include <FNetNetTypes.h>
32 #include <FNetIp4Address.h>
33 #include <FBaseSysLog.h>
34 #include <FBase_StringConverter.h>
35 #include "FNet_NetTypes.h"
36 #include "FNet_Ip4AddressImpl.h"
37
38 using namespace std;
39 using namespace Tizen::Base;
40 using namespace Tizen::Base::Utility;
41 using namespace Tizen::Net::Sockets;
42
43 namespace Tizen { namespace Net {
44
45 static const int _IP4_ADDRESS_LENGTH = sizeof(unsigned long);
46 static const unsigned long _IP4_ADDRESS_NONE = 0xFFFFFFFF;
47 static const wchar_t _VALID_IP4_ADDRESS_DELIMITER[] = L".";
48
49 _Ip4AddressImpl::_Ip4AddressImpl(void)
50         : __address(_IP4_ADDRESS_NONE)
51         , __addressInTextualFormat()
52         , __isValid(false)
53 {
54 }
55
56 _Ip4AddressImpl::_Ip4AddressImpl(const String& ipAddr)
57         : __address(_IP4_ADDRESS_NONE)
58         , __addressInTextualFormat()
59         , __isValid(false)
60 {
61         ClearLastResult();
62
63         result r = E_SUCCESS;
64         int ret = 0;
65         struct in_addr in;
66
67         SysTryReturnVoidResult(NID_NET, ipAddr.GetLength() > 0, E_INVALID_ARG,
68                         "[%s] Invalid argument is used. The length of Ip4Address must be longer than 0.", GetErrorMessage(E_INVALID_ARG));
69         SysTryReturnVoidResult(NID_NET, ipAddr.GetLength() < _MAX_IPV4_ADDRESS_STRING_LENGTH, E_INVALID_ARG,
70                         "[%s] Invalid argument is used. The length of Ip4Address is longer than the maximum length.", GetErrorMessage(E_INVALID_ARG));
71
72         StringTokenizer tokens(ipAddr, _VALID_IP4_ADDRESS_DELIMITER);
73         SysTryReturnVoidResult(NID_NET, (tokens.GetTokenCount() == 1) || (tokens.GetTokenCount() == 4), E_INVALID_ARG,
74                         "[%s] Invalid argument is used. Ip4Address is in an invalid format. ipAddr=%ls", GetErrorMessage(E_INVALID_ARG), ipAddr.GetPointer());
75
76         while (tokens.HasMoreTokens())
77         {
78                 String token;
79                 int value = 0;
80
81                 tokens.GetNextToken(token);
82
83                 r = Integer::Parse(token, value);
84                 SysTryReturnVoidResult(NID_NET, r == E_SUCCESS, E_INVALID_ARG,
85                                 "[%s] Invalid argument is used. Ip4Address contains a number that cannot be parsed.", GetErrorMessage(E_INVALID_ARG), ipAddr.GetPointer());
86                 SysTryReturnVoidResult(NID_NET, (value >= 0) && (value <= 255), E_INVALID_ARG,
87                                 "[%s] Invalid argument is used. Each value of Ip4Address must be in the range of 0 to 255.", GetErrorMessage(E_INVALID_ARG));
88         }
89
90         unique_ptr<char[]> pIpAddr(_StringConverter::CopyToCharArrayN(ipAddr));
91         SysTryReturnVoidResult(NID_NET, pIpAddr != null, E_OUT_OF_MEMORY,
92                         "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
93
94         ret = inet_aton(pIpAddr.get(), &in);
95         SysTryReturnVoidResult(NID_NET, ret != 0, E_INVALID_ARG,
96                         "[%s] Invalid argument is used. Ip4Address=%ls", GetErrorMessage(E_INVALID_ARG), ipAddr.GetPointer());
97
98         __address = ntohl(in.s_addr);
99         __addressInTextualFormat = ipAddr;
100         __isValid = true;
101 }
102
103 _Ip4AddressImpl::_Ip4AddressImpl(unsigned long ipAddr)
104         : __address(_IP4_ADDRESS_NONE)
105         , __addressInTextualFormat()
106         , __isValid(false)
107 {
108         ClearLastResult();
109
110         struct in_addr in;
111         in.s_addr = htonl(ipAddr);
112         char* pIpAddr = inet_ntoa(in);
113
114         SysTryReturnVoidResult(NID_NET, pIpAddr != null, E_INVALID_ARG,
115                         "[%s] Invalid argument is used. Ip4Address must not be null.", GetErrorMessage(E_INVALID_ARG));
116
117         __address = ipAddr;
118         __addressInTextualFormat = String(pIpAddr);
119         __isValid = true;
120 }
121
122 _Ip4AddressImpl::_Ip4AddressImpl(const _Ip4AddressImpl& value)
123 {
124         __address = value.__address;
125         __addressInTextualFormat = value.__addressInTextualFormat;
126         __isValid = value.__isValid;
127 }
128
129 _Ip4AddressImpl::~_Ip4AddressImpl(void)
130 {
131 }
132
133 NetAddressFamily
134 _Ip4AddressImpl::GetNetAddressFamily(void) const
135 {
136         ClearLastResult();
137
138         SysTryReturn(NID_NET, __isValid, NET_AF_NONE, E_INVALID_STATE,
139                         "[%s] The Ip4Address is in an invalid state.", GetErrorMessage(E_INVALID_STATE));
140
141         return NET_AF_IPV4;
142 }
143
144 result
145 _Ip4AddressImpl::GetAddress(ByteBuffer& ipAddr) const
146 {
147         result r = E_SUCCESS;
148
149         SysTryReturnResult(NID_NET, ipAddr.GetRemaining() >= _IP4_ADDRESS_LENGTH, E_OVERFLOW,
150                                 "The size of buffer[%d] is smaller than [%d].", ipAddr.GetRemaining(), _IP4_ADDRESS_LENGTH);
151
152         r = ipAddr.SetLong((long)__address);
153
154         return r;
155 }
156
157 result
158 _Ip4AddressImpl::GetAddress(unsigned long& ipAddr) const
159 {
160         result r = E_SUCCESS;
161
162         SysTryReturnResult(NID_NET, __isValid, E_INVALID_STATE, "The Ip4Address is in an invalid state.");
163
164         ipAddr = __address;
165
166         return r;
167 }
168
169 String
170 _Ip4AddressImpl::ToString(void) const
171 {
172         return __addressInTextualFormat;
173 }
174
175 _Ip4AddressImpl&
176 _Ip4AddressImpl::operator =(const _Ip4AddressImpl& rhs)
177 {
178         if (this == &rhs)
179         {
180                 return *this;
181         }
182
183         __address = rhs.__address;
184         __addressInTextualFormat = rhs.__addressInTextualFormat;
185         __isValid = rhs.__isValid;
186
187         return *this;
188 }
189
190 bool
191 _Ip4AddressImpl::Equals(const Object& obj) const
192 {
193         const _Ip4AddressImpl* pObj = dynamic_cast <const _Ip4AddressImpl*>(&obj);
194
195         if (pObj == null)
196         {
197                 return false;
198         }
199
200         if (__address != pObj->__address)
201         {
202                 return false;
203         }
204
205         return true;
206 }
207
208 int
209 _Ip4AddressImpl::GetHashCode(void) const
210 {
211         return __address;
212 }
213
214 bool
215 _Ip4AddressImpl::IsValid(void) const
216 {
217         return __isValid;
218 }
219
220 _Ip4AddressImpl*
221 _Ip4AddressImpl::GetInstance(Ip4Address& ip4Address)
222 {
223         return ip4Address.__pIp4AddressImpl;
224 }
225
226 const _Ip4AddressImpl*
227 _Ip4AddressImpl::GetInstance(const Ip4Address& ip4Address)
228 {
229         return ip4Address.__pIp4AddressImpl;
230 }
231
232 }} // Tizen::Net