Home | History | Annotate | Download | only in base
      1 /*
      2  * libjingle
      3  * Copyright 2004--2005, Google Inc.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are met:
      7  *
      8  *  1. Redistributions of source code must retain the above copyright notice,
      9  *     this list of conditions and the following disclaimer.
     10  *  2. Redistributions in binary form must reproduce the above copyright notice,
     11  *     this list of conditions and the following disclaimer in the documentation
     12  *     and/or other materials provided with the distribution.
     13  *  3. The name of the author may not be used to endorse or promote products
     14  *     derived from this software without specific prior written permission.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
     19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #ifndef TALK_BASE_SOCKETADDRESS_H_
     29 #define TALK_BASE_SOCKETADDRESS_H_
     30 
     31 #include <string>
     32 #include <vector>
     33 #include <iosfwd>
     34 #include "talk/base/basictypes.h"
     35 #include "talk/base/ipaddress.h"
     36 
     37 #undef SetPort
     38 
     39 struct sockaddr_in;
     40 struct sockaddr_storage;
     41 
     42 namespace talk_base {
     43 
     44 // Records an IP address and port.
     45 class SocketAddress {
     46  public:
     47   // Creates a nil address.
     48   SocketAddress();
     49 
     50   // Creates the address with the given host and port. Host may be a
     51   // literal IP string or a hostname to be resolved later.
     52   SocketAddress(const std::string& hostname, int port);
     53 
     54   // Creates the address with the given IP and port.
     55   // IP is given as an integer in host byte order. V4 only, to be deprecated.
     56   SocketAddress(uint32 ip_as_host_order_integer, int port);
     57 
     58   // Creates the address with the given IP and port.
     59   SocketAddress(const IPAddress& ip, int port);
     60 
     61   // Creates a copy of the given address.
     62   SocketAddress(const SocketAddress& addr);
     63 
     64   // Resets to the nil address.
     65   void Clear();
     66 
     67   // Determines if this is a nil address (empty hostname, any IP, null port)
     68   bool IsNil() const;
     69 
     70   // Returns true if ip and port are set.
     71   bool IsComplete() const;
     72 
     73   // Replaces our address with the given one.
     74   SocketAddress& operator=(const SocketAddress& addr);
     75 
     76   // Changes the IP of this address to the given one, and clears the hostname
     77   // IP is given as an integer in host byte order. V4 only, to be deprecated..
     78   void SetIP(uint32 ip_as_host_order_integer);
     79 
     80   // Changes the IP of this address to the given one, and clears the hostname.
     81   void SetIP(const IPAddress& ip);
     82 
     83   // Changes the hostname of this address to the given one.
     84   // Does not resolve the address; use Resolve to do so.
     85   void SetIP(const std::string& hostname);
     86 
     87   // Sets the IP address while retaining the hostname.  Useful for bypassing
     88   // DNS for a pre-resolved IP.
     89   // IP is given as an integer in host byte order. V4 only, to be deprecated.
     90   void SetResolvedIP(uint32 ip_as_host_order_integer);
     91 
     92   // Sets the IP address while retaining the hostname.  Useful for bypassing
     93   // DNS for a pre-resolved IP.
     94   void SetResolvedIP(const IPAddress& ip);
     95 
     96   // Changes the port of this address to the given one.
     97   void SetPort(int port);
     98 
     99   // Returns the hostname.
    100   const std::string& hostname() const { return hostname_; }
    101 
    102   // Returns the IP address as a host byte order integer.
    103   // Returns 0 for non-v4 addresses.
    104   uint32 ip() const;
    105 
    106   const IPAddress& ipaddr() const;
    107 
    108   int family() const {return ip_.family(); }
    109 
    110   // Returns the port part of this address.
    111   uint16 port() const;
    112 
    113   // Returns the scope ID associated with this address. Scope IDs are a
    114   // necessary addition to IPv6 link-local addresses, with different network
    115   // interfaces having different scope-ids for their link-local addresses.
    116   // IPv4 address do not have scope_ids and sockaddr_in structures do not have
    117   // a field for them.
    118   int scope_id() const {return scope_id_; }
    119   void SetScopeID(int id) { scope_id_ = id; }
    120 
    121   // Returns the 'host' portion of the address (hostname or IP) in a form
    122   // suitable for use in a URI. If both IP and hostname are present, hostname
    123   // is preferred. IPv6 addresses are enclosed in square brackets ('[' and ']').
    124   std::string HostAsURIString() const;
    125 
    126   // Same as HostAsURIString but anonymizes IP addresses by hiding the last
    127   // part.
    128   std::string HostAsSensitiveURIString() const;
    129 
    130   // Returns the port as a string.
    131   std::string PortAsString() const;
    132 
    133   // Returns hostname:port or [hostname]:port.
    134   std::string ToString() const;
    135 
    136   // Same as ToString but anonymizes it by hiding the last part.
    137   std::string ToSensitiveString() const;
    138 
    139   // Parses hostname:port and [hostname]:port.
    140   bool FromString(const std::string& str);
    141 
    142   friend std::ostream& operator<<(std::ostream& os, const SocketAddress& addr);
    143 
    144   // Determines whether this represents a missing / any IP address.
    145   // That is, 0.0.0.0 or ::.
    146   // Hostname and/or port may be set.
    147   bool IsAnyIP() const;
    148   inline bool IsAny() const { return IsAnyIP(); }  // deprecated
    149 
    150   // Determines whether the IP address refers to a loopback address.
    151   // For v4 addresses this means the address is in the range 127.0.0.0/8.
    152   // For v6 addresses this means the address is ::1.
    153   bool IsLoopbackIP() const;
    154 
    155   // Determines whether the IP address is in one of the private ranges:
    156   // For v4: 127.0.0.0/8 10.0.0.0/8 192.168.0.0/16 172.16.0.0/12.
    157   // For v6: FE80::/16 and ::1.
    158   bool IsPrivateIP() const;
    159 
    160   // Determines whether the hostname has been resolved to an IP.
    161   bool IsUnresolvedIP() const;
    162   inline bool IsUnresolved() const { return IsUnresolvedIP(); }  // deprecated
    163 
    164   // Determines whether this address is identical to the given one.
    165   bool operator ==(const SocketAddress& addr) const;
    166   inline bool operator !=(const SocketAddress& addr) const {
    167     return !this->operator ==(addr);
    168   }
    169 
    170   // Compares based on IP and then port.
    171   bool operator <(const SocketAddress& addr) const;
    172 
    173   // Determines whether this address has the same IP as the one given.
    174   bool EqualIPs(const SocketAddress& addr) const;
    175 
    176   // Determines whether this address has the same port as the one given.
    177   bool EqualPorts(const SocketAddress& addr) const;
    178 
    179   // Hashes this address into a small number.
    180   size_t Hash() const;
    181 
    182   // Write this address to a sockaddr_in.
    183   // If IPv6, will zero out the sockaddr_in and sets family to AF_UNSPEC.
    184   void ToSockAddr(sockaddr_in* saddr) const;
    185 
    186   // Read this address from a sockaddr_in.
    187   bool FromSockAddr(const sockaddr_in& saddr);
    188 
    189   // Read and write the address to/from a sockaddr_storage.
    190   // Dual stack version always sets family to AF_INET6, and maps v4 addresses.
    191   // The other version doesn't map, and outputs an AF_INET address for
    192   // v4 or mapped addresses, and AF_INET6 addresses for others.
    193   // Returns the size of the sockaddr_in or sockaddr_in6 structure that is
    194   // written to the sockaddr_storage, or zero on failure.
    195   size_t ToDualStackSockAddrStorage(sockaddr_storage* saddr) const;
    196   size_t ToSockAddrStorage(sockaddr_storage* saddr) const;
    197 
    198   // Converts the IP address given in 'compact form' into dotted form.
    199   // IP is given as an integer in host byte order. V4 only, to be deprecated.
    200   // TODO: Deprecate this.
    201   static std::string IPToString(uint32 ip_as_host_order_integer);
    202 
    203   // Same as IPToString but anonymizes it by hiding the last part.
    204   // TODO: Deprecate this.
    205   static std::string IPToSensitiveString(uint32 ip_as_host_order_integer);
    206 
    207   // Converts the IP address given in dotted form into compact form.
    208   // Only dotted names (A.B.C.D) are  converted.
    209   // Output integer is returned in host byte order.
    210   // TODO: Deprecate, replace wth agnostic versions.
    211   static bool StringToIP(const std::string& str, uint32* ip);
    212   static uint32 StringToIP(const std::string& str);
    213 
    214   // Converts the IP address given in printable form into an IPAddress.
    215   static bool StringToIP(const std::string& str, IPAddress* ip);
    216 
    217  private:
    218   std::string hostname_;
    219   IPAddress ip_;
    220   uint16 port_;
    221   int scope_id_;
    222   bool literal_;  // Indicates that 'hostname_' contains a literal IP string.
    223 };
    224 
    225 bool SocketAddressFromSockAddrStorage(const sockaddr_storage& saddr,
    226                                       SocketAddress* out);
    227 SocketAddress EmptySocketAddressWithFamily(int family);
    228 
    229 }  // namespace talk_base
    230 
    231 #endif  // TALK_BASE_SOCKETADDRESS_H_
    232