blob: 81881f70f7ab6bbd3edd9a54c5cccb8ab27c5089 [file] [log] [blame]
Paul Stewart188a84a2012-01-20 16:28:15 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Paul Stewart1d18e8c2011-07-15 11:00:31 -07002// 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 Stewartf748a362012-03-07 12:01:20 -080020 static const char kFamilyNameUnknown[];
21 static const char kFamilyNameIPv4[];
22 static const char kFamilyNameIPv6[];
Paul Stewart1d18e8c2011-07-15 11:00:31 -070023
24 explicit IPAddress(Family family);
25 IPAddress(Family family, const ByteString &address);
Paul Stewart9e3fcd72011-08-26 15:46:16 -070026 IPAddress(Family family, const ByteString &address, unsigned int prefix);
Paul Stewart1d18e8c2011-07-15 11:00:31 -070027 ~IPAddress();
28
Paul Stewart9e3fcd72011-08-26 15:46:16 -070029 // 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 Stewart1d18e8c2011-07-15 11:00:31 -070041 // Static utilities
42 // Get the length in bytes of addresses of the given family
Eric Shienbroodc74cf9c2012-03-02 15:00:35 -050043 static size_t GetAddressLength(Family family);
Paul Stewart1d18e8c2011-07-15 11:00:31 -070044
Paul Stewart48100b02012-03-19 07:53:52 -070045 // 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
Paul Stewartf7bf9bf2012-04-17 17:30:14 -070049 // Provides a guideline for the minimum sensible prefix for this IP
50 // address. As opposed to GetMaxPrefixLength() above, this function
51 // takes into account the class of this IP address to determine the
52 // smallest prefix that makes sense for this class of address to have.
53 // Since this function uses classful (pre-CIDR) rules to perform this
54 // estimate, this is not an absolute rule and others methods like
55 // IsValid() do not consider this a criteria. It is only useful for
56 // making guesses as to the mimimal plausible prefix that might be
57 // viable for an address when the supplied prefix is obviously incorrect.
58 size_t GetMinPrefixLength() const;
59
Darin Petkov14c29ec2012-03-02 11:34:19 +010060 // Returns the prefix length given an address |family| and a |mask|. For
61 // example, returns 24 for an IPv4 mask 255.255.255.0.
Eric Shienbrood3e20a232012-02-16 11:35:56 -050062 static size_t GetPrefixLengthFromMask(Family family, const std::string &mask);
Darin Petkov14c29ec2012-03-02 11:34:19 +010063
Paul Stewartf7bf9bf2012-04-17 17:30:14 -070064 // Returns an IPAddress of type |family| that has all the high-order |prefix|
65 // bits set.
66 static IPAddress GetAddressMaskFromPrefix(Family family, size_t prefix);
67
Paul Stewartf748a362012-03-07 12:01:20 -080068 // Returns the name of an address family.
69 static std::string GetAddressFamilyName(Family family);
70
Paul Stewart1d18e8c2011-07-15 11:00:31 -070071 // Getters and Setters
72 Family family() const { return family_; }
73 const ByteString &address() const { return address_; }
Paul Stewart9e3fcd72011-08-26 15:46:16 -070074 unsigned int prefix() const { return prefix_; }
75 void set_prefix(unsigned int prefix) { prefix_ = prefix; }
Paul Stewart1d18e8c2011-07-15 11:00:31 -070076 const unsigned char *GetConstData() const { return address_.GetConstData(); }
Eric Shienbroodc74cf9c2012-03-02 15:00:35 -050077 size_t GetLength() const { return address_.GetLength(); }
Paul Stewart1d18e8c2011-07-15 11:00:31 -070078 bool IsDefault() const { return address_.IsZero(); }
79 bool IsValid() const {
Paul Stewart7355ce12011-09-02 10:47:01 -070080 return family_ != kFamilyUnknown &&
Paul Stewart1d18e8c2011-07-15 11:00:31 -070081 GetLength() == GetAddressLength(family_);
82 }
83
Paul Stewartf7bf9bf2012-04-17 17:30:14 -070084 // Parse an IP address string.
Paul Stewart1d18e8c2011-07-15 11:00:31 -070085 bool SetAddressFromString(const std::string &address_string);
Paul Stewartf7bf9bf2012-04-17 17:30:14 -070086 // Parse an "address/prefix" IP address and prefix pair from a string.
87 bool SetAddressAndPrefixFromString(const std::string &address_string);
Paul Stewart1d18e8c2011-07-15 11:00:31 -070088 // An uninitialized IPAddress is empty and invalid when constructed.
89 // Use SetAddressToDefault() to set it to the default or "all-zeroes" address.
90 void SetAddressToDefault();
Paul Stewart188a84a2012-01-20 16:28:15 -080091 // Return the string equivalent of the address. Returns true if the
92 // conversion succeeds in which case |address_string| is set to the
93 // result. Otherwise the function returns false and |address_string|
94 // is left unmodified.
mukesh agrawal2c15d2c2012-02-21 16:09:21 -080095 bool IntoString(std::string *address_string) const;
96 // Similar to IntoString, but returns by value. Convenient for logging.
97 std::string ToString() const;
Paul Stewart1d18e8c2011-07-15 11:00:31 -070098
99 bool Equals(const IPAddress &b) const {
Paul Stewart9e3fcd72011-08-26 15:46:16 -0700100 return family_ == b.family_ && address_.Equals(b.address_) &&
101 prefix_ == b.prefix_;
Paul Stewart75e89d22011-08-01 10:00:02 -0700102 }
103
Paul Stewartf7bf9bf2012-04-17 17:30:14 -0700104 // Perform an AND operation between the address data of |this| and that
105 // of |b|. Returns an IPAddress containing the result of the operation.
106 // It is an error if |this| and |b| are not of the same address family
107 // or if either are not valid,
108 IPAddress MaskWith(const IPAddress &b);
109
Paul Stewartfe1c0e12012-04-30 19:57:04 -0700110 // Perform an OR operation between the address data of |this| and that
111 // of |b|. Returns an IPAddress containing the result of the operation.
112 // It is an error if |this| and |b| are not of the same address family
113 // or if either are not valid,
114 IPAddress MergeWith(const IPAddress &b);
115
Paul Stewartf7bf9bf2012-04-17 17:30:14 -0700116 // Return an address that represents the network-part of the address,
117 // i.e, the address with all but the prefix bits masked out.
118 IPAddress GetNetworkPart();
119
Paul Stewartfe1c0e12012-04-30 19:57:04 -0700120 // Return the default broadcast address for the IP address, by setting
121 // all of the host-part bits to 1.
122 IPAddress GetDefaultBroadcast();
123
Paul Stewartf7bf9bf2012-04-17 17:30:14 -0700124 // Tests whether this IPAddress is able to directly access the address
125 // |b| without an intervening gateway. It tests whether the network
126 // part of |b| is the same as the network part of |this|, using the
127 // prefix of |this|. Returns true if |b| is reachable, false otherwise.
128 bool CanReachAddress(const IPAddress &b);
129
Paul Stewart1d18e8c2011-07-15 11:00:31 -0700130 private:
131 Family family_;
132 ByteString address_;
Paul Stewart9e3fcd72011-08-26 15:46:16 -0700133 unsigned int prefix_;
134 // NO DISALLOW_COPY_AND_ASSIGN -- we assign IPAddresses in STL datatypes
Paul Stewart1d18e8c2011-07-15 11:00:31 -0700135};
136
137} // namespace shill
138
139#endif // SHILL_IP_ADDRESS_