You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

220 lines
8.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #ifndef TALK_BASE_SOCKETADDRESS_H_
  28. #define TALK_BASE_SOCKETADDRESS_H_
  29. #include <string>
  30. #include <vector>
  31. #include <iosfwd>
  32. #include "talk/base/basictypes.h"
  33. #include "talk/base/ipaddress.h"
  34. #undef SetPort
  35. struct sockaddr_in;
  36. struct sockaddr_storage;
  37. namespace talk_base {
  38. // Records an IP address and port.
  39. class SocketAddress {
  40. public:
  41. // Creates a nil address.
  42. SocketAddress();
  43. // Creates the address with the given host and port. Host may be a
  44. // literal IP string or a hostname to be resolved later.
  45. SocketAddress(const std::string& hostname, int port);
  46. // Creates the address with the given IP and port.
  47. // IP is given as an integer in host byte order. V4 only, to be deprecated.
  48. SocketAddress(uint32 ip_as_host_order_integer, int port);
  49. // Creates the address with the given IP and port.
  50. SocketAddress(const IPAddress& ip, int port);
  51. // Creates a copy of the given address.
  52. SocketAddress(const SocketAddress& addr);
  53. // Resets to the nil address.
  54. void Clear();
  55. // Determines if this is a nil address (empty hostname, any IP, null port)
  56. bool IsNil() const;
  57. // Returns true if ip and port are set.
  58. bool IsComplete() const;
  59. // Replaces our address with the given one.
  60. SocketAddress& operator=(const SocketAddress& addr);
  61. // Changes the IP of this address to the given one, and clears the hostname
  62. // IP is given as an integer in host byte order. V4 only, to be deprecated..
  63. void SetIP(uint32 ip_as_host_order_integer);
  64. // Changes the IP of this address to the given one, and clears the hostname.
  65. void SetIP(const IPAddress& ip);
  66. // Changes the hostname of this address to the given one.
  67. // Does not resolve the address; use Resolve to do so.
  68. void SetIP(const std::string& hostname);
  69. // Sets the IP address while retaining the hostname. Useful for bypassing
  70. // DNS for a pre-resolved IP.
  71. // IP is given as an integer in host byte order. V4 only, to be deprecated.
  72. void SetResolvedIP(uint32 ip_as_host_order_integer);
  73. // Sets the IP address while retaining the hostname. Useful for bypassing
  74. // DNS for a pre-resolved IP.
  75. void SetResolvedIP(const IPAddress& ip);
  76. // Changes the port of this address to the given one.
  77. void SetPort(int port);
  78. // Returns the hostname.
  79. const std::string& hostname() const { return hostname_; }
  80. // Returns the IP address as a host byte order integer.
  81. // Returns 0 for non-v4 addresses.
  82. uint32 ip() const;
  83. const IPAddress& ipaddr() const;
  84. int family() const {return ip_.family(); }
  85. // Returns the port part of this address.
  86. uint16 port() const;
  87. // Returns the scope ID associated with this address. Scope IDs are a
  88. // necessary addition to IPv6 link-local addresses, with different network
  89. // interfaces having different scope-ids for their link-local addresses.
  90. // IPv4 address do not have scope_ids and sockaddr_in structures do not have
  91. // a field for them.
  92. int scope_id() const {return scope_id_; }
  93. void SetScopeID(int id) { scope_id_ = id; }
  94. // Returns the 'host' portion of the address (hostname or IP) in a form
  95. // suitable for use in a URI. If both IP and hostname are present, hostname
  96. // is preferred. IPv6 addresses are enclosed in square brackets ('[' and ']').
  97. std::string HostAsURIString() const;
  98. // Returns the port as a string.
  99. std::string PortAsString() const;
  100. // Returns hostname:port or [hostname]:port.
  101. std::string ToString() const;
  102. // Parses hostname:port and [hostname]:port.
  103. bool FromString(const std::string& str);
  104. friend std::ostream& operator<<(std::ostream& os, const SocketAddress& addr);
  105. // Determines whether this represents a missing / any IP address.
  106. // That is, 0.0.0.0 or ::.
  107. // Hostname and/or port may be set.
  108. bool IsAnyIP() const;
  109. inline bool IsAny() const { return IsAnyIP(); } // deprecated
  110. // Determines whether the IP address refers to a loopback address.
  111. // For v4 addresses this means the address is in the range 127.0.0.0/8.
  112. // For v6 addresses this means the address is ::1.
  113. bool IsLoopbackIP() const;
  114. // Determines whether the IP address is in one of the private ranges:
  115. // For v4: 127.0.0.0/8 10.0.0.0/8 192.168.0.0/16 172.16.0.0/12.
  116. // For v6: FE80::/16 and ::1.
  117. bool IsPrivateIP() const;
  118. // Determines whether the hostname has been resolved to an IP.
  119. bool IsUnresolvedIP() const;
  120. inline bool IsUnresolved() const { return IsUnresolvedIP(); } // deprecated
  121. // Determines whether this address is identical to the given one.
  122. bool operator ==(const SocketAddress& addr) const;
  123. inline bool operator !=(const SocketAddress& addr) const {
  124. return !this->operator ==(addr);
  125. }
  126. // Compares based on IP and then port.
  127. bool operator <(const SocketAddress& addr) const;
  128. // Determines whether this address has the same IP as the one given.
  129. bool EqualIPs(const SocketAddress& addr) const;
  130. // Determines whether this address has the same port as the one given.
  131. bool EqualPorts(const SocketAddress& addr) const;
  132. // Hashes this address into a small number.
  133. size_t Hash() const;
  134. // Write this address to a sockaddr_in.
  135. // If IPv6, will zero out the sockaddr_in and sets family to AF_UNSPEC.
  136. void ToSockAddr(sockaddr_in* saddr) const;
  137. // Read this address from a sockaddr_in.
  138. bool FromSockAddr(const sockaddr_in& saddr);
  139. // Read and write the address to/from a sockaddr_storage.
  140. // Dual stack version always sets family to AF_INET6, and maps v4 addresses.
  141. // The other version doesn't map, and outputs an AF_INET address for
  142. // v4 or mapped addresses, and AF_INET6 addresses for others.
  143. // Returns the size of the sockaddr_in or sockaddr_in6 structure that is
  144. // written to the sockaddr_storage, or zero on failure.
  145. size_t ToDualStackSockAddrStorage(sockaddr_storage* saddr) const;
  146. size_t ToSockAddrStorage(sockaddr_storage* saddr) const;
  147. // Converts the IP address given in 'compact form' into dotted form.
  148. // IP is given as an integer in host byte order. V4 only, to be deprecated.
  149. // TODO: Deprecate this.
  150. static std::string IPToString(uint32 ip_as_host_order_integer);
  151. // Converts the IP address given in dotted form into compact form.
  152. // Only dotted names (A.B.C.D) are converted.
  153. // Output integer is returned in host byte order.
  154. // TODO: Deprecate, replace wth agnostic versions.
  155. static bool StringToIP(const std::string& str, uint32* ip);
  156. static uint32 StringToIP(const std::string& str);
  157. // Converts the IP address given in printable form into an IPAddress.
  158. static bool StringToIP(const std::string& str, IPAddress* ip);
  159. private:
  160. std::string hostname_;
  161. IPAddress ip_;
  162. uint16 port_;
  163. int scope_id_;
  164. bool literal_; // Indicates that 'hostname_' contains a literal IP string.
  165. };
  166. bool SocketAddressFromSockAddrStorage(const sockaddr_storage& saddr,
  167. SocketAddress* out);
  168. SocketAddress EmptySocketAddressWithFamily(int family);
  169. } // namespace talk_base
  170. #endif // TALK_BASE_SOCKETADDRESS_H_