OpenTTD Source  20241108-master-g80f628063a
NetworkAddress Class Reference

Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string and then back again to pass it to functions. More...

#include <address.h>

Public Member Functions

 NetworkAddress (struct sockaddr_storage &address, int address_length)
 Create a network address based on a resolved IP and port. More...
 
 NetworkAddress (sockaddr *address, int address_length)
 Create a network address based on a resolved IP and port. More...
 
 NetworkAddress (std::string_view hostname="", uint16_t port=0, int family=AF_UNSPEC)
 Create a network address based on a unresolved host and port. More...
 
const std::string & GetHostname ()
 Get the hostname; in case it wasn't given the IPv4 dotted representation is given. More...
 
std::string GetAddressAsString (bool with_family=true)
 Get the address as a string, e.g. More...
 
const sockaddr_storage * GetAddress ()
 Get the address in its internal representation. More...
 
int GetAddressLength ()
 Get the (valid) length of the address. More...
 
uint16_t GetPort () const
 Get the port. More...
 
void SetPort (uint16_t port)
 Set the port. More...
 
bool IsResolved () const
 Check whether the IP address has been resolved already. More...
 
bool IsFamily (int family)
 Checks of this address is of the given family. More...
 
bool IsInNetmask (const std::string &netmask)
 Checks whether this IP address is contained by the given netmask. More...
 
int CompareTo (NetworkAddress &address)
 Compare the address of this class with the address of another. More...
 
bool operator== (NetworkAddress &address)
 Compare the address of this class with the address of another. More...
 
bool operator== (NetworkAddress &address) const
 Compare the address of this class with the address of another. More...
 
bool operator!= (NetworkAddress address) const
 Compare the address of this class with the address of another. More...
 
bool operator< (NetworkAddress &address)
 Compare the address of this class with the address of another. More...
 
void Listen (int socktype, SocketList *sockets)
 Make the given socket listen. More...
 

Static Public Member Functions

static const char * SocketTypeAsString (int socktype)
 Convert the socket type into a string. More...
 
static const char * AddressFamilyAsString (int family)
 Convert the address family into a string. More...
 
static NetworkAddress GetPeerAddress (SOCKET sock)
 Get the peer address of a socket as NetworkAddress. More...
 
static NetworkAddress GetSockAddress (SOCKET sock)
 Get the local address of a socket as NetworkAddress. More...
 
static const std::string GetPeerName (SOCKET sock)
 Get the peer name of a socket in string format. More...
 

Private Types

typedef SOCKET(* LoopProc) (addrinfo *runp)
 Helper function to resolve something to a socket. More...
 

Private Member Functions

SOCKET Resolve (int family, int socktype, int flags, SocketList *sockets, LoopProc func)
 Resolve this address into a socket. More...
 

Private Attributes

std::string hostname
 The hostname.
 
int address_length
 The length of the resolved address.
 
sockaddr_storage address
 The resolved address.
 
bool resolved
 Whether the address has been (tried to be) resolved.
 

Detailed Description

Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string and then back again to pass it to functions.

It furthermore allows easier delaying of the hostname lookup.

Definition at line 28 of file address.h.

Member Typedef Documentation

◆ LoopProc

typedef SOCKET(* NetworkAddress::LoopProc) (addrinfo *runp)
private

Helper function to resolve something to a socket.

Parameters
runpinformation about the socket to try not
Returns
the opened socket or INVALID_SOCKET

Definition at line 40 of file address.h.

Constructor & Destructor Documentation

◆ NetworkAddress() [1/3]

NetworkAddress::NetworkAddress ( struct sockaddr_storage &  address,
int  address_length 
)
inline

Create a network address based on a resolved IP and port.

Parameters
addressThe IP address with port.
address_lengthThe length of the address.

Definition at line 49 of file address.h.

Referenced by GetPeerAddress(), GetSockAddress(), and IsInNetmask().

◆ NetworkAddress() [2/3]

NetworkAddress::NetworkAddress ( sockaddr *  address,
int  address_length 
)
inline

Create a network address based on a resolved IP and port.

Parameters
addressThe IP address with port.
address_lengthThe length of the address.

Definition at line 61 of file address.h.

References address, and address_length.

◆ NetworkAddress() [3/3]

NetworkAddress::NetworkAddress ( std::string_view  hostname = "",
uint16_t  port = 0,
int  family = AF_UNSPEC 
)
inline

Create a network address based on a unresolved host and port.

Parameters
hostnamethe unresolved hostname
portthe port
familythe address family

Definition at line 75 of file address.h.

Member Function Documentation

◆ AddressFamilyAsString()

const char * NetworkAddress::AddressFamilyAsString ( int  family)
static

Convert the address family into a string.

Parameters
familythe family to convert
Returns
the string representation
Note
only works for AF_INET, AF_INET6 and AF_UNSPEC

Definition at line 388 of file address.cpp.

Referenced by TCPConnecter::Connect(), ListenLoopProc(), and Resolve().

◆ CompareTo()

int NetworkAddress::CompareTo ( NetworkAddress address)
inline

Compare the address of this class with the address of another.

Parameters
addressthe other address.
Returns
< 0 if address is less, 0 if equal and > 0 if address is more

Definition at line 125 of file address.h.

References address, GetAddressLength(), and GetPort().

Referenced by operator!=(), operator<(), and operator==().

◆ GetAddress()

const sockaddr_storage * NetworkAddress::GetAddress ( )

Get the address in its internal representation.

Returns
the address

Definition at line 113 of file address.cpp.

References address, IsResolved(), Resolve(), resolved, and ResolveLoopProc().

Referenced by TCPConnecter::Connect(), GetAddressAsString(), GetAddressLength(), IsInNetmask(), and NetworkUDPSocketHandler::SendPacket().

◆ GetAddressAsString()

std::string NetworkAddress::GetAddressAsString ( bool  with_family = true)

◆ GetAddressLength()

int NetworkAddress::GetAddressLength ( )
inline

Get the (valid) length of the address.

Returns
the length

Definition at line 98 of file address.h.

References address_length, GetAddress(), and IsResolved().

Referenced by CompareTo(), IsInNetmask(), and NetworkUDPSocketHandler::SendPacket().

◆ GetHostname()

◆ GetPeerAddress()

NetworkAddress NetworkAddress::GetPeerAddress ( SOCKET  sock)
static

Get the peer address of a socket as NetworkAddress.

Parameters
sockThe socket to get the peer address of.
Returns
The NetworkAddress of the peer address.

Definition at line 403 of file address.cpp.

References Debug, NetworkError::GetLast(), and NetworkAddress().

Referenced by GetPeerName().

◆ GetPeerName()

const std::string NetworkAddress::GetPeerName ( SOCKET  sock)
static

Get the peer name of a socket in string format.

Parameters
sockThe socket to get the peer name of.
Returns
The string representation of the peer name.

Definition at line 435 of file address.cpp.

References GetAddressAsString(), and GetPeerAddress().

◆ GetPort()

uint16_t NetworkAddress::GetPort ( ) const

Get the port.

Returns
the port.

Definition at line 38 of file address.cpp.

Referenced by CompareTo(), TCPConnecter::Connect(), GetAddressAsString(), TCPConnecter::Resolve(), and Resolve().

◆ GetSockAddress()

NetworkAddress NetworkAddress::GetSockAddress ( SOCKET  sock)
static

Get the local address of a socket as NetworkAddress.

Parameters
sockThe socket to get the local address of.
Returns
The NetworkAddress of the local address.

Definition at line 419 of file address.cpp.

References Debug, NetworkError::GetLast(), and NetworkAddress().

◆ IsFamily()

bool NetworkAddress::IsFamily ( int  family)

Checks of this address is of the given family.

Parameters
familythe family to check against
Returns
true if it is of the given family

Definition at line 132 of file address.cpp.

References address, IsResolved(), Resolve(), and ResolveLoopProc().

Referenced by NetworkUDPSocketHandler::SendPacket().

◆ IsInNetmask()

bool NetworkAddress::IsInNetmask ( const std::string &  netmask)

Checks whether this IP address is contained by the given netmask.

Parameters
netmaskthe netmask in CIDR notation to test against.
Note
netmask without /n assumes all bits need to match.
Returns
true if this IP is within the netmask.

Definition at line 146 of file address.cpp.

References address, GetAddress(), GetAddressLength(), IsResolved(), and NetworkAddress().

◆ IsResolved()

bool NetworkAddress::IsResolved ( ) const
inline

Check whether the IP address has been resolved already.

Returns
true iff the port has been resolved

Definition at line 112 of file address.h.

References resolved.

Referenced by GetAddress(), GetAddressLength(), IsFamily(), and IsInNetmask().

◆ Listen()

void NetworkAddress::Listen ( int  socktype,
SocketList sockets 
)

Make the given socket listen.

Parameters
socktypethe type of socket (TCP, UDP, etc)
socketsthe list of sockets to add the sockets to

Definition at line 351 of file address.cpp.

References address, address_length, ListenLoopProc(), and Resolve().

◆ operator!=()

bool NetworkAddress::operator!= ( NetworkAddress  address) const
inline

Compare the address of this class with the address of another.

Parameters
addressthe other address.
Returns
true if both do not match.

Definition at line 158 of file address.h.

References address, and CompareTo().

◆ operator<()

bool NetworkAddress::operator< ( NetworkAddress address)
inline

Compare the address of this class with the address of another.

Parameters
addressthe other address.

Definition at line 167 of file address.h.

References CompareTo().

◆ operator==() [1/2]

bool NetworkAddress::operator== ( NetworkAddress address)
inline

Compare the address of this class with the address of another.

Parameters
addressthe other address.
Returns
true if both match.

Definition at line 139 of file address.h.

References CompareTo().

◆ operator==() [2/2]

bool NetworkAddress::operator== ( NetworkAddress address) const
inline

Compare the address of this class with the address of another.

Parameters
addressthe other address.
Returns
true if both match.

Definition at line 149 of file address.h.

References address, and CompareTo().

◆ Resolve()

SOCKET NetworkAddress::Resolve ( int  family,
int  socktype,
int  flags,
SocketList sockets,
LoopProc  func 
)
private

Resolve this address into a socket.

Parameters
familythe type of 'protocol' (IPv4, IPv6)
socktypethe type of socket (TCP, UDP, etc)
flagsthe flags to send to getaddrinfo
socketsthe list of sockets to add the sockets to
functhe inner working while looping over the address info
Returns
the resolved socket or INVALID_SOCKET.

Definition at line 207 of file address.cpp.

References address, address_length, AddressFamilyAsString(), Debug, FS2OTTD(), GetPort(), hostname, ResolveLoopProc(), and SocketTypeAsString().

Referenced by GetAddress(), IsFamily(), and Listen().

◆ SetPort()

void NetworkAddress::SetPort ( uint16_t  port)

Set the port.

Parameters
portset the port number.

Definition at line 57 of file address.cpp.

◆ SocketTypeAsString()

const char * NetworkAddress::SocketTypeAsString ( int  socktype)
static

Convert the socket type into a string.

Parameters
socktypethe socket type to convert
Returns
the string representation
Note
only works for SOCK_STREAM and SOCK_DGRAM

Definition at line 373 of file address.cpp.

Referenced by TCPConnecter::Connect(), ListenLoopProc(), and Resolve().


The documentation for this class was generated from the following files: