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"
8 #include "base/logging.h"
9 #include "base/values.h"
10 #include "net/base/net_util.h"
11 #include "net/base/sys_addrinfo.h"
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
);
33 AddressList::AddressList() {}
35 AddressList::~AddressList() {}
37 AddressList::AddressList(const IPEndPoint
& endpoint
) {
42 AddressList
AddressList::CreateFromIPAddress(const IPAddressNumber
& address
,
44 return AddressList(IPEndPoint(address
, port
));
48 AddressList
AddressList::CreateFromIPAddressList(
49 const IPAddressList
& addresses
,
50 const std::string
& canonical_name
) {
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));
61 AddressList
AddressList::CreateFromAddrinfo(const struct addrinfo
* head
) {
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
) {
68 // NOTE: Ignoring non-INET* families.
69 if (ipe
.FromSockAddr(ai
->ai_addr
, ai
->ai_addrlen
))
72 DLOG(WARNING
) << "Unknown family found in addrinfo: " << ai
->ai_family
;
78 AddressList
AddressList::CopyWithPort(const AddressList
& list
, uint16 port
) {
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
));
86 void AddressList::SetDefaultCanonicalName() {
88 set_canonical_name(front().ToStringWithoutPort());
91 NetLog::ParametersCallback
AddressList::CreateNetLogCallback() const {
92 return base::Bind(&NetLogAddressListCallback
, this);