2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FNet_Ip4AddressImpl.cpp
19 * @brief This is the implementation for the _Ip4AddressImpl class.
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"
39 using namespace Tizen::Base;
40 using namespace Tizen::Base::Utility;
41 using namespace Tizen::Net::Sockets;
43 namespace Tizen { namespace Net {
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".";
49 _Ip4AddressImpl::_Ip4AddressImpl(void)
50 : __address(_IP4_ADDRESS_NONE)
51 , __addressInTextualFormat()
56 _Ip4AddressImpl::_Ip4AddressImpl(const String& ipAddr)
57 : __address(_IP4_ADDRESS_NONE)
58 , __addressInTextualFormat()
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));
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());
76 while (tokens.HasMoreTokens())
81 tokens.GetNextToken(token);
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));
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));
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());
98 __address = ntohl(in.s_addr);
99 __addressInTextualFormat = ipAddr;
103 _Ip4AddressImpl::_Ip4AddressImpl(unsigned long ipAddr)
104 : __address(_IP4_ADDRESS_NONE)
105 , __addressInTextualFormat()
111 in.s_addr = htonl(ipAddr);
112 char* pIpAddr = inet_ntoa(in);
114 SysTryReturnVoidResult(NID_NET, pIpAddr != null, E_INVALID_ARG,
115 "[%s] Invalid argument is used. Ip4Address must not be null.", GetErrorMessage(E_INVALID_ARG));
118 __addressInTextualFormat = String(pIpAddr);
122 _Ip4AddressImpl::_Ip4AddressImpl(const _Ip4AddressImpl& value)
124 __address = value.__address;
125 __addressInTextualFormat = value.__addressInTextualFormat;
126 __isValid = value.__isValid;
129 _Ip4AddressImpl::~_Ip4AddressImpl(void)
134 _Ip4AddressImpl::GetNetAddressFamily(void) const
138 SysTryReturn(NID_NET, __isValid, NET_AF_NONE, E_INVALID_STATE,
139 "[%s] The Ip4Address is in an invalid state.", GetErrorMessage(E_INVALID_STATE));
145 _Ip4AddressImpl::GetAddress(ByteBuffer& ipAddr) const
147 result r = E_SUCCESS;
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);
152 r = ipAddr.SetLong((long)__address);
158 _Ip4AddressImpl::GetAddress(unsigned long& ipAddr) const
160 result r = E_SUCCESS;
162 SysTryReturnResult(NID_NET, __isValid, E_INVALID_STATE, "The Ip4Address is in an invalid state.");
170 _Ip4AddressImpl::ToString(void) const
172 return __addressInTextualFormat;
176 _Ip4AddressImpl::operator =(const _Ip4AddressImpl& rhs)
183 __address = rhs.__address;
184 __addressInTextualFormat = rhs.__addressInTextualFormat;
185 __isValid = rhs.__isValid;
191 _Ip4AddressImpl::Equals(const Object& obj) const
193 const _Ip4AddressImpl* pObj = dynamic_cast <const _Ip4AddressImpl*>(&obj);
200 if (__address != pObj->__address)
209 _Ip4AddressImpl::GetHashCode(void) const
215 _Ip4AddressImpl::IsValid(void) const
221 _Ip4AddressImpl::GetInstance(Ip4Address& ip4Address)
223 return ip4Address.__pIp4AddressImpl;
226 const _Ip4AddressImpl*
227 _Ip4AddressImpl::GetInstance(const Ip4Address& ip4Address)
229 return ip4Address.__pIp4AddressImpl;