socket_address.h 7.3 KB

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