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.

149 lines
4.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2011, 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_IPADDRESS_H_
  28. #define TALK_BASE_IPADDRESS_H_
  29. #ifdef POSIX
  30. #include <netinet/in.h>
  31. #include <sys/socket.h>
  32. #include <arpa/inet.h>
  33. #include <netdb.h>
  34. #endif
  35. #ifdef WIN32
  36. #include <winsock2.h>
  37. #include <ws2tcpip.h>
  38. #endif
  39. #include <string.h>
  40. #include <string>
  41. #include <vector>
  42. #include "talk/base/basictypes.h"
  43. #include "talk/base/byteorder.h"
  44. #ifdef WIN32
  45. #include "talk/base/win32.h"
  46. #endif
  47. namespace talk_base {
  48. // Version-agnostic IP address class, wraps a union of in_addr and in6_addr.
  49. class IPAddress {
  50. public:
  51. IPAddress() : family_(AF_UNSPEC) {
  52. ::memset(&u_, 0, sizeof(u_));
  53. }
  54. explicit IPAddress(const in_addr &ip4) : family_(AF_INET) {
  55. memset(&u_, 0, sizeof(u_));
  56. u_.ip4 = ip4;
  57. }
  58. explicit IPAddress(const in6_addr &ip6) : family_(AF_INET6) {
  59. u_.ip6 = ip6;
  60. }
  61. explicit IPAddress(uint32 ip_in_host_byte_order) : family_(AF_INET) {
  62. memset(&u_, 0, sizeof(u_));
  63. u_.ip4.s_addr = HostToNetwork32(ip_in_host_byte_order);
  64. }
  65. IPAddress(const IPAddress &other) : family_(other.family_) {
  66. ::memcpy(&u_, &other.u_, sizeof(u_));
  67. }
  68. ~IPAddress() {}
  69. const IPAddress & operator=(const IPAddress &other) {
  70. family_ = other.family_;
  71. ::memcpy(&u_, &other.u_, sizeof(u_));
  72. return *this;
  73. }
  74. bool operator==(const IPAddress &other) const;
  75. bool operator!=(const IPAddress &other) const;
  76. bool operator <(const IPAddress &other) const;
  77. bool operator >(const IPAddress &other) const;
  78. friend std::ostream& operator<<(std::ostream& os, const IPAddress& addr);
  79. int family() const { return family_; }
  80. in_addr ipv4_address() const;
  81. in6_addr ipv6_address() const;
  82. // Returns the number of bytes needed to store the raw address.
  83. size_t Size() const;
  84. // Wraps inet_ntop.
  85. std::string ToString() const;
  86. // Returns an unmapped address from a possibly-mapped address.
  87. // Returns the same address if this isn't a mapped address.
  88. IPAddress Normalized() const;
  89. // Returns this address as an IPv6 address.
  90. // Maps v4 addresses (as ::ffff:a.b.c.d), returns v6 addresses unchanged.
  91. IPAddress AsIPv6Address() const;
  92. // For socketaddress' benefit. Returns the IP in host byte order.
  93. uint32 v4AddressAsHostOrderInteger() const;
  94. private:
  95. int family_;
  96. union {
  97. in_addr ip4;
  98. in6_addr ip6;
  99. } u_;
  100. };
  101. bool IPFromAddrInfo(struct addrinfo* info, IPAddress* out);
  102. bool IPFromString(const std::string& str, IPAddress* out);
  103. bool IPIsAny(const IPAddress& ip);
  104. bool IPIsLoopback(const IPAddress& ip);
  105. bool IPIsPrivate(const IPAddress& ip);
  106. bool IPIsUnspec(const IPAddress& ip);
  107. size_t HashIP(const IPAddress& ip);
  108. // These are only really applicable for IPv6 addresses.
  109. bool IPIs6Bone(const IPAddress& ip);
  110. bool IPIs6To4(const IPAddress& ip);
  111. bool IPIsSiteLocal(const IPAddress& ip);
  112. bool IPIsTeredo(const IPAddress& ip);
  113. bool IPIsULA(const IPAddress& ip);
  114. bool IPIsV4Compatibility(const IPAddress& ip);
  115. bool IPIsV4Mapped(const IPAddress& ip);
  116. // Returns the precedence value for this IP as given in RFC3484.
  117. int IPAddressPrecedence(const IPAddress& ip);
  118. // Returns 'ip' truncated to be 'length' bits long.
  119. IPAddress TruncateIP(const IPAddress& ip, int length);
  120. // Returns the number of contiguously set bits, counting from the MSB in network
  121. // byte order, in this IPAddress. Bits after the first 0 encountered are not
  122. // counted.
  123. int CountIPMaskBits(IPAddress mask);
  124. } // namespace talk_base
  125. #endif // TALK_BASE_IPADDRESS_H_