Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 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 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 7 | #include <arpa/inet.h> |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 8 | #include <netinet/in.h> |
| 9 | |
| 10 | #include <string> |
| 11 | |
| 12 | #include "shill/byte_string.h" |
| 13 | |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 14 | using std::string; |
| 15 | |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 16 | namespace shill { |
| 17 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 18 | // static |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 19 | const IPAddress::Family IPAddress::kFamilyUnknown = AF_UNSPEC; |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 20 | // static |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 21 | const IPAddress::Family IPAddress::kFamilyIPv4 = AF_INET; |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 22 | // static |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 23 | const IPAddress::Family IPAddress::kFamilyIPv6 = AF_INET6; |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 24 | |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 25 | IPAddress::IPAddress(Family family, const ByteString &address) |
| 26 | : family_(family) , |
Paul Stewart | 9e3fcd7 | 2011-08-26 15:46:16 -0700 | [diff] [blame] | 27 | address_(address), |
| 28 | prefix_(0) {} |
| 29 | |
| 30 | IPAddress::IPAddress(Family family, |
| 31 | const ByteString &address, |
| 32 | unsigned int prefix) |
| 33 | : family_(family) , |
| 34 | address_(address), |
| 35 | prefix_(prefix) {} |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 36 | |
| 37 | IPAddress::IPAddress(Family family) |
Paul Stewart | 9e3fcd7 | 2011-08-26 15:46:16 -0700 | [diff] [blame] | 38 | : family_(family), |
| 39 | prefix_(0) {} |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 40 | |
| 41 | IPAddress::~IPAddress() {} |
| 42 | |
| 43 | int IPAddress::GetAddressLength(Family family) { |
| 44 | switch (family) { |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 45 | case kFamilyIPv4: |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 46 | return sizeof(in_addr); |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 47 | case kFamilyIPv6: |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 48 | return sizeof(in6_addr); |
| 49 | default: |
| 50 | return 0; |
| 51 | } |
| 52 | } |
| 53 | |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 54 | bool IPAddress::SetAddressFromString(const string &address_string) { |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 55 | int address_length = GetAddressLength(family_); |
| 56 | |
| 57 | if (!address_length) { |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | ByteString address(address_length); |
| 62 | if (inet_pton(family_, address_string.c_str(), address.GetData()) <= 0) { |
| 63 | return false; |
| 64 | } |
| 65 | address_ = address; |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | void IPAddress::SetAddressToDefault() { |
| 70 | address_ = ByteString(GetAddressLength(family_)); |
| 71 | } |
| 72 | |
mukesh agrawal | 2c15d2c | 2012-02-21 16:09:21 -0800 | [diff] [blame] | 73 | bool IPAddress::IntoString(string *address_string) const { |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 74 | // Noting that INET6_ADDRSTRLEN > INET_ADDRSTRLEN |
| 75 | char address_buf[INET6_ADDRSTRLEN]; |
| 76 | if (GetLength() != GetAddressLength(family_) || |
| 77 | !inet_ntop(family_, GetConstData(), address_buf, sizeof(address_buf))) { |
| 78 | return false; |
| 79 | } |
| 80 | *address_string = address_buf; |
| 81 | return true; |
| 82 | } |
| 83 | |
mukesh agrawal | 2c15d2c | 2012-02-21 16:09:21 -0800 | [diff] [blame] | 84 | string IPAddress::ToString() const { |
| 85 | string out("<unknown>"); |
| 86 | IntoString(&out); |
| 87 | return out; |
| 88 | } |
| 89 | |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 90 | } // namespace shill |