Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(156)

Side by Side Diff: remoting/host/gnubby_advertiser.cc

Issue 138753005: Add gnubby authentication to remoting host (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 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 "remoting/host/gnubby_advertiser.h"
6
7 #include <string>
8
9 #include "base/bind.h"
10 #include "base/strings/stringprintf.h"
11 #include "net/base/address_list.h"
12 #include "net/base/io_buffer.h"
13 #include "net/base/ip_endpoint.h"
14 #include "net/base/net_errors.h"
15 #include "net/base/net_util.h"
16 #include "remoting/base/logging.h"
17
18 namespace {
19
20 const int kLocalGnubbydPort = 1817;
21 const char kGnubbydProxyHost[] = "localhost";
22 const char kLocalGnubbydAddress[] = "127.0.0.1";
23
24 } // namespace
25
26 namespace remoting {
27
28 GnubbyAdvertiser::GnubbyAdvertiser(
29 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner,
Sergey Ulanov 2014/01/25 02:03:33 It looks like you are using network_task_runner ju
psj 2014/01/29 09:07:15 Done.
30 net::ClientSocketFactory* client_socket_factory)
31 : network_task_runner_(network_task_runner),
32 client_socket_factory_(client_socket_factory),
33 proxy_port_(0) {
34
35 DCHECK(network_task_runner_->BelongsToCurrentThread());
36 DCHECK(client_socket_factory_);
37
38 in_buffer_ = new net::IOBufferWithSize(64);
39 }
40
41 void GnubbyAdvertiser::Advertise(int proxy_port) {
42 DCHECK(network_task_runner_->BelongsToCurrentThread());
43 proxy_port_ = proxy_port;
44
45 // Increment reference to ensure object isn't deleted before final callback.
46 AddRef();
47
48 net::IPAddressNumber gnubbyd_ip;
49 if (!net::ParseIPLiteralToNumber(kLocalGnubbydAddress, &gnubbyd_ip)) {
50 Close(net::ERR_FAILED);
51 }
52
53 net::AddressList addresses(net::IPEndPoint(gnubbyd_ip, kLocalGnubbydPort));
54 net::NetLog::Source source;
55
56 socket_ = client_socket_factory_->CreateTransportClientSocket(
57 addresses, NULL, source);
58 int result = socket_->Connect(
59 base::Bind(&GnubbyAdvertiser::OnConnect, base::Unretained(this)));
60 if (result != net::ERR_IO_PENDING) {
61 OnConnect(result);
62 }
63 }
64
65 GnubbyAdvertiser::~GnubbyAdvertiser() {}
66
67 void GnubbyAdvertiser::Close(int result) {
68 DCHECK(network_task_runner_->BelongsToCurrentThread());
69
70 socket_->Disconnect();
71 // All done. Decrement reference to balance the increment in Advertise.
72 Release();
73 }
74
75 void GnubbyAdvertiser::OnConnect(int result) {
76 DCHECK(network_task_runner_->BelongsToCurrentThread());
77
78 if (result == net::OK) {
79 Write();
80 } else {
81 HOST_LOG << "Failed to connect to gnubbyd: " << result;
82 Close(result);
83 }
84 }
85
86 void GnubbyAdvertiser::Write() {
87 DCHECK(network_task_runner_->BelongsToCurrentThread());
88
89 std::string listener =
90 base::StringPrintf("%s: %d\r\n\r\n", kGnubbydProxyHost, proxy_port_);
91 HOST_LOG << "Advertising [" << listener << "] with local gnubbyd";
92
93 scoped_refptr<net::StringIOBuffer> buffer = new net::StringIOBuffer(listener);
94 int result = socket_->Write(
95 buffer.get(),
96 buffer->size(),
97 base::Bind(&GnubbyAdvertiser::OnWrite, base::Unretained(this)));
98 if (result != net::ERR_IO_PENDING) {
99 OnWrite(result);
100 }
101 }
102
103 void GnubbyAdvertiser::OnWrite(int result) {
104 DCHECK(network_task_runner_->BelongsToCurrentThread());
105
106 if (result > 0) {
107 Read();
108 } else {
109 if (result < 0) {
110 HOST_LOG << "Failed to write to gnubbyd: " << result;
111 }
112 Close(result);
113 }
114 }
115
116 void GnubbyAdvertiser::Read() {
117 DCHECK(network_task_runner_->BelongsToCurrentThread());
118
119 int result = socket_->Read(
120 in_buffer_.get(),
121 in_buffer_->size(),
122 base::Bind(&GnubbyAdvertiser::OnRead, base::Unretained(this)));
123 if (result != net::ERR_IO_PENDING) {
124 OnRead(result);
125 }
126 }
127
128 void GnubbyAdvertiser::OnRead(int result) {
129 DCHECK(network_task_runner_->BelongsToCurrentThread());
130
131 // The advertiser is done once a reply has been received.
132 Close(result);
133 }
134
135 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698