Line data Source code
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/platform.h" 11 : #include "envoy/common/pure.h" 12 : 13 : #include "absl/numeric/int128.h" 14 : #include "absl/strings/string_view.h" 15 : 16 : namespace Envoy { 17 : namespace Network { 18 : 19 : /* Forward declaration */ 20 : class SocketInterface; 21 : 22 : namespace Address { 23 : 24 : class Instance; 25 : using InstanceConstSharedPtr = std::shared_ptr<const Instance>; 26 : 27 : /** 28 : * Interface for an Ipv4 address. 29 : */ 30 : class Ipv4 { 31 : public: 32 198177 : virtual ~Ipv4() = default; 33 : 34 : /** 35 : * @return the 32-bit IPv4 address in network byte order. 36 : */ 37 : virtual uint32_t address() const PURE; 38 : }; 39 : 40 : /** 41 : * Interface for an Ipv6 address. 42 : */ 43 : class Ipv6 { 44 : public: 45 1080 : virtual ~Ipv6() = default; 46 : 47 : /** 48 : * @return the absl::uint128 IPv6 address in network byte order. 49 : */ 50 : virtual absl::uint128 address() const PURE; 51 : 52 : /** 53 : * @return the uint32_t scope/zone identifier of the IPv6 address. 54 : */ 55 : virtual uint32_t scopeId() const PURE; 56 : 57 : /** 58 : * @return true if address is Ipv6 and Ipv4 compatibility is disabled, false otherwise 59 : */ 60 : virtual bool v6only() const PURE; 61 : 62 : /** 63 : * @return Ipv4 address from Ipv4-compatible Ipv6 address. Return `nullptr` 64 : * if the Ipv6 address isn't Ipv4 mapped. 65 : */ 66 : virtual InstanceConstSharedPtr v4CompatibleAddress() const PURE; 67 : 68 : /** 69 : * @return Ipv6 address that has no scope/zone identifier. Return `nullptr` 70 : * if the conversion failed. 71 : */ 72 : virtual InstanceConstSharedPtr addressWithoutScopeId() const PURE; 73 : }; 74 : 75 : enum class IpVersion { v4, v6 }; // NOLINT(readability-identifier-naming) 76 : 77 : /** 78 : * Interface for a generic IP address. 79 : */ 80 : class Ip { 81 : public: 82 199257 : virtual ~Ip() = default; 83 : 84 : /** 85 : * @return the address as a string. E.g., "1.2.3.4" for an IPv4 address. 86 : */ 87 : virtual const std::string& addressAsString() const PURE; 88 : 89 : /** 90 : * @return whether this address is wild card, i.e., '0.0.0.0'. 91 : */ 92 : virtual bool isAnyAddress() const PURE; 93 : 94 : /** 95 : * @return whether this address is a valid unicast address, i.e., not an wild card, broadcast, or 96 : * multicast address. 97 : */ 98 : virtual bool isUnicastAddress() const PURE; 99 : 100 : /** 101 : * @return Ipv4 address data IFF version() == IpVersion::v4, otherwise nullptr. 102 : */ 103 : virtual const Ipv4* ipv4() const PURE; 104 : 105 : /** 106 : * @return Ipv6 address data IFF version() == IpVersion::v6, otherwise nullptr. 107 : */ 108 : virtual const Ipv6* ipv6() const PURE; 109 : 110 : /** 111 : * @return the port associated with the address. Port may be zero if not specified, not 112 : * determinable before socket creation, or not applicable. 113 : * The port is in host byte order. 114 : */ 115 : virtual uint32_t port() const PURE; 116 : 117 : /** 118 : * @return the version of IP address. 119 : */ 120 : virtual IpVersion version() const PURE; 121 : }; 122 : 123 : /** 124 : * Interface for a generic Pipe address. 125 : */ 126 : class Pipe { 127 : public: 128 738 : virtual ~Pipe() = default; 129 : /** 130 : * @return abstract namespace flag. 131 : */ 132 : virtual bool abstractNamespace() const PURE; 133 : 134 : /** 135 : * @return pipe mode. 136 : */ 137 : virtual mode_t mode() const PURE; 138 : }; 139 : 140 : /** 141 : * Interface for a generic internal address. 142 : */ 143 : class EnvoyInternalAddress { 144 : public: 145 4 : virtual ~EnvoyInternalAddress() = default; 146 : 147 : /** 148 : * @return The unique id of the internal address. If the address represents the destination 149 : * internal listener, the address id is that listener name. 150 : */ 151 : virtual const std::string& addressId() const PURE; 152 : 153 : /** 154 : * @return The optional endpoint id of the internal address. 155 : */ 156 : virtual const std::string& endpointId() const PURE; 157 : }; 158 : 159 : enum class Type { Ip, Pipe, EnvoyInternal }; 160 : 161 : /** 162 : * Interface for all network addresses. 163 : */ 164 : class Instance { 165 : public: 166 200000 : virtual ~Instance() = default; 167 : 168 : virtual bool operator==(const Instance& rhs) const PURE; 169 0 : bool operator!=(const Instance& rhs) const { return !operator==(rhs); } 170 : 171 : /** 172 : * @return a human readable string for the address that represents the 173 : * physical/resolved address. (This will not necessarily include port 174 : * information, if applicable, since that may not be resolved until bind()). 175 : * 176 : * This string will be compatible with the following example formats: 177 : * For IPv4 addresses: "1.2.3.4:80" 178 : * For IPv6 addresses: "[1234:5678::9]:443" 179 : * For pipe addresses: "/foo" 180 : */ 181 : virtual const std::string& asString() const PURE; 182 : 183 : /** 184 : * @return Similar to asString but returns a string view. 185 : */ 186 : virtual absl::string_view asStringView() const PURE; 187 : 188 : /** 189 : * @return a human readable string for the address that represents the 190 : * logical/unresolved name. 191 : * 192 : * This string has a source-dependent format and should preserve the original 193 : * name for Address::Instances resolved by a Network::Address::Resolver. 194 : */ 195 : virtual const std::string& logicalName() const PURE; 196 : 197 : /** 198 : * @return the IP address information IFF type() == Type::Ip, otherwise nullptr. 199 : */ 200 : virtual const Ip* ip() const PURE; 201 : 202 : /** 203 : * @return the pipe address information IFF type() == Type::Pipe, otherwise nullptr. 204 : */ 205 : virtual const Pipe* pipe() const PURE; 206 : 207 : /** 208 : * @return the envoy internal address information IFF type() == 209 : * Type::EnvoyInternal, otherwise nullptr. 210 : */ 211 : virtual const EnvoyInternalAddress* envoyInternalAddress() const PURE; 212 : 213 : /** 214 : * @return the underlying structure wherein the address is stored. Return nullptr if the address 215 : * type is internal address. 216 : */ 217 : virtual const sockaddr* sockAddr() const PURE; 218 : 219 : /** 220 : * @return length of the address container. 221 : */ 222 : virtual socklen_t sockAddrLen() const PURE; 223 : 224 : /** 225 : * @return the type of address. 226 : */ 227 : virtual Type type() const PURE; 228 : 229 : /** 230 : * Return the address type in string_view. The returned type name is used to find the 231 : * ClientConnectionFactory. 232 : */ 233 : virtual absl::string_view addressType() const PURE; 234 : 235 : /** 236 : * @return SocketInterface to be used with the address. 237 : */ 238 : virtual const Network::SocketInterface& socketInterface() const PURE; 239 : }; 240 : 241 : } // namespace Address 242 : } // namespace Network 243 : } // namespace Envoy