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 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());
34 AddressList::AddressList() {}
36 AddressList::~AddressList() {}
38 AddressList::AddressList(const IPEndPoint
& endpoint
) {
43 AddressList
AddressList::CreateFromIPAddress(const IPAddressNumber
& address
,
45 return AddressList(IPEndPoint(address
, port
));
49 AddressList
AddressList::CreateFromIPAddressList(
50 const IPAddressList
& addresses
,
51 const std::string
& canonical_name
) {
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));
62 AddressList
AddressList::CreateFromAddrinfo(const struct addrinfo
* head
) {
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
) {
69 // NOTE: Ignoring non-INET* families.
70 if (ipe
.FromSockAddr(ai
->ai_addr
, ai
->ai_addrlen
))
73 DLOG(WARNING
) << "Unknown family found in addrinfo: " << ai
->ai_family
;
79 AddressList
AddressList::CopyWithPort(const AddressList
& list
, uint16_t port
) {
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
));
87 void AddressList::SetDefaultCanonicalName() {
89 set_canonical_name(front().ToStringWithoutPort());
92 NetLog::ParametersCallback
AddressList::CreateNetLogCallback() const {
93 return base::Bind(&NetLogAddressListCallback
, this);