Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 1 | // 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 | #include "shill/ip_address.h" |
| 6 | |
| 7 | #include <netinet/in.h> |
| 8 | |
| 9 | #include <string> |
| 10 | |
| 11 | #include "shill/byte_string.h" |
| 12 | |
| 13 | namespace shill { |
| 14 | |
| 15 | IPAddress::IPAddress(Family family, const ByteString &address) |
| 16 | : family_(family) , |
| 17 | address_(address) {} |
| 18 | |
| 19 | IPAddress::IPAddress(Family family) |
| 20 | : family_(family) {} |
| 21 | |
| 22 | IPAddress::~IPAddress() {} |
| 23 | |
| 24 | int IPAddress::GetAddressLength(Family family) { |
| 25 | switch (family) { |
| 26 | case kAddressFamilyIPv4: |
| 27 | return sizeof(in_addr); |
| 28 | case kAddressFamilyIPv6: |
| 29 | return sizeof(in6_addr); |
| 30 | default: |
| 31 | return 0; |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | bool IPAddress::SetAddressFromString(const std::string &address_string) { |
| 36 | int address_length = GetAddressLength(family_); |
| 37 | |
| 38 | if (!address_length) { |
| 39 | return false; |
| 40 | } |
| 41 | |
| 42 | ByteString address(address_length); |
| 43 | if (inet_pton(family_, address_string.c_str(), address.GetData()) <= 0) { |
| 44 | return false; |
| 45 | } |
| 46 | address_ = address; |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | void IPAddress::SetAddressToDefault() { |
| 51 | address_ = ByteString(GetAddressLength(family_)); |
| 52 | } |
| 53 | |
| 54 | } // namespace shill |