1
#pragma once
2

            
3
#include <sys/types.h>
4

            
5
#include <array>
6
#include <cstdint>
7
#include <memory>
8
#include <string>
9

            
10
#include "envoy/common/optref.h"
11
#include "envoy/common/platform.h"
12
#include "envoy/common/pure.h"
13
#include "envoy/stream_info/filter_state.h"
14

            
15
#include "absl/numeric/int128.h"
16
#include "absl/strings/string_view.h"
17

            
18
namespace Envoy {
19
namespace Network {
20

            
21
/* Forward declaration */
22
class SocketInterface;
23

            
24
namespace Address {
25

            
26
class Instance;
27
using InstanceConstSharedPtr = std::shared_ptr<const Instance>;
28
using InstanceConstOptRef = OptRef<const Instance>;
29

            
30
/**
31
 * Interface for an Ipv4 address.
32
 */
33
class Ipv4 {
34
public:
35
3554163
  virtual ~Ipv4() = default;
36

            
37
  /**
38
   * @return the 32-bit IPv4 address in network byte order.
39
   */
40
  virtual uint32_t address() const PURE;
41
};
42

            
43
/**
44
 * Interface for an Ipv6 address.
45
 */
46
class Ipv6 {
47
public:
48
114289
  virtual ~Ipv6() = default;
49

            
50
  /**
51
   * @return the absl::uint128 IPv6 address in network byte order.
52
   */
53
  virtual absl::uint128 address() const PURE;
54

            
55
  /**
56
   * @return the uint32_t scope/zone identifier of the IPv6 address.
57
   */
58
  virtual uint32_t scopeId() const PURE;
59

            
60
  /**
61
   * @return true if address is Ipv6 and Ipv4 compatibility is disabled, false otherwise
62
   */
63
  virtual bool v6only() const PURE;
64

            
65
  /**
66
   * @return Ipv4 address from Ipv4-compatible Ipv6 address. Return `nullptr`
67
   * if the Ipv6 address isn't Ipv4 mapped.
68
   */
69
  virtual InstanceConstSharedPtr v4CompatibleAddress() const PURE;
70

            
71
  /**
72
   * @return Ipv6 address that has no scope/zone identifier. Return `nullptr`
73
   * if the conversion failed.
74
   */
75
  virtual InstanceConstSharedPtr addressWithoutScopeId() const PURE;
76
};
77

            
78
enum class IpVersion { v4, v6 }; // NOLINT(readability-identifier-naming)
79

            
80
/**
81
 * Interface for a generic IP address.
82
 */
83
class Ip {
84
public:
85
3668585
  virtual ~Ip() = default;
86

            
87
  /**
88
   * @return the address as a string. E.g., "1.2.3.4" for an IPv4 address.
89
   */
90
  virtual const std::string& addressAsString() const PURE;
91

            
92
  /**
93
   * @return whether this address is wild card, i.e., '0.0.0.0'.
94
   */
95
  virtual bool isAnyAddress() const PURE;
96

            
97
  /**
98
   * @return whether this address is a valid unicast address, i.e., not an wild card, broadcast, or
99
   * multicast address.
100
   */
101
  virtual bool isUnicastAddress() const PURE;
102

            
103
  /**
104
   * Determines whether the address is a link-local address. For IPv6, the prefix is fe80::/10. For
105
   * IPv4, the prefix is 169.254.0.0/16.
106
   *
107
   * See https://datatracker.ietf.org/doc/html/rfc3513#section-2.4 for details.
108
   *
109
   * @return true if the address is a link-local address, false otherwise.
110
   */
111
  virtual bool isLinkLocalAddress() const PURE;
112

            
113
  /**
114
   * Determines whether the address is a Unique Local Address. Applies to IPv6 addresses only, where
115
   * the prefix is fc00::/7.
116
   *
117
   * See https://datatracker.ietf.org/doc/html/rfc4193 for details.
118
   *
119
   * @return true if the address is a Unique Local Address, false otherwise.
120
   */
121
  virtual bool isUniqueLocalAddress() const PURE;
122

            
123
  /**
124
   * Determines whether the address is a Site-Local Address. Applies to IPv6 addresses only, where
125
   * the prefix is fec0::/10.
126
   *
127
   * See https://datatracker.ietf.org/doc/html/rfc3513#section-2.4 for details.
128
   *
129
   * @return true if the address is a Site-Local Address, false otherwise.
130
   */
131
  virtual bool isSiteLocalAddress() const PURE;
132

            
133
  /**
134
   * Determines whether the address is a Teredo address. Applies to IPv6 addresses only, where the
135
   * prefix is 2001:0000::/32.
136
   *
137
   * See https://datatracker.ietf.org/doc/html/rfc4380 for details.
138
   *
139
   * @return true if the address is a Teredo address, false otherwise.
140
   */
141
  virtual bool isTeredoAddress() const PURE;
142

            
143
  /**
144
   * @return Ipv4 address data IFF version() == IpVersion::v4, otherwise nullptr.
145
   */
146
  virtual const Ipv4* ipv4() const PURE;
147

            
148
  /**
149
   * @return Ipv6 address data IFF version() == IpVersion::v6, otherwise nullptr.
150
   */
151
  virtual const Ipv6* ipv6() const PURE;
152

            
153
  /**
154
   * @return the port associated with the address. Port may be zero if not specified, not
155
   * determinable before socket creation, or not applicable.
156
   * The port is in host byte order.
157
   */
158
  virtual uint32_t port() const PURE;
159

            
160
  /**
161
   * @return the version of IP address.
162
   */
163
  virtual IpVersion version() const PURE;
164
};
165

            
166
/**
167
 * Interface for a generic Pipe address.
168
 */
169
class Pipe {
170
public:
171
655
  virtual ~Pipe() = default;
172
  /**
173
   * @return abstract namespace flag.
174
   */
175
  virtual bool abstractNamespace() const PURE;
176

            
177
  /**
178
   * @return pipe mode.
179
   */
180
  virtual mode_t mode() const PURE;
181
};
182

            
183
/**
184
 * Interface for a generic internal address.
185
 */
186
class EnvoyInternalAddress {
187
public:
188
189
  virtual ~EnvoyInternalAddress() = default;
189

            
190
  /**
191
   * @return The unique id of the internal address. If the address represents the destination
192
   * internal listener, the address id is that listener name.
193
   */
194
  virtual const std::string& addressId() const PURE;
195

            
196
  /**
197
   * @return The optional endpoint id of the internal address.
198
   */
199
  virtual const std::string& endpointId() const PURE;
200
};
201

            
202
enum class Type { Ip, Pipe, EnvoyInternal };
203

            
204
/**
205
 * Interface for all network addresses.
206
 */
207
class Instance {
208
public:
209
3669463
  virtual ~Instance() = default;
210

            
211
  virtual bool operator==(const Instance& rhs) const PURE;
212
370
  bool operator!=(const Instance& rhs) const { return !operator==(rhs); }
213

            
214
  /**
215
   * @return a human readable string for the address that represents the
216
   * physical/resolved address. (This will not necessarily include port
217
   * information, if applicable, since that may not be resolved until bind()).
218
   *
219
   * This string will be compatible with the following example formats:
220
   * For IPv4 addresses: "1.2.3.4:80"
221
   * For IPv6 addresses: "[1234:5678::9]:443"
222
   * For pipe addresses: "/foo"
223
   */
224
  virtual const std::string& asString() const PURE;
225

            
226
  /**
227
   * @return Similar to asString but returns a string view.
228
   */
229
  virtual absl::string_view asStringView() const PURE;
230

            
231
  /**
232
   * @return a human readable string for the address that represents the
233
   * logical/unresolved name.
234
   *
235
   * This string has a source-dependent format and should preserve the original
236
   * name for Address::Instances resolved by a Network::Address::Resolver.
237
   */
238
  virtual const std::string& logicalName() const PURE;
239

            
240
  /**
241
   * @return the IP address information IFF type() == Type::Ip, otherwise nullptr.
242
   */
243
  virtual const Ip* ip() const PURE;
244

            
245
  /**
246
   * @return the pipe address information IFF type() == Type::Pipe, otherwise nullptr.
247
   */
248
  virtual const Pipe* pipe() const PURE;
249

            
250
  /**
251
   * @return the envoy internal address information IFF type() ==
252
   * Type::EnvoyInternal, otherwise nullptr.
253
   */
254
  virtual const EnvoyInternalAddress* envoyInternalAddress() const PURE;
255

            
256
  /**
257
   * @return the underlying structure wherein the address is stored. Return nullptr if the address
258
   * type is internal address.
259
   */
260
  virtual const sockaddr* sockAddr() const PURE;
261

            
262
  /**
263
   * @return length of the address container.
264
   */
265
  virtual socklen_t sockAddrLen() const PURE;
266

            
267
  /**
268
   * @return the type of address.
269
   */
270
  virtual Type type() const PURE;
271

            
272
  /**
273
   * Return the address type in string_view. The returned type name is used to find the
274
   * ClientConnectionFactory.
275
   */
276
  virtual absl::string_view addressType() const PURE;
277

            
278
  /**
279
   * @return SocketInterface to be used with the address.
280
   */
281
  virtual const Network::SocketInterface& socketInterface() const PURE;
282

            
283
  /**
284
   * @return filepath of the network namespace for the address.
285
   */
286
  virtual absl::optional<std::string> networkNamespace() const PURE;
287

            
288
  /**
289
   * @return a copy of the address with the linux network namespace overridden for IPv4/v6
290
   * addresses, or nullptr if the address does not support network namespaces. An empty string
291
   * argument clears the network namespace.
292
   */
293
  virtual InstanceConstSharedPtr
294
  withNetworkNamespace(absl::string_view network_namespace) const PURE;
295
};
296

            
297
/*
298
 * Used to store Instance in filter state.
299
 */
300
class InstanceAccessor : public Envoy::StreamInfo::FilterState::Object {
301
public:
302
40
  InstanceAccessor(InstanceConstSharedPtr ip) : ip_(std::move(ip)) {}
303

            
304
42
  InstanceConstOptRef getIp() const { return makeOptRefFromPtr<const Instance>(ip_.get()); }
305
16
  InstanceConstSharedPtr getAddress() const { return ip_; }
306

            
307
private:
308
  InstanceConstSharedPtr ip_;
309
};
310

            
311
} // namespace Address
312
} // namespace Network
313
} // namespace Envoy