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);
117 return !INVALID_SOCKET;
148 return this->
address.ss_family == family;
162 int cidr = this->
address.ss_family == AF_INET ? 32 : 128;
167 const char *chr_cidr = strchr(netmask,
'/');
168 if (chr_cidr !=
nullptr) {
169 int tmp_cidr = atoi(chr_cidr + 1);
172 if (tmp_cidr > 0 || tmp_cidr < cidr) cidr = tmp_cidr;
175 std::string ip_str(netmask, chr_cidr - netmask);
185 switch (this->
address.ss_family) {
187 ip = (uint32*)&((
struct sockaddr_in*)&this->
address)->sin_addr.s_addr;
188 mask = (uint32*)&((
struct sockaddr_in*)&mask_address.
address)->sin_addr.s_addr;
192 ip = (uint32*)&((
struct sockaddr_in6*)&this->
address)->sin6_addr;
193 mask = (uint32*)&((
struct sockaddr_in6*)&mask_address.
address)->sin6_addr;
201 uint32 msk = cidr >= 32 ? (uint32)-1 : htonl(-(1 << (32 - cidr)));
202 if ((*mask++ & msk) != (*ip++ & msk))
return false;
222 struct addrinfo hints;
223 memset(&hints, 0,
sizeof (hints));
224 hints.ai_family = family;
225 hints.ai_flags = flags;
226 hints.ai_socktype = socktype;
232 bool reset_hostname =
false;
237 reset_hostname =
true;
238 int fam = this->
address.ss_family;
239 if (fam == AF_UNSPEC) fam = family;
245 if (reset_hostname)
strecpy(this->hostname,
"",
lastof(this->hostname));
249 DEBUG(net, 0,
"getaddrinfo for hostname \"%s\", port %s, address family %s and socket type %s failed: %s",
252 return INVALID_SOCKET;
255 SOCKET sock = INVALID_SOCKET;
256 for (
struct addrinfo *runp = ai; runp !=
nullptr; runp = runp->ai_next) {
260 if (sockets !=
nullptr) {
262 if (sockets->
Contains(address))
continue;
265 if (sock == INVALID_SOCKET)
continue;
267 if (sockets ==
nullptr) {
269 assert(
sizeof(this->
address) >= runp->ai_addrlen);
270 memcpy(&this->
address, runp->ai_addr, runp->ai_addrlen);
275 (*sockets)[addr] = sock;
276 sock = INVALID_SOCKET;
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);
338 SOCKET sock = socket(runp->ai_family, runp->ai_socktype, runp->ai_protocol);
339 if (sock == INVALID_SOCKET) {
340 DEBUG(net, 0,
"[%s] could not create %s socket on port %s: %s", type, family, address, strerror(errno));
341 return INVALID_SOCKET;
344 if (runp->ai_socktype == SOCK_STREAM && !
SetNoDelay(sock)) {
345 DEBUG(net, 3,
"[%s] setting TCP_NODELAY failed for port %s", type, address);
350 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (
const char*)&on,
sizeof(on)) == -1) {
351 DEBUG(net, 3,
"[%s] could not set reusable %s sockets for port %s: %s", type, family, address, strerror(errno));
355 if (runp->ai_family == AF_INET6 &&
356 setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (
const char*)&on,
sizeof(on)) == -1) {
357 DEBUG(net, 3,
"[%s] could not disable IPv4 over IPv6 on port %s: %s", type, address, strerror(errno));
361 if (bind(sock, runp->ai_addr, (
int)runp->ai_addrlen) != 0) {
362 DEBUG(net, 1,
"[%s] could not bind on %s port %s: %s", type, family, address, strerror(errno));
364 return INVALID_SOCKET;
367 if (runp->ai_socktype != SOCK_DGRAM && listen(sock, 1) != 0) {
368 DEBUG(net, 1,
"[%s] could not listen at %s port %s: %s", type, family, address, strerror(errno));
370 return INVALID_SOCKET;
374 if (!
SetNonBlocking(sock))
DEBUG(net, 0,
"[%s] setting non-blocking mode failed for %s port %s", type, family, address);
376 DEBUG(net, 1,
"[%s] listening on %s port %s", type, family, address);
387 assert(sockets !=
nullptr);
410 case SOCK_STREAM:
return "tcp";
411 case SOCK_DGRAM:
return "udp";
412 default:
return "unsupported";
425 case AF_UNSPEC:
return "either IPv4 or IPv6";
426 case AF_INET:
return "IPv4";
427 case AF_INET6:
return "IPv6";
428 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.