Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-06-02 08:58:14

0001 ////////////////////////////////////////////////////////////
0002 //
0003 // SFML - Simple and Fast Multimedia Library
0004 // Copyright (C) 2007-2023 Laurent Gomila (laurent@sfml-dev.org)
0005 //
0006 // This software is provided 'as-is', without any express or implied warranty.
0007 // In no event will the authors be held liable for any damages arising from the use of this software.
0008 //
0009 // Permission is granted to anyone to use this software for any purpose,
0010 // including commercial applications, and to alter it and redistribute it freely,
0011 // subject to the following restrictions:
0012 //
0013 // 1. The origin of this software must not be misrepresented;
0014 //    you must not claim that you wrote the original software.
0015 //    If you use this software in a product, an acknowledgment
0016 //    in the product documentation would be appreciated but is not required.
0017 //
0018 // 2. Altered source versions must be plainly marked as such,
0019 //    and must not be misrepresented as being the original software.
0020 //
0021 // 3. This notice may not be removed or altered from any source distribution.
0022 //
0023 ////////////////////////////////////////////////////////////
0024 
0025 #ifndef SFML_IPADDRESS_HPP
0026 #define SFML_IPADDRESS_HPP
0027 
0028 ////////////////////////////////////////////////////////////
0029 // Headers
0030 ////////////////////////////////////////////////////////////
0031 #include <SFML/Network/Export.hpp>
0032 #include <SFML/System/Time.hpp>
0033 #include <istream>
0034 #include <ostream>
0035 #include <string>
0036 
0037 
0038 namespace sf
0039 {
0040 ////////////////////////////////////////////////////////////
0041 /// \brief Encapsulate an IPv4 network address
0042 ///
0043 ////////////////////////////////////////////////////////////
0044 class SFML_NETWORK_API IpAddress
0045 {
0046 public:
0047 
0048     ////////////////////////////////////////////////////////////
0049     /// \brief Default constructor
0050     ///
0051     /// This constructor creates an empty (invalid) address
0052     ///
0053     ////////////////////////////////////////////////////////////
0054     IpAddress();
0055 
0056     ////////////////////////////////////////////////////////////
0057     /// \brief Construct the address from a string
0058     ///
0059     /// Here \a address can be either a decimal address
0060     /// (ex: "192.168.1.56") or a network name (ex: "localhost").
0061     ///
0062     /// \param address IP address or network name
0063     ///
0064     ////////////////////////////////////////////////////////////
0065     IpAddress(const std::string& address);
0066 
0067     ////////////////////////////////////////////////////////////
0068     /// \brief Construct the address from a string
0069     ///
0070     /// Here \a address can be either a decimal address
0071     /// (ex: "192.168.1.56") or a network name (ex: "localhost").
0072     /// This is equivalent to the constructor taking a std::string
0073     /// parameter, it is defined for convenience so that the
0074     /// implicit conversions from literal strings to IpAddress work.
0075     ///
0076     /// \param address IP address or network name
0077     ///
0078     ////////////////////////////////////////////////////////////
0079     IpAddress(const char* address);
0080 
0081     ////////////////////////////////////////////////////////////
0082     /// \brief Construct the address from 4 bytes
0083     ///
0084     /// Calling IpAddress(a, b, c, d) is equivalent to calling
0085     /// IpAddress("a.b.c.d"), but safer as it doesn't have to
0086     /// parse a string to get the address components.
0087     ///
0088     /// \param byte0 First byte of the address
0089     /// \param byte1 Second byte of the address
0090     /// \param byte2 Third byte of the address
0091     /// \param byte3 Fourth byte of the address
0092     ///
0093     ////////////////////////////////////////////////////////////
0094     IpAddress(Uint8 byte0, Uint8 byte1, Uint8 byte2, Uint8 byte3);
0095 
0096     ////////////////////////////////////////////////////////////
0097     /// \brief Construct the address from a 32-bits integer
0098     ///
0099     /// This constructor uses the internal representation of
0100     /// the address directly. It should be used for optimization
0101     /// purposes, and only if you got that representation from
0102     /// IpAddress::toInteger().
0103     ///
0104     /// \param address 4 bytes of the address packed into a 32-bits integer
0105     ///
0106     /// \see toInteger
0107     ///
0108     ////////////////////////////////////////////////////////////
0109     explicit IpAddress(Uint32 address);
0110 
0111     ////////////////////////////////////////////////////////////
0112     /// \brief Get a string representation of the address
0113     ///
0114     /// The returned string is the decimal representation of the
0115     /// IP address (like "192.168.1.56"), even if it was constructed
0116     /// from a host name.
0117     ///
0118     /// \return String representation of the address
0119     ///
0120     /// \see toInteger
0121     ///
0122     ////////////////////////////////////////////////////////////
0123     std::string toString() const;
0124 
0125     ////////////////////////////////////////////////////////////
0126     /// \brief Get an integer representation of the address
0127     ///
0128     /// The returned number is the internal representation of the
0129     /// address, and should be used for optimization purposes only
0130     /// (like sending the address through a socket).
0131     /// The integer produced by this function can then be converted
0132     /// back to a sf::IpAddress with the proper constructor.
0133     ///
0134     /// \return 32-bits unsigned integer representation of the address
0135     ///
0136     /// \see toString
0137     ///
0138     ////////////////////////////////////////////////////////////
0139     Uint32 toInteger() const;
0140 
0141     ////////////////////////////////////////////////////////////
0142     /// \brief Get the computer's local address
0143     ///
0144     /// The local address is the address of the computer from the
0145     /// LAN point of view, i.e. something like 192.168.1.56. It is
0146     /// meaningful only for communications over the local network.
0147     /// Unlike getPublicAddress, this function is fast and may be
0148     /// used safely anywhere.
0149     ///
0150     /// \return Local IP address of the computer
0151     ///
0152     /// \see getPublicAddress
0153     ///
0154     ////////////////////////////////////////////////////////////
0155     static IpAddress getLocalAddress();
0156 
0157     ////////////////////////////////////////////////////////////
0158     /// \brief Get the computer's public address
0159     ///
0160     /// The public address is the address of the computer from the
0161     /// internet point of view, i.e. something like 89.54.1.169.
0162     /// It is necessary for communications over the world wide web.
0163     /// The only way to get a public address is to ask it to a
0164     /// distant website; as a consequence, this function depends on
0165     /// both your network connection and the server, and may be
0166     /// very slow. You should use it as few as possible. Because
0167     /// this function depends on the network connection and on a distant
0168     /// server, you may use a time limit if you don't want your program
0169     /// to be possibly stuck waiting in case there is a problem; this
0170     /// limit is deactivated by default.
0171     ///
0172     /// \param timeout Maximum time to wait
0173     ///
0174     /// \return Public IP address of the computer
0175     ///
0176     /// \see getLocalAddress
0177     ///
0178     ////////////////////////////////////////////////////////////
0179     static IpAddress getPublicAddress(Time timeout = Time::Zero);
0180 
0181     ////////////////////////////////////////////////////////////
0182     // Static member data
0183     ////////////////////////////////////////////////////////////
0184     static const IpAddress None;      //!< Value representing an empty/invalid address
0185     static const IpAddress Any;       //!< Value representing any address (0.0.0.0)
0186     static const IpAddress LocalHost; //!< The "localhost" address (for connecting a computer to itself locally)
0187     static const IpAddress Broadcast; //!< The "broadcast" address (for sending UDP messages to everyone on a local network)
0188 
0189 private:
0190 
0191     friend SFML_NETWORK_API bool operator <(const IpAddress& left, const IpAddress& right);
0192 
0193     ////////////////////////////////////////////////////////////
0194     /// \brief Resolve the given address string
0195     ///
0196     /// \param address Address string
0197     ///
0198     ////////////////////////////////////////////////////////////
0199     void resolve(const std::string& address);
0200 
0201     ////////////////////////////////////////////////////////////
0202     // Member data
0203     ////////////////////////////////////////////////////////////
0204     Uint32 m_address; //!< Address stored as an unsigned 32 bits integer
0205     bool   m_valid;   //!< Is the address valid?
0206 };
0207 
0208 ////////////////////////////////////////////////////////////
0209 /// \brief Overload of == operator to compare two IP addresses
0210 ///
0211 /// \param left  Left operand (a IP address)
0212 /// \param right Right operand (a IP address)
0213 ///
0214 /// \return True if both addresses are equal
0215 ///
0216 ////////////////////////////////////////////////////////////
0217 SFML_NETWORK_API bool operator ==(const IpAddress& left, const IpAddress& right);
0218 
0219 ////////////////////////////////////////////////////////////
0220 /// \brief Overload of != operator to compare two IP addresses
0221 ///
0222 /// \param left  Left operand (a IP address)
0223 /// \param right Right operand (a IP address)
0224 ///
0225 /// \return True if both addresses are different
0226 ///
0227 ////////////////////////////////////////////////////////////
0228 SFML_NETWORK_API bool operator !=(const IpAddress& left, const IpAddress& right);
0229 
0230 ////////////////////////////////////////////////////////////
0231 /// \brief Overload of < operator to compare two IP addresses
0232 ///
0233 /// \param left  Left operand (a IP address)
0234 /// \param right Right operand (a IP address)
0235 ///
0236 /// \return True if \a left is lesser than \a right
0237 ///
0238 ////////////////////////////////////////////////////////////
0239 SFML_NETWORK_API bool operator <(const IpAddress& left, const IpAddress& right);
0240 
0241 ////////////////////////////////////////////////////////////
0242 /// \brief Overload of > operator to compare two IP addresses
0243 ///
0244 /// \param left  Left operand (a IP address)
0245 /// \param right Right operand (a IP address)
0246 ///
0247 /// \return True if \a left is greater than \a right
0248 ///
0249 ////////////////////////////////////////////////////////////
0250 SFML_NETWORK_API bool operator >(const IpAddress& left, const IpAddress& right);
0251 
0252 ////////////////////////////////////////////////////////////
0253 /// \brief Overload of <= operator to compare two IP addresses
0254 ///
0255 /// \param left  Left operand (a IP address)
0256 /// \param right Right operand (a IP address)
0257 ///
0258 /// \return True if \a left is lesser or equal than \a right
0259 ///
0260 ////////////////////////////////////////////////////////////
0261 SFML_NETWORK_API bool operator <=(const IpAddress& left, const IpAddress& right);
0262 
0263 ////////////////////////////////////////////////////////////
0264 /// \brief Overload of >= operator to compare two IP addresses
0265 ///
0266 /// \param left  Left operand (a IP address)
0267 /// \param right Right operand (a IP address)
0268 ///
0269 /// \return True if \a left is greater or equal than \a right
0270 ///
0271 ////////////////////////////////////////////////////////////
0272 SFML_NETWORK_API bool operator >=(const IpAddress& left, const IpAddress& right);
0273 
0274 ////////////////////////////////////////////////////////////
0275 /// \brief Overload of >> operator to extract an IP address from an input stream
0276 ///
0277 /// \param stream  Input stream
0278 /// \param address IP address to extract
0279 ///
0280 /// \return Reference to the input stream
0281 ///
0282 ////////////////////////////////////////////////////////////
0283 SFML_NETWORK_API std::istream& operator >>(std::istream& stream, IpAddress& address);
0284 
0285 ////////////////////////////////////////////////////////////
0286 /// \brief Overload of << operator to print an IP address to an output stream
0287 ///
0288 /// \param stream  Output stream
0289 /// \param address IP address to print
0290 ///
0291 /// \return Reference to the output stream
0292 ///
0293 ////////////////////////////////////////////////////////////
0294 SFML_NETWORK_API std::ostream& operator <<(std::ostream& stream, const IpAddress& address);
0295 
0296 } // namespace sf
0297 
0298 
0299 #endif // SFML_IPADDRESS_HPP
0300 
0301 
0302 ////////////////////////////////////////////////////////////
0303 /// \class sf::IpAddress
0304 /// \ingroup network
0305 ///
0306 /// sf::IpAddress is a utility class for manipulating network
0307 /// addresses. It provides a set a implicit constructors and
0308 /// conversion functions to easily build or transform an IP
0309 /// address from/to various representations.
0310 ///
0311 /// Usage example:
0312 /// \code
0313 /// sf::IpAddress a0;                                     // an invalid address
0314 /// sf::IpAddress a1 = sf::IpAddress::None;               // an invalid address (same as a0)
0315 /// sf::IpAddress a2("127.0.0.1");                        // the local host address
0316 /// sf::IpAddress a3 = sf::IpAddress::Broadcast;          // the broadcast address
0317 /// sf::IpAddress a4(192, 168, 1, 56);                    // a local address
0318 /// sf::IpAddress a5("my_computer");                      // a local address created from a network name
0319 /// sf::IpAddress a6("89.54.1.169");                      // a distant address
0320 /// sf::IpAddress a7("www.google.com");                   // a distant address created from a network name
0321 /// sf::IpAddress a8 = sf::IpAddress::getLocalAddress();  // my address on the local network
0322 /// sf::IpAddress a9 = sf::IpAddress::getPublicAddress(); // my address on the internet
0323 /// \endcode
0324 ///
0325 /// Note that sf::IpAddress currently doesn't support IPv6
0326 /// nor other types of network addresses.
0327 ///
0328 ////////////////////////////////////////////////////////////