1 ////////////////////////////////////////////////////////////
\r
3 // SFML - Simple and Fast Multimedia Library
\r
4 // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
\r
6 // This software is provided 'as-is', without any express or implied warranty.
\r
7 // In no event will the authors be held liable for any damages arising from the use of this software.
\r
9 // Permission is granted to anyone to use this software for any purpose,
\r
10 // including commercial applications, and to alter it and redistribute it freely,
\r
11 // subject to the following restrictions:
\r
13 // 1. The origin of this software must not be misrepresented;
\r
14 // you must not claim that you wrote the original software.
\r
15 // If you use this software in a product, an acknowledgment
\r
16 // in the product documentation would be appreciated but is not required.
\r
18 // 2. Altered source versions must be plainly marked as such,
\r
19 // and must not be misrepresented as being the original software.
\r
21 // 3. This notice may not be removed or altered from any source distribution.
\r
23 ////////////////////////////////////////////////////////////
\r
25 #ifndef SFML_IPADDRESS_HPP
\r
26 #define SFML_IPADDRESS_HPP
\r
28 ////////////////////////////////////////////////////////////
\r
30 ////////////////////////////////////////////////////////////
\r
31 #include <SFML/Config.hpp>
\r
39 ////////////////////////////////////////////////////////////
\r
40 /// IPAddress provides easy manipulation of IP v4 addresses
\r
41 ////////////////////////////////////////////////////////////
\r
42 class SFML_API IPAddress
\r
46 ////////////////////////////////////////////////////////////
\r
47 /// Default constructor -- constructs an invalid address
\r
49 ////////////////////////////////////////////////////////////
\r
52 ////////////////////////////////////////////////////////////
\r
53 /// Construct the address from a string
\r
55 /// \param Address : IP address ("xxx.xxx.xxx.xxx") or network name
\r
57 ////////////////////////////////////////////////////////////
\r
58 IPAddress(const std::string& Address);
\r
60 ////////////////////////////////////////////////////////////
\r
61 /// Construct the address from a C-style string ;
\r
62 /// Needed for implicit conversions from literal strings to IPAddress to work
\r
64 /// \param Address : IP address ("xxx.xxx.xxx.xxx") or network name
\r
66 ////////////////////////////////////////////////////////////
\r
67 IPAddress(const char* Address);
\r
69 ////////////////////////////////////////////////////////////
\r
70 /// Construct the address from 4 bytes
\r
72 /// \param Byte0 : First byte of the address
\r
73 /// \param Byte1 : Second byte of the address
\r
74 /// \param Byte2 : Third byte of the address
\r
75 /// \param Byte3 : Fourth byte of the address
\r
77 ////////////////////////////////////////////////////////////
\r
78 IPAddress(Uint8 Byte0, Uint8 Byte1, Uint8 Byte2, Uint8 Byte3);
\r
80 ////////////////////////////////////////////////////////////
\r
81 /// Construct the address from a 32-bits integer
\r
83 /// \param Address : 4 bytes of the address packed into a 32-bits integer
\r
85 ////////////////////////////////////////////////////////////
\r
86 IPAddress(Uint32 Address);
\r
88 ////////////////////////////////////////////////////////////
\r
89 /// Tell if the address is a valid one
\r
91 /// \return True if address has a valid syntax
\r
93 ////////////////////////////////////////////////////////////
\r
94 bool IsValid() const;
\r
96 ////////////////////////////////////////////////////////////
\r
97 /// Get a string representation of the address
\r
99 /// \return String representation of the IP address ("xxx.xxx.xxx.xxx")
\r
101 ////////////////////////////////////////////////////////////
\r
102 std::string ToString() const;
\r
104 ////////////////////////////////////////////////////////////
\r
105 /// Get an integer representation of the address
\r
107 /// \return 32-bits integer containing the 4 bytes of the address, in system endianness
\r
109 ////////////////////////////////////////////////////////////
\r
110 Uint32 ToInteger() const;
\r
112 ////////////////////////////////////////////////////////////
\r
113 /// Get the computer's local IP address (from the LAN point of view)
\r
115 /// \return Local IP address
\r
117 ////////////////////////////////////////////////////////////
\r
118 static IPAddress GetLocalAddress();
\r
120 ////////////////////////////////////////////////////////////
\r
121 /// Get the computer's public IP address (from the web point of view).
\r
122 /// The only way to get a public address is to ask it to a
\r
123 /// distant website ; as a consequence, this function may be
\r
124 /// very slow -- use it as few as possible !
\r
126 /// \param Timeout : Maximum time to wait, in seconds (0 by default : no timeout)
\r
128 /// \return Public IP address
\r
130 ////////////////////////////////////////////////////////////
\r
131 static IPAddress GetPublicAddress(float Timeout = 0.f);
\r
133 ////////////////////////////////////////////////////////////
\r
134 /// Comparison operator ==
\r
136 /// \param Other : Address to compare
\r
138 /// \return True if *this == Other
\r
140 ////////////////////////////////////////////////////////////
\r
141 bool operator ==(const IPAddress& Other) const;
\r
143 ////////////////////////////////////////////////////////////
\r
144 /// Comparison operator !=
\r
146 /// \param Other : Address to compare
\r
148 /// \return True if *this != Other
\r
150 ////////////////////////////////////////////////////////////
\r
151 bool operator !=(const IPAddress& Other) const;
\r
153 ////////////////////////////////////////////////////////////
\r
154 /// Comparison operator <
\r
156 /// \param Other : Address to compare
\r
158 /// \return True if *this < Other
\r
160 ////////////////////////////////////////////////////////////
\r
161 bool operator <(const IPAddress& Other) const;
\r
163 ////////////////////////////////////////////////////////////
\r
164 /// Comparison operator >
\r
166 /// \param Other : Address to compare
\r
168 /// \return True if *this > Other
\r
170 ////////////////////////////////////////////////////////////
\r
171 bool operator >(const IPAddress& Other) const;
\r
173 ////////////////////////////////////////////////////////////
\r
174 /// Comparison operator <=
\r
176 /// \param Other : Address to compare
\r
178 /// \return True if *this <= Other
\r
180 ////////////////////////////////////////////////////////////
\r
181 bool operator <=(const IPAddress& Other) const;
\r
183 ////////////////////////////////////////////////////////////
\r
184 /// Comparison operator >=
\r
186 /// \param Other : Address to compare
\r
188 /// \return True if *this >= Other
\r
190 ////////////////////////////////////////////////////////////
\r
191 bool operator >=(const IPAddress& Other) const;
\r
193 ////////////////////////////////////////////////////////////
\r
194 // Static member data
\r
195 ////////////////////////////////////////////////////////////
\r
196 static const IPAddress LocalHost; ///< Local host address (to connect to the same computer)
\r
200 ////////////////////////////////////////////////////////////
\r
202 ////////////////////////////////////////////////////////////
\r
203 Uint32 myAddress; ///< Address stored as an unsigned 32 bits integer
\r
206 ////////////////////////////////////////////////////////////
\r
207 /// Operator >> overload to extract an address from an input stream
\r
209 /// \param Stream : Input stream
\r
210 /// \param Address : Address to extract
\r
212 /// \return Reference to the input stream
\r
214 ////////////////////////////////////////////////////////////
\r
215 SFML_API std::istream& operator >>(std::istream& Stream, IPAddress& Address);
\r
217 ////////////////////////////////////////////////////////////
\r
218 /// Operator << overload to print an address to an output stream
\r
220 /// \param Stream : Output stream
\r
221 /// \param Address : Address to print
\r
223 /// \return Reference to the output stream
\r
225 ////////////////////////////////////////////////////////////
\r
226 SFML_API std::ostream& operator <<(std::ostream& Stream, const IPAddress& Address);
\r
231 #endif // SFML_IPADDRESS_HPP
\r