|
OpenTTD Source 20251116-master-g21329071df
|
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. | |
| NetworkAddress (const sockaddr *address, int address_length) | |
| Create a network address based on a resolved IP and port. | |
| NetworkAddress (std::string_view hostname="", uint16_t port=0, int family=AF_UNSPEC) | |
| Create a network address based on a unresolved host and port. | |
| const std::string & | GetHostname () |
| Get the hostname; in case it wasn't given the IPv4 dotted representation is given. | |
| std::string | GetAddressAsString (bool with_family=true) |
| Get the address as a string, e.g. | |
| const sockaddr_storage * | GetAddress () |
| Get the address in its internal representation. | |
| int | GetAddressLength () |
| Get the (valid) length of the address. | |
| uint16_t | GetPort () const |
| Get the port. | |
| void | SetPort (uint16_t port) |
| Set the port. | |
| bool | IsResolved () const |
| Check whether the IP address has been resolved already. | |
| bool | IsFamily (int family) |
| Checks of this address is of the given family. | |
| bool | IsInNetmask (std::string_view netmask) |
| Checks whether this IP address is contained by the given netmask. | |
| int | CompareTo (NetworkAddress &address) |
| Compare the address of this class with the address of another. | |
| bool | operator== (NetworkAddress &address) |
| Compare the address of this class with the address of another. | |
| bool | operator== (NetworkAddress &address) const |
| Compare the address of this class with the address of another. | |
| auto | operator<=> (NetworkAddress &address) |
| Compare the address of this class with the address of another. | |
| void | Listen (int socktype, SocketList *sockets) |
| Make the given socket listen. | |
Static Public Member Functions | |
| static std::string_view | SocketTypeAsString (int socktype) |
| Convert the socket type into a string. | |
| static std::string_view | AddressFamilyAsString (int family) |
| Convert the address family into a string. | |
| static NetworkAddress | GetPeerAddress (SOCKET sock) |
| Get the peer address of a socket as NetworkAddress. | |
| static NetworkAddress | GetSockAddress (SOCKET sock) |
| Get the local address of a socket as NetworkAddress. | |
| static const std::string | GetPeerName (SOCKET sock) |
| Get the peer name of a socket in string format. | |
Private Types | |
| typedef SOCKET(* | LoopProc) (addrinfo *runp) |
| Helper function to resolve something to a socket. | |
Private Member Functions | |
| SOCKET | Resolve (int family, int socktype, int flags, SocketList *sockets, LoopProc func) |
| Resolve this address into a socket. | |
Private Attributes | |
| std::string | hostname {} |
| The hostname. | |
| int | address_length {} |
| The length of the resolved address. | |
| sockaddr_storage | address {} |
| The resolved address. | |
| bool | resolved = false |
| 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 |
|
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 61 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 382 of file address.cpp.
Referenced by TCPConnecter::Connect(), ListenLoopProc(), and Resolve().
|
inline |
Compare the address of this class with the address of another.
| address | the other address. |
Definition at line 122 of file address.h.
References address, GetAddressLength(), and GetPort().
Referenced by operator<=>(), operator==(), and operator==().
| const sockaddr_storage * NetworkAddress::GetAddress | ( | ) |
Get the address in its internal representation.
Definition at line 114 of file address.cpp.
References address, IsResolved(), Resolve(), resolved, and ResolveLoopProc().
Referenced by TCPConnecter::Connect(), GetAddressAsString(), GetAddressLength(), IsInNetmask(), 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 95 of file address.cpp.
References GetAddress(), GetAddressFormatString(), GetHostname(), and GetPort().
Referenced by TCPConnecter::Connect(), GetPeerName(), NetworkUDPSocketHandler::HandleUDPPacket(), ListenLoopProc(), TCPConnecter::OnResolved(), ClientNetworkUDPSocketHandler::Receive_SERVER_RESPONSE(), NetworkUDPSocketHandler::ReceiveInvalidPacket(), NetworkUDPSocketHandler::ReceivePackets(), and NetworkUDPSocketHandler::SendPacket().
|
inline |
Get the (valid) length of the address.
Definition at line 95 of file address.h.
References address_length, GetAddress(), and IsResolved().
Referenced by CompareTo(), IsInNetmask(), and NetworkUDPSocketHandler::SendPacket().
| const std::string & NetworkAddress::GetHostname | ( | ) |
Get the hostname; in case it wasn't given the IPv4 dotted representation is given.
Definition at line 24 of file address.cpp.
References address, address_length, hostname, and NETWORK_HOSTNAME_LENGTH.
Referenced by TCPListenHandler< Tsocket, Tfull_packet, Tban_packet >::AcceptClient(), ClientNetworkCoordinatorSocketHandler::ConnectSuccess(), GetAddressAsString(), ServerNetworkGameSocketHandler::GetClientIP(), ServerNetworkUDPSocketHandler::Receive_CLIENT_FIND_SERVER(), and TCPConnecter::Resolve().
|
static |
Get the peer address of a socket as NetworkAddress.
| sock | The socket to get the peer address of. |
Definition at line 397 of file address.cpp.
References Debug, and NetworkError::GetLast().
Referenced by GetPeerName(), NetworkDirectConnecter::OnConnect(), and NetworkReuseStunConnecter::OnConnect().
|
static |
Get the peer name of a socket in string format.
| sock | The socket to get the peer name of. |
Definition at line 429 of file address.cpp.
References GetAddressAsString(), and GetPeerAddress().
Referenced by TCPConnecter::CheckActivity().
| uint16_t NetworkAddress::GetPort | ( | ) | const |
Get the port.
Definition at line 39 of file address.cpp.
References address.
Referenced by CompareTo(), TCPConnecter::Connect(), GetAddressAsString(), TCPConnecter::Resolve(), and Resolve().
|
static |
Get the local address of a socket as NetworkAddress.
| sock | The socket to get the local address of. |
Definition at line 413 of file address.cpp.
References Debug, and NetworkError::GetLast().
Referenced by NetworkStunConnecter::OnConnect().
| bool NetworkAddress::IsFamily | ( | int | family | ) |
Checks of this address is of the given family.
| family | the family to check against |
Definition at line 133 of file address.cpp.
References address, IsResolved(), Resolve(), and ResolveLoopProc().
Referenced by NetworkUDPSocketHandler::SendPacket().
| bool NetworkAddress::IsInNetmask | ( | std::string_view | netmask | ) |
Checks whether this IP address is contained by the given netmask.
| netmask | the netmask in CIDR notation to test against. |
Definition at line 147 of file address.cpp.
References address, GetAddress(), GetAddressLength(), IsResolved(), and StringConsumer::SKIP_ONE_SEPARATOR.
|
inline |
Check whether the IP address has been resolved already.
Definition at line 109 of file address.h.
References resolved.
Referenced by GetAddress(), GetAddressLength(), IsFamily(), and IsInNetmask().
| 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 345 of file address.cpp.
References address, address_length, ListenLoopProc(), and Resolve().
|
inline |
Compare the address of this class with the address of another.
| address | the other address. |
Definition at line 155 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 136 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 146 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 202 of file address.cpp.
References address, address_length, AddressFamilyAsString(), Debug, FS2OTTD(), GetPort(), hostname, ResolveLoopProc(), and SocketTypeAsString().
Referenced by GetAddress(), IsFamily(), and Listen().
| void NetworkAddress::SetPort | ( | uint16_t | port | ) |
Set the port.
| port | set the port number. |
Definition at line 58 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 367 of file address.cpp.
Referenced by TCPConnecter::Connect(), ListenLoopProc(), and Resolve().
|
private |
The resolved address.
Definition at line 32 of file address.h.
Referenced by CompareTo(), GetAddress(), GetHostname(), GetPort(), IsFamily(), IsInNetmask(), Listen(), NetworkAddress(), NetworkAddress(), operator==(), Resolve(), and SetPort().
|
private |
The length of the resolved address.
Definition at line 31 of file address.h.
Referenced by GetAddressLength(), GetHostname(), Listen(), NetworkAddress(), and Resolve().
|
private |
The hostname.
Definition at line 30 of file address.h.
Referenced by GetHostname(), NetworkAddress(), and Resolve().
|
private |
Whether the address has been (tried to be) resolved.
Definition at line 33 of file address.h.
Referenced by GetAddress(), and IsResolved().