1 //
2 // Copyright (C) 2012 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #ifndef SHILL_NET_IP_ADDRESS_H_
18 #define SHILL_NET_IP_ADDRESS_H_
19 
20 #include <netinet/in.h>
21 
22 #include <string>
23 
24 #include "shill/net/byte_string.h"
25 #include "shill/net/shill_export.h"
26 
27 namespace shill {
28 
29 class SHILL_EXPORT IPAddress {
30  public:
31   typedef unsigned char Family;
32   static const Family kFamilyUnknown;
33   static const Family kFamilyIPv4;
34   static const Family kFamilyIPv6;
35   static const char kFamilyNameUnknown[];
36   static const char kFamilyNameIPv4[];
37   static const char kFamilyNameIPv6[];
38 
39   explicit IPAddress(Family family);
40   // Constructs an IPAddress object given a standard string representation of an
41   // IP address (e.g. "192.144.30.54").
42   explicit IPAddress(std::string ip_string);
43 
44   // Constructs an IPAddress object from a sockaddr_in or sockaddr_in6
45   // structure, depending on the family specified in |address_struct|.  |size|
46   // specifies the actual size of the structure backing |address_struct|.
47   explicit IPAddress(const sockaddr* address_struct, size_t size);
48 
49   IPAddress(Family family, const ByteString& address);
50   IPAddress(Family family, const ByteString& address, unsigned int prefix);
51   ~IPAddress();
52 
53   // Since this is a copyable datatype...
IPAddress(const IPAddress & b)54   IPAddress(const IPAddress& b)
55     : family_(b.family_),
56       address_(b.address_),
57       prefix_(b.prefix_) {}
58   IPAddress& operator=(const IPAddress& b) {
59     family_ = b.family_;
60     address_ = b.address_;
61     prefix_ = b.prefix_;
62     return *this;
63   }
64 
65   // Static utilities
66   // Get the length in bytes of addresses of the given family
67   static size_t GetAddressLength(Family family);
68 
69   // Returns the maximum prefix length for address family |family|, i.e.,
70   // the length of this address type in bits.
71   static size_t GetMaxPrefixLength(Family family);
72 
73   // Provides a guideline for the minimum sensible prefix for this IP
74   // address.  As opposed to GetMaxPrefixLength() above, this function
75   // takes into account the class of this IP address to determine the
76   // smallest prefix that makes sense for this class of address to have.
77   // Since this function uses classful (pre-CIDR) rules to perform this
78   // estimate, this is not an absolute rule and others methods like
79   // IsValid() do not consider this a criteria.  It is only useful for
80   // making guesses as to the mimimal plausible prefix that might be
81   // viable for an address when the supplied prefix is obviously incorrect.
82   size_t GetMinPrefixLength() const;
83 
84   // Returns the prefix length given an address |family| and a |mask|. For
85   // example, returns 24 for an IPv4 mask 255.255.255.0.
86   static size_t GetPrefixLengthFromMask(Family family, const std::string& mask);
87 
88   // Returns an IPAddress of type |family| that has all the high-order |prefix|
89   // bits set.
90   static IPAddress GetAddressMaskFromPrefix(Family family, size_t prefix);
91 
92   // Returns the name of an address family.
93   static std::string GetAddressFamilyName(Family family);
94 
95   // Getters and Setters
family()96   Family family() const { return family_; }
set_family(Family family)97   void set_family(Family family) { family_ = family; }
address()98   const ByteString& address() const { return address_; }
prefix()99   unsigned int prefix() const { return prefix_; }
set_prefix(unsigned int prefix)100   void set_prefix(unsigned int prefix) { prefix_ = prefix; }
GetConstData()101   const unsigned char* GetConstData() const { return address_.GetConstData(); }
GetLength()102   size_t GetLength() const { return address_.GetLength(); }
IsDefault()103   bool IsDefault() const { return address_.IsZero(); }
IsValid()104   bool IsValid() const {
105     return family_ != kFamilyUnknown &&
106         GetLength() == GetAddressLength(family_);
107   }
108 
109   // Parse an IP address string.
110   bool SetAddressFromString(const std::string& address_string);
111   // Parse an "address/prefix" IP address and prefix pair from a string.
112   bool SetAddressAndPrefixFromString(const std::string& address_string);
113   // An uninitialized IPAddress is empty and invalid when constructed.
114   // Use SetAddressToDefault() to set it to the default or "all-zeroes" address.
115   void SetAddressToDefault();
116   // Return the string equivalent of the address.  Returns true if the
117   // conversion succeeds in which case |address_string| is set to the
118   // result.  Otherwise the function returns false and |address_string|
119   // is left unmodified.
120   bool IntoString(std::string* address_string) const;
121   // Similar to IntoString, but returns by value. Convenient for logging.
122   std::string ToString() const;
123 
124   // Populates the address and family portion of a sockaddr_in or
125   // sockaddr_in6 structure, depending on the IPAddress family.  Returns true
126   // if the specified |size| is large enough to accommodate the address family,
127   // and a valid address and family are written to the structure.  Otherwise,
128   // false is returned and the memory at |address_struct| is unmodified.
129   bool IntoSockAddr(sockaddr* address_struct, size_t size) const;
130 
131   // Returns whether |b| has the same family, address and prefix as |this|.
132   bool Equals(const IPAddress& b) const;
133 
134   // Returns whether |b| has the same family and address as |this|.
135   bool HasSameAddressAs(const IPAddress& b) const;
136 
137   // Perform an AND operation between the address data of |this| and that
138   // of |b|.  Returns an IPAddress containing the result of the operation.
139   // It is an error if |this| and |b| are not of the same address family
140   // or if either are not valid,
141   IPAddress MaskWith(const IPAddress& b) const;
142 
143   // Perform an OR operation between the address data of |this| and that
144   // of |b|.  Returns an IPAddress containing the result of the operation.
145   // It is an error if |this| and |b| are not of the same address family
146   // or if either are not valid,
147   IPAddress MergeWith(const IPAddress& b) const;
148 
149   // Return an address that represents the network-part of the address,
150   // i.e, the address with all but the prefix bits masked out.
151   IPAddress GetNetworkPart() const;
152 
153   // Return the default broadcast address for the IP address, by setting
154   // all of the host-part bits to 1.
155   IPAddress GetDefaultBroadcast();
156 
157   // Tests whether this IPAddress is able to directly access the address
158   // |b| without an intervening gateway.  It tests whether the network
159   // part of |b| is the same as the network part of |this|, using the
160   // prefix of |this|.  Returns true if |b| is reachable, false otherwise.
161   bool CanReachAddress(const IPAddress& b) const;
162 
163  private:
164   Family family_;
165   ByteString address_;
166   unsigned int prefix_;
167   // NO DISALLOW_COPY_AND_ASSIGN -- we assign IPAddresses in STL datatypes
168 };
169 
170 }  // namespace shill
171 
172 #endif  // SHILL_NET_IP_ADDRESS_H_
173