summaryrefslogtreecommitdiffstats
path: root/net/dns/dns_client.cc
blob: de60cc33fee98c30b3c7844035b1bbb3443bacf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/dns/dns_client.h"

#include "base/bind.h"
#include "base/string_piece.h"
#include "net/base/net_errors.h"
#include "net/dns/dns_response.h"
#include "net/dns/dns_session.h"
#include "net/dns/dns_transaction.h"
#include "net/socket/client_socket_factory.h"

namespace net {

DnsClient::Request::Request(const base::StringPiece& qname,
                            uint16 qtype,
                            const RequestCallback& callback)
    : qname_(qname.data(), qname.size()),
      qtype_(qtype),
      callback_(callback) {
}

DnsClient::Request::~Request() {}

// Implementation of DnsClient that uses DnsTransaction to serve requests.
class DnsClientImpl : public DnsClient {
 public:
  class RequestImpl : public Request {
   public:
    RequestImpl(const base::StringPiece& qname,
                uint16 qtype,
                const RequestCallback& callback,
                DnsSession* session,
                const BoundNetLog& net_log)
        : Request(qname, qtype, callback),
          session_(session),
          net_log_(net_log) {
    }

    virtual int Start() OVERRIDE {
      transaction_.reset(new DnsTransaction(
          session_,
          qname(),
          qtype(),
          base::Bind(&RequestImpl::OnComplete, base::Unretained(this)),
          net_log_));
      return transaction_->Start();
    }

    void OnComplete(DnsTransaction* transaction, int rv) {
      DCHECK_EQ(transaction_.get(), transaction);
      // TODO(szym):
      // - handle retransmissions here instead of DnsTransaction
      // - handle rcode and flags here instead of DnsTransaction
      // - update RTT in DnsSession
      // - perform suffix search
      // - handle DNS over TCP
      DoCallback(rv, (rv == OK) ? transaction->response() : NULL);
    }

   private:
    scoped_refptr<DnsSession> session_;
    BoundNetLog net_log_;
    scoped_ptr<DnsTransaction> transaction_;
  };

  explicit DnsClientImpl(DnsSession* session) {
    session_ = session;
  }

  virtual Request* CreateRequest(
      const base::StringPiece& qname,
      uint16 qtype,
      const RequestCallback& callback,
      const BoundNetLog& source_net_log) OVERRIDE {
    return new RequestImpl(qname, qtype, callback, session_, source_net_log);
  }

 private:
  scoped_refptr<DnsSession> session_;
};

// static
DnsClient* DnsClient::CreateClient(DnsSession* session) {
  return new DnsClientImpl(session);
}

}  // namespace net