CTR Pia  4.11.3
Game Communication Engine
common_InetAddress.h
1 /*---------------------------------------------------------------------------*
2  Project: Pia
3  File: common_InetAddress.h
4 
5  Copyright Nintendo. All rights reserved.
6 
7  These coded instructions, statements, and computer programs contain
8  proprietary information of Nintendo of America Inc. and/or Nintendo
9  Company Ltd., and are protected by Federal copyright law. They may
10  not be disclosed to third parties or copied or duplicated in any form,
11  in whole or in part, without the prior written consent of Nintendo.
12  *---------------------------------------------------------------------------*/
13 
14 
15 #pragma once
16 
17 
18 #include <pia/common/common_definitions.h>
19 #include <pia/common/common_RootObject.h>
20 #include <pia/common/common_ByteOrder.h>
21 
22 namespace nn
23 {
24 namespace pia
25 {
26 namespace common
27 {
28 
29 // Forward declaration.
30 class String;
31 
32 /**
33 * @brief An IPv4 address structure.
34 */
35 struct InAddr
36 {
37  u32 addr;
38 };
39 
40 
41 //n1589
42 #define NN_PIA_COMMON_INET_ADDRESS_STRING_LEN_MAX sizeof(_T("255.255.255.255"))
43 
44 
45 /*!
46 @brief Class that represents an Internet address.
47 
48 @details This class contains information such as the IP address and port number.
49 
50 @date 2012-04-19 Initial version.
51 */
52 class InetAddress : public RootObject
53 {
54 public:
55  static const size_t SERIALIZE_SIZE_MAX =
56  sizeof(u32) // m_Address
57  + sizeof(u16); // m_Port
58 
59 /*!
60 @brief Instantiates the object with default parameters (default constructor).
61 Sets the IP address and port number being cleared to zero.
62 */
63  InetAddress();
64 
65 /*!
66 @brief Instantiates the object and initializes it with the IP address and port number specified.
67 
68 @param[in] uiAddress The IP address.
69 @param[in] uiPort Specifies the port number.
70 */
71  InetAddress(u32 uiAddress, u16 uiPort);
72 
73 /*!
74 @brief Copy constructor.
75 
76 @param[in] oInetAddress Specifies the object being copied.
77 */
78  InetAddress(const InetAddress& oInetAddress);
79 
80 /*!
81 @brief Destroys the object.
82 */
83  ~InetAddress();
84 
85 /*!
86 @brief Instantiates the object.
87 */
88  void Init()
89  {
90  m_Address = 0;
91  m_Port = 0;
92  }
93 
94 /*!
95 @brief Sets the IP address in host byte order.
96 
97 @param[in] uiAddress Specifies the IP address (in host byte order).
98 @see GetAddress
99 */
100  void SetAddress(const u32 uiAddress)
101  {
102  m_Address = uiAddress;
103  }
104 
105 /*!
106 @brief Gets the IP address in host byte order.
107 
108 @return Returns the IP address set for this object (in host byte order).
109 @see SetAddress
110 */
111  u32 GetAddress() const
112  {
113  return m_Address;
114  }
115 
116 /*!
117 @brief Sets the port number in host byte order.
118 
119 @param[in] ui16Port Specifies the port number (in host byte order).
120 @see GetPort
121 */
122  void SetPort(const u16 ui16Port)
123  {
124  m_Port = ui16Port;
125  }
126 
127 /*!
128 @brief Sets the port number in host byte order.
129 
130 @return Returns the port number set for this object (in host byte order).
131 @see SetPort
132 */
133  u16 GetPort() const
134  {
135  return m_Port;
136  }
137 
138 /*!
139 @brief Gets the key for comparison.
140 
141 @return Returns the key for comparison.
142 */
143  s64 GetKey() const;
144 
145 /*!
146 @brief This is the assignment operator.
147 
148 @param[in] oInetAddress The object to assign.
149 @return See this object.
150 */
151  InetAddress& operator=(const InetAddress& oInetAddress);
152 
153 /*!
154 @brief Gets the address string notation.
155 Address strings are represented in a format such as <tt>255.255.255.255:80</tt>.
156 
157 @param[out] pString Specifies a pointer to the <tt>String</tt> object used to set the address string notation.
158 */
159  void GetAddressString(String* pString) const;
160 
161 /*!
162 @brief Prints information that is useful for debugging.
163 
164 @param[in] uiTraceFlag Bitwise OR of trace flags. For more information, see the <tt>@ref TraceFlag</tt> type.
165 */
166  void Trace(u64 uiTraceFlag) const;
167 
168 /*!
169 @brief This is an equality operator.
170 
171 @param[in] oInetAddress Specifies the object to compare.
172 @return Returns <tt>true</tt> if this object and the one being compared to are the same; returns <tt>false</tt> otherwise.
173 */
174  bool operator==(const InetAddress& oInetAddress) const
175  {
176  return (GetKey() == oInetAddress.GetKey());
177  }
178 
179 /*!
180 @brief This is an equality operator.
181 
182 @param[in] oInetAddress Specifies the object to compare.
183 @return Returns <tt>true</tt> if this object and the one being compared to are not the same; returns <tt>false</tt> otherwise.
184 */
185  bool operator!=(const InetAddress& oInetAddress) const
186  {
187  return !operator==(oInetAddress);
188  }
189 
190 /*!
191 @brief Comparison operator.
192 
193 @param[in] oInetAddress Specifies the object to compare.
194 @return
195 */
196  bool operator<(const InetAddress& oInetAddress) const
197  {
198  return (GetKey() < oInetAddress.GetKey());
199  }
200 
201 /*!
202 @cond PRIVATE
203 @brief Gets the length of the object as a serialized byte array. This value is in bytes.
204 
205 @return Returns the length of the serialized data. This value is in bytes.
206 @see Serialize, Deserialize
207 */
208  size_t GetSerializedSize(void) const
209  {
210  return sizeof(u32) + sizeof(u16);
211  }
212  //! @endcond
213 
214 /*!
215 @cond PRIVATE
216 @brief Serializes the object.
217 
218 @param[out] pBuffer Holds the serialized data.
219 @param[out] pDataLen Receives a pointer to the length of the serialized data that was written to the buffer. This value is in bytes.
220 @param[in] bufferSize Specifies the size of the buffer that was specified in <span class="argument">pBuffer</span>.
221 @return On success, returns a <tt>Result</tt> value for which the <tt>IsSuccess</tt> function will return <tt>true</tt>.
222 @retval ResultInvalidArgument Indicates that an argument is invalid. (For example, a NULL pointer was specified.) This function also returns this error if the size specified by <span class="argument">pBuffer</span> or <span class="argument">bufferSize</span> is too small.
223 @see Deserialize, GetSerializedSize
224 */
225  nn::Result Serialize(bit8* pBuffer, size_t* pDataLen, size_t bufferSize) const;
226  //! @endcond
227 
228 /*!
229 @cond PRIVATE
230 @brief Restores an object from serialized data.
231 
232 @param[in] pData Pointer to the serialized data.
233 
234 @return On success, returns a <tt>Result</tt> value for which the <tt>IsSuccess</tt> function will return <tt>true</tt>.
235 @retval ResultInvalidArgument Indicates that an argument is invalid. (For example, a NULL pointer was specified.)
236 @see Serialize, GetSerializedSize
237 */
238  nn::Result Deserialize(const bit8* pData);
239  //! @endcond
240 
241 /*!
242 @cond PRIVATE
243 @brief Determines whether a valid address and port have been set.
244 
245 @return Returns <tt>true</tt> if valid and <tt>false</tt> otherwise.
246 */
247  bool IsValid(void) const;
248  //! @endcond
249 
250 /*!
251 @cond PRIVATE
252 @brief Determines whether the specified address is valid.
253 
254 @return Returns <tt>true</tt> if valid and <tt>false</tt> otherwise.
255 */
256  bool IsValidAddress(void) const;
257  //! @endcond
258 
259 /*!
260 @cond PRIVATE
261 @brief Determines whether the specified port is valid.
262 
263 @return Returns <tt>true</tt> if valid and <tt>false</tt> otherwise.
264 */
265  bool IsValidPort(void) const;
266  //! @endcond
267 
268 /*!
269 @cond PRIVATE
270 @brief Determines whether the address is a private address.
271 
272 @return Returns <tt>true</tt> if private, and returns <tt>false</tt> otherwise.
273 */
274  bool IsPrivate(void) const;
275  //! @endcond
276 
277 
278 protected:
279  u32 m_Address;
280  u16 m_Port;
281 };
282 }
283 }
284 } // end of namespace nn::pia::common
bool operator!=(const InetAddress &oInetAddress) const
This is an equality operator.
Definition: common_InetAddress.h:185
Definition: assert.h:115
An IPv4 address structure.
Definition: common_InetAddress.h:35
This class is the trace class.
Definition: common_Trace.h:212
void SetPort(const u16 ui16Port)
Sets the port number in host byte order.
Definition: common_InetAddress.h:122
s64 GetKey() const
Gets the key for comparison.
bool operator==(const InetAddress &oInetAddress) const
This is an equality operator.
Definition: common_InetAddress.h:174
Class for representing strings.
Definition: common_String.h:35
u32 GetAddress() const
Gets the IP address in host byte order.
Definition: common_InetAddress.h:111
u16 GetPort() const
Sets the port number in host byte order.
Definition: common_InetAddress.h:133
void SetAddress(const u32 uiAddress)
Sets the IP address in host byte order.
Definition: common_InetAddress.h:100
void Init()
Instantiates the object.
Definition: common_InetAddress.h:88
bool operator<(const InetAddress &oInetAddress) const
Comparison operator.
Definition: common_InetAddress.h:196
Class that represents an Internet address.
Definition: common_InetAddress.h:52
This is the common base class used inside the Pia library.
Definition: common_RootObject.h:40