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