OpenTTD
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 
19 typedef std::vector<NetworkAddress> NetworkAddressList;
21 
28 private:
31  sockaddr_storage address;
32  bool resolved;
33 
39  typedef SOCKET (*LoopProc)(addrinfo *runp);
40 
41  SOCKET Resolve(int family, int socktype, int flags, SocketList *sockets, LoopProc func);
42 public:
48  NetworkAddress(struct sockaddr_storage &address, int address_length) :
49  address_length(address_length),
50  address(address),
51  resolved(address_length != 0)
52  {
53  *this->hostname = '\0';
54  }
55 
61  NetworkAddress(sockaddr *address, int address_length) :
62  address_length(address_length),
63  resolved(address_length != 0)
64  {
65  *this->hostname = '\0';
66  memset(&this->address, 0, sizeof(this->address));
67  memcpy(&this->address, address, address_length);
68  }
69 
76  NetworkAddress(const char *hostname = "", uint16 port = 0, int family = AF_UNSPEC) :
77  address_length(0),
78  resolved(false)
79  {
80  /* Also handle IPv6 bracket enclosed hostnames */
81  if (StrEmpty(hostname)) hostname = "";
82  if (*hostname == '[') hostname++;
83  strecpy(this->hostname, StrEmpty(hostname) ? "" : hostname, lastof(this->hostname));
84  char *tmp = strrchr(this->hostname, ']');
85  if (tmp != nullptr) *tmp = '\0';
86 
87  memset(&this->address, 0, sizeof(this->address));
88  this->address.ss_family = family;
89  this->SetPort(port);
90  }
91 
92  const char *GetHostname();
93  void GetAddressAsString(char *buffer, const char *last, bool with_family = true);
94  const char *GetAddressAsString(bool with_family = true);
95  const sockaddr_storage *GetAddress();
96 
102  {
103  /* Resolve it if we didn't do it already */
104  if (!this->IsResolved()) this->GetAddress();
105  return this->address_length;
106  }
107 
108  uint16 GetPort() const;
109  void SetPort(uint16 port);
110 
115  bool IsResolved() const
116  {
117  return this->resolved;
118  }
119 
120  bool IsFamily(int family);
121  bool IsInNetmask(const char *netmask);
122 
128  int CompareTo(NetworkAddress &address)
129  {
130  int r = this->GetAddressLength() - address.GetAddressLength();
131  if (r == 0) r = this->address.ss_family - address.address.ss_family;
132  if (r == 0) r = memcmp(&this->address, &address.address, this->address_length);
133  if (r == 0) r = this->GetPort() - address.GetPort();
134  return r;
135  }
136 
143  {
144  return this->CompareTo(address) == 0;
145  }
146 
152  bool operator == (NetworkAddress &address) const
153  {
154  return const_cast<NetworkAddress*>(this)->CompareTo(address) == 0;
155  }
161  bool operator != (NetworkAddress address) const
162  {
163  return const_cast<NetworkAddress*>(this)->CompareTo(address) != 0;
164  }
165 
171  {
172  return this->CompareTo(address) < 0;
173  }
174 
175  SOCKET Connect();
176  void Listen(int socktype, SocketList *sockets);
177 
178  static const char *SocketTypeAsString(int socktype);
179  static const char *AddressFamilyAsString(int family);
180 };
181 
182 #endif /* NETWORK_CORE_ADDRESS_H */
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:128
sockaddr_storage address
The resolved address.
Definition: address.h:31
bool operator==(NetworkAddress &address)
Compare the address of this class with the address of another.
Definition: address.h:142
SmallMap< NetworkAddress, SOCKET > SocketList
Type for a mapping between address and socket.
Definition: address.h:20
Wrapper for (un)resolved network addresses; there&#39;s no reason to transform a numeric IP to a string a...
Definition: address.h:27
NetworkAddress(struct sockaddr_storage &address, int address_length)
Create a network address based on a resolved IP and port.
Definition: address.h:48
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:48
Configuration options of the network stuff.
bool IsFamily(int family)
Checks of this address is of the given family.
Definition: address.cpp:143
std::vector< NetworkAddress > NetworkAddressList
Type for a list of addresses.
Definition: address.h:18
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:76
int GetAddressLength()
Get the (valid) length of the address.
Definition: address.h:101
const char * GetHostname()
Get the hostname; in case it wasn&#39;t given the IPv4 dotted representation is given.
Definition: address.cpp:22
static const char * SocketTypeAsString(int socktype)
Convert the socket type into a string.
Definition: address.cpp:407
static const uint NETWORK_HOSTNAME_LENGTH
The maximum length of the host name, in bytes including &#39;\0&#39;.
Definition: config.h:42
bool IsInNetmask(const char *netmask)
Checks whether this IP address is contained by the given netmask.
Definition: address.cpp:157
void SetPort(uint16 port)
Set the port.
Definition: address.cpp:54
SOCKET Resolve(int family, int socktype, int flags, SocketList *sockets, LoopProc func)
Resolve this address into a socket.
Definition: address.cpp:219
SOCKET Connect()
Connect to the given address.
Definition: address.cpp:320
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:57
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: depend.cpp:66
static const char * AddressFamilyAsString(int family)
Convert the address family into a string.
Definition: address.cpp:422
bool operator!=(NetworkAddress address) const
Compare the address of this class with the address of another.
Definition: address.h:161
SOCKET(* LoopProc)(addrinfo *runp)
Helper function to resolve something to a socket.
Definition: address.h:39
uint16 GetPort() const
Get the port.
Definition: address.cpp:35
char hostname[NETWORK_HOSTNAME_LENGTH]
The hostname.
Definition: address.h:29
bool operator<(NetworkAddress &address)
Compare the address of this class with the address of another.
Definition: address.h:170
NetworkAddress(sockaddr *address, int address_length)
Create a network address based on a resolved IP and port.
Definition: address.h:61
bool resolved
Whether the address has been (tried to be) resolved.
Definition: address.h:32
const sockaddr_storage * GetAddress()
Get the address in its internal representation.
Definition: address.cpp:124
void GetAddressAsString(char *buffer, const char *last, bool with_family=true)
Get the address as a string, e.g.
Definition: address.cpp:77
int address_length
The length of the resolved address.
Definition: address.h:30
bool IsResolved() const
Check whether the IP address has been resolved already.
Definition: address.h:115
void Listen(int socktype, SocketList *sockets)
Make the given socket listen.
Definition: address.cpp:385