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; |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 17 | static const Family kAddressFamilyUnknown; |
| 18 | static const Family kAddressFamilyIPv4; |
| 19 | static const Family kAddressFamilyIPv6; |
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); |
| 23 | ~IPAddress(); |
| 24 | |
| 25 | // Static utilities |
| 26 | // Get the length in bytes of addresses of the given family |
| 27 | static int GetAddressLength(Family family); |
| 28 | |
| 29 | // Getters and Setters |
| 30 | Family family() const { return family_; } |
| 31 | const ByteString &address() const { return address_; } |
| 32 | const unsigned char *GetConstData() const { return address_.GetConstData(); } |
| 33 | int GetLength() const { return address_.GetLength(); } |
| 34 | bool IsDefault() const { return address_.IsZero(); } |
| 35 | bool IsValid() const { |
| 36 | return family_ != kAddressFamilyUnknown && |
| 37 | GetLength() == GetAddressLength(family_); |
| 38 | } |
| 39 | |
| 40 | // Parse an IP address string |
| 41 | bool SetAddressFromString(const std::string &address_string); |
| 42 | // An uninitialized IPAddress is empty and invalid when constructed. |
| 43 | // Use SetAddressToDefault() to set it to the default or "all-zeroes" address. |
| 44 | void SetAddressToDefault(); |
| 45 | |
| 46 | bool Equals(const IPAddress &b) const { |
| 47 | return family_ == b.family_ && address_.Equals(b.address_); |
| 48 | } |
| 49 | |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 50 | void Clone(const IPAddress &b) { |
| 51 | family_ = b.family_; |
| 52 | address_ = b.address_; |
| 53 | } |
| 54 | |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 55 | private: |
| 56 | Family family_; |
| 57 | ByteString address_; |
| 58 | DISALLOW_COPY_AND_ASSIGN(IPAddress); |
| 59 | }; |
| 60 | |
| 61 | } // namespace shill |
| 62 | |
| 63 | #endif // SHILL_IP_ADDRESS_ |