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/>.
8 /** @file core/address.cpp Implementation of the address. */
10 #include "../../stdafx.h"
13 #include "../network_internal.h"
14 #include "../../debug.h"
16 #include "../../safeguards.h"
19 * Get the hostname; in case it wasn't given the
20 * IPv4 dotted representation is given.
21 * @return the hostname
23 const std::string
&NetworkAddress::GetHostname()
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
;
31 return this->hostname
;
38 uint16_t NetworkAddress::GetPort() const
40 switch (this->address
.ss_family
) {
43 return ntohs(((const struct sockaddr_in
*)&this->address
)->sin_port
);
46 return ntohs(((const struct sockaddr_in6
*)&this->address
)->sin6_port
);
55 * @param port set the port number.
57 void NetworkAddress::SetPort(uint16_t port
)
59 switch (this->address
.ss_family
) {
62 ((struct sockaddr_in
*)&this->address
)->sin_port
= htons(port
);
66 ((struct sockaddr_in6
*)&this->address
)->sin6_port
= htons(port
);
75 * Helper to get the formatting string of an address for a given family.
76 * @param family The family to get the address format for.
77 * @param with_family Whether to add the familty to the address (e.g. IPv4).
78 * @return The format string for the address.
80 static const char *GetAddressFormatString(uint16_t family
, bool with_family
)
83 case AF_INET
: return with_family
? "{}:{} (IPv4)" : "{}:{}";
84 case AF_INET6
: return with_family
? "[{}]:{} (IPv6)" : "[{}]:{}";
85 default: return with_family
? "{}:{} (IPv?)" : "{}:{}";
90 * Get the address as a string, e.g. 127.0.0.1:12345.
91 * @param with_family whether to add the family (e.g. IPvX).
94 std::string
NetworkAddress::GetAddressAsString(bool with_family
)
96 return fmt::format(fmt::runtime(GetAddressFormatString(this->GetAddress()->ss_family
, with_family
)), this->GetHostname(), this->GetPort());
100 * Helper function to resolve without opening a socket.
101 * @return the opened socket or INVALID_SOCKET
103 static SOCKET
ResolveLoopProc(addrinfo
*)
105 /* We just want the first 'entry', so return a valid socket. */
106 return !INVALID_SOCKET
;
110 * Get the address in its internal representation.
111 * @return the address
113 const sockaddr_storage
*NetworkAddress::GetAddress()
115 if (!this->IsResolved()) {
116 /* Here we try to resolve a network address. We use SOCK_STREAM as
117 * socket type because some stupid OSes, like Solaris, cannot be
118 * bothered to implement the specifications and allow '0' as value
119 * that means "don't care whether it is SOCK_STREAM or SOCK_DGRAM".
121 this->Resolve(this->address
.ss_family
, SOCK_STREAM
, AI_ADDRCONFIG
, nullptr, ResolveLoopProc
);
122 this->resolved
= true;
124 return &this->address
;
128 * Checks of this address is of the given family.
129 * @param family the family to check against
130 * @return true if it is of the given family
132 bool NetworkAddress::IsFamily(int family
)
134 if (!this->IsResolved()) {
135 this->Resolve(family
, SOCK_STREAM
, AI_ADDRCONFIG
, nullptr, ResolveLoopProc
);
137 return this->address
.ss_family
== family
;
141 * Checks whether this IP address is contained by the given netmask.
142 * @param netmask the netmask in CIDR notation to test against.
143 * @note netmask without /n assumes all bits need to match.
144 * @return true if this IP is within the netmask.
146 bool NetworkAddress::IsInNetmask(const std::string
&netmask
)
148 /* Resolve it if we didn't do it already */
149 if (!this->IsResolved()) this->GetAddress();
151 int cidr
= this->address
.ss_family
== AF_INET
? 32 : 128;
153 NetworkAddress mask_address
;
155 /* Check for CIDR separator */
156 auto cidr_separator_location
= netmask
.find('/');
157 if (cidr_separator_location
!= std::string::npos
) {
158 int tmp_cidr
= atoi(netmask
.substr(cidr_separator_location
+ 1).c_str());
160 /* Invalid CIDR, treat as single host */
161 if (tmp_cidr
> 0 && tmp_cidr
< cidr
) cidr
= tmp_cidr
;
163 /* Remove the / so that NetworkAddress works on the IP portion */
164 mask_address
= NetworkAddress(netmask
.substr(0, cidr_separator_location
), 0, this->address
.ss_family
);
166 mask_address
= NetworkAddress(netmask
, 0, this->address
.ss_family
);
169 if (mask_address
.GetAddressLength() == 0) return false;
173 switch (this->address
.ss_family
) {
175 ip
= (uint32_t*)&((struct sockaddr_in
*)&this->address
)->sin_addr
.s_addr
;
176 mask
= (uint32_t*)&((struct sockaddr_in
*)&mask_address
.address
)->sin_addr
.s_addr
;
180 ip
= (uint32_t*)&((struct sockaddr_in6
*)&this->address
)->sin6_addr
;
181 mask
= (uint32_t*)&((struct sockaddr_in6
*)&mask_address
.address
)->sin6_addr
;
189 uint32_t msk
= cidr
>= 32 ? (uint32_t)-1 : htonl(-(1 << (32 - cidr
)));
190 if ((*mask
++ & msk
) != (*ip
++ & msk
)) return false;
199 * Resolve this address into a socket
200 * @param family the type of 'protocol' (IPv4, IPv6)
201 * @param socktype the type of socket (TCP, UDP, etc)
202 * @param flags the flags to send to getaddrinfo
203 * @param sockets the list of sockets to add the sockets to
204 * @param func the inner working while looping over the address info
205 * @return the resolved socket or INVALID_SOCKET.
207 SOCKET
NetworkAddress::Resolve(int family
, int socktype
, int flags
, SocketList
*sockets
, LoopProc func
)
210 struct addrinfo hints
;
211 memset(&hints
, 0, sizeof (hints
));
212 hints
.ai_family
= family
;
213 hints
.ai_flags
= flags
;
214 hints
.ai_socktype
= socktype
;
216 /* The port needs to be a string. Six is enough to contain all characters + '\0'. */
217 std::string port_name
= std::to_string(this->GetPort());
219 bool reset_hostname
= false;
220 /* Setting both hostname to nullptr and port to 0 is not allowed.
221 * As port 0 means bind to any port, the other must mean that
222 * we want to bind to 'all' IPs. */
223 if (this->hostname
.empty() && this->address_length
== 0 && this->GetPort() == 0) {
224 reset_hostname
= true;
225 int fam
= this->address
.ss_family
;
226 if (fam
== AF_UNSPEC
) fam
= family
;
227 this->hostname
= fam
== AF_INET
? "0.0.0.0" : "::";
230 static bool _resolve_timeout_error_message_shown
= false;
231 auto start
= std::chrono::steady_clock::now();
232 int e
= getaddrinfo(this->hostname
.empty() ? nullptr : this->hostname
.c_str(), port_name
.c_str(), &hints
, &ai
);
233 auto end
= std::chrono::steady_clock::now();
234 std::chrono::seconds duration
= std::chrono::duration_cast
<std::chrono::seconds
>(end
- start
);
235 if (!_resolve_timeout_error_message_shown
&& duration
>= std::chrono::seconds(5)) {
236 Debug(net
, 0, "getaddrinfo for hostname \"{}\", port {}, address family {} and socket type {} took {} seconds",
237 this->hostname
, port_name
, AddressFamilyAsString(family
), SocketTypeAsString(socktype
), duration
.count());
238 Debug(net
, 0, " this is likely an issue in the DNS name resolver's configuration causing it to time out");
239 _resolve_timeout_error_message_shown
= true;
243 if (reset_hostname
) this->hostname
.clear();
246 if (func
!= ResolveLoopProc
) {
247 Debug(net
, 0, "getaddrinfo for hostname \"{}\", port {}, address family {} and socket type {} failed: {}",
248 this->hostname
, port_name
, AddressFamilyAsString(family
), SocketTypeAsString(socktype
), FS2OTTD(gai_strerror(e
)));
250 return INVALID_SOCKET
;
253 SOCKET sock
= INVALID_SOCKET
;
254 for (struct addrinfo
*runp
= ai
; runp
!= nullptr; runp
= runp
->ai_next
) {
255 /* When we are binding to multiple sockets, make sure we do not
256 * connect to one with exactly the same address twice. That's
257 * of course totally unneeded ;) */
258 if (sockets
!= nullptr) {
259 NetworkAddress
address(runp
->ai_addr
, (int)runp
->ai_addrlen
);
260 if (std::any_of(sockets
->begin(), sockets
->end(), [&address
](const auto &p
) { return p
.second
== address
; })) continue;
263 if (sock
== INVALID_SOCKET
) continue;
265 if (sockets
== nullptr) {
266 this->address_length
= (int)runp
->ai_addrlen
;
267 assert(sizeof(this->address
) >= runp
->ai_addrlen
);
268 memcpy(&this->address
, runp
->ai_addr
, runp
->ai_addrlen
);
269 #ifdef __EMSCRIPTEN__
270 /* Emscripten doesn't zero sin_zero, but as we compare addresses
271 * to see if they are the same address, we need them to be zero'd.
272 * Emscripten is, as far as we know, the only OS not doing this.
274 * https://github.com/emscripten-core/emscripten/issues/12998
276 if (this->address
.ss_family
== AF_INET
) {
277 sockaddr_in
*address_ipv4
= (sockaddr_in
*)&this->address
;
278 memset(address_ipv4
->sin_zero
, 0, sizeof(address_ipv4
->sin_zero
));
284 NetworkAddress
addr(runp
->ai_addr
, (int)runp
->ai_addrlen
);
285 (*sockets
)[sock
] = addr
;
286 sock
= INVALID_SOCKET
;
294 * Helper function to resolve a listening.
295 * @param runp information about the socket to try not
296 * @return the opened socket or INVALID_SOCKET
298 static SOCKET
ListenLoopProc(addrinfo
*runp
)
300 std::string address
= NetworkAddress(runp
->ai_addr
, (int)runp
->ai_addrlen
).GetAddressAsString();
302 SOCKET sock
= socket(runp
->ai_family
, runp
->ai_socktype
, runp
->ai_protocol
);
303 if (sock
== INVALID_SOCKET
) {
304 const char *type
= NetworkAddress::SocketTypeAsString(runp
->ai_socktype
);
305 const char *family
= NetworkAddress::AddressFamilyAsString(runp
->ai_family
);
306 Debug(net
, 0, "Could not create {} {} socket: {}", type
, family
, NetworkError::GetLast().AsString());
307 return INVALID_SOCKET
;
310 if (runp
->ai_socktype
== SOCK_STREAM
&& !SetNoDelay(sock
)) {
311 Debug(net
, 1, "Setting no-delay mode failed: {}", NetworkError::GetLast().AsString());
314 if (!SetReusePort(sock
)) {
315 Debug(net
, 0, "Setting reuse-address mode failed: {}", NetworkError::GetLast().AsString());
319 if (runp
->ai_family
== AF_INET6
&&
320 setsockopt(sock
, IPPROTO_IPV6
, IPV6_V6ONLY
, (const char*)&on
, sizeof(on
)) == -1) {
321 Debug(net
, 3, "Could not disable IPv4 over IPv6: {}", NetworkError::GetLast().AsString());
324 if (bind(sock
, runp
->ai_addr
, (int)runp
->ai_addrlen
) != 0) {
325 Debug(net
, 0, "Could not bind socket on {}: {}", address
, NetworkError::GetLast().AsString());
327 return INVALID_SOCKET
;
330 if (runp
->ai_socktype
!= SOCK_DGRAM
&& listen(sock
, 1) != 0) {
331 Debug(net
, 0, "Could not listen on socket: {}", NetworkError::GetLast().AsString());
333 return INVALID_SOCKET
;
336 /* Connection succeeded */
338 if (!SetNonBlocking(sock
)) {
339 Debug(net
, 0, "Setting non-blocking mode failed: {}", NetworkError::GetLast().AsString());
342 Debug(net
, 3, "Listening on {}", address
);
347 * Make the given socket listen.
348 * @param socktype the type of socket (TCP, UDP, etc)
349 * @param sockets the list of sockets to add the sockets to
351 void NetworkAddress::Listen(int socktype
, SocketList
*sockets
)
353 assert(sockets
!= nullptr);
355 /* Setting both hostname to "" and port to 0 is not allowed.
356 * As port 0 means bind to any port, the other must mean that
357 * we want to bind to 'all' IPs. */
358 if (this->address_length
== 0 && this->address
.ss_family
== AF_UNSPEC
&&
359 this->hostname
.empty() && this->GetPort() == 0) {
360 this->Resolve(AF_INET
, socktype
, AI_ADDRCONFIG
| AI_PASSIVE
, sockets
, ListenLoopProc
);
361 this->Resolve(AF_INET6
, socktype
, AI_ADDRCONFIG
| AI_PASSIVE
, sockets
, ListenLoopProc
);
363 this->Resolve(AF_UNSPEC
, socktype
, AI_ADDRCONFIG
| AI_PASSIVE
, sockets
, ListenLoopProc
);
368 * Convert the socket type into a string
369 * @param socktype the socket type to convert
370 * @return the string representation
371 * @note only works for SOCK_STREAM and SOCK_DGRAM
373 /* static */ const char *NetworkAddress::SocketTypeAsString(int socktype
)
376 case SOCK_STREAM
: return "tcp";
377 case SOCK_DGRAM
: return "udp";
378 default: return "unsupported";
383 * Convert the address family into a string
384 * @param family the family to convert
385 * @return the string representation
386 * @note only works for AF_INET, AF_INET6 and AF_UNSPEC
388 /* static */ const char *NetworkAddress::AddressFamilyAsString(int family
)
391 case AF_UNSPEC
: return "either IPv4 or IPv6";
392 case AF_INET
: return "IPv4";
393 case AF_INET6
: return "IPv6";
394 default: return "unsupported";
399 * Get the peer address of a socket as NetworkAddress.
400 * @param sock The socket to get the peer address of.
401 * @return The NetworkAddress of the peer address.
403 /* static */ NetworkAddress
NetworkAddress::GetPeerAddress(SOCKET sock
)
405 sockaddr_storage addr
= {};
406 socklen_t addr_len
= sizeof(addr
);
407 if (getpeername(sock
, (sockaddr
*)&addr
, &addr_len
) != 0) {
408 Debug(net
, 0, "Failed to get address of the peer: {}", NetworkError::GetLast().AsString());
409 return NetworkAddress();
411 return NetworkAddress(addr
, addr_len
);
415 * Get the local address of a socket as NetworkAddress.
416 * @param sock The socket to get the local address of.
417 * @return The NetworkAddress of the local address.
419 /* static */ NetworkAddress
NetworkAddress::GetSockAddress(SOCKET sock
)
421 sockaddr_storage addr
= {};
422 socklen_t addr_len
= sizeof(addr
);
423 if (getsockname(sock
, (sockaddr
*)&addr
, &addr_len
) != 0) {
424 Debug(net
, 0, "Failed to get address of the socket: {}", NetworkError::GetLast().AsString());
425 return NetworkAddress();
427 return NetworkAddress(addr
, addr_len
);
431 * Get the peer name of a socket in string format.
432 * @param sock The socket to get the peer name of.
433 * @return The string representation of the peer name.
435 /* static */ const std::string
NetworkAddress::GetPeerName(SOCKET sock
)
437 return NetworkAddress::GetPeerAddress(sock
).GetAddressAsString();
441 * Convert a string containing either "hostname", "hostname:port" or invite code
442 * to a ServerAddress, where the string can be postfixed with "#company" to
443 * indicate the requested company.
445 * @param connection_string The string to parse.
446 * @param default_port The default port to set port to if not in connection_string.
447 * @param company Pointer to the company variable to set iff indicated.
448 * @return A valid ServerAddress of the parsed information.
450 /* static */ ServerAddress
ServerAddress::Parse(const std::string
&connection_string
, uint16_t default_port
, CompanyID
*company_id
)
452 if (connection_string
.starts_with("+")) {
453 std::string_view invite_code
= ParseCompanyFromConnectionString(connection_string
, company_id
);
454 return ServerAddress(SERVER_ADDRESS_INVITE_CODE
, std::string(invite_code
));
457 uint16_t port
= default_port
;
458 std::string_view ip
= ParseFullConnectionString(connection_string
, port
, company_id
);
459 return ServerAddress(SERVER_ADDRESS_DIRECT
, std::string(ip
) + ":" + std::to_string(port
));