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 | |
| 8 | #include <arpa/inet.h> |
| 9 | |
| 10 | #include <string> |
| 11 | |
| 12 | #include "shill/byte_string.h" |
| 13 | |
| 14 | namespace shill { |
| 15 | |
| 16 | class IPAddress { |
| 17 | public: |
| 18 | enum Family { |
| 19 | kAddressFamilyUnknown, |
| 20 | kAddressFamilyIPv4 = AF_INET, |
| 21 | kAddressFamilyIPv6 = AF_INET6 |
| 22 | }; |
| 23 | |
| 24 | explicit IPAddress(Family family); |
| 25 | IPAddress(Family family, const ByteString &address); |
| 26 | ~IPAddress(); |
| 27 | |
| 28 | // Static utilities |
| 29 | // Get the length in bytes of addresses of the given family |
| 30 | static int GetAddressLength(Family family); |
| 31 | |
| 32 | // Getters and Setters |
| 33 | Family family() const { return family_; } |
| 34 | const ByteString &address() const { return address_; } |
| 35 | const unsigned char *GetConstData() const { return address_.GetConstData(); } |
| 36 | int GetLength() const { return address_.GetLength(); } |
| 37 | bool IsDefault() const { return address_.IsZero(); } |
| 38 | bool IsValid() const { |
| 39 | return family_ != kAddressFamilyUnknown && |
| 40 | GetLength() == GetAddressLength(family_); |
| 41 | } |
| 42 | |
| 43 | // Parse an IP address string |
| 44 | bool SetAddressFromString(const std::string &address_string); |
| 45 | // An uninitialized IPAddress is empty and invalid when constructed. |
| 46 | // Use SetAddressToDefault() to set it to the default or "all-zeroes" address. |
| 47 | void SetAddressToDefault(); |
| 48 | |
| 49 | bool Equals(const IPAddress &b) const { |
| 50 | return family_ == b.family_ && address_.Equals(b.address_); |
| 51 | } |
| 52 | |
| 53 | private: |
| 54 | Family family_; |
| 55 | ByteString address_; |
| 56 | DISALLOW_COPY_AND_ASSIGN(IPAddress); |
| 57 | }; |
| 58 | |
| 59 | } // namespace shill |
| 60 | |
| 61 | #endif // SHILL_IP_ADDRESS_ |