1f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org/* 2f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org * Copyright 2004 The WebRTC Project Authors. All rights reserved. 3f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org * 4f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org * Use of this source code is governed by a BSD-style license 5f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org * that can be found in the LICENSE file in the root of the source 6f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org * tree. An additional intellectual property rights grant can be found 7f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org * in the file PATENTS. All contributing project authors may 8f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org * be found in the AUTHORS file in the root of the source tree. 9f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org */ 10f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 11f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org#ifndef WEBRTC_BASE_SOCKETADDRESS_H_ 12f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org#define WEBRTC_BASE_SOCKETADDRESS_H_ 13f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 14f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org#include <string> 15f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org#include <vector> 16f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org#include <iosfwd> 17f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org#include "webrtc/base/basictypes.h" 18f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org#include "webrtc/base/ipaddress.h" 19f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 20f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org#undef SetPort 21f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 22f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.orgstruct sockaddr_in; 23f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.orgstruct sockaddr_storage; 24f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 25f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.orgnamespace rtc { 26f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 27f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org// Records an IP address and port. 28f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.orgclass SocketAddress { 29f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org public: 30f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Creates a nil address. 31f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org SocketAddress(); 32f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 33f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Creates the address with the given host and port. Host may be a 34f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // literal IP string or a hostname to be resolved later. 35f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org SocketAddress(const std::string& hostname, int port); 36f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 37f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Creates the address with the given IP and port. 38f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // IP is given as an integer in host byte order. V4 only, to be deprecated. 390c4e06b4c6107a1b94f764e279e4fb4161e905b0Peter Boström SocketAddress(uint32_t ip_as_host_order_integer, int port); 40f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 41f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Creates the address with the given IP and port. 42f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org SocketAddress(const IPAddress& ip, int port); 43f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 44f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Creates a copy of the given address. 45f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org SocketAddress(const SocketAddress& addr); 46f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 47f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Resets to the nil address. 48f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org void Clear(); 49f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 50f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Determines if this is a nil address (empty hostname, any IP, null port) 51f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org bool IsNil() const; 52f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 53f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Returns true if ip and port are set. 54f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org bool IsComplete() const; 55f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 56f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Replaces our address with the given one. 57f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org SocketAddress& operator=(const SocketAddress& addr); 58f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 59f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Changes the IP of this address to the given one, and clears the hostname 60f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // IP is given as an integer in host byte order. V4 only, to be deprecated.. 610c4e06b4c6107a1b94f764e279e4fb4161e905b0Peter Boström void SetIP(uint32_t ip_as_host_order_integer); 62f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 63f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Changes the IP of this address to the given one, and clears the hostname. 64f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org void SetIP(const IPAddress& ip); 65f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 66f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Changes the hostname of this address to the given one. 67f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Does not resolve the address; use Resolve to do so. 68f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org void SetIP(const std::string& hostname); 69f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 70f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Sets the IP address while retaining the hostname. Useful for bypassing 71f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // DNS for a pre-resolved IP. 72f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // IP is given as an integer in host byte order. V4 only, to be deprecated. 730c4e06b4c6107a1b94f764e279e4fb4161e905b0Peter Boström void SetResolvedIP(uint32_t ip_as_host_order_integer); 74f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 75f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Sets the IP address while retaining the hostname. Useful for bypassing 76f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // DNS for a pre-resolved IP. 77f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org void SetResolvedIP(const IPAddress& ip); 78f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 79f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Changes the port of this address to the given one. 80f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org void SetPort(int port); 81f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 82f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Returns the hostname. 83f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org const std::string& hostname() const { return hostname_; } 84f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 85f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Returns the IP address as a host byte order integer. 86f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Returns 0 for non-v4 addresses. 870c4e06b4c6107a1b94f764e279e4fb4161e905b0Peter Boström uint32_t ip() const; 88f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 89f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org const IPAddress& ipaddr() const; 90f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 91f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org int family() const {return ip_.family(); } 92f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 93f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Returns the port part of this address. 940c4e06b4c6107a1b94f764e279e4fb4161e905b0Peter Boström uint16_t port() const; 95f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 96f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Returns the scope ID associated with this address. Scope IDs are a 97f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // necessary addition to IPv6 link-local addresses, with different network 98f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // interfaces having different scope-ids for their link-local addresses. 99f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // IPv4 address do not have scope_ids and sockaddr_in structures do not have 100f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // a field for them. 101f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org int scope_id() const {return scope_id_; } 102f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org void SetScopeID(int id) { scope_id_ = id; } 103f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 104f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Returns the 'host' portion of the address (hostname or IP) in a form 105f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // suitable for use in a URI. If both IP and hostname are present, hostname 106f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // is preferred. IPv6 addresses are enclosed in square brackets ('[' and ']'). 107f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org std::string HostAsURIString() const; 108f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 109f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Same as HostAsURIString but anonymizes IP addresses by hiding the last 110f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // part. 111f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org std::string HostAsSensitiveURIString() const; 112f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 113f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Returns the port as a string. 114f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org std::string PortAsString() const; 115f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 116f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Returns hostname:port or [hostname]:port. 117f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org std::string ToString() const; 118f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 119f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Same as ToString but anonymizes it by hiding the last part. 120f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org std::string ToSensitiveString() const; 121f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 122f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Parses hostname:port and [hostname]:port. 123f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org bool FromString(const std::string& str); 124f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 125f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org friend std::ostream& operator<<(std::ostream& os, const SocketAddress& addr); 126f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 127f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Determines whether this represents a missing / any IP address. 128f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // That is, 0.0.0.0 or ::. 129f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Hostname and/or port may be set. 130f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org bool IsAnyIP() const; 131f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 132f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Determines whether the IP address refers to a loopback address. 133f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // For v4 addresses this means the address is in the range 127.0.0.0/8. 134f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // For v6 addresses this means the address is ::1. 135f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org bool IsLoopbackIP() const; 136f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 137f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Determines whether the IP address is in one of the private ranges: 138f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // For v4: 127.0.0.0/8 10.0.0.0/8 192.168.0.0/16 172.16.0.0/12. 139f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // For v6: FE80::/16 and ::1. 140f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org bool IsPrivateIP() const; 141f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 142f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Determines whether the hostname has been resolved to an IP. 143f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org bool IsUnresolvedIP() const; 144f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 145f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Determines whether this address is identical to the given one. 146f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org bool operator ==(const SocketAddress& addr) const; 147f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org inline bool operator !=(const SocketAddress& addr) const { 148f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org return !this->operator ==(addr); 149f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org } 150f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 151f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Compares based on IP and then port. 152f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org bool operator <(const SocketAddress& addr) const; 153f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 154f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Determines whether this address has the same IP as the one given. 155f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org bool EqualIPs(const SocketAddress& addr) const; 156f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 157f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Determines whether this address has the same port as the one given. 158f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org bool EqualPorts(const SocketAddress& addr) const; 159f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 160f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Hashes this address into a small number. 161f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org size_t Hash() const; 162f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 163f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Write this address to a sockaddr_in. 164f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // If IPv6, will zero out the sockaddr_in and sets family to AF_UNSPEC. 165f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org void ToSockAddr(sockaddr_in* saddr) const; 166f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 167f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Read this address from a sockaddr_in. 168f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org bool FromSockAddr(const sockaddr_in& saddr); 169f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 170f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Read and write the address to/from a sockaddr_storage. 171f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Dual stack version always sets family to AF_INET6, and maps v4 addresses. 172f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // The other version doesn't map, and outputs an AF_INET address for 173f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // v4 or mapped addresses, and AF_INET6 addresses for others. 174f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // Returns the size of the sockaddr_in or sockaddr_in6 structure that is 175f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org // written to the sockaddr_storage, or zero on failure. 176f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org size_t ToDualStackSockAddrStorage(sockaddr_storage* saddr) const; 177f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org size_t ToSockAddrStorage(sockaddr_storage* saddr) const; 178f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 179f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org private: 180f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org std::string hostname_; 181f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org IPAddress ip_; 1820c4e06b4c6107a1b94f764e279e4fb4161e905b0Peter Boström uint16_t port_; 183f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org int scope_id_; 184f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org bool literal_; // Indicates that 'hostname_' contains a literal IP string. 185f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org}; 186f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 187f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.orgbool SocketAddressFromSockAddrStorage(const sockaddr_storage& saddr, 188f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org SocketAddress* out); 189f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.orgSocketAddress EmptySocketAddressWithFamily(int family); 190f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 191f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org} // namespace rtc 192f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org 193f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org#endif // WEBRTC_BASE_SOCKETADDRESS_H_ 194