OpenTTD Source  20240919-master-gdf0233f4c2
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 
20 typedef std::vector<NetworkAddress> NetworkAddressList;
21 using SocketList = std::map<SOCKET, NetworkAddress>;
22 
29 private:
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);
43 public:
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) :
76  address_length(0),
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 
189 };
190 
197 private:
207 
208 public:
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 */
NetworkAddress::hostname
std::string hostname
The hostname.
Definition: address.h:30
NetworkAddress::GetAddress
const sockaddr_storage * GetAddress()
Get the address in its internal representation.
Definition: address.cpp:113
NetworkAddress::GetAddressLength
int GetAddressLength()
Get the (valid) length of the address.
Definition: address.h:98
NetworkAddress::Resolve
SOCKET Resolve(int family, int socktype, int flags, SocketList *sockets, LoopProc func)
Resolve this address into a socket.
Definition: address.cpp:207
NetworkAddress::resolved
bool resolved
Whether the address has been (tried to be) resolved.
Definition: address.h:33
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
NetworkAddress::GetPort
uint16_t GetPort() const
Get the port.
Definition: address.cpp:38
ServerAddress::Parse
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
NetworkAddress::IsInNetmask
bool IsInNetmask(const std::string &netmask)
Checks whether this IP address is contained by the given netmask.
Definition: address.cpp:146
SocketList
std::map< SOCKET, NetworkAddress > SocketList
Type for a mapping between address and socket.
Definition: address.h:21
NetworkAddress::GetPeerAddress
static NetworkAddress GetPeerAddress(SOCKET sock)
Get the peer address of a socket as NetworkAddress.
Definition: address.cpp:403
NetworkAddress::GetSockAddress
static NetworkAddress GetSockAddress(SOCKET sock)
Get the local address of a socket as NetworkAddress.
Definition: address.cpp:419
ServerAddress::type
ServerAddressType type
The type of this ServerAddress.
Definition: address.h:209
NetworkAddress::GetPeerName
static const std::string GetPeerName(SOCKET sock)
Get the peer name of a socket in string format.
Definition: address.cpp:435
NetworkAddress::operator!=
bool operator!=(NetworkAddress address) const
Compare the address of this class with the address of another.
Definition: address.h:158
NetworkAddressList
std::vector< NetworkAddress > NetworkAddressList
Type for a list of addresses.
Definition: address.h:19
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:75
SERVER_ADDRESS_DIRECT
@ SERVER_ADDRESS_DIRECT
Server-address is based on an hostname:port.
Definition: address.h:187
NetworkAddress::address_length
int address_length
The length of the resolved address.
Definition: address.h:31
ServerAddressType
ServerAddressType
Types of server addresses we know.
Definition: address.h:186
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::GetAddressAsString
std::string GetAddressAsString(bool with_family=true)
Get the address as a string, e.g.
Definition: address.cpp:94
NetworkAddress::operator==
bool operator==(NetworkAddress &address)
Compare the address of this class with the address of another.
Definition: address.h:139
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::IsResolved
bool IsResolved() const
Check whether the IP address has been resolved already.
Definition: address.h:112
NetworkAddress::CompareTo
int CompareTo(NetworkAddress &address)
Compare the address of this class with the address of another.
Definition: address.h:125
NetworkAddress::GetHostname
const std::string & GetHostname()
Get the hostname; in case it wasn't given the IPv4 dotted representation is given.
Definition: address.cpp:23
NetworkAddress::LoopProc
SOCKET(* LoopProc)(addrinfo *runp)
Helper function to resolve something to a socket.
Definition: address.h:40
NetworkAddress::NetworkAddress
NetworkAddress(sockaddr *address, int address_length)
Create a network address based on a resolved IP and port.
Definition: address.h:61
NetworkAddress::SocketTypeAsString
static const char * SocketTypeAsString(int socktype)
Convert the socket type into a string.
Definition: address.cpp:373
SERVER_ADDRESS_INVITE_CODE
@ SERVER_ADDRESS_INVITE_CODE
Server-address is based on an invite code.
Definition: address.h:188
os_abstraction.h
NetworkAddress::IsFamily
bool IsFamily(int family)
Checks of this address is of the given family.
Definition: address.cpp:132
config.h
NetworkAddress::AddressFamilyAsString
static const char * AddressFamilyAsString(int family)
Convert the address family into a string.
Definition: address.cpp:388
ServerAddress::connection_string
std::string connection_string
The connection string for this ServerAddress.
Definition: address.h:210
NetworkAddress::operator<
bool operator<(NetworkAddress &address)
Compare the address of this class with the address of another.
Definition: address.h:167
NetworkAddress::Listen
void Listen(int socktype, SocketList *sockets)
Make the given socket listen.
Definition: address.cpp:351
ServerAddress
Address to a game server.
Definition: address.h:196
ServerAddress::ServerAddress
ServerAddress(ServerAddressType type, const std::string &connection_string)
Create a new ServerAddress object.
Definition: address.h:206
NetworkAddress::SetPort
void SetPort(uint16_t port)
Set the port.
Definition: address.cpp:57
NetworkAddress::address
sockaddr_storage address
The resolved address.
Definition: address.h:32