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 | |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 49 | // Returns the prefix length given an address |family| and a |mask|. For |
| 50 | // example, returns 24 for an IPv4 mask 255.255.255.0. |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 51 | static size_t GetPrefixLengthFromMask(Family family, const std::string &mask); |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 52 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 53 | // Returns the name of an address family. |
| 54 | static std::string GetAddressFamilyName(Family family); |
| 55 | |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 56 | // Getters and Setters |
| 57 | Family family() const { return family_; } |
| 58 | const ByteString &address() const { return address_; } |
Paul Stewart | 9e3fcd7 | 2011-08-26 15:46:16 -0700 | [diff] [blame] | 59 | unsigned int prefix() const { return prefix_; } |
| 60 | void set_prefix(unsigned int prefix) { prefix_ = prefix; } |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 61 | const unsigned char *GetConstData() const { return address_.GetConstData(); } |
Eric Shienbrood | c74cf9c | 2012-03-02 15:00:35 -0500 | [diff] [blame] | 62 | size_t GetLength() const { return address_.GetLength(); } |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 63 | bool IsDefault() const { return address_.IsZero(); } |
| 64 | bool IsValid() const { |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 65 | return family_ != kFamilyUnknown && |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 66 | GetLength() == GetAddressLength(family_); |
| 67 | } |
| 68 | |
| 69 | // Parse an IP address string |
| 70 | bool SetAddressFromString(const std::string &address_string); |
| 71 | // An uninitialized IPAddress is empty and invalid when constructed. |
| 72 | // Use SetAddressToDefault() to set it to the default or "all-zeroes" address. |
| 73 | void SetAddressToDefault(); |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 74 | // Return the string equivalent of the address. Returns true if the |
| 75 | // conversion succeeds in which case |address_string| is set to the |
| 76 | // result. Otherwise the function returns false and |address_string| |
| 77 | // is left unmodified. |
mukesh agrawal | 2c15d2c | 2012-02-21 16:09:21 -0800 | [diff] [blame] | 78 | bool IntoString(std::string *address_string) const; |
| 79 | // Similar to IntoString, but returns by value. Convenient for logging. |
| 80 | std::string ToString() const; |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 81 | |
| 82 | bool Equals(const IPAddress &b) const { |
Paul Stewart | 9e3fcd7 | 2011-08-26 15:46:16 -0700 | [diff] [blame] | 83 | return family_ == b.family_ && address_.Equals(b.address_) && |
| 84 | prefix_ == b.prefix_; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 87 | private: |
| 88 | Family family_; |
| 89 | ByteString address_; |
Paul Stewart | 9e3fcd7 | 2011-08-26 15:46:16 -0700 | [diff] [blame] | 90 | unsigned int prefix_; |
| 91 | // NO DISALLOW_COPY_AND_ASSIGN -- we assign IPAddresses in STL datatypes |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | } // namespace shill |
| 95 | |
| 96 | #endif // SHILL_IP_ADDRESS_ |