Update V8 to version 4.7.42.
[chromium-blink-merge.git] / net / base / address_list.cc
blob98704edd3c3ec8f9afab6223c15f2a54527bdaf9
1 // Copyright (c) 2012 The Chromium 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.
5 #include "net/base/address_list.h"
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/values.h"
10 #include "net/base/net_util.h"
11 #include "net/base/sys_addrinfo.h"
13 namespace net {
15 namespace {
17 scoped_ptr<base::Value> NetLogAddressListCallback(
18 const AddressList* address_list,
19 NetLogCaptureMode capture_mode) {
20 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
21 scoped_ptr<base::ListValue> list(new base::ListValue());
23 for (AddressList::const_iterator it = address_list->begin();
24 it != address_list->end(); ++it) {
25 list->Append(new base::StringValue(it->ToString()));
28 dict->Set("address_list", list.Pass());
29 return dict.Pass();
32 } // namespace
34 AddressList::AddressList() {}
36 AddressList::~AddressList() {}
38 AddressList::AddressList(const IPEndPoint& endpoint) {
39 push_back(endpoint);
42 // static
43 AddressList AddressList::CreateFromIPAddress(const IPAddressNumber& address,
44 uint16_t port) {
45 return AddressList(IPEndPoint(address, port));
48 // static
49 AddressList AddressList::CreateFromIPAddressList(
50 const IPAddressList& addresses,
51 const std::string& canonical_name) {
52 AddressList list;
53 list.set_canonical_name(canonical_name);
54 for (IPAddressList::const_iterator iter = addresses.begin();
55 iter != addresses.end(); ++iter) {
56 list.push_back(IPEndPoint(*iter, 0));
58 return list;
61 // static
62 AddressList AddressList::CreateFromAddrinfo(const struct addrinfo* head) {
63 DCHECK(head);
64 AddressList list;
65 if (head->ai_canonname)
66 list.set_canonical_name(std::string(head->ai_canonname));
67 for (const struct addrinfo* ai = head; ai; ai = ai->ai_next) {
68 IPEndPoint ipe;
69 // NOTE: Ignoring non-INET* families.
70 if (ipe.FromSockAddr(ai->ai_addr, ai->ai_addrlen))
71 list.push_back(ipe);
72 else
73 DLOG(WARNING) << "Unknown family found in addrinfo: " << ai->ai_family;
75 return list;
78 // static
79 AddressList AddressList::CopyWithPort(const AddressList& list, uint16_t port) {
80 AddressList out;
81 out.set_canonical_name(list.canonical_name());
82 for (size_t i = 0; i < list.size(); ++i)
83 out.push_back(IPEndPoint(list[i].address(), port));
84 return out;
87 void AddressList::SetDefaultCanonicalName() {
88 DCHECK(!empty());
89 set_canonical_name(front().ToStringWithoutPort());
92 NetLog::ParametersCallback AddressList::CreateNetLogCallback() const {
93 return base::Bind(&NetLogAddressListCallback, this);
96 } // namespace net