Convert ARRAYSIZE_UNSAFE -> arraysize in base/.
[chromium-blink-merge.git] / net / base / address_list.cc
blob02a762bf76a4062ef6bdaeda50150b544e59e60b
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 base::Value* NetLogAddressListCallback(const AddressList* address_list,
18 NetLog::LogLevel log_level) {
19 base::DictionaryValue* dict = new base::DictionaryValue();
20 base::ListValue* list = new base::ListValue();
22 for (AddressList::const_iterator it = address_list->begin();
23 it != address_list->end(); ++it) {
24 list->Append(new base::StringValue(it->ToString()));
27 dict->Set("address_list", list);
28 return dict;
31 } // namespace
33 AddressList::AddressList() {}
35 AddressList::~AddressList() {}
37 AddressList::AddressList(const IPEndPoint& endpoint) {
38 push_back(endpoint);
41 // static
42 AddressList AddressList::CreateFromIPAddress(const IPAddressNumber& address,
43 uint16 port) {
44 return AddressList(IPEndPoint(address, port));
47 // static
48 AddressList AddressList::CreateFromIPAddressList(
49 const IPAddressList& addresses,
50 const std::string& canonical_name) {
51 AddressList list;
52 list.set_canonical_name(canonical_name);
53 for (IPAddressList::const_iterator iter = addresses.begin();
54 iter != addresses.end(); ++iter) {
55 list.push_back(IPEndPoint(*iter, 0));
57 return list;
60 // static
61 AddressList AddressList::CreateFromAddrinfo(const struct addrinfo* head) {
62 DCHECK(head);
63 AddressList list;
64 if (head->ai_canonname)
65 list.set_canonical_name(std::string(head->ai_canonname));
66 for (const struct addrinfo* ai = head; ai; ai = ai->ai_next) {
67 IPEndPoint ipe;
68 // NOTE: Ignoring non-INET* families.
69 if (ipe.FromSockAddr(ai->ai_addr, ai->ai_addrlen))
70 list.push_back(ipe);
71 else
72 DLOG(WARNING) << "Unknown family found in addrinfo: " << ai->ai_family;
74 return list;
77 // static
78 AddressList AddressList::CopyWithPort(const AddressList& list, uint16 port) {
79 AddressList out;
80 out.set_canonical_name(list.canonical_name());
81 for (size_t i = 0; i < list.size(); ++i)
82 out.push_back(IPEndPoint(list[i].address(), port));
83 return out;
86 void AddressList::SetDefaultCanonicalName() {
87 DCHECK(!empty());
88 set_canonical_name(front().ToStringWithoutPort());
91 NetLog::ParametersCallback AddressList::CreateNetLogCallback() const {
92 return base::Bind(&NetLogAddressListCallback, this);
95 } // namespace net