blob: 927544acc25954711c8693be2984b32e335c7f6e [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
Peter Qiu8d6b5972014-10-28 15:33:34 -07005#ifndef SHILL_NET_IP_ADDRESS_H_
6#define SHILL_NET_IP_ADDRESS_H_
Paul Stewart1d18e8c2011-07-15 11:00:31 -07007
Paul Stewart1d18e8c2011-07-15 11:00:31 -07008#include <string>
9
Peter Qiu8d6b5972014-10-28 15:33:34 -070010#include "shill/net/byte_string.h"
Peter Qiuba24e6f2014-11-17 10:26:35 -080011#include "shill/shill_export.h"
Paul Stewart1d18e8c2011-07-15 11:00:31 -070012
13namespace shill {
14
Peter Qiuba24e6f2014-11-17 10:26:35 -080015class SHILL_EXPORT IPAddress {
Paul Stewart1d18e8c2011-07-15 11:00:31 -070016 public:
Paul Stewartdd7df792011-07-15 11:09:50 -070017 typedef unsigned char Family;
Paul Stewart7355ce12011-09-02 10:47:01 -070018 static const Family kFamilyUnknown;
19 static const Family kFamilyIPv4;
20 static const Family kFamilyIPv6;
Paul Stewartf748a362012-03-07 12:01:20 -080021 static const char kFamilyNameUnknown[];
22 static const char kFamilyNameIPv4[];
23 static const char kFamilyNameIPv6[];
Paul Stewart1d18e8c2011-07-15 11:00:31 -070024
25 explicit IPAddress(Family family);
Samuel Tanda775de2014-08-12 18:41:31 -070026 // Constructs an IPAdress object given a standard string representation of an
27 // IP address (e.g. "192.144.30.54").
28 explicit IPAddress(std::string ip_string);
Samuel Tanc863d062014-08-14 17:52:16 -070029
Paul Stewart1d18e8c2011-07-15 11:00:31 -070030 IPAddress(Family family, const ByteString &address);
Paul Stewart9e3fcd72011-08-26 15:46:16 -070031 IPAddress(Family family, const ByteString &address, unsigned int prefix);
Paul Stewart1d18e8c2011-07-15 11:00:31 -070032 ~IPAddress();
33
Paul Stewart9e3fcd72011-08-26 15:46:16 -070034 // Since this is a copyable datatype...
35 IPAddress(const IPAddress &b)
36 : family_(b.family_),
37 address_(b.address_),
38 prefix_(b.prefix_) {}
39 IPAddress &operator=(const IPAddress &b) {
40 family_ = b.family_;
41 address_ = b.address_;
42 prefix_ = b.prefix_;
43 return *this;
44 }
45
Paul Stewart1d18e8c2011-07-15 11:00:31 -070046 // Static utilities
47 // Get the length in bytes of addresses of the given family
Eric Shienbroodc74cf9c2012-03-02 15:00:35 -050048 static size_t GetAddressLength(Family family);
Paul Stewart1d18e8c2011-07-15 11:00:31 -070049
Paul Stewart48100b02012-03-19 07:53:52 -070050 // Returns the maximum prefix length for address family |family|, i.e.,
51 // the length of this address type in bits.
52 static size_t GetMaxPrefixLength(Family family);
53
Paul Stewartf7bf9bf2012-04-17 17:30:14 -070054 // Provides a guideline for the minimum sensible prefix for this IP
55 // address. As opposed to GetMaxPrefixLength() above, this function
56 // takes into account the class of this IP address to determine the
57 // smallest prefix that makes sense for this class of address to have.
58 // Since this function uses classful (pre-CIDR) rules to perform this
59 // estimate, this is not an absolute rule and others methods like
60 // IsValid() do not consider this a criteria. It is only useful for
61 // making guesses as to the mimimal plausible prefix that might be
62 // viable for an address when the supplied prefix is obviously incorrect.
63 size_t GetMinPrefixLength() const;
64
Darin Petkov14c29ec2012-03-02 11:34:19 +010065 // Returns the prefix length given an address |family| and a |mask|. For
66 // example, returns 24 for an IPv4 mask 255.255.255.0.
Eric Shienbrood3e20a232012-02-16 11:35:56 -050067 static size_t GetPrefixLengthFromMask(Family family, const std::string &mask);
Darin Petkov14c29ec2012-03-02 11:34:19 +010068
Paul Stewartf7bf9bf2012-04-17 17:30:14 -070069 // Returns an IPAddress of type |family| that has all the high-order |prefix|
70 // bits set.
71 static IPAddress GetAddressMaskFromPrefix(Family family, size_t prefix);
72
Paul Stewartf748a362012-03-07 12:01:20 -080073 // Returns the name of an address family.
74 static std::string GetAddressFamilyName(Family family);
75
Paul Stewart1d18e8c2011-07-15 11:00:31 -070076 // Getters and Setters
77 Family family() const { return family_; }
Peter Qiuf3a8f902014-08-20 10:05:42 -070078 void set_family(Family family) { family_ = family; }
Paul Stewart1d18e8c2011-07-15 11:00:31 -070079 const ByteString &address() const { return address_; }
Paul Stewart9e3fcd72011-08-26 15:46:16 -070080 unsigned int prefix() const { return prefix_; }
81 void set_prefix(unsigned int prefix) { prefix_ = prefix; }
Paul Stewart1d18e8c2011-07-15 11:00:31 -070082 const unsigned char *GetConstData() const { return address_.GetConstData(); }
Eric Shienbroodc74cf9c2012-03-02 15:00:35 -050083 size_t GetLength() const { return address_.GetLength(); }
Paul Stewart1d18e8c2011-07-15 11:00:31 -070084 bool IsDefault() const { return address_.IsZero(); }
85 bool IsValid() const {
Paul Stewart7355ce12011-09-02 10:47:01 -070086 return family_ != kFamilyUnknown &&
Paul Stewart1d18e8c2011-07-15 11:00:31 -070087 GetLength() == GetAddressLength(family_);
88 }
89
Paul Stewartf7bf9bf2012-04-17 17:30:14 -070090 // Parse an IP address string.
Paul Stewart1d18e8c2011-07-15 11:00:31 -070091 bool SetAddressFromString(const std::string &address_string);
Paul Stewartf7bf9bf2012-04-17 17:30:14 -070092 // Parse an "address/prefix" IP address and prefix pair from a string.
93 bool SetAddressAndPrefixFromString(const std::string &address_string);
Paul Stewart1d18e8c2011-07-15 11:00:31 -070094 // An uninitialized IPAddress is empty and invalid when constructed.
95 // Use SetAddressToDefault() to set it to the default or "all-zeroes" address.
96 void SetAddressToDefault();
Paul Stewart188a84a2012-01-20 16:28:15 -080097 // Return the string equivalent of the address. Returns true if the
98 // conversion succeeds in which case |address_string| is set to the
99 // result. Otherwise the function returns false and |address_string|
100 // is left unmodified.
mukesh agrawal2c15d2c2012-02-21 16:09:21 -0800101 bool IntoString(std::string *address_string) const;
102 // Similar to IntoString, but returns by value. Convenient for logging.
103 std::string ToString() const;
Paul Stewart1d18e8c2011-07-15 11:00:31 -0700104
105 bool Equals(const IPAddress &b) const {
Paul Stewart9e3fcd72011-08-26 15:46:16 -0700106 return family_ == b.family_ && address_.Equals(b.address_) &&
107 prefix_ == b.prefix_;
Paul Stewart75e89d22011-08-01 10:00:02 -0700108 }
109
Paul Stewartf7bf9bf2012-04-17 17:30:14 -0700110 // Perform an AND 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,
Paul Stewart4a6748d2012-07-17 14:31:36 -0700114 IPAddress MaskWith(const IPAddress &b) const;
Paul Stewartf7bf9bf2012-04-17 17:30:14 -0700115
Paul Stewartfe1c0e12012-04-30 19:57:04 -0700116 // Perform an OR operation between the address data of |this| and that
117 // of |b|. Returns an IPAddress containing the result of the operation.
118 // It is an error if |this| and |b| are not of the same address family
119 // or if either are not valid,
Paul Stewart4a6748d2012-07-17 14:31:36 -0700120 IPAddress MergeWith(const IPAddress &b) const;
Paul Stewartfe1c0e12012-04-30 19:57:04 -0700121
Paul Stewartf7bf9bf2012-04-17 17:30:14 -0700122 // Return an address that represents the network-part of the address,
123 // i.e, the address with all but the prefix bits masked out.
Paul Stewart4a6748d2012-07-17 14:31:36 -0700124 IPAddress GetNetworkPart() const;
Paul Stewartf7bf9bf2012-04-17 17:30:14 -0700125
Paul Stewartfe1c0e12012-04-30 19:57:04 -0700126 // Return the default broadcast address for the IP address, by setting
127 // all of the host-part bits to 1.
128 IPAddress GetDefaultBroadcast();
129
Paul Stewartf7bf9bf2012-04-17 17:30:14 -0700130 // Tests whether this IPAddress is able to directly access the address
131 // |b| without an intervening gateway. It tests whether the network
132 // part of |b| is the same as the network part of |this|, using the
133 // prefix of |this|. Returns true if |b| is reachable, false otherwise.
Paul Stewart4a6748d2012-07-17 14:31:36 -0700134 bool CanReachAddress(const IPAddress &b) const;
Paul Stewartf7bf9bf2012-04-17 17:30:14 -0700135
Paul Stewart1d18e8c2011-07-15 11:00:31 -0700136 private:
137 Family family_;
138 ByteString address_;
Paul Stewart9e3fcd72011-08-26 15:46:16 -0700139 unsigned int prefix_;
140 // NO DISALLOW_COPY_AND_ASSIGN -- we assign IPAddresses in STL datatypes
Paul Stewart1d18e8c2011-07-15 11:00:31 -0700141};
142
143} // namespace shill
144
Peter Qiu8d6b5972014-10-28 15:33:34 -0700145#endif // SHILL_NET_IP_ADDRESS_H_