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.cc b/ip_address.cc
new file mode 100644
index 0000000..9d300bd
--- /dev/null
+++ b/ip_address.cc
@@ -0,0 +1,54 @@
+// 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.
+
+#include "shill/ip_address.h"
+
+#include <netinet/in.h>
+
+#include <string>
+
+#include "shill/byte_string.h"
+
+namespace shill {
+
+IPAddress::IPAddress(Family family, const ByteString &address)
+ : family_(family) ,
+ address_(address) {}
+
+IPAddress::IPAddress(Family family)
+ : family_(family) {}
+
+IPAddress::~IPAddress() {}
+
+int IPAddress::GetAddressLength(Family family) {
+ switch (family) {
+ case kAddressFamilyIPv4:
+ return sizeof(in_addr);
+ case kAddressFamilyIPv6:
+ return sizeof(in6_addr);
+ default:
+ return 0;
+ }
+}
+
+bool IPAddress::SetAddressFromString(const std::string &address_string) {
+ int address_length = GetAddressLength(family_);
+
+ if (!address_length) {
+ return false;
+ }
+
+ ByteString address(address_length);
+ if (inet_pton(family_, address_string.c_str(), address.GetData()) <= 0) {
+ return false;
+ }
+ address_ = address;
+ return true;
+}
+
+void IPAddress::SetAddressToDefault() {
+ address_ = ByteString(GetAddressLength(family_));
+}
+
+} // namespace shill