OpenTTD Source  12.0-beta2
address.cpp
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 #include "../../stdafx.h"
11 
12 #include "address.h"
13 #include "../network_internal.h"
14 #include "../../debug.h"
15 
16 #include "../../safeguards.h"
17 
23 const std::string &NetworkAddress::GetHostname()
24 {
25  if (this->hostname.empty() && this->address.ss_family != AF_UNSPEC) {
26  assert(this->address_length != 0);
27  char buffer[NETWORK_HOSTNAME_LENGTH];
28  getnameinfo((struct sockaddr *)&this->address, this->address_length, buffer, sizeof(buffer), nullptr, 0, NI_NUMERICHOST);
29  this->hostname = buffer;
30  }
31  return this->hostname;
32 }
33 
39 {
40  switch (this->address.ss_family) {
41  case AF_UNSPEC:
42  case AF_INET:
43  return ntohs(((const struct sockaddr_in *)&this->address)->sin_port);
44 
45  case AF_INET6:
46  return ntohs(((const struct sockaddr_in6 *)&this->address)->sin6_port);
47 
48  default:
49  NOT_REACHED();
50  }
51 }
52 
57 void NetworkAddress::SetPort(uint16 port)
58 {
59  switch (this->address.ss_family) {
60  case AF_UNSPEC:
61  case AF_INET:
62  ((struct sockaddr_in*)&this->address)->sin_port = htons(port);
63  break;
64 
65  case AF_INET6:
66  ((struct sockaddr_in6*)&this->address)->sin6_port = htons(port);
67  break;
68 
69  default:
70  NOT_REACHED();
71  }
72 }
73 
80 static const char *GetAddressFormatString(uint16 family, bool with_family)
81 {
82  switch (family) {
83  case AF_INET: return with_family ? "{}:{} (IPv4)" : "{}:{}";
84  case AF_INET6: return with_family ? "[{}]:{} (IPv6)" : "[{}]:{}";
85  default: return with_family ? "{}:{} (IPv?)" : "{}:{}";
86  }
87 }
88 
94 std::string NetworkAddress::GetAddressAsString(bool with_family)
95 {
96  return fmt::format(GetAddressFormatString(this->GetAddress()->ss_family, with_family), this->GetHostname(), this->GetPort());
97 }
98 
104 static SOCKET ResolveLoopProc(addrinfo *runp)
105 {
106  /* We just want the first 'entry', so return a valid socket. */
107  return !INVALID_SOCKET;
108 }
109 
114 const sockaddr_storage *NetworkAddress::GetAddress()
115 {
116  if (!this->IsResolved()) {
117  /* Here we try to resolve a network address. We use SOCK_STREAM as
118  * socket type because some stupid OSes, like Solaris, cannot be
119  * bothered to implement the specifications and allow '0' as value
120  * that means "don't care whether it is SOCK_STREAM or SOCK_DGRAM".
121  */
122  this->Resolve(this->address.ss_family, SOCK_STREAM, AI_ADDRCONFIG, nullptr, ResolveLoopProc);
123  this->resolved = true;
124  }
125  return &this->address;
126 }
127 
133 bool NetworkAddress::IsFamily(int family)
134 {
135  if (!this->IsResolved()) {
136  this->Resolve(family, SOCK_STREAM, AI_ADDRCONFIG, nullptr, ResolveLoopProc);
137  }
138  return this->address.ss_family == family;
139 }
140 
147 bool NetworkAddress::IsInNetmask(const std::string &netmask)
148 {
149  /* Resolve it if we didn't do it already */
150  if (!this->IsResolved()) this->GetAddress();
151 
152  int cidr = this->address.ss_family == AF_INET ? 32 : 128;
153 
154  NetworkAddress mask_address;
155 
156  /* Check for CIDR separator */
157  auto cidr_separator_location = netmask.find('/');
158  if (cidr_separator_location != std::string::npos) {
159  int tmp_cidr = atoi(netmask.substr(cidr_separator_location + 1).c_str());
160 
161  /* Invalid CIDR, treat as single host */
162  if (tmp_cidr > 0 && tmp_cidr < cidr) cidr = tmp_cidr;
163 
164  /* Remove the / so that NetworkAddress works on the IP portion */
165  mask_address = NetworkAddress(netmask.substr(0, cidr_separator_location), 0, this->address.ss_family);
166  } else {
167  mask_address = NetworkAddress(netmask, 0, this->address.ss_family);
168  }
169 
170  if (mask_address.GetAddressLength() == 0) return false;
171 
172  uint32 *ip;
173  uint32 *mask;
174  switch (this->address.ss_family) {
175  case AF_INET:
176  ip = (uint32*)&((struct sockaddr_in*)&this->address)->sin_addr.s_addr;
177  mask = (uint32*)&((struct sockaddr_in*)&mask_address.address)->sin_addr.s_addr;
178  break;
179 
180  case AF_INET6:
181  ip = (uint32*)&((struct sockaddr_in6*)&this->address)->sin6_addr;
182  mask = (uint32*)&((struct sockaddr_in6*)&mask_address.address)->sin6_addr;
183  break;
184 
185  default:
186  NOT_REACHED();
187  }
188 
189  while (cidr > 0) {
190  uint32 msk = cidr >= 32 ? (uint32)-1 : htonl(-(1 << (32 - cidr)));
191  if ((*mask++ & msk) != (*ip++ & msk)) return false;
192 
193  cidr -= 32;
194  }
195 
196  return true;
197 }
198 
208 SOCKET NetworkAddress::Resolve(int family, int socktype, int flags, SocketList *sockets, LoopProc func)
209 {
210  struct addrinfo *ai;
211  struct addrinfo hints;
212  memset(&hints, 0, sizeof (hints));
213  hints.ai_family = family;
214  hints.ai_flags = flags;
215  hints.ai_socktype = socktype;
216 
217  /* The port needs to be a string. Six is enough to contain all characters + '\0'. */
218  char port_name[6];
219  seprintf(port_name, lastof(port_name), "%u", this->GetPort());
220 
221  bool reset_hostname = false;
222  /* Setting both hostname to nullptr and port to 0 is not allowed.
223  * As port 0 means bind to any port, the other must mean that
224  * we want to bind to 'all' IPs. */
225  if (this->hostname.empty() && this->address_length == 0 && this->GetPort() == 0) {
226  reset_hostname = true;
227  int fam = this->address.ss_family;
228  if (fam == AF_UNSPEC) fam = family;
229  this->hostname = fam == AF_INET ? "0.0.0.0" : "::";
230  }
231 
232  static bool _resolve_timeout_error_message_shown = false;
233  auto start = std::chrono::steady_clock::now();
234  int e = getaddrinfo(this->hostname.empty() ? nullptr : this->hostname.c_str(), port_name, &hints, &ai);
235  auto end = std::chrono::steady_clock::now();
236  std::chrono::seconds duration = std::chrono::duration_cast<std::chrono::seconds>(end - start);
237  if (!_resolve_timeout_error_message_shown && duration >= std::chrono::seconds(5)) {
238  Debug(net, 0, "getaddrinfo for hostname \"{}\", port {}, address family {} and socket type {} took {} seconds",
239  this->hostname, port_name, AddressFamilyAsString(family), SocketTypeAsString(socktype), duration.count());
240  Debug(net, 0, " this is likely an issue in the DNS name resolver's configuration causing it to time out");
241  _resolve_timeout_error_message_shown = true;
242  }
243 
244 
245  if (reset_hostname) this->hostname.clear();
246 
247  if (e != 0) {
248  if (func != ResolveLoopProc) {
249  Debug(net, 0, "getaddrinfo for hostname \"{}\", port {}, address family {} and socket type {} failed: {}",
250  this->hostname, port_name, AddressFamilyAsString(family), SocketTypeAsString(socktype), FS2OTTD(gai_strerror(e)));
251  }
252  return INVALID_SOCKET;
253  }
254 
255  SOCKET sock = INVALID_SOCKET;
256  for (struct addrinfo *runp = ai; runp != nullptr; runp = runp->ai_next) {
257  /* When we are binding to multiple sockets, make sure we do not
258  * connect to one with exactly the same address twice. That's
259  * of course totally unneeded ;) */
260  if (sockets != nullptr) {
261  NetworkAddress address(runp->ai_addr, (int)runp->ai_addrlen);
262  if (sockets->Contains(address)) continue;
263  }
264  sock = func(runp);
265  if (sock == INVALID_SOCKET) continue;
266 
267  if (sockets == nullptr) {
268  this->address_length = (int)runp->ai_addrlen;
269  assert(sizeof(this->address) >= runp->ai_addrlen);
270  memcpy(&this->address, runp->ai_addr, runp->ai_addrlen);
271 #ifdef __EMSCRIPTEN__
272  /* Emscripten doesn't zero sin_zero, but as we compare addresses
273  * to see if they are the same address, we need them to be zero'd.
274  * Emscripten is, as far as we know, the only OS not doing this.
275  *
276  * https://github.com/emscripten-core/emscripten/issues/12998
277  */
278  if (this->address.ss_family == AF_INET) {
279  sockaddr_in *address_ipv4 = (sockaddr_in *)&this->address;
280  memset(address_ipv4->sin_zero, 0, sizeof(address_ipv4->sin_zero));
281  }
282 #endif
283  break;
284  }
285 
286  NetworkAddress addr(runp->ai_addr, (int)runp->ai_addrlen);
287  (*sockets)[addr] = sock;
288  sock = INVALID_SOCKET;
289  }
290  freeaddrinfo (ai);
291 
292  return sock;
293 }
294 
300 static SOCKET ListenLoopProc(addrinfo *runp)
301 {
302  std::string address = NetworkAddress(runp->ai_addr, (int)runp->ai_addrlen).GetAddressAsString();
303 
304  SOCKET sock = socket(runp->ai_family, runp->ai_socktype, runp->ai_protocol);
305  if (sock == INVALID_SOCKET) {
306  const char *type = NetworkAddress::SocketTypeAsString(runp->ai_socktype);
307  const char *family = NetworkAddress::AddressFamilyAsString(runp->ai_family);
308  Debug(net, 0, "Could not create {} {} socket: {}", type, family, NetworkError::GetLast().AsString());
309  return INVALID_SOCKET;
310  }
311 
312  if (runp->ai_socktype == SOCK_STREAM && !SetNoDelay(sock)) {
313  Debug(net, 1, "Setting no-delay mode failed: {}", NetworkError::GetLast().AsString());
314  }
315 
316  if (!SetReusePort(sock)) {
317  Debug(net, 0, "Setting reuse-address mode failed: {}", NetworkError::GetLast().AsString());
318  }
319 
320 #ifndef __OS2__
321  int on = 1;
322  if (runp->ai_family == AF_INET6 &&
323  setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&on, sizeof(on)) == -1) {
324  Debug(net, 3, "Could not disable IPv4 over IPv6: {}", NetworkError::GetLast().AsString());
325  }
326 #endif
327 
328  if (bind(sock, runp->ai_addr, (int)runp->ai_addrlen) != 0) {
329  Debug(net, 0, "Could not bind socket on {}: {}", address, NetworkError::GetLast().AsString());
330  closesocket(sock);
331  return INVALID_SOCKET;
332  }
333 
334  if (runp->ai_socktype != SOCK_DGRAM && listen(sock, 1) != 0) {
335  Debug(net, 0, "Could not listen on socket: {}", NetworkError::GetLast().AsString());
336  closesocket(sock);
337  return INVALID_SOCKET;
338  }
339 
340  /* Connection succeeded */
341 
342  if (!SetNonBlocking(sock)) {
343  Debug(net, 0, "Setting non-blocking mode failed: {}", NetworkError::GetLast().AsString());
344  }
345 
346  Debug(net, 3, "Listening on {}", address);
347  return sock;
348 }
349 
355 void NetworkAddress::Listen(int socktype, SocketList *sockets)
356 {
357  assert(sockets != nullptr);
358 
359  /* Setting both hostname to "" and port to 0 is not allowed.
360  * As port 0 means bind to any port, the other must mean that
361  * we want to bind to 'all' IPs. */
362  if (this->address_length == 0 && this->address.ss_family == AF_UNSPEC &&
363  this->hostname.empty() && this->GetPort() == 0) {
364  this->Resolve(AF_INET, socktype, AI_ADDRCONFIG | AI_PASSIVE, sockets, ListenLoopProc);
365  this->Resolve(AF_INET6, socktype, AI_ADDRCONFIG | AI_PASSIVE, sockets, ListenLoopProc);
366  } else {
367  this->Resolve(AF_UNSPEC, socktype, AI_ADDRCONFIG | AI_PASSIVE, sockets, ListenLoopProc);
368  }
369 }
370 
377 /* static */ const char *NetworkAddress::SocketTypeAsString(int socktype)
378 {
379  switch (socktype) {
380  case SOCK_STREAM: return "tcp";
381  case SOCK_DGRAM: return "udp";
382  default: return "unsupported";
383  }
384 }
385 
392 /* static */ const char *NetworkAddress::AddressFamilyAsString(int family)
393 {
394  switch (family) {
395  case AF_UNSPEC: return "either IPv4 or IPv6";
396  case AF_INET: return "IPv4";
397  case AF_INET6: return "IPv6";
398  default: return "unsupported";
399  }
400 }
401 
408 {
409  sockaddr_storage addr = {};
410  socklen_t addr_len = sizeof(addr);
411  if (getpeername(sock, (sockaddr *)&addr, &addr_len) != 0) {
412  Debug(net, 0, "Failed to get address of the peer: {}", NetworkError::GetLast().AsString());
413  return NetworkAddress();
414  }
415  return NetworkAddress(addr, addr_len);
416 }
417 
424 {
425  sockaddr_storage addr = {};
426  socklen_t addr_len = sizeof(addr);
427  if (getsockname(sock, (sockaddr *)&addr, &addr_len) != 0) {
428  Debug(net, 0, "Failed to get address of the socket: {}", NetworkError::GetLast().AsString());
429  return NetworkAddress();
430  }
431  return NetworkAddress(addr, addr_len);
432 }
433 
439 /* static */ const std::string NetworkAddress::GetPeerName(SOCKET sock)
440 {
442 }
443 
454 /* static */ ServerAddress ServerAddress::Parse(const std::string &connection_string, uint16 default_port, CompanyID *company_id)
455 {
456  if (StrStartsWith(connection_string, "+")) {
457  std::string_view invite_code = ParseCompanyFromConnectionString(connection_string, company_id);
458  return ServerAddress(SERVER_ADDRESS_INVITE_CODE, std::string(invite_code));
459  }
460 
461  uint16 port = default_port;
462  std::string_view ip = ParseFullConnectionString(connection_string, port, company_id);
463  return ServerAddress(SERVER_ADDRESS_DIRECT, std::string(ip) + ":" + std::to_string(port));
464 }
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
SetReusePort
bool SetReusePort(SOCKET d)
Try to set the socket to reuse ports.
Definition: os_abstraction.cpp:167
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
ResolveLoopProc
static SOCKET ResolveLoopProc(addrinfo *runp)
Helper function to resolve without opening a socket.
Definition: address.cpp:104
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 >
NetworkAddress::GetPeerName
static const std::string GetPeerName(SOCKET sock)
Get the peer name of a socket in string format.
Definition: address.cpp:439
address.h
FS2OTTD
std::string FS2OTTD(const std::wstring &name)
Convert to OpenTTD's encoding from a wide string.
Definition: win32.cpp:542
NetworkError::GetLast
static NetworkError GetLast()
Get the last network error.
Definition: os_abstraction.cpp:116
SERVER_ADDRESS_DIRECT
@ SERVER_ADDRESS_DIRECT
Server-address is based on an hostname:port.
Definition: address.h:189
ListenLoopProc
static SOCKET ListenLoopProc(addrinfo *runp)
Helper function to resolve a listening.
Definition: address.cpp:300
StrStartsWith
bool StrStartsWith(const std::string_view str, const std::string_view prefix)
Check whether the given string starts with the given prefix.
Definition: string.cpp:367
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
ParseFullConnectionString
std::string_view ParseFullConnectionString(const std::string &connection_string, uint16 &port, CompanyID *company_id)
Converts a string to ip/port/company Format: IP:port::company.
Definition: network.cpp:503
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
ParseCompanyFromConnectionString
std::string_view ParseCompanyFromConnectionString(const std::string &connection_string, CompanyID *company_id)
Parse the company part ("#company" postfix) of a connecting string.
Definition: network.cpp:459
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::GetHostname
const std::string & GetHostname()
Get the hostname; in case it wasn't given the IPv4 dotted representation is given.
Definition: address.cpp:23
seprintf
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:535
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
Debug
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
SetNonBlocking
bool SetNonBlocking(SOCKET d)
Try to set the socket into non-blocking mode.
Definition: os_abstraction.cpp:133
NetworkAddress::IsFamily
bool IsFamily(int family)
Checks of this address is of the given family.
Definition: address.cpp:133
NETWORK_HOSTNAME_LENGTH
static const uint NETWORK_HOSTNAME_LENGTH
The maximum length of the host name, in bytes including '\0'.
Definition: config.h:57
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
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:394
SetNoDelay
bool SetNoDelay(SOCKET d)
Try to set the socket to not delay sending.
Definition: os_abstraction.cpp:151
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
GetAddressFormatString
static const char * GetAddressFormatString(uint16 family, bool with_family)
Helper to get the formatting string of an address for a given family.
Definition: address.cpp:80
NetworkAddress::address
sockaddr_storage address
The resolved address.
Definition: address.h:34
SmallMap::Contains
bool Contains(const T &key) const
Tests whether a key is assigned in this map.
Definition: smallmap_type.hpp:79