12 #include "../../stdafx.h" 15 #include "../../debug.h" 17 #include "../../safeguards.h" 39 switch (this->
address.ss_family) {
42 return ntohs(((
const struct sockaddr_in *)&this->
address)->sin_port);
45 return ntohs(((
const struct sockaddr_in6 *)&this->address)->sin6_port);
58 switch (this->
address.ss_family) {
61 ((
struct sockaddr_in*)&this->
address)->sin_port = htons(port);
65 ((
struct sockaddr_in6*)&this->
address)->sin6_port = htons(port);
81 if (this->
GetAddress()->ss_family == AF_INET6) buffer =
strecpy(buffer,
"[", last);
83 if (this->
GetAddress()->ss_family == AF_INET6) buffer =
strecpy(buffer,
"]", last);
88 switch (this->
address.ss_family) {
89 case AF_INET: family =
'4';
break;
90 case AF_INET6: family =
'6';
break;
91 default: family =
'?';
break;
93 seprintf(buffer, last,
" (IPv%c)", family);
119 return !INVALID_SOCKET;
150 return this->
address.ss_family == family;
164 int cidr = this->
address.ss_family == AF_INET ? 32 : 128;
169 const char *chr_cidr = strchr(netmask,
'/');
170 if (chr_cidr !=
nullptr) {
171 int tmp_cidr = atoi(chr_cidr + 1);
174 if (tmp_cidr > 0 || tmp_cidr < cidr) cidr = tmp_cidr;
177 std::string ip_str(netmask, chr_cidr - netmask);
187 switch (this->
address.ss_family) {
189 ip = (uint32*)&((
struct sockaddr_in*)&this->
address)->sin_addr.s_addr;
190 mask = (uint32*)&((
struct sockaddr_in*)&mask_address.
address)->sin_addr.s_addr;
194 ip = (uint32*)&((
struct sockaddr_in6*)&this->
address)->sin6_addr;
195 mask = (uint32*)&((
struct sockaddr_in6*)&mask_address.
address)->sin6_addr;
203 uint32 msk = cidr >= 32 ? (uint32)-1 : htonl(-(1 << (32 - cidr)));
204 if ((*mask++ & msk) != (*ip++ & msk))
return false;
224 struct addrinfo hints;
225 memset(&hints, 0,
sizeof (hints));
226 hints.ai_family = family;
227 hints.ai_flags = flags;
228 hints.ai_socktype = socktype;
234 bool reset_hostname =
false;
239 reset_hostname =
true;
240 int fam = this->
address.ss_family;
241 if (fam == AF_UNSPEC) fam = family;
247 if (reset_hostname)
strecpy(this->hostname,
"",
lastof(this->hostname));
251 DEBUG(net, 0,
"getaddrinfo for hostname \"%s\", port %s, address family %s and socket type %s failed: %s",
254 return INVALID_SOCKET;
257 SOCKET sock = INVALID_SOCKET;
258 for (
struct addrinfo *runp = ai; runp !=
nullptr; runp = runp->ai_next) {
262 if (sockets !=
nullptr) {
264 if (sockets->
Contains(address))
continue;
267 if (sock == INVALID_SOCKET)
continue;
269 if (sockets ==
nullptr) {
271 assert(
sizeof(this->
address) >= runp->ai_addrlen);
272 memcpy(&this->
address, runp->ai_addr, runp->ai_addrlen);
277 (*sockets)[addr] = sock;
278 sock = INVALID_SOCKET;
296 SOCKET sock = socket(runp->ai_family, runp->ai_socktype, runp->ai_protocol);
297 if (sock == INVALID_SOCKET) {
298 DEBUG(net, 1,
"[%s] could not create %s socket: %s", type, family, strerror(errno));
299 return INVALID_SOCKET;
302 if (!
SetNoDelay(sock))
DEBUG(net, 1,
"[%s] setting TCP_NODELAY failed", type);
304 if (connect(sock, runp->ai_addr, (
int)runp->ai_addrlen) != 0) {
305 DEBUG(net, 1,
"[%s] could not connect %s socket: %s", type, family, strerror(errno));
307 return INVALID_SOCKET;
313 DEBUG(net, 1,
"[%s] connected to %s", type, address);
340 SOCKET sock = socket(runp->ai_family, runp->ai_socktype, runp->ai_protocol);
341 if (sock == INVALID_SOCKET) {
342 DEBUG(net, 0,
"[%s] could not create %s socket on port %s: %s", type, family, address, strerror(errno));
343 return INVALID_SOCKET;
346 if (runp->ai_socktype == SOCK_STREAM && !
SetNoDelay(sock)) {
347 DEBUG(net, 3,
"[%s] setting TCP_NODELAY failed for port %s", type, address);
352 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (
const char*)&on,
sizeof(on)) == -1) {
353 DEBUG(net, 3,
"[%s] could not set reusable %s sockets for port %s: %s", type, family, address, strerror(errno));
357 if (runp->ai_family == AF_INET6 &&
358 setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (
const char*)&on,
sizeof(on)) == -1) {
359 DEBUG(net, 3,
"[%s] could not disable IPv4 over IPv6 on port %s: %s", type, address, strerror(errno));
363 if (bind(sock, runp->ai_addr, (
int)runp->ai_addrlen) != 0) {
364 DEBUG(net, 1,
"[%s] could not bind on %s port %s: %s", type, family, address, strerror(errno));
366 return INVALID_SOCKET;
369 if (runp->ai_socktype != SOCK_DGRAM && listen(sock, 1) != 0) {
370 DEBUG(net, 1,
"[%s] could not listen at %s port %s: %s", type, family, address, strerror(errno));
372 return INVALID_SOCKET;
376 if (!
SetNonBlocking(sock))
DEBUG(net, 0,
"[%s] setting non-blocking mode failed for %s port %s", type, family, address);
378 DEBUG(net, 1,
"[%s] listening on %s port %s", type, family, address);
389 assert(sockets !=
nullptr);
412 case SOCK_STREAM:
return "tcp";
413 case SOCK_DGRAM:
return "udp";
414 default:
return "unsupported";
427 case AF_UNSPEC:
return "either IPv4 or IPv6";
428 case AF_INET:
return "IPv4";
429 case AF_INET6:
return "IPv6";
430 default:
return "unsupported";
const char * FS2OTTD(const TCHAR *name)
Convert to OpenTTD's encoding from that of the local environment.
sockaddr_storage address
The resolved address.
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
NetworkAddress(struct sockaddr_storage &address, int address_length)
Create a network address based on a resolved IP and port.
#define lastof(x)
Get the last element of an fixed size array.
bool IsFamily(int family)
Checks of this address is of the given family.
static SOCKET ListenLoopProc(addrinfo *runp)
Helper function to resolve a listening.
int GetAddressLength()
Get the (valid) length of the address.
bool Contains(const T &key) const
Tests whether a key is assigned in this map.
static SOCKET ConnectLoopProc(addrinfo *runp)
Helper function to resolve a connected socket.
Wrapper for network addresses.
const char * GetHostname()
Get the hostname; in case it wasn't given the IPv4 dotted representation is given.
static const char * SocketTypeAsString(int socktype)
Convert the socket type into a string.
static SOCKET ResolveLoopProc(addrinfo *runp)
Helper function to resolve without opening a socket.
static const uint NETWORK_HOSTNAME_LENGTH
The maximum length of the host name, in bytes including '\0'.
bool IsInNetmask(const char *netmask)
Checks whether this IP address is contained by the given netmask.
#define DEBUG(name, level,...)
Output a line of debugging information.
void SetPort(uint16 port)
Set the port.
SOCKET Resolve(int family, int socktype, int flags, SocketList *sockets, LoopProc func)
Resolve this address into a socket.
SOCKET Connect()
Connect to the given address.
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
static bool SetNonBlocking(SOCKET d)
Try to set the socket into non-blocking mode.
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
static const char * AddressFamilyAsString(int family)
Convert the address family into a string.
SOCKET(* LoopProc)(addrinfo *runp)
Helper function to resolve something to a socket.
uint16 GetPort() const
Get the port.
char hostname[NETWORK_HOSTNAME_LENGTH]
The hostname.
static bool SetNoDelay(SOCKET d)
Try to set the socket to not delay sending.
bool resolved
Whether the address has been (tried to be) resolved.
const sockaddr_storage * GetAddress()
Get the address in its internal representation.
void GetAddressAsString(char *buffer, const char *last, bool with_family=true)
Get the address as a string, e.g.
int address_length
The length of the resolved address.
bool IsResolved() const
Check whether the IP address has been resolved already.
void Listen(int socktype, SocketList *sockets)
Make the given socket listen.