blob: d69f8960a0d8887afcb47aacccdc8ff2a3ee9835 [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
Darin Petkov14c29ec2012-03-02 11:34:19 +010049 // Returns the prefix length given an address |family| and a |mask|. For
50 // example, returns 24 for an IPv4 mask 255.255.255.0.
Eric Shienbrood3e20a232012-02-16 11:35:56 -050051 static size_t GetPrefixLengthFromMask(Family family, const std::string &mask);
Darin Petkov14c29ec2012-03-02 11:34:19 +010052
Paul Stewartf748a362012-03-07 12:01:20 -080053 // Returns the name of an address family.
54 static std::string GetAddressFamilyName(Family family);
55
Paul Stewart1d18e8c2011-07-15 11:00:31 -070056 // Getters and Setters
57 Family family() const { return family_; }
58 const ByteString &address() const { return address_; }
Paul Stewart9e3fcd72011-08-26 15:46:16 -070059 unsigned int prefix() const { return prefix_; }
60 void set_prefix(unsigned int prefix) { prefix_ = prefix; }
Paul Stewart1d18e8c2011-07-15 11:00:31 -070061 const unsigned char *GetConstData() const { return address_.GetConstData(); }
Eric Shienbroodc74cf9c2012-03-02 15:00:35 -050062 size_t GetLength() const { return address_.GetLength(); }
Paul Stewart1d18e8c2011-07-15 11:00:31 -070063 bool IsDefault() const { return address_.IsZero(); }
64 bool IsValid() const {
Paul Stewart7355ce12011-09-02 10:47:01 -070065 return family_ != kFamilyUnknown &&
Paul Stewart1d18e8c2011-07-15 11:00:31 -070066 GetLength() == GetAddressLength(family_);
67 }
68
69 // Parse an IP address string
70 bool SetAddressFromString(const std::string &address_string);
71 // An uninitialized IPAddress is empty and invalid when constructed.
72 // Use SetAddressToDefault() to set it to the default or "all-zeroes" address.
73 void SetAddressToDefault();
Paul Stewart188a84a2012-01-20 16:28:15 -080074 // Return the string equivalent of the address. Returns true if the
75 // conversion succeeds in which case |address_string| is set to the
76 // result. Otherwise the function returns false and |address_string|
77 // is left unmodified.
mukesh agrawal2c15d2c2012-02-21 16:09:21 -080078 bool IntoString(std::string *address_string) const;
79 // Similar to IntoString, but returns by value. Convenient for logging.
80 std::string ToString() const;
Paul Stewart1d18e8c2011-07-15 11:00:31 -070081
82 bool Equals(const IPAddress &b) const {
Paul Stewart9e3fcd72011-08-26 15:46:16 -070083 return family_ == b.family_ && address_.Equals(b.address_) &&
84 prefix_ == b.prefix_;
Paul Stewart75e89d22011-08-01 10:00:02 -070085 }
86
Paul Stewart1d18e8c2011-07-15 11:00:31 -070087 private:
88 Family family_;
89 ByteString address_;
Paul Stewart9e3fcd72011-08-26 15:46:16 -070090 unsigned int prefix_;
91 // NO DISALLOW_COPY_AND_ASSIGN -- we assign IPAddresses in STL datatypes
Paul Stewart1d18e8c2011-07-15 11:00:31 -070092};
93
94} // namespace shill
95
96#endif // SHILL_IP_ADDRESS_