OpenTTD Source 20261112-master-gbcbec5e63f
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
9
10#ifndef NETWORK_CORE_ADDRESS_H
11#define NETWORK_CORE_ADDRESS_H
12
13#include "os_abstraction.h"
14#include "../../company_type.h"
15
16
17class NetworkAddress;
18typedef std::vector<NetworkAddress> NetworkAddressList;
19using SocketList = std::map<SOCKET, NetworkAddress>;
20
27private:
28 std::string hostname{};
30 sockaddr_storage address{};
31 bool resolved = false;
32
38 typedef SOCKET (*LoopProc)(addrinfo *runp);
39
40 SOCKET Resolve(int family, int socktype, int flags, SocketList *sockets, LoopProc func);
41public:
47 NetworkAddress(struct sockaddr_storage &address, int address_length) :
51 {
52 }
53
59 NetworkAddress(const sockaddr *address, int address_length) :
62 {
63 std::copy_n(reinterpret_cast<const std::byte *>(address), address_length, reinterpret_cast<std::byte *>(&this->address));
64 }
65
72 NetworkAddress(std::string_view hostname = "", uint16_t port = 0, int family = AF_UNSPEC) :
74 resolved(false)
75 {
76 if (!hostname.empty() && hostname.front() == '[' && hostname.back() == ']') {
77 hostname.remove_prefix(1);
78 hostname.remove_suffix(1);
79 }
80 this->hostname = hostname;
81 this->address.ss_family = family;
82 this->SetPort(port);
83 }
84
85 const std::string &GetHostname();
86 std::string GetAddressAsString(bool with_family = true);
87 const sockaddr_storage *GetAddress();
88
94 {
95 /* Resolve it if we didn't do it already */
96 if (!this->IsResolved()) this->GetAddress();
97 return this->address_length;
98 }
99
100 uint16_t GetPort() const;
101 void SetPort(uint16_t port);
102
107 bool IsResolved() const
108 {
109 return this->resolved;
110 }
111
112 bool IsFamily(int family);
113 bool IsInNetmask(std::string_view netmask);
114
121 {
122 int r = this->GetAddressLength() - address.GetAddressLength();
123 if (r == 0) r = this->address.ss_family - address.address.ss_family;
124 if (r == 0) r = memcmp(&this->address, &address.address, this->address_length);
125 if (r == 0) r = this->GetPort() - address.GetPort();
126 return r;
127 }
128
135 {
136 return this->CompareTo(address) == 0;
137 }
138
145 {
146 return const_cast<NetworkAddress*>(this)->CompareTo(address) == 0;
147 }
148
155 {
156 return this->CompareTo(address) <=> 0;
157 }
158
159 void Listen(int socktype, SocketList *sockets);
160
161 static std::string_view SocketTypeAsString(int socktype);
162 static std::string_view AddressFamilyAsString(int family);
163 static NetworkAddress GetPeerAddress(SOCKET sock);
164 static NetworkAddress GetSockAddress(SOCKET sock);
165 static const std::string GetPeerName(SOCKET sock);
166};
167
173enum class ServerAddressType : uint8_t {
176};
177
184private:
194
195public:
197 std::string connection_string;
198
199 static ServerAddress Parse(std::string_view connection_string, uint16_t default_port, CompanyID *company_id = nullptr);
200};
201
202#endif /* NETWORK_CORE_ADDRESS_H */
std::vector< NetworkAddress > NetworkAddressList
Type for a list of addresses.
Definition address.h:18
ServerAddressType
Types of server addresses we know.
Definition address.h:173
@ InviteCode
Server-address is based on an invite code.
Definition address.h:175
@ Direct
Server-address is based on an hostname:port.
Definition address.h:174
std::map< SOCKET, NetworkAddress > SocketList
Type for a mapping between address and socket.
Definition address.h:19
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
Definition address.h:26
static NetworkAddress GetPeerAddress(SOCKET sock)
Get the peer address of a socket as NetworkAddress.
Definition address.cpp:398
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:72
int CompareTo(NetworkAddress &address)
Compare the address of this class with the address of another.
Definition address.h:120
bool resolved
Whether the address has been (tried to be) resolved.
Definition address.h:31
int GetAddressLength()
Get the (valid) length of the address.
Definition address.h:93
auto operator<=>(NetworkAddress &address)
Compare the address of this class with the address of another.
Definition address.h:154
bool operator==(NetworkAddress &address)
Compare the address of this class with the address of another.
Definition address.h:134
sockaddr_storage address
The resolved address.
Definition address.h:30
static std::string_view AddressFamilyAsString(int family)
Convert the address family into a string.
Definition address.cpp:383
bool IsFamily(int family)
Checks of this address is of the given family.
Definition address.cpp:134
bool IsResolved() const
Check whether the IP address has been resolved already.
Definition address.h:107
static const std::string GetPeerName(SOCKET sock)
Get the peer name of a socket in string format.
Definition address.cpp:430
SOCKET(* LoopProc)(addrinfo *runp)
Helper function to resolve something to a socket.
Definition address.h:38
static std::string_view SocketTypeAsString(int socktype)
Convert the socket type into a string.
Definition address.cpp:368
static NetworkAddress GetSockAddress(SOCKET sock)
Get the local address of a socket as NetworkAddress.
Definition address.cpp:414
std::string hostname
The hostname.
Definition address.h:28
uint16_t GetPort() const
Get the port.
Definition address.cpp:40
void Listen(int socktype, SocketList *sockets)
Make the given socket listen.
Definition address.cpp:346
SOCKET Resolve(int family, int socktype, int flags, SocketList *sockets, LoopProc func)
Resolve this address into a socket.
Definition address.cpp:203
NetworkAddress(const sockaddr *address, int address_length)
Create a network address based on a resolved IP and port.
Definition address.h:59
void SetPort(uint16_t port)
Set the port.
Definition address.cpp:59
NetworkAddress(struct sockaddr_storage &address, int address_length)
Create a network address based on a resolved IP and port.
Definition address.h:47
const sockaddr_storage * GetAddress()
Get the address in its internal representation.
Definition address.cpp:115
const std::string & GetHostname()
Get the hostname; in case it wasn't given the IPv4 dotted representation is given.
Definition address.cpp:25
int address_length
The length of the resolved address.
Definition address.h:29
std::string GetAddressAsString(bool with_family=true)
Get the address as a string, e.g.
Definition address.cpp:96
bool IsInNetmask(std::string_view netmask)
Checks whether this IP address is contained by the given netmask.
Definition address.cpp:148
std::string connection_string
The connection string for this ServerAddress.
Definition address.h:197
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:445
ServerAddressType type
The type of this ServerAddress.
Definition address.h:196
ServerAddress(ServerAddressType type, std::string &&connection_string)
Create a new ServerAddress object.
Definition address.h:193
Types related to companies.
Includes and/or implementations for the network stuff.
SocketList TCPListenHandler< Tsocket, EnumPacketType, Tfull_packet, Tban_packet >::sockets
Instantiate the sockets.
Definition tcp_listen.h:180