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.
4
5 #include "net/base/address_list.h"
6
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"
12
13 namespace net {
14
15 namespace {
16
NetLogAddressListCallback(const AddressList * address_list,NetLog::LogLevel log_level)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();
21
22 for (AddressList::const_iterator it = address_list->begin();
23 it != address_list->end(); ++it) {
24 list->Append(new base::StringValue(it->ToString()));
25 }
26
27 dict->Set("address_list", list);
28 return dict;
29 }
30
31 } // namespace
32
AddressList()33 AddressList::AddressList() {}
34
~AddressList()35 AddressList::~AddressList() {}
36
AddressList(const IPEndPoint & endpoint)37 AddressList::AddressList(const IPEndPoint& endpoint) {
38 push_back(endpoint);
39 }
40
41 // static
CreateFromIPAddress(const IPAddressNumber & address,uint16 port)42 AddressList AddressList::CreateFromIPAddress(const IPAddressNumber& address,
43 uint16 port) {
44 return AddressList(IPEndPoint(address, port));
45 }
46
47 // static
CreateFromIPAddressList(const IPAddressList & addresses,const std::string & canonical_name)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));
56 }
57 return list;
58 }
59
60 // static
CreateFromAddrinfo(const struct addrinfo * head)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;
73 }
74 return list;
75 }
76
77 // static
CopyWithPort(const AddressList & list,uint16 port)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;
84 }
85
SetDefaultCanonicalName()86 void AddressList::SetDefaultCanonicalName() {
87 DCHECK(!empty());
88 set_canonical_name(front().ToStringWithoutPort());
89 }
90
CreateNetLogCallback() const91 NetLog::ParametersCallback AddressList::CreateNetLogCallback() const {
92 return base::Bind(&NetLogAddressListCallback, this);
93 }
94
95 } // namespace net
96