blob: 763522d89c65dad1231f4522d28d36e5d5834e2a [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;
Chris Masone2aa97072011-08-09 17:35:08 -070017 static const Family kAddressFamilyUnknown;
18 static const Family kAddressFamilyIPv4;
19 static const Family kAddressFamilyIPv6;
Paul Stewart1d18e8c2011-07-15 11:00:31 -070020
21 explicit IPAddress(Family family);
22 IPAddress(Family family, const ByteString &address);
23 ~IPAddress();
24
25 // Static utilities
26 // Get the length in bytes of addresses of the given family
27 static int GetAddressLength(Family family);
28
29 // Getters and Setters
30 Family family() const { return family_; }
31 const ByteString &address() const { return address_; }
32 const unsigned char *GetConstData() const { return address_.GetConstData(); }
33 int GetLength() const { return address_.GetLength(); }
34 bool IsDefault() const { return address_.IsZero(); }
35 bool IsValid() const {
36 return family_ != kAddressFamilyUnknown &&
37 GetLength() == GetAddressLength(family_);
38 }
39
40 // Parse an IP address string
41 bool SetAddressFromString(const std::string &address_string);
42 // An uninitialized IPAddress is empty and invalid when constructed.
43 // Use SetAddressToDefault() to set it to the default or "all-zeroes" address.
44 void SetAddressToDefault();
45
46 bool Equals(const IPAddress &b) const {
47 return family_ == b.family_ && address_.Equals(b.address_);
48 }
49
Paul Stewart75e89d22011-08-01 10:00:02 -070050 void Clone(const IPAddress &b) {
51 family_ = b.family_;
52 address_ = b.address_;
53 }
54
Paul Stewart1d18e8c2011-07-15 11:00:31 -070055 private:
56 Family family_;
57 ByteString address_;
58 DISALLOW_COPY_AND_ASSIGN(IPAddress);
59};
60
61} // namespace shill
62
63#endif // SHILL_IP_ADDRESS_