blob: 1aa1111bba687f6d8767543b0bf0d4e9eceedbdb [file] [log] [blame]
Paul Stewart188a84a2012-01-20 16:28:15 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Paul Stewart1d18e8c2011-07-15 11:00:31 -07002// 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 Masone2aa97072011-08-09 17:35:08 -07007#include <arpa/inet.h>
Paul Stewart1d18e8c2011-07-15 11:00:31 -07008#include <netinet/in.h>
9
10#include <string>
11
12#include "shill/byte_string.h"
13
Paul Stewart188a84a2012-01-20 16:28:15 -080014using std::string;
15
Paul Stewart1d18e8c2011-07-15 11:00:31 -070016namespace shill {
17
Chris Masone2aa97072011-08-09 17:35:08 -070018// static
Paul Stewart7355ce12011-09-02 10:47:01 -070019const IPAddress::Family IPAddress::kFamilyUnknown = AF_UNSPEC;
Chris Masone2aa97072011-08-09 17:35:08 -070020// static
Paul Stewart7355ce12011-09-02 10:47:01 -070021const IPAddress::Family IPAddress::kFamilyIPv4 = AF_INET;
Chris Masone2aa97072011-08-09 17:35:08 -070022// static
Paul Stewart7355ce12011-09-02 10:47:01 -070023const IPAddress::Family IPAddress::kFamilyIPv6 = AF_INET6;
Chris Masone2aa97072011-08-09 17:35:08 -070024
Paul Stewart1d18e8c2011-07-15 11:00:31 -070025IPAddress::IPAddress(Family family, const ByteString &address)
26 : family_(family) ,
Paul Stewart9e3fcd72011-08-26 15:46:16 -070027 address_(address),
28 prefix_(0) {}
29
30IPAddress::IPAddress(Family family,
31 const ByteString &address,
32 unsigned int prefix)
33 : family_(family) ,
34 address_(address),
35 prefix_(prefix) {}
Paul Stewart1d18e8c2011-07-15 11:00:31 -070036
37IPAddress::IPAddress(Family family)
Paul Stewart9e3fcd72011-08-26 15:46:16 -070038 : family_(family),
39 prefix_(0) {}
Paul Stewart1d18e8c2011-07-15 11:00:31 -070040
41IPAddress::~IPAddress() {}
42
Eric Shienbroodc74cf9c2012-03-02 15:00:35 -050043size_t IPAddress::GetAddressLength(Family family) {
Paul Stewart1d18e8c2011-07-15 11:00:31 -070044 switch (family) {
Paul Stewart7355ce12011-09-02 10:47:01 -070045 case kFamilyIPv4:
Paul Stewart1d18e8c2011-07-15 11:00:31 -070046 return sizeof(in_addr);
Paul Stewart7355ce12011-09-02 10:47:01 -070047 case kFamilyIPv6:
Paul Stewart1d18e8c2011-07-15 11:00:31 -070048 return sizeof(in6_addr);
49 default:
50 return 0;
51 }
52}
53
Paul Stewart188a84a2012-01-20 16:28:15 -080054bool IPAddress::SetAddressFromString(const string &address_string) {
Eric Shienbroodc74cf9c2012-03-02 15:00:35 -050055 size_t address_length = GetAddressLength(family_);
Paul Stewart1d18e8c2011-07-15 11:00:31 -070056
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
69void IPAddress::SetAddressToDefault() {
70 address_ = ByteString(GetAddressLength(family_));
71}
72
mukesh agrawal2c15d2c2012-02-21 16:09:21 -080073bool IPAddress::IntoString(string *address_string) const {
Paul Stewart188a84a2012-01-20 16:28:15 -080074 // 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 agrawal2c15d2c2012-02-21 16:09:21 -080084string IPAddress::ToString() const {
85 string out("<unknown>");
86 IntoString(&out);
87 return out;
88}
89
Paul Stewart1d18e8c2011-07-15 11:00:31 -070090} // namespace shill