OpenTTD Source: src/network/core/address.h Source File

OpenTTD Source 15.3

address.h
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#ifndef NETWORK_CORE_ADDRESS_H
11#define NETWORK_CORE_ADDRESS_H
12
13#include "os_abstraction.h"
14#include "config.h"
15#include "../../company_type.h"
16#include "../../string_func.h"
17
18
19class NetworkAddress;
20 typedef std::vector<NetworkAddress> NetworkAddressList;
21 using SocketList = std::map<SOCKET, NetworkAddress>;
22
28 class NetworkAddress {
29private:
30 std::string hostname{};
31 int address_length{};
32 sockaddr_storage address{};
33 bool resolved = false;
34
40 typedef SOCKET (*LoopProc)(addrinfo *runp);
41
42 SOCKET Resolve(int family, int socktype, int flags, SocketList *sockets, LoopProc func);
43public:
49 NetworkAddress(struct sockaddr_storage &address, int address_length) :
50 address_length(address_length),
51 address(address),
52 resolved(address_length != 0)
53 {
54 }
55
61 NetworkAddress(const sockaddr *address, int address_length) :
62 address_length(address_length),
63 resolved(address_length != 0)
64 {
65 std::copy_n(reinterpret_cast<const std::byte *>(address), address_length, reinterpret_cast<std::byte *>(&this->address));
66 }
67
74 NetworkAddress(std::string_view hostname = "", uint16_t port = 0, int family = AF_UNSPEC) :
75 address_length(0),
76 resolved(false)
77 {
78 if (!hostname.empty() && hostname.front() == '[' && hostname.back() == ']') {
79 hostname.remove_prefix(1);
80 hostname.remove_suffix(1);
81 }
82 this->hostname = hostname;
83 this->address.ss_family = family;
84 this->SetPort(port);
85 }
86
87 const std::string &GetHostname();
88 std::string GetAddressAsString(bool with_family = true);
89 const sockaddr_storage *GetAddress();
90
95 int GetAddressLength()
96 {
97 /* Resolve it if we didn't do it already */
98 if (!this->IsResolved()) this->GetAddress();
99 return this->address_length;
100 }
101
102 uint16_t GetPort() const;
103 void SetPort(uint16_t port);
104
109 bool IsResolved() const
110 {
111 return this->resolved;
112 }
113
114 bool IsFamily(int family);
115 bool IsInNetmask(std::string_view netmask);
116
122 int CompareTo(NetworkAddress &address)
123 {
124 int r = this->GetAddressLength() - address.GetAddressLength();
125 if (r == 0) r = this->address.ss_family - address.address.ss_family;
126 if (r == 0) r = memcmp(&this->address, &address.address, this->address_length);
127 if (r == 0) r = this->GetPort() - address.GetPort();
128 return r;
129 }
130
136 bool operator == (NetworkAddress &address)
137 {
138 return this->CompareTo(address) == 0;
139 }
140
146 bool operator == (NetworkAddress &address) const
147 {
148 return const_cast<NetworkAddress*>(this)->CompareTo(address) == 0;
149 }
150
155 auto operator <=>(NetworkAddress &address)
156 {
157 return this->CompareTo(address) <=> 0;
158 }
159
160 void Listen(int socktype, SocketList *sockets);
161
162 static std::string_view SocketTypeAsString(int socktype);
163 static std::string_view AddressFamilyAsString(int family);
164 static NetworkAddress GetPeerAddress(SOCKET sock);
165 static NetworkAddress GetSockAddress(SOCKET sock);
166 static const std::string GetPeerName(SOCKET sock);
167};
168
174 enum ServerAddressType : uint8_t {
175 SERVER_ADDRESS_DIRECT,
176 SERVER_ADDRESS_INVITE_CODE,
177};
178
184 class ServerAddress {
185private:
194 ServerAddress(ServerAddressType type, std::string &&connection_string) : type(type), connection_string(std::move(connection_string)) {}
195
196public:
197 ServerAddressType type;
198 std::string connection_string;
199
200 static ServerAddress Parse(std::string_view connection_string, uint16_t default_port, CompanyID *company_id = nullptr);
201};
202
203#endif /* NETWORK_CORE_ADDRESS_H */
NetworkAddressList
std::vector< NetworkAddress > NetworkAddressList
Type for a list of addresses.
Definition address.h:20
ServerAddressType
ServerAddressType
Types of server addresses we know.
Definition address.h:174
SERVER_ADDRESS_INVITE_CODE
@ SERVER_ADDRESS_INVITE_CODE
Server-address is based on an invite code.
Definition address.h:176
SERVER_ADDRESS_DIRECT
@ SERVER_ADDRESS_DIRECT
Server-address is based on an hostname:port.
Definition address.h:175
SocketList
std::map< SOCKET, NetworkAddress > SocketList
Type for a mapping between address and socket.
Definition address.h:21
NetworkAddress
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
Definition address.h:28
NetworkAddress::GetPeerAddress
static NetworkAddress GetPeerAddress(SOCKET sock)
Get the peer address of a socket as NetworkAddress.
Definition address.cpp:397
NetworkAddress::NetworkAddress
NetworkAddress(std::string_view hostname="", uint16_t port=0, int family=AF_UNSPEC)
Create a network address based on a unresolved host and port.
Definition address.h:74
NetworkAddress::CompareTo
int CompareTo(NetworkAddress &address)
Compare the address of this class with the address of another.
Definition address.h:122
NetworkAddress::resolved
bool resolved
Whether the address has been (tried to be) resolved.
Definition address.h:33
NetworkAddress::GetAddressLength
int GetAddressLength()
Get the (valid) length of the address.
Definition address.h:95
NetworkAddress::operator<=>
auto operator<=>(NetworkAddress &address)
Compare the address of this class with the address of another.
Definition address.h:155
NetworkAddress::operator==
bool operator==(NetworkAddress &address)
Compare the address of this class with the address of another.
Definition address.h:136
NetworkAddress::address
sockaddr_storage address
The resolved address.
Definition address.h:32
NetworkAddress::AddressFamilyAsString
static std::string_view AddressFamilyAsString(int family)
Convert the address family into a string.
Definition address.cpp:382
NetworkAddress::IsFamily
bool IsFamily(int family)
Checks of this address is of the given family.
Definition address.cpp:133
NetworkAddress::IsResolved
bool IsResolved() const
Check whether the IP address has been resolved already.
Definition address.h:109
NetworkAddress::GetPeerName
static const std::string GetPeerName(SOCKET sock)
Get the peer name of a socket in string format.
Definition address.cpp:429
NetworkAddress::LoopProc
SOCKET(* LoopProc)(addrinfo *runp)
Helper function to resolve something to a socket.
Definition address.h:40
NetworkAddress::SocketTypeAsString
static std::string_view SocketTypeAsString(int socktype)
Convert the socket type into a string.
Definition address.cpp:367
NetworkAddress::GetSockAddress
static NetworkAddress GetSockAddress(SOCKET sock)
Get the local address of a socket as NetworkAddress.
Definition address.cpp:413
NetworkAddress::hostname
std::string hostname
The hostname.
Definition address.h:30
NetworkAddress::GetPort
uint16_t GetPort() const
Get the port.
Definition address.cpp:39
NetworkAddress::Listen
void Listen(int socktype, SocketList *sockets)
Make the given socket listen.
Definition address.cpp:345
NetworkAddress::Resolve
SOCKET Resolve(int family, int socktype, int flags, SocketList *sockets, LoopProc func)
Resolve this address into a socket.
Definition address.cpp:202
NetworkAddress::NetworkAddress
NetworkAddress(const sockaddr *address, int address_length)
Create a network address based on a resolved IP and port.
Definition address.h:61
NetworkAddress::SetPort
void SetPort(uint16_t port)
Set the port.
Definition address.cpp:58
NetworkAddress::NetworkAddress
NetworkAddress(struct sockaddr_storage &address, int address_length)
Create a network address based on a resolved IP and port.
Definition address.h:49
NetworkAddress::GetAddress
const sockaddr_storage * GetAddress()
Get the address in its internal representation.
Definition address.cpp:114
NetworkAddress::GetHostname
const std::string & GetHostname()
Get the hostname; in case it wasn't given the IPv4 dotted representation is given.
Definition address.cpp:24
NetworkAddress::address_length
int address_length
The length of the resolved address.
Definition address.h:31
NetworkAddress::GetAddressAsString
std::string GetAddressAsString(bool with_family=true)
Get the address as a string, e.g.
Definition address.cpp:95
NetworkAddress::IsInNetmask
bool IsInNetmask(std::string_view netmask)
Checks whether this IP address is contained by the given netmask.
Definition address.cpp:147
ServerAddress
Address to a game server.
Definition address.h:184
ServerAddress::connection_string
std::string connection_string
The connection string for this ServerAddress.
Definition address.h:198
ServerAddress::Parse
static ServerAddress Parse(std::string_view connection_string, uint16_t default_port, CompanyID *company_id=nullptr)
Convert a string containing either "hostname", "hostname:port" or invite code to a ServerAddress,...
Definition address.cpp:444
ServerAddress::type
ServerAddressType type
The type of this ServerAddress.
Definition address.h:197
ServerAddress::ServerAddress
ServerAddress(ServerAddressType type, std::string &&connection_string)
Create a new ServerAddress object.
Definition address.h:194
config.h
Configuration options of the network stuff.
os_abstraction.h
Network stuff has many things that needs to be included and/or implemented by default.
PoolID< uint8_t, struct CompanyIDTag, 0xF, 0xFF >