OpenTTD Source  12.0-beta2
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 #include "../../core/smallmap_type.hpp"
18 
19 #include <string>
20 
22 typedef std::vector<NetworkAddress> NetworkAddressList;
24 
31 private:
32  std::string hostname;
34  sockaddr_storage address;
35  bool resolved;
36 
42  typedef SOCKET (*LoopProc)(addrinfo *runp);
43 
44  SOCKET Resolve(int family, int socktype, int flags, SocketList *sockets, LoopProc func);
45 public:
51  NetworkAddress(struct sockaddr_storage &address, int address_length) :
55  {
56  }
57 
66  {
67  memset(&this->address, 0, sizeof(this->address));
68  memcpy(&this->address, address, address_length);
69  }
70 
77  NetworkAddress(std::string_view hostname = "", uint16 port = 0, int family = AF_UNSPEC) :
78  address_length(0),
79  resolved(false)
80  {
81  if (!hostname.empty() && hostname.front() == '[' && hostname.back() == ']') {
82  hostname.remove_prefix(1);
83  hostname.remove_suffix(1);
84  }
85  this->hostname = hostname;
86 
87  memset(&this->address, 0, sizeof(this->address));
88  this->address.ss_family = family;
89  this->SetPort(port);
90  }
91 
92  const std::string &GetHostname();
93  std::string GetAddressAsString(bool with_family = true);
94  const sockaddr_storage *GetAddress();
95 
101  {
102  /* Resolve it if we didn't do it already */
103  if (!this->IsResolved()) this->GetAddress();
104  return this->address_length;
105  }
106 
107  uint16 GetPort() const;
108  void SetPort(uint16 port);
109 
114  bool IsResolved() const
115  {
116  return this->resolved;
117  }
118 
119  bool IsFamily(int family);
120  bool IsInNetmask(const std::string &netmask);
121 
128  {
129  int r = this->GetAddressLength() - address.GetAddressLength();
130  if (r == 0) r = this->address.ss_family - address.address.ss_family;
131  if (r == 0) r = memcmp(&this->address, &address.address, this->address_length);
132  if (r == 0) r = this->GetPort() - address.GetPort();
133  return r;
134  }
135 
142  {
143  return this->CompareTo(address) == 0;
144  }
145 
152  {
153  return const_cast<NetworkAddress*>(this)->CompareTo(address) == 0;
154  }
161  {
162  return const_cast<NetworkAddress*>(this)->CompareTo(address) != 0;
163  }
164 
170  {
171  return this->CompareTo(address) < 0;
172  }
173 
174  void Listen(int socktype, SocketList *sockets);
175 
176  static const char *SocketTypeAsString(int socktype);
177  static const char *AddressFamilyAsString(int family);
178  static NetworkAddress GetPeerAddress(SOCKET sock);
179  static NetworkAddress GetSockAddress(SOCKET sock);
180  static const std::string GetPeerName(SOCKET sock);
181 };
182 
191 };
192 
199 private:
209 
210 public:
212  std::string connection_string;
213 
214  static ServerAddress Parse(const std::string &connection_string, uint16 default_port, CompanyID *company_id = nullptr);
215 };
216 
217 #endif /* NETWORK_CORE_ADDRESS_H */
NetworkAddress::NetworkAddress
NetworkAddress(std::string_view hostname="", uint16 port=0, int family=AF_UNSPEC)
Create a network address based on a unresolved host and port.
Definition: address.h:77
SocketList
SmallMap< NetworkAddress, SOCKET > SocketList
Type for a mapping between address and socket.
Definition: address.h:23
NetworkAddress::hostname
std::string hostname
The hostname.
Definition: address.h:32
NetworkAddress::GetAddress
const sockaddr_storage * GetAddress()
Get the address in its internal representation.
Definition: address.cpp:114
NetworkAddress::GetAddressLength
int GetAddressLength()
Get the (valid) length of the address.
Definition: address.h:100
NetworkAddress::SetPort
void SetPort(uint16 port)
Set the port.
Definition: address.cpp:57
NetworkAddress::Resolve
SOCKET Resolve(int family, int socktype, int flags, SocketList *sockets, LoopProc func)
Resolve this address into a socket.
Definition: address.cpp:208
NetworkAddress::resolved
bool resolved
Whether the address has been (tried to be) resolved.
Definition: address.h:35
NetworkAddress::IsInNetmask
bool IsInNetmask(const std::string &netmask)
Checks whether this IP address is contained by the given netmask.
Definition: address.cpp:147
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
ServerAddress::Parse
static ServerAddress Parse(const std::string &connection_string, uint16 default_port, CompanyID *company_id=nullptr)
Convert a string containing either "hostname", "hostname:port" or invite code to a ServerAddress,...
Definition: address.cpp:454
NetworkAddress::GetPeerAddress
static NetworkAddress GetPeerAddress(SOCKET sock)
Get the peer address of a socket as NetworkAddress.
Definition: address.cpp:407
NetworkAddress::GetSockAddress
static NetworkAddress GetSockAddress(SOCKET sock)
Get the local address of a socket as NetworkAddress.
Definition: address.cpp:423
SmallMap< NetworkAddress, SOCKET >
ServerAddress::type
ServerAddressType type
The type of this ServerAddress.
Definition: address.h:211
NetworkAddress::GetPeerName
static const std::string GetPeerName(SOCKET sock)
Get the peer name of a socket in string format.
Definition: address.cpp:439
NetworkAddress::operator!=
bool operator!=(NetworkAddress address) const
Compare the address of this class with the address of another.
Definition: address.h:160
NetworkAddressList
std::vector< NetworkAddress > NetworkAddressList
Type for a list of addresses.
Definition: address.h:21
SERVER_ADDRESS_DIRECT
@ SERVER_ADDRESS_DIRECT
Server-address is based on an hostname:port.
Definition: address.h:189
NetworkAddress::GetPort
uint16 GetPort() const
Get the port.
Definition: address.cpp:38
NetworkAddress::address_length
int address_length
The length of the resolved address.
Definition: address.h:33
ServerAddressType
ServerAddressType
Types of server addresses we know.
Definition: address.h:188
NetworkAddress::NetworkAddress
NetworkAddress(struct sockaddr_storage &address, int address_length)
Create a network address based on a resolved IP and port.
Definition: address.h:51
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:141
NetworkAddress
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
Definition: address.h:30
NetworkAddress::IsResolved
bool IsResolved() const
Check whether the IP address has been resolved already.
Definition: address.h:114
NetworkAddress::CompareTo
int CompareTo(NetworkAddress &address)
Compare the address of this class with the address of another.
Definition: address.h:127
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:42
NetworkAddress::NetworkAddress
NetworkAddress(sockaddr *address, int address_length)
Create a network address based on a resolved IP and port.
Definition: address.h:63
NetworkAddress::SocketTypeAsString
static const char * SocketTypeAsString(int socktype)
Convert the socket type into a string.
Definition: address.cpp:377
SERVER_ADDRESS_INVITE_CODE
@ SERVER_ADDRESS_INVITE_CODE
Server-address is based on an invite code.
Definition: address.h:190
os_abstraction.h
NetworkAddress::IsFamily
bool IsFamily(int family)
Checks of this address is of the given family.
Definition: address.cpp:133
config.h
NetworkAddress::AddressFamilyAsString
static const char * AddressFamilyAsString(int family)
Convert the address family into a string.
Definition: address.cpp:392
ServerAddress::connection_string
std::string connection_string
The connection string for this ServerAddress.
Definition: address.h:212
NetworkAddress::operator<
bool operator<(NetworkAddress &address)
Compare the address of this class with the address of another.
Definition: address.h:169
NetworkAddress::Listen
void Listen(int socktype, SocketList *sockets)
Make the given socket listen.
Definition: address.cpp:355
ServerAddress
Address to a game server.
Definition: address.h:198
ServerAddress::ServerAddress
ServerAddress(ServerAddressType type, const std::string &connection_string)
Create a new ServerAddress object.
Definition: address.h:208
NetworkAddress::address
sockaddr_storage address
The resolved address.
Definition: address.h:34