123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- #ifndef RTC_BASE_IP_ADDRESS_H_
- #define RTC_BASE_IP_ADDRESS_H_
- #if defined(WEBRTC_POSIX)
- #include <arpa/inet.h>
- #include <netdb.h>
- #include <netinet/in.h>
- #include <sys/socket.h>
- #endif
- #if defined(WEBRTC_WIN)
- #include <winsock2.h>
- #include <ws2tcpip.h>
- #endif
- #include <string.h>
- #include <string>
- #include "rtc_base/byte_order.h"
- #if defined(WEBRTC_WIN)
- #include "rtc_base/win32.h"
- #endif
- #include "rtc_base/system/rtc_export.h"
- namespace rtc {
- enum IPv6AddressFlag {
- IPV6_ADDRESS_FLAG_NONE = 0x00,
-
-
- IPV6_ADDRESS_FLAG_TEMPORARY = 1 << 0,
-
-
-
- IPV6_ADDRESS_FLAG_DEPRECATED = 1 << 1,
- };
- class RTC_EXPORT IPAddress {
- public:
- IPAddress() : family_(AF_UNSPEC) { ::memset(&u_, 0, sizeof(u_)); }
- explicit IPAddress(const in_addr& ip4) : family_(AF_INET) {
- memset(&u_, 0, sizeof(u_));
- u_.ip4 = ip4;
- }
- explicit IPAddress(const in6_addr& ip6) : family_(AF_INET6) { u_.ip6 = ip6; }
- explicit IPAddress(uint32_t ip_in_host_byte_order) : family_(AF_INET) {
- memset(&u_, 0, sizeof(u_));
- u_.ip4.s_addr = HostToNetwork32(ip_in_host_byte_order);
- }
- IPAddress(const IPAddress& other) : family_(other.family_) {
- ::memcpy(&u_, &other.u_, sizeof(u_));
- }
- virtual ~IPAddress() {}
- const IPAddress& operator=(const IPAddress& other) {
- family_ = other.family_;
- ::memcpy(&u_, &other.u_, sizeof(u_));
- return *this;
- }
- bool operator==(const IPAddress& other) const;
- bool operator!=(const IPAddress& other) const;
- bool operator<(const IPAddress& other) const;
- bool operator>(const IPAddress& other) const;
- #ifdef UNIT_TEST
- inline std::ostream& operator<<(
- std::ostream& os) {
- return os << ToString();
- }
- #endif
- int family() const { return family_; }
- in_addr ipv4_address() const;
- in6_addr ipv6_address() const;
-
- size_t Size() const;
-
- std::string ToString() const;
-
- std::string ToSensitiveString() const;
-
-
- IPAddress Normalized() const;
-
-
- IPAddress AsIPv6Address() const;
-
- uint32_t v4AddressAsHostOrderInteger() const;
-
- int overhead() const;
-
- bool IsNil() const;
- private:
- int family_;
- union {
- in_addr ip4;
- in6_addr ip6;
- } u_;
- };
- class RTC_EXPORT InterfaceAddress : public IPAddress {
- public:
- InterfaceAddress() : ipv6_flags_(IPV6_ADDRESS_FLAG_NONE) {}
- explicit InterfaceAddress(IPAddress ip)
- : IPAddress(ip), ipv6_flags_(IPV6_ADDRESS_FLAG_NONE) {}
- InterfaceAddress(IPAddress addr, int ipv6_flags)
- : IPAddress(addr), ipv6_flags_(ipv6_flags) {}
- InterfaceAddress(const in6_addr& ip6, int ipv6_flags)
- : IPAddress(ip6), ipv6_flags_(ipv6_flags) {}
- InterfaceAddress(const InterfaceAddress& other) = default;
- const InterfaceAddress& operator=(const InterfaceAddress& other);
- bool operator==(const InterfaceAddress& other) const;
- bool operator!=(const InterfaceAddress& other) const;
- int ipv6_flags() const { return ipv6_flags_; }
- std::string ToString() const;
- private:
- int ipv6_flags_;
- };
- bool IPFromAddrInfo(struct addrinfo* info, IPAddress* out);
- RTC_EXPORT bool IPFromString(const std::string& str, IPAddress* out);
- RTC_EXPORT bool IPFromString(const std::string& str,
- int flags,
- InterfaceAddress* out);
- bool IPIsAny(const IPAddress& ip);
- bool IPIsLoopback(const IPAddress& ip);
- bool IPIsLinkLocal(const IPAddress& ip);
- bool IPIsPrivateNetwork(const IPAddress& ip);
- bool IPIsSharedNetwork(const IPAddress& ip);
- RTC_EXPORT bool IPIsPrivate(const IPAddress& ip);
- bool IPIsUnspec(const IPAddress& ip);
- size_t HashIP(const IPAddress& ip);
- bool IPIs6Bone(const IPAddress& ip);
- bool IPIs6To4(const IPAddress& ip);
- RTC_EXPORT bool IPIsMacBased(const IPAddress& ip);
- bool IPIsSiteLocal(const IPAddress& ip);
- bool IPIsTeredo(const IPAddress& ip);
- bool IPIsULA(const IPAddress& ip);
- bool IPIsV4Compatibility(const IPAddress& ip);
- bool IPIsV4Mapped(const IPAddress& ip);
- int IPAddressPrecedence(const IPAddress& ip);
- RTC_EXPORT IPAddress TruncateIP(const IPAddress& ip, int length);
- IPAddress GetLoopbackIP(int family);
- IPAddress GetAnyIP(int family);
- int CountIPMaskBits(const IPAddress& mask);
- }
- #endif
|