Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 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 | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 20 | |
| 21 | explicit IPAddress(Family family); |
| 22 | IPAddress(Family family, const ByteString &address); |
Paul Stewart | 9e3fcd7 | 2011-08-26 15:46:16 -0700 | [diff] [blame] | 23 | IPAddress(Family family, const ByteString &address, unsigned int prefix); |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 24 | ~IPAddress(); |
| 25 | |
Paul Stewart | 9e3fcd7 | 2011-08-26 15:46:16 -0700 | [diff] [blame] | 26 | // Since this is a copyable datatype... |
| 27 | IPAddress(const IPAddress &b) |
| 28 | : family_(b.family_), |
| 29 | address_(b.address_), |
| 30 | prefix_(b.prefix_) {} |
| 31 | IPAddress &operator=(const IPAddress &b) { |
| 32 | family_ = b.family_; |
| 33 | address_ = b.address_; |
| 34 | prefix_ = b.prefix_; |
| 35 | return *this; |
| 36 | } |
| 37 | |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 38 | // Static utilities |
| 39 | // Get the length in bytes of addresses of the given family |
| 40 | static int GetAddressLength(Family family); |
| 41 | |
| 42 | // Getters and Setters |
| 43 | Family family() const { return family_; } |
| 44 | const ByteString &address() const { return address_; } |
Paul Stewart | 9e3fcd7 | 2011-08-26 15:46:16 -0700 | [diff] [blame] | 45 | unsigned int prefix() const { return prefix_; } |
| 46 | void set_prefix(unsigned int prefix) { prefix_ = prefix; } |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 47 | const unsigned char *GetConstData() const { return address_.GetConstData(); } |
| 48 | int GetLength() const { return address_.GetLength(); } |
| 49 | bool IsDefault() const { return address_.IsZero(); } |
| 50 | bool IsValid() const { |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 51 | return family_ != kFamilyUnknown && |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 52 | GetLength() == GetAddressLength(family_); |
| 53 | } |
| 54 | |
| 55 | // Parse an IP address string |
| 56 | bool SetAddressFromString(const std::string &address_string); |
| 57 | // An uninitialized IPAddress is empty and invalid when constructed. |
| 58 | // Use SetAddressToDefault() to set it to the default or "all-zeroes" address. |
| 59 | void SetAddressToDefault(); |
| 60 | |
| 61 | bool Equals(const IPAddress &b) const { |
Paul Stewart | 9e3fcd7 | 2011-08-26 15:46:16 -0700 | [diff] [blame] | 62 | return family_ == b.family_ && address_.Equals(b.address_) && |
| 63 | prefix_ == b.prefix_; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 66 | private: |
| 67 | Family family_; |
| 68 | ByteString address_; |
Paul Stewart | 9e3fcd7 | 2011-08-26 15:46:16 -0700 | [diff] [blame] | 69 | unsigned int prefix_; |
| 70 | // NO DISALLOW_COPY_AND_ASSIGN -- we assign IPAddresses in STL datatypes |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | } // namespace shill |
| 74 | |
| 75 | #endif // SHILL_IP_ADDRESS_ |