OpenTTD Source: src/network/core/address.h Source File

OpenTTD Source 1.11.0-RC1

address.h
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 #ifndef NETWORK_CORE_ADDRESS_H
11 #define NETWORK_CORE_ADDRESS_H
12
13 #include "os_abstraction.h"
14 #include "config.h"
15 #include "../../string_func.h"
16 #include "../../core/smallmap_type.hpp"
17
18 #include <string>
19
20 class NetworkAddress;
21 typedef std::vector<NetworkAddress> NetworkAddressList;
22 typedef SmallMap<NetworkAddress, SOCKET> SocketList;
23
29 class NetworkAddress {
30 private:
31 char hostname[NETWORK_HOSTNAME_LENGTH];
32 int address_length;
33 sockaddr_storage address;
34 bool resolved;
35
41 typedef SOCKET (*LoopProc)(addrinfo *runp);
42
43 SOCKET Resolve(int family, int socktype, int flags, SocketList *sockets, LoopProc func);
44 public:
50 NetworkAddress(struct sockaddr_storage &address, int address_length) :
51 address_length(address_length),
52 address(address),
53 resolved(address_length != 0)
54 {
55 *this->hostname = '\0';
56 }
57
63 NetworkAddress(sockaddr *address, int address_length) :
64 address_length(address_length),
65 resolved(address_length != 0)
66 {
67 *this->hostname = '\0';
68 memset(&this->address, 0, sizeof(this->address));
69 memcpy(&this->address, address, address_length);
70 }
71
78 NetworkAddress(const char *hostname = "", uint16 port = 0, int family = AF_UNSPEC) :
79 address_length(0),
80 resolved(false)
81 {
82 /* Also handle IPv6 bracket enclosed hostnames */
83 if (StrEmpty(hostname)) hostname = "";
84 if (*hostname == '[') hostname++;
85 strecpy(this->hostname, StrEmpty(hostname) ? "" : hostname, lastof(this->hostname));
86 char *tmp = strrchr(this->hostname, ']');
87 if (tmp != nullptr) *tmp = '\0';
88
89 memset(&this->address, 0, sizeof(this->address));
90 this->address.ss_family = family;
91 this->SetPort(port);
92 }
93
94 const char *GetHostname();
95 void GetAddressAsString(char *buffer, const char *last, bool with_family = true);
96 std::string GetAddressAsString(bool with_family = true);
97 const sockaddr_storage *GetAddress();
98
103 int GetAddressLength()
104 {
105 /* Resolve it if we didn't do it already */
106 if (!this->IsResolved()) this->GetAddress();
107 return this->address_length;
108 }
109
110 uint16 GetPort() const;
111 void SetPort(uint16 port);
112
117 bool IsResolved() const
118 {
119 return this->resolved;
120 }
121
122 bool IsFamily(int family);
123 bool IsInNetmask(const char *netmask);
124
130 int CompareTo(NetworkAddress &address)
131 {
132 int r = this->GetAddressLength() - address.GetAddressLength();
133 if (r == 0) r = this->address.ss_family - address.address.ss_family;
134 if (r == 0) r = memcmp(&this->address, &address.address, this->address_length);
135 if (r == 0) r = this->GetPort() - address.GetPort();
136 return r;
137 }
138
144 bool operator == (NetworkAddress &address)
145 {
146 return this->CompareTo(address) == 0;
147 }
148
154 bool operator == (NetworkAddress &address) const
155 {
156 return const_cast<NetworkAddress*>(this)->CompareTo(address) == 0;
157 }
163 bool operator != (NetworkAddress address) const
164 {
165 return const_cast<NetworkAddress*>(this)->CompareTo(address) != 0;
166 }
167
172 bool operator < (NetworkAddress &address)
173 {
174 return this->CompareTo(address) < 0;
175 }
176
177 SOCKET Connect();
178 void Listen(int socktype, SocketList *sockets);
179
180 static const char *SocketTypeAsString(int socktype);
181 static const char *AddressFamilyAsString(int family);
182 };
183
184 #endif /* NETWORK_CORE_ADDRESS_H */
NetworkAddress::Connect
SOCKET Connect()
Connect to the given address.
Definition: address.cpp:340
SocketList
SmallMap< NetworkAddress, SOCKET > SocketList
Type for a mapping between address and socket.
Definition: address.h:22
NetworkAddress::GetAddress
const sockaddr_storage * GetAddress()
Get the address in its internal representation.
Definition: address.cpp:123
NetworkAddress::GetAddressLength
int GetAddressLength()
Get the (valid) length of the address.
Definition: address.h:103
NetworkAddress::SetPort
void SetPort(uint16 port)
Set the port.
Definition: address.cpp:54
NetworkAddress::Resolve
SOCKET Resolve(int family, int socktype, int flags, SocketList *sockets, LoopProc func)
Resolve this address into a socket.
Definition: address.cpp:218
NetworkAddress::resolved
bool resolved
Whether the address has been (tried to be) resolved.
Definition: address.h:34
NetworkAddress::IsInNetmask
bool IsInNetmask(const char *netmask)
Checks whether this IP address is contained by the given netmask.
Definition: address.cpp:156
SmallMap< NetworkAddress, SOCKET >
NetworkAddress::operator!=
bool operator!=(NetworkAddress address) const
Compare the address of this class with the address of another.
Definition: address.h:163
NetworkAddressList
std::vector< NetworkAddress > NetworkAddressList
Type for a list of addresses.
Definition: address.h:20
NetworkAddress::GetHostname
const char * GetHostname()
Get the hostname; in case it wasn't given the IPv4 dotted representation is given.
Definition: address.cpp:22
NetworkAddress::GetPort
uint16 GetPort() const
Get the port.
Definition: address.cpp:35
NetworkAddress::address_length
int address_length
The length of the resolved address.
Definition: address.h:32
StrEmpty
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:60
NetworkAddress::NetworkAddress
NetworkAddress(struct sockaddr_storage &address, int address_length)
Create a network address based on a resolved IP and port.
Definition: address.h:50
NetworkAddress::NetworkAddress
NetworkAddress(const char *hostname="", uint16 port=0, int family=AF_UNSPEC)
Create a network address based on a unresolved host and port.
Definition: address.h:78
NetworkAddress::operator==
bool operator==(NetworkAddress &address)
Compare the address of this class with the address of another.
Definition: address.h:144
NetworkAddress
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
Definition: address.h:29
NetworkAddress::hostname
char hostname[NETWORK_HOSTNAME_LENGTH]
The hostname.
Definition: address.h:31
NetworkAddress::IsResolved
bool IsResolved() const
Check whether the IP address has been resolved already.
Definition: address.h:117
NetworkAddress::CompareTo
int CompareTo(NetworkAddress &address)
Compare the address of this class with the address of another.
Definition: address.h:130
NetworkAddress::LoopProc
SOCKET(* LoopProc)(addrinfo *runp)
Helper function to resolve something to a socket.
Definition: address.h:41
NetworkAddress::NetworkAddress
NetworkAddress(sockaddr *address, int address_length)
Create a network address based on a resolved IP and port.
Definition: address.h:63
NetworkAddress::SocketTypeAsString
static const char * SocketTypeAsString(int socktype)
Convert the socket type into a string.
Definition: address.cpp:428
os_abstraction.h
NetworkAddress::IsFamily
bool IsFamily(int family)
Checks of this address is of the given family.
Definition: address.cpp:142
config.h
NETWORK_HOSTNAME_LENGTH
static const uint NETWORK_HOSTNAME_LENGTH
The maximum length of the host name, in bytes including '\0'.
Definition: config.h:42
strecpy
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: string.cpp:112
NetworkAddress::AddressFamilyAsString
static const char * AddressFamilyAsString(int family)
Convert the address family into a string.
Definition: address.cpp:443
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:383
NetworkAddress::operator<
bool operator<(NetworkAddress &address)
Compare the address of this class with the address of another.
Definition: address.h:172
NetworkAddress::Listen
void Listen(int socktype, SocketList *sockets)
Make the given socket listen.
Definition: address.cpp:406
NetworkAddress::GetAddressAsString
void GetAddressAsString(char *buffer, const char *last, bool with_family=true)
Get the address as a string, e.g.
Definition: address.cpp:77
NetworkAddress::address
sockaddr_storage address
The resolved address.
Definition: address.h:33