OpenTTD

address.h
Go to the documentation of this file.
1 /* $Id: address.h 22695 2011-07-30 10:28:52Z rubidium $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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 */
9
12 #ifndef NETWORK_CORE_ADDRESS_H
13 #define NETWORK_CORE_ADDRESS_H
14
15 #include "os_abstraction.h"
16 #include "config.h"
17 #include "../../string_func.h"
18 #include "../../core/smallmap_type.hpp"
19
20 #ifdef ENABLE_NETWORK
21
22 class NetworkAddress;
23 typedef SmallVector<NetworkAddress, 4> NetworkAddressList;
24 typedef SmallMap<NetworkAddress, SOCKET, 4> SocketList;
25
31 class NetworkAddress {
32 private:
33 char hostname[NETWORK_HOSTNAME_LENGTH];
34 int address_length;
35 sockaddr_storage address;
36 bool resolved;
37
43 typedef SOCKET (*LoopProc)(addrinfo *runp);
44
45 SOCKET Resolve(int family, int socktype, int flags, SocketList *sockets, LoopProc func);
46 public:
52 NetworkAddress(struct sockaddr_storage &address, int address_length) :
53 address_length(address_length),
54 address(address),
55 resolved(address_length != 0)
56 {
57 *this->hostname = '\0';
58 }
59
65 NetworkAddress(sockaddr *address, int address_length) :
66 address_length(address_length),
67 resolved(address_length != 0)
68 {
69 *this->hostname = '\0';
70 memset(&this->address, 0, sizeof(this->address));
71 memcpy(&this->address, address, address_length);
72 }
73
80 NetworkAddress(const char *hostname = "", uint16 port = 0, int family = AF_UNSPEC) :
81 address_length(0),
82 resolved(false)
83 {
84 /* Also handle IPv6 bracket enclosed hostnames */
85 if (StrEmpty(hostname)) hostname = "";
86 if (*hostname == '[') hostname++;
87 strecpy(this->hostname, StrEmpty(hostname) ? "" : hostname, lastof(this->hostname));
88 char *tmp = strrchr(this->hostname, ']');
89 if (tmp != NULL) *tmp = '\0';
90
91 memset(&this->address, 0, sizeof(this->address));
92 this->address.ss_family = family;
93 this->SetPort(port);
94 }
95
100 NetworkAddress(const NetworkAddress &address)
101 {
102 memcpy(this, &address, sizeof(*this));
103 }
104
105 const char *GetHostname();
106 void GetAddressAsString(char *buffer, const char *last, bool with_family = true);
107 const char *GetAddressAsString(bool with_family = true);
108 const sockaddr_storage *GetAddress();
109
114 int GetAddressLength()
115 {
116 /* Resolve it if we didn't do it already */
117 if (!this->IsResolved()) this->GetAddress();
118 return this->address_length;
119 }
120
121 uint16 GetPort() const;
122 void SetPort(uint16 port);
123
128 bool IsResolved() const
129 {
130 return this->resolved;
131 }
132
133 bool IsFamily(int family);
134 bool IsInNetmask(char *netmask);
135
141 int CompareTo(NetworkAddress &address)
142 {
143 int r = this->GetAddressLength() - address.GetAddressLength();
144 if (r == 0) r = this->address.ss_family - address.address.ss_family;
145 if (r == 0) r = memcmp(&this->address, &address.address, this->address_length);
146 if (r == 0) r = this->GetPort() - address.GetPort();
147 return r;
148 }
149
155 bool operator == (NetworkAddress &address)
156 {
157 return this->CompareTo(address) == 0;
158 }
159
165 bool operator == (NetworkAddress &address) const
166 {
167 return const_cast<NetworkAddress*>(this)->CompareTo(address) == 0;
168 }
174 bool operator != (NetworkAddress address) const
175 {
176 return const_cast<NetworkAddress*>(this)->CompareTo(address) != 0;
177 }
178
183 bool operator < (NetworkAddress &address)
184 {
185 return this->CompareTo(address) < 0;
186 }
187
188 SOCKET Connect();
189 void Listen(int socktype, SocketList *sockets);
190
191 static const char *SocketTypeAsString(int socktype);
192 static const char *AddressFamilyAsString(int family);
193 };
194
195 #endif /* ENABLE_NETWORK */
196 #endif /* NETWORK_CORE_ADDRESS_H */
NetworkAddress::NetworkAddress
NetworkAddress(const NetworkAddress &address)
Make a clone of another address.
Definition: address.h:100
os_abstraction.h
Network stuff has many things that needs to be included and/or implemented by default.
NetworkAddress::CompareTo
int CompareTo(NetworkAddress &address)
Compare the address of this class with the address of another.
Definition: address.h:141
NetworkAddress::address
sockaddr_storage address
The resolved address.
Definition: address.h:35
NetworkAddress::operator==
bool operator==(NetworkAddress &address)
Compare the address of this class with the address of another.
Definition: address.h:155
SmallMap< NetworkAddress, SOCKET, 4 >
NetworkAddress
Wrapper for (un)resolved network addresses; there&#39;s no reason to transform a numeric IP to a string a...
Definition: address.h:31
NetworkAddress::NetworkAddress
NetworkAddress(struct sockaddr_storage &address, int address_length)
Create a network address based on a resolved IP and port.
Definition: address.h:52
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:50
SmallVector< NetworkAddress, 4 >
config.h
Configuration options of the network stuff.
NetworkAddress::IsFamily
bool IsFamily(int family)
Checks of this address is of the given family.
Definition: address.cpp:147
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:80
SocketList
SmallMap< NetworkAddress, SOCKET, 4 > SocketList
Type for a mapping between address and socket.
Definition: address.h:24
NetworkAddress::GetAddressLength
int GetAddressLength()
Get the (valid) length of the address.
Definition: address.h:114
NetworkAddressList
SmallVector< NetworkAddress, 4 > NetworkAddressList
Type for a list of addresses.
Definition: address.h:22
NetworkAddress::GetHostname
const char * GetHostname()
Get the hostname; in case it wasn&#39;t given the IPv4 dotted representation is given.
Definition: address.cpp:26
NetworkAddress::SocketTypeAsString
static const char * SocketTypeAsString(int socktype)
Convert the socket type into a string.
Definition: address.cpp:412
NETWORK_HOSTNAME_LENGTH
static const uint NETWORK_HOSTNAME_LENGTH
The maximum length of the host name, in bytes including &#39;\0&#39;.
Definition: config.h:44
NetworkAddress::SetPort
void SetPort(uint16 port)
Set the port.
Definition: address.cpp:58
NetworkAddress::Resolve
SOCKET Resolve(int family, int socktype, int flags, SocketList *sockets, LoopProc func)
Resolve this address into a socket.
Definition: address.cpp:224
NetworkAddress::Connect
SOCKET Connect()
Connect to the given address.
Definition: address.cpp:325
StrEmpty
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:59
NetworkAddress::IsInNetmask
bool IsInNetmask(char *netmask)
Checks whether this IP address is contained by the given netmask.
Definition: address.cpp:161
strecpy
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: depend.cpp:68
NetworkAddress::AddressFamilyAsString
static const char * AddressFamilyAsString(int family)
Convert the address family into a string.
Definition: address.cpp:427
NetworkAddress::operator!=
bool operator!=(NetworkAddress address) const
Compare the address of this class with the address of another.
Definition: address.h:174
NetworkAddress::LoopProc
SOCKET(* LoopProc)(addrinfo *runp)
Helper function to resolve something to a socket.
Definition: address.h:43
NetworkAddress::GetPort
uint16 GetPort() const
Get the port.
Definition: address.cpp:39
NetworkAddress::hostname
char hostname[NETWORK_HOSTNAME_LENGTH]
The hostname.
Definition: address.h:33
NetworkAddress::operator<
bool operator<(NetworkAddress &address)
Compare the address of this class with the address of another.
Definition: address.h:183
NetworkAddress::NetworkAddress
NetworkAddress(sockaddr *address, int address_length)
Create a network address based on a resolved IP and port.
Definition: address.h:65
NetworkAddress::resolved
bool resolved
Whether the address has been (tried to be) resolved.
Definition: address.h:36
NetworkAddress::GetAddress
const sockaddr_storage * GetAddress()
Get the address in its internal representation.
Definition: address.cpp:128
NetworkAddress::GetAddressAsString
void GetAddressAsString(char *buffer, const char *last, bool with_family=true)
Get the address as a string, e.g.
Definition: address.cpp:81
NetworkAddress::address_length
int address_length
The length of the resolved address.
Definition: address.h:34
NetworkAddress::IsResolved
bool IsResolved() const
Check whether the IP address has been resolved already.
Definition: address.h:128
NetworkAddress::Listen
void Listen(int socktype, SocketList *sockets)
Make the given socket listen.
Definition: address.cpp:390