Skip to content

Commit

Permalink
Added optional argument on which address to bind (socket).
Browse files Browse the repository at this point in the history
  • Loading branch information
bumbar1 authored and eXpl0it3r committed Oct 10, 2015
1 parent 6b97814 commit 3a12fc6
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 61 deletions.
14 changes: 13 additions & 1 deletion include/SFML/Network/IpAddress.hpp
Expand Up @@ -99,7 +99,7 @@ class SFML_NETWORK_API IpAddress
/// 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,15 +182,27 @@ class SFML_NETWORK_API IpAddress
// Static member data
////////////////////////////////////////////////////////////
static const IpAddress None; ///< Value representing an empty/invalid address
static const IpAddress Any; ///< Value representing any address (0.0.0.0)
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:

friend SFML_NETWORK_API bool operator <(const IpAddress& left, const IpAddress& right);

////////////////////////////////////////////////////////////
/// \brief Resolve the given address string
///
/// \param address Address string
///
////////////////////////////////////////////////////////////
void resolve(const std::string& address);

////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
Uint32 m_address; ///< Address stored as an unsigned 32 bits integer
bool m_valid; ///< Is the address valid?
};

////////////////////////////////////////////////////////////
Expand Down
6 changes: 4 additions & 2 deletions include/SFML/Network/TcpListener.hpp
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 @@ -71,14 +72,15 @@ class SFML_NETWORK_API TcpListener : public Socket
/// If the socket was previously listening to another port,
/// it will be stopped first and bound to the new port.
///
/// \param port Port to listen for new connections
/// \param port Port to listen for new connections
/// \param address Address of the interface to listen on
///
/// \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
7 changes: 4 additions & 3 deletions include/SFML/Network/UdpSocket.hpp
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 @@ -82,14 +82,15 @@ class SFML_NETWORK_API UdpSocket : public Socket
/// system to automatically pick an available port, and then
/// call getLocalPort to retrieve the chosen port.
///
/// \param port Port to bind the socket to
/// \param port Port to bind the socket to
/// \param address Address of the interface to bind to
///
/// \return Status code
///
/// \see unbind, getLocalPort
///
////////////////////////////////////////////////////////////
Status bind(unsigned short port);
Status bind(unsigned short port, const IpAddress& address = IpAddress::Any);

////////////////////////////////////////////////////////////
/// \brief Unbind the socket from the local port to which it is bound
Expand Down
114 changes: 65 additions & 49 deletions src/SFML/Network/IpAddress.cpp
Expand Up @@ -29,89 +29,56 @@
#include <SFML/Network/Http.hpp>
#include <SFML/Network/SocketImpl.hpp>
#include <cstring>


namespace
{
sf::Uint32 resolve(const std::string& address)
{
if (address == "255.255.255.255")
{
// The broadcast address needs to be handled explicitly,
// because it is also the value returned by inet_addr on error
return INADDR_BROADCAST;
}
else
{
// Try to convert the address as a byte representation ("xxx.xxx.xxx.xxx")
sf::Uint32 ip = inet_addr(address.c_str());
if (ip != INADDR_NONE)
return ip;

// Not a valid address, try to convert it as a host name
addrinfo hints;
std::memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
addrinfo* result = NULL;
if (getaddrinfo(address.c_str(), NULL, &hints, &result) == 0)
{
if (result)
{
ip = reinterpret_cast<sockaddr_in*>(result->ai_addr)->sin_addr.s_addr;
freeaddrinfo(result);
return ip;
}
}

// Not a valid address nor a host name
return 0;
}
}
}
#include <utility>


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
// SFML doesn't publicly use INADDR_ANY (it is always used implicitly)
}


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


////////////////////////////////////////////////////////////
IpAddress::IpAddress(const char* address) :
m_address(resolve(address))
m_address(0),
m_valid (false)
{
resolve(address);
}


////////////////////////////////////////////////////////////
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 @@ -193,10 +160,59 @@ IpAddress IpAddress::getPublicAddress(Time timeout)
}


////////////////////////////////////////////////////////////
void IpAddress::resolve(const std::string& address)
{
m_address = 0;
m_valid = false;

if (address == "255.255.255.255")
{
// The broadcast address needs to be handled explicitly,
// because it is also the value returned by inet_addr on error
m_address = INADDR_BROADCAST;
m_valid = true;
}
else if (address == "0.0.0.0")
{
m_address = INADDR_ANY;
m_valid = true;
}
else
{
// Try to convert the address as a byte representation ("xxx.xxx.xxx.xxx")
Uint32 ip = inet_addr(address.c_str());
if (ip != INADDR_NONE)
{
m_address = ip;
m_valid = true;
}
else
{
// Not a valid address, try to convert it as a host name
addrinfo hints;
std::memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
addrinfo* result = NULL;
if (getaddrinfo(address.c_str(), NULL, &hints, &result) == 0)
{
if (result)
{
ip = reinterpret_cast<sockaddr_in*>(result->ai_addr)->sin_addr.s_addr;
freeaddrinfo(result);
m_address = ip;
m_valid = true;
}
}
}
}
}


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


Expand All @@ -210,7 +226,7 @@ bool operator !=(const IpAddress& left, const IpAddress& right)
////////////////////////////////////////////////////////////
bool operator <(const IpAddress& left, const IpAddress& right)
{
return left.toInteger() < right.toInteger();
return std::make_pair(left.m_valid, left.m_address) < std::make_pair(right.m_valid, right.m_address);
}


Expand Down
10 changes: 7 additions & 3 deletions src/SFML/Network/TcpListener.cpp
Expand Up @@ -61,14 +61,18 @@ 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();

// Check if the address is valid
if ((address == IpAddress::None) || (address == IpAddress::Broadcast))
return Error;

// 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
10 changes: 7 additions & 3 deletions src/SFML/Network/UdpSocket.cpp
Expand Up @@ -64,14 +64,18 @@ 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();

// Check if the address is valid
if ((address == IpAddress::None) || (address == IpAddress::Broadcast))
return Error;

// 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

0 comments on commit 3a12fc6

Please sign in to comment.