OpenTTD Source 20241224-master-gf74b0cf984
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;
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
64 {
65 memset(&this->address, 0, sizeof(this->address));
66 memcpy(&this->address, address, address_length);
67 }
68
75 NetworkAddress(std::string_view hostname = "", uint16_t port = 0, int family = AF_UNSPEC) :
77 resolved(false)
78 {
79 if (!hostname.empty() && hostname.front() == '[' && hostname.back() == ']') {
80 hostname.remove_prefix(1);
81 hostname.remove_suffix(1);
82 }
83 this->hostname = hostname;
84
85 memset(&this->address, 0, sizeof(this->address));
86 this->address.ss_family = family;
87 this->SetPort(port);
88 }
89
90 const std::string &GetHostname();
91 std::string GetAddressAsString(bool with_family = true);
92 const sockaddr_storage *GetAddress();
93
99 {
100 /* Resolve it if we didn't do it already */
101 if (!this->IsResolved()) this->GetAddress();
102 return this->address_length;
103 }
104
105 uint16_t GetPort() const;
106 void SetPort(uint16_t port);
107
112 bool IsResolved() const
113 {
114 return this->resolved;
115 }
116
117 bool IsFamily(int family);
118 bool IsInNetmask(const std::string &netmask);
119
126 {
127 int r = this->GetAddressLength() - address.GetAddressLength();
128 if (r == 0) r = this->address.ss_family - address.address.ss_family;
129 if (r == 0) r = memcmp(&this->address, &address.address, this->address_length);
130 if (r == 0) r = this->GetPort() - address.GetPort();
131 return r;
132 }
133
140 {
141 return this->CompareTo(address) == 0;
142 }
143
150 {
151 return const_cast<NetworkAddress*>(this)->CompareTo(address) == 0;
152 }
159 {
160 return const_cast<NetworkAddress*>(this)->CompareTo(address) != 0;
161 }
162
168 {
169 return this->CompareTo(address) < 0;
170 }
171
172 void Listen(int socktype, SocketList *sockets);
173
174 static const char *SocketTypeAsString(int socktype);
175 static const char *AddressFamilyAsString(int family);
176 static NetworkAddress GetPeerAddress(SOCKET sock);
177 static NetworkAddress GetSockAddress(SOCKET sock);
178 static const std::string GetPeerName(SOCKET sock);
179};
180
190
197private:
207
208public:
210 std::string connection_string;
211
212 static ServerAddress Parse(const std::string &connection_string, uint16_t default_port, CompanyID *company_id = nullptr);
213};
214
215#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:186
@ SERVER_ADDRESS_INVITE_CODE
Server-address is based on an invite code.
Definition address.h:188
@ SERVER_ADDRESS_DIRECT
Server-address is based on an hostname:port.
Definition address.h:187
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
bool operator!=(NetworkAddress address) const
Compare the address of this class with the address of another.
Definition address.h:158
static NetworkAddress GetPeerAddress(SOCKET sock)
Get the peer address of a socket as NetworkAddress.
Definition address.cpp:403
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:75
static const char * AddressFamilyAsString(int family)
Convert the address family into a string.
Definition address.cpp:388
int CompareTo(NetworkAddress &address)
Compare the address of this class with the address of another.
Definition address.h:125
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:98
bool operator<(NetworkAddress &address)
Compare the address of this class with the address of another.
Definition address.h:167
NetworkAddress(sockaddr *address, int address_length)
Create a network address based on a resolved IP and port.
Definition address.h:61
bool operator==(NetworkAddress &address)
Compare the address of this class with the address of another.
Definition address.h:139
static const char * SocketTypeAsString(int socktype)
Convert the socket type into a string.
Definition address.cpp:373
sockaddr_storage address
The resolved address.
Definition address.h:32
bool IsFamily(int family)
Checks of this address is of the given family.
Definition address.cpp:132
bool IsResolved() const
Check whether the IP address has been resolved already.
Definition address.h:112
static const std::string GetPeerName(SOCKET sock)
Get the peer name of a socket in string format.
Definition address.cpp:435
SOCKET(* LoopProc)(addrinfo *runp)
Helper function to resolve something to a socket.
Definition address.h:40
static NetworkAddress GetSockAddress(SOCKET sock)
Get the local address of a socket as NetworkAddress.
Definition address.cpp:419
bool IsInNetmask(const std::string &netmask)
Checks whether this IP address is contained by the given netmask.
Definition address.cpp:146
std::string hostname
The hostname.
Definition address.h:30
uint16_t GetPort() const
Get the port.
Definition address.cpp:38
void Listen(int socktype, SocketList *sockets)
Make the given socket listen.
Definition address.cpp:351
SOCKET Resolve(int family, int socktype, int flags, SocketList *sockets, LoopProc func)
Resolve this address into a socket.
Definition address.cpp:207
void SetPort(uint16_t port)
Set the port.
Definition address.cpp:57
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:113
const std::string & GetHostname()
Get the hostname; in case it wasn't given the IPv4 dotted representation is given.
Definition address.cpp:23
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:94
Address to a game server.
Definition address.h:196
std::string connection_string
The connection string for this ServerAddress.
Definition address.h:210
ServerAddress(ServerAddressType type, const std::string &connection_string)
Create a new ServerAddress object.
Definition address.h:206
ServerAddressType type
The type of this ServerAddress.
Definition address.h:209
static ServerAddress Parse(const std::string &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:450
Owner
Enum for all companies/owners.
Configuration options of the network stuff.
Network stuff has many things that needs to be included and/or implemented by default.