Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Peter Qiu | 8d6b597 | 2014-10-28 15:33:34 -0700 | [diff] [blame] | 5 | #ifndef SHILL_NET_IP_ADDRESS_H_ |
| 6 | #define SHILL_NET_IP_ADDRESS_H_ |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 7 | |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 8 | #include <string> |
| 9 | |
Peter Qiu | 8d6b597 | 2014-10-28 15:33:34 -0700 | [diff] [blame] | 10 | #include "shill/net/byte_string.h" |
Peter Qiu | ba24e6f | 2014-11-17 10:26:35 -0800 | [diff] [blame] | 11 | #include "shill/shill_export.h" |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 12 | |
| 13 | namespace shill { |
| 14 | |
Peter Qiu | ba24e6f | 2014-11-17 10:26:35 -0800 | [diff] [blame] | 15 | class SHILL_EXPORT IPAddress { |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 16 | public: |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 17 | typedef unsigned char Family; |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 18 | static const Family kFamilyUnknown; |
| 19 | static const Family kFamilyIPv4; |
| 20 | static const Family kFamilyIPv6; |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 21 | static const char kFamilyNameUnknown[]; |
| 22 | static const char kFamilyNameIPv4[]; |
| 23 | static const char kFamilyNameIPv6[]; |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 24 | |
| 25 | explicit IPAddress(Family family); |
Samuel Tan | da775de | 2014-08-12 18:41:31 -0700 | [diff] [blame] | 26 | // Constructs an IPAdress object given a standard string representation of an |
| 27 | // IP address (e.g. "192.144.30.54"). |
| 28 | explicit IPAddress(std::string ip_string); |
Samuel Tan | c863d06 | 2014-08-14 17:52:16 -0700 | [diff] [blame] | 29 | |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 30 | IPAddress(Family family, const ByteString &address); |
Paul Stewart | 9e3fcd7 | 2011-08-26 15:46:16 -0700 | [diff] [blame] | 31 | IPAddress(Family family, const ByteString &address, unsigned int prefix); |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 32 | ~IPAddress(); |
| 33 | |
Paul Stewart | 9e3fcd7 | 2011-08-26 15:46:16 -0700 | [diff] [blame] | 34 | // Since this is a copyable datatype... |
| 35 | IPAddress(const IPAddress &b) |
| 36 | : family_(b.family_), |
| 37 | address_(b.address_), |
| 38 | prefix_(b.prefix_) {} |
| 39 | IPAddress &operator=(const IPAddress &b) { |
| 40 | family_ = b.family_; |
| 41 | address_ = b.address_; |
| 42 | prefix_ = b.prefix_; |
| 43 | return *this; |
| 44 | } |
| 45 | |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 46 | // Static utilities |
| 47 | // Get the length in bytes of addresses of the given family |
Eric Shienbrood | c74cf9c | 2012-03-02 15:00:35 -0500 | [diff] [blame] | 48 | static size_t GetAddressLength(Family family); |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 49 | |
Paul Stewart | 48100b0 | 2012-03-19 07:53:52 -0700 | [diff] [blame] | 50 | // Returns the maximum prefix length for address family |family|, i.e., |
| 51 | // the length of this address type in bits. |
| 52 | static size_t GetMaxPrefixLength(Family family); |
| 53 | |
Paul Stewart | f7bf9bf | 2012-04-17 17:30:14 -0700 | [diff] [blame] | 54 | // Provides a guideline for the minimum sensible prefix for this IP |
| 55 | // address. As opposed to GetMaxPrefixLength() above, this function |
| 56 | // takes into account the class of this IP address to determine the |
| 57 | // smallest prefix that makes sense for this class of address to have. |
| 58 | // Since this function uses classful (pre-CIDR) rules to perform this |
| 59 | // estimate, this is not an absolute rule and others methods like |
| 60 | // IsValid() do not consider this a criteria. It is only useful for |
| 61 | // making guesses as to the mimimal plausible prefix that might be |
| 62 | // viable for an address when the supplied prefix is obviously incorrect. |
| 63 | size_t GetMinPrefixLength() const; |
| 64 | |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 65 | // Returns the prefix length given an address |family| and a |mask|. For |
| 66 | // example, returns 24 for an IPv4 mask 255.255.255.0. |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 67 | static size_t GetPrefixLengthFromMask(Family family, const std::string &mask); |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 68 | |
Paul Stewart | f7bf9bf | 2012-04-17 17:30:14 -0700 | [diff] [blame] | 69 | // Returns an IPAddress of type |family| that has all the high-order |prefix| |
| 70 | // bits set. |
| 71 | static IPAddress GetAddressMaskFromPrefix(Family family, size_t prefix); |
| 72 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 73 | // Returns the name of an address family. |
| 74 | static std::string GetAddressFamilyName(Family family); |
| 75 | |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 76 | // Getters and Setters |
| 77 | Family family() const { return family_; } |
Peter Qiu | f3a8f90 | 2014-08-20 10:05:42 -0700 | [diff] [blame] | 78 | void set_family(Family family) { family_ = family; } |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 79 | const ByteString &address() const { return address_; } |
Paul Stewart | 9e3fcd7 | 2011-08-26 15:46:16 -0700 | [diff] [blame] | 80 | unsigned int prefix() const { return prefix_; } |
| 81 | void set_prefix(unsigned int prefix) { prefix_ = prefix; } |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 82 | const unsigned char *GetConstData() const { return address_.GetConstData(); } |
Eric Shienbrood | c74cf9c | 2012-03-02 15:00:35 -0500 | [diff] [blame] | 83 | size_t GetLength() const { return address_.GetLength(); } |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 84 | bool IsDefault() const { return address_.IsZero(); } |
| 85 | bool IsValid() const { |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 86 | return family_ != kFamilyUnknown && |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 87 | GetLength() == GetAddressLength(family_); |
| 88 | } |
| 89 | |
Paul Stewart | f7bf9bf | 2012-04-17 17:30:14 -0700 | [diff] [blame] | 90 | // Parse an IP address string. |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 91 | bool SetAddressFromString(const std::string &address_string); |
Paul Stewart | f7bf9bf | 2012-04-17 17:30:14 -0700 | [diff] [blame] | 92 | // Parse an "address/prefix" IP address and prefix pair from a string. |
| 93 | bool SetAddressAndPrefixFromString(const std::string &address_string); |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 94 | // An uninitialized IPAddress is empty and invalid when constructed. |
| 95 | // Use SetAddressToDefault() to set it to the default or "all-zeroes" address. |
| 96 | void SetAddressToDefault(); |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 97 | // Return the string equivalent of the address. Returns true if the |
| 98 | // conversion succeeds in which case |address_string| is set to the |
| 99 | // result. Otherwise the function returns false and |address_string| |
| 100 | // is left unmodified. |
mukesh agrawal | 2c15d2c | 2012-02-21 16:09:21 -0800 | [diff] [blame] | 101 | bool IntoString(std::string *address_string) const; |
| 102 | // Similar to IntoString, but returns by value. Convenient for logging. |
| 103 | std::string ToString() const; |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 104 | |
| 105 | bool Equals(const IPAddress &b) const { |
Paul Stewart | 9e3fcd7 | 2011-08-26 15:46:16 -0700 | [diff] [blame] | 106 | return family_ == b.family_ && address_.Equals(b.address_) && |
| 107 | prefix_ == b.prefix_; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Paul Stewart | f7bf9bf | 2012-04-17 17:30:14 -0700 | [diff] [blame] | 110 | // Perform an AND operation between the address data of |this| and that |
| 111 | // of |b|. Returns an IPAddress containing the result of the operation. |
| 112 | // It is an error if |this| and |b| are not of the same address family |
| 113 | // or if either are not valid, |
Paul Stewart | 4a6748d | 2012-07-17 14:31:36 -0700 | [diff] [blame] | 114 | IPAddress MaskWith(const IPAddress &b) const; |
Paul Stewart | f7bf9bf | 2012-04-17 17:30:14 -0700 | [diff] [blame] | 115 | |
Paul Stewart | fe1c0e1 | 2012-04-30 19:57:04 -0700 | [diff] [blame] | 116 | // Perform an OR operation between the address data of |this| and that |
| 117 | // of |b|. Returns an IPAddress containing the result of the operation. |
| 118 | // It is an error if |this| and |b| are not of the same address family |
| 119 | // or if either are not valid, |
Paul Stewart | 4a6748d | 2012-07-17 14:31:36 -0700 | [diff] [blame] | 120 | IPAddress MergeWith(const IPAddress &b) const; |
Paul Stewart | fe1c0e1 | 2012-04-30 19:57:04 -0700 | [diff] [blame] | 121 | |
Paul Stewart | f7bf9bf | 2012-04-17 17:30:14 -0700 | [diff] [blame] | 122 | // Return an address that represents the network-part of the address, |
| 123 | // i.e, the address with all but the prefix bits masked out. |
Paul Stewart | 4a6748d | 2012-07-17 14:31:36 -0700 | [diff] [blame] | 124 | IPAddress GetNetworkPart() const; |
Paul Stewart | f7bf9bf | 2012-04-17 17:30:14 -0700 | [diff] [blame] | 125 | |
Paul Stewart | fe1c0e1 | 2012-04-30 19:57:04 -0700 | [diff] [blame] | 126 | // Return the default broadcast address for the IP address, by setting |
| 127 | // all of the host-part bits to 1. |
| 128 | IPAddress GetDefaultBroadcast(); |
| 129 | |
Paul Stewart | f7bf9bf | 2012-04-17 17:30:14 -0700 | [diff] [blame] | 130 | // Tests whether this IPAddress is able to directly access the address |
| 131 | // |b| without an intervening gateway. It tests whether the network |
| 132 | // part of |b| is the same as the network part of |this|, using the |
| 133 | // prefix of |this|. Returns true if |b| is reachable, false otherwise. |
Paul Stewart | 4a6748d | 2012-07-17 14:31:36 -0700 | [diff] [blame] | 134 | bool CanReachAddress(const IPAddress &b) const; |
Paul Stewart | f7bf9bf | 2012-04-17 17:30:14 -0700 | [diff] [blame] | 135 | |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 136 | private: |
| 137 | Family family_; |
| 138 | ByteString address_; |
Paul Stewart | 9e3fcd7 | 2011-08-26 15:46:16 -0700 | [diff] [blame] | 139 | unsigned int prefix_; |
| 140 | // NO DISALLOW_COPY_AND_ASSIGN -- we assign IPAddresses in STL datatypes |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | } // namespace shill |
| 144 | |
Peter Qiu | 8d6b597 | 2014-10-28 15:33:34 -0700 | [diff] [blame] | 145 | #endif // SHILL_NET_IP_ADDRESS_H_ |