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 
25 
32 private:
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 
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 
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 
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 
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(const NetworkAddress &address)
Make a clone of another address.
Definition: address.h:100
Network stuff has many things that needs to be included and/or implemented by default.
int CompareTo(NetworkAddress &address)
Compare the address of this class with the address of another.
Definition: address.h:141
sockaddr_storage address
The resolved address.
Definition: address.h:35
bool operator==(NetworkAddress &address)
Compare the address of this class with the address of another.
Definition: address.h:155
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(struct sockaddr_storage &address, int address_length)
Create a network address based on a resolved IP and port.
Definition: address.h:52
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:50
Configuration options of the network stuff.
bool IsFamily(int family)
Checks of this address is of the given family.
Definition: address.cpp:147
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
SmallMap< NetworkAddress, SOCKET, 4 > SocketList
Type for a mapping between address and socket.
Definition: address.h:24
int GetAddressLength()
Get the (valid) length of the address.
Definition: address.h:114
SmallVector< NetworkAddress, 4 > NetworkAddressList
Type for a list of addresses.
Definition: address.h:22
const char * GetHostname()
Get the hostname; in case it wasn&#39;t given the IPv4 dotted representation is given.
Definition: address.cpp:26
static const char * SocketTypeAsString(int socktype)
Convert the socket type into a string.
Definition: address.cpp:412
static const uint NETWORK_HOSTNAME_LENGTH
The maximum length of the host name, in bytes including &#39;\0&#39;.
Definition: config.h:44
void SetPort(uint16 port)
Set the port.
Definition: address.cpp:58
SOCKET Resolve(int family, int socktype, int flags, SocketList *sockets, LoopProc func)
Resolve this address into a socket.
Definition: address.cpp:224
SOCKET Connect()
Connect to the given address.
Definition: address.cpp:325
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:59
bool IsInNetmask(char *netmask)
Checks whether this IP address is contained by the given netmask.
Definition: address.cpp:161
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: depend.cpp:68
static const char * AddressFamilyAsString(int family)
Convert the address family into a string.
Definition: address.cpp:427
bool operator!=(NetworkAddress address) const
Compare the address of this class with the address of another.
Definition: address.h:174
SOCKET(* LoopProc)(addrinfo *runp)
Helper function to resolve something to a socket.
Definition: address.h:43
uint16 GetPort() const
Get the port.
Definition: address.cpp:39
char hostname[NETWORK_HOSTNAME_LENGTH]
The hostname.
Definition: address.h:33
bool operator<(NetworkAddress &address)
Compare the address of this class with the address of another.
Definition: address.h:183
NetworkAddress(sockaddr *address, int address_length)
Create a network address based on a resolved IP and port.
Definition: address.h:65
bool resolved
Whether the address has been (tried to be) resolved.
Definition: address.h:36
const sockaddr_storage * GetAddress()
Get the address in its internal representation.
Definition: address.cpp:128
void GetAddressAsString(char *buffer, const char *last, bool with_family=true)
Get the address as a string, e.g.
Definition: address.cpp:81
int address_length
The length of the resolved address.
Definition: address.h:34
bool IsResolved() const
Check whether the IP address has been resolved already.
Definition: address.h:128
void Listen(int socktype, SocketList *sockets)
Make the given socket listen.
Definition: address.cpp:390