10 #include "../../stdafx.h" 13 #include "../../debug.h" 15 #include "../../safeguards.h" 37 switch (this->
address.ss_family) {
40 return ntohs(((
const struct sockaddr_in *)&this->
address)->sin_port);
43 return ntohs(((
const struct sockaddr_in6 *)&this->address)->sin6_port);
56 switch (this->
address.ss_family) {
59 ((
struct sockaddr_in*)&this->
address)->sin_port = htons(port);
63 ((
struct sockaddr_in6*)&this->
address)->sin6_port = htons(port);
79 if (this->
GetAddress()->ss_family == AF_INET6) buffer =
strecpy(buffer,
"[", last);
81 if (this->
GetAddress()->ss_family == AF_INET6) buffer =
strecpy(buffer,
"]", last);
86 switch (this->
address.ss_family) {
87 case AF_INET: family =
'4';
break;
88 case AF_INET6: family =
'6';
break;
89 default: family =
'?';
break;
91 seprintf(buffer, last,
" (IPv%c)", family);
116 return !INVALID_SOCKET;
147 return this->
address.ss_family == family;
161 int cidr = this->
address.ss_family == AF_INET ? 32 : 128;
166 const char *chr_cidr = strchr(netmask,
'/');
167 if (chr_cidr !=
nullptr) {
168 int tmp_cidr = atoi(chr_cidr + 1);
171 if (tmp_cidr > 0 || tmp_cidr < cidr) cidr = tmp_cidr;
174 std::string ip_str(netmask, chr_cidr - netmask);
184 switch (this->
address.ss_family) {
186 ip = (uint32*)&((
struct sockaddr_in*)&this->
address)->sin_addr.s_addr;
187 mask = (uint32*)&((
struct sockaddr_in*)&mask_address.
address)->sin_addr.s_addr;
191 ip = (uint32*)&((
struct sockaddr_in6*)&this->
address)->sin6_addr;
192 mask = (uint32*)&((
struct sockaddr_in6*)&mask_address.
address)->sin6_addr;
200 uint32 msk = cidr >= 32 ? (uint32)-1 : htonl(-(1 << (32 - cidr)));
201 if ((*mask++ & msk) != (*ip++ & msk))
return false;
221 struct addrinfo hints;
222 memset(&hints, 0,
sizeof (hints));
223 hints.ai_family = family;
224 hints.ai_flags = flags;
225 hints.ai_socktype = socktype;
231 bool reset_hostname =
false;
236 reset_hostname =
true;
237 int fam = this->
address.ss_family;
238 if (fam == AF_UNSPEC) fam = family;
244 if (reset_hostname)
strecpy(this->hostname,
"",
lastof(this->hostname));
248 DEBUG(net, 0,
"getaddrinfo for hostname \"%s\", port %s, address family %s and socket type %s failed: %s",
251 return INVALID_SOCKET;
254 SOCKET sock = INVALID_SOCKET;
255 for (
struct addrinfo *runp = ai; runp !=
nullptr; runp = runp->ai_next) {
259 if (sockets !=
nullptr) {
261 if (sockets->
Contains(address))
continue;
264 if (sock == INVALID_SOCKET)
continue;
266 if (sockets ==
nullptr) {
268 assert(
sizeof(this->
address) >= runp->ai_addrlen);
269 memcpy(&this->
address, runp->ai_addr, runp->ai_addrlen);
274 (*sockets)[addr] = sock;
275 sock = INVALID_SOCKET;
292 NetworkAddress(runp->ai_addr, (
int)runp->ai_addrlen).GetAddressAsString(address,
lastof(address));
294 SOCKET sock = socket(runp->ai_family, runp->ai_socktype, runp->ai_protocol);
295 if (sock == INVALID_SOCKET) {
296 DEBUG(net, 1,
"[%s] could not create %s socket: %s", type, family, strerror(errno));
297 return INVALID_SOCKET;
300 if (!
SetNoDelay(sock))
DEBUG(net, 1,
"[%s] setting TCP_NODELAY failed", type);
302 if (connect(sock, runp->ai_addr, (
int)runp->ai_addrlen) != 0) {
303 DEBUG(net, 1,
"[%s] could not connect %s socket: %s", type, family, strerror(errno));
305 return INVALID_SOCKET;
311 DEBUG(net, 1,
"[%s] connected to %s", type, address);
337 NetworkAddress(runp->ai_addr, (
int)runp->ai_addrlen).GetAddressAsString(address,
lastof(address));
339 SOCKET sock = socket(runp->ai_family, runp->ai_socktype, runp->ai_protocol);
340 if (sock == INVALID_SOCKET) {
341 DEBUG(net, 0,
"[%s] could not create %s socket on port %s: %s", type, family, address, strerror(errno));
342 return INVALID_SOCKET;
345 if (runp->ai_socktype == SOCK_STREAM && !
SetNoDelay(sock)) {
346 DEBUG(net, 3,
"[%s] setting TCP_NODELAY failed for port %s", type, address);
351 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (
const char*)&on,
sizeof(on)) == -1) {
352 DEBUG(net, 3,
"[%s] could not set reusable %s sockets for port %s: %s", type, family, address, strerror(errno));
356 if (runp->ai_family == AF_INET6 &&
357 setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (
const char*)&on,
sizeof(on)) == -1) {
358 DEBUG(net, 3,
"[%s] could not disable IPv4 over IPv6 on port %s: %s", type, address, strerror(errno));
362 if (bind(sock, runp->ai_addr, (
int)runp->ai_addrlen) != 0) {
363 DEBUG(net, 1,
"[%s] could not bind on %s port %s: %s", type, family, address, strerror(errno));
365 return INVALID_SOCKET;
368 if (runp->ai_socktype != SOCK_DGRAM && listen(sock, 1) != 0) {
369 DEBUG(net, 1,
"[%s] could not listen at %s port %s: %s", type, family, address, strerror(errno));
371 return INVALID_SOCKET;
375 if (!
SetNonBlocking(sock))
DEBUG(net, 0,
"[%s] setting non-blocking mode failed for %s port %s", type, family, address);
377 DEBUG(net, 1,
"[%s] listening on %s port %s", type, family, address);
388 assert(sockets !=
nullptr);
411 case SOCK_STREAM:
return "tcp";
412 case SOCK_DGRAM:
return "udp";
413 default:
return "unsupported";
426 case AF_UNSPEC:
return "either IPv4 or IPv6";
427 case AF_INET:
return "IPv4";
428 case AF_INET6:
return "IPv6";
429 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.