Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 62 additions & 65 deletions include/SFML/Network/IpAddress.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public :
/// This constructor uses the internal representation of
/// the address directly. It should be used for optimization
/// purposes, and only if you got that representation from
/// IpAddress::ToInteger().
/// IpAddress::toInteger().
///
/// \param address 4 bytes of the address packed into a 32-bits integer
///
Expand Down Expand Up @@ -182,82 +182,79 @@ public :
// Static member data
////////////////////////////////////////////////////////////
static const IpAddress None; ///< Value representing an empty/invalid address
static const IpAddress Any; ///< Value representing any (valid) address
static const IpAddress LocalHost; ///< The "localhost" address (for connecting a computer to itself locally)
static const IpAddress Broadcast; ///< The "broadcast" address (for sending UDP messages to everyone on a local network)

private :
////////////////////////////////////////////////////////////
/// \brief Overload of == operator to compare two IP addresses
///
/// \param right Right operand (a IP address)
///
/// \return True if \a *this and \a right are equal
///
////////////////////////////////////////////////////////////
bool operator ==(const IpAddress& right) const;

////////////////////////////////////////////////////////////
// Member data
/// \brief Overload of != operator to compare two IP addresses
///
/// \param right Right operand (a IP address)
///
/// \return True if both \a *this and \a right are different
///
////////////////////////////////////////////////////////////
Uint32 m_address; ///< Address stored as an unsigned 32 bits integer
};
bool operator !=(const IpAddress& right) const;

////////////////////////////////////////////////////////////
/// \brief Overload of == operator to compare two IP addresses
///
/// \param left Left operand (a IP address)
/// \param right Right operand (a IP address)
///
/// \return True if both addresses are equal
///
////////////////////////////////////////////////////////////
SFML_NETWORK_API bool operator ==(const IpAddress& left, const IpAddress& right);
////////////////////////////////////////////////////////////
/// \brief Overload of < operator to compare two IP addresses
///
/// \param right Right operand (a IP address)
///
/// \return True if \a *this is lesser than \a right
///
////////////////////////////////////////////////////////////
bool operator <(const IpAddress& right) const;

////////////////////////////////////////////////////////////
/// \brief Overload of != operator to compare two IP addresses
///
/// \param left Left operand (a IP address)
/// \param right Right operand (a IP address)
///
/// \return True if both addresses are different
///
////////////////////////////////////////////////////////////
SFML_NETWORK_API bool operator !=(const IpAddress& left, const IpAddress& right);
////////////////////////////////////////////////////////////
/// \brief Overload of > operator to compare two IP addresses
///
/// \param right Right operand (a IP address)
///
/// \return True if \a *this is greater than \a right
///
////////////////////////////////////////////////////////////
bool operator >(const IpAddress& right) const;

////////////////////////////////////////////////////////////
/// \brief Overload of < operator to compare two IP addresses
///
/// \param left Left operand (a IP address)
/// \param right Right operand (a IP address)
///
/// \return True if \a left is lesser than \a right
///
////////////////////////////////////////////////////////////
SFML_NETWORK_API bool operator <(const IpAddress& left, const IpAddress& right);
////////////////////////////////////////////////////////////
/// \brief Overload of <= operator to compare two IP addresses
///
/// \param right Right operand (a IP address)
///
/// \return True if \a *this is lesser or equal than \a right
///
////////////////////////////////////////////////////////////
bool operator <=(const IpAddress& right) const;

////////////////////////////////////////////////////////////
/// \brief Overload of > operator to compare two IP addresses
///
/// \param left Left operand (a IP address)
/// \param right Right operand (a IP address)
///
/// \return True if \a left is greater than \a right
///
////////////////////////////////////////////////////////////
SFML_NETWORK_API bool operator >(const IpAddress& left, const IpAddress& right);
////////////////////////////////////////////////////////////
/// \brief Overload of >= operator to compare two IP addresses
///
/// \param right Right operand (a IP address)
///
/// \return True if \a *this is greater or equal than \a right
///
////////////////////////////////////////////////////////////
bool operator >=(const IpAddress& right) const;

////////////////////////////////////////////////////////////
/// \brief Overload of <= operator to compare two IP addresses
///
/// \param left Left operand (a IP address)
/// \param right Right operand (a IP address)
///
/// \return True if \a left is lesser or equal than \a right
///
////////////////////////////////////////////////////////////
SFML_NETWORK_API bool operator <=(const IpAddress& left, const IpAddress& right);
private :

////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
Uint32 m_address; ///< Address stored as an unsigned 32 bits integer
bool m_valid; ///< Flag indicating if it's valid address
};

////////////////////////////////////////////////////////////
/// \brief Overload of >= operator to compare two IP addresses
///
/// \param left Left operand (a IP address)
/// \param right Right operand (a IP address)
///
/// \return True if \a left is greater or equal than \a right
///
////////////////////////////////////////////////////////////
SFML_NETWORK_API bool operator >=(const IpAddress& left, const IpAddress& right);

////////////////////////////////////////////////////////////
/// \brief Overload of >> operator to extract an IP address from an input stream
Expand Down
4 changes: 3 additions & 1 deletion include/SFML/Network/TcpListener.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
////////////////////////////////////////////////////////////
#include <SFML/Network/Export.hpp>
#include <SFML/Network/Socket.hpp>
#include <SFML/Network/IpAddress.hpp>


namespace sf
Expand Down Expand Up @@ -72,13 +73,14 @@ public :
/// it will be stopped first and bound to the new port.
///
/// \param port Port to listen for new connections
/// \param address Address of interface to listen
///
/// \return Status code
///
/// \see accept, close
///
////////////////////////////////////////////////////////////
Status listen(unsigned short port);
Status listen(unsigned short port, const IpAddress& address = IpAddress::Any);

////////////////////////////////////////////////////////////
/// \brief Stop listening and close the socket
Expand Down
5 changes: 3 additions & 2 deletions include/SFML/Network/UdpSocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
////////////////////////////////////////////////////////////
#include <SFML/Network/Export.hpp>
#include <SFML/Network/Socket.hpp>
#include <SFML/Network/IpAddress.hpp>
#include <vector>


namespace sf
{
class IpAddress;
class Packet;

////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -83,13 +83,14 @@ public :
/// call getLocalPort to retrieve the chosen port.
///
/// \param port Port to bind the socket to
/// \param address Address of interface to bind to
///
/// \return Status code
///
/// \see unbind, getLocalPort
///
////////////////////////////////////////////////////////////
Status bind(unsigned short port);
Status bind(unsigned short port, const IpAddress& address = sf::IpAddress::Any);

////////////////////////////////////////////////////////////
/// \brief Unbind the socket from the local port to which it is bound
Expand Down
40 changes: 23 additions & 17 deletions src/SFML/Network/IpAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,15 @@ namespace sf
{
////////////////////////////////////////////////////////////
const IpAddress IpAddress::None;
const IpAddress IpAddress::Any(0, 0, 0, 0);
const IpAddress IpAddress::LocalHost(127, 0, 0, 1);
const IpAddress IpAddress::Broadcast(255, 255, 255, 255);


////////////////////////////////////////////////////////////
IpAddress::IpAddress() :
m_address(0)
m_address(0),
m_valid(false)
{
// We're using 0 (INADDR_ANY) instead of INADDR_NONE to represent the invalid address,
// because the latter is also the broadcast address (255.255.255.255); it's ok because
Expand All @@ -90,28 +92,32 @@ m_address(0)

////////////////////////////////////////////////////////////
IpAddress::IpAddress(const std::string& address) :
m_address(resolve(address))
m_address(resolve(address)),
m_valid(true)
{
}


////////////////////////////////////////////////////////////
IpAddress::IpAddress(const char* address) :
m_address(resolve(address))
m_address(resolve(address)),
m_valid(true)
{
}


////////////////////////////////////////////////////////////
IpAddress::IpAddress(Uint8 byte0, Uint8 byte1, Uint8 byte2, Uint8 byte3) :
m_address(htonl((byte0 << 24) | (byte1 << 16) | (byte2 << 8) | byte3))
m_address(htonl((byte0 << 24) | (byte1 << 16) | (byte2 << 8) | byte3)),
m_valid(true)
{
}


////////////////////////////////////////////////////////////
IpAddress::IpAddress(Uint32 address) :
m_address(htonl(address))
m_address(htonl(address)),
m_valid(true)
{
}

Expand Down Expand Up @@ -194,44 +200,44 @@ IpAddress IpAddress::getPublicAddress(Time timeout)


////////////////////////////////////////////////////////////
bool operator ==(const IpAddress& left, const IpAddress& right)
bool IpAddress::operator ==(const IpAddress& right) const
{
return left.toInteger() == right.toInteger();
return m_valid && right.m_valid && toInteger() == right.toInteger();
}


////////////////////////////////////////////////////////////
bool operator !=(const IpAddress& left, const IpAddress& right)
bool IpAddress::operator !=(const IpAddress& right) const
{
return !(left == right);
return !(*this == right);
}


////////////////////////////////////////////////////////////
bool operator <(const IpAddress& left, const IpAddress& right)
bool IpAddress::operator <(const IpAddress& right) const
{
return left.toInteger() < right.toInteger();
return m_valid && right.m_valid && toInteger() < right.toInteger();
}


////////////////////////////////////////////////////////////
bool operator >(const IpAddress& left, const IpAddress& right)
bool IpAddress::operator >(const IpAddress& right) const
{
return right < left;
return right < *this;
}


////////////////////////////////////////////////////////////
bool operator <=(const IpAddress& left, const IpAddress& right)
bool IpAddress::operator <=(const IpAddress& right) const
{
return !(right < left);
return !(right < *this);
}


////////////////////////////////////////////////////////////
bool operator >=(const IpAddress& left, const IpAddress& right)
bool IpAddress::operator >=(const IpAddress& right) const
{
return !(left < right);
return !(*this < right);
}


Expand Down
6 changes: 3 additions & 3 deletions src/SFML/Network/TcpListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ unsigned short TcpListener::getLocalPort() const


////////////////////////////////////////////////////////////
Socket::Status TcpListener::listen(unsigned short port)
Socket::Status TcpListener::listen(unsigned short port, const IpAddress& address)
{
// Create the internal socket if it doesn't exist
create();

// Bind the socket to the specified port
sockaddr_in address = priv::SocketImpl::createAddress(INADDR_ANY, port);
if (bind(getHandle(), reinterpret_cast<sockaddr*>(&address), sizeof(address)) == -1)
sockaddr_in addr = priv::SocketImpl::createAddress(address.toInteger(), port);
if (bind(getHandle(), reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) == -1)
{
// Not likely to happen, but...
err() << "Failed to bind listener socket to port " << port << std::endl;
Expand Down
6 changes: 3 additions & 3 deletions src/SFML/Network/UdpSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ unsigned short UdpSocket::getLocalPort() const


////////////////////////////////////////////////////////////
Socket::Status UdpSocket::bind(unsigned short port)
Socket::Status UdpSocket::bind(unsigned short port, const IpAddress& address)
{
// Create the internal socket if it doesn't exist
create();

// Bind the socket
sockaddr_in address = priv::SocketImpl::createAddress(INADDR_ANY, port);
if (::bind(getHandle(), reinterpret_cast<sockaddr*>(&address), sizeof(address)) == -1)
sockaddr_in addr = priv::SocketImpl::createAddress(address.toInteger(), port);
if (::bind(getHandle(), reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) == -1)
{
err() << "Failed to bind socket to port " << port << std::endl;
return Error;
Expand Down