blob: d6f013916f4f5dcdef9659d767edc23f54f41525 [file] [log] [blame]
Paul Stewart1d18e8c2011-07-15 11:00:31 -07001// 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 Stewart1d18e8c2011-07-15 11:00:31 -07008#include <string>
9
10#include "shill/byte_string.h"
11
12namespace shill {
13
14class IPAddress {
15 public:
Paul Stewartdd7df792011-07-15 11:09:50 -070016 typedef unsigned char Family;
Paul Stewart7355ce12011-09-02 10:47:01 -070017 static const Family kFamilyUnknown;
18 static const Family kFamilyIPv4;
19 static const Family kFamilyIPv6;
Paul Stewart1d18e8c2011-07-15 11:00:31 -070020
21 explicit IPAddress(Family family);
22 IPAddress(Family family, const ByteString &address);
Paul Stewart9e3fcd72011-08-26 15:46:16 -070023 IPAddress(Family family, const ByteString &address, unsigned int prefix);
Paul Stewart1d18e8c2011-07-15 11:00:31 -070024 ~IPAddress();
25
Paul Stewart9e3fcd72011-08-26 15:46:16 -070026 // 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 Stewart1d18e8c2011-07-15 11:00:31 -070038 // 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 Stewart9e3fcd72011-08-26 15:46:16 -070045 unsigned int prefix() const { return prefix_; }
46 void set_prefix(unsigned int prefix) { prefix_ = prefix; }
Paul Stewart1d18e8c2011-07-15 11:00:31 -070047 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 Stewart7355ce12011-09-02 10:47:01 -070051 return family_ != kFamilyUnknown &&
Paul Stewart1d18e8c2011-07-15 11:00:31 -070052 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 Stewart9e3fcd72011-08-26 15:46:16 -070062 return family_ == b.family_ && address_.Equals(b.address_) &&
63 prefix_ == b.prefix_;
Paul Stewart75e89d22011-08-01 10:00:02 -070064 }
65
Paul Stewart1d18e8c2011-07-15 11:00:31 -070066 private:
67 Family family_;
68 ByteString address_;
Paul Stewart9e3fcd72011-08-26 15:46:16 -070069 unsigned int prefix_;
70 // NO DISALLOW_COPY_AND_ASSIGN -- we assign IPAddresses in STL datatypes
Paul Stewart1d18e8c2011-07-15 11:00:31 -070071};
72
73} // namespace shill
74
75#endif // SHILL_IP_ADDRESS_