OpenTTD Source
1.11.2
|
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 (const char *hostname="", uint16 port=0, int family=AF_UNSPEC) | |
Create a network address based on a unresolved host and port. More... | |
const char * | GetHostname () |
Get the hostname; in case it wasn't given the IPv4 dotted representation is given. More... | |
void | GetAddressAsString (char *buffer, const char *last, bool with_family=true) |
Get the address as a string, e.g. 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 | GetPort () const |
Get the port. More... | |
void | SetPort (uint16 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 char *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... | |
SOCKET | Connect () |
Connect to the given address. 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... | |
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 | |
char | hostname [NETWORK_HOSTNAME_LENGTH] |
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. | |
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.
|
private |
|
inline |
Create a network address based on a resolved IP and port.
address | The IP address with port. |
address_length | The length of the address. |
Definition at line 50 of file address.h.
Referenced by IsInNetmask().
|
inline |
Create a network address based on a resolved IP and port.
address | The IP address with port. |
address_length | The length of the address. |
Definition at line 63 of file address.h.
References address, and address_length.
|
inline |
|
static |
Convert the address family into a string.
family | the family to convert |
Definition at line 454 of file address.cpp.
Referenced by ConnectLoopProc(), ListenLoopProc(), MasterNetworkUDPSocketHandler::Receive_MASTER_ACK_REGISTER(), MasterNetworkUDPSocketHandler::Receive_MASTER_SESSION_KEY(), and Resolve().
|
inline |
Compare the address of this class with the address of another.
address | the other address. |
Definition at line 130 of file address.h.
References address, GetAddressLength(), and GetPort().
Referenced by NetworkGameWindow::NGameNameSorter(), operator!=(), operator<(), and operator==().
SOCKET NetworkAddress::Connect | ( | ) |
Connect to the given address.
Definition at line 351 of file address.cpp.
References ConnectLoopProc(), DEBUG, GetAddressAsString(), and Resolve().
Referenced by TCPConnecter::Connect().
const sockaddr_storage * NetworkAddress::GetAddress | ( | ) |
Get the address in its internal representation.
Definition at line 123 of file address.cpp.
References address, IsResolved(), Resolve(), resolved, and ResolveLoopProc().
Referenced by GetAddressAsString(), GetAddressLength(), IsInNetmask(), MasterNetworkUDPSocketHandler::Receive_MASTER_ACK_REGISTER(), MasterNetworkUDPSocketHandler::Receive_MASTER_SESSION_KEY(), ClientNetworkUDPSocketHandler::Receive_SERVER_RESPONSE(), and NetworkUDPSocketHandler::SendPacket().
std::string NetworkAddress::GetAddressAsString | ( | bool | with_family = true | ) |
Get the address as a string, e.g.
127.0.0.1:12345.
with_family | whether to add the family (e.g. IPvX). |
Definition at line 100 of file address.cpp.
References GetAddressAsString(), lastof, and NETWORK_HOSTNAME_LENGTH.
void NetworkAddress::GetAddressAsString | ( | char * | buffer, |
const char * | last, | ||
bool | with_family = true |
||
) |
Get the address as a string, e.g.
127.0.0.1:12345.
buffer | the buffer to write to |
last | the last element in the buffer |
with_family | whether to add the family (e.g. IPvX). |
Definition at line 77 of file address.cpp.
References address, GetAddress(), GetHostname(), GetPort(), seprintf(), and strecpy().
Referenced by Connect(), ConnectLoopProc(), DoNetworkUDPQueryServer(), GetAddressAsString(), NetworkContentSocketHandler::HandlePacket(), NetworkUDPSocketHandler::HandleUDPPacket(), ListenLoopProc(), NetworkUDPAdvertiseThread(), NetworkUDPQueryMasterServer(), ServerNetworkUDPSocketHandler::Receive_CLIENT_GET_NEWGRFS(), ClientNetworkUDPSocketHandler::Receive_SERVER_NEWGRFS(), ClientNetworkUDPSocketHandler::Receive_SERVER_RESPONSE(), NetworkContentSocketHandler::ReceiveInvalidPacket(), NetworkUDPSocketHandler::ReceiveInvalidPacket(), NetworkUDPSocketHandler::ReceivePackets(), and NetworkUDPSocketHandler::SendPacket().
|
inline |
Get the (valid) length of the address.
Definition at line 103 of file address.h.
References address_length, GetAddress(), and IsResolved().
Referenced by CompareTo(), IsInNetmask(), and NetworkUDPSocketHandler::SendPacket().
const char * NetworkAddress::GetHostname | ( | ) |
Get the hostname; in case it wasn't given the IPv4 dotted representation is given.
Definition at line 22 of file address.cpp.
References address, address_length, hostname, and StrEmpty().
Referenced by TCPListenHandler< ServerNetworkGameSocketHandler, PACKET_SERVER_FULL, PACKET_SERVER_BANNED >::AcceptClient(), DoNetworkUDPQueryServer(), GetAddressAsString(), ServerNetworkGameSocketHandler::GetClientIP(), NetworkGameListAddItem(), NetworkHTTPContentConnecter::OnConnect(), ServerNetworkUDPSocketHandler::Receive_CLIENT_FIND_SERVER(), ClientNetworkUDPSocketHandler::Receive_SERVER_RESPONSE(), and ServerNetworkAdminSocketHandler::SendClientInfo().
uint16 NetworkAddress::GetPort | ( | ) | const |
Get the port.
Definition at line 35 of file address.cpp.
References address.
Referenced by CompareTo(), GetAddressAsString(), Listen(), and Resolve().
bool NetworkAddress::IsFamily | ( | int | family | ) |
Checks of this address is of the given family.
family | the family to check against |
Definition at line 142 of file address.cpp.
References address, IsResolved(), Resolve(), and ResolveLoopProc().
Referenced by NetworkUDPSocketHandler::SendPacket().
bool NetworkAddress::IsInNetmask | ( | const char * | netmask | ) |
Checks whether this IP address is contained by the given netmask.
netmask | the netmask in CIDR notation to test against. |
Definition at line 156 of file address.cpp.
References address, GetAddress(), GetAddressLength(), IsResolved(), and NetworkAddress().
Referenced by TCPListenHandler< ServerNetworkGameSocketHandler, PACKET_SERVER_FULL, PACKET_SERVER_BANNED >::AcceptClient().
|
inline |
Check whether the IP address has been resolved already.
Definition at line 117 of file address.h.
References resolved.
Referenced by GetAddress(), GetAddressLength(), IsFamily(), IsInNetmask(), and NetworkUDPQueryServer().
void NetworkAddress::Listen | ( | int | socktype, |
SocketList * | sockets | ||
) |
Make the given socket listen.
socktype | the type of socket (TCP, UDP, etc) |
sockets | the list of sockets to add the sockets to |
Definition at line 417 of file address.cpp.
References address, address_length, GetPort(), hostname, ListenLoopProc(), Resolve(), and StrEmpty().
|
inline |
Compare the address of this class with the address of another.
address | the other address. |
Definition at line 163 of file address.h.
References address, and CompareTo().
|
inline |
Compare the address of this class with the address of another.
address | the other address. |
Definition at line 172 of file address.h.
References CompareTo().
|
inline |
Compare the address of this class with the address of another.
address | the other address. |
Definition at line 144 of file address.h.
References CompareTo().
|
inline |
Compare the address of this class with the address of another.
address | the other address. |
Definition at line 154 of file address.h.
References address, and CompareTo().
|
private |
Resolve this address into a socket.
family | the type of 'protocol' (IPv4, IPv6) |
socktype | the type of socket (TCP, UDP, etc) |
flags | the flags to send to getaddrinfo |
sockets | the list of sockets to add the sockets to |
func | the inner working while looping over the address info |
Definition at line 218 of file address.cpp.
References address, address_length, AddressFamilyAsString(), SmallMap< T, U >::Contains(), DEBUG, FS2OTTD(), GetPort(), hostname, lastof, ResolveLoopProc(), seprintf(), SocketTypeAsString(), strecpy(), and StrEmpty().
Referenced by Connect(), GetAddress(), IsFamily(), and Listen().
void NetworkAddress::SetPort | ( | uint16 | port | ) |
Set the port.
port | set the port number. |
Definition at line 54 of file address.cpp.
References address.
Referenced by NetworkAddress().
|
static |
Convert the socket type into a string.
socktype | the socket type to convert |
Definition at line 439 of file address.cpp.
Referenced by ConnectLoopProc(), ListenLoopProc(), and Resolve().