shill: Add IPAddress class
Add class that holds IP Addresses. Routing will need these.
Move address family constants out of IPConfig to this new class.
BUG=chromium-os:17277
TEST=New unittest
Change-Id: I69d9c4d551061de890ed6919462597a15ae51857
Reviewed-on: http://gerrit.chromium.org/gerrit/4180
Reviewed-by: Darin Petkov <petkov@chromium.org>
Tested-by: Paul Stewart <pstew@chromium.org>
diff --git a/ip_address.h b/ip_address.h
new file mode 100644
index 0000000..1677ca9
--- /dev/null
+++ b/ip_address.h
@@ -0,0 +1,61 @@
+// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef SHILL_IP_ADDRESS_
+#define SHILL_IP_ADDRESS_
+
+#include <arpa/inet.h>
+
+#include <string>
+
+#include "shill/byte_string.h"
+
+namespace shill {
+
+class IPAddress {
+ public:
+ enum Family {
+ kAddressFamilyUnknown,
+ kAddressFamilyIPv4 = AF_INET,
+ kAddressFamilyIPv6 = AF_INET6
+ };
+
+ explicit IPAddress(Family family);
+ IPAddress(Family family, const ByteString &address);
+ ~IPAddress();
+
+ // Static utilities
+ // Get the length in bytes of addresses of the given family
+ static int GetAddressLength(Family family);
+
+ // Getters and Setters
+ Family family() const { return family_; }
+ const ByteString &address() const { return address_; }
+ const unsigned char *GetConstData() const { return address_.GetConstData(); }
+ int GetLength() const { return address_.GetLength(); }
+ bool IsDefault() const { return address_.IsZero(); }
+ bool IsValid() const {
+ return family_ != kAddressFamilyUnknown &&
+ GetLength() == GetAddressLength(family_);
+ }
+
+ // Parse an IP address string
+ bool SetAddressFromString(const std::string &address_string);
+ // An uninitialized IPAddress is empty and invalid when constructed.
+ // Use SetAddressToDefault() to set it to the default or "all-zeroes" address.
+ void SetAddressToDefault();
+
+ bool Equals(const IPAddress &b) const {
+ return family_ == b.family_ && address_.Equals(b.address_);
+ }
+
+ private:
+ Family family_;
+ ByteString address_;
+ DISALLOW_COPY_AND_ASSIGN(IPAddress);
+};
+
+} // namespace shill
+
+#endif // SHILL_IP_ADDRESS_