OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

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 */