OpenTTD Source 20250529-master-g10c159a79f
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 <http://www.gnu.org/licenses/>.
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;
20typedef std::vector<NetworkAddress> NetworkAddressList;
21using SocketList = std::map<SOCKET, NetworkAddress>;
22
29private:
30 std::string hostname{};
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) :
53 {
54 }
55
61 NetworkAddress(const sockaddr *address, int address_length) :
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) :
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
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
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
137 {
138 return this->CompareTo(address) == 0;
139 }
140
147 {
148 return const_cast<NetworkAddress*>(this)->CompareTo(address) == 0;
149 }
150
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
178
185private:
195
196public:
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 */
std::vector< NetworkAddress > NetworkAddressList
Type for a list of addresses.
Definition address.h:20
ServerAddressType
Types of server addresses we know.
Definition address.h:174
@ SERVER_ADDRESS_INVITE_CODE
Server-address is based on an invite code.
Definition address.h:176
@ SERVER_ADDRESS_DIRECT
Server-address is based on an hostname:port.
Definition address.h:175
std::map< SOCKET, NetworkAddress > SocketList
Type for a mapping between address and socket.
Definition address.h:21
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
Definition address.h:28
static NetworkAddress GetPeerAddress(SOCKET sock)
Get the peer address of a socket as NetworkAddress.
Definition address.cpp:397
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
int CompareTo(NetworkAddress &address)
Compare the address of this class with the address of another.
Definition address.h:122
bool resolved
Whether the address has been (tried to be) resolved.
Definition address.h:33
int GetAddressLength()
Get the (valid) length of the address.
Definition address.h:95
auto operator<=>(NetworkAddress &address)
Compare the address of this class with the address of another.
Definition address.h:155
bool operator==(NetworkAddress &address)
Compare the address of this class with the address of another.
Definition address.h:136
sockaddr_storage address
The resolved address.
Definition address.h:32
static std::string_view AddressFamilyAsString(int family)
Convert the address family into a string.
Definition address.cpp:382
bool IsFamily(int family)
Checks of this address is of the given family.
Definition address.cpp:133
bool IsResolved() const
Check whether the IP address has been resolved already.
Definition address.h:109
static const std::string GetPeerName(SOCKET sock)
Get the peer name of a socket in string format.
Definition address.cpp:429
SOCKET(* LoopProc)(addrinfo *runp)
Helper function to resolve something to a socket.
Definition address.h:40
static std::string_view SocketTypeAsString(int socktype)
Convert the socket type into a string.
Definition address.cpp:367
static NetworkAddress GetSockAddress(SOCKET sock)
Get the local address of a socket as NetworkAddress.
Definition address.cpp:413
std::string hostname
The hostname.
Definition address.h:30
uint16_t GetPort() const
Get the port.
Definition address.cpp:39
void Listen(int socktype, SocketList *sockets)
Make the given socket listen.
Definition address.cpp:345
SOCKET Resolve(int family, int socktype, int flags, SocketList *sockets, LoopProc func)
Resolve this address into a socket.
Definition address.cpp:202
NetworkAddress(const sockaddr *address, int address_length)
Create a network address based on a resolved IP and port.
Definition address.h:61
void SetPort(uint16_t port)
Set the port.
Definition address.cpp:58
NetworkAddress(struct sockaddr_storage &address, int address_length)
Create a network address based on a resolved IP and port.
Definition address.h:49
const sockaddr_storage * GetAddress()
Get the address in its internal representation.
Definition address.cpp:114
const std::string & GetHostname()
Get the hostname; in case it wasn't given the IPv4 dotted representation is given.
Definition address.cpp:24
int address_length
The length of the resolved address.
Definition address.h:31
std::string GetAddressAsString(bool with_family=true)
Get the address as a string, e.g.
Definition address.cpp:95
bool IsInNetmask(std::string_view netmask)
Checks whether this IP address is contained by the given netmask.
Definition address.cpp:147
Address to a game server.
Definition address.h:184
std::string connection_string
The connection string for this ServerAddress.
Definition address.h:198
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
ServerAddressType type
The type of this ServerAddress.
Definition address.h:197
ServerAddress(ServerAddressType type, std::string &&connection_string)
Create a new ServerAddress object.
Definition address.h:194
Configuration options of the network stuff.
Network stuff has many things that needs to be included and/or implemented by default.