15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/dns/dns_client.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/rand_util.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_log.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/dns/address_sorter.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/dns/dns_config_service.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/dns/dns_session.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "net/dns/dns_socket_pool.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/dns/dns_transaction.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/socket/client_socket_factory.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace net {
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class DnsClientImpl : public DnsClient {
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit DnsClientImpl(NetLog* net_log)
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      : address_sorter_(AddressSorter::CreateAddressSorter()),
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        net_log_(net_log) {}
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void SetConfig(const DnsConfig& config) OVERRIDE {
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    factory_.reset();
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    session_ = NULL;
303551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    if (config.IsValid() && !config.unhandled_options) {
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ClientSocketFactory* factory = ClientSocketFactory::GetDefaultFactory();
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      scoped_ptr<DnsSocketPool> socket_pool(
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          config.randomize_ports ? DnsSocketPool::CreateDefault(factory)
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                 : DnsSocketPool::CreateNull(factory));
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      session_ = new DnsSession(config,
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                socket_pool.Pass(),
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                base::Bind(&base::RandInt),
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                net_log_);
39868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      factory_ = DnsTransactionFactory::CreateFactory(session_.get());
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual const DnsConfig* GetConfig() const OVERRIDE {
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return session_.get() ? &session_->config() : NULL;
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual DnsTransactionFactory* GetTransactionFactory() OVERRIDE {
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return session_.get() ? factory_.get() : NULL;
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual AddressSorter* GetAddressSorter() OVERRIDE {
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return address_sorter_.get();
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<DnsSession> session_;
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<DnsTransactionFactory> factory_;
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<AddressSorter> address_sorter_;
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NetLog* net_log_;
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)scoped_ptr<DnsClient> DnsClient::CreateClient(NetLog* net_log) {
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return scoped_ptr<DnsClient>(new DnsClientImpl(net_log));
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace net
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
72