/extlibs/SFML/include/SFML/Network/IpAddress.hpp

https://bitbucket.org/hugoruscitti/pilascpp · C++ Header · 314 lines · 35 code · 29 blank · 250 comment · 0 complexity · 298f7d17dfad514ed241a9a85b80d509 MD5 · raw file

  1. ////////////////////////////////////////////////////////////
  2. //
  3. // SFML - Simple and Fast Multimedia Library
  4. // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
  5. //
  6. // This software is provided 'as-is', without any express or implied warranty.
  7. // In no event will the authors be held liable for any damages arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it freely,
  11. // subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented;
  14. // you must not claim that you wrote the original software.
  15. // If you use this software in a product, an acknowledgment
  16. // in the product documentation would be appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such,
  19. // and must not be misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source distribution.
  22. //
  23. ////////////////////////////////////////////////////////////
  24. #ifndef SFML_IPADDRESS_HPP
  25. #define SFML_IPADDRESS_HPP
  26. ////////////////////////////////////////////////////////////
  27. // Headers
  28. ////////////////////////////////////////////////////////////
  29. #include <SFML/Config.hpp>
  30. #include <istream>
  31. #include <ostream>
  32. #include <string>
  33. namespace sf
  34. {
  35. ////////////////////////////////////////////////////////////
  36. /// \brief Encapsulate an IPv4 network address
  37. ///
  38. ////////////////////////////////////////////////////////////
  39. class SFML_API IpAddress
  40. {
  41. public :
  42. ////////////////////////////////////////////////////////////
  43. /// \brief Default constructor
  44. ///
  45. /// This constructor creates an empty (invalid) address
  46. ///
  47. ////////////////////////////////////////////////////////////
  48. IpAddress();
  49. ////////////////////////////////////////////////////////////
  50. /// \brief Construct the address from a string
  51. ///
  52. /// Here \a address can be either a decimal address
  53. /// (ex: "192.168.1.56") or a network name (ex: "localhost").
  54. ///
  55. /// \param address IP address or network name
  56. ///
  57. ////////////////////////////////////////////////////////////
  58. IpAddress(const std::string& address);
  59. ////////////////////////////////////////////////////////////
  60. /// \brief Construct the address from a string
  61. ///
  62. /// Here \a address can be either a decimal address
  63. /// (ex: "192.168.1.56") or a network name (ex: "localhost").
  64. /// This is equivalent to the constructor taking a std::string
  65. /// parameter, it is defined for convenience so that the
  66. /// implicit conversions from literal strings to IpAddress work.
  67. ///
  68. /// \param address IP address or network name
  69. ///
  70. ////////////////////////////////////////////////////////////
  71. IpAddress(const char* address);
  72. ////////////////////////////////////////////////////////////
  73. /// \brief Construct the address from 4 bytes
  74. ///
  75. /// Calling IpAddress(a, b, c, d) is equivalent to calling
  76. /// IpAddress("a.b.c.d"), but safer as it doesn't have to
  77. /// parse a string to get the address components.
  78. ///
  79. /// \param byte0 First byte of the address
  80. /// \param byte1 Second byte of the address
  81. /// \param byte2 Third byte of the address
  82. /// \param byte3 Fourth byte of the address
  83. ///
  84. ////////////////////////////////////////////////////////////
  85. IpAddress(Uint8 byte0, Uint8 byte1, Uint8 byte2, Uint8 byte3);
  86. ////////////////////////////////////////////////////////////
  87. /// \brief Construct the address from a 32-bits integer
  88. ///
  89. /// This constructor uses the internal representation of
  90. /// the address directly. It should be used for optimization
  91. /// purposes, and only if you got that representation from
  92. /// IpAddress::ToInteger().
  93. ///
  94. /// \param address 4 bytes of the address packed into a 32-bits integer
  95. ///
  96. /// \see ToInteger
  97. ///
  98. ////////////////////////////////////////////////////////////
  99. explicit IpAddress(Uint32 address);
  100. ////////////////////////////////////////////////////////////
  101. /// \brief Get a string representation of the address
  102. ///
  103. /// The returned string is the decimal representation of the
  104. /// IP address (like "192.168.1.56"), even if it was constructed
  105. /// from a host name.
  106. ///
  107. /// \return String representation of the address
  108. ///
  109. /// \see ToInteger
  110. ///
  111. ////////////////////////////////////////////////////////////
  112. std::string ToString() const;
  113. ////////////////////////////////////////////////////////////
  114. /// \brief Get an integer representation of the address
  115. ///
  116. /// The returned number is the internal representation of the
  117. /// address, and should be used for optimization purposes only
  118. /// (like sending the address through a socket).
  119. /// The integer produced by this function can then be converted
  120. /// back to a sf::IpAddress with the proper constructor.
  121. ///
  122. /// \return 32-bits unsigned integer representation of the address
  123. ///
  124. /// \see ToString
  125. ///
  126. ////////////////////////////////////////////////////////////
  127. Uint32 ToInteger() const;
  128. ////////////////////////////////////////////////////////////
  129. /// \brief Get the computer's local address
  130. ///
  131. /// The local address is the address of the computer from the
  132. /// LAN point of view, i.e. something like 192.168.1.56. It is
  133. /// meaningful only for communications over the local network.
  134. /// Unlike GetPublicAddress, this function is fast and may be
  135. /// used safely anywhere.
  136. ///
  137. /// \return Local IP address of the computer
  138. ///
  139. /// \see GetPublicAddress
  140. ///
  141. ////////////////////////////////////////////////////////////
  142. static IpAddress GetLocalAddress();
  143. ////////////////////////////////////////////////////////////
  144. /// \brief Get the computer's public address
  145. ///
  146. /// The public address is the address of the computer from the
  147. /// internet point of view, i.e. something like 89.54.1.169.
  148. /// It is necessary for communications over the world wide web.
  149. /// The only way to get a public address is to ask it to a
  150. /// distant website; as a consequence, this function depends on
  151. /// both your network connection and the server, and may be
  152. /// very slow. You should use it as few as possible. Because
  153. /// this function depends on the network connection and on a distant
  154. /// server, you may use a time limit if you don't want your program
  155. /// to be possibly stuck waiting in case there is a problem; this
  156. /// limit is deactivated by default.
  157. ///
  158. /// \param timeout Maximum time to wait, in seconds
  159. ///
  160. /// \return Public IP address of the computer
  161. ///
  162. /// \see GetLocalAddress
  163. ///
  164. ////////////////////////////////////////////////////////////
  165. static IpAddress GetPublicAddress(float timeout = 0.f);
  166. ////////////////////////////////////////////////////////////
  167. // Static member data
  168. ////////////////////////////////////////////////////////////
  169. static const IpAddress None; ///< Value representing an empty/invalid address
  170. static const IpAddress LocalHost; ///< The "localhost" address (for connecting a computer to itself locally)
  171. private :
  172. ////////////////////////////////////////////////////////////
  173. // Member data
  174. ////////////////////////////////////////////////////////////
  175. Uint32 myAddress; ///< Address stored as an unsigned 32 bits integer
  176. };
  177. ////////////////////////////////////////////////////////////
  178. /// \brief Overload of == operator to compare two IP addresses
  179. ///
  180. /// \param left Left operand (a IP address)
  181. /// \param right Right operand (a IP address)
  182. ///
  183. /// \return True if both addresses are equal
  184. ///
  185. ////////////////////////////////////////////////////////////
  186. SFML_API bool operator ==(const IpAddress& left, const IpAddress& right);
  187. ////////////////////////////////////////////////////////////
  188. /// \brief Overload of != operator to compare two IP addresses
  189. ///
  190. /// \param left Left operand (a IP address)
  191. /// \param right Right operand (a IP address)
  192. ///
  193. /// \return True if both addresses are different
  194. ///
  195. ////////////////////////////////////////////////////////////
  196. SFML_API bool operator !=(const IpAddress& left, const IpAddress& right);
  197. ////////////////////////////////////////////////////////////
  198. /// \brief Overload of < operator to compare two IP addresses
  199. ///
  200. /// \param left Left operand (a IP address)
  201. /// \param right Right operand (a IP address)
  202. ///
  203. /// \return True if \a left is lesser than \a right
  204. ///
  205. ////////////////////////////////////////////////////////////
  206. SFML_API bool operator <(const IpAddress& left, const IpAddress& right);
  207. ////////////////////////////////////////////////////////////
  208. /// \brief Overload of > operator to compare two IP addresses
  209. ///
  210. /// \param left Left operand (a IP address)
  211. /// \param right Right operand (a IP address)
  212. ///
  213. /// \return True if \a left is greater than \a right
  214. ///
  215. ////////////////////////////////////////////////////////////
  216. SFML_API bool operator >(const IpAddress& left, const IpAddress& right);
  217. ////////////////////////////////////////////////////////////
  218. /// \brief Overload of <= operator to compare two IP addresses
  219. ///
  220. /// \param left Left operand (a IP address)
  221. /// \param right Right operand (a IP address)
  222. ///
  223. /// \return True if \a left is lesser or equal than \a right
  224. ///
  225. ////////////////////////////////////////////////////////////
  226. SFML_API bool operator <=(const IpAddress& left, const IpAddress& right);
  227. ////////////////////////////////////////////////////////////
  228. /// \brief Overload of >= operator to compare two IP addresses
  229. ///
  230. /// \param left Left operand (a IP address)
  231. /// \param right Right operand (a IP address)
  232. ///
  233. /// \return True if \a left is greater or equal than \a right
  234. ///
  235. ////////////////////////////////////////////////////////////
  236. SFML_API bool operator >=(const IpAddress& left, const IpAddress& right);
  237. ////////////////////////////////////////////////////////////
  238. /// \brief Overload of >> operator to extract an IP address from an input stream
  239. ///
  240. /// \param stream Input stream
  241. /// \param address IP address to extract
  242. ///
  243. /// \return Reference to the input stream
  244. ///
  245. ////////////////////////////////////////////////////////////
  246. SFML_API std::istream& operator >>(std::istream& stream, IpAddress& address);
  247. ////////////////////////////////////////////////////////////
  248. /// \brief Overload of << operator to print an IP address to an output stream
  249. ///
  250. /// \param stream Output stream
  251. /// \param address IP address to print
  252. ///
  253. /// \return Reference to the output stream
  254. ///
  255. ////////////////////////////////////////////////////////////
  256. SFML_API std::ostream& operator <<(std::ostream& stream, const IpAddress& address);
  257. } // namespace sf
  258. #endif // SFML_IPADDRESS_HPP
  259. ////////////////////////////////////////////////////////////
  260. /// \class sf::IpAddress
  261. /// \ingroup network
  262. ///
  263. /// sf::IpAddress is a utility class for manipulating network
  264. /// addresses. It provides a set a implicit constructors and
  265. /// conversion functions to easily build or transform an IP
  266. /// address from/to various representations.
  267. ///
  268. /// Usage example:
  269. /// \code
  270. /// sf::IpAddress a0; // an invalid address
  271. /// sf::IpAddress a1 = sf::IpAddress::None; // an invalid address (same as a0)
  272. /// sf::IpAddress a2("127.0.0.1"); // the local host address
  273. /// sf::IpAddress a3 = sf::IpAddress::LocalHost; // the local host address (same as a2)
  274. /// sf::IpAddress a4(192, 168, 1, 56); // a local address
  275. /// sf::IpAddress a5("my_computer"); // a local address created from a network name
  276. /// sf::IpAddress a6("89.54.1.169"); // a distant address
  277. /// sf::IpAddress a7("www.google.com"); // a distant address created from a network name
  278. /// sf::IpAddress a8 = sf::IpAddress::GetLocalAddress(); // my address on the local network
  279. /// sf::IpAddress a9 = sf::IpAddress::GetPublicAddress(); // my address on the internet
  280. /// \endcode
  281. ///
  282. /// Note that sf::IpAddress currently doesn't support IPv6
  283. /// nor other types of network addresses.
  284. ///
  285. ////////////////////////////////////////////////////////////