blob: f6013af4d4b3e073cc89db5d30c689db64b1593d [file] [log] [blame]
// Copyright (c) 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#if defined(_WIN32)
#include <winsock2.h>
#include <ws2tcpip.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <string>
#include "net/third_party/quiche/src/quic/platform/api/quic_export.h"
#include "net/quic/platform/impl/quic_ip_address_impl.h"
namespace quic {
class QUIC_EXPORT_PRIVATE QuicIpAddress {
// A class representing an IPv4 or IPv6 address in QUIC. The actual
// implementation (platform dependent) of an IP address is in
// QuicIpAddressImpl.
enum : size_t {
kIPv4AddressSize = QuicIpAddressImpl::kIPv4AddressSize,
kIPv6AddressSize = QuicIpAddressImpl::kIPv6AddressSize
// TODO(fayang): Remove Loopback*() and use TestLoopback*() in tests.
static QuicIpAddress Loopback4();
static QuicIpAddress Loopback6();
static QuicIpAddress Any4();
static QuicIpAddress Any6();
QuicIpAddress() = default;
QuicIpAddress(const QuicIpAddress& other) = default;
explicit QuicIpAddress(const QuicIpAddressImpl& impl);
explicit QuicIpAddress(const in_addr& ipv4_address);
explicit QuicIpAddress(const in6_addr& ipv6_address);
QuicIpAddress& operator=(const QuicIpAddress& other) = default;
QuicIpAddress& operator=(QuicIpAddress&& other) = default;
QUIC_EXPORT_PRIVATE friend bool operator==(QuicIpAddress lhs,
QuicIpAddress rhs);
QUIC_EXPORT_PRIVATE friend bool operator!=(QuicIpAddress lhs,
QuicIpAddress rhs);
bool IsInitialized() const;
IpAddressFamily address_family() const;
int AddressFamilyToInt() const;
// Returns the address as a sequence of bytes in network-byte-order. IPv4 will
// be 4 bytes. IPv6 will be 16 bytes.
std::string ToPackedString() const;
// Returns string representation of the address.
std::string ToString() const;
// Normalizes the address representation with respect to IPv4 addresses, i.e,
// mapped IPv4 addresses ("::ffff:X.Y.Z.Q") are converted to pure IPv4
// addresses. All other IPv4, IPv6, and empty values are left unchanged.
QuicIpAddress Normalized() const;
// Returns an address suitable for use in IPv6-aware contexts. This is the
// opposite of NormalizeIPAddress() above. IPv4 addresses are converted into
// their IPv4-mapped address equivalents (e.g. becomes
// ::ffff: IPv6 addresses are a noop (they are returned
// unchanged).
QuicIpAddress DualStacked() const;
bool FromPackedString(const char* data, size_t length);
bool FromString(std::string str);
bool IsIPv4() const;
bool IsIPv6() const;
bool InSameSubnet(const QuicIpAddress& other, int subnet_length);
const QuicIpAddressImpl& impl() const { return impl_; }
QuicIpAddressImpl impl_;
} // namespace quic